-
Notifications
You must be signed in to change notification settings - Fork 15
/
jquery.entwine.domevents.addrem.js
138 lines (110 loc) · 3.95 KB
/
jquery.entwine.domevents.addrem.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
(function($){
// Gets all the child elements of a particular elements, stores it in an array
function getElements(store, original) {
var node, i = store.length, next = original.firstChild;
while ((node = next)) {
if (node.nodeType === 1) store[i++] = node;
next = node.firstChild || node.nextSibling;
while (!next && (node = node.parentNode) && node !== original) next = node.nextSibling;
}
}
// This might be faster? Or slower? @todo: benchmark.
function getElementsAlt(store, node) {
if (node.getElementsByTagName) {
var els = node.getElementsByTagName('*'), len = els.length, i = 0, j = store.length;
for(; i < len; i++, j++) {
store[j] = els[i];
}
}
else if (node.childNodes) {
var els = node.childNodes, len = els.length, i = 0;
for(; i < len; i++) {
getElements(store, els[i]);
}
}
}
var dontTrigger = false;
var patchDomManipCallback = function(original) {
var patched = function(elem){
var added = [];
if (!dontTrigger) {
if (elem.nodeType == 1) added[added.length] = elem;
getElements(added, elem);
}
var rv = original.apply(this, arguments);
if (!dontTrigger && added.length) {
var event = $.Event('EntwineElementsAdded');
event.targets = added;
$(document).triggerHandler(event);
}
return rv;
}
patched.patched = true;
return patched;
}
var version = $.prototype.jquery.split('.');
var callbackIdx = (version[0] > 1 || version[1] >= 10 ? 1 : 2);
// Monkey patch $.fn.domManip to catch all regular jQuery add element calls
var _domManip = $.prototype.domManip;
$.prototype.domManip = function() {
if (!arguments[callbackIdx].patched) arguments[callbackIdx] = patchDomManipCallback(arguments[callbackIdx]);
return _domManip.apply(this, arguments);
}
// Monkey patch $.fn.html to catch when jQuery sets innerHTML directly
var _html = $.prototype.html;
$.prototype.html = function(value) {
if (value === undefined) return _html.apply(this, arguments);
dontTrigger = true;
var res = _html.apply(this, arguments);
dontTrigger = false;
var added = [];
var i = 0, length = this.length;
for (; i < length; i++ ) getElements(added, this[i]);
var event = $.Event('EntwineElementsAdded');
event.targets = added;
$(document).triggerHandler(event);
return res;
}
// If this is true, we've changed something to call cleanData so that we can catch the elements, but we don't
// want to call the underlying original $.cleanData
var supressActualClean = false;
// Monkey patch $.cleanData to catch element removal
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
// By default we can assume all elements passed are legitimately being removeed
var removed = elems;
// Except if we're supressing actual clean - we might be being called by jQuery "being careful" about detaching nodes
// before attaching them. So we need to check to make sure these nodes currently are in a document
if (supressActualClean) {
var i = 0, len = elems.length, removed = [], ri = 0;
for(; i < len; i++) {
var node = elems[i], current = node;
while (current = current.parentNode) {
if (current.nodeType == 9) { removed[ri++] = node; break; }
}
}
}
if (removed.length) {
var event = $.Event('EntwineElementsRemoved');
event.targets = removed;
$(document).triggerHandler(event);
}
if (!supressActualClean) _cleanData.apply(this, arguments);
}
// Monkey patch $.fn.remove to catch when we're just detaching (keepdata == 1) -
// this doesn't call cleanData but still needs to trigger event
var _remove = $.prototype.remove;
$.prototype.remove = function(selector, keepdata) {
supressActualClean = keepdata;
var rv = _remove.call(this, selector);
supressActualClean = false;
return rv;
}
// And on DOM ready, trigger adding once
$(function(){
var added = []; getElements(added, document);
var event = $.Event('EntwineElementsAdded');
event.targets = added;
$(document).triggerHandler(event);
});
})(jQuery);