-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathportal.ts
256 lines (209 loc) · 6.84 KB
/
portal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
TemplateRef,
ViewContainerRef,
ElementRef,
ComponentRef,
EmbeddedViewRef,
Injector,
ComponentFactoryResolver,
} from '@angular/core';
import {
throwNullPortalOutletError,
throwPortalAlreadyAttachedError,
throwNoPortalAttachedError,
throwNullPortalError,
throwPortalOutletAlreadyDisposedError,
throwUnknownPortalTypeError
} from './portal-errors';
/** Interface that can be used to generically type a class. */
export interface ComponentType<T> {
new (...args: any[]): T;
}
/**
* A `Portal` is something that you want to render somewhere else.
* It can be attach to / detached from a `PortalOutlet`.
*/
export abstract class Portal<T> {
private _attachedHost: PortalOutlet | null;
/** Attach this portal to a host. */
attach(host: PortalOutlet): T {
if (host == null) {
throwNullPortalOutletError();
}
if (host.hasAttached()) {
throwPortalAlreadyAttachedError();
}
this._attachedHost = host;
return <T> host.attach(this);
}
/** Detach this portal from its host */
detach(): void {
let host = this._attachedHost;
if (host == null) {
throwNoPortalAttachedError();
} else {
this._attachedHost = null;
host.detach();
}
}
/** Whether this portal is attached to a host. */
get isAttached(): boolean {
return this._attachedHost != null;
}
/**
* Sets the PortalOutlet reference without performing `attach()`. This is used directly by
* the PortalOutlet when it is performing an `attach()` or `detach()`.
*/
setAttachedHost(host: PortalOutlet | null) {
this._attachedHost = host;
}
}
/**
* A `ComponentPortal` is a portal that instantiates some Component upon attachment.
*/
export class ComponentPortal<T> extends Portal<ComponentRef<T>> {
/** The type of the component that will be instantiated for attachment. */
component: ComponentType<T>;
/**
* [Optional] Where the attached component should live in Angular's *logical* component tree.
* This is different from where the component *renders*, which is determined by the PortalOutlet.
* The origin is necessary when the host is outside of the Angular application context.
*/
viewContainerRef?: ViewContainerRef | null;
/** [Optional] Injector used for the instantiation of the component. */
injector?: Injector | null;
/**
* Alternate `ComponentFactoryResolver` to use when resolving the associated component.
* Defaults to using the resolver from the outlet that the portal is attached to.
*/
componentFactoryResolver?: ComponentFactoryResolver | null;
constructor(
component: ComponentType<T>,
viewContainerRef?: ViewContainerRef | null,
injector?: Injector | null,
componentFactoryResolver?: ComponentFactoryResolver | null) {
super();
this.component = component;
this.viewContainerRef = viewContainerRef;
this.injector = injector;
this.componentFactoryResolver = componentFactoryResolver;
}
}
/**
* A `TemplatePortal` is a portal that represents some embedded template (TemplateRef).
*/
export class TemplatePortal<C = any> extends Portal<C> {
/** The embedded template that will be used to instantiate an embedded View in the host. */
templateRef: TemplateRef<C>;
/** Reference to the ViewContainer into which the template will be stamped out. */
viewContainerRef: ViewContainerRef;
/** Contextual data to be passed in to the embedded view. */
context: C | undefined;
constructor(template: TemplateRef<C>, viewContainerRef: ViewContainerRef, context?: C) {
super();
this.templateRef = template;
this.viewContainerRef = viewContainerRef;
this.context = context;
}
get origin(): ElementRef {
return this.templateRef.elementRef;
}
/**
* Attach the the portal to the provided `PortalOutlet`.
* When a context is provided it will override the `context` property of the `TemplatePortal`
* instance.
*/
attach(host: PortalOutlet, context: C | undefined = this.context): C {
this.context = context;
return super.attach(host);
}
detach(): void {
this.context = undefined;
return super.detach();
}
}
/** A `PortalOutlet` is an space that can contain a single `Portal`. */
export interface PortalOutlet {
/** Attaches a portal to this outlet. */
attach(portal: Portal<any>): any;
/** Detaches the currently attached portal from this outlet. */
detach(): any;
/** Performs cleanup before the outlet is destroyed. */
dispose(): void;
/** Whether there is currently a portal attached to this outlet. */
hasAttached(): boolean;
}
/**
* Partial implementation of PortalOutlet that handles attaching
* ComponentPortal and TemplatePortal.
*/
export abstract class BasePortalOutlet implements PortalOutlet {
/** The portal currently attached to the host. */
protected _attachedPortal: Portal<any> | null;
/** A function that will permanently dispose this host. */
private _disposeFn: (() => void) | null;
/** Whether this host has already been permanently disposed. */
private _isDisposed: boolean = false;
/** Whether this host has an attached portal. */
hasAttached(): boolean {
return !!this._attachedPortal;
}
attach<T>(portal: ComponentPortal<T>): ComponentRef<T>;
attach<T>(portal: TemplatePortal<T>): EmbeddedViewRef<T>;
attach(portal: any): any;
/** Attaches a portal. */
attach(portal: Portal<any>): any {
if (!portal) {
throwNullPortalError();
}
if (this.hasAttached()) {
throwPortalAlreadyAttachedError();
}
if (this._isDisposed) {
throwPortalOutletAlreadyDisposedError();
}
if (portal instanceof ComponentPortal) {
this._attachedPortal = portal;
return this.attachComponentPortal(portal);
} else if (portal instanceof TemplatePortal) {
this._attachedPortal = portal;
return this.attachTemplatePortal(portal);
}
throwUnknownPortalTypeError();
}
abstract attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T>;
abstract attachTemplatePortal<C>(portal: TemplatePortal<C>): EmbeddedViewRef<C>;
/** Detaches a previously attached portal. */
detach(): void {
if (this._attachedPortal) {
this._attachedPortal.setAttachedHost(null);
this._attachedPortal = null;
}
this._invokeDisposeFn();
}
/** Permanently dispose of this portal host. */
dispose(): void {
if (this.hasAttached()) {
this.detach();
}
this._invokeDisposeFn();
this._isDisposed = true;
}
/** @docs-private */
setDisposeFn(fn: () => void) {
this._disposeFn = fn;
}
private _invokeDisposeFn() {
if (this._disposeFn) {
this._disposeFn();
this._disposeFn = null;
}
}
}