1 | import { ElementRef, Directive, TemplateRef, ViewContainerRef, EventEmitter, ComponentFactoryResolver, Inject, Output, NgModule } from '@angular/core';
|
---|
2 | import { DOCUMENT } from '@angular/common';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * @license
|
---|
6 | * Copyright Google LLC All Rights Reserved.
|
---|
7 | *
|
---|
8 | * Use of this source code is governed by an MIT-style license that can be
|
---|
9 | * found in the LICENSE file at https://angular.io/license
|
---|
10 | */
|
---|
11 | /**
|
---|
12 | * Throws an exception when attempting to attach a null portal to a host.
|
---|
13 | * @docs-private
|
---|
14 | */
|
---|
15 | import * as ɵngcc0 from '@angular/core';
|
---|
16 | function throwNullPortalError() {
|
---|
17 | throw Error('Must provide a portal to attach');
|
---|
18 | }
|
---|
19 | /**
|
---|
20 | * Throws an exception when attempting to attach a portal to a host that is already attached.
|
---|
21 | * @docs-private
|
---|
22 | */
|
---|
23 | function throwPortalAlreadyAttachedError() {
|
---|
24 | throw Error('Host already has a portal attached');
|
---|
25 | }
|
---|
26 | /**
|
---|
27 | * Throws an exception when attempting to attach a portal to an already-disposed host.
|
---|
28 | * @docs-private
|
---|
29 | */
|
---|
30 | function throwPortalOutletAlreadyDisposedError() {
|
---|
31 | throw Error('This PortalOutlet has already been disposed');
|
---|
32 | }
|
---|
33 | /**
|
---|
34 | * Throws an exception when attempting to attach an unknown portal type.
|
---|
35 | * @docs-private
|
---|
36 | */
|
---|
37 | function throwUnknownPortalTypeError() {
|
---|
38 | throw Error('Attempting to attach an unknown Portal type. BasePortalOutlet accepts either ' +
|
---|
39 | 'a ComponentPortal or a TemplatePortal.');
|
---|
40 | }
|
---|
41 | /**
|
---|
42 | * Throws an exception when attempting to attach a portal to a null host.
|
---|
43 | * @docs-private
|
---|
44 | */
|
---|
45 | function throwNullPortalOutletError() {
|
---|
46 | throw Error('Attempting to attach a portal to a null PortalOutlet');
|
---|
47 | }
|
---|
48 | /**
|
---|
49 | * Throws an exception when attempting to detach a portal that is not attached.
|
---|
50 | * @docs-private
|
---|
51 | */
|
---|
52 | function throwNoPortalAttachedError() {
|
---|
53 | throw Error('Attempting to detach a portal that is not attached to a host');
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @license
|
---|
58 | * Copyright Google LLC All Rights Reserved.
|
---|
59 | *
|
---|
60 | * Use of this source code is governed by an MIT-style license that can be
|
---|
61 | * found in the LICENSE file at https://angular.io/license
|
---|
62 | */
|
---|
63 | /**
|
---|
64 | * A `Portal` is something that you want to render somewhere else.
|
---|
65 | * It can be attach to / detached from a `PortalOutlet`.
|
---|
66 | */
|
---|
67 | class Portal {
|
---|
68 | /** Attach this portal to a host. */
|
---|
69 | attach(host) {
|
---|
70 | if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
---|
71 | if (host == null) {
|
---|
72 | throwNullPortalOutletError();
|
---|
73 | }
|
---|
74 | if (host.hasAttached()) {
|
---|
75 | throwPortalAlreadyAttachedError();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | this._attachedHost = host;
|
---|
79 | return host.attach(this);
|
---|
80 | }
|
---|
81 | /** Detach this portal from its host */
|
---|
82 | detach() {
|
---|
83 | let host = this._attachedHost;
|
---|
84 | if (host != null) {
|
---|
85 | this._attachedHost = null;
|
---|
86 | host.detach();
|
---|
87 | }
|
---|
88 | else if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
---|
89 | throwNoPortalAttachedError();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | /** Whether this portal is attached to a host. */
|
---|
93 | get isAttached() {
|
---|
94 | return this._attachedHost != null;
|
---|
95 | }
|
---|
96 | /**
|
---|
97 | * Sets the PortalOutlet reference without performing `attach()`. This is used directly by
|
---|
98 | * the PortalOutlet when it is performing an `attach()` or `detach()`.
|
---|
99 | */
|
---|
100 | setAttachedHost(host) {
|
---|
101 | this._attachedHost = host;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | /**
|
---|
105 | * A `ComponentPortal` is a portal that instantiates some Component upon attachment.
|
---|
106 | */
|
---|
107 | class ComponentPortal extends Portal {
|
---|
108 | constructor(component, viewContainerRef, injector, componentFactoryResolver) {
|
---|
109 | super();
|
---|
110 | this.component = component;
|
---|
111 | this.viewContainerRef = viewContainerRef;
|
---|
112 | this.injector = injector;
|
---|
113 | this.componentFactoryResolver = componentFactoryResolver;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | /**
|
---|
117 | * A `TemplatePortal` is a portal that represents some embedded template (TemplateRef).
|
---|
118 | */
|
---|
119 | class TemplatePortal extends Portal {
|
---|
120 | constructor(template, viewContainerRef, context) {
|
---|
121 | super();
|
---|
122 | this.templateRef = template;
|
---|
123 | this.viewContainerRef = viewContainerRef;
|
---|
124 | this.context = context;
|
---|
125 | }
|
---|
126 | get origin() {
|
---|
127 | return this.templateRef.elementRef;
|
---|
128 | }
|
---|
129 | /**
|
---|
130 | * Attach the portal to the provided `PortalOutlet`.
|
---|
131 | * When a context is provided it will override the `context` property of the `TemplatePortal`
|
---|
132 | * instance.
|
---|
133 | */
|
---|
134 | attach(host, context = this.context) {
|
---|
135 | this.context = context;
|
---|
136 | return super.attach(host);
|
---|
137 | }
|
---|
138 | detach() {
|
---|
139 | this.context = undefined;
|
---|
140 | return super.detach();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | /**
|
---|
144 | * A `DomPortal` is a portal whose DOM element will be taken from its current position
|
---|
145 | * in the DOM and moved into a portal outlet, when it is attached. On detach, the content
|
---|
146 | * will be restored to its original position.
|
---|
147 | */
|
---|
148 | class DomPortal extends Portal {
|
---|
149 | constructor(element) {
|
---|
150 | super();
|
---|
151 | this.element = element instanceof ElementRef ? element.nativeElement : element;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | /**
|
---|
155 | * Partial implementation of PortalOutlet that handles attaching
|
---|
156 | * ComponentPortal and TemplatePortal.
|
---|
157 | */
|
---|
158 | class BasePortalOutlet {
|
---|
159 | constructor() {
|
---|
160 | /** Whether this host has already been permanently disposed. */
|
---|
161 | this._isDisposed = false;
|
---|
162 | // @breaking-change 10.0.0 `attachDomPortal` to become a required abstract method.
|
---|
163 | this.attachDomPortal = null;
|
---|
164 | }
|
---|
165 | /** Whether this host has an attached portal. */
|
---|
166 | hasAttached() {
|
---|
167 | return !!this._attachedPortal;
|
---|
168 | }
|
---|
169 | /** Attaches a portal. */
|
---|
170 | attach(portal) {
|
---|
171 | if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
---|
172 | if (!portal) {
|
---|
173 | throwNullPortalError();
|
---|
174 | }
|
---|
175 | if (this.hasAttached()) {
|
---|
176 | throwPortalAlreadyAttachedError();
|
---|
177 | }
|
---|
178 | if (this._isDisposed) {
|
---|
179 | throwPortalOutletAlreadyDisposedError();
|
---|
180 | }
|
---|
181 | }
|
---|
182 | if (portal instanceof ComponentPortal) {
|
---|
183 | this._attachedPortal = portal;
|
---|
184 | return this.attachComponentPortal(portal);
|
---|
185 | }
|
---|
186 | else if (portal instanceof TemplatePortal) {
|
---|
187 | this._attachedPortal = portal;
|
---|
188 | return this.attachTemplatePortal(portal);
|
---|
189 | // @breaking-change 10.0.0 remove null check for `this.attachDomPortal`.
|
---|
190 | }
|
---|
191 | else if (this.attachDomPortal && portal instanceof DomPortal) {
|
---|
192 | this._attachedPortal = portal;
|
---|
193 | return this.attachDomPortal(portal);
|
---|
194 | }
|
---|
195 | if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
---|
196 | throwUnknownPortalTypeError();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | /** Detaches a previously attached portal. */
|
---|
200 | detach() {
|
---|
201 | if (this._attachedPortal) {
|
---|
202 | this._attachedPortal.setAttachedHost(null);
|
---|
203 | this._attachedPortal = null;
|
---|
204 | }
|
---|
205 | this._invokeDisposeFn();
|
---|
206 | }
|
---|
207 | /** Permanently dispose of this portal host. */
|
---|
208 | dispose() {
|
---|
209 | if (this.hasAttached()) {
|
---|
210 | this.detach();
|
---|
211 | }
|
---|
212 | this._invokeDisposeFn();
|
---|
213 | this._isDisposed = true;
|
---|
214 | }
|
---|
215 | /** @docs-private */
|
---|
216 | setDisposeFn(fn) {
|
---|
217 | this._disposeFn = fn;
|
---|
218 | }
|
---|
219 | _invokeDisposeFn() {
|
---|
220 | if (this._disposeFn) {
|
---|
221 | this._disposeFn();
|
---|
222 | this._disposeFn = null;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | /**
|
---|
227 | * @deprecated Use `BasePortalOutlet` instead.
|
---|
228 | * @breaking-change 9.0.0
|
---|
229 | */
|
---|
230 | class BasePortalHost extends BasePortalOutlet {
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * @license
|
---|
235 | * Copyright Google LLC All Rights Reserved.
|
---|
236 | *
|
---|
237 | * Use of this source code is governed by an MIT-style license that can be
|
---|
238 | * found in the LICENSE file at https://angular.io/license
|
---|
239 | */
|
---|
240 | /**
|
---|
241 | * A PortalOutlet for attaching portals to an arbitrary DOM element outside of the Angular
|
---|
242 | * application context.
|
---|
243 | */
|
---|
244 | class DomPortalOutlet extends BasePortalOutlet {
|
---|
245 | constructor(
|
---|
246 | /** Element into which the content is projected. */
|
---|
247 | outletElement, _componentFactoryResolver, _appRef, _defaultInjector,
|
---|
248 | /**
|
---|
249 | * @deprecated `_document` Parameter to be made required.
|
---|
250 | * @breaking-change 10.0.0
|
---|
251 | */
|
---|
252 | _document) {
|
---|
253 | super();
|
---|
254 | this.outletElement = outletElement;
|
---|
255 | this._componentFactoryResolver = _componentFactoryResolver;
|
---|
256 | this._appRef = _appRef;
|
---|
257 | this._defaultInjector = _defaultInjector;
|
---|
258 | /**
|
---|
259 | * Attaches a DOM portal by transferring its content into the outlet.
|
---|
260 | * @param portal Portal to be attached.
|
---|
261 | * @deprecated To be turned into a method.
|
---|
262 | * @breaking-change 10.0.0
|
---|
263 | */
|
---|
264 | this.attachDomPortal = (portal) => {
|
---|
265 | // @breaking-change 10.0.0 Remove check and error once the
|
---|
266 | // `_document` constructor parameter is required.
|
---|
267 | if (!this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
268 | throw Error('Cannot attach DOM portal without _document constructor parameter');
|
---|
269 | }
|
---|
270 | const element = portal.element;
|
---|
271 | if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
272 | throw Error('DOM portal content must be attached to a parent node.');
|
---|
273 | }
|
---|
274 | // Anchor used to save the element's previous position so
|
---|
275 | // that we can restore it when the portal is detached.
|
---|
276 | const anchorNode = this._document.createComment('dom-portal');
|
---|
277 | element.parentNode.insertBefore(anchorNode, element);
|
---|
278 | this.outletElement.appendChild(element);
|
---|
279 | this._attachedPortal = portal;
|
---|
280 | super.setDisposeFn(() => {
|
---|
281 | // We can't use `replaceWith` here because IE doesn't support it.
|
---|
282 | if (anchorNode.parentNode) {
|
---|
283 | anchorNode.parentNode.replaceChild(element, anchorNode);
|
---|
284 | }
|
---|
285 | });
|
---|
286 | };
|
---|
287 | this._document = _document;
|
---|
288 | }
|
---|
289 | /**
|
---|
290 | * Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver.
|
---|
291 | * @param portal Portal to be attached
|
---|
292 | * @returns Reference to the created component.
|
---|
293 | */
|
---|
294 | attachComponentPortal(portal) {
|
---|
295 | const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
|
---|
296 | const componentFactory = resolver.resolveComponentFactory(portal.component);
|
---|
297 | let componentRef;
|
---|
298 | // If the portal specifies a ViewContainerRef, we will use that as the attachment point
|
---|
299 | // for the component (in terms of Angular's component tree, not rendering).
|
---|
300 | // When the ViewContainerRef is missing, we use the factory to create the component directly
|
---|
301 | // and then manually attach the view to the application.
|
---|
302 | if (portal.viewContainerRef) {
|
---|
303 | componentRef = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.injector);
|
---|
304 | this.setDisposeFn(() => componentRef.destroy());
|
---|
305 | }
|
---|
306 | else {
|
---|
307 | componentRef = componentFactory.create(portal.injector || this._defaultInjector);
|
---|
308 | this._appRef.attachView(componentRef.hostView);
|
---|
309 | this.setDisposeFn(() => {
|
---|
310 | this._appRef.detachView(componentRef.hostView);
|
---|
311 | componentRef.destroy();
|
---|
312 | });
|
---|
313 | }
|
---|
314 | // At this point the component has been instantiated, so we move it to the location in the DOM
|
---|
315 | // where we want it to be rendered.
|
---|
316 | this.outletElement.appendChild(this._getComponentRootNode(componentRef));
|
---|
317 | this._attachedPortal = portal;
|
---|
318 | return componentRef;
|
---|
319 | }
|
---|
320 | /**
|
---|
321 | * Attaches a template portal to the DOM as an embedded view.
|
---|
322 | * @param portal Portal to be attached.
|
---|
323 | * @returns Reference to the created embedded view.
|
---|
324 | */
|
---|
325 | attachTemplatePortal(portal) {
|
---|
326 | let viewContainer = portal.viewContainerRef;
|
---|
327 | let viewRef = viewContainer.createEmbeddedView(portal.templateRef, portal.context);
|
---|
328 | // The method `createEmbeddedView` will add the view as a child of the viewContainer.
|
---|
329 | // But for the DomPortalOutlet the view can be added everywhere in the DOM
|
---|
330 | // (e.g Overlay Container) To move the view to the specified host element. We just
|
---|
331 | // re-append the existing root nodes.
|
---|
332 | viewRef.rootNodes.forEach(rootNode => this.outletElement.appendChild(rootNode));
|
---|
333 | // Note that we want to detect changes after the nodes have been moved so that
|
---|
334 | // any directives inside the portal that are looking at the DOM inside a lifecycle
|
---|
335 | // hook won't be invoked too early.
|
---|
336 | viewRef.detectChanges();
|
---|
337 | this.setDisposeFn((() => {
|
---|
338 | let index = viewContainer.indexOf(viewRef);
|
---|
339 | if (index !== -1) {
|
---|
340 | viewContainer.remove(index);
|
---|
341 | }
|
---|
342 | }));
|
---|
343 | this._attachedPortal = portal;
|
---|
344 | // TODO(jelbourn): Return locals from view.
|
---|
345 | return viewRef;
|
---|
346 | }
|
---|
347 | /**
|
---|
348 | * Clears out a portal from the DOM.
|
---|
349 | */
|
---|
350 | dispose() {
|
---|
351 | super.dispose();
|
---|
352 | if (this.outletElement.parentNode != null) {
|
---|
353 | this.outletElement.parentNode.removeChild(this.outletElement);
|
---|
354 | }
|
---|
355 | }
|
---|
356 | /** Gets the root HTMLElement for an instantiated component. */
|
---|
357 | _getComponentRootNode(componentRef) {
|
---|
358 | return componentRef.hostView.rootNodes[0];
|
---|
359 | }
|
---|
360 | }
|
---|
361 | /**
|
---|
362 | * @deprecated Use `DomPortalOutlet` instead.
|
---|
363 | * @breaking-change 9.0.0
|
---|
364 | */
|
---|
365 | class DomPortalHost extends DomPortalOutlet {
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * @license
|
---|
370 | * Copyright Google LLC All Rights Reserved.
|
---|
371 | *
|
---|
372 | * Use of this source code is governed by an MIT-style license that can be
|
---|
373 | * found in the LICENSE file at https://angular.io/license
|
---|
374 | */
|
---|
375 | /**
|
---|
376 | * Directive version of a `TemplatePortal`. Because the directive *is* a TemplatePortal,
|
---|
377 | * the directive instance itself can be attached to a host, enabling declarative use of portals.
|
---|
378 | */
|
---|
379 | class CdkPortal extends TemplatePortal {
|
---|
380 | constructor(templateRef, viewContainerRef) {
|
---|
381 | super(templateRef, viewContainerRef);
|
---|
382 | }
|
---|
383 | }
|
---|
384 | CdkPortal.ɵfac = function CdkPortal_Factory(t) { return new (t || CdkPortal)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.TemplateRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ViewContainerRef)); };
|
---|
385 | CdkPortal.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: CdkPortal, selectors: [["", "cdkPortal", ""]], exportAs: ["cdkPortal"], features: [ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
386 | CdkPortal.ctorParameters = () => [
|
---|
387 | { type: TemplateRef },
|
---|
388 | { type: ViewContainerRef }
|
---|
389 | ];
|
---|
390 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(CdkPortal, [{
|
---|
391 | type: Directive,
|
---|
392 | args: [{
|
---|
393 | selector: '[cdkPortal]',
|
---|
394 | exportAs: 'cdkPortal'
|
---|
395 | }]
|
---|
396 | }], function () { return [{ type: ɵngcc0.TemplateRef }, { type: ɵngcc0.ViewContainerRef }]; }, null); })();
|
---|
397 | /**
|
---|
398 | * @deprecated Use `CdkPortal` instead.
|
---|
399 | * @breaking-change 9.0.0
|
---|
400 | */
|
---|
401 | class TemplatePortalDirective extends CdkPortal {
|
---|
402 | }
|
---|
403 | TemplatePortalDirective.ɵfac = /*@__PURE__*/ function () { let ɵTemplatePortalDirective_BaseFactory; return function TemplatePortalDirective_Factory(t) { return (ɵTemplatePortalDirective_BaseFactory || (ɵTemplatePortalDirective_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(TemplatePortalDirective)))(t || TemplatePortalDirective); }; }();
|
---|
404 | TemplatePortalDirective.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: TemplatePortalDirective, selectors: [["", "cdk-portal", ""], ["", "portal", ""]], exportAs: ["cdkPortal"], features: [ɵngcc0.ɵɵProvidersFeature([{
|
---|
405 | provide: CdkPortal,
|
---|
406 | useExisting: TemplatePortalDirective
|
---|
407 | }]), ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
408 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(TemplatePortalDirective, [{
|
---|
409 | type: Directive,
|
---|
410 | args: [{
|
---|
411 | selector: '[cdk-portal], [portal]',
|
---|
412 | exportAs: 'cdkPortal',
|
---|
413 | providers: [{
|
---|
414 | provide: CdkPortal,
|
---|
415 | useExisting: TemplatePortalDirective
|
---|
416 | }]
|
---|
417 | }]
|
---|
418 | }], null, null); })();
|
---|
419 | /**
|
---|
420 | * Directive version of a PortalOutlet. Because the directive *is* a PortalOutlet, portals can be
|
---|
421 | * directly attached to it, enabling declarative use.
|
---|
422 | *
|
---|
423 | * Usage:
|
---|
424 | * `<ng-template [cdkPortalOutlet]="greeting"></ng-template>`
|
---|
425 | */
|
---|
426 | class CdkPortalOutlet extends BasePortalOutlet {
|
---|
427 | constructor(_componentFactoryResolver, _viewContainerRef,
|
---|
428 | /**
|
---|
429 | * @deprecated `_document` parameter to be made required.
|
---|
430 | * @breaking-change 9.0.0
|
---|
431 | */
|
---|
432 | _document) {
|
---|
433 | super();
|
---|
434 | this._componentFactoryResolver = _componentFactoryResolver;
|
---|
435 | this._viewContainerRef = _viewContainerRef;
|
---|
436 | /** Whether the portal component is initialized. */
|
---|
437 | this._isInitialized = false;
|
---|
438 | /** Emits when a portal is attached to the outlet. */
|
---|
439 | this.attached = new EventEmitter();
|
---|
440 | /**
|
---|
441 | * Attaches the given DomPortal to this PortalHost by moving all of the portal content into it.
|
---|
442 | * @param portal Portal to be attached.
|
---|
443 | * @deprecated To be turned into a method.
|
---|
444 | * @breaking-change 10.0.0
|
---|
445 | */
|
---|
446 | this.attachDomPortal = (portal) => {
|
---|
447 | // @breaking-change 9.0.0 Remove check and error once the
|
---|
448 | // `_document` constructor parameter is required.
|
---|
449 | if (!this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
450 | throw Error('Cannot attach DOM portal without _document constructor parameter');
|
---|
451 | }
|
---|
452 | const element = portal.element;
|
---|
453 | if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
454 | throw Error('DOM portal content must be attached to a parent node.');
|
---|
455 | }
|
---|
456 | // Anchor used to save the element's previous position so
|
---|
457 | // that we can restore it when the portal is detached.
|
---|
458 | const anchorNode = this._document.createComment('dom-portal');
|
---|
459 | portal.setAttachedHost(this);
|
---|
460 | element.parentNode.insertBefore(anchorNode, element);
|
---|
461 | this._getRootNode().appendChild(element);
|
---|
462 | this._attachedPortal = portal;
|
---|
463 | super.setDisposeFn(() => {
|
---|
464 | if (anchorNode.parentNode) {
|
---|
465 | anchorNode.parentNode.replaceChild(element, anchorNode);
|
---|
466 | }
|
---|
467 | });
|
---|
468 | };
|
---|
469 | this._document = _document;
|
---|
470 | }
|
---|
471 | /** Portal associated with the Portal outlet. */
|
---|
472 | get portal() {
|
---|
473 | return this._attachedPortal;
|
---|
474 | }
|
---|
475 | set portal(portal) {
|
---|
476 | // Ignore the cases where the `portal` is set to a falsy value before the lifecycle hooks have
|
---|
477 | // run. This handles the cases where the user might do something like `<div cdkPortalOutlet>`
|
---|
478 | // and attach a portal programmatically in the parent component. When Angular does the first CD
|
---|
479 | // round, it will fire the setter with empty string, causing the user's content to be cleared.
|
---|
480 | if (this.hasAttached() && !portal && !this._isInitialized) {
|
---|
481 | return;
|
---|
482 | }
|
---|
483 | if (this.hasAttached()) {
|
---|
484 | super.detach();
|
---|
485 | }
|
---|
486 | if (portal) {
|
---|
487 | super.attach(portal);
|
---|
488 | }
|
---|
489 | this._attachedPortal = portal;
|
---|
490 | }
|
---|
491 | /** Component or view reference that is attached to the portal. */
|
---|
492 | get attachedRef() {
|
---|
493 | return this._attachedRef;
|
---|
494 | }
|
---|
495 | ngOnInit() {
|
---|
496 | this._isInitialized = true;
|
---|
497 | }
|
---|
498 | ngOnDestroy() {
|
---|
499 | super.dispose();
|
---|
500 | this._attachedPortal = null;
|
---|
501 | this._attachedRef = null;
|
---|
502 | }
|
---|
503 | /**
|
---|
504 | * Attach the given ComponentPortal to this PortalOutlet using the ComponentFactoryResolver.
|
---|
505 | *
|
---|
506 | * @param portal Portal to be attached to the portal outlet.
|
---|
507 | * @returns Reference to the created component.
|
---|
508 | */
|
---|
509 | attachComponentPortal(portal) {
|
---|
510 | portal.setAttachedHost(this);
|
---|
511 | // If the portal specifies an origin, use that as the logical location of the component
|
---|
512 | // in the application tree. Otherwise use the location of this PortalOutlet.
|
---|
513 | const viewContainerRef = portal.viewContainerRef != null ?
|
---|
514 | portal.viewContainerRef :
|
---|
515 | this._viewContainerRef;
|
---|
516 | const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
|
---|
517 | const componentFactory = resolver.resolveComponentFactory(portal.component);
|
---|
518 | const ref = viewContainerRef.createComponent(componentFactory, viewContainerRef.length, portal.injector || viewContainerRef.injector);
|
---|
519 | // If we're using a view container that's different from the injected one (e.g. when the portal
|
---|
520 | // specifies its own) we need to move the component into the outlet, otherwise it'll be rendered
|
---|
521 | // inside of the alternate view container.
|
---|
522 | if (viewContainerRef !== this._viewContainerRef) {
|
---|
523 | this._getRootNode().appendChild(ref.hostView.rootNodes[0]);
|
---|
524 | }
|
---|
525 | super.setDisposeFn(() => ref.destroy());
|
---|
526 | this._attachedPortal = portal;
|
---|
527 | this._attachedRef = ref;
|
---|
528 | this.attached.emit(ref);
|
---|
529 | return ref;
|
---|
530 | }
|
---|
531 | /**
|
---|
532 | * Attach the given TemplatePortal to this PortalHost as an embedded View.
|
---|
533 | * @param portal Portal to be attached.
|
---|
534 | * @returns Reference to the created embedded view.
|
---|
535 | */
|
---|
536 | attachTemplatePortal(portal) {
|
---|
537 | portal.setAttachedHost(this);
|
---|
538 | const viewRef = this._viewContainerRef.createEmbeddedView(portal.templateRef, portal.context);
|
---|
539 | super.setDisposeFn(() => this._viewContainerRef.clear());
|
---|
540 | this._attachedPortal = portal;
|
---|
541 | this._attachedRef = viewRef;
|
---|
542 | this.attached.emit(viewRef);
|
---|
543 | return viewRef;
|
---|
544 | }
|
---|
545 | /** Gets the root node of the portal outlet. */
|
---|
546 | _getRootNode() {
|
---|
547 | const nativeElement = this._viewContainerRef.element.nativeElement;
|
---|
548 | // The directive could be set on a template which will result in a comment
|
---|
549 | // node being the root. Use the comment's parent node if that is the case.
|
---|
550 | return (nativeElement.nodeType === nativeElement.ELEMENT_NODE ?
|
---|
551 | nativeElement : nativeElement.parentNode);
|
---|
552 | }
|
---|
553 | }
|
---|
554 | CdkPortalOutlet.ɵfac = function CdkPortalOutlet_Factory(t) { return new (t || CdkPortalOutlet)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ComponentFactoryResolver), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ViewContainerRef), ɵngcc0.ɵɵdirectiveInject(DOCUMENT)); };
|
---|
555 | CdkPortalOutlet.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: CdkPortalOutlet, selectors: [["", "cdkPortalOutlet", ""]], inputs: { portal: ["cdkPortalOutlet", "portal"] }, outputs: { attached: "attached" }, exportAs: ["cdkPortalOutlet"], features: [ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
556 | CdkPortalOutlet.ctorParameters = () => [
|
---|
557 | { type: ComponentFactoryResolver },
|
---|
558 | { type: ViewContainerRef },
|
---|
559 | { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] }
|
---|
560 | ];
|
---|
561 | CdkPortalOutlet.propDecorators = {
|
---|
562 | attached: [{ type: Output }]
|
---|
563 | };
|
---|
564 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(CdkPortalOutlet, [{
|
---|
565 | type: Directive,
|
---|
566 | args: [{
|
---|
567 | selector: '[cdkPortalOutlet]',
|
---|
568 | exportAs: 'cdkPortalOutlet',
|
---|
569 | inputs: ['portal: cdkPortalOutlet']
|
---|
570 | }]
|
---|
571 | }], function () { return [{ type: ɵngcc0.ComponentFactoryResolver }, { type: ɵngcc0.ViewContainerRef }, { type: undefined, decorators: [{
|
---|
572 | type: Inject,
|
---|
573 | args: [DOCUMENT]
|
---|
574 | }] }]; }, { attached: [{
|
---|
575 | type: Output
|
---|
576 | }] }); })();
|
---|
577 | /**
|
---|
578 | * @deprecated Use `CdkPortalOutlet` instead.
|
---|
579 | * @breaking-change 9.0.0
|
---|
580 | */
|
---|
581 | class PortalHostDirective extends CdkPortalOutlet {
|
---|
582 | }
|
---|
583 | PortalHostDirective.ɵfac = /*@__PURE__*/ function () { let ɵPortalHostDirective_BaseFactory; return function PortalHostDirective_Factory(t) { return (ɵPortalHostDirective_BaseFactory || (ɵPortalHostDirective_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(PortalHostDirective)))(t || PortalHostDirective); }; }();
|
---|
584 | PortalHostDirective.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: PortalHostDirective, selectors: [["", "cdkPortalHost", ""], ["", "portalHost", ""]], inputs: { portal: ["cdkPortalHost", "portal"] }, exportAs: ["cdkPortalHost"], features: [ɵngcc0.ɵɵProvidersFeature([{
|
---|
585 | provide: CdkPortalOutlet,
|
---|
586 | useExisting: PortalHostDirective
|
---|
587 | }]), ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
588 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(PortalHostDirective, [{
|
---|
589 | type: Directive,
|
---|
590 | args: [{
|
---|
591 | selector: '[cdkPortalHost], [portalHost]',
|
---|
592 | exportAs: 'cdkPortalHost',
|
---|
593 | inputs: ['portal: cdkPortalHost'],
|
---|
594 | providers: [{
|
---|
595 | provide: CdkPortalOutlet,
|
---|
596 | useExisting: PortalHostDirective
|
---|
597 | }]
|
---|
598 | }]
|
---|
599 | }], null, null); })();
|
---|
600 | class PortalModule {
|
---|
601 | }
|
---|
602 | PortalModule.ɵfac = function PortalModule_Factory(t) { return new (t || PortalModule)(); };
|
---|
603 | PortalModule.ɵmod = /*@__PURE__*/ ɵngcc0.ɵɵdefineNgModule({ type: PortalModule });
|
---|
604 | PortalModule.ɵinj = /*@__PURE__*/ ɵngcc0.ɵɵdefineInjector({});
|
---|
605 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(PortalModule, [{
|
---|
606 | type: NgModule,
|
---|
607 | args: [{
|
---|
608 | exports: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective],
|
---|
609 | declarations: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective]
|
---|
610 | }]
|
---|
611 | }], null, null); })();
|
---|
612 | (function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵngcc0.ɵɵsetNgModuleScope(PortalModule, { declarations: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective], exports: [CdkPortal, CdkPortalOutlet, TemplatePortalDirective, PortalHostDirective] }); })();
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * @license
|
---|
616 | * Copyright Google LLC All Rights Reserved.
|
---|
617 | *
|
---|
618 | * Use of this source code is governed by an MIT-style license that can be
|
---|
619 | * found in the LICENSE file at https://angular.io/license
|
---|
620 | */
|
---|
621 | /**
|
---|
622 | * Custom injector to be used when providing custom
|
---|
623 | * injection tokens to components inside a portal.
|
---|
624 | * @docs-private
|
---|
625 | * @deprecated Use `Injector.create` instead.
|
---|
626 | * @breaking-change 11.0.0
|
---|
627 | */
|
---|
628 | class PortalInjector {
|
---|
629 | constructor(_parentInjector, _customTokens) {
|
---|
630 | this._parentInjector = _parentInjector;
|
---|
631 | this._customTokens = _customTokens;
|
---|
632 | }
|
---|
633 | get(token, notFoundValue) {
|
---|
634 | const value = this._customTokens.get(token);
|
---|
635 | if (typeof value !== 'undefined') {
|
---|
636 | return value;
|
---|
637 | }
|
---|
638 | return this._parentInjector.get(token, notFoundValue);
|
---|
639 | }
|
---|
640 | }
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * @license
|
---|
644 | * Copyright Google LLC All Rights Reserved.
|
---|
645 | *
|
---|
646 | * Use of this source code is governed by an MIT-style license that can be
|
---|
647 | * found in the LICENSE file at https://angular.io/license
|
---|
648 | */
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * Generated bundle index. Do not edit.
|
---|
652 | */
|
---|
653 |
|
---|
654 | export { BasePortalHost, BasePortalOutlet, CdkPortal, CdkPortalOutlet, ComponentPortal, DomPortal, DomPortalHost, DomPortalOutlet, Portal, PortalHostDirective, PortalInjector, PortalModule, TemplatePortal, TemplatePortalDirective };
|
---|
655 |
|
---|
656 | //# sourceMappingURL=portal.js.map |
---|