This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (214 loc) · 5.14 KB
/
index.js
File metadata and controls
261 lines (214 loc) · 5.14 KB
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
const rxQuery = /^\s*([>+~])?\s*([*\w-]+)?(?:\[([\w-=[\]]+)\])?(?:#([\w-]+))?(?:\.([\w.-]+))?\s*/;
const rxClassOnly = /^\.([-\w]+)$/;
const rxIdOnly = /^#([-\w]+)$/;
function parseAttributes(string) {
if (!string) {
return;
}
return string.split('][').reduce((res, string) => {
const parts = string.split('=');
res[parts[0]] = parts[1] || '';
return res;
}, {});
}
function parseClasses(string) {
return string ? string.split('.') : undefined;
}
export function get(selector, root = document) {
const id = selector.match(rxIdOnly);
if (id) {
return document.getElementById(id[1]);
}
const className = selector.match(rxClassOnly);
if (className) {
return root.getElementsByClassName(className[1]);
}
return root.querySelectorAll(selector);
}
export function query(selector) {
let f;
const out = [];
if (typeof selector === 'string') {
while (selector) {
f = selector.match(rxQuery);
if (f[0] === '') {
break;
}
out.push({
rel: f[1],
tag: (f[2] || '').toUpperCase(),
attributes: parseAttributes(f[3]),
id: f[4],
classes: parseClasses(f[5])
});
selector = selector.substring(f[0].length);
}
}
return out;
}
export function createNs(namespaceURI, selector) {
const s = query(selector)[0];
const tag = s.tag;
if (!tag) {
return null;
}
const el = document.createElementNs(namespaceURI, tag);
const id = s.id;
if (id) {
el.id = id;
}
const classes = s.classes;
if (classes) {
el.className = classes.join(' ');
}
return el;
}
export function create(selector, content) {
const s = query(selector)[0];
const tag = s.tag;
if (!tag) {
return null;
}
const el = document.createElement(tag);
const id = s.id;
if (id) {
el.id = id;
}
const classes = s.classes;
if (classes) {
el.className = classes.join(' ');
}
const attrs = s.attributes;
if (attrs) {
for (const name in attrs) {
el.setAttribute(name, attrs[name]);
}
}
if (content) {
if (content instanceof Node) {
el.appendChild(content);
} else {
el.innerHTML = content;
}
}
return el;
}
export function empty(el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
return el;
}
export function closest(el, selector) {
while (!el.matches(selector) && (el = el.parentElement));
return el;
}
export function attr(el, name, value) {
if (value === undefined) {
return el.getAttribute(name);
}
el.setAttribute(name, value);
}
export function append(parent, el) {
parent.appendChild(el);
return parent;
}
export function prepend(parent, el) {
parent.insertBefore(el, parent.firstChild);
return parent;
}
export function appendTo(el, parent) {
parent.appendChild(el);
return el;
}
export function prependTo(el, parent) {
parent.insertBefore(el, parent.firstChild);
return el;
}
export function ready(fn) {
if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
export function on(el, event, selector, handler, options) {
if (typeof selector !== 'string') {
handler = selector;
selector = undefined;
}
if (!selector) {
el.addEventListener(event, handler, options);
return handler;
}
return on(el, event, e => {
const target = closest(e.target, selector);
if (target) {
handler.call(target, e);
}
}, options);
}
export function off(el, event, handler, options) {
el.removeEventListener(event, handler, options);
return handler;
}
export function once(el, event, handler, options) {
const _handler = (...args) => {
handler(...args);
off(el, event, handler);
}
el.addEventListener(event, handler, options);
return _handler;
}
export const ALL_EVENTS = '__on';
export function onEvents(ctx, events) {
if (!ctx[ALL_EVENTS]) {
ctx[ALL_EVENTS] = {}
}
for (const event in events) {
ctx[ALL_EVENTS][event] = on(ctx.el, event, events[event]);
}
}
export function offEvents(ctx) {
const events = ctx[ALL_EVENTS];
for (const event in events) {
off(ctx.el, event, events[event]);
}
delete ctx[ALL_EVENTS];
}
export function addClass(el, ...cls) {
return el.classList.add(...cls);
}
export function removeClass(el, ...cls) {
return el.classList.remove(...cls);
}
export function toggleClass(el, cls, force) {
return el.classList.toggle(cls, force);
}
export function addDelayRemoveClass(el, cls, delay, cb) {
addClass(el, cls);
return setTimeout(() => {
removeClass(el, cls);
cb && cb();
}, delay);
}
export function replaceClass(el, rx, newClass) {
const newClasses = [];
attr(el, 'class').split(' ').forEach(function(cls) {
const c = rx.test(cls) ? newClass : cls;
if (newClasses.indexOf(c) === -1) {
newClasses.push(c);
}
});
attr(el, 'class', newClasses.join(' '));
return newClasses.length;
}
export function insertBefore(el, node) {
return node.parentNode.insertBefore(el, node);
}
export function insertAfter(el, node) {
return node.parentNode.insertBefore(el, node.nextSibling);
}
export function remove(el) {
return el.parentNode.removeChild(el);
}