-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryX.js
357 lines (345 loc) · 11.7 KB
/
QueryX.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
var queryX = function (selector, context) {
return new QueryX(selector, context);
};
function QueryX(selector, context) {
if (!(this instanceof QueryX)) {
return new QueryX(selector, context);
}
if (typeof selector === 'string') {
this.nodes = Array.from((context || document).querySelectorAll(selector));
} else if (selector && selector.nodeName) {
this.nodes = [selector];
} else if (selector instanceof QueryX) {
this.nodes = selector.nodes;
} else {
this.nodes = [];
}
}
QueryX.prototype = {
get length() {
return this.nodes.length;
},
nodes: [],
addClass: function () {
return this.eacharg(arguments, function (node, className) {
node.classList.add(className);
});
},
adjacent: function (content, target, callback) {
var targets = typeof target === 'number' ? Array.from({ length: target }) : Array.from(target || []);
this.each(function (node, index) {
var fragment = document.createDocumentFragment();
targets.map(function (target) {
var processedContent = typeof content === 'function' ? content.call(this, target, index, node) : content;
var newNode = typeof processedContent === 'string' ? queryX.generate(processedContent) : processedContent;
return newNode instanceof QueryX ? newNode.nodes : newNode;
}).forEach(function (element) {
if (queryX.isInPage(element)) {
fragment.appendChild(queryX(element).clone().first());
} else {
fragment.appendChild(element);
}
});
callback.call(this, node, fragment);
});
return this;
},
after: function (content) {
return this.adjacent(content, 1, function (node, fragment) {
node.parentNode.insertBefore(fragment, node.nextSibling);
});
},
append: function (content) {
return this.adjacent(content, null, function (node, fragment) {
node.appendChild(fragment);
});
},
args: function (args, element, index) {
return (typeof args === 'function' ? args(element, index) : args)
.toString()
.split(/[\s,]+/)
.filter(function (arg) {
return arg.length;
});
},
array: function (callback) {
var self = this;
return this.nodes.reduce(function (acc, node, index) {
var result = callback.call(self, node, index);
return acc.concat(result !== false ? result : []);
}, []);
},
attr: function (name, value, dataPrefix) {
dataPrefix = dataPrefix ? 'data-' : '';
if (value === undefined) {
return this.nodes.length ? this.nodes[0].getAttribute(dataPrefix + name) : undefined;
} else {
this.each(function (node) {
if (value === null) {
node.removeAttribute(dataPrefix + name);
} else {
node.setAttribute(dataPrefix + name, value);
}
});
return this;
}
},
before: function (content) {
return this.adjacent(content, 1, function (node, fragment) {
node.parentNode.insertBefore(fragment, node);
});
},
children: function (selector) {
return queryX(this.map(function (node) {
return Array.from(node.children).filter(function (child) {
return !selector || child.matches(selector);
});
}).array());
},
clone: function () {
return queryX(this.map(function (node) {
var clonedNode = node.cloneNode(true);
return queryX(clonedNode);
}).array());
},
closest: function (selector) {
return queryX(this.map(function (node) {
var current = node;
while (current && current !== document) {
if (current.matches(selector)) {
return current;
}
current = current.parentNode;
}
return null;
}).array());
},
data: function (key, value) {
return this.attr(key, value, true);
},
each: function (callback) {
this.nodes.forEach(callback, this);
return this;
},
eacharg: function (args, callback) {
return this.each(function (node, index) {
queryX.args(args, node, index).forEach(function (arg) {
callback.call(this, node, arg);
}, this);
});
},
empty: function () {
return this.each(function (node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
});
},
filter: function (selector) {
var self = this;
var filterFn = typeof selector === 'function' ?
selector :
function (node) {
return node.matches(selector);
};
return queryX(this.nodes.filter(filterFn).map(function (node) {
return node;
}));
},
find: function (selector) {
return queryX(this.map(function (node) {
return node.querySelectorAll(selector);
}).array());
},
first: function () {
return this.nodes[0] || null;
},
generate: function (html) {
var template = document.createElement('template');
template.innerHTML = html.trim();
return queryX(template.content.childNodes);
},
hasClass: function (className) {
return this.nodes.some(function (node) {
return node.classList.contains(className);
});
},
html: function (htmlString) {
if (htmlString === undefined) {
return this.nodes.length ? this.nodes[0].innerHTML : '';
} else {
this.each(function (node) {
node.innerHTML = htmlString;
});
return this;
}
},
isInPage: function (node) {
return node !== document.body && document.body.contains(node);
},
last: function () {
return this.nodes[this.nodes.length - 1] || null;
},
map: function (callback) {
return queryX(this.array(callback));
},
not: function (selector) {
var self = this;
var filterFn = typeof selector === 'function' ?
selector :
function (node) {
return !queryX(node).is(selector);
};
return queryX(this.nodes.filter(filterFn));
},
off: function (event, handler) {
this.each(function (node) {
node.removeEventListener(event, handler);
});
return this;
},
on: function (event, selector, handler) {
this.each(function (node) {
node.addEventListener(event, function (e) {
if (e.target.matches(selector)) {
handler.call(e.target, e);
}
});
});
return this;
},
parent: function (selector) {
return queryX(this.map(function (node) {
return node.parentNode;
}).filter(selector).array());
},
prepend: function (content) {
return this.adjacent(content, 1, function (node, fragment) {
node.insertBefore(fragment, node.firstChild);
});
},
remove: function () {
this.each(function (node) {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
});
return this;
},
removeClass: function () {
return this.eacharg(arguments, function (node, className) {
node.classList.remove(className);
});
},
replace: function (content) {
var removed = [];
this.adjacent(content, 1, function (node, fragment) {
removed = removed.concat(Array.from(node.children));
node.parentNode.replaceChild(fragment, node);
});
return queryX(removed);
},
scroll: function () {
var firstNode = this.first();
if (firstNode) {
firstNode.scrollIntoView({ behavior: 'smooth' });
}
return this;
},
serialize: function () {
var self = this;
return this.map(function (form) {
return Array.from(form.elements).filter(function (element) {
return element.name && !element.disabled &&
['file', 'submit', 'reset', 'button'].indexOf(element.type) === -1 &&
!element.matches(':disabled');
}).map(function (element) {
if (element.type === 'select-multiple') {
return Array.from(element.selectedOptions).map(function (option) {
return encodeURIComponent(element.name) + '=' + encodeURIComponent(option.value);
}).join('&');
} else {
return encodeURIComponent(element.name) + '=' + encodeURIComponent(element.value);
}
}).join('&');
}).array().join('&');
},
siblings: function (selector) {
return this.parent().children(selector).not(this);
},
size: function () {
var rect = this.first().getBoundingClientRect();
return rect ? { width: rect.width, height: rect.height } : null;
},
slice: function (arrayLike) {
return Array.prototype.slice.call(arrayLike || this.nodes || []);
},
str: function (element, index) {
return function (value) {
return typeof value === 'function' ? value.call(this, element, index) : value.toString();
};
},
text: function (text) {
if (text === undefined) {
return this.nodes.length ? this.nodes[0].textContent : '';
} else {
this.each(function (node) {
node.textContent = text;
});
return this;
}
},
toggleClass: function (className, state) {
if (state !== undefined) {
return this[state ? 'addClass' : 'removeClass'](className);
} else {
return this.eacharg(className, function (node, className) {
node.classList.toggle(className);
});
}
},
trigger: function (event, detail) {
var customEvent;
if (typeof CustomEvent === 'function') {
customEvent = new CustomEvent(event, { bubbles: true, cancelable: true, detail: detail });
} else {
customEvent = document.createEvent('CustomEvent');
customEvent.initCustomEvent(event, true, true, detail);
}
return this.each(function (node) {
node.dispatchEvent(customEvent);
});
},
unique: function () {
var uniqueNodes = this.nodes.reduce(function (acc, node) {
if (acc.indexOf(node) === -1) {
acc.push(node);
}
return acc;
}, []);
return queryX(uniqueNodes);
},
uri: function (string) {
return encodeURIComponent(string)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
.replace(/%20/g, '+');
},
wrap: function (wrapper) {
var wrapperNode = typeof wrapper === 'string' ? queryX.generate(wrapper).first() : wrapper;
this.each(function (node) {
var parent = node.parentNode;
if (parent) {
parent.insertBefore(wrapperNode, node);
wrapperNode.appendChild(node);
}
});
return this;
}
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = queryX;
}