md2
Version:
Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.
95 lines • 4.54 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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.
*/
var DomPortalHost = (function (_super) {
__extends(DomPortalHost, _super);
function DomPortalHost(_hostDomElement, _componentFactoryResolver, _appRef, _defaultInjector) {
var _this = _super.call(this) || this;
_this._hostDomElement = _hostDomElement;
_this._componentFactoryResolver = _componentFactoryResolver;
_this._appRef = _appRef;
_this._defaultInjector = _defaultInjector;
return _this;
}
/**
* Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver.
* @param portal Portal to be attached
*/
DomPortalHost.prototype.attachComponentPortal = function (portal) {
var _this = this;
var componentFactory = this._componentFactoryResolver.resolveComponentFactory(portal.component);
var 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 view to the application.
if (portal.viewContainerRef) {
componentRef = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.parentInjector);
this.setDisposeFn(function () { return componentRef.destroy(); });
}
else {
componentRef = componentFactory.create(portal.injector || this._defaultInjector);
this._appRef.attachView(componentRef.hostView);
this.setDisposeFn(function () {
_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.
this._hostDomElement.appendChild(this._getComponentRootNode(componentRef));
return componentRef;
};
/**
* Attaches a template portal to the DOM as an embedded view.
* @param portal Portal to be attached.
*/
DomPortalHost.prototype.attachTemplatePortal = function (portal) {
var _this = this;
var viewContainer = portal.viewContainerRef;
var viewRef = viewContainer.createEmbeddedView(portal.templateRef);
viewRef.detectChanges();
// The method `createEmbeddedView` will add the view as a child of the viewContainer.
// But for the DomPortalHost the view can be added everywhere in the DOM (e.g Overlay Container)
// To move the view to the specified host element. We just re-append the existing root nodes.
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();
};
/**
* Clears out a portal from the DOM.
*/
DomPortalHost.prototype.dispose = function () {
_super.prototype.dispose.call(this);
if (this._hostDomElement.parentNode != null) {
this._hostDomElement.parentNode.removeChild(this._hostDomElement);
}
};
/** Gets the root HTMLElement for an instantiated component. */
DomPortalHost.prototype._getComponentRootNode = function (componentRef) {
return componentRef.hostView.rootNodes[0];
};
return DomPortalHost;
}(BasePortalHost));
export { DomPortalHost };
//# sourceMappingURL=dom-portal-host.js.map