Guest User

Untitled

a guest
Nov 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import {
  2. Attribute,
  3. ChangeDetectorRef,
  4. ComponentFactoryResolver,
  5. Directive,
  6. Input,
  7. ViewContainerRef
  8. } from '@angular/core';
  9. import { ActivatedRoute, ChildrenOutletContexts, PRIMARY_OUTLET, RouterOutlet } from '@angular/router';
  10. // insert correct location to the fallback view
  11. import { PortalLangSelectView } from './portal-lang-select.view';
  12.  
  13. export enum CurrentPageType {
  14. NONE,
  15. MAIN,
  16. PORTAL
  17. }
  18.  
  19. @Directive({ selector: 'i18n-router-outlet' })
  20. export class I18nRouterOutletDirective extends RouterOutlet {
  21. private _lang = null;
  22. private _currentPage = CurrentPageType.NONE;
  23.  
  24. constructor(
  25. private thisParentContexts: ChildrenOutletContexts,
  26. private thisLocation: ViewContainerRef,
  27. private thisResolver: ComponentFactoryResolver,
  28. @Attribute('name') private thisName: string,
  29. private thisChangeDetector: ChangeDetectorRef
  30. ) {
  31. super(thisParentContexts, thisLocation, thisResolver, thisName, thisChangeDetector);
  32. this.thisName = name || PRIMARY_OUTLET;
  33. }
  34.  
  35. @Input()
  36. set lang(lang: string) {
  37. this._lang = lang || null;
  38. if (this._currentPage === CurrentPageType.PORTAL) {
  39. const context = this.thisParentContexts.getContext(this.thisName);
  40. this.thisLocation.clear();
  41. this.activateWith(context.route, context.resolver || null);
  42. }
  43. }
  44.  
  45. activateWith(activatedRoute: ActivatedRoute, resolver: ComponentFactoryResolver | null): void {
  46. if (this._lang !== null) {
  47. this._currentPage = CurrentPageType.MAIN;
  48. super.activateWith(activatedRoute, resolver);
  49. } else if (this._currentPage === CurrentPageType.NONE) {
  50. this._currentPage = CurrentPageType.PORTAL;
  51. resolver = resolver || this.thisResolver;
  52. const factory = resolver.resolveComponentFactory(PortalLangSelectView);
  53. const routeActivated = this.thisLocation.createComponent(factory, this.thisLocation.length);
  54. this.activateEvents.emit(routeActivated.instance);
  55. }
  56. }
  57. }
Add Comment
Please, Sign In to add comment