-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDollarDollar.js
310 lines (280 loc) · 8.92 KB
/
DollarDollar.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
/*
* ---------------------------------
* DollarDollar
* Author: Mohamad Jahani @Baarande @FictionTeam
* Mon 29 Sep 2014 01:35:56 AM IRST
* ---------------------------------
*/
var DollarDollar, $$;
(function() {
"use strict";
/**
* -----------------------------
* Polyfills
* Most of them are here for "IE8".
* -----------------------------
*/
// No Polyfills for now! :)
/**
* DollarDollar
*/
window.DollarDollar = window.$$ = function(selector) {
return new DollarDollar(selector);
};
/**
* Selector
* @param {string|array} selector Selector string or an array of elements
* return {object} selected arrays
*/
DollarDollar = function(selector) {
var nodes;
if (typeof selector === 'string') {
//var classPattern = /\.-?[_a-zA-Z]+[_a-zA-Z0-9-]$/;
var classPattern = /^(?:\s*(<[\w\W]+>)[^>]*|\.([\w-]*))$/;
//var idPattern = /\#-?[_a-zA-Z]+[_a-zA-Z0-9-]$/;
var idPattern = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/;
if (classPattern.test(selector) && document.getElementsByClassName) {
nodes = document.getElementsByClassName(selector.slice(1));
} else if (idPattern.test(selector)) {
nodes = [];
nodes.push(document.getElementById(selector.slice(1)));
}
else {
nodes = document.querySelectorAll(selector);
}
this.query = selector;
} else if (typeof selector === 'object') {
nodes = selector.length ? selector : [selector];
} else {
return new $$.ready(selector);
}
for (var i = 0; i < nodes.length; i++) {
this[i] = nodes[i];
}
this.length = nodes.length;
};
DollarDollar.fn = DollarDollar.prototype = {
VERSION: '0.0.3',
isLatest: function() {
return true;
},
each: function(fn) {
for (var i = 0, len = this.length; i < len; i++) {
fn.apply(this[i], [i, this[i]]);
}
return this;
},
html: function(val) {
if (val === undefined) {
return this[this.length - 1].innerHTML;
} else {
this.each(function() {
this.innerHTML = val;
});
return this;
}
},
attr: function(prop, val) {
if (val === undefined) {
return this[this.length - 1].getAttribute(prop);
} else {
this.each(function() {
this.setAttribute(prop, val);
});
return this;
}
},
after: function(val) {
this.each(function() {
this.insertAdjacentHTML('afterend', val);
});
return this;
},
before: function(val) {
this.each(function() {
this.insertAdjacentHTML('beforebegin', val);
});
return this;
},
append: function(val) {
this.each(function() {
this.innerHTML += val;
});
return this;
},
hide: function() {
this.each(function() {
this.style.display = 'none';
});
return this;
},
show: function() {
this.each(function() {
this.style.display = '';
});
return this;
},
rm: function() {
this.each(function() {
this.parentNode.removeChild(this);
});
return this;
},
first: function() {
return new $$(this[0]);
},
last: function() {
return new $$(this[this.length - 1]);
},
at: function(index) {
return new $$(this[index]);
},
par: function() {
return new $$(this[this.length - 1].parentNode);
},
addCls: function(className) {
if (this.classList) {
this.each(function() {
this.classList.add(className);
});
} else {
this.each(function() {
this.className = this.className.length ? this.className + ' ' + className : className;
});
}
return this;
},
rmCls: function(className) {
if (this.classList) {
this.each(function() {
this.classList.remove(className);
});
} else {
this.each(function() {
this.className = this.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), '');
});
}
return this;
},
hasCls: function(className) {
if (this.classList) {
return this[this.length - 1].classList.contains(className);
} else {
return new RegExp('(^| )' + className + '( |$)', 'gi').test(this[this.length - 1].className);
}
},
css: function(prop, val) {
if (val === undefined) {
return this[this.length - 1].style[prop];
} else {
this.each(function() {
this.style[prop] = val;
});
return this;
}
},
on: function(ev, fn) {
this.each(function() {
this.addEventListener(ev, fn, false);
});
return this;
},
off: function(ev, fn) {
this.each(function() {
this.removeEventListener(ev, fn, false);
});
return this;
},
click: function(fn) {
this.each(function() {
this.addEventListener('click', fn);
});
return this;
}
};
/**
* Add main functions which can be used without selectors.
* They can be used like $$.functionName (or DollarDollar.functionName)
*/
var fns = {
VERSION: DollarDollar.fn.VERSION,
load: function(fn) {
window.addEventListener('load', fn, false);
return window;
},
ready: function(fn) {
document.addEventListener('DOMContentLoaded', fn, false);
return document;
},
each: function(ar, fn) {
return new $$(ar).each(fn);
},
ajax: function(options) {
var request = new XMLHttpRequest();
//options.type = options.type || 'GET';
request.open(options.type || 'GET', options.url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
if (options.success) {
options.success.apply(this, [request.responseText]);
}
} else {
if (options.error) {
options.error.apply(this, [request.statusText]);
}
}
};
request.onerror = function() {
if (options.error) {
options.error.apply(this, [request.statusText]);
}
};
if (options.type === "POST") {
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.send(options.data || "");
} else {
request.send();
}
return request;
},
on: function(el, ev, fn) {
return new $$(el).on(ev, fn);
},
/**
* Unbind events from element[s]
* @param {array|string} el Element[s] to remove event
* @param {string} ev Event name to remove
* @param {function} fn Function to remove
* @return {object} Selected elements
*/
off: function(el, ev, fn) {
return new $$(el).off(ev, fn);
},
/**
* Execute a function after a specified time
* @param {number} ms Time to wait in milliseconds
* @param {function} fn Function to execute after specified time
* @return {number} timerId
*/
wait: function(ms, fn) {
return window.setTimeout(fn, ms);
},
/**
* Clear wait timer
* @param {number} id timerId
* @return {number} timerId
*/
rmWait: function(id) {
window.clearInterval(id);
return id;
},
};
for (var fn in fns) {
$$[fn] = DollarDollar[fn] = fns[fn];
}
if (typeof define === 'function' && define.amd) {
define([], function() {
return DollarDollar;
});
}
})();