@webalternatif/js-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +12 -0
- package/.idea/inspectionProfiles/Project_Default.xml +12 -0
- package/.idea/js-core.iml +8 -0
- package/.idea/modules.xml +8 -0
- package/.idea/php.xml +20 -0
- package/.idea/vcs.xml +6 -0
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/cjs/array.js +116 -0
- package/dist/cjs/dom.js +466 -0
- package/dist/cjs/eventDispatcher.js +96 -0
- package/dist/cjs/i18n.js +50 -0
- package/dist/cjs/index.js +55 -0
- package/dist/cjs/is.js +85 -0
- package/dist/cjs/math.js +92 -0
- package/dist/cjs/random.js +38 -0
- package/dist/cjs/string.js +474 -0
- package/dist/cjs/stringPrototype.js +16 -0
- package/dist/cjs/traversal.js +110 -0
- package/dist/cjs/utils.js +118 -0
- package/dist/esm/array.js +116 -0
- package/dist/esm/dom.js +466 -0
- package/dist/esm/eventDispatcher.js +96 -0
- package/dist/esm/i18n.js +50 -0
- package/dist/esm/index.js +55 -0
- package/dist/esm/is.js +85 -0
- package/dist/esm/math.js +92 -0
- package/dist/esm/random.js +38 -0
- package/dist/esm/string.js +474 -0
- package/dist/esm/stringPrototype.js +16 -0
- package/dist/esm/traversal.js +110 -0
- package/dist/esm/utils.js +118 -0
- package/i18n/agenda/en.js +73 -0
- package/i18n/agenda/fr.js +73 -0
- package/i18n/agenda/index.js +2 -0
- package/i18n/ajaxform/en.js +5 -0
- package/i18n/ajaxform/fr.js +5 -0
- package/i18n/ajaxform/index.js +2 -0
- package/i18n/ajaxupload/en.js +12 -0
- package/i18n/ajaxupload/fr.js +12 -0
- package/i18n/ajaxupload/index.js +2 -0
- package/i18n/autocomplete/en.js +3 -0
- package/i18n/autocomplete/fr.js +3 -0
- package/i18n/autocomplete/index.js +2 -0
- package/i18n/confirm/en.js +5 -0
- package/i18n/confirm/fr.js +5 -0
- package/i18n/confirm/index.js +2 -0
- package/i18n/core/en.js +4 -0
- package/i18n/core/fr.js +4 -0
- package/i18n/core/index.js +2 -0
- package/i18n/datagrid/en.js +8 -0
- package/i18n/datagrid/fr.js +8 -0
- package/i18n/datagrid/index.js +2 -0
- package/i18n/date/en.js +51 -0
- package/i18n/date/fr.js +51 -0
- package/i18n/date/index.js +2 -0
- package/i18n/datetimepicker/en.js +30 -0
- package/i18n/datetimepicker/fr.js +30 -0
- package/i18n/datetimepicker/index.js +2 -0
- package/i18n/dialog/en.js +3 -0
- package/i18n/dialog/fr.js +3 -0
- package/i18n/dialog/index.js +2 -0
- package/i18n/fulldayeventagenda/en.js +73 -0
- package/i18n/fulldayeventagenda/fr.js +73 -0
- package/i18n/fulldayeventagenda/index.js +2 -0
- package/i18n/index.d.ts +4 -0
- package/i18n/index.js +15 -0
- package/i18n/richtexteditor/en.js +58 -0
- package/i18n/richtexteditor/fr.js +58 -0
- package/i18n/richtexteditor/index.js +2 -0
- package/i18n/select/en.js +3 -0
- package/i18n/select/fr.js +3 -0
- package/i18n/select/index.js +2 -0
- package/i18n/timepicker/en.js +3 -0
- package/i18n/timepicker/fr.js +3 -0
- package/i18n/timepicker/index.js +2 -0
- package/i18n/useragenda/en.js +74 -0
- package/i18n/useragenda/fr.js +73 -0
- package/i18n/useragenda/index.js +2 -0
- package/jest.config.js +14 -0
- package/package.json +33 -0
- package/src/array.js +124 -0
- package/src/dom.js +569 -0
- package/src/eventDispatcher.js +118 -0
- package/src/i18n.js +55 -0
- package/src/index.js +33 -0
- package/src/is.js +89 -0
- package/src/math.js +109 -0
- package/src/random.js +40 -0
- package/src/string.js +576 -0
- package/src/stringPrototype.js +15 -0
- package/src/traversal.js +134 -0
- package/src/utils.js +130 -0
- package/tests/array.test.js +326 -0
- package/tests/dom.test.js +239 -0
- package/tests/eventdispatcher.test.js +177 -0
- package/tests/i18n.test.js +132 -0
- package/tests/index.test.js +29 -0
- package/tests/is.test.js +354 -0
- package/tests/math.test.js +221 -0
- package/tests/random.test.js +72 -0
- package/tests/string.test.js +1106 -0
- package/tests/traversal.test.js +517 -0
- package/tests/utils.test.js +371 -0
- package/tsconfig.json +16 -0
- package/types/array.d.ts +8 -0
- package/types/dom.d.ts +241 -0
- package/types/eventDispatcher.d.ts +12 -0
- package/types/i18n.d.ts +4 -0
- package/types/index.d.ts +139 -0
- package/types/is.d.ts +16 -0
- package/types/math.d.ts +7 -0
- package/types/random.d.ts +7 -0
- package/types/string.d.ts +37 -0
- package/types/stringPrototype.d.ts +1 -0
- package/types/traversal.d.ts +7 -0
- package/types/utils.d.ts +7 -0
- package/webpack.config.cjs +31 -0
package/src/dom.js
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import {isObject, isPlainObject, isString} from "./is.js";
|
|
2
|
+
import {camelCase} from "./string.js";
|
|
3
|
+
import {each} from "./traversal.js";
|
|
4
|
+
import {compareArray} from "./array.js";
|
|
5
|
+
|
|
6
|
+
export const isWindow = function(o) {
|
|
7
|
+
return !!o && o === o.window;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const isDomElement = function(o) {
|
|
11
|
+
return isObject(o) && o instanceof HTMLElement;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const getStyle = function(elem, cssRule) {
|
|
15
|
+
if (!isDomElement(elem)) {
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (window.getComputedStyle) {
|
|
20
|
+
const computedStyle = window.getComputedStyle(elem, null);
|
|
21
|
+
|
|
22
|
+
return computedStyle.getPropertyValue(cssRule) || computedStyle[camelCase(cssRule)] || '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return elem.style[camelCase(cssRule)] || '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default {
|
|
29
|
+
/**
|
|
30
|
+
* @param {Element} el
|
|
31
|
+
* @param {string} [selector]
|
|
32
|
+
* @returns {NodeList}
|
|
33
|
+
*/
|
|
34
|
+
children(el, selector) {
|
|
35
|
+
return selector
|
|
36
|
+
? this.find(el, `:scope > ${selector}`)
|
|
37
|
+
: el.children;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {Element} el
|
|
42
|
+
* @param {string} [selector]
|
|
43
|
+
* @returns {Element|null}
|
|
44
|
+
*/
|
|
45
|
+
child(el, selector) {
|
|
46
|
+
return this.first(this.children(el, selector));
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {Element|Document|string} refEl
|
|
51
|
+
* @param {string} [selector='*']
|
|
52
|
+
* @returns {NodeList}
|
|
53
|
+
*/
|
|
54
|
+
find(refEl, selector = '*') {
|
|
55
|
+
if (isString(refEl)) {
|
|
56
|
+
selector = refEl;
|
|
57
|
+
refEl = document;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
return refEl.querySelectorAll(selector);
|
|
62
|
+
} catch(e) {
|
|
63
|
+
return document.querySelectorAll(':not(*)');
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {Element|Document|string} refEl
|
|
69
|
+
* @param {string} [selector='*']
|
|
70
|
+
* @returns {Element|null}
|
|
71
|
+
*/
|
|
72
|
+
findOne(refEl, selector = '*') {
|
|
73
|
+
if (isString(refEl)) {
|
|
74
|
+
selector = refEl;
|
|
75
|
+
refEl = document;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
return refEl.querySelector(selector);
|
|
80
|
+
} catch (e) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {Element} el
|
|
87
|
+
* @param {string} className
|
|
88
|
+
* @returns {Element}
|
|
89
|
+
*/
|
|
90
|
+
addClass(el, className) {
|
|
91
|
+
if (!className) return el;
|
|
92
|
+
|
|
93
|
+
const classNames = className.split(' ')
|
|
94
|
+
.map(c => c.trim())
|
|
95
|
+
.filter(Boolean);
|
|
96
|
+
|
|
97
|
+
el.classList.add(...classNames);
|
|
98
|
+
|
|
99
|
+
return el;
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @param {Element} el
|
|
104
|
+
* @param {string} classNames
|
|
105
|
+
* @returns {Element}
|
|
106
|
+
*/
|
|
107
|
+
removeClass(el, classNames) {
|
|
108
|
+
if (!classNames) return el;
|
|
109
|
+
|
|
110
|
+
el.classList.remove(...classNames.split(' ')
|
|
111
|
+
.map(c => c.trim())
|
|
112
|
+
.filter(Boolean));
|
|
113
|
+
|
|
114
|
+
return el;
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {Element} el
|
|
119
|
+
* @param {string} classNames
|
|
120
|
+
* @param {boolean} [force]
|
|
121
|
+
* @returns {Element}
|
|
122
|
+
*/
|
|
123
|
+
toggleClass(el, classNames, force) {
|
|
124
|
+
each(
|
|
125
|
+
classNames.split(' ')
|
|
126
|
+
.map(c => c.trim())
|
|
127
|
+
.filter(Boolean),
|
|
128
|
+
(i, c) => el.classList.toggle(c, force)
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
return el;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @param {Element} el
|
|
136
|
+
* @param {string} classNames
|
|
137
|
+
* @returns {boolean}
|
|
138
|
+
*/
|
|
139
|
+
hasClass(el, classNames) {
|
|
140
|
+
return compareArray(
|
|
141
|
+
[...el.classList],
|
|
142
|
+
classNames.split(' ')
|
|
143
|
+
.map(c => c.trim())
|
|
144
|
+
.filter(Boolean),
|
|
145
|
+
);
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @param {Element} el
|
|
150
|
+
* @param {Element} child
|
|
151
|
+
* @returns {Element}
|
|
152
|
+
*/
|
|
153
|
+
append(el, child) {
|
|
154
|
+
el.append(child);
|
|
155
|
+
return el;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {Element} el
|
|
160
|
+
* @param {Element} child
|
|
161
|
+
* @returns {Element}
|
|
162
|
+
*/
|
|
163
|
+
prepend(el, child) {
|
|
164
|
+
el.prepend(child);
|
|
165
|
+
return el;
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {Element} el
|
|
170
|
+
* @returns {void}
|
|
171
|
+
*/
|
|
172
|
+
remove(el) {
|
|
173
|
+
el.remove();
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {Element} el
|
|
178
|
+
* @param {string} [selector]
|
|
179
|
+
* @returns {Element|null}
|
|
180
|
+
*/
|
|
181
|
+
closest(el, selector) {
|
|
182
|
+
return el.closest(selector);
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {Element} el
|
|
187
|
+
* @param {string} [selector]
|
|
188
|
+
* @returns {Element|null}
|
|
189
|
+
*/
|
|
190
|
+
next(el, selector = null) {
|
|
191
|
+
let sibling = el.nextElementSibling;
|
|
192
|
+
|
|
193
|
+
if (!selector) return sibling;
|
|
194
|
+
|
|
195
|
+
if (sibling && sibling.matches(selector)) {
|
|
196
|
+
return sibling;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return sibling;
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {Element} el
|
|
204
|
+
* @param {string|null} [selector]
|
|
205
|
+
* @returns {Element|null}
|
|
206
|
+
*/
|
|
207
|
+
prev(el, selector = null) {
|
|
208
|
+
let sibling = el.previousElementSibling;
|
|
209
|
+
|
|
210
|
+
if (!selector) return sibling;
|
|
211
|
+
|
|
212
|
+
if (sibling && sibling.matches(selector)) {
|
|
213
|
+
return sibling;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return null;
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @param {Element} el
|
|
221
|
+
* @param {string} [selector]
|
|
222
|
+
* @returns {Element[]}
|
|
223
|
+
*/
|
|
224
|
+
nextAll(el, selector) {
|
|
225
|
+
const siblings = [];
|
|
226
|
+
|
|
227
|
+
let sibling = el.nextElementSibling;
|
|
228
|
+
|
|
229
|
+
while (sibling) {
|
|
230
|
+
if (undefined === selector || sibling.matches(selector)) {
|
|
231
|
+
siblings.push(sibling);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
sibling = sibling.nextElementSibling;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return siblings;
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param {Element} el
|
|
242
|
+
* @param {string} [selector]
|
|
243
|
+
* @returns {Element[]}
|
|
244
|
+
*/
|
|
245
|
+
prevAll(el, selector) {
|
|
246
|
+
const siblings = [];
|
|
247
|
+
|
|
248
|
+
let sibling = el.previousElementSibling;
|
|
249
|
+
|
|
250
|
+
while (sibling) {
|
|
251
|
+
if (undefined === selector || sibling.matches(selector)) {
|
|
252
|
+
siblings.push(sibling);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
sibling = sibling.previousElementSibling;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return siblings;
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @param {Element} el
|
|
263
|
+
* @param {Element} wrappingElement
|
|
264
|
+
* @returns {Element}
|
|
265
|
+
*/
|
|
266
|
+
wrap(el, wrappingElement) {
|
|
267
|
+
if (!wrappingElement.isConnected) {
|
|
268
|
+
el.parentNode.insertBefore(wrappingElement, el);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
this.append(wrappingElement, el);
|
|
272
|
+
|
|
273
|
+
return el;
|
|
274
|
+
},
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @param {Element} el
|
|
278
|
+
* @param {string} name
|
|
279
|
+
* @param {*} [value]
|
|
280
|
+
* @returns {Element|*}
|
|
281
|
+
*/
|
|
282
|
+
attr(el, name, value) {
|
|
283
|
+
if (undefined === value) return el.getAttribute(name);
|
|
284
|
+
|
|
285
|
+
if (null === value) {
|
|
286
|
+
el.removeAttribute(name);
|
|
287
|
+
} else {
|
|
288
|
+
el.setAttribute(name, value);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return el;
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @param {Element} el
|
|
296
|
+
* @param {string} name
|
|
297
|
+
* @param {*} [value]
|
|
298
|
+
* @returns {*|Element}
|
|
299
|
+
*/
|
|
300
|
+
prop(el, name, value) {
|
|
301
|
+
if (undefined === value) {
|
|
302
|
+
return el[name];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
el[name] = value;
|
|
306
|
+
return el;
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {Element} el
|
|
311
|
+
* @param {string} [html]
|
|
312
|
+
* @returns {Element|*}
|
|
313
|
+
*/
|
|
314
|
+
html(el, html) {
|
|
315
|
+
if (undefined === html) return el.innerHTML;
|
|
316
|
+
|
|
317
|
+
el.innerHTML = html;
|
|
318
|
+
return el;
|
|
319
|
+
},
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @param {Element} el
|
|
323
|
+
* @param {string} [text]
|
|
324
|
+
* @returns {Element|*}
|
|
325
|
+
*/
|
|
326
|
+
text(el, text) {
|
|
327
|
+
if (undefined === text) return el.innerText;
|
|
328
|
+
|
|
329
|
+
el.innerText = text;
|
|
330
|
+
return el;
|
|
331
|
+
},
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* @param {Element} el
|
|
335
|
+
* @returns {Element}
|
|
336
|
+
*/
|
|
337
|
+
hide(el) {
|
|
338
|
+
if (undefined === this.data(el, '__display__')) {
|
|
339
|
+
const display = getComputedStyle(el).display;
|
|
340
|
+
this.data(el, '__display__', display);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
el.style.display = 'none';
|
|
344
|
+
return el;
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @param {Element} el
|
|
349
|
+
* @returns {Element}
|
|
350
|
+
*/
|
|
351
|
+
show(el) {
|
|
352
|
+
const dataDisplay = this.data(el, '__display__')
|
|
353
|
+
|
|
354
|
+
if (undefined === dataDisplay) {
|
|
355
|
+
el.style.removeProperty('display');
|
|
356
|
+
} else {
|
|
357
|
+
el.style.display = dataDisplay;
|
|
358
|
+
this.removeData(el, '__display__');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return el;
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* @param {Element} el
|
|
366
|
+
* @returns {Element}
|
|
367
|
+
*/
|
|
368
|
+
toggle(el) {
|
|
369
|
+
return 'none' === el.style.display ? this.show(el) : this.hide(el);
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @param {Element} el
|
|
374
|
+
* @param {Object<string, string>|string} name
|
|
375
|
+
* @param {string} [value]
|
|
376
|
+
* @returns {Element}
|
|
377
|
+
*/
|
|
378
|
+
data(el, name, value) {
|
|
379
|
+
if (isPlainObject(name)) {
|
|
380
|
+
each(name, (k, v) => this.data(el, k, v));
|
|
381
|
+
return el;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const isAttr = /^data-/.test(name + '');
|
|
385
|
+
const key = camelCase(isAttr ? (name + '').replace(/^data-/, '') : name + '');
|
|
386
|
+
|
|
387
|
+
if (undefined === value) return el.dataset[key];
|
|
388
|
+
|
|
389
|
+
if (null === value) this.removeData(el, key);
|
|
390
|
+
|
|
391
|
+
el.dataset[key] = value;
|
|
392
|
+
|
|
393
|
+
return el;
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* @param {Element} el
|
|
398
|
+
* @param {string} name
|
|
399
|
+
* @returns {Element|*}
|
|
400
|
+
*/
|
|
401
|
+
removeData(el, name) {
|
|
402
|
+
const key = (name + '').replace(/^data-/, '').camelCase();
|
|
403
|
+
|
|
404
|
+
delete el.dataset[key];
|
|
405
|
+
|
|
406
|
+
return el;
|
|
407
|
+
},
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @param {Element|Document|Window} el
|
|
411
|
+
* @param {string} event
|
|
412
|
+
* @param {function} handler
|
|
413
|
+
* @param {AddEventListenerOptions|false} options
|
|
414
|
+
* @returns {Element}
|
|
415
|
+
*/
|
|
416
|
+
on(el, event, handler, options = false) {
|
|
417
|
+
el.addEventListener(event, handler, options);
|
|
418
|
+
return el;
|
|
419
|
+
},
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* @param {Element|Document|Window} el
|
|
423
|
+
* @param {string} event
|
|
424
|
+
* @param {function} handler
|
|
425
|
+
* @param {Object} options
|
|
426
|
+
* @returns {Element}
|
|
427
|
+
*/
|
|
428
|
+
off(el, event, handler, options) {
|
|
429
|
+
el.removeEventListener(event, handler, options);
|
|
430
|
+
return el;
|
|
431
|
+
},
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* @param {HTMLElement} el
|
|
435
|
+
* @param {Object<string, string>|string} styles
|
|
436
|
+
* @param {string} [value]
|
|
437
|
+
* @returns {Element}
|
|
438
|
+
*/
|
|
439
|
+
css(el, styles, value) {
|
|
440
|
+
if (isString(styles)) {
|
|
441
|
+
if (undefined === value) {
|
|
442
|
+
return styles.startsWith('--')
|
|
443
|
+
? el.style.getPropertyValue(styles)
|
|
444
|
+
: el.style[styles];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (styles.startsWith('--')) {
|
|
448
|
+
el.style.setProperty(styles, String(value));
|
|
449
|
+
} else {
|
|
450
|
+
el.style[styles] = value;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return el;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
each(styles, (name, v) => {
|
|
457
|
+
if (name.startsWith('--')) {
|
|
458
|
+
el.style.setProperty(name, String(v));
|
|
459
|
+
} else {
|
|
460
|
+
el.style[name] = v;
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
return el;
|
|
465
|
+
},
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* @param {Element} el
|
|
469
|
+
* @param {string} selectorClosest
|
|
470
|
+
* @param {string} selectorFind
|
|
471
|
+
* @returns {NodeList|null}
|
|
472
|
+
*/
|
|
473
|
+
closestFind(el, selectorClosest, selectorFind) {
|
|
474
|
+
const closest = this.closest(el, selectorClosest);
|
|
475
|
+
|
|
476
|
+
if (closest) {
|
|
477
|
+
return this.find(closest, selectorFind);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return null;
|
|
481
|
+
},
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* @param {Element} el
|
|
485
|
+
* @param {string} selectorClosest
|
|
486
|
+
* @param {string} selectorFindOne
|
|
487
|
+
* @returns {Element|null}
|
|
488
|
+
*/
|
|
489
|
+
closestFindOne(el, selectorClosest, selectorFindOne) {
|
|
490
|
+
const closest = this.closest(el, selectorClosest);
|
|
491
|
+
|
|
492
|
+
if (closest) {
|
|
493
|
+
return this.findOne(closest, selectorFindOne);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return null;
|
|
497
|
+
},
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* @param {NodeList} nodeList
|
|
501
|
+
* @returns {Element|null}
|
|
502
|
+
*/
|
|
503
|
+
first(nodeList) {
|
|
504
|
+
return nodeList?.length ? nodeList.item(0) : null;
|
|
505
|
+
},
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* @param {NodeList} nodeList
|
|
509
|
+
* @returns {Element|null}
|
|
510
|
+
*/
|
|
511
|
+
last(nodeList) {
|
|
512
|
+
return nodeList.length ? nodeList.item(nodeList.length - 1) : null;
|
|
513
|
+
},
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* @param {string} html
|
|
517
|
+
* @returns {Element}
|
|
518
|
+
*/
|
|
519
|
+
create(html) {
|
|
520
|
+
const tpl = document.createElement('template');
|
|
521
|
+
tpl.innerHTML = html.trim();
|
|
522
|
+
return tpl.content.firstElementChild;
|
|
523
|
+
},
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* @param {NodeList} nodeList
|
|
527
|
+
* @param {number} [index=0]
|
|
528
|
+
* @returns {Element|null}
|
|
529
|
+
*/
|
|
530
|
+
eq(nodeList, index = 0) {
|
|
531
|
+
if (Math.abs(index) >= nodeList.length) return null;
|
|
532
|
+
|
|
533
|
+
if (index < 0) {
|
|
534
|
+
index = nodeList.length + index;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return nodeList.item(index);
|
|
538
|
+
},
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* @param {Element} el
|
|
542
|
+
* @param {Element} newEl
|
|
543
|
+
* @returns {Element}
|
|
544
|
+
*/
|
|
545
|
+
after(el, newEl) {
|
|
546
|
+
return el.parentElement.insertBefore(newEl, el.nextElementSibling);
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* @param {Element} el
|
|
551
|
+
* @param {Element} newEl
|
|
552
|
+
* @returns {Element}
|
|
553
|
+
*/
|
|
554
|
+
before(el, newEl) {
|
|
555
|
+
return el.parentElement.insertBefore(newEl, el);
|
|
556
|
+
},
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* @param {Element} el
|
|
560
|
+
* @returns {Element}
|
|
561
|
+
*/
|
|
562
|
+
empty(el) {
|
|
563
|
+
while (el.firstChild) {
|
|
564
|
+
el.removeChild(el.firstChild);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return el;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {isFunction, isString, isUndefined} from "./is.js";
|
|
2
|
+
import {each, map} from "./traversal.js";
|
|
3
|
+
|
|
4
|
+
class EventDispatcher
|
|
5
|
+
{
|
|
6
|
+
#listeners = {};
|
|
7
|
+
|
|
8
|
+
addListener(eventsName, callback, context, ...args) {
|
|
9
|
+
if (!isFunction(callback)) {
|
|
10
|
+
throw new Error('Callback must be a function.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!isString(eventsName)) {
|
|
14
|
+
throw new Error('Events name must be a string separated by comma.');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const listener = { callback, context, args };
|
|
18
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
19
|
+
if (!eventName) return true; // continue
|
|
20
|
+
|
|
21
|
+
eventName = eventName.trim();
|
|
22
|
+
|
|
23
|
+
if (this.hasListener(eventName)) {
|
|
24
|
+
this.#listeners[eventName].push(listener);
|
|
25
|
+
} else {
|
|
26
|
+
this.#listeners[eventName] = [listener];
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
addListenerOnce(eventsName, callback, context, ...listenerArgs) {
|
|
34
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
35
|
+
eventName = eventName.trim();
|
|
36
|
+
|
|
37
|
+
if (!eventName) return true; // continue
|
|
38
|
+
|
|
39
|
+
const handler = (...args) => {
|
|
40
|
+
callback.apply(context,
|
|
41
|
+
[eventName]
|
|
42
|
+
.concat(listenerArgs)
|
|
43
|
+
.concat(args.slice(1))
|
|
44
|
+
);
|
|
45
|
+
this.removeListener(eventName, handler);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
this.addListener(eventName, handler, context);
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
dispatch(eventsName, ...args) {
|
|
55
|
+
if (!isString(eventsName)) {
|
|
56
|
+
throw new Error('Events name must be a string seperated by comma.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
60
|
+
eventName = eventName.trim();
|
|
61
|
+
|
|
62
|
+
if (!eventName) return true; // continue
|
|
63
|
+
|
|
64
|
+
if (!this.hasListener(eventName)) {
|
|
65
|
+
console.warn(`No listeners found for event: ${eventName}`);
|
|
66
|
+
return true; // continue
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
each(this.#listeners[eventName], (i, listener) => {
|
|
70
|
+
listener.callback.apply(listener.context,
|
|
71
|
+
[eventName]
|
|
72
|
+
.concat(listener.args)
|
|
73
|
+
.concat(args)
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
hasListener(eventName, callback, context) {
|
|
82
|
+
if (isUndefined(callback)) {
|
|
83
|
+
return !isUndefined(this.#listeners[eventName]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return !!map(this.#listeners[eventName], (i, listener) => {
|
|
87
|
+
return listener.callback === callback && listener.context === context ? true : null;
|
|
88
|
+
}).length;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
removeListener(eventName, callback, context) {
|
|
92
|
+
if (this.hasListener(eventName, callback, context)) {
|
|
93
|
+
if (isUndefined(callback)) {
|
|
94
|
+
this.#listeners[eventName].splice(0);
|
|
95
|
+
} else {
|
|
96
|
+
each(this.#listeners[eventName], (i) => {
|
|
97
|
+
this.#listeners[eventName].splice(i, 1);
|
|
98
|
+
delete this.#listeners[eventName];
|
|
99
|
+
return false; // break
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
getListeners(eventName) {
|
|
108
|
+
return eventName ? this.#listeners[eventName] || [] : this.#listeners;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
reset() {
|
|
112
|
+
this.#listeners = {};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const eventDispatcher = new EventDispatcher;
|
|
117
|
+
|
|
118
|
+
export default eventDispatcher
|