-
Notifications
You must be signed in to change notification settings - Fork 11
/
html-element.js
372 lines (327 loc) · 9.01 KB
/
html-element.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
var applyProperties = require('./lib/apply-properties')
var isObservable = require('./is-observable')
var parseTag = require('./lib/parse-tag')
var walk = require('./lib/walk')
var watch = require('./watch')
var caches = new global.WeakMap()
var bindQueue = []
var currentlyBinding = false
var watcher = null
var releaseNextTick = require('./lib/release-next-tick')
module.exports = function (tag, attributes, children) {
return Element(global.document, null, tag, attributes, children)
}
module.exports.forDocument = function (document, namespace) {
return Element.bind(this, document, namespace)
}
function Element (document, namespace, tagName, properties, children) {
if (!children && (Array.isArray(properties) || isText(properties) || isNode(properties) || isObservable(properties))) {
children = properties
properties = null
}
checkWatcher(document)
properties = properties || {}
var tag = parseTag(tagName, properties, namespace)
var node = namespace
? document.createElementNS(namespace, tag.tagName)
: document.createElement(tag.tagName)
if (tag.id) {
node.id = tag.id
}
if (tag.classes && tag.classes.length) {
node.className = tag.classes.join(' ')
}
var data = {
targets: new Map(),
observing: false,
bindings: [],
hookBindings: []
}
if ('intersectionBindingViewport' in properties) {
var options = properties.intersectionBindingViewport
delete properties.intersectionBindingViewport
if (options && global.IntersectionObserver) {
node.__mutantIntersectionBindingViewport = true
data.intersectionObserver = new global.IntersectionObserver(onIntersection, {
root: node,
rootMargin: options.rootMargin || '0px',
threshold: [0, 0.1]
})
}
}
caches.set(node, data)
applyProperties(node, properties, data)
if (children != null) {
appendChild(document, node, data, children)
}
maybeBind(document, node)
return node
}
function appendChild (document, target, data, node) {
if (Array.isArray(node)) {
node.forEach(function (child) {
appendChild(document, target, data, child)
})
} else if (isObservable(node)) {
var nodes = getNodes(document, resolve(node))
nodes.forEach(append, { target: target, document: document })
data.targets.set(node, nodes)
data.bindings.push(new Binding(document, node, data))
} else {
node = getNode(document, node)
target.appendChild(node)
if (getRootNode(node) === document) {
walk(node, rebind)
}
}
}
function append (child) {
this.target.appendChild(child)
if (getRootNode(child) === this.document) {
walk(child, rebind)
}
}
function maybeBind (document, node) {
bindQueue.push([document, node])
if (!currentlyBinding) {
currentlyBinding = true
setImmediate(flushBindQueue)
}
}
function flushBindQueue () {
currentlyBinding = false
while (bindQueue.length) {
var item = bindQueue.shift()
var document = item[0]
var node = item[1]
if (getRootNode(node) === document) {
walk(node, rebind)
}
}
}
function checkWatcher (document) {
if (!watcher && global.MutationObserver) {
watcher = new global.MutationObserver(onMutate)
watcher.observe(document, {subtree: true, childList: true})
}
}
function onMutate (changes) {
changes.forEach(handleChange)
}
function onIntersection (changes) {
changes.forEach(handleIntersect)
}
function handleIntersect (change) {
if (change.isIntersecting) {
if (change.intersectionRatio >= 0.1) {
enterViewport(change.target)
}
} else {
exitViewport(change.target)
}
}
function getRootNode (el) {
var element = el
while (element.parentNode) {
element = element.parentNode
}
return element
}
function getIntersectionObserver (el) {
var element = el
while (element.parentNode) {
element = element.parentNode
if (element.__mutantIntersectionBindingViewport) {
var data = caches.get(element)
if (data) {
return data.intersectionObserver
}
}
}
}
function enterViewport (node) {
if (node.nodeType === 1) {
var data = caches.get(node)
if (data) {
if (data.observing) {
data.bindings.forEach(invokeBind)
}
}
}
}
function exitViewport (node) {
if (node.nodeType === 1) {
var data = caches.get(node)
if (data) {
if (data.observing) {
data.bindings.forEach(invokeUnbind)
}
}
}
}
function handleChange (change) {
for (var i = 0; i < change.addedNodes.length; i++) {
// if parent is a mutant element, then safe to assume it has already been bound
var node = change.addedNodes[i]
if (!caches.has(node.parentNode) || !isBound(node)) {
walk(node, rebind)
}
}
for (var i = 0; i < change.removedNodes.length; i++) {
var node = change.removedNodes[i]
walk(node, unbind)
}
}
function indexOf (target, item) {
return Array.prototype.indexOf.call(target, item)
}
function replace (oldNodes, newNodes) {
var parent = oldNodes[oldNodes.length - 1].parentNode
var nodes = parent.childNodes
var startIndex = indexOf(nodes, oldNodes[0])
// avoid reinserting nodes that are already in correct position!
for (var i = 0; i < newNodes.length; i++) {
if (nodes[i + startIndex] === newNodes[i]) {
continue
} else if (nodes[i + startIndex + 1] === newNodes[i]) {
parent.removeChild(nodes[i + startIndex])
continue
} else if (nodes[i + startIndex] === newNodes[i + 1] && newNodes[i + 1]) {
parent.insertBefore(newNodes[i], nodes[i + startIndex])
} else if (nodes[i + startIndex]) {
parent.insertBefore(newNodes[i], nodes[i + startIndex])
} else {
parent.appendChild(newNodes[i])
}
walk(newNodes[i], rebind)
}
oldNodes.filter(function (node) {
return !~newNodes.indexOf(node)
}).forEach(function (node) {
if (node.parentNode) {
parent.removeChild(node)
}
walk(node, unbind)
})
}
function isText (value) {
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'
}
function isNode (value) {
// for some reason, img elements are not instances of Node
return value instanceof global.Node || (global.HTMLElement && value instanceof global.HTMLElement)
}
function getNode (document, nodeOrText) {
if (nodeOrText == null) {
return document.createTextNode('')
} else if (isText(nodeOrText)) {
return document.createTextNode(nodeOrText.toString())
} else {
return nodeOrText
}
}
function getNodes (document, nodeOrNodes) {
if (Array.isArray(nodeOrNodes)) {
if (nodeOrNodes.length) {
var result = []
for (var i = 0; i < nodeOrNodes.length; i++) {
var item = nodeOrNodes[i]
if (Array.isArray(item)) {
getNodes(document, item).forEach(push, result)
} else {
result.push(getNode(document, item))
}
}
return result.map(getNode.bind(this, document))
} else {
return [getNode(document, null)]
}
} else {
return [getNode(document, nodeOrNodes)]
}
}
function rebind (node) {
if (node.nodeType === 1) {
var data = caches.get(node)
if (data) {
var intersectionObserver = getIntersectionObserver(node)
if (!data.observing && intersectionObserver) {
data.observing = true
intersectionObserver.observe(node)
} else {
data.bindings.forEach(invokeBind)
}
data.hookBindings.forEach(invokeBind)
}
}
}
function unbind (node) {
if (node.nodeType === 1) {
var data = caches.get(node)
if (data) {
var intersectionObserver = getIntersectionObserver(node)
if (intersectionObserver && data.observing) {
data.observing = false
intersectionObserver.unobserve(node)
}
data.bindings.forEach(invokeUnbind)
data.hookBindings.forEach(invokeUnbind)
}
}
}
function isBound (node) {
if (node.nodeType === 1) {
var data = caches.get(node)
if (data) {
return data.observing || data.bindings.some(getBound) || data.hookBindings.some(getBound)
}
}
}
function getBound (binding) {
return binding.bound
}
function invokeBind (binding) {
binding.bind()
}
function invokeUnbind (binding) {
binding.unbind()
}
function push (item) {
this.push(item)
}
function resolve (source) {
return typeof source === 'function' ? source() : source
}
function Binding (document, obs, data) {
this.document = document
this.obs = obs
this.data = data
this.bound = false
this.update = function (value) {
var oldNodes = data.targets.get(obs)
var newNodes = getNodes(document, value)
if (oldNodes) {
replace(oldNodes, newNodes)
data.targets.set(obs, newNodes)
}
}
// listen immediately, but if not bound before next tick, release
this.release = obs(this.update)
releaseNextTick(this)
}
Binding.prototype = {
bind: function () {
if (!this.bound) {
if (!this.release) {
this.release = watch(this.obs, this.update)
}
this.bound = true
}
},
unbind: function () {
if (this.bound) {
this.bound = false
releaseNextTick(this)
}
}
}