Skip to content

Commit 737195c

Browse files
110889: Made the SearchLabelLoaderComponent extend AbstractComponentLoaderComponent
1 parent 386a1c9 commit 737195c

4 files changed

Lines changed: 7 additions & 129 deletions

File tree

src/app/shared/search/search-labels/search-label-loader/search-label-loader-directive.directive.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/app/shared/search/search-labels/search-label-loader/search-label-loader.component.html

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 7 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,30 @@
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';
32
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';
73
import { getSearchLabelByOperator } from './search-label-loader.decorator';
84
import { AppliedFilter } from '../../models/applied-filter.model';
5+
import { AbstractComponentLoaderComponent } from '../../../abstract-component-loader/abstract-component-loader.component';
96

107
@Component({
118
selector: 'ds-search-label-loader',
12-
templateUrl: './search-label-loader.component.html',
9+
templateUrl: '../../../abstract-component-loader/abstract-component-loader.component.html',
1310
})
14-
export class SearchLabelLoaderComponent implements OnInit, OnChanges, OnDestroy {
11+
export class SearchLabelLoaderComponent extends AbstractComponentLoaderComponent<Component> implements OnInit, OnChanges {
1512

1613
@Input() inPlaceSearch: boolean;
1714

1815
@Input() appliedFilter: AppliedFilter;
1916

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+
];
4120

42-
/**
43-
* The list of the @Input() names that should be passed down to the dynamically created components.
44-
*/
4521
protected inputNames: (keyof this & string)[] = [
4622
'inPlaceSearch',
4723
'appliedFilter',
4824
];
4925

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-
*/
11626
public getComponent(): GenericConstructor<Component> {
11727
return getSearchLabelByOperator(this.appliedFilter.operator);
11828
}
11929

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-
13230
}

src/app/shared/search/search.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { SearchFilterComponent } from './search-filters/search-filter/search-fil
66
import { SearchFacetFilterComponent } from './search-filters/search-filter/search-facet-filter/search-facet-filter.component';
77
import { SearchLabelsComponent } from './search-labels/search-labels.component';
88
import { SearchLabelLoaderComponent } from './search-labels/search-label-loader/search-label-loader.component';
9-
import { SearchLabelLoaderDirective } from './search-labels/search-label-loader/search-label-loader-directive.directive';
109
import { SearchLabelComponent } from './search-labels/search-label/search-label.component';
1110
import { SearchLabelRangeComponent } from './search-labels/search-label-range/search-label-range.component';
1211
import { SearchFacetFilterWrapperComponent } from './search-filters/search-filter/search-facet-filter-wrapper/search-facet-filter-wrapper.component';
@@ -86,7 +85,6 @@ export const MODELS = [
8685
@NgModule({
8786
declarations: [
8887
...COMPONENTS,
89-
SearchLabelLoaderDirective,
9088
],
9189
imports: [
9290
CommonModule,
@@ -99,7 +97,6 @@ export const MODELS = [
9997
],
10098
exports: [
10199
...COMPONENTS,
102-
SearchLabelLoaderDirective,
103100
]
104101
})
105102
export class SearchModule {

0 commit comments

Comments
 (0)