import { Directive, ComponentFactoryResolver, ViewContainerRef, OnDestroy, Input, } from '@angular/core'; import { Portal, TemplatePortal, BasePortalHost } from './portal'; @Directive({ selector: '[clPortalHost]' }) export class ClPortalHostDirective extends BasePortalHost implements OnDestroy { /** The attached portal. */ private _portal: Portal; constructor( private _componentFactoryResolver: ComponentFactoryResolver, private _viewContainerRef: ViewContainerRef) { super(); } /** Portal associated with the Portal host. */ @Input('clPortalHost') set portal(p: Portal) { if (p) { this._replaceAttachedPortal(p); } } get portal(): Portal { return this._portal; } ngOnDestroy() { this.dispose(); } /** * Attach the given TemplatePortal to this PortlHost as an embedded View. * @param portal Portal to be attached. */ attachTemplatePortal(portal: TemplatePortal): Map { portal.setAttachedHost(this); this._viewContainerRef.createEmbeddedView(portal.templateRef); this.setDisposeFn(() => this._viewContainerRef.clear()); return new Map(); } /** Detaches the currently attached Portal (if there is one) and attaches the given Portal. */ private _replaceAttachedPortal(p: Portal): void { if (this.hasAttached()) { this.detach(); } if (p) { this.attach(p); this._portal = p; } } }