111731: Added theme support to renderSearchLabelFor

This commit is contained in:
Alexandre Vryghem
2024-04-13 17:26:57 +02:00
parent 7eb26582db
commit 8d1d04ed58
3 changed files with 30 additions and 19 deletions

View File

@@ -1,6 +1,5 @@
import { Component, ComponentRef, OnChanges, OnDestroy, OnInit, ViewChild, ViewContainerRef, SimpleChanges, Input } from '@angular/core';
import { Component, ComponentRef, OnChanges, OnDestroy, OnInit, ViewChild, ViewContainerRef, SimpleChanges, Input, Type } from '@angular/core';
import { Subscription } from 'rxjs';
import { GenericConstructor } from 'src/app/core/shared/generic-constructor';
import { hasValue, isNotEmpty } from 'src/app/shared/empty.util';
import { ThemeService } from '../../../theme-support/theme.service';
import { SearchLabelLoaderDirective } from './search-label-loader-directive.directive';
@@ -85,7 +84,7 @@ export class SearchLabelLoaderComponent implements OnInit, OnChanges, OnDestroy
* Creates the component and connects the @Input() & @Output() from the ThemedComponent to its child Component.
*/
public instantiateComponent(): void {
const component: GenericConstructor<Component> = this.getComponent();
const component: Type<Component> = this.getComponent();
const viewContainerRef: ViewContainerRef = this.componentDirective.viewContainerRef;
viewContainerRef.clear();
@@ -113,8 +112,8 @@ export class SearchLabelLoaderComponent implements OnInit, OnChanges, OnDestroy
/**
* Fetch the component depending on the item's entity type, metadata representation type and context
*/
public getComponent(): GenericConstructor<Component> {
return getSearchLabelByOperator(this.appliedFilter.operator);
public getComponent(): Type<Component> {
return getSearchLabelByOperator(this.appliedFilter.operator, this.themeService.getThemeName());
}
/**

View File

@@ -1,17 +1,35 @@
import { Component } from '@angular/core';
import { GenericConstructor } from '../../../../core/shared/generic-constructor';
import { Component, Type } from '@angular/core';
import { DEFAULT_THEME } from '../../../object-collection/shared/listable-object/listable-object.decorator';
import { hasNoValue } from '../../../empty.util';
export const map: Map<string, GenericConstructor<Component>> = new Map();
export const DEFAULT_LABEL_OPERATOR = undefined;
export function renderSearchLabelFor(operator: string) {
const map: Map<string, Map<string, Type<Component>>> = new Map();
export function renderSearchLabelFor(operator: string = DEFAULT_LABEL_OPERATOR, theme = DEFAULT_THEME) {
return function decorator(objectElement: any) {
if (!objectElement) {
return;
}
map.set(operator, objectElement);
if (hasNoValue(map.get(operator))) {
map.set(operator, new Map());
}
if (hasNoValue(map.get(operator).get(theme))) {
map.get(operator).set(theme, objectElement);
} else {
throw new Error(`There can't be more than one component to render Label with operator "${operator}" and theme "${theme}"`);
}
};
}
export function getSearchLabelByOperator(operator: string): GenericConstructor<Component> {
return map.get(operator);
export function getSearchLabelByOperator(operator: string, theme: string): Type<Component> {
let themeMap: Map<string, Type<Component>> = map.get(operator);
if (hasNoValue(themeMap)) {
themeMap = map.get(DEFAULT_LABEL_OPERATOR);
}
const comp: Type<Component> = themeMap.get(theme);
if (hasNoValue(comp)) {
return themeMap.get(DEFAULT_THEME);
}
return comp;
}

View File

@@ -15,13 +15,7 @@ import { PaginationService } from '../../../../core/pagination/pagination.servic
selector: 'ds-search-label',
templateUrl: './search-label.component.html',
})
@renderSearchLabelFor('equals')
@renderSearchLabelFor('notequals')
@renderSearchLabelFor('authority')
@renderSearchLabelFor('notauthority')
@renderSearchLabelFor('contains')
@renderSearchLabelFor('notcontains')
@renderSearchLabelFor('query')
@renderSearchLabelFor()
export class SearchLabelComponent implements OnInit {
@Input() inPlaceSearch: boolean;
@Input() appliedFilter: AppliedFilter;