|
1 | | -import { Component, ComponentRef, OnChanges, OnDestroy, OnInit, ViewChild, ViewContainerRef, SimpleChanges, Input } from '@angular/core'; |
2 | | -import { Subscription } from 'rxjs'; |
| 1 | +import { Component, OnChanges, OnInit, Input } from '@angular/core'; |
3 | 2 | import { GenericConstructor } from 'src/app/core/shared/generic-constructor'; |
4 | | -import { hasValue, isNotEmpty } from 'src/app/shared/empty.util'; |
5 | | -import { ThemeService } from '../../../theme-support/theme.service'; |
6 | | -import { SearchLabelLoaderDirective } from './search-label-loader-directive.directive'; |
7 | 3 | import { getSearchLabelByOperator } from './search-label-loader.decorator'; |
8 | 4 | import { AppliedFilter } from '../../models/applied-filter.model'; |
| 5 | +import { AbstractComponentLoaderComponent } from '../../../abstract-component-loader/abstract-component-loader.component'; |
9 | 6 |
|
10 | 7 | @Component({ |
11 | 8 | selector: 'ds-search-label-loader', |
12 | | - templateUrl: './search-label-loader.component.html', |
| 9 | + templateUrl: '../../../abstract-component-loader/abstract-component-loader.component.html', |
13 | 10 | }) |
14 | | -export class SearchLabelLoaderComponent implements OnInit, OnChanges, OnDestroy { |
| 11 | +export class SearchLabelLoaderComponent extends AbstractComponentLoaderComponent<Component> implements OnInit, OnChanges { |
15 | 12 |
|
16 | 13 | @Input() inPlaceSearch: boolean; |
17 | 14 |
|
18 | 15 | @Input() appliedFilter: AppliedFilter; |
19 | 16 |
|
20 | | - /** |
21 | | - * Directive to determine where the dynamic child component is located |
22 | | - */ |
23 | | - @ViewChild(SearchLabelLoaderDirective, { static: true }) componentDirective: SearchLabelLoaderDirective; |
24 | | - |
25 | | - /** |
26 | | - * The reference to the dynamic component |
27 | | - */ |
28 | | - protected compRef: ComponentRef<Component>; |
29 | | - |
30 | | - /** |
31 | | - * Array to track all subscriptions and unsubscribe them onDestroy |
32 | | - */ |
33 | | - protected subs: Subscription[] = []; |
34 | | - |
35 | | - /** |
36 | | - * The @Input() that are used to find the matching component using {@link getComponent}. When the value of |
37 | | - * one of these @Input() change this loader needs to retrieve the best matching component again using the |
38 | | - * {@link getComponent} method. |
39 | | - */ |
40 | | - protected inputNamesDependentForComponent: (keyof this & string)[] = []; |
| 17 | + protected inputNamesDependentForComponent: (keyof this & string)[] = [ |
| 18 | + 'appliedFilter', |
| 19 | + ]; |
41 | 20 |
|
42 | | - /** |
43 | | - * The list of the @Input() names that should be passed down to the dynamically created components. |
44 | | - */ |
45 | 21 | protected inputNames: (keyof this & string)[] = [ |
46 | 22 | 'inPlaceSearch', |
47 | 23 | 'appliedFilter', |
48 | 24 | ]; |
49 | 25 |
|
50 | | - constructor( |
51 | | - protected themeService: ThemeService, |
52 | | - ) { |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * Set up the dynamic child component |
57 | | - */ |
58 | | - ngOnInit(): void { |
59 | | - this.instantiateComponent(); |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Whenever the inputs change, update the inputs of the dynamic component |
64 | | - */ |
65 | | - ngOnChanges(changes: SimpleChanges): void { |
66 | | - if (hasValue(this.compRef)) { |
67 | | - if (this.inputNamesDependentForComponent.some((name: keyof this & string) => hasValue(changes[name]) && changes[name].previousValue !== changes[name].currentValue)) { |
68 | | - // Recreate the component when the @Input()s used by getComponent() aren't up-to-date anymore |
69 | | - this.destroyComponentInstance(); |
70 | | - this.instantiateComponent(); |
71 | | - } else { |
72 | | - this.connectInputsAndOutputs(); |
73 | | - } |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - ngOnDestroy(): void { |
78 | | - this.subs |
79 | | - .filter((subscription: Subscription) => hasValue(subscription)) |
80 | | - .forEach((subscription: Subscription) => subscription.unsubscribe()); |
81 | | - this.destroyComponentInstance(); |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Creates the component and connects the @Input() & @Output() from the ThemedComponent to its child Component. |
86 | | - */ |
87 | | - public instantiateComponent(): void { |
88 | | - const component: GenericConstructor<Component> = this.getComponent(); |
89 | | - |
90 | | - const viewContainerRef: ViewContainerRef = this.componentDirective.viewContainerRef; |
91 | | - viewContainerRef.clear(); |
92 | | - |
93 | | - this.compRef = viewContainerRef.createComponent( |
94 | | - component, { |
95 | | - index: 0, |
96 | | - injector: undefined, |
97 | | - }, |
98 | | - ); |
99 | | - |
100 | | - this.connectInputsAndOutputs(); |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Destroys the themed component and calls it's `ngOnDestroy` |
105 | | - */ |
106 | | - public destroyComponentInstance(): void { |
107 | | - if (hasValue(this.compRef)) { |
108 | | - this.compRef.destroy(); |
109 | | - this.compRef = null; |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - /** |
114 | | - * Fetch the component depending on the item's entity type, metadata representation type and context |
115 | | - */ |
116 | 26 | public getComponent(): GenericConstructor<Component> { |
117 | 27 | return getSearchLabelByOperator(this.appliedFilter.operator); |
118 | 28 | } |
119 | 29 |
|
120 | | - /** |
121 | | - * Connect the inputs and outputs of this component to the dynamic component, |
122 | | - * to ensure they're in sync |
123 | | - */ |
124 | | - public connectInputsAndOutputs(): void { |
125 | | - if (isNotEmpty(this.inputNames) && hasValue(this.compRef) && hasValue(this.compRef.instance)) { |
126 | | - this.inputNames.filter((name: string) => this[name] !== undefined).filter((name: string) => this[name] !== this.compRef.instance[name]).forEach((name: string) => { |
127 | | - this.compRef.instance[name] = this[name]; |
128 | | - }); |
129 | | - } |
130 | | - } |
131 | | - |
132 | 30 | } |
0 commit comments