This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapter.js
270 lines (248 loc) · 8.16 KB
/
adapter.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
/**
* adapter.js
*
* A jQuery/MooTools adapter for various DOM operations.
* -----------------------------------------------------------------------------
*
* Composer.js is an MVC framework for creating and organizing javascript
* applications. For documentation, please visit:
*
* http://lyonbros.github.com/composer.js/
*
* -----------------------------------------------------------------------------
*
* Copyright (c) 2011, Lyon Bros LLC. (http://www.lyonbros.com)
*
* Licensed under The MIT License.
* Redistributions of files must retain the above copyright notice.
*/
(function() {
"use strict";
var Composer = this.Composer;
var global = this;
var document = global.document || {_blank: true};
var has_sizzle = !!global.Sizzle;
var has_jquery = !!global.jQuery;
var has_slick = !!global.Slick;
var which_adapter = function(types) {
var wrap = function(fn) {
return function(context, selector) {
context || (context = document);
if(types.native && context instanceof global.DocumentFragment) {
return types.native(context, selector);
} else {
return fn(context, selector);
}
};
};
if(has_slick && types.slick) return wrap(types.slick);
if(has_sizzle && types.sizzle) return wrap(types.sizzle);
if(has_jquery && types.jquery) return wrap(types.jquery);
if('querySelector' in document && types.native) return wrap(types.native);
if(document._blank) return function() {};
throw new Error('No selector engine present. Include Sizzle/jQuery or Slick/Mootools before loading composer (or use a modern browser with document.querySelector).');
};
var find = which_adapter({
slick: function(context, selector) {
return Slick.find(context, selector);
},
sizzle: function(context, selector) {
return Sizzle.select(selector, context)[0];
},
jquery: function(context, selector) {
return jQuery(context).find(selector)[0];
},
native: (function() {
var scope = false;
try { document.querySelector(':scope > h1'); scope = true; }
catch(e) {}
return function(context, selector) {
if(scope && !(context instanceof global.DocumentFragment)) selector = ':scope '+selector;
return context.querySelector(selector);
};
})()
});
var match = which_adapter({
slick: function(context, selector) {
return Slick.match(context, selector);
},
sizzle: function(context, selector) {
return Sizzle.matchesSelector(context, selector);
},
jquery: function(context, selector) {
return jQuery(context).is(selector);
},
native: function(context, selector) {
if('matches' in context) var domatch = context.matches;
if('msMatchesSelector' in context) var domatch = context.msMatchesSelector;
if('mozMatchesSelector' in context) var domatch = context.mozMatchesSelector;
if('webkitMatchesSelector' in context) var domatch = context.webkitMatchesSelector;
return domatch.call(context, selector);
}
});
var captured_events = {
'focus': true,
'blur': true
};
var add_event = (function() {
return function(el, ev, fn, selector) {
var capture = captured_events[ev] || false;
if(selector) {
el.addEventListener(ev, function(event) {
// if we have a mootools event class, wrap the event in it
if(event && global.MooTools && global.DOMEvent) event = new DOMEvent(event);
var target = event.target || event.srcElement;
while(target) {
if(match(target, selector)) {
fn.apply(this, [event].concat(event.params || []));
break;
}
target = target.parentNode;
if(target == el.parentNode || target == document.body.parentNode) {
target = false;
}
}
}, capture);
} else {
el.addEventListener(ev, function(event) {
// if we have a mootools event class, wrap the event in it
if(event && global.MooTools && global.DOMEvent) event = new DOMEvent(event);
fn.apply(this, [event].concat(event.params || []));
}, capture);
}
};
})();
var remove_event = (function() {
return function(el, ev, fn) {
el.removeEventListener(ev, fn, false);
};
})();
var fire_event = (function() {
/**
* NOTE: taken from http://stackoverflow.com/a/2381862/236331
*
* Fire an event handler to the specified node. Event handlers can
* detect that the event was fired programatically by testing for a
* 'synthetic=true' property on the event object
*
* @param {HTMLNode} node The node to fire the event handler on.
* @param {String} eventName The name of the event without the "on" (e.g., "focus")
*/
return function(el, type, options) {
options || (options = {});
if(type == 'click' && el.click) {
return el.click();
}
var ev = new CustomEvent(type, options.args);
el.dispatchEvent(ev);
};
})();
var find_parent = function(selector, element, stop) {
if(!element) return false;
if(element == stop) return false;
if(match(element, selector)) return element;
var par = element.parentNode;
return find_parent(selector, par);
};
var frame = function(cb) { global.requestAnimationFrame(cb); };
/**
* our xdom system! provides hooks for diffing/patching the DOM, and also
* allows parts of itself to be replaced by different implementations.
*/
var xdom = {
/**
* diff two DOM elements. in the default case, we use morphdom which
* does the diffing/patching in one call so we don't really need to do
* anything here, but other DOM patching libs may have discrete steps so
* we want to have a hook for it.
*/
diff: function(from, to, options) {
return [from, to];
},
/**
* patch the DOM! uses morphdom by default. takes a root DOM node to
* patch and a patch to apply to it.
*/
patch: function(root, diff, options) {
options || (options = {});
if(!root || !diff[1]) return;
var ignore_elements = options.ignore_elements || [];
var ignore_children = options.ignore_children || [];
return morphdom(root, diff[1], {
// this callback preserves form input values (text, checkboxes,
// radios, textarea, selects)
onBeforeElUpdated: function(from, to) {
if(options.reset_inputs) return;
if(options.before_update instanceof Function) {
options.before_update(from, to);
}
var tag = from.tagName.toLowerCase();
var from_type = from.getAttribute('type');
var to_tag = to.tagName.toLowerCase();
var to_type = to.getAttribute('type');
// we treat files differently, because you cannot
// programmatically set the value of a file input. so
// instead, we copy all the attributes from the `to`
// element into the `from` (except `value`, obvis) and
// then block morphdom from updating the from el by
// returning false.
if(to_tag == 'input' && to_type == 'file') {
if(tag == 'input' && from_type == 'file') {
// copy traits from to -> from
var attrs = to.attributes;
for(var i = 0, n = attrs.length; i < n; i++) {
var key = attrs.item(i).name;
// don't copy the value
if(key == 'value') continue;
from.setAttribute(key, to.getAttribute(key));
}
// block the update
return false;
}
// to is a file, from isn't. don't bother trying to
// preserve anything, just let the change happen
return;
}
switch(tag)
{
case 'input':
case 'textarea':
to.checked = from.checked;
to.value = from.value;
break;
case 'select':
to.value = from.value;
break;
}
},
onBeforeNodeDiscarded: function(node) {
if(ignore_elements.indexOf(node) >= 0) return false;
},
onBeforeElChildrenUpdated: function(from, to) {
if(ignore_children.indexOf(from) >= 0) return false;
},
childrenOnly: options.children_only
});
},
/**
* allows hooking in your own DOM diffing/patching library
*/
hooks: function(options) {
options || (options = {});
var diff = options.diff;
var patch = options.patch;
if(diff) xdom.diff = diff;
if(patch) xdom.patch = patch;
}
};
Composer.exp0rt({
find: find,
match: match,
add_event: add_event,
fire_event: fire_event,
remove_event: remove_event,
find_parent: find_parent,
frame: frame,
xdom: xdom
});
}).apply((typeof exports != 'undefined') ? exports : this);