-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathair.js
86 lines (76 loc) · 2.29 KB
/
air.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
;(function() {
'use strict'
var plug = require('zephyr');
/**
* Chainable wrapper class.
*
* This is the core of the entire plugin system and typically you extend
* functionality by adding methods to the prototype of this function.
*
* However a plugin may also add static methods to the main function,
* see the `create` plugin for an example of adding static methods.
*
* This implementation targets modern browsers (IE9+) and requires that
* the array methods `isArray`, `forEach` and `filter` are available,
* for older browsers you will need to include polyfills.
*
* @param el A selector, DOM element, array of elements or Air instance.
* @param context The context element for a selector.
*/
function Air(el, context) {
if(!(this instanceof Air)) {
return new Air(el, context);
}
if(typeof context === 'string') {
context = document.querySelector(context);
}else if(context instanceof Air) {
context = context.get(0);
}
context = context || document;
// NOTE: do not pass empty string to querySelectorAll
if(!arguments.length || el === '') {
this.dom = [];
}else{
this.dom = typeof el === 'string' ? context.querySelectorAll(el) : el;
}
if(el instanceof Air) {
this.dom = el.dom.slice(0);
}else if(!Array.isArray(el)) {
if(this.dom instanceof NodeList) {
this.dom = Array.prototype.slice.call(this.dom);
}else if(el) {
this.dom = ((el instanceof Node) || el === window) ? [el] : [];
}
}
// shortcut to Array.prototype.slice
this.slice = Array.prototype.slice;
}
var proto = Air.prototype
, air = plug({proto: proto, type: Air});
/**
* Get the number of wrapped DOM elements.
*/
Object.defineProperty(
proto, 'length', {get: function(){return this.dom.length}});
/**
* Get the DOM element at the specified index.
*/
proto.get = function get(index) {
if(index === undefined) {
return this.dom;
}
return this.dom[index];
}
/**
* Iterate the encapsulated DOM elements.
*/
proto.each = function each(cb) {
this.dom.forEach(cb);
return this;
}
// expose class reference
air.Air = Air;
// alias main function
proto.air = air;
module.exports = air;
})();