-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
projectors.ts
170 lines (138 loc) · 5.34 KB
/
projectors.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
import { IContainer, IResolver, PLATFORM, Registration, Reporter, Tracer } from '@aurelia/kernel';
import {
CustomElementHost,
ICustomElement,
IDOM,
IElementProjector,
INodeSequence,
IProjectorLocator,
TemplateDefinition
} from '@aurelia/runtime';
const slice = Array.prototype.slice;
const defaultShadowOptions = {
mode: 'open' as 'open' | 'closed'
};
export class HTMLProjectorLocator implements IProjectorLocator<Node> {
public static register(container: IContainer): IResolver<IProjectorLocator> {
return Registration.singleton(IProjectorLocator, this).register(container);
}
public getElementProjector(dom: IDOM<Node>, $component: ICustomElement<Node>, host: CustomElementHost<HTMLElement>, def: TemplateDefinition): IElementProjector<Node> {
if (def.shadowOptions || def.hasSlots) {
if (def.containerless) {
throw Reporter.error(21);
}
return new ShadowDOMProjector(dom, $component, host, def);
}
if (def.containerless) {
return new ContainerlessProjector(dom, $component, host);
}
return new HostProjector($component, host);
}
}
const childObserverOptions = { childList: true };
/** @internal */
export class ShadowDOMProjector implements IElementProjector<Node> {
public host: CustomElementHost<Node>;
public shadowRoot: CustomElementHost<ShadowRoot>;
public dom: IDOM<Node>;
constructor(dom: IDOM<Node>, $customElement: ICustomElement<Node>, host: CustomElementHost<HTMLElement>, definition: TemplateDefinition) {
this.dom = dom;
this.host = host;
let shadowOptions: ShadowRootInit;
if (
definition.shadowOptions !== undefined &&
definition.shadowOptions !== null &&
typeof definition.shadowOptions === 'object' &&
'mode' in definition.shadowOptions
) {
shadowOptions = definition.shadowOptions as unknown as ShadowRootInit;
} else {
shadowOptions = defaultShadowOptions;
}
this.shadowRoot = host.attachShadow(shadowOptions);
this.host.$customElement = $customElement;
this.shadowRoot.$customElement = $customElement as ICustomElement<ShadowRoot>;
}
public get children(): ArrayLike<CustomElementHost<Node>> {
return this.shadowRoot.childNodes;
}
public subscribeToChildrenChange(callback: () => void): void {
// TODO: add a way to dispose/disconnect
this.dom.createNodeObserver(this.shadowRoot, callback, childObserverOptions);
}
public provideEncapsulationSource(): CustomElementHost<ShadowRoot> {
return this.shadowRoot;
}
public project(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('ShadowDOMProjector', 'project', slice.call(arguments)); }
nodes.appendTo(this.shadowRoot);
if (Tracer.enabled) { Tracer.leave(); }
}
public take(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('ShadowDOMProjector', 'take', slice.call(arguments)); }
nodes.remove();
if (Tracer.enabled) { Tracer.leave(); }
}
}
/** @internal */
export class ContainerlessProjector implements IElementProjector<Node> {
public host: CustomElementHost<Node>;
private readonly childNodes: ReadonlyArray<CustomElementHost<Node>>;
constructor(dom: IDOM<Node>, $customElement: ICustomElement<Node>, host: Node) {
if (host.childNodes.length) {
this.childNodes = PLATFORM.toArray(host.childNodes);
} else {
this.childNodes = PLATFORM.emptyArray;
}
this.host = dom.convertToRenderLocation(host) as CustomElementHost<Node>;
this.host.$customElement = $customElement;
}
public get children(): ArrayLike<CustomElementHost<Node>> {
return this.childNodes;
}
public subscribeToChildrenChange(callback: () => void): void {
// TODO: add a way to dispose/disconnect
const observer = new MutationObserver(callback);
observer.observe(this.host, childObserverOptions);
}
public provideEncapsulationSource(): Node {
return this.host.getRootNode();
}
public project(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('ContainerlessProjector', 'project', slice.call(arguments)); }
nodes.insertBefore(this.host);
if (Tracer.enabled) { Tracer.leave(); }
}
public take(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('ContainerlessProjector', 'take', slice.call(arguments)); }
nodes.remove();
if (Tracer.enabled) { Tracer.leave(); }
}
}
/** @internal */
export class HostProjector implements IElementProjector<Node> {
public host: CustomElementHost<Node>;
constructor($customElement: ICustomElement<Node>, host: CustomElementHost<Node>) {
this.host = host;
this.host.$customElement = $customElement;
}
public get children(): ArrayLike<CustomElementHost<Node>> {
return this.host.childNodes;
}
public subscribeToChildrenChange(callback: () => void): void {
// Do nothing since this scenario will never have children.
}
public provideEncapsulationSource(): Node {
return this.host.getRootNode();
}
public project(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('HostProjector', 'project', slice.call(arguments)); }
nodes.appendTo(this.host);
if (Tracer.enabled) { Tracer.leave(); }
}
public take(nodes: INodeSequence<Node>): void {
if (Tracer.enabled) { Tracer.enter('HostProjector', 'take', slice.call(arguments)); }
nodes.remove();
if (Tracer.enabled) { Tracer.leave(); }
}
}