-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy paththemed.component.ts
More file actions
241 lines (217 loc) · 8.11 KB
/
themed.component.ts
File metadata and controls
241 lines (217 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ComponentRef,
ElementRef,
HostBinding,
inject,
Injector,
OnChanges,
OnDestroy,
SimpleChanges,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import {
BehaviorSubject,
combineLatest,
from as fromPromise,
Observable,
of as observableOf,
Subscription,
} from 'rxjs';
import {
catchError,
map,
switchMap,
tap,
} from 'rxjs/operators';
import { GenericConstructor } from '../../core/shared/generic-constructor';
import {
hasNoValue,
hasValue,
isNotEmpty,
} from '../empty.util';
import { BASE_THEME_NAME } from './theme.constants';
import { ThemeService } from './theme.service';
@Component({
selector: 'ds-themed',
styleUrls: ['./themed.component.scss'],
templateUrl: './themed.component.html',
standalone: true,
})
export abstract class ThemedComponent<T extends object> implements AfterViewInit, OnDestroy, OnChanges {
@ViewChild('vcr', { read: ViewContainerRef }) vcr: ViewContainerRef;
@ViewChild('content') themedElementContent: ElementRef;
compRef: ComponentRef<T>;
/**
* A reference to the themed component. Will start as undefined and emit every time the themed
* component is rendered
*/
public compRef$: BehaviorSubject<ComponentRef<T>> = new BehaviorSubject(undefined);
protected lazyLoadObs: Observable<any>;
protected lazyLoadSub: Subscription;
protected themeSub: Subscription;
protected inAndOutputNames: (keyof T & keyof this)[] = [];
protected injector = inject(Injector);
/**
* A data attribute on the ThemedComponent to indicate which theme the rendered component came from.
*/
@HostBinding('attr.data-used-theme') usedTheme: string;
constructor(
protected cdr: ChangeDetectorRef,
protected themeService: ThemeService,
) {
}
protected abstract getComponentName(): string;
protected abstract importThemedComponent(themeName: string): Promise<any>;
protected abstract importUnthemedComponent(): Promise<any>;
ngOnChanges(changes: SimpleChanges): void {
if (hasNoValue(this.compRef)) {
// sometimes the component has not been initialized yet, so it first needs to be initialized
// before being called again
this.initComponentInstance(changes);
} else {
// if an input or output has changed
if (this.inAndOutputNames.some((name: any) => hasValue(changes[name]))) {
this.connectInputsAndOutputs();
if (this.compRef?.instance && 'ngOnChanges' in this.compRef.instance) {
(this.compRef.instance as any).ngOnChanges(changes);
}
}
}
}
ngAfterViewInit(): void {
this.initComponentInstance();
}
ngOnDestroy(): void {
[this.themeSub, this.lazyLoadSub].filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe());
this.destroyComponentInstance();
}
initComponentInstance(changes?: SimpleChanges) {
this.themeSub = this.themeService?.getThemeName$().subscribe(() => {
this.renderComponentInstance(changes);
});
}
protected renderComponentInstance(changes?: SimpleChanges): void {
if (hasValue(this.lazyLoadSub)) {
this.lazyLoadSub.unsubscribe();
}
if (hasNoValue(this.lazyLoadObs)) {
this.lazyLoadObs = combineLatest([
observableOf(changes),
this.resolveThemedComponent(this.themeService.getThemeName()).pipe(
switchMap((themedFile: any) => {
if (hasValue(themedFile) && hasValue(themedFile[this.getComponentName()])) {
// if the file is not null, and exports a component with the specified name,
// return that component
return [themedFile[this.getComponentName()]];
} else {
// otherwise import and return the default component
return fromPromise(this.importUnthemedComponent()).pipe(
tap(() => this.usedTheme = BASE_THEME_NAME),
map((unthemedFile: any) => {
return unthemedFile[this.getComponentName()];
}),
);
}
})),
]);
}
this.lazyLoadSub = this.lazyLoadObs.subscribe(([simpleChanges, constructor]: [SimpleChanges, GenericConstructor<T>]) => {
this.destroyComponentInstance();
// Get the ng-content selectors from the component metadata
const ngContentSelectors = this.getComponentNgContentSelectors(constructor);
const projectableNodes = this.getNgContent(this.themedElementContent.nativeElement, ngContentSelectors);
// Create component without using ComponentFactoryResolver
this.compRef = this.vcr.createComponent(constructor, {
injector: this.injector,
projectableNodes: projectableNodes,
});
if (hasValue(simpleChanges)) {
this.ngOnChanges(simpleChanges);
} else {
this.connectInputsAndOutputs();
}
this.compRef$.next(this.compRef);
this.cdr.markForCheck();
this.themedElementContent.nativeElement.remove();
});
}
/**
* Retrieves ng-content selectors from a component type
*/
private getComponentNgContentSelectors(componentType: GenericConstructor<any>): string[] {
const componentDef = (componentType as any).ɵcmp;
if (componentDef && componentDef.ngContentSelectors) {
return componentDef.ngContentSelectors;
}
return ['*']; // Default fallback
}
protected destroyComponentInstance(): void {
if (hasValue(this.compRef)) {
this.compRef.destroy();
this.compRef = null;
}
if (hasValue(this.vcr)) {
this.vcr.clear();
}
}
/**
* Extracts and returns the content nodes from the given element based on the provided ng-content selectors.
*
* @param {Element} element - The DOM element from which to extract content nodes.
* @param {string[]} ngSelectors - An array of ng-content selectors to match against the element's children.
* @returns {Node[][]} - A 2D array where each sub-array contains the nodes matching a specific selector.
*/
protected getNgContent(element: Element, ngSelectors: string[]): Node[][] {
return ngSelectors.map(selector => {
if (selector === '*') {
// If the selector is '*', return all child nodes of the element.
return Array.from(element.childNodes);
} else {
// Otherwise, select and return the nodes matching the specific selector.
const selectedElements = Array.from(element.querySelectorAll(selector));
// Remove the selected elements from the DOM.
selectedElements.forEach(e => e.remove());
return selectedElements;
}
});
}
protected connectInputsAndOutputs(): void {
if (isNotEmpty(this.inAndOutputNames) && hasValue(this.compRef) && hasValue(this.compRef.instance)) {
this.inAndOutputNames.filter((name: any) => this[name] !== undefined).forEach((name: any) => {
this.compRef.instance[name] = this[name];
});
}
}
/**
* Attempt to import this component from the current theme or a theme it {@link NamedThemeConfig.extends}.
* Recurse until we succeed or when until we run out of themes to fall back to.
*
* @param themeName The name of the theme to check
* @param checkedThemeNames The list of theme names that are already checked
* @private
*/
private resolveThemedComponent(themeName?: string, checkedThemeNames: string[] = []): Observable<any> {
if (isNotEmpty(themeName)) {
return fromPromise(this.importThemedComponent(themeName)).pipe(
tap(() => this.usedTheme = themeName),
catchError(() => {
// Try the next ancestor theme instead
const nextTheme = this.themeService.getThemeConfigFor(themeName)?.extends;
const nextCheckedThemeNames = [...checkedThemeNames, themeName];
if (checkedThemeNames.includes(nextTheme)) {
throw new Error('Theme extension cycle detected: ' + [...nextCheckedThemeNames, nextTheme].join(' -> '));
} else {
return this.resolveThemedComponent(nextTheme, nextCheckedThemeNames);
}
}),
);
} else {
// If we got here, we've failed to import this component from any ancestor theme → fall back to unthemed
return observableOf(null);
}
}
}