-
Notifications
You must be signed in to change notification settings - Fork 3
/
nunjs.js
408 lines (389 loc) · 9.46 KB
/
nunjs.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
/**
nunjs 0.0.5
https://github.com/tvrzna/nunjs
Released under the MIT License.
**/
Nunjs = {
each: function(obj, callback) {
var length, i = 0;
if (Array.isArray(obj) || obj instanceof NodeList) {
length = obj.length;
for (; i < length; i++ ) {
if (callback.call(obj[i], i, obj[i]) === false ) {
break;
}
}
} else {
callback.call(obj, 0, obj);
}
return obj;
},
includes: function(arr, el) {
if (arr && el) {
return arr.indexOf(el) >= 0;
}
return false;
},
matches: function(obj, selector) {
if (!selector || !obj || obj.nodeType !== 1) return false;
var matchesSelector = obj.matches || obj.webkitMatchesSelector ||
obj.mozMatchesSelector || obj.oMatchesSelector ||
obj.matchesSelector || obj.msMatchesSelector;
if (matchesSelector) return matchesSelector.call(obj, selector);
return false;
},
removeWithChildren : function (i, el) {
$(el).find('*').each(Nunjs.removeWithChildren);
$(el).remove();
},
'_events' : {}
};
window.$ = function(selector) {
var dom;
if (typeof selector === 'string') {
if (selector.trim().startsWith('<') && selector.trim().endsWith('>')) {
var tmp = document.createElement('tmp');
tmp.innerHTML = selector.trim();
dom = tmp.firstChild;
} else {
dom = document.querySelectorAll(selector);
}
} else {
dom = selector;
}
var nunjs = {
nunjs: '0.0.5',
addClass: function(name) {
this.each(function() {
if (!this.classList.contains(name)) {
this.classList.add(name);
}
});
return this;
},
appendTo: function(selector) {
var target = 'string' == typeof selector ? document.querySelectorAll(selector) : selector;
var result = [];
this.each(function() {
for(var i = 0; i <= target.length; i++) {
var copy = this.cloneNode(true);
target[i].appendChild(copy);
result.push(copy);
}
});
return $(result);
},
attr: function(attr, value) {
if (value === null || value === undefined) {
return this[0].getAttribute(attr);
} else {
this.each(function() {
this.setAttribute(attr, value);
});
return this;
}
},
click: function(trigger) {
if (trigger === null || typeof trigger !== 'function') {
this.each(function() {
this.click();
});
} else {
this.each(function(i, el) {
$(el).on('click', trigger);
});
}
return this;
},
clone: function() {
var result = [];
this.each(function () {
var clone = this.cloneNode(true);
result.push(clone);
});
return $(result);
},
each: function(callback) {
Nunjs.each(dom, callback);
return this;
},
find: function(selector) {
var result = [];
this.each(function() {
var data = this.querySelectorAll(selector);
for(var i=0; i < data.length; i++) {
if (!Nunjs.includes(result, data[i]))
result.push(data[i]);
}
});
return $(result);
},
hasClass: function(name) {
var result = false;
this.each(function() {
if (this.classList.contains(name))
result = true;
return;
});
return result;
},
hasEvent: function(event) {
return Nunjs._events[this[0]] !== undefined && Nunjs._events[this[0]][event] !== undefined;
},
hide: function() {
this.each(function() {
this.style.display = 'none';
});
return this;
},
html: function(content) {
if (content === undefined) {
return this[0].innerHTML;
}
this.find('*').each(Nunjs.removeWithChildren);
this.each(function() {
this.innerHTML = content;
});
return this;
},
insertAfter: function(selector) {
var target = 'string' == typeof selector ? document.querySelectorAll(selector) : selector;
var result = [];
this.each(function() {
for(var i = 0; i <= target.length; i++) {
var copy = this.cloneNode(true);
target.parent()[0].insertBefore(copy, target[i].nextSibling);
result.push(copy);
}
});
return $(result);
},
insertBefore: function(selector) {
var target = 'string' == typeof selector ? document.querySelectorAll(selector) : selector;
var result = [];
this.each(function() {
for(var i = 0; i <= target.length; i++) {
var copy = this.cloneNode(true);
target.parent()[0].insertBefore(copy, target[i]);
result.push(copy);
}
});
return $(result);
},
is: function(selector) {
return Nunjs.matches(this[0], selector);
},
off: function(event, trigger) {
this.each(function() {
if (trigger === undefined) {
for (var i = 0; i < Nunjs._events[this][event].length; i++) {
this.removeEventListener(event, Nunjs._events[this][event][i]);
}
Nunjs._events[this][event] = [];
} else {
this.removeEventListener(event, trigger);
}
});
return this;
},
on: function(event, trigger) {
this.each(function() {
if (!(this in Nunjs._events)) Nunjs._events[this] = {};
if (!(event in Nunjs._events[this])) Nunjs._events[this][event] = [];
Nunjs._events[this][event].push(trigger);
this.addEventListener(event, trigger);
});
return this;
},
parent: function(selector) {
var result = [];
this.each(function(i, el) {
if (el.parentElement === undefined)
return;
if (!selector && !Nunjs.includes(result, el.parentElement))
result.push(el.parentElement);
if (selector) {
if (Nunjs.matches(el.parentElement, selector) && !Nunjs.includes(result, el.parentElement)) {
result.push(el.parentElement);
} else if(!Nunjs.matches(el, selector)) {
var parent = el.parentElement;
do {
if (Nunjs.matches(parent, selector)) {
if (!Nunjs.includes(result, parent))
result.push(parent);
break;
}
parent = parent.parentNode;
} while(parent !== 'undefined');
}
}
});
return $(result);
},
prependTo: function(selector) {
var target = 'string' == typeof selector ? document.querySelectorAll(selector) : selector;
var result = [];
this.each(function() {
for(var i = 0; i <= target.length; i++) {
var copy = this.cloneNode(true);
target[i].insertBefore(copy, target[i].childNodes[0]);
result.push(copy);
}
});
return $(result);
},
prop: function(prop, value) {
if (value === null || value === undefined) {
return this[0][prop];
} else {
this.each(function() {
this[prop] = value;
});
return this;
}
},
ready: function(trigger){
this.each(function() {
this.addEventListener('DOMContentLoaded', trigger);
});
return this;
},
remove: function() {
this.each(function() {
Nunjs._events[this] = [];
this.remove();
});
},
removeClass: function(name) {
this.each(function() {
if (this.classList.contains(name))
this.classList.remove(name);
});
return this;
},
removeAttr: function(name) {
this.each(function() {
this.removeAttribute(name);
});
return this;
},
removeProp: function(prop) {
this.each(function() {
this[prop] = undefined;
});
return this;
},
show: function() {
this.each(function() {
this.style.display == 'none' && (this.style.display = '');
if (getComputedStyle(this, '').getPropertyValue('display') == 'none')
this.style.display = 'block';
});
return this;
},
submit: function() {
this.each(function() {
this.submit();
});
return this;
},
text: function(content) {
var resultText = "";
this.each(function() {
if (content === undefined)
resultText += this.textContent;
else
this.textContent = content;
});
if (content === undefined) {
return resultText;
}
return this;
},
toggle: function() {
var show = getComputedStyle(this[0], '').getPropertyValue('display') == 'none';
this.each(function() {
show ? $(this).show() : $(this).hide();
});
return this;
},
val: function(value) {
if (value === undefined) {
return this[0].value;
} else {
this.each(function() {
this.value = value;
});
return this;
}
}
};
nunjs.length = 0;
Nunjs.each(dom, function(i, el) {
nunjs[i] = el;
nunjs.length = i;
});
return nunjs;
};
window.$.ajax = function(arg1, arg2, type) {
var options;
if (typeof arg1 === 'object') {
options = arg1;
} else if (typeof arg2 === 'object') {
options = arg2;
} else {
options = {};
}
if (typeof arg1 === 'string') {
options.url = arg1;
}
if (type !== undefined && typeof type === 'string') {
options.type = type;
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (options.success !== undefined) {
options.success(this.responseText, this);
}
} else {
if (options.error !== undefined) {
options.error(this);
}
}
if (options.complete !== undefined) {
options.complete();
}
}
};
xhttp.open(options.type !== undefined ? options.type : 'GET', options.url, options.async !== undefined ? options.async : true);
if (options.headers !== undefined) {
for (var name in options.headers) {
xhttp.setRequestHeader(name, options.headers[name]);
}
}
if (options.data) {
var body;
switch (typeof options.data) {
case 'object':
body = JSON.stringify(options.data);
break;
default:
body = options.data;
}
xhttp.send(body);
} else {
xhttp.send();
}
return xhttp;
};
window.$.post = function(arg1, arg2) {
return window.$.ajax(arg1, arg2, 'POST');
};
window.$.get = function(arg1, arg2) {
return window.$.ajax(arg1, arg2, 'GET');
};
window.$.put = function(arg1, arg2) {
return window.$.ajax(arg1, arg2, 'PUT');
};