UNPKG

2.62 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || function (d, b) {
2 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3 function __() { this.constructor = d; }
4 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
5};
6import { BasePortalHost } from './portal';
7import { MdComponentPortalAttachedToDomWithoutOriginError } from './portal-errors';
8/**
9 * A PortalHost for attaching portals to an arbitrary DOM element outside of the Angular
10 * application context.
11 *
12 * This is the only part of the portal core that directly touches the DOM.
13 */
14export var DomPortalHost = (function (_super) {
15 __extends(DomPortalHost, _super);
16 function DomPortalHost(_hostDomElement, _componentFactoryResolver) {
17 _super.call(this);
18 this._hostDomElement = _hostDomElement;
19 this._componentFactoryResolver = _componentFactoryResolver;
20 }
21 /** Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver. */
22 DomPortalHost.prototype.attachComponentPortal = function (portal) {
23 if (portal.viewContainerRef == null) {
24 throw new MdComponentPortalAttachedToDomWithoutOriginError();
25 }
26 var componentFactory = this._componentFactoryResolver.resolveComponentFactory(portal.component);
27 var ref = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.parentInjector);
28 var hostView = ref.hostView;
29 this._hostDomElement.appendChild(hostView.rootNodes[0]);
30 this.setDisposeFn(function () { return ref.destroy(); });
31 return ref;
32 };
33 DomPortalHost.prototype.attachTemplatePortal = function (portal) {
34 var _this = this;
35 var viewContainer = portal.viewContainerRef;
36 var viewRef = viewContainer.createEmbeddedView(portal.templateRef);
37 viewRef.rootNodes.forEach(function (rootNode) { return _this._hostDomElement.appendChild(rootNode); });
38 this.setDisposeFn((function () {
39 var index = viewContainer.indexOf(viewRef);
40 if (index != -1) {
41 viewContainer.remove(index);
42 }
43 }));
44 // TODO(jelbourn): Return locals from view.
45 return new Map();
46 };
47 DomPortalHost.prototype.dispose = function () {
48 _super.prototype.dispose.call(this);
49 if (this._hostDomElement.parentNode != null) {
50 this._hostDomElement.parentNode.removeChild(this._hostDomElement);
51 }
52 };
53 return DomPortalHost;
54}(BasePortalHost));
55
56//# sourceMappingURL=dom-portal-host.js.map