Advertisement
xapu

Portal stuff

Jan 3rd, 2019
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.     ApplicationRef,
  3.     ComponentFactoryResolver,
  4.     ComponentRef,
  5.     ElementRef,
  6.     EmbeddedViewRef,
  7.     Injectable,
  8.     Injector
  9. } from '@angular/core';
  10.  
  11. import { Observable } from 'rxjs';
  12.  
  13. import {
  14.     Product,
  15.     ProductStatusData,
  16.     ProjectProductDetailedStatus,
  17.     ServerItem
  18. } from '@app/shared/models';
  19.  
  20. import { SharedServiceModule } from './shared-services.module';
  21.  
  22. @Injectable({
  23.     providedIn: SharedServiceModule
  24. })
  25. export class ProductStatusComponentPortalService {
  26.  
  27.     constructor(
  28.         private componentFactoryResolver: ComponentFactoryResolver,
  29.         private appRef: ApplicationRef,
  30.         private injector: Injector,
  31.     ) { }
  32.  
  33.     data: ProductStatusData;
  34.     parsedElementToAppend: HTMLElement;
  35.     elementToAppend: ComponentRef<{}>;
  36.  
  37.     getConfig() {
  38.         return this.data;
  39.     }
  40.  
  41.     appendComponentToBody(
  42.         component,
  43.         row: Product,
  44.         productDetailedStatus$: Observable<ServerItem<ProjectProductDetailedStatus>>,
  45.         isInModal: boolean,
  46.         e: ElementRef
  47.     ) {
  48.         if (this.elementToAppend) {
  49.             this.detachComponent();
  50.         }
  51.  
  52.         const buttonDimensions = (<HTMLElement>e.nativeElement).getBoundingClientRect();
  53.  
  54.         this.elementToAppend = this.componentFactoryResolver
  55.             .resolveComponentFactory(component)
  56.             .create(this.injector);
  57.  
  58.         this.data = {
  59.             row,
  60.             productDetailedStatus$,
  61.             x: buttonDimensions.left + buttonDimensions.width,
  62.             y: buttonDimensions.top + window.pageYOffset,
  63.             isInModal
  64.         };
  65.  
  66.         this.appRef.attachView(this.elementToAppend.hostView);
  67.  
  68.         this.parsedElementToAppend = (this.elementToAppend.hostView as EmbeddedViewRef<any>)
  69.             .rootNodes[0] as HTMLElement;
  70.  
  71.         document.getElementById('cdkPortalOutlet').appendChild(this.parsedElementToAppend);
  72.     }
  73.  
  74.     detachComponent() {
  75.         this.elementToAppend.destroy();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement