import { BasePortalHost } from './portal'; /** * A PortalHost for attaching portals to an arbitrary DOM element outside of the Angular * application context. * * This is the only part of the portal core that directly touches the DOM. */ export class DomPortalHost extends BasePortalHost { _hostDomElement; _componentFactoryResolver; _appRef; constructor(_hostDomElement, _componentFactoryResolver, _appRef) { super(); this._hostDomElement = _hostDomElement; this._componentFactoryResolver = _componentFactoryResolver; this._appRef = _appRef; } /** * Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver. * @param portal Portal to be attached */ attachComponentPortal(portal, newestOnTop) { const componentFactory = this._componentFactoryResolver.resolveComponentFactory(portal.component); let componentRef; // If the portal specifies a ViewContainerRef, we will use that as the attachment point // for the component (in terms of Angular's component tree, not rendering). // When the ViewContainerRef is missing, we use the factory to create the component directly // and then manually attach the ChangeDetector for that component to the application (which // happens automatically when using a ViewContainer). componentRef = componentFactory.create(portal.injector); // When creating a component outside of a ViewContainer, we need to manually register // its ChangeDetector with the application. This API is unfortunately not yet published // in Angular core. The change detector must also be deregistered when the component // is destroyed to prevent memory leaks. this._appRef.attachView(componentRef.hostView); this.setDisposeFn(() => { this._appRef.detachView(componentRef.hostView); componentRef.destroy(); }); // At this point the component has been instantiated, so we move it to the location in the DOM // where we want it to be rendered. if (newestOnTop) { this._hostDomElement.insertBefore(this._getComponentRootNode(componentRef), this._hostDomElement.firstChild); } else { this._hostDomElement.appendChild(this._getComponentRootNode(componentRef)); } return componentRef; } /** Gets the root HTMLElement for an instantiated component. */ _getComponentRootNode(componentRef) { return componentRef.hostView.rootNodes[0]; } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9tLXBvcnRhbC1ob3N0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi9wb3J0YWwvZG9tLXBvcnRhbC1ob3N0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQU1BLE9BQU8sRUFBRSxjQUFjLEVBQW1CLE1BQ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