forked from madrobby/zepto
/
core.js
208 lines (181 loc) · 5.75 KB
/
core.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
/**
* @license zepto.js v{{version_number}}
* - original by Thomas Fuchs (http://github.com/madrobby/zepto), forked by Miller Medeiros (http://github.com/millermedeiros/zepto).
* Released under the MIT license (http://www.opensource.org/licenses/mit-license.php)
* Build: {{build_number}} - Date: {{build_date}}
*/
(function(window, document){
/**
* zepto.js
* @constructor
* @namespace
* @param {string|zepto|HTMLElement} [selector]
* @param {HTMLElement|Document|zepto} [context]
*/
var zepto = function(selector, context){
if(this instanceof zepto){ //enforce `new` on constructor (scope-safe).
context = context || document;
var matched;
//inspired by jQuery.init method (highly simplified)
if(selector){
if(selector.nodeType){ //DOMElement
context = selector;
matched = [selector];
selector = null;
}else if(selector instanceof zepto){ //"clone" zepto object
selector = selector.selector;
context = selector.context;
}
if(context instanceof zepto){ //if finding descendant node(s) of all matched elements
matched = [];
context.each(function(el){
matched = matched.concat( zepto.makeArray(el.querySelectorAll(selector)) );
});
matched = zepto.unique(matched);
}else if(! matched){ //avoid querySelector if `selector` is a DOMElement
matched = zepto.makeArray( context.querySelectorAll(selector) );
}
}
this.selector = selector;
this.context = context;
this.add(matched);
}else{
return new zepto(selector, context);
}
}
/**
* zepto.js
* @constructor
* @namespace
* @param {string|zepto|HTMLElement} [selector]
* @param {HTMLElement|Document|zepto} [context]
*/
window.zepto = window.$ = zepto; //export '$' and 'zepto' to global scope
/**
* @namespace reference to zepto.prototype for easy plugin developement
*/
zepto.fn = zepto.prototype = {
/**
* Execute a function for each matched element.
* @param {function(this:zepto, HTMLElement)} fn
* @return {zepto}
*/
each : function(fn){
this.get().forEach(function(el){
fn.call(this, el); //bind `this` to zepto object
}, this);
return this;
},
/**
* Find descendant node based on selector.
* @param {string} selector
* @return {zepto}
*/
find : function(selector){
return zepto(selector, this);
},
/**
* Retrieve matched DOM elements.
* @param {number} [index] Element index
* @return {Array|HTMLElement} All matched elements if `index` is `undefined` or a single element if `index` is specified.
*/
get : function(index){
return zepto.isDef(index)? this[parseInt(index, 10)] : zepto.makeArray(this);
},
/**
* Reduce matched elements to a single element by index.
* @param {number} index Element Index
* @return {zepto}
*/
eq : function(index){
return zepto(this.get(index));
},
/**
* Add a set of elements to the stack.
* @param {Array} elements Selector string or Elements to be added to current stack.
*/
add : function(elements){
Array.prototype.push.apply(this, elements); //copy reference of elements to $[n] and update length (convert `zepto` into a pseudo-array object)
return this;
},
/**
* Pass each element in the current matched set through a function, producing a new zepto object containing the return values.
* @param {function(this:Element, number, Element):*} callback Function that will be called for each element.
*/
map : function(callback){
return zepto().add(zepto.map(this, function(el, i){
return callback.call(el, i, el);
}));
}
};
//------------------------------ Helpers ------------------------------//
/**
* Copy properties from one Object into another (mixin).
* - will extend `zepto` or `zepto.fn` by default if `second` is `undefined`.
* @param {Object} first
* @param {Object} [second]
* @return {Object}
*/
zepto.extend = zepto.fn.extend = function(first, second){
var key;
if(! second){ //extend zepto by default
second = first;
first = this;
}
for(key in second){
if(second.hasOwnProperty(key)){ //avoid copying properties from prototype and makes JSLint happy! :)
first[key] = second[key];
}
}
return first;
};
//generics (static methods)
zepto.extend({
/**
* Return Array without any duplicate items.
* @param {Array} array
* @return {Array}
*/
unique : function(array){
function unique(item, i, arr){
return arr.indexOf(item) === i;
}
return array.filter(unique);
},
/**
* Convert Array-like object into a true Array
* @param {Array} obj
* @return {Array}
*/
makeArray : function(obj){
return Array.prototype.slice.call(obj);
},
/**
* Check if parameter is different than `undefined`.
* @return {boolean} `true` if parameter isn't `undefined`.
*/
isDef : function(param){
return (typeof param !== 'undefined');
},
/**
* Translate all items in an array or array-like object to another array of items.
* - similar to `jQuery.map` and not to `Array.prototype.map`
* @param {Array} target Array or Array-like Object to be mapped.
* @param {function(*, number, Array): *} callback Function called for each item on the array passing "item" as first parameter and "index" as second parameter and "base array" as 3rd, if callback returns any value besides `null` will add value to "mapped" array.
*/
map : function(target, callback){
//didn't used `Array.prototype.map` because `jQuery.map` works different than JavaScript 1.6 `Array.map`
var ret = [],
value,
i,
n = target.length;
for(i = 0; i < n; i++){
value = callback(target[i], i);
if(value != null){
ret[ret.length] = value; //faster than push
}
}
return ret;
}
});
}(window, document));