-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathflattened-nodes-observer.js
312 lines (298 loc) · 9.91 KB
/
flattened-nodes-observer.js
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import './boot.js';
import { calculateSplices } from './array-splice.js';
import { microTask } from './async.js';
/**
* Returns true if `node` is a slot element
* @param {!Node} node Node to test.
* @return {boolean} Returns true if the given `node` is a slot
* @private
*/
function isSlot(node) {
return (node.localName === 'slot');
}
/**
* Class that listens for changes (additions or removals) to
* "flattened nodes" on a given `node`. The list of flattened nodes consists
* of a node's children and, for any children that are `<slot>` elements,
* the expanded flattened list of `assignedNodes`.
* For example, if the observed node has children `<a></a><slot></slot><b></b>`
* and the `<slot>` has one `<div>` assigned to it, then the flattened
* nodes list is `<a></a><div></div><b></b>`. If the `<slot>` has other
* `<slot>` elements assigned to it, these are flattened as well.
*
* The provided `callback` is called whenever any change to this list
* of flattened nodes occurs, where an addition or removal of a node is
* considered a change. The `callback` is called with one argument, an object
* containing an array of any `addedNodes` and `removedNodes`.
*
* Note: the callback is called asynchronous to any changes
* at a microtask checkpoint. This is because observation is performed using
* `MutationObserver` and the `<slot>` element's `slotchange` event which
* are asynchronous.
*
* An example:
* ```js
* class TestSelfObserve extends PolymerElement {
* static get is() { return 'test-self-observe';}
* connectedCallback() {
* super.connectedCallback();
* this._observer = new FlattenedNodesObserver(this, (info) => {
* this.info = info;
* });
* }
* disconnectedCallback() {
* super.disconnectedCallback();
* this._observer.disconnect();
* }
* }
* customElements.define(TestSelfObserve.is, TestSelfObserve);
* ```
*
* @summary Class that listens for changes (additions or removals) to
* "flattened nodes" on a given `node`.
*/
export let FlattenedNodesObserver = class {
/**
* Returns the list of flattened nodes for the given `node`.
* This list consists of a node's children and, for any children
* that are `<slot>` elements, the expanded flattened list of `assignedNodes`.
* For example, if the observed node has children `<a></a><slot></slot><b></b>`
* and the `<slot>` has one `<div>` assigned to it, then the flattened
* nodes list is `<a></a><div></div><b></b>`. If the `<slot>` has other
* `<slot>` elements assigned to it, these are flattened as well.
*
* @param {!HTMLElement|!HTMLSlotElement} node The node for which to
* return the list of flattened nodes.
* @return {!Array<!Node>} The list of flattened nodes for the given `node`.
* @nocollapse See https://github.com/google/closure-compiler/issues/2763
*/
// eslint-disable-next-line
static getFlattenedNodes(node, wrapper = (n) => n) {
const wrapped = wrapper(node);
if (isSlot(node)) {
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
return wrapped.assignedNodes({flatten: true});
} else {
return Array.from(wrapped.childNodes).map((node) => {
if (isSlot(node)) {
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
return wrapper(node).assignedNodes({flatten: true});
} else {
return [node];
}
}).reduce((a, b) => a.concat(b), []);
}
}
/**
* @param {!HTMLElement} target Node on which to listen for changes.
* @param {?function(this: Element, { target: !HTMLElement, addedNodes: !Array<!Element>, removedNodes: !Array<!Element> }):void} callback Function called when there are additions
* or removals from the target's list of flattened nodes.
*/
// eslint-disable-next-line
constructor(target, callback, wrapper = n=> n) {
/**
* @type {MutationObserver}
* @private
*/
this._wrap = wrapper;
this._shadyChildrenObserver = null;
/**
* @type {MutationObserver}
* @private
*/
this._nativeChildrenObserver = null;
this._connected = false;
/**
* @type {!HTMLElement}
* @private
*/
this._target = target;
this.callback = callback;
this._effectiveNodes = [];
this._observer = null;
this._scheduled = false;
/**
* @type {function()}
* @private
*/
this._boundSchedule = () => {
this._schedule();
};
this.connect();
this._schedule();
}
/**
* Activates an observer. This method is automatically called when
* a `FlattenedNodesObserver` is created. It should only be called to
* re-activate an observer that has been deactivated via the `disconnect` method.
*
* @return {void}
*/
connect() {
if (isSlot(this._target)) {
this._listenSlots([this._target]);
} else if (this._wrap(this._target).children) {
this._listenSlots(
/** @type {!NodeList<!Node>} */ (this._wrap(this._target).children));
if (window.ShadyDOM) {
this._shadyChildrenObserver =
ShadyDOM.observeChildren(this._target, (mutations) => {
this._processMutations(mutations);
});
} else {
this._nativeChildrenObserver =
new MutationObserver((mutations) => {
this._processMutations(mutations);
});
this._nativeChildrenObserver.observe(this._target, {childList: true});
}
}
this._connected = true;
}
/**
* Deactivates the flattened nodes observer. After calling this method
* the observer callback will not be called when changes to flattened nodes
* occur. The `connect` method may be subsequently called to reactivate
* the observer.
*
* @return {void}
*/
disconnect() {
if (isSlot(this._target)) {
this._unlistenSlots([this._target]);
} else if (this._wrap(this._target).children) {
this._unlistenSlots(
/** @type {!NodeList<!Node>} */ (this._wrap(this._target).children));
if (window.ShadyDOM && this._shadyChildrenObserver) {
ShadyDOM.unobserveChildren(this._shadyChildrenObserver);
this._shadyChildrenObserver = null;
} else if (this._nativeChildrenObserver) {
this._nativeChildrenObserver.disconnect();
this._nativeChildrenObserver = null;
}
}
this._connected = false;
}
/**
* @return {void}
* @private
*/
_schedule() {
if (!this._scheduled) {
this._scheduled = true;
microTask.run(() => this.flush());
}
}
/**
* @param {Array<MutationRecord>} mutations Mutations signaled by the mutation observer
* @return {void}
* @private
*/
_processMutations(mutations) {
this._processSlotMutations(mutations);
this.flush();
}
/**
* @param {Array<MutationRecord>} mutations Mutations signaled by the mutation observer
* @return {void}
* @private
*/
_processSlotMutations(mutations) {
if (mutations) {
for (let i=0; i < mutations.length; i++) {
let mutation = mutations[i];
if (mutation.addedNodes) {
this._listenSlots(mutation.addedNodes);
}
if (mutation.removedNodes) {
this._unlistenSlots(mutation.removedNodes);
}
}
}
}
/**
* Flushes the observer causing any pending changes to be immediately
* delivered the observer callback. By default these changes are delivered
* asynchronously at the next microtask checkpoint.
*
* @return {boolean} Returns true if any pending changes caused the observer
* callback to run.
*/
flush() {
if (!this._connected) {
return false;
}
if (window.ShadyDOM) {
ShadyDOM.flush();
}
if (this._nativeChildrenObserver) {
this._processSlotMutations(this._nativeChildrenObserver.takeRecords());
} else if (this._shadyChildrenObserver) {
this._processSlotMutations(this._shadyChildrenObserver.takeRecords());
}
this._scheduled = false;
let info = {
target: this._target,
addedNodes: [],
removedNodes: []
};
let newNodes = this.constructor.getFlattenedNodes(this._target);
let splices = calculateSplices(newNodes,
this._effectiveNodes);
// process removals
for (let i=0, s; (i<splices.length) && (s=splices[i]); i++) {
for (let j=0, n; (j < s.removed.length) && (n=s.removed[j]); j++) {
info.removedNodes.push(n);
}
}
// process adds
for (let i=0, s; (i<splices.length) && (s=splices[i]); i++) {
for (let j=s.index; j < s.index + s.addedCount; j++) {
info.addedNodes.push(newNodes[j]);
}
}
// update cache
this._effectiveNodes = newNodes;
let didFlush = false;
if (info.addedNodes.length || info.removedNodes.length) {
didFlush = true;
this.callback.call(this._target, info);
}
return didFlush;
}
/**
* @param {!Array<!Node>|!NodeList<!Node>} nodeList Nodes that could change
* @return {void}
* @private
*/
_listenSlots(nodeList) {
for (let i=0; i < nodeList.length; i++) {
let n = nodeList[i];
if (isSlot(n)) {
n.addEventListener('slotchange', this._boundSchedule);
}
}
}
/**
* @param {!Array<!Node>|!NodeList<!Node>} nodeList Nodes that could change
* @return {void}
* @private
*/
_unlistenSlots(nodeList) {
for (let i=0; i < nodeList.length; i++) {
let n = nodeList[i];
if (isSlot(n)) {
n.removeEventListener('slotchange', this._boundSchedule);
}
}
}
};