From 0308c8bc8e1d6b16b2f925ecb0392152d41b0628 Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Mon, 18 Jan 2021 13:43:44 +0100 Subject: [PATCH] Revert "replace menu decorators with switches to solve an issue where decorators were not loaded in time" This reverts commit ece84332a0bac23bdc256d47465ec18fa534a4f5. --- .../admin-sidebar-section.component.ts | 2 ++ ...andable-admin-sidebar-section.component.ts | 2 ++ .../expandable-navbar-section.component.ts | 2 ++ .../navbar-section.component.ts | 2 ++ src/app/shared/menu/menu-item.decorator.ts | 28 +++++++++------ .../menu-item/link-menu-item.component.ts | 5 ++- .../menu-item/onclick-menu-item.component.ts | 3 ++ .../menu-item/text-menu-item.component.ts | 5 ++- src/app/shared/menu/menu-section.decorator.ts | 34 ++++++++++++------- 9 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/app/+admin/admin-sidebar/admin-sidebar-section/admin-sidebar-section.component.ts b/src/app/+admin/admin-sidebar/admin-sidebar-section/admin-sidebar-section.component.ts index 18943f4c3f..a19a1f95e4 100644 --- a/src/app/+admin/admin-sidebar/admin-sidebar-section/admin-sidebar-section.component.ts +++ b/src/app/+admin/admin-sidebar/admin-sidebar-section/admin-sidebar-section.component.ts @@ -2,6 +2,7 @@ import { Component, Inject, Injector, OnInit } from '@angular/core'; import { MenuSectionComponent } from '../../../shared/menu/menu-section/menu-section.component'; import { MenuID } from '../../../shared/menu/initial-menus-state'; import { MenuService } from '../../../shared/menu/menu.service'; +import { rendersSectionForMenu } from '../../../shared/menu/menu-section.decorator'; import { LinkMenuItemModel } from '../../../shared/menu/menu-item/models/link.model'; import { MenuSection } from '../../../shared/menu/menu.reducer'; @@ -14,6 +15,7 @@ import { MenuSection } from '../../../shared/menu/menu.reducer'; styleUrls: ['./admin-sidebar-section.component.scss'], }) +@rendersSectionForMenu(MenuID.ADMIN, false) export class AdminSidebarSectionComponent extends MenuSectionComponent implements OnInit { /** diff --git a/src/app/+admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component.ts b/src/app/+admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component.ts index 6148c64bc4..112560de16 100644 --- a/src/app/+admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component.ts +++ b/src/app/+admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component.ts @@ -8,6 +8,7 @@ import { MenuID } from '../../../shared/menu/initial-menus-state'; import { MenuService } from '../../../shared/menu/menu.service'; import { combineLatest as combineLatestObservable, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; +import { rendersSectionForMenu } from '../../../shared/menu/menu-section.decorator'; /** * Represents a expandable section in the sidebar @@ -19,6 +20,7 @@ import { map } from 'rxjs/operators'; animations: [rotate, slide, bgColor] }) +@rendersSectionForMenu(MenuID.ADMIN, true) export class ExpandableAdminSidebarSectionComponent extends AdminSidebarSectionComponent implements OnInit { /** * This section resides in the Admin Sidebar diff --git a/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.ts b/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.ts index 93002c7e7b..068854d6f8 100644 --- a/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.ts +++ b/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.ts @@ -5,6 +5,7 @@ import { MenuID } from '../../shared/menu/initial-menus-state'; import { slide } from '../../shared/animations/slide'; import { first } from 'rxjs/operators'; import { HostWindowService } from '../../shared/host-window.service'; +import { rendersSectionForMenu } from '../../shared/menu/menu-section.decorator'; /** * Represents an expandable section in the navbar @@ -15,6 +16,7 @@ import { HostWindowService } from '../../shared/host-window.service'; styleUrls: ['./expandable-navbar-section.component.scss'], animations: [slide] }) +@rendersSectionForMenu(MenuID.PUBLIC, true) export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements OnInit { /** * This section resides in the Public Navbar diff --git a/src/app/navbar/navbar-section/navbar-section.component.ts b/src/app/navbar/navbar-section/navbar-section.component.ts index bfb0a4ab27..e1488de3d3 100644 --- a/src/app/navbar/navbar-section/navbar-section.component.ts +++ b/src/app/navbar/navbar-section/navbar-section.component.ts @@ -2,6 +2,7 @@ import { Component, Inject, Injector, OnInit } from '@angular/core'; import { MenuSectionComponent } from '../../shared/menu/menu-section/menu-section.component'; import { MenuService } from '../../shared/menu/menu.service'; import { MenuID } from '../../shared/menu/initial-menus-state'; +import { rendersSectionForMenu } from '../../shared/menu/menu-section.decorator'; /** * Represents a non-expandable section in the navbar @@ -11,6 +12,7 @@ import { MenuID } from '../../shared/menu/initial-menus-state'; templateUrl: './navbar-section.component.html', styleUrls: ['./navbar-section.component.scss'] }) +@rendersSectionForMenu(MenuID.PUBLIC, false) export class NavbarSectionComponent extends MenuSectionComponent implements OnInit { /** * This section resides in the Public Navbar diff --git a/src/app/shared/menu/menu-item.decorator.ts b/src/app/shared/menu/menu-item.decorator.ts index 38207fc463..3740a4eba4 100644 --- a/src/app/shared/menu/menu-item.decorator.ts +++ b/src/app/shared/menu/menu-item.decorator.ts @@ -1,7 +1,20 @@ import { MenuItemType } from './initial-menus-state'; -import { LinkMenuItemComponent } from './menu-item/link-menu-item.component'; -import { OnClickMenuItemComponent } from './menu-item/onclick-menu-item.component'; -import { TextMenuItemComponent } from './menu-item/text-menu-item.component'; + +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); + }; +} /** * Retrieves the Component matching a given MenuItemType @@ -9,12 +22,5 @@ import { TextMenuItemComponent } from './menu-item/text-menu-item.component'; * @returns {GenericConstructor} The constructor of the Component that matches the MenuItemType */ export function getComponentForMenuItemType(type: MenuItemType) { - switch (type) { - case MenuItemType.LINK: - return LinkMenuItemComponent; - case MenuItemType.ONCLICK: - return OnClickMenuItemComponent; - case MenuItemType.TEXT: - return TextMenuItemComponent; - } + return menuMenuItemComponentMap.get(type); } diff --git a/src/app/shared/menu/menu-item/link-menu-item.component.ts b/src/app/shared/menu/menu-item/link-menu-item.component.ts index af43e11c31..e5b66c5aab 100644 --- a/src/app/shared/menu/menu-item/link-menu-item.component.ts +++ b/src/app/shared/menu/menu-item/link-menu-item.component.ts @@ -1,5 +1,7 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, Inject, Input, OnInit } from '@angular/core'; import { LinkMenuItemModel } from './models/link.model'; +import { MenuItemType } from '../initial-menus-state'; +import { rendersMenuItemForType } from '../menu-item.decorator'; import { isNotEmpty } from '../../empty.util'; import { environment } from '../../../../environments/environment'; @@ -10,6 +12,7 @@ import { environment } from '../../../../environments/environment'; selector: 'ds-link-menu-item', templateUrl: './link-menu-item.component.html' }) +@rendersMenuItemForType(MenuItemType.LINK) export class LinkMenuItemComponent implements OnInit { item: LinkMenuItemModel; hasLink: boolean; diff --git a/src/app/shared/menu/menu-item/onclick-menu-item.component.ts b/src/app/shared/menu/menu-item/onclick-menu-item.component.ts index 5e90986617..95b896ed64 100644 --- a/src/app/shared/menu/menu-item/onclick-menu-item.component.ts +++ b/src/app/shared/menu/menu-item/onclick-menu-item.component.ts @@ -1,4 +1,6 @@ import { Component, Inject } from '@angular/core'; +import { MenuItemType } from '../initial-menus-state'; +import { rendersMenuItemForType } from '../menu-item.decorator'; import { OnClickMenuItemModel } from './models/onclick.model'; /** @@ -9,6 +11,7 @@ import { OnClickMenuItemModel } from './models/onclick.model'; styleUrls: ['./onclick-menu-item.component.scss'], templateUrl: './onclick-menu-item.component.html' }) +@rendersMenuItemForType(MenuItemType.ONCLICK) export class OnClickMenuItemComponent { item: OnClickMenuItemModel; constructor(@Inject('itemModelProvider') item: OnClickMenuItemModel) { diff --git a/src/app/shared/menu/menu-item/text-menu-item.component.ts b/src/app/shared/menu/menu-item/text-menu-item.component.ts index 13c8b62a11..f7d3402be0 100644 --- a/src/app/shared/menu/menu-item/text-menu-item.component.ts +++ b/src/app/shared/menu/menu-item/text-menu-item.component.ts @@ -1,5 +1,7 @@ -import { Component, Inject } from '@angular/core'; +import { Component, Inject, Input } from '@angular/core'; import { TextMenuItemModel } from './models/text.model'; +import { MenuItemType } from '../initial-menus-state'; +import { rendersMenuItemForType } from '../menu-item.decorator'; /** * Component that renders a menu section of type TEXT @@ -8,6 +10,7 @@ import { TextMenuItemModel } from './models/text.model'; selector: 'ds-text-menu-item', templateUrl: './text-menu-item.component.html', }) +@rendersMenuItemForType(MenuItemType.TEXT) export class TextMenuItemComponent { item: TextMenuItemModel; constructor(@Inject('itemModelProvider') item: TextMenuItemModel) { diff --git a/src/app/shared/menu/menu-section.decorator.ts b/src/app/shared/menu/menu-section.decorator.ts index 7e8dbcc8dd..c27e870e13 100644 --- a/src/app/shared/menu/menu-section.decorator.ts +++ b/src/app/shared/menu/menu-section.decorator.ts @@ -1,8 +1,24 @@ import { MenuID } from './initial-menus-state'; -import { NavbarSectionComponent } from '../../navbar/navbar-section/navbar-section.component'; -import { ExpandableNavbarSectionComponent } from '../../navbar/expandable-navbar-section/expandable-navbar-section.component'; -import { ExpandableAdminSidebarSectionComponent } from '../../+admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component'; -import { AdminSidebarSectionComponent } from '../../+admin/admin-sidebar/admin-sidebar-section/admin-sidebar-section.component'; + +const menuComponentMap = new Map(); + +/** + * Decorator function to render a MenuSection for a menu + * @param {MenuID} menuID The ID of the Menu in which the section is rendered + * @param {boolean} expandable True when the section should be expandable, false when if should not + * @returns {(menuSectionWrapperComponent: GenericConstructor) => void} + */ +export function rendersSectionForMenu(menuID: MenuID, expandable: boolean) { + return function decorator(menuSectionWrapperComponent: any) { + if (!menuSectionWrapperComponent) { + return; + } + if (!menuComponentMap.get(menuID)) { + menuComponentMap.set(menuID, new Map()); + } + menuComponentMap.get(menuID).set(expandable, menuSectionWrapperComponent); + }; +} /** * Retrieves the component matching the given MenuID and whether or not it should be expandable @@ -11,13 +27,5 @@ import { AdminSidebarSectionComponent } from '../../+admin/admin-sidebar/admin-s * @returns {GenericConstructor} The constructor of the matching Component */ export function getComponentForMenu(menuID: MenuID, expandable: boolean) { - if (menuID === MenuID.PUBLIC && expandable === true) { - return ExpandableNavbarSectionComponent; - } else if (menuID === MenuID.PUBLIC && expandable === false) { - return NavbarSectionComponent; - } else if (menuID === MenuID.ADMIN && expandable === true) { - return ExpandableAdminSidebarSectionComponent; - } else if (menuID === MenuID.ADMIN && expandable === false) { - return AdminSidebarSectionComponent; - } + return menuComponentMap.get(menuID).get(expandable); }