UNPKG

5.52 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 { MdNullPortalHostError, MdPortalAlreadyAttachedError, MdNoPortalAttachedError, MdNullPortalError, MdPortalHostAlreadyDisposedError, MdUnknownPortalTypeError } from './portal-errors';
7/**
8 * A `Portal` is something that you want to render somewhere else.
9 * It can be attach to / detached from a `PortalHost`.
10 */
11export var Portal = (function () {
12 function Portal() {
13 }
14 /** Attach this portal to a host. */
15 Portal.prototype.attach = function (host) {
16 if (host == null) {
17 throw new MdNullPortalHostError();
18 }
19 if (host.hasAttached()) {
20 throw new MdPortalAlreadyAttachedError();
21 }
22 this._attachedHost = host;
23 return host.attach(this);
24 };
25 /** Detach this portal from its host */
26 Portal.prototype.detach = function () {
27 var host = this._attachedHost;
28 if (host == null) {
29 throw new MdNoPortalAttachedError();
30 }
31 this._attachedHost = null;
32 return host.detach();
33 };
34 Object.defineProperty(Portal.prototype, "isAttached", {
35 /** Whether this portal is attached to a host. */
36 get: function () {
37 return this._attachedHost != null;
38 },
39 enumerable: true,
40 configurable: true
41 });
42 /**
43 * Sets the PortalHost reference without performing `attach()`. This is used directly by
44 * the PortalHost when it is performing an `attach()` or `detatch()`.
45 */
46 Portal.prototype.setAttachedHost = function (host) {
47 this._attachedHost = host;
48 };
49 return Portal;
50}());
51/**
52 * A `ComponentPortal` is a portal that instantiates some Component upon attachment.
53 */
54export var ComponentPortal = (function (_super) {
55 __extends(ComponentPortal, _super);
56 function ComponentPortal(component, viewContainerRef, injector) {
57 if (viewContainerRef === void 0) { viewContainerRef = null; }
58 if (injector === void 0) { injector = null; }
59 _super.call(this);
60 this.component = component;
61 this.viewContainerRef = viewContainerRef;
62 this.injector = injector;
63 }
64 return ComponentPortal;
65}(Portal));
66/**
67 * A `TemplatePortal` is a portal that represents some embedded template (TemplateRef).
68 */
69export var TemplatePortal = (function (_super) {
70 __extends(TemplatePortal, _super);
71 function TemplatePortal(template, viewContainerRef) {
72 _super.call(this);
73 /**
74 * Additional locals for the instantiated embedded view.
75 * These locals can be seen as "exports" for the template, such as how ngFor has
76 * index / event / odd.
77 * See https://angular.io/docs/ts/latest/api/core/EmbeddedViewRef-class.html
78 */
79 this.locals = new Map();
80 this.templateRef = template;
81 this.viewContainerRef = viewContainerRef;
82 }
83 Object.defineProperty(TemplatePortal.prototype, "origin", {
84 get: function () {
85 return this.templateRef.elementRef;
86 },
87 enumerable: true,
88 configurable: true
89 });
90 TemplatePortal.prototype.attach = function (host, locals) {
91 this.locals = locals == null ? new Map() : locals;
92 return _super.prototype.attach.call(this, host);
93 };
94 TemplatePortal.prototype.detach = function () {
95 this.locals = new Map();
96 return _super.prototype.detach.call(this);
97 };
98 return TemplatePortal;
99}(Portal));
100/**
101 * Partial implementation of PortalHost that only deals with attaching either a
102 * ComponentPortal or a TemplatePortal.
103 */
104export var BasePortalHost = (function () {
105 function BasePortalHost() {
106 /** Whether this host has already been permanently disposed. */
107 this._isDisposed = false;
108 }
109 /** Whether this host has an attached portal. */
110 BasePortalHost.prototype.hasAttached = function () {
111 return this._attachedPortal != null;
112 };
113 BasePortalHost.prototype.attach = function (portal) {
114 if (portal == null) {
115 throw new MdNullPortalError();
116 }
117 if (this.hasAttached()) {
118 throw new MdPortalAlreadyAttachedError();
119 }
120 if (this._isDisposed) {
121 throw new MdPortalHostAlreadyDisposedError();
122 }
123 if (portal instanceof ComponentPortal) {
124 this._attachedPortal = portal;
125 return this.attachComponentPortal(portal);
126 }
127 else if (portal instanceof TemplatePortal) {
128 this._attachedPortal = portal;
129 return this.attachTemplatePortal(portal);
130 }
131 throw new MdUnknownPortalTypeError();
132 };
133 BasePortalHost.prototype.detach = function () {
134 if (this._attachedPortal) {
135 this._attachedPortal.setAttachedHost(null);
136 }
137 this._attachedPortal = null;
138 if (this._disposeFn != null) {
139 this._disposeFn();
140 this._disposeFn = null;
141 }
142 };
143 BasePortalHost.prototype.dispose = function () {
144 if (this.hasAttached()) {
145 this.detach();
146 }
147 this._isDisposed = true;
148 };
149 BasePortalHost.prototype.setDisposeFn = function (fn) {
150 this._disposeFn = fn;
151 };
152 return BasePortalHost;
153}());
154
155//# sourceMappingURL=portal.js.map