/
index.js
executable file
·415 lines (363 loc) · 12.1 KB
/
index.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
;(function() {
'use strict';
var slice = Array.prototype.slice;
function camelize(string) {
return string.replace(/-+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
}
function isNode(value) {
return value && (value.nodeType == 1 || value.nodeType == 3);
}
function isString(value) {
return Object.prototype.toString.call(value) === '[object String]';
}
function isUndefined(value) {
return void(0) === value;
}
var proto = (typeof Element != 'undefined') ? Element.prototype : {} ;
// Check for native `matchesSelector` method:
var matchesSelector = proto.matchesSelector || proto.webkitMatchesSelector
|| proto.mozMatchesSelector || proto.msMatchesSelector || proto.oMatchesSelector;
if (!matchesSelector) {
// No native `matchesSelector` method; create userland alternative:
matchesSelector = function(selector) {
var matches = this.parentNode.querySelectorAll(selector);
return slice.call(matches).indexOf(this) > -1;
};
}
/**
* Element#hasClass(className) -> Element
* - className (String): The CSS class name to check for.
*
* Checks is the element has the specified class name.
**/
function hasClass(className) {
var value = this.className;
return (value.length > 0
&& (value == className || new RegExp("(^|\\s)" + className + "(\\s|$)").test(value)));
}
/**
* Element#addClass(className) -> Element
* - className (String): The CSS class name to add to the element
*
* Adds `className` to the element.
**/
function addClass(className) {
var element = this;
className.split(/\s+/g).forEach(function(name) {
if (!Extensions.hasClass.call(element, name))
element.className += (element.className ? ' ' : '') + name;
});
return this;
}
/**
* Element#removeClass(className) -> Element
* - className (String): The CSS class name to remove from the element
*
* Removes `className` from the element.
**/
function removeClass(className) {
var result = this.className;
className.split(/\s+/g).forEach(function(name) {
result = result.replace(new RegExp("(^|\\s+)" + name + "(\\s+|$)"), ' ');
});
this.className = result.trim();
return this;
}
/**
* Element#toggleClass(className) -> Element
* - className (String): The CSS class name to toggle on the element
*
* Toggles `className` on the element.
**/
function toggleClass(className) {
return Extensions[
Extensions.hasClass.call(this, className) ? 'removeClass' : 'addClass'
].call(this, className);
}
/**
* Element#getStyle(style) -> String
* - style (String): The name of the style to get.
*
* Gets `style` from the element's computed style.
**/
function getStyle(style) {
style = style == 'float' ? 'cssFloat' : camelize(style);
var value = this.style[style];
if (!value || value == 'auto') {
var css = this.ownerDocument.defaultView.getComputedStyle(this, null);
value = css ? css[style] : null;
}
if (style == 'opacity') return value ? parseFloat(value) : 1.0;
return value == 'auto' ? null : value;
}
/**
* Element#setStyle(style) -> Element
* - style (String): The name of the style to set.
*
* Sets `style` on the element's style attribute.
**/
function setStyle(name, value) {
var styles;
if (arguments.length == 2) {
// Two args, name and value given.
styles = name + ':' + value + ';';
} else if (arguments.length == 1 && isString(name)) {
// One arg, and it's a string. Must be string of CSS.
styles = name;
}
var style = this.style;
if (styles) {
style.cssText += ';' + styles;
return this;
}
// Single argument which is not a string: must be an object of
// key/value pairs.
styles = name;
for (var property in styles)
style[
(property == 'float' || property == 'cssFloat')
? (isUndefined(style.styleFloat) ? 'cssFloat' : 'styleFloat')
: property
] = styles[property];
return this;
}
/**
* Element#hide() -> Element
*
* Hides the element.
**/
function hide() {
this.style.display = 'none';
return this;
}
/**
* Element#show() -> Element
*
* Shows the element. The
**/
function show() {
this.style.display = '';
return this;
}
/**
* Element#find(selector) -> Element
* - selector (String): CSS Selector
*
* Alias of Element#querySelector
**/
function find(selector) {
return this.querySelector(selector);
}
/**
* Element#findAll(selector) -> NodeList
* - selector (String): CSS Selector
*
* Alias of Element#querySelectorAll
**/
function findAll(selector) {
return this.querySelectorAll(selector);
}
/**
* Element#on(eventName[, selector], handler) -> Element
* - eventName (String): DOM Event Name to listen to.
* - selector (String): Optional CSS Selector.
* - handler (function|Object): Event handler of object with `handleEvent` method.
*
* Provides DOM event observing with optional event delegation when `selector`
* is provided.
*
* Unlike `addEventListener` API, the handler function is bound to the
* scope of the matched element, so `this` within the handler will refer
* to the element.
*
* NOTE: To prevent memory leaks, always call `Element#off` before removing
* and element from the dom.
**/
function on(eventName, selector, handler) {
var scope = this,
wrappedHandler;
if (!this.__registry__) {
// Lazily create the event registry.
Object.defineProperty(this, '__registry__', {
value: [],
configurable: false, enumerable: false, writable: true
});
}
if (typeof selector != 'undefined' && handler) {
wrappedHandler = function(event) {
var match = null;
if (selector) {
var current = event.target;
while(current !== scope && match == null) {
if (matchesSelector.call(current, selector)) {
match = current;
} else {
current = current.parentNode;
}
}
if (match) (handler.handleEvent || handler).call(match, event);
}
};
} else {
wrappedHandler = function(event) {
(selector.handleEvent || selector).call(scope, event);
};
}
this.__registry__.push([eventName, selector, handler, wrappedHandler]);
this.addEventListener(eventName, wrappedHandler, false);
return this;
}
/**
* Element#off([eventName], [selector], [handler]) -> Element
* - eventName (String): DOM Event Name to listen to.
* - selector (String): Optional CSS Selector.
* - handler (function|Object): Event handler of object with `handleEvent` method.
*
* Removes DOM event observes for the element. When called without,
* arguments, all event handlers are removed.
**/
function off(eventName, selector, handler) {
if (!this.__registry__) return this;
if (typeof selector == 'undefined' && typeof handler == 'undefined') {
this.__registry__.forEach(function(group) {
this.removeEventListener(group[0], group[3], false);
}.bind(this));
this.__registry__.length = 0;
return this;
}
for (var i=0; i<this.__registry__.length; i++) {
var group = this.__registry__[i];
if (group[0] == eventName && group[1] == selector && group[2] == handler) {
this.removeEventListener(eventName, group[3], false);
var rest = this.__registry__.slice(i+1);
this.__registry__.length = i;
this.__registry__.push.apply(this.__registry__, rest);
break;
}
}
return this;
}
/**
* Document#create(tag, [attributes], [content]) -> Element
* - tag (String): Element tag name.
* - attributes (Object): Optional hash of Element attributes
* - content (String|Node): Optional content to insert into the new element.
*
* Creates a new Element.
*
* TODO: Support namespaced tag names and attributes.
**/
function create(name, attributes, content) {
var element = document.createElement(name);
var contentIsNode, contentIsString, typeChecked;
if (attributes) {
if (!content && ((typeChecked = true, contentIsNode = isNode(attributes)) || (contentIsString = isString(attributes)))) {
content = attributes;
attributes = null;
}
}
if (attributes) {
for (var key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
if (content) {
if ((typeChecked && contentIsString) || (!typeChecked && isString(content))) {
element.innerHTML = content;
} else if ((typeChecked && contentIsNode) || (!typeChecked && isNode(content))) {
element.appendChild(content);
}
}
return element;
}
var Extensions = {
hasClass: hasClass,
addClass: addClass,
removeClass: removeClass,
toggleClass: toggleClass,
getStyle: getStyle,
setStyle: setStyle,
hide: hide,
show: show,
find: find,
findAll: findAll,
on: on,
off: off,
create: create
};
var Module = {
/**
* install([debug]) -> undefined
* - debug (Boolean): If debug messages should printed when a function is installed on DOM objects.
*
* Installs all module functions (except Module.install) on `Element.prototype`.
**/
install: function(debug) {
if (typeof Element == "undefined") {
if (debug && console)
console.log('DOM Additions could not be installed -- no Element object host.');
return;
}
Object.keys(Extensions).forEach(function(method) {
if (method.match(/^on|off$/) && !Window.prototype.hasOwnProperty(method)) {
Object.defineProperty(Window.prototype, method, {
value: Extensions[method],
enumerable: false, configurable: true, writable: true
});
if (debug && console) console.log("Installed Window#" + method + "()");
}
if (method.match(/^create|find|findAll|on|off$/) && !Document.prototype.hasOwnProperty(method)) {
Object.defineProperty(Document.prototype, method, {
value: Extensions[method],
enumerable: false, configurable: true, writable: true
});
if (debug && console) console.log("Installed Document#" + method + "()");
if (method === 'create') return; // Keep from adding `create` to Element.prototype.
}
if (!Element.prototype.hasOwnProperty(method)) {
Object.defineProperty(Element.prototype, method, {
value: Extensions[method],
enumerable: false, configurable: true, writable: true
});
if (debug && console) console.log("Installed Element#" + method + "()");
}
});
if (typeof NodeList != 'undefined') {
// Extend NodeList with common Array methods. Only installed if present
// on Array.prototype so make sure to extend that first if you want array
// extensions to make it onto NodeList.
['every', 'filter', 'forEach', 'map', 'slice', 'some', 'splice', 'invoke']
.forEach(function(method) {
if (Array.prototype[method] && !NodeList.prototype.hasOwnProperty(method)) {
Object.defineProperty(NodeList.prototype, method, {
value: Array.prototype[method],
enumerable: false, configurable: true, writable: true
});
}
});
}
}
};
// Copy methods over to the Module object.
Object.keys(Extensions).forEach(function(method) {
if (method == 'create') {
Module[method] = Extensions[method];
return;
}
Module[method] = function(element, args) {
return Extensions[method].apply(element, slice.call(arguments, 1));
}
});
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// RequireJS
define(function() { return Module; });
} else if (typeof module == 'object') {
// CommonJS
module.exports = Module;
} else if (typeof window == 'object') {
// No module system
Module.install();
}
}(this));