jquery.entwine.ctors.js
7.76 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
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
(function($) {
/* Add the methods to handle constructor & destructor binding to the Namespace class */
$.entwine.Namespace.addMethods({
bind_condesc: function(selector, name, func) {
var ctors = this.store.ctors || (this.store.ctors = $.entwine.RuleList()) ;
var rule;
for (var i = 0 ; i < ctors.length; i++) {
if (ctors[i].selector.selector == selector.selector) {
rule = ctors[i]; break;
}
}
if (!rule) {
rule = ctors.addRule(selector, 'ctors');
}
rule[name] = func;
if (!ctors[name+'proxy']) {
var one = this.one('ctors', name);
var namespace = this;
var proxy = function(els, i, func) {
var j = els.length;
while (j--) {
var el = els[j];
var tmp_i = el.i, tmp_f = el.f;
el.i = i; el.f = one;
try { func.call(namespace.$(el)); }
catch(e) { $.entwine.warn_exception(name, el, e); }
finally { el.i = tmp_i; el.f = tmp_f; }
}
};
ctors[name+'proxy'] = proxy;
}
}
});
$.entwine.Namespace.addHandler({
order: 30,
bind: function(selector, k, v) {
if ($.isFunction(v) && (k == 'onmatch' || k == 'onunmatch')) {
// When we add new matchers we need to trigger a full global recalc once, regardless of the DOM changes that triggered the event
this.matchersDirty = true;
this.bind_condesc(selector, k, v);
return true;
}
}
});
/**
* Finds all the elements that now match a different rule (or have been removed) and call onmatch on onunmatch as appropriate
*
* Because this has to scan the DOM, and is therefore fairly slow, this is normally triggered off a short timeout, so that
* a series of DOM manipulations will only trigger this once.
*
* The downside of this is that things like:
* $('#foo').addClass('tabs'); $('#foo').tabFunctionBar();
* won't work.
*/
$(document).bind('EntwineSubtreeMaybeChanged', function(e, changes){
// var start = (new Date).getTime();
// For every namespace
for (var k in $.entwine.namespaces) {
var namespace = $.entwine.namespaces[k];
// That has constructors or destructors
var ctors = namespace.store.ctors;
if (ctors) {
// Keep a record of elements that have matched some previous more specific rule.
// Not that we _don't_ actually do that until this is needed. If matched is null, it's not been calculated yet.
// We also keep track of any elements that have newly been taken or released by a specific rule
var matched = null, taken = $([]), released = $([]);
// Updates matched to contain all the previously matched elements as if we'd been keeping track all along
var calcmatched = function(j){
if (matched !== null) return;
matched = $([]);
var cache, k = ctors.length;
while ((--k) > j) {
if (cache = ctors[k].cache) matched = matched.add(cache);
}
}
// Some declared variables used in the loop
var add, rem, res, rule, sel, ctor, dtor, full;
// Stepping through each selector from most to least specific
var j = ctors.length;
while (j--) {
// Build some quick-access variables
rule = ctors[j];
sel = rule.selector.selector;
ctor = rule.onmatch;
dtor = rule.onunmatch;
/*
Rule.cache might be stale or fresh. It'll be stale if
- some more specific selector now has some of rule.cache in it
- some change has happened that means new elements match this selector now
- some change has happened that means elements no longer match this selector
The first we can just compare rules.cache with matched, removing anything that's there already.
*/
// Reset the "elements that match this selector and no more specific selector with an onmatch rule" to null.
// Staying null means this selector is fresh.
res = null;
// If this gets changed to true, it's too hard to do a delta update, so do a full update
full = false;
if (namespace.matchersDirty || changes.global) {
// For now, just fall back to old version. We need to do something like changed.Subtree.find('*').andSelf().filter(sel), but that's _way_ slower on modern browsers than the below
full = true;
}
else {
// We don't deal with attributes yet, so any attribute change means we need to do a full recalc
for (var k in changes.attrs) { full = true; break; }
/*
If a class changes, but it isn't listed in our selector, we don't care - the change couldn't affect whether or not any element matches
If it is listed on our selector
- If it is on the direct match part, it could have added or removed the node it changed on
- If it is on the context part, it could have added or removed any node that were previously included or excluded because of a match or failure to match with the context required on that node
- NOTE: It might be on _both_
*/
var method = rule.selector.affectedBy(changes);
if (method.classes.context) {
full = true;
}
else {
for (var k in method.classes.direct) {
calcmatched(j);
var recheck = changes.classes[k].not(matched);
if (res === null) {
res = rule.cache ? rule.cache.not(taken).add(released.filter(sel)) : $([]);
}
res = res.not(recheck).add(recheck.filter(sel));
}
}
}
if (full) {
calcmatched(j);
res = $(sel).not(matched);
}
else {
if (!res) {
// We weren't stale because of any changes to the DOM that affected this selector, but more specific
// onmatches might have caused stale-ness
// Do any of the previous released elements match this selector?
add = released.length && released.filter(sel);
if (add && add.length) {
// Yes, so we're stale as we need to include them. Filter for any possible taken value at the same time
res = rule.cache ? rule.cache.not(taken).add(add) : add;
}
else {
// Do we think we own any of the elements now taken by more specific rules?
rem = taken.length && rule.cache && rule.cache.filter(taken);
if (rem && rem.length) {
// Yes, so we're stale as we need to exclude them.
res = rule.cache.not(rem);
}
}
}
}
// Res will be null if we know we are fresh (no full needed, selector not affectedBy changes)
if (res === null) {
// If we are tracking matched, add ourselves
if (matched && rule.cache) matched = matched.add(rule.cache);
}
else {
// If this selector has a list of elements it matched against last time
if (rule.cache) {
// Find the ones that are extra this time
add = res.not(rule.cache);
rem = rule.cache.not(res);
}
else {
add = res; rem = null;
}
if ((add && add.length) || (rem && rem.length)) {
if (rem && rem.length) {
released = released.add(rem);
if (dtor && !rule.onunmatchRunning) {
rule.onunmatchRunning = true;
ctors.onunmatchproxy(rem, j, dtor);
rule.onunmatchRunning = false;
}
}
// Call the constructor on the newly matched ones
if (add && add.length) {
taken = taken.add(add);
released = released.not(add);
if (ctor && !rule.onmatchRunning) {
rule.onmatchRunning = true;
ctors.onmatchproxy(add, j, ctor);
rule.onmatchRunning = false;
}
}
}
// If we are tracking matched, add ourselves
if (matched) matched = matched.add(res);
// And remember this list of matching elements again this selector, so next matching we can find the unmatched ones
rule.cache = res;
}
}
namespace.matchersDirty = false;
}
}
// console.log((new Date).getTime() - start);
});
})(jQuery);