UNPKG

@angular2-material/core

Version:
156 lines (154 loc) 5.52 kB
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 { MdNullPortalHostError, MdPortalAlreadyAttachedError, MdNoPortalAttachedError, MdNullPortalError, MdPortalHostAlreadyDisposedError, MdUnknownPortalTypeError } from './portal-errors'; /** * A `Portal` is something that you want to render somewhere else. * It can be attach to / detached from a `PortalHost`. */ export var Portal = (function () { function Portal() { } /** Attach this portal to a host. */ Portal.prototype.attach = function (host) { if (host == null) { throw new MdNullPortalHostError(); } if (host.hasAttached()) { throw new MdPortalAlreadyAttachedError(); } this._attachedHost = host; return host.attach(this); }; /** Detach this portal from its host */ Portal.prototype.detach = function () { var host = this._attachedHost; if (host == null) { throw new MdNoPortalAttachedError(); } this._attachedHost = null; return host.detach(); }; Object.defineProperty(Portal.prototype, "isAttached", { /** Whether this portal is attached to a host. */ get: function () { return this._attachedHost != null; }, enumerable: true, configurable: true }); /** * Sets the PortalHost reference without performing `attach()`. This is used directly by * the PortalHost when it is performing an `attach()` or `detatch()`. */ Portal.prototype.setAttachedHost = function (host) { this._attachedHost = host; }; return Portal; }()); /** * A `ComponentPortal` is a portal that instantiates some Component upon attachment. */ export var ComponentPortal = (function (_super) { __extends(ComponentPortal, _super); function ComponentPortal(component, viewContainerRef, injector) { if (viewContainerRef === void 0) { viewContainerRef = null; } if (injector === void 0) { injector = null; } _super.call(this); this.component = component; this.viewContainerRef = viewContainerRef; this.injector = injector; } return ComponentPortal; }(Portal)); /** * A `TemplatePortal` is a portal that represents some embedded template (TemplateRef). */ export var TemplatePortal = (function (_super) { __extends(TemplatePortal, _super); function TemplatePortal(template, viewContainerRef) { _super.call(this); /** * Additional locals for the instantiated embedded view. * These locals can be seen as "exports" for the template, such as how ngFor has * index / event / odd. * See https://angular.io/docs/ts/latest/api/core/EmbeddedViewRef-class.html */ this.locals = new Map(); this.templateRef = template; this.viewContainerRef = viewContainerRef; } Object.defineProperty(TemplatePortal.prototype, "origin", { get: function () { return this.templateRef.elementRef; }, enumerable: true, configurable: true }); TemplatePortal.prototype.attach = function (host, locals) { this.locals = locals == null ? new Map() : locals; return _super.prototype.attach.call(this, host); }; TemplatePortal.prototype.detach = function () { this.locals = new Map(); return _super.prototype.detach.call(this); }; return TemplatePortal; }(Portal)); /** * Partial implementation of PortalHost that only deals with attaching either a * ComponentPortal or a TemplatePortal. */ export var BasePortalHost = (function () { function BasePortalHost() { /** Whether this host has already been permanently disposed. */ this._isDisposed = false; } /** Whether this host has an attached portal. */ BasePortalHost.prototype.hasAttached = function () { return this._attachedPortal != null; }; BasePortalHost.prototype.attach = function (portal) { if (portal == null) { throw new MdNullPortalError(); } if (this.hasAttached()) { throw new MdPortalAlreadyAttachedError(); } if (this._isDisposed) { throw new MdPortalHostAlreadyDisposedError(); } if (portal instanceof ComponentPortal) { this._attachedPortal = portal; return this.attachComponentPortal(portal); } else if (portal instanceof TemplatePortal) { this._attachedPortal = portal; return this.attachTemplatePortal(portal); } throw new MdUnknownPortalTypeError(); }; BasePortalHost.prototype.detach = function () { if (this._attachedPortal) { this._attachedPortal.setAttachedHost(null); } this._attachedPortal = null; if (this._disposeFn != null) { this._disposeFn(); this._disposeFn = null; } }; BasePortalHost.prototype.dispose = function () { if (this.hasAttached()) { this.detach(); } this._isDisposed = true; }; BasePortalHost.prototype.setDisposeFn = function (fn) { this._disposeFn = fn; }; return BasePortalHost; }()); //# sourceMappingURL=portal.js.map