@angular2-material/core
Version:
Angular 2 Material core
57 lines (55 loc) • 2.62 kB
JavaScript
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { BasePortalHost } from './portal';
import { MdComponentPortalAttachedToDomWithoutOriginError } from './portal-errors';
/**
* 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 var DomPortalHost = (function (_super) {
__extends(DomPortalHost, _super);
function DomPortalHost(_hostDomElement, _componentFactoryResolver) {
_super.call(this);
this._hostDomElement = _hostDomElement;
this._componentFactoryResolver = _componentFactoryResolver;
}
/** Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver. */
DomPortalHost.prototype.attachComponentPortal = function (portal) {
if (portal.viewContainerRef == null) {
throw new MdComponentPortalAttachedToDomWithoutOriginError();
}
var componentFactory = this._componentFactoryResolver.resolveComponentFactory(portal.component);
var ref = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.parentInjector);
var hostView = ref.hostView;
this._hostDomElement.appendChild(hostView.rootNodes[0]);
this.setDisposeFn(function () { return ref.destroy(); });
return ref;
};
DomPortalHost.prototype.attachTemplatePortal = function (portal) {
var _this = this;
var viewContainer = portal.viewContainerRef;
var viewRef = viewContainer.createEmbeddedView(portal.templateRef);
viewRef.rootNodes.forEach(function (rootNode) { return _this._hostDomElement.appendChild(rootNode); });
this.setDisposeFn((function () {
var index = viewContainer.indexOf(viewRef);
if (index != -1) {
viewContainer.remove(index);
}
}));
// TODO(jelbourn): Return locals from view.
return new Map();
};
DomPortalHost.prototype.dispose = function () {
_super.prototype.dispose.call(this);
if (this._hostDomElement.parentNode != null) {
this._hostDomElement.parentNode.removeChild(this._hostDomElement);
}
};
return DomPortalHost;
}(BasePortalHost));
//# sourceMappingURL=dom-portal-host.js.map