UNPKG

44.5 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common')) :
3 typeof define === 'function' && define.amd ? define('@angular/cdk/portal', ['exports', '@angular/core', '@angular/common'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ng = global.ng || {}, global.ng.cdk = global.ng.cdk || {}, global.ng.cdk.portal = {}), global.ng.core, global.ng.common));
5}(this, (function (exports, core, common) { 'use strict';
6
7 /*! *****************************************************************************
8 Copyright (c) Microsoft Corporation.
9
10 Permission to use, copy, modify, and/or distribute this software for any
11 purpose with or without fee is hereby granted.
12
13 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 PERFORMANCE OF THIS SOFTWARE.
20 ***************************************************************************** */
21 /* global Reflect, Promise */
22 var extendStatics = function (d, b) {
23 extendStatics = Object.setPrototypeOf ||
24 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
25 function (d, b) { for (var p in b)
26 if (Object.prototype.hasOwnProperty.call(b, p))
27 d[p] = b[p]; };
28 return extendStatics(d, b);
29 };
30 function __extends(d, b) {
31 if (typeof b !== "function" && b !== null)
32 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
33 extendStatics(d, b);
34 function __() { this.constructor = d; }
35 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
36 }
37 var __assign = function () {
38 __assign = Object.assign || function __assign(t) {
39 for (var s, i = 1, n = arguments.length; i < n; i++) {
40 s = arguments[i];
41 for (var p in s)
42 if (Object.prototype.hasOwnProperty.call(s, p))
43 t[p] = s[p];
44 }
45 return t;
46 };
47 return __assign.apply(this, arguments);
48 };
49 function __rest(s, e) {
50 var t = {};
51 for (var p in s)
52 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
53 t[p] = s[p];
54 if (s != null && typeof Object.getOwnPropertySymbols === "function")
55 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
56 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
57 t[p[i]] = s[p[i]];
58 }
59 return t;
60 }
61 function __decorate(decorators, target, key, desc) {
62 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
63 if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
64 r = Reflect.decorate(decorators, target, key, desc);
65 else
66 for (var i = decorators.length - 1; i >= 0; i--)
67 if (d = decorators[i])
68 r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
69 return c > 3 && r && Object.defineProperty(target, key, r), r;
70 }
71 function __param(paramIndex, decorator) {
72 return function (target, key) { decorator(target, key, paramIndex); };
73 }
74 function __metadata(metadataKey, metadataValue) {
75 if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
76 return Reflect.metadata(metadataKey, metadataValue);
77 }
78 function __awaiter(thisArg, _arguments, P, generator) {
79 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
80 return new (P || (P = Promise))(function (resolve, reject) {
81 function fulfilled(value) { try {
82 step(generator.next(value));
83 }
84 catch (e) {
85 reject(e);
86 } }
87 function rejected(value) { try {
88 step(generator["throw"](value));
89 }
90 catch (e) {
91 reject(e);
92 } }
93 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
94 step((generator = generator.apply(thisArg, _arguments || [])).next());
95 });
96 }
97 function __generator(thisArg, body) {
98 var _ = { label: 0, sent: function () { if (t[0] & 1)
99 throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
100 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
101 function verb(n) { return function (v) { return step([n, v]); }; }
102 function step(op) {
103 if (f)
104 throw new TypeError("Generator is already executing.");
105 while (_)
106 try {
107 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
108 return t;
109 if (y = 0, t)
110 op = [op[0] & 2, t.value];
111 switch (op[0]) {
112 case 0:
113 case 1:
114 t = op;
115 break;
116 case 4:
117 _.label++;
118 return { value: op[1], done: false };
119 case 5:
120 _.label++;
121 y = op[1];
122 op = [0];
123 continue;
124 case 7:
125 op = _.ops.pop();
126 _.trys.pop();
127 continue;
128 default:
129 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
130 _ = 0;
131 continue;
132 }
133 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
134 _.label = op[1];
135 break;
136 }
137 if (op[0] === 6 && _.label < t[1]) {
138 _.label = t[1];
139 t = op;
140 break;
141 }
142 if (t && _.label < t[2]) {
143 _.label = t[2];
144 _.ops.push(op);
145 break;
146 }
147 if (t[2])
148 _.ops.pop();
149 _.trys.pop();
150 continue;
151 }
152 op = body.call(thisArg, _);
153 }
154 catch (e) {
155 op = [6, e];
156 y = 0;
157 }
158 finally {
159 f = t = 0;
160 }
161 if (op[0] & 5)
162 throw op[1];
163 return { value: op[0] ? op[1] : void 0, done: true };
164 }
165 }
166 var __createBinding = Object.create ? (function (o, m, k, k2) {
167 if (k2 === undefined)
168 k2 = k;
169 Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
170 }) : (function (o, m, k, k2) {
171 if (k2 === undefined)
172 k2 = k;
173 o[k2] = m[k];
174 });
175 function __exportStar(m, o) {
176 for (var p in m)
177 if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
178 __createBinding(o, m, p);
179 }
180 function __values(o) {
181 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
182 if (m)
183 return m.call(o);
184 if (o && typeof o.length === "number")
185 return {
186 next: function () {
187 if (o && i >= o.length)
188 o = void 0;
189 return { value: o && o[i++], done: !o };
190 }
191 };
192 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
193 }
194 function __read(o, n) {
195 var m = typeof Symbol === "function" && o[Symbol.iterator];
196 if (!m)
197 return o;
198 var i = m.call(o), r, ar = [], e;
199 try {
200 while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
201 ar.push(r.value);
202 }
203 catch (error) {
204 e = { error: error };
205 }
206 finally {
207 try {
208 if (r && !r.done && (m = i["return"]))
209 m.call(i);
210 }
211 finally {
212 if (e)
213 throw e.error;
214 }
215 }
216 return ar;
217 }
218 /** @deprecated */
219 function __spread() {
220 for (var ar = [], i = 0; i < arguments.length; i++)
221 ar = ar.concat(__read(arguments[i]));
222 return ar;
223 }
224 /** @deprecated */
225 function __spreadArrays() {
226 for (var s = 0, i = 0, il = arguments.length; i < il; i++)
227 s += arguments[i].length;
228 for (var r = Array(s), k = 0, i = 0; i < il; i++)
229 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
230 r[k] = a[j];
231 return r;
232 }
233 function __spreadArray(to, from, pack) {
234 if (pack || arguments.length === 2)
235 for (var i = 0, l = from.length, ar; i < l; i++) {
236 if (ar || !(i in from)) {
237 if (!ar)
238 ar = Array.prototype.slice.call(from, 0, i);
239 ar[i] = from[i];
240 }
241 }
242 return to.concat(ar || from);
243 }
244 function __await(v) {
245 return this instanceof __await ? (this.v = v, this) : new __await(v);
246 }
247 function __asyncGenerator(thisArg, _arguments, generator) {
248 if (!Symbol.asyncIterator)
249 throw new TypeError("Symbol.asyncIterator is not defined.");
250 var g = generator.apply(thisArg, _arguments || []), i, q = [];
251 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
252 function verb(n) { if (g[n])
253 i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
254 function resume(n, v) { try {
255 step(g[n](v));
256 }
257 catch (e) {
258 settle(q[0][3], e);
259 } }
260 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
261 function fulfill(value) { resume("next", value); }
262 function reject(value) { resume("throw", value); }
263 function settle(f, v) { if (f(v), q.shift(), q.length)
264 resume(q[0][0], q[0][1]); }
265 }
266 function __asyncDelegator(o) {
267 var i, p;
268 return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
269 function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
270 }
271 function __asyncValues(o) {
272 if (!Symbol.asyncIterator)
273 throw new TypeError("Symbol.asyncIterator is not defined.");
274 var m = o[Symbol.asyncIterator], i;
275 return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
276 function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
277 function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
278 }
279 function __makeTemplateObject(cooked, raw) {
280 if (Object.defineProperty) {
281 Object.defineProperty(cooked, "raw", { value: raw });
282 }
283 else {
284 cooked.raw = raw;
285 }
286 return cooked;
287 }
288 ;
289 var __setModuleDefault = Object.create ? (function (o, v) {
290 Object.defineProperty(o, "default", { enumerable: true, value: v });
291 }) : function (o, v) {
292 o["default"] = v;
293 };
294 function __importStar(mod) {
295 if (mod && mod.__esModule)
296 return mod;
297 var result = {};
298 if (mod != null)
299 for (var k in mod)
300 if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
301 __createBinding(result, mod, k);
302 __setModuleDefault(result, mod);
303 return result;
304 }
305 function __importDefault(mod) {
306 return (mod && mod.__esModule) ? mod : { default: mod };
307 }
308 function __classPrivateFieldGet(receiver, state, kind, f) {
309 if (kind === "a" && !f)
310 throw new TypeError("Private accessor was defined without a getter");
311 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
312 throw new TypeError("Cannot read private member from an object whose class did not declare it");
313 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
314 }
315 function __classPrivateFieldSet(receiver, state, value, kind, f) {
316 if (kind === "m")
317 throw new TypeError("Private method is not writable");
318 if (kind === "a" && !f)
319 throw new TypeError("Private accessor was defined without a setter");
320 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
321 throw new TypeError("Cannot write private member to an object whose class did not declare it");
322 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
323 }
324
325 /**
326 * @license
327 * Copyright Google LLC All Rights Reserved.
328 *
329 * Use of this source code is governed by an MIT-style license that can be
330 * found in the LICENSE file at https://angular.io/license
331 */
332 /**
333 * Throws an exception when attempting to attach a null portal to a host.
334 * @docs-private
335 */
336 function throwNullPortalError() {
337 throw Error('Must provide a portal to attach');
338 }
339 /**
340 * Throws an exception when attempting to attach a portal to a host that is already attached.
341 * @docs-private
342 */
343 function throwPortalAlreadyAttachedError() {
344 throw Error('Host already has a portal attached');
345 }
346 /**
347 * Throws an exception when attempting to attach a portal to an already-disposed host.
348 * @docs-private
349 */
350 function throwPortalOutletAlreadyDisposedError() {
351 throw Error('This PortalOutlet has already been disposed');
352 }
353 /**
354 * Throws an exception when attempting to attach an unknown portal type.
355 * @docs-private
356 */
357 function throwUnknownPortalTypeError() {
358 throw Error('Attempting to attach an unknown Portal type. BasePortalOutlet accepts either ' +
359 'a ComponentPortal or a TemplatePortal.');
360 }
361 /**
362 * Throws an exception when attempting to attach a portal to a null host.
363 * @docs-private
364 */
365 function throwNullPortalOutletError() {
366 throw Error('Attempting to attach a portal to a null PortalOutlet');
367 }
368 /**
369 * Throws an exception when attempting to detach a portal that is not attached.
370 * @docs-private
371 */
372 function throwNoPortalAttachedError() {
373 throw Error('Attempting to detach a portal that is not attached to a host');
374 }
375
376 /**
377 * A `Portal` is something that you want to render somewhere else.
378 * It can be attach to / detached from a `PortalOutlet`.
379 */
380 var Portal = /** @class */ (function () {
381 function Portal() {
382 }
383 /** Attach this portal to a host. */
384 Portal.prototype.attach = function (host) {
385 if (typeof ngDevMode === 'undefined' || ngDevMode) {
386 if (host == null) {
387 throwNullPortalOutletError();
388 }
389 if (host.hasAttached()) {
390 throwPortalAlreadyAttachedError();
391 }
392 }
393 this._attachedHost = host;
394 return host.attach(this);
395 };
396 /** Detach this portal from its host */
397 Portal.prototype.detach = function () {
398 var host = this._attachedHost;
399 if (host != null) {
400 this._attachedHost = null;
401 host.detach();
402 }
403 else if (typeof ngDevMode === 'undefined' || ngDevMode) {
404 throwNoPortalAttachedError();
405 }
406 };
407 Object.defineProperty(Portal.prototype, "isAttached", {
408 /** Whether this portal is attached to a host. */
409 get: function () {
410 return this._attachedHost != null;
411 },
412 enumerable: false,
413 configurable: true
414 });
415 /**
416 * Sets the PortalOutlet reference without performing `attach()`. This is used directly by
417 * the PortalOutlet when it is performing an `attach()` or `detach()`.
418 */
419 Portal.prototype.setAttachedHost = function (host) {
420 this._attachedHost = host;
421 };
422 return Portal;
423 }());
424 /**
425 * A `ComponentPortal` is a portal that instantiates some Component upon attachment.
426 */
427 var ComponentPortal = /** @class */ (function (_super) {
428 __extends(ComponentPortal, _super);
429 function ComponentPortal(component, viewContainerRef, injector, componentFactoryResolver) {
430 var _this = _super.call(this) || this;
431 _this.component = component;
432 _this.viewContainerRef = viewContainerRef;
433 _this.injector = injector;
434 _this.componentFactoryResolver = componentFactoryResolver;
435 return _this;
436 }
437 return ComponentPortal;
438 }(Portal));
439 /**
440 * A `TemplatePortal` is a portal that represents some embedded template (TemplateRef).
441 */
442 var TemplatePortal = /** @class */ (function (_super) {
443 __extends(TemplatePortal, _super);
444 function TemplatePortal(template, viewContainerRef, context) {
445 var _this = _super.call(this) || this;
446 _this.templateRef = template;
447 _this.viewContainerRef = viewContainerRef;
448 _this.context = context;
449 return _this;
450 }
451 Object.defineProperty(TemplatePortal.prototype, "origin", {
452 get: function () {
453 return this.templateRef.elementRef;
454 },
455 enumerable: false,
456 configurable: true
457 });
458 /**
459 * Attach the portal to the provided `PortalOutlet`.
460 * When a context is provided it will override the `context` property of the `TemplatePortal`
461 * instance.
462 */
463 TemplatePortal.prototype.attach = function (host, context) {
464 if (context === void 0) { context = this.context; }
465 this.context = context;
466 return _super.prototype.attach.call(this, host);
467 };
468 TemplatePortal.prototype.detach = function () {
469 this.context = undefined;
470 return _super.prototype.detach.call(this);
471 };
472 return TemplatePortal;
473 }(Portal));
474 /**
475 * A `DomPortal` is a portal whose DOM element will be taken from its current position
476 * in the DOM and moved into a portal outlet, when it is attached. On detach, the content
477 * will be restored to its original position.
478 */
479 var DomPortal = /** @class */ (function (_super) {
480 __extends(DomPortal, _super);
481 function DomPortal(element) {
482 var _this = _super.call(this) || this;
483 _this.element = element instanceof core.ElementRef ? element.nativeElement : element;
484 return _this;
485 }
486 return DomPortal;
487 }(Portal));
488 /**
489 * Partial implementation of PortalOutlet that handles attaching
490 * ComponentPortal and TemplatePortal.
491 */
492 var BasePortalOutlet = /** @class */ (function () {
493 function BasePortalOutlet() {
494 /** Whether this host has already been permanently disposed. */
495 this._isDisposed = false;
496 // @breaking-change 10.0.0 `attachDomPortal` to become a required abstract method.
497 this.attachDomPortal = null;
498 }
499 /** Whether this host has an attached portal. */
500 BasePortalOutlet.prototype.hasAttached = function () {
501 return !!this._attachedPortal;
502 };
503 /** Attaches a portal. */
504 BasePortalOutlet.prototype.attach = function (portal) {
505 if (typeof ngDevMode === 'undefined' || ngDevMode) {
506 if (!portal) {
507 throwNullPortalError();
508 }
509 if (this.hasAttached()) {
510 throwPortalAlreadyAttachedError();
511 }
512 if (this._isDisposed) {
513 throwPortalOutletAlreadyDisposedError();
514 }
515 }
516 if (portal instanceof ComponentPortal) {
517 this._attachedPortal = portal;
518 return this.attachComponentPortal(portal);
519 }
520 else if (portal instanceof TemplatePortal) {
521 this._attachedPortal = portal;
522 return this.attachTemplatePortal(portal);
523 // @breaking-change 10.0.0 remove null check for `this.attachDomPortal`.
524 }
525 else if (this.attachDomPortal && portal instanceof DomPortal) {
526 this._attachedPortal = portal;
527 return this.attachDomPortal(portal);
528 }
529 if (typeof ngDevMode === 'undefined' || ngDevMode) {
530 throwUnknownPortalTypeError();
531 }
532 };
533 /** Detaches a previously attached portal. */
534 BasePortalOutlet.prototype.detach = function () {
535 if (this._attachedPortal) {
536 this._attachedPortal.setAttachedHost(null);
537 this._attachedPortal = null;
538 }
539 this._invokeDisposeFn();
540 };
541 /** Permanently dispose of this portal host. */
542 BasePortalOutlet.prototype.dispose = function () {
543 if (this.hasAttached()) {
544 this.detach();
545 }
546 this._invokeDisposeFn();
547 this._isDisposed = true;
548 };
549 /** @docs-private */
550 BasePortalOutlet.prototype.setDisposeFn = function (fn) {
551 this._disposeFn = fn;
552 };
553 BasePortalOutlet.prototype._invokeDisposeFn = function () {
554 if (this._disposeFn) {
555 this._disposeFn();
556 this._disposeFn = null;
557 }
558 };
559 return BasePortalOutlet;
560 }());
561 /**
562 * @deprecated Use `BasePortalOutlet` instead.
563 * @breaking-change 9.0.0
564 */
565 var BasePortalHost = /** @class */ (function (_super) {
566 __extends(BasePortalHost, _super);
567 function BasePortalHost() {
568 return _super !== null && _super.apply(this, arguments) || this;
569 }
570 return BasePortalHost;
571 }(BasePortalOutlet));
572
573 /**
574 * A PortalOutlet for attaching portals to an arbitrary DOM element outside of the Angular
575 * application context.
576 */
577 var DomPortalOutlet = /** @class */ (function (_super) {
578 __extends(DomPortalOutlet, _super);
579 function DomPortalOutlet(
580 /** Element into which the content is projected. */
581 outletElement, _componentFactoryResolver, _appRef, _defaultInjector,
582 /**
583 * @deprecated `_document` Parameter to be made required.
584 * @breaking-change 10.0.0
585 */
586 _document) {
587 var _this = _super.call(this) || this;
588 _this.outletElement = outletElement;
589 _this._componentFactoryResolver = _componentFactoryResolver;
590 _this._appRef = _appRef;
591 _this._defaultInjector = _defaultInjector;
592 /**
593 * Attaches a DOM portal by transferring its content into the outlet.
594 * @param portal Portal to be attached.
595 * @deprecated To be turned into a method.
596 * @breaking-change 10.0.0
597 */
598 _this.attachDomPortal = function (portal) {
599 // @breaking-change 10.0.0 Remove check and error once the
600 // `_document` constructor parameter is required.
601 if (!_this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
602 throw Error('Cannot attach DOM portal without _document constructor parameter');
603 }
604 var element = portal.element;
605 if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
606 throw Error('DOM portal content must be attached to a parent node.');
607 }
608 // Anchor used to save the element's previous position so
609 // that we can restore it when the portal is detached.
610 var anchorNode = _this._document.createComment('dom-portal');
611 element.parentNode.insertBefore(anchorNode, element);
612 _this.outletElement.appendChild(element);
613 _this._attachedPortal = portal;
614 _super.prototype.setDisposeFn.call(_this, function () {
615 // We can't use `replaceWith` here because IE doesn't support it.
616 if (anchorNode.parentNode) {
617 anchorNode.parentNode.replaceChild(element, anchorNode);
618 }
619 });
620 };
621 _this._document = _document;
622 return _this;
623 }
624 /**
625 * Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver.
626 * @param portal Portal to be attached
627 * @returns Reference to the created component.
628 */
629 DomPortalOutlet.prototype.attachComponentPortal = function (portal) {
630 var _this = this;
631 var resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
632 var componentFactory = resolver.resolveComponentFactory(portal.component);
633 var componentRef;
634 // If the portal specifies a ViewContainerRef, we will use that as the attachment point
635 // for the component (in terms of Angular's component tree, not rendering).
636 // When the ViewContainerRef is missing, we use the factory to create the component directly
637 // and then manually attach the view to the application.
638 if (portal.viewContainerRef) {
639 componentRef = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.injector);
640 this.setDisposeFn(function () { return componentRef.destroy(); });
641 }
642 else {
643 componentRef = componentFactory.create(portal.injector || this._defaultInjector);
644 this._appRef.attachView(componentRef.hostView);
645 this.setDisposeFn(function () {
646 _this._appRef.detachView(componentRef.hostView);
647 componentRef.destroy();
648 });
649 }
650 // At this point the component has been instantiated, so we move it to the location in the DOM
651 // where we want it to be rendered.
652 this.outletElement.appendChild(this._getComponentRootNode(componentRef));
653 this._attachedPortal = portal;
654 return componentRef;
655 };
656 /**
657 * Attaches a template portal to the DOM as an embedded view.
658 * @param portal Portal to be attached.
659 * @returns Reference to the created embedded view.
660 */
661 DomPortalOutlet.prototype.attachTemplatePortal = function (portal) {
662 var _this = this;
663 var viewContainer = portal.viewContainerRef;
664 var viewRef = viewContainer.createEmbeddedView(portal.templateRef, portal.context);
665 // The method `createEmbeddedView` will add the view as a child of the viewContainer.
666 // But for the DomPortalOutlet the view can be added everywhere in the DOM
667 // (e.g Overlay Container) To move the view to the specified host element. We just
668 // re-append the existing root nodes.
669 viewRef.rootNodes.forEach(function (rootNode) { return _this.outletElement.appendChild(rootNode); });
670 // Note that we want to detect changes after the nodes have been moved so that
671 // any directives inside the portal that are looking at the DOM inside a lifecycle
672 // hook won't be invoked too early.
673 viewRef.detectChanges();
674 this.setDisposeFn((function () {
675 var index = viewContainer.indexOf(viewRef);
676 if (index !== -1) {
677 viewContainer.remove(index);
678 }
679 }));
680 this._attachedPortal = portal;
681 // TODO(jelbourn): Return locals from view.
682 return viewRef;
683 };
684 /**
685 * Clears out a portal from the DOM.
686 */
687 DomPortalOutlet.prototype.dispose = function () {
688 _super.prototype.dispose.call(this);
689 if (this.outletElement.parentNode != null) {
690 this.outletElement.parentNode.removeChild(this.outletElement);
691 }
692 };
693 /** Gets the root HTMLElement for an instantiated component. */
694 DomPortalOutlet.prototype._getComponentRootNode = function (componentRef) {
695 return componentRef.hostView.rootNodes[0];
696 };
697 return DomPortalOutlet;
698 }(BasePortalOutlet));
699 /**
700 * @deprecated Use `DomPortalOutlet` instead.
701 * @breaking-change 9.0.0
702 */
703 var DomPortalHost = /** @class */ (function (_super) {
704 __extends(DomPortalHost, _super);
705 function DomPortalHost() {
706 return _super !== null && _super.apply(this, arguments) || this;
707 }
708 return DomPortalHost;
709 }(DomPortalOutlet));
710
711 /**
712 * Directive version of a `TemplatePortal`. Because the directive *is* a TemplatePortal,
713 * the directive instance itself can be attached to a host, enabling declarative use of portals.
714 */
715 var CdkPortal = /** @class */ (function (_super) {
716 __extends(CdkPortal, _super);
717 function CdkPortal(templateRef, viewContainerRef) {
718 return _super.call(this, templateRef, viewContainerRef) || this;
719 }
720 return CdkPortal;
721 }(TemplatePortal));
722 CdkPortal.decorators = [
723 { type: core.Directive, args: [{
724 selector: '[cdkPortal]',
725 exportAs: 'cdkPortal',
726 },] }
727 ];
728 CdkPortal.ctorParameters = function () { return [
729 { type: core.TemplateRef },
730 { type: core.ViewContainerRef }
731 ]; };
732 /**
733 * @deprecated Use `CdkPortal` instead.
734 * @breaking-change 9.0.0
735 */
736 var TemplatePortalDirective = /** @class */ (function (_super) {
737 __extends(TemplatePortalDirective, _super);
738 function TemplatePortalDirective() {
739 return _super !== null && _super.apply(this, arguments) || this;
740 }
741 return TemplatePortalDirective;
742 }(CdkPortal));
743 TemplatePortalDirective.decorators = [
744 { type: core.Directive, args: [{
745 selector: '[cdk-portal], [portal]',
746 exportAs: 'cdkPortal',
747 providers: [{
748 provide: CdkPortal,
749 useExisting: TemplatePortalDirective
750 }]
751 },] }
752 ];
753 /**
754 * Directive version of a PortalOutlet. Because the directive *is* a PortalOutlet, portals can be
755 * directly attached to it, enabling declarative use.
756 *
757 * Usage:
758 * `<ng-template [cdkPortalOutlet]="greeting"></ng-template>`
759 */
760 var CdkPortalOutlet = /** @class */ (function (_super) {
761 __extends(CdkPortalOutlet, _super);
762 function CdkPortalOutlet(_componentFactoryResolver, _viewContainerRef,
763 /**
764 * @deprecated `_document` parameter to be made required.
765 * @breaking-change 9.0.0
766 */
767 _document) {
768 var _this = _super.call(this) || this;
769 _this._componentFactoryResolver = _componentFactoryResolver;
770 _this._viewContainerRef = _viewContainerRef;
771 /** Whether the portal component is initialized. */
772 _this._isInitialized = false;
773 /** Emits when a portal is attached to the outlet. */
774 _this.attached = new core.EventEmitter();
775 /**
776 * Attaches the given DomPortal to this PortalHost by moving all of the portal content into it.
777 * @param portal Portal to be attached.
778 * @deprecated To be turned into a method.
779 * @breaking-change 10.0.0
780 */
781 _this.attachDomPortal = function (portal) {
782 // @breaking-change 9.0.0 Remove check and error once the
783 // `_document` constructor parameter is required.
784 if (!_this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
785 throw Error('Cannot attach DOM portal without _document constructor parameter');
786 }
787 var element = portal.element;
788 if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
789 throw Error('DOM portal content must be attached to a parent node.');
790 }
791 // Anchor used to save the element's previous position so
792 // that we can restore it when the portal is detached.
793 var anchorNode = _this._document.createComment('dom-portal');
794 portal.setAttachedHost(_this);
795 element.parentNode.insertBefore(anchorNode, element);
796 _this._getRootNode().appendChild(element);
797 _this._attachedPortal = portal;
798 _super.prototype.setDisposeFn.call(_this, function () {
799 if (anchorNode.parentNode) {
800 anchorNode.parentNode.replaceChild(element, anchorNode);
801 }
802 });
803 };
804 _this._document = _document;
805 return _this;
806 }
807 Object.defineProperty(CdkPortalOutlet.prototype, "portal", {
808 /** Portal associated with the Portal outlet. */
809 get: function () {
810 return this._attachedPortal;
811 },
812 set: function (portal) {
813 // Ignore the cases where the `portal` is set to a falsy value before the lifecycle hooks have
814 // run. This handles the cases where the user might do something like `<div cdkPortalOutlet>`
815 // and attach a portal programmatically in the parent component. When Angular does the first CD
816 // round, it will fire the setter with empty string, causing the user's content to be cleared.
817 if (this.hasAttached() && !portal && !this._isInitialized) {
818 return;
819 }
820 if (this.hasAttached()) {
821 _super.prototype.detach.call(this);
822 }
823 if (portal) {
824 _super.prototype.attach.call(this, portal);
825 }
826 this._attachedPortal = portal;
827 },
828 enumerable: false,
829 configurable: true
830 });
831 Object.defineProperty(CdkPortalOutlet.prototype, "attachedRef", {
832 /** Component or view reference that is attached to the portal. */
833 get: function () {
834 return this._attachedRef;
835 },
836 enumerable: false,
837 configurable: true
838 });
839 CdkPortalOutlet.prototype.ngOnInit = function () {
840 this._isInitialized = true;
841 };
842 CdkPortalOutlet.prototype.ngOnDestroy = function () {
843 _super.prototype.dispose.call(this);
844 this._attachedPortal = null;
845 this._attachedRef = null;
846 };
847 /**
848 * Attach the given ComponentPortal to this PortalOutlet using the ComponentFactoryResolver.
849 *
850 * @param portal Portal to be attached to the portal outlet.
851 * @returns Reference to the created component.
852 */
853 CdkPortalOutlet.prototype.attachComponentPortal = function (portal) {
854 portal.setAttachedHost(this);
855 // If the portal specifies an origin, use that as the logical location of the component
856 // in the application tree. Otherwise use the location of this PortalOutlet.
857 var viewContainerRef = portal.viewContainerRef != null ?
858 portal.viewContainerRef :
859 this._viewContainerRef;
860 var resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
861 var componentFactory = resolver.resolveComponentFactory(portal.component);
862 var ref = viewContainerRef.createComponent(componentFactory, viewContainerRef.length, portal.injector || viewContainerRef.injector);
863 // If we're using a view container that's different from the injected one (e.g. when the portal
864 // specifies its own) we need to move the component into the outlet, otherwise it'll be rendered
865 // inside of the alternate view container.
866 if (viewContainerRef !== this._viewContainerRef) {
867 this._getRootNode().appendChild(ref.hostView.rootNodes[0]);
868 }
869 _super.prototype.setDisposeFn.call(this, function () { return ref.destroy(); });
870 this._attachedPortal = portal;
871 this._attachedRef = ref;
872 this.attached.emit(ref);
873 return ref;
874 };
875 /**
876 * Attach the given TemplatePortal to this PortalHost as an embedded View.
877 * @param portal Portal to be attached.
878 * @returns Reference to the created embedded view.
879 */
880 CdkPortalOutlet.prototype.attachTemplatePortal = function (portal) {
881 var _this = this;
882 portal.setAttachedHost(this);
883 var viewRef = this._viewContainerRef.createEmbeddedView(portal.templateRef, portal.context);
884 _super.prototype.setDisposeFn.call(this, function () { return _this._viewContainerRef.clear(); });
885 this._attachedPortal = portal;
886 this._attachedRef = viewRef;
887 this.attached.emit(viewRef);
888 return viewRef;
889 };
890 /** Gets the root node of the portal outlet. */
891 CdkPortalOutlet.prototype._getRootNode = function () {
892 var nativeElement = this._viewContainerRef.element.nativeElement;
893 // The directive could be set on a template which will result in a comment
894 // node being the root. Use the comment's parent node if that is the case.
895 return (nativeElement.nodeType === nativeElement.ELEMENT_NODE ?
896 nativeElement : nativeElement.parentNode);
897 };
898 return CdkPortalOutlet;
899 }(BasePortalOutlet));
900 CdkPortalOutlet.decorators = [
901 { type: core.Directive, args: [{
902 selector: '[cdkPortalOutlet]',
903 exportAs: 'cdkPortalOutlet',
904 inputs: ['portal: cdkPortalOutlet']
905 },] }
906 ];
907 CdkPortalOutlet.ctorParameters = function () { return [
908 { type: core.ComponentFactoryResolver },
909 { type: core.ViewContainerRef },
910 { type: undefined, decorators: [{ type: core.Inject, args: [common.DOCUMENT,] }] }
911 ]; };
912 CdkPortalOutlet.propDecorators = {
913 attached: [{ type: core.Output }]
914 };
915 /**
916 * @deprecated Use `CdkPortalOutlet` instead.
917 * @breaking-change 9.0.0
918 */
919 var PortalHostDirective = /** @class */ (function (_super) {
920 __extends(PortalHostDirective, _super);
921 function PortalHostDirective() {
922 return _super !== null && _super.apply(this, arguments) || this;
923 }
924 return PortalHostDirective;
925 }(CdkPortalOutlet));
926 PortalHostDirective.decorators = [
927 { type: core.Directive, args: [{
928 selector: '[cdkPortalHost], [portalHost]',
929 exportAs: 'cdkPortalHost',
930 inputs: ['portal: cdkPortalHost'],
931 providers: [{
932 provide: CdkPortalOutlet,
933 useExisting: PortalHostDirective
934 }]
935 },] }
936 ];
937 var PortalModule = /** @class */ (function () {
938 function PortalModule() {
939 }
940 return PortalModule;
941 }());
942 PortalModule.decorators = [
943 { type: core.NgModule, args: [{
944 exports: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective],
945 declarations: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective],
946 },] }
947 ];
948
949 /**
950 * @license
951 * Copyright Google LLC All Rights Reserved.
952 *
953 * Use of this source code is governed by an MIT-style license that can be
954 * found in the LICENSE file at https://angular.io/license
955 */
956 /**
957 * Custom injector to be used when providing custom
958 * injection tokens to components inside a portal.
959 * @docs-private
960 * @deprecated Use `Injector.create` instead.
961 * @breaking-change 11.0.0
962 */
963 var PortalInjector = /** @class */ (function () {
964 function PortalInjector(_parentInjector, _customTokens) {
965 this._parentInjector = _parentInjector;
966 this._customTokens = _customTokens;
967 }
968 PortalInjector.prototype.get = function (token, notFoundValue) {
969 var value = this._customTokens.get(token);
970 if (typeof value !== 'undefined') {
971 return value;
972 }
973 return this._parentInjector.get(token, notFoundValue);
974 };
975 return PortalInjector;
976 }());
977
978 /**
979 * @license
980 * Copyright Google LLC All Rights Reserved.
981 *
982 * Use of this source code is governed by an MIT-style license that can be
983 * found in the LICENSE file at https://angular.io/license
984 */
985
986 /**
987 * Generated bundle index. Do not edit.
988 */
989
990 exports.BasePortalHost = BasePortalHost;
991 exports.BasePortalOutlet = BasePortalOutlet;
992 exports.CdkPortal = CdkPortal;
993 exports.CdkPortalOutlet = CdkPortalOutlet;
994 exports.ComponentPortal = ComponentPortal;
995 exports.DomPortal = DomPortal;
996 exports.DomPortalHost = DomPortalHost;
997 exports.DomPortalOutlet = DomPortalOutlet;
998 exports.Portal = Portal;
999 exports.PortalHostDirective = PortalHostDirective;
1000 exports.PortalInjector = PortalInjector;
1001 exports.PortalModule = PortalModule;
1002 exports.TemplatePortal = TemplatePortal;
1003 exports.TemplatePortalDirective = TemplatePortalDirective;
1004
1005 Object.defineProperty(exports, '__esModule', { value: true });
1006
1007})));
1008//# sourceMappingURL=cdk-portal.umd.js.map