Browse Source

fix dynamic layout get component problem

pull/7164/head
mehmet-erim 6 years ago
parent
commit
48cc369c7c
  1. 67
      npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts

67
npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts

@ -1,5 +1,6 @@
import { Component, Injector, Optional, SkipSelf, Type } from '@angular/core'; import { Component, Injector, Optional, SkipSelf, Type } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router'; import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
import { eLayoutType } from '../enums/common'; import { eLayoutType } from '../enums/common';
import { ABP } from '../models'; import { ABP } from '../models';
import { ReplaceableComponents } from '../models/replaceable-components'; import { ReplaceableComponents } from '../models/replaceable-components';
@ -23,6 +24,7 @@ import { TreeNode } from '../utils/tree-utils';
}) })
export class DynamicLayoutComponent { export class DynamicLayoutComponent {
layout: Type<any>; layout: Type<any>;
layoutKey: eLayoutType;
// TODO: Consider a shared enum (eThemeSharedComponents) for known layouts // TODO: Consider a shared enum (eThemeSharedComponents) for known layouts
readonly layouts = new Map([ readonly layouts = new Map([
@ -33,6 +35,10 @@ export class DynamicLayoutComponent {
isLayoutVisible = true; isLayoutVisible = true;
private router: Router;
private route: ActivatedRoute;
private routes: RoutesService;
constructor( constructor(
injector: Injector, injector: Injector,
private localizationService: LocalizationService, private localizationService: LocalizationService,
@ -41,36 +47,45 @@ export class DynamicLayoutComponent {
@Optional() @SkipSelf() dynamicLayoutComponent: DynamicLayoutComponent, @Optional() @SkipSelf() dynamicLayoutComponent: DynamicLayoutComponent,
) { ) {
if (dynamicLayoutComponent) return; if (dynamicLayoutComponent) return;
const route = injector.get(ActivatedRoute); this.route = injector.get(ActivatedRoute);
const router = injector.get(Router); this.router = injector.get(Router);
const routes = injector.get(RoutesService); this.routes = injector.get(RoutesService);
this.subscription.addOne(router.events, event => { this.getLayout();
if (event instanceof NavigationEnd) { this.subscription.addOne(
let expectedLayout = (route.snapshot.data || {}).layout; this.router.events.pipe(filter(event => event instanceof NavigationEnd)),
() => {
if (!expectedLayout) { this.getLayout();
let node = findRoute(routes, getRoutePath(router)); },
node = { parent: node } as TreeNode<ABP.Route>; );
while (node.parent) { this.listenToLanguageChange();
node = node.parent; }
if (node.layout) { private getLayout() {
expectedLayout = node.layout; let expectedLayout = (this.route.snapshot.data || {}).layout;
break;
}
}
}
if (!expectedLayout) expectedLayout = eLayoutType.empty; if (!expectedLayout) {
let node = findRoute(this.routes, getRoutePath(this.router));
node = { parent: node } as TreeNode<ABP.Route>;
const key = this.layouts.get(expectedLayout); while (node.parent) {
this.layout = this.getComponent(key)?.component; node = node.parent;
if (node.layout) {
expectedLayout = node.layout;
break;
}
} }
}); }
this.listenToLanguageChange(); if (!expectedLayout) expectedLayout = eLayoutType.empty;
if (this.layoutKey === expectedLayout) return;
const key = this.layouts.get(expectedLayout);
this.layout = this.getComponent(key)?.component;
this.layoutKey = expectedLayout;
} }
private listenToLanguageChange() { private listenToLanguageChange() {

Loading…
Cancel
Save