/
Dom.js
372 lines (332 loc) · 9.76 KB
/
Dom.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 racer = require('racer')
, domShim = require('dom-shim')
, lookup = require('racer/lib/path').lookup
, EventDispatcher = require('./EventDispatcher')
, escapeHtml = require('html-util').escapeHtml
, merge = racer.util.merge
, win = window
, doc = document
, markers = {}
, elements = {
$_win: win
, $_doc: doc
}
, addListener, removeListener;
module.exports = Dom;
function Dom(model, appExports) {
var dom = this
, fns = this.fns
// Map dom event name -> true
, listenerAdded = {}
, captureListenerAdded = {};
// DOM listener capturing allows blur and focus to be delegated
// http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
var events = this._events = new EventDispatcher({
onTrigger: onTrigger
, onBind: function(name, listener, eventName) {
if (!listenerAdded[eventName]) {
addListener(doc, eventName, trigger, true);
listenerAdded[eventName] = true;
}
}
});
var captureEvents = this._captureEvents = new EventDispatcher({
onTrigger: function(name, listener, e) {
var el = doc.getElementById(id)
, id = listener.id;
if (el.tagName === 'HTML' || el.contains(e.target)) {
onTrigger(name, listener, id, e, el);
}
}
, onBind: function(name, listener) {
if (!captureListenerAdded[name]) {
addListener(doc, name, captureTrigger, true);
captureListenerAdded[name] = true;
}
}
});
function onTrigger(name, listener, id, e, el, next) {
var fn = listener.fn
, delay = listener.delay
, finish;
if (fn != null) {
finish = fns[fn] || appExports[fn] || lookup(fn, appExports);
if (!finish) return;
} else {
// Update the model when the element's value changes
finish = function() {
var value = dom.getMethods[listener.method](el, listener.property)
, setValue = listener.setValue;
// Allow the listener to override the setting function
if (setValue) {
setValue(model, value);
return;
}
// Remove this listener if its path id is no longer registered
var path = model.__pathMap.paths[listener.pathId];
if (!path) return false;
// Set the value if changed
if (model.get(path) === value) return;
model.pass(e).set(path, value);
}
}
if (delay != null) {
setTimeout(finish, delay, e, el, next, dom);
} else {
finish(e, el, next, dom);
}
}
function trigger(e, el, noBubble, continued) {
if (!el) el = e.target;
var prefix = e.type + ':'
, id;
// Next can be called from a listener to continue bubbling
function next() {
trigger(e, el.parentNode, false, true);
}
next.firstTrigger = !continued;
if (noBubble && (id = el.id)) {
return events.trigger(prefix + id, id, e, el, next);
}
while (true) {
while (!(id = el.id)) {
if (!(el = el.parentNode)) return;
}
// Stop bubbling once the event is handled
if (events.trigger(prefix + id, id, e, el, next)) return;
if (!(el = el.parentNode)) return;
}
}
function captureTrigger(e) {
captureEvents.trigger(e.type, e);
}
this.trigger = trigger;
this.captureTrigger = captureTrigger;
this.addListener = addListener;
this.removeListener = removeListener;
}
Dom.prototype = {
clear: function() {
this._events.clear();
this._captureEvents.clear();
markers = {};
}
, bind: function(eventName, id, listener) {
if (listener.capture) {
listener.id = id;
this._captureEvents.bind(eventName, listener);
} else {
this._events.bind("" + eventName + ":" + id, listener, eventName);
}
}
, update: function(el, method, ignore, value, property, index) {
// Don't do anything if the element is already up to date
if (value === this.getMethods[method](el, property)) return;
this.setMethods[method](el, ignore, value, property, index);
}
, item: function(id) {
return doc.getElementById(id) || elements[id] || getRange(id);
}
, getMethods: {
attr: getAttr
, prop: getProp
, propPolite: getProp
, html: getHtml
// These methods return NaN, because it never equals anything else. Thus,
// when compared against the new value, the new value will always be set
, append: getNaN
, insert: getNaN
, remove: getNaN
, move: getNaN
}
, setMethods: {
attr: setAttr
, prop: setProp
, propPolite: setProp
, html: setHtml
, append: setAppend
, insert: setInsert
, remove: setRemove
, move: setMove
}
, fns: {
$forChildren: forChildren
, $forName: forName
}
}
function getAttr(el, attr) {
return el.getAttribute(attr);
}
function getProp(el, prop) {
return el[prop];
}
function getHtml(el) {
return el.innerHTML;
}
function getNaN() {
return NaN;
}
function setAttr(el, ignore, value, attr) {
if (ignore && el.id === ignore) return;
el.setAttribute(attr, value);
}
function setProp(el, ignore, value, prop) {
if (ignore && el.id === ignore) return;
el[prop] = value;
}
function propPolite(el, ignore, value, prop) {
if (ignore && el.id === ignore) return;
if (el !== doc.activeElement || !doc.hasFocus()) {
el[prop] = value;
}
}
function setHtml(obj, ignore, value, escape) {
if (escape) value = escapeHtml(value);
if (obj.nodeType) {
// Element
if (ignore && obj.id === ignore) return;
obj.innerHTML = value;
} else {
// Range
obj.deleteContents();
obj.insertNode(obj.createContextualFragment(value));
}
}
function setAppend(obj, ignore, value, escape) {
if (escape) value = escapeHtml(value);
if (obj.nodeType) {
// Element
obj.insertAdjacentHTML('beforeend', value);
} else {
// Range
var el = obj.endContainer
, ref = el.childNodes[obj.endOffset];
el.insertBefore(obj.createContextualFragment(value), ref);
}
}
function setInsert(obj, ignore, value, escape, index) {
if (escape) value = escapeHtml(value);
if (obj.nodeType) {
// Element
if (ref = obj.childNodes[index]) {
ref.insertAdjacentHTML('beforebegin', value);
} else {
obj.insertAdjacentHTML('beforeend', value);
}
} else {
// Range
var el = obj.startContainer
, ref = el.childNodes[obj.startOffset + index];
el.insertBefore(obj.createContextualFragment(value), ref);
}
}
function setRemove(el, ignore, index) {
if (!el.nodeType) {
// Range
index += el.startOffset;
el = el.startContainer;
}
var child = el.childNodes[index];
if (child) el.removeChild(child);
}
function setMove(el, ignore, from, to, howMany) {
var child, fragment, nextChild, offset, ref, toEl;
if (!el.nodeType) {
offset = el.startOffset;
from += offset;
to += offset;
el = el.startContainer;
}
child = el.childNodes[from];
// Don't move if the item at the destination is passed as the ignore
// option, since this indicates the intended item was already moved
// Also don't move if the child to move matches the ignore option
if (!child || ignore && (toEl = el.childNodes[to]) &&
toEl.id === ignore || child.id === ignore) return;
ref = el.childNodes[to > from ? to + howMany : to];
if (howMany > 1) {
fragment = document.createDocumentFragment();
while (howMany--) {
nextChild = child.nextSibling;
fragment.appendChild(child);
if (!(child = nextChild)) break;
}
el.insertBefore(fragment, ref);
return;
}
el.insertBefore(child, ref);
}
function forChildren(e, el, next, dom) {
// Prevent infinte emission
if (!next.firstTrigger) return;
// Re-trigger the event on all child elements
var children = el.childNodes;
for (var i = 0, len = children.length, child; i < len; i++) {
child = children[i];
if (child.nodeType !== 1) continue; // Node.ELEMENT_NODE
dom.trigger(e, child, true, true);
forChildren(e, child, next, dom);
}
}
function forName(e, el, next, dom) {
// Prevent infinte emission
if (!next.firstTrigger) return;
var name = el.getAttribute('name');
if (!name) return;
// Re-trigger the event on all other elements with
// the same 'name' attribute
var elements = doc.getElementsByName(name)
, len = elements.length;
if (!(len > 1)) return;
for (var i = 0, element; i < len; i++) {
element = elements[i];
if (element === el) continue;
dom.trigger(e, element, false, true);
}
}
function getRange(name) {
var start = markers[name]
, end = markers['$' + name]
, comment, commentIterator, range;
if (!(start && end)) {
// NodeFilter.SHOW_COMMENT == 128
commentIterator = doc.createTreeWalker(doc.body, 128, null, false);
while (comment = commentIterator.nextNode()) {
markers[comment.data] = comment;
}
start = markers[name];
end = markers['$' + name];
if (!(start && end)) return;
}
// Comment nodes may continue to exist even if they have been removed from
// the page. Thus, make sure they are still somewhere in the page body
if (!doc.body.contains(start)) {
delete markers[name];
delete markers['$' + name];
return;
}
range = doc.createRange();
range.setStartAfter(start);
range.setEndBefore(end);
return range;
}
if (doc.addEventListener) {
addListener = function(el, name, callback, captures) {
el.addEventListener(name, callback, captures || false);
};
removeListener = function(el, name, callback, captures) {
el.removeEventListener(name, callback, captures || false);
};
} else if (doc.attachEvent) {
addListener = function(el, name, callback) {
function listener() {
if (!event.target) event.target = event.srcElement;
callback(event);
}
callback.$derbyListener = listener;
el.attachEvent('on' + name, listener);
};
removeListener = function(el, name, callback) {
el.detachEvent('on' + name, callback.$derbyListener);
};
}