[DURACOM-191] set static map for menu-item decorator

This commit is contained in:
Andrea Barbasso
2024-01-15 12:13:14 +01:00
parent 4cab7a31af
commit 2d3c5cef06
5 changed files with 8 additions and 28 deletions

View File

@@ -1,20 +1,12 @@
import { MenuItemType } from './menu-item-type.model';
import { defer } from 'rxjs';
const menuMenuItemComponentMap = new Map();
/**
* Decorator function to link a MenuItemType to a Component
* @param {MenuItemType} type The MenuItemType of the MenuSection's model
* @returns {(sectionComponent: GenericContructor) => void}
*/
export function rendersMenuItemForType(type: MenuItemType) {
return function decorator(sectionComponent: any) {
if (!sectionComponent) {
return;
}
menuMenuItemComponentMap.set(type, sectionComponent);
};
}
const menuMenuItemComponentMap = {
[MenuItemType.EXTERNAL]: defer(() => import('./menu-item/external-link-menu-item.component').then(m => m.ExternalLinkMenuItemComponent)),
[MenuItemType.LINK]: defer(() => import('./menu-item/link-menu-item.component').then(m => m.LinkMenuItemComponent)),
[MenuItemType.ONCLICK]: defer(() => import('./menu-item/onclick-menu-item.component').then(m => m.OnClickMenuItemComponent)),
[MenuItemType.TEXT]: defer(() => import('./menu-item/text-menu-item.component').then(m => m.TextMenuItemComponent))
};
/**
* Retrieves the Component matching a given MenuItemType
@@ -22,5 +14,5 @@ export function rendersMenuItemForType(type: MenuItemType) {
* @returns {GenericConstructor} The constructor of the Component that matches the MenuItemType
*/
export function getComponentForMenuItemType(type: MenuItemType) {
return menuMenuItemComponentMap.get(type);
return menuMenuItemComponentMap[type];
}

View File

@@ -1,8 +1,6 @@
import { Component, Inject, OnInit } from '@angular/core';
import { rendersMenuItemForType } from '../menu-item.decorator';
import { isNotEmpty } from '../../empty.util';
import { ExternalLinkMenuItemModel } from './models/external-link.model';
import { MenuItemType } from '../menu-item-type.model';
import { TranslateModule } from '@ngx-translate/core';
import { NgClass } from '@angular/common';
@@ -15,7 +13,6 @@ import { NgClass } from '@angular/common';
standalone: true,
imports: [NgClass, TranslateModule]
})
@rendersMenuItemForType(MenuItemType.EXTERNAL)
export class ExternalLinkMenuItemComponent implements OnInit {
item: ExternalLinkMenuItemModel;

View File

@@ -1,8 +1,6 @@
import { Component, Inject, OnInit } from '@angular/core';
import { LinkMenuItemModel } from './models/link.model';
import { rendersMenuItemForType } from '../menu-item.decorator';
import { isNotEmpty } from '../../empty.util';
import { MenuItemType } from '../menu-item-type.model';
import { Router, RouterLink } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { NgClass } from '@angular/common';
@@ -16,7 +14,6 @@ import { NgClass } from '@angular/common';
standalone: true,
imports: [NgClass, RouterLink, TranslateModule]
})
@rendersMenuItemForType(MenuItemType.LINK)
export class LinkMenuItemComponent implements OnInit {
item: LinkMenuItemModel;
hasLink: boolean;

View File

@@ -1,7 +1,5 @@
import { Component, Inject } from '@angular/core';
import { rendersMenuItemForType } from '../menu-item.decorator';
import { OnClickMenuItemModel } from './models/onclick.model';
import { MenuItemType } from '../menu-item-type.model';
import { TranslateModule } from '@ngx-translate/core';
import { NgIf } from '@angular/common';
@@ -15,7 +13,6 @@ import { NgIf } from '@angular/common';
standalone: true,
imports: [NgIf, TranslateModule]
})
@rendersMenuItemForType(MenuItemType.ONCLICK)
export class OnClickMenuItemComponent {
item: OnClickMenuItemModel;

View File

@@ -1,7 +1,5 @@
import { Component, Inject } from '@angular/core';
import { TextMenuItemModel } from './models/text.model';
import { rendersMenuItemForType } from '../menu-item.decorator';
import { MenuItemType } from '../menu-item-type.model';
import { TranslateModule } from '@ngx-translate/core';
/**
@@ -13,7 +11,6 @@ import { TranslateModule } from '@ngx-translate/core';
standalone: true,
imports: [TranslateModule]
})
@rendersMenuItemForType(MenuItemType.TEXT)
export class TextMenuItemComponent {
item: TextMenuItemModel;
constructor(@Inject('itemModelProvider') item: TextMenuItemModel) {