-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (107 loc) · 2.93 KB
/
index.js
File metadata and controls
107 lines (107 loc) · 2.93 KB
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
/*! (c) Andrea Giammarchi */
function disconnected(poly) {'use strict';
var CONNECTED = 'connected';
var DISCONNECTED = 'dis' + CONNECTED;
var Event = poly.Event;
var WeakSet = poly.WeakSet;
var notObserving = true;
var observer = new WeakSet;
return function observe(node) {
if (notObserving) {
notObserving = !notObserving;
startObserving(node.ownerDocument);
}
observer.add(node);
return node;
};
function startObserving(document) {
var dispatched = null;
try {
(new MutationObserver(changes)).observe(
document,
{subtree: true, childList: true}
);
}
catch(o_O) {
var timer = 0;
var records = [];
var reschedule = function (record) {
records.push(record);
clearTimeout(timer);
timer = setTimeout(
function () {
changes(records.splice(timer = 0, records.length));
},
0
);
};
document.addEventListener(
'DOMNodeRemoved',
function (event) {
reschedule({addedNodes: [], removedNodes: [event.target]});
},
true
);
document.addEventListener(
'DOMNodeInserted',
function (event) {
reschedule({addedNodes: [event.target], removedNodes: []});
},
true
);
}
function changes(records) {
dispatched = new Tracker;
for (var
record,
length = records.length,
i = 0; i < length; i++
) {
record = records[i];
dispatchAll(record.removedNodes, DISCONNECTED, CONNECTED);
dispatchAll(record.addedNodes, CONNECTED, DISCONNECTED);
}
dispatched = null;
}
function dispatchAll(nodes, type, counter) {
for (var
node,
event = new Event(type),
length = nodes.length,
i = 0; i < length;
(node = nodes[i++]).nodeType === 1 &&
dispatchTarget(node, event, type, counter)
);
}
function dispatchTarget(node, event, type, counter) {
if (observer.has(node) && !dispatched[type].has(node)) {
dispatched[counter].delete(node);
dispatched[type].add(node);
node.dispatchEvent(event);
/*
// The event is not bubbling (perf reason: should it?),
// hence there's no way to know if
// stop/Immediate/Propagation() was called.
// Should DOM Level 0 work at all?
// I say it's a YAGNI case for the time being,
// and easy to implement in user-land.
if (!event.cancelBubble) {
var fn = node['on' + type];
if (fn)
fn.call(node, event);
}
*/
}
for (var
children = node.children,
length = children.length,
i = 0; i < length;
dispatchTarget(children[i++], event, type, counter)
);
}
function Tracker() {
this[CONNECTED] = new WeakSet;
this[DISCONNECTED] = new WeakSet;
}
}
}