/
portal.ts
263 lines (235 loc) · 7.31 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
257
258
259
260
261
262
263
import { OverrideContext } from 'aurelia-binding';
import {
customAttribute,
templateController,
ViewSlot,
BoundViewFactory,
bindable,
View
} from 'aurelia-templating';
import { PLATFORM } from 'aurelia-pal';
const document: Document = PLATFORM.global.document;
type PortalTarget = string | Element | null | undefined
/**
* Indicates where to insert a portalled view. Aligns with webapi for insertion
*/
type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
export type PortalLifecycleCallback = (target: Element, view: View) => Promise<any> | any;
const validPositions = {
beforebegin: 1,
afterbegin: 1,
beforeend: 1,
afterend: 1
};
@templateController()
@customAttribute('portal')
export class Portal {
/**@internal */
private static getTarget(target: PortalTarget, context?: PortalTarget): Element | null {
if (target) {
if (typeof target === 'string') {
let queryContext: Element | Document = document;
if (context) {
if (typeof context === 'string') {
context = document.querySelector(context);
}
if (context instanceof Element) {
queryContext = context;
}
}
target = queryContext.querySelector(target);
}
if (target instanceof Element) {
return target;
}
}
return null;
}
/**@internal */
private static createViewSlot(position: InsertPosition, target: Element): ViewSlot {
if (typeof position !== 'string' || validPositions[position.toLowerCase()] !== 1) {
throw new Error('Invalid position for portalling. Expected one of "beforebegin", "afterbegin", "beforeend" or "afterend".');
}
const anchorCommentHolder = document.createElement('portal-placeholder');
const normalizedPosition = position.toLowerCase() as InsertPosition;
target.insertAdjacentElement(normalizedPosition, anchorCommentHolder);
const anchorComment = document.createComment('portal');
// If the position is beforeBegin or aftrEnd,
// then anchorCommentHolder wont be a child of target
// In all situations, it's always correct to use anchorCommentHolder rather than target
anchorCommentHolder.parentNode!.replaceChild(anchorComment, anchorCommentHolder);
return new ViewSlot(anchorComment, false);
}
/**
* Only needs the BoundViewFactory as a custom viewslot will be used
* @internal
*/
public static inject = [BoundViewFactory, ViewSlot];
/**
* Target to render to, CSS string | Element
*/
@bindable({
primaryProperty: true,
defaultValue: null
})
public target: string | Element | null | undefined;
/**
* Insertion position. See https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
* for explanation of what the possible values mean.
*
* Possible values are (case insensitive): `beforeBegin` | `afterBegin` | `beforeEnd` | `afterEnd`
*
* Default value is `beforeEnd`
*/
@bindable({ changeHandler: 'targetChanged' })
public position: InsertPosition = 'beforeend';
@bindable({ changeHandler: 'targetChanged' })
public renderContext: string | Element | null | undefined;
@bindable()
public strict: boolean = false;
@bindable()
public initialRender: boolean = false;
/**
* Will be called when the attribute receive new target after the first render.
*/
@bindable() public deactivating: PortalLifecycleCallback;
/**
* Will be called after `portaled` element has been added to target
*/
@bindable() public activating: PortalLifecycleCallback;
/**
* Will be called after activating has been resolved
*/
@bindable() public activated: PortalLifecycleCallback;
/**
* Will be called after deactivating has been resolved.
*/
@bindable() public deactivated: PortalLifecycleCallback;
/**
* The object that will becontextwhen calling life cycle methods above
*/
@bindable() public callbackContext: any
/**@internal */
private currentTarget: typeof unset | Element | null = unset;
/**@internal */
private isAttached: boolean;
/**@internal */
private viewSlot: ViewSlot | null;
/**@internal */
private view: View;
/**@internal */
private removed: boolean;
constructor(
/**@internal */
private viewFactory: BoundViewFactory,
/**@internal */
private originalViewslot: ViewSlot
) {
}
/**@internal */
public bind(bindingContext: any, overrideContext: OverrideContext) {
if (!this.callbackContext) {
this.callbackContext = bindingContext;
}
let view = this.view;
if (!view) {
view = this.view = this.viewFactory.create();
}
const shouldInitRender = this.initialRender;
if (shouldInitRender) {
this.originalViewslot.add(view);
}
view.bind(bindingContext, overrideContext);
if (shouldInitRender) {
this.originalViewslot.remove(view);
}
}
/**@internal*/
public attached() {
this.isAttached = true;
return this.render();
}
/**@internal */
public detached() {
this.isAttached = false;
if (this.viewSlot) {
this.viewSlot.detached();
}
}
/**@internal */
public unbind() {
if (this.viewSlot) {
this.viewSlot.remove(this.view);
this.viewSlot = null;
}
this.view.unbind();
this.callbackContext = null;
}
/**@internal */
private getTarget(): Element | null {
let target = Portal.getTarget(this.target, this.renderContext);
if (target === null) {
if (this.strict) {
throw new Error('Render target not found.');
} else {
target = document.body;
}
}
return target;
}
/**@internal */
private render(): void | Promise<any> {
const oldTarget = this.currentTarget;
const view = this.view;
const target = this.currentTarget = this.getTarget();
const oldViewSlot = this.viewSlot;
if (oldTarget === target && oldViewSlot) {
return;
}
const addAction = () => {
if (this.isAttached) {
return Promise.resolve(
typeof this.activating === 'function'
? this.activating.call(this.callbackContext, target!, view)
: null
).then(() => {
if (target === this.currentTarget || oldTarget === unset) {
const viewSlot = this.viewSlot = Portal.createViewSlot(this.position, target!); // new ViewSlot(target!, true);
viewSlot.attached();
viewSlot.add(view);
this.removed = false;
}
return Promise.resolve().then(() => {
typeof this.activated === 'function'
? this.activated.call(this.callbackContext, target!, view)
: null
});
});
}
return Promise.resolve(null);
}
if (oldViewSlot) {
return Promise.resolve(
typeof this.deactivating === 'function'
? this.deactivating.call(this.callbackContext, oldTarget as Element, view)
: null
).then(() => {
if (typeof this.deactivated === 'function') {
this.deactivated.call(this.callbackContext, oldTarget as Element, view);
}
this.viewSlot = null;
if (!this.removed) {
oldViewSlot.remove(view);
this.removed = true;
}
return addAction();
});
}
return Promise.resolve(addAction());
}
/**@internal */
public targetChanged() {
return this.render();
}
}
const unset = {};