@webalternatif/js-core 1.1.1 → 1.1.2
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/dist/cjs/dom.js +173 -74
- package/dist/cjs/is.js +5 -1
- package/dist/cjs/traversal.js +117 -25
- package/dist/esm/array.js +24 -35
- package/dist/esm/dom.js +185 -95
- package/dist/esm/eventDispatcher.js +15 -21
- package/dist/esm/i18n.js +11 -21
- package/dist/esm/index.js +16 -47
- package/dist/esm/is.js +22 -38
- package/dist/esm/math.js +21 -33
- package/dist/esm/random.js +8 -21
- package/dist/esm/string.js +74 -113
- package/dist/esm/stringPrototype.js +3 -6
- package/dist/esm/traversal.js +139 -59
- package/dist/esm/utils.js +14 -27
- package/package.json +1 -1
- package/types/dom.d.ts +53 -35
- package/types/index.d.ts +22 -19
- package/types/is.d.ts +1 -0
- package/types/traversal.d.ts +10 -7
package/dist/esm/array.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { each } from "./traversal.js";
|
|
2
|
+
import { isArray, isInteger, isObject, isString, isUndefined } from "./is.js";
|
|
3
|
+
import { round } from "./math.js";
|
|
4
|
+
import { equals } from "./utils.js";
|
|
2
5
|
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.range = exports.indexOf = exports.inArray = exports.compareArray = exports.array_unique = exports.array_diff = exports.arrayUnique = exports.arrayDiff = void 0;
|
|
7
|
-
var _traversal = require("./traversal.js");
|
|
8
|
-
var _is = require("./is.js");
|
|
9
|
-
var _math = require("./math.js");
|
|
10
|
-
var _utils = require("./utils.js");
|
|
11
6
|
/**
|
|
12
7
|
* Checks if a value exists in an array or an object
|
|
13
8
|
*
|
|
@@ -29,9 +24,9 @@ var _utils = require("./utils.js");
|
|
|
29
24
|
* inArray(5, [1, 2, 3])
|
|
30
25
|
* // → false
|
|
31
26
|
*/
|
|
32
|
-
const inArray = function (value, arr, index = 0, strict = false) {
|
|
27
|
+
export const inArray = function (value, arr, index = 0, strict = false) {
|
|
33
28
|
let ret = false;
|
|
34
|
-
|
|
29
|
+
each(arr, (i, val) => {
|
|
35
30
|
if (i >= index) {
|
|
36
31
|
if (strict) {
|
|
37
32
|
if (val === value) {
|
|
@@ -39,10 +34,10 @@ const inArray = function (value, arr, index = 0, strict = false) {
|
|
|
39
34
|
return false;
|
|
40
35
|
}
|
|
41
36
|
} else {
|
|
42
|
-
if (
|
|
43
|
-
ret =
|
|
37
|
+
if (isObject(value) && isObject(val)) {
|
|
38
|
+
ret = equals(val, value);
|
|
44
39
|
return false;
|
|
45
|
-
} else if (
|
|
40
|
+
} else if (isArray(value) && isObject(val)) {
|
|
46
41
|
ret = compareArray(val, value);
|
|
47
42
|
return false;
|
|
48
43
|
} else if (val == value) {
|
|
@@ -54,8 +49,7 @@ const inArray = function (value, arr, index = 0, strict = false) {
|
|
|
54
49
|
});
|
|
55
50
|
return ret;
|
|
56
51
|
};
|
|
57
|
-
|
|
58
|
-
const indexOf = function (arr, elt, from = 0) {
|
|
52
|
+
export const indexOf = function (arr, elt, from = 0) {
|
|
59
53
|
from = from < 0 ? Math.ceil(from) + arr.length : Math.floor(from);
|
|
60
54
|
for (; from < arr.length; from++) {
|
|
61
55
|
if (from in arr && arr[from] === elt) {
|
|
@@ -64,14 +58,13 @@ const indexOf = function (arr, elt, from = 0) {
|
|
|
64
58
|
}
|
|
65
59
|
return -1;
|
|
66
60
|
};
|
|
67
|
-
|
|
68
|
-
const compareArray = function (a1, a2) {
|
|
61
|
+
export const compareArray = function (a1, a2) {
|
|
69
62
|
if (a1.length !== a2.length) {
|
|
70
63
|
return false;
|
|
71
64
|
} else {
|
|
72
65
|
for (let i = 0; i < a1.length; i++) {
|
|
73
|
-
if (
|
|
74
|
-
if (!
|
|
66
|
+
if (isArray(a1[i])) {
|
|
67
|
+
if (!isArray(a2[i])) {
|
|
75
68
|
return false;
|
|
76
69
|
}
|
|
77
70
|
return compareArray(a1[i], a2[i]);
|
|
@@ -82,35 +75,31 @@ const compareArray = function (a1, a2) {
|
|
|
82
75
|
}
|
|
83
76
|
return true;
|
|
84
77
|
};
|
|
85
|
-
|
|
86
|
-
const arrayUnique = function (arr) {
|
|
78
|
+
export const arrayUnique = function (arr) {
|
|
87
79
|
return arr.filter((el, index, arr) => index === indexOf(arr, el));
|
|
88
80
|
};
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
const arrayDiff = (array1, array2, strict = false) => {
|
|
81
|
+
export const array_unique = arrayUnique;
|
|
82
|
+
export const arrayDiff = (array1, array2, strict = false) => {
|
|
92
83
|
return array1.filter(item => !inArray(item, array2, 0, strict));
|
|
93
84
|
};
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
step = (0, _math.round)(step);
|
|
85
|
+
export const array_diff = arrayUnique;
|
|
86
|
+
export const range = function (size, startAt = 0, step = 1) {
|
|
87
|
+
size = round(size);
|
|
88
|
+
step = round(step);
|
|
99
89
|
const rng = [];
|
|
100
|
-
if (
|
|
90
|
+
if (isUndefined(startAt) || size < 1 || step === 0 || size < Math.abs(step)) {
|
|
101
91
|
return rng;
|
|
102
92
|
}
|
|
103
93
|
const end = size * step;
|
|
104
|
-
if (
|
|
94
|
+
if (isString(startAt)) {
|
|
105
95
|
startAt = startAt.charCodeAt(0);
|
|
106
96
|
for (let i = 0; step > 0 ? i < end : i > end; i += step) {
|
|
107
97
|
rng.push(String.fromCharCode(startAt + i));
|
|
108
98
|
}
|
|
109
|
-
} else if (
|
|
99
|
+
} else if (isInteger(startAt)) {
|
|
110
100
|
for (let i = 0; step > 0 ? i < end : i > end; i += step) {
|
|
111
101
|
rng.push(startAt + i);
|
|
112
102
|
}
|
|
113
103
|
}
|
|
114
104
|
return rng;
|
|
115
|
-
};
|
|
116
|
-
exports.range = range;
|
|
105
|
+
};
|
package/dist/esm/dom.js
CHANGED
|
@@ -1,33 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _is = require("./is.js");
|
|
8
|
-
var _string = require("./string.js");
|
|
9
|
-
var _traversal = require("./traversal.js");
|
|
10
|
-
var _array = require("./array.js");
|
|
11
|
-
const isWindow = function (o) {
|
|
1
|
+
import { isArray, isArrayLike, isObject, isString } from "./is.js";
|
|
2
|
+
import { camelCase } from "./string.js";
|
|
3
|
+
import { each, foreach, map } from "./traversal.js";
|
|
4
|
+
import { inArray } from "./array.js";
|
|
5
|
+
const cssNumber = ['animationIterationCount', 'aspectRatio', 'borderImageSlice', 'columnCount', 'flexGrow', 'flexShrink', 'fontWeight', 'gridArea', 'gridColumn', 'gridColumnEnd', 'gridColumnStart', 'gridRow', 'gridRowEnd', 'gridRowStart', 'lineHeight', 'opacity', 'order', 'orphans', 'scale', 'widows', 'zIndex', 'zoom', 'fillOpacity', 'floodOpacity', 'stopOpacity', 'strokeMiterlimit', 'strokeOpacity'];
|
|
6
|
+
export const isWindow = function (o) {
|
|
12
7
|
return !!o && o === o.window;
|
|
13
8
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return (0, _is.isObject)(o) && o instanceof HTMLElement;
|
|
9
|
+
export const isDomElement = function (o) {
|
|
10
|
+
return isObject(o) && o instanceof HTMLElement;
|
|
17
11
|
};
|
|
18
|
-
|
|
19
|
-
const getStyle = function (elem, cssRule) {
|
|
12
|
+
export const getStyle = function (elem, cssRule) {
|
|
20
13
|
if (!isDomElement(elem)) {
|
|
21
14
|
return '';
|
|
22
15
|
}
|
|
23
16
|
if (window.getComputedStyle) {
|
|
24
17
|
const computedStyle = window.getComputedStyle(elem, null);
|
|
25
|
-
return computedStyle.getPropertyValue(cssRule) || computedStyle[
|
|
18
|
+
return computedStyle.getPropertyValue(cssRule) || computedStyle[camelCase(cssRule)] || '';
|
|
26
19
|
}
|
|
27
|
-
return elem.style[
|
|
20
|
+
return elem.style[camelCase(cssRule)] || '';
|
|
28
21
|
};
|
|
29
|
-
|
|
30
|
-
var _default = exports.default = {
|
|
22
|
+
export default {
|
|
31
23
|
/**
|
|
32
24
|
* @param {Element} el
|
|
33
25
|
* @param {string} [selector]
|
|
@@ -45,56 +37,66 @@ var _default = exports.default = {
|
|
|
45
37
|
return this.first(this.children(el, selector));
|
|
46
38
|
},
|
|
47
39
|
/**
|
|
48
|
-
* @param {Element|Document
|
|
49
|
-
* @param {string} [selector
|
|
50
|
-
* @returns {
|
|
40
|
+
* @param {Element|Document} refEl
|
|
41
|
+
* @param {string|Element|NodeList|Array<Element>} [selector]
|
|
42
|
+
* @returns {Element|null}
|
|
51
43
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
selector = refEl;
|
|
55
|
-
refEl = document;
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
return refEl.querySelectorAll(selector);
|
|
59
|
-
} catch (e) {
|
|
60
|
-
return document.querySelectorAll(':not(*)');
|
|
61
|
-
}
|
|
44
|
+
findOne(refEl, selector) {
|
|
45
|
+
return this.find(refEl, selector)[0] ?? null;
|
|
62
46
|
},
|
|
63
47
|
/**
|
|
64
|
-
* @param {Element|Document
|
|
65
|
-
* @param {string}
|
|
66
|
-
* @returns {Element
|
|
48
|
+
* @param {Element|Document} refEl
|
|
49
|
+
* @param {string|Element|NodeList|Array<Element>} selector
|
|
50
|
+
* @returns {Array<Element>}
|
|
67
51
|
*/
|
|
68
|
-
|
|
69
|
-
if (
|
|
52
|
+
find(refEl, selector) {
|
|
53
|
+
if (undefined === selector) {
|
|
70
54
|
selector = refEl;
|
|
71
55
|
refEl = document;
|
|
72
56
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
57
|
+
if (selector instanceof Element) {
|
|
58
|
+
selector = [selector];
|
|
59
|
+
}
|
|
60
|
+
if (isArrayLike(selector)) {
|
|
61
|
+
return map(Array.from(selector), (i, el) => {
|
|
62
|
+
if (el instanceof Element) {
|
|
63
|
+
return refEl === el || refEl.contains(el) ? el : null;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
});
|
|
77
67
|
}
|
|
68
|
+
return Array.from(refEl.querySelectorAll(selector));
|
|
78
69
|
},
|
|
79
70
|
/**
|
|
80
|
-
* @param {Element} el
|
|
71
|
+
* @param {Element|NodeList|Array<Element>} el
|
|
81
72
|
* @param {string} className
|
|
82
|
-
* @returns {Element}
|
|
73
|
+
* @returns {Element|NodeList|Array<Element>}
|
|
83
74
|
*/
|
|
84
75
|
addClass(el, className) {
|
|
85
76
|
if (!className) return el;
|
|
86
77
|
const classNames = className.split(' ').map(c => c.trim()).filter(Boolean);
|
|
87
|
-
el.
|
|
78
|
+
const elements = el instanceof Element ? [el] : Array.from(el);
|
|
79
|
+
elements.forEach(e => {
|
|
80
|
+
if (e instanceof Element) {
|
|
81
|
+
e.classList.add(...classNames);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
88
84
|
return el;
|
|
89
85
|
},
|
|
90
86
|
/**
|
|
91
|
-
* @param {Element} el
|
|
92
|
-
* @param {string}
|
|
93
|
-
* @returns {Element}
|
|
87
|
+
* @param {Element|NodeList|Array<Element>} el
|
|
88
|
+
* @param {string} className
|
|
89
|
+
* @returns {Element|NodeList|Array<Element>}
|
|
94
90
|
*/
|
|
95
|
-
removeClass(el,
|
|
96
|
-
if (!
|
|
97
|
-
|
|
91
|
+
removeClass(el, className) {
|
|
92
|
+
if (!className) return;
|
|
93
|
+
const classNames = className.split(' ').map(c => c.trim()).filter(Boolean);
|
|
94
|
+
const elements = el instanceof Element ? [el] : Array.from(el);
|
|
95
|
+
elements.forEach(e => {
|
|
96
|
+
if (e instanceof Element) {
|
|
97
|
+
e.classList.remove(...classNames);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
98
100
|
return el;
|
|
99
101
|
},
|
|
100
102
|
/**
|
|
@@ -104,7 +106,7 @@ var _default = exports.default = {
|
|
|
104
106
|
* @returns {Element}
|
|
105
107
|
*/
|
|
106
108
|
toggleClass(el, classNames, force) {
|
|
107
|
-
(
|
|
109
|
+
foreach(classNames.split(' ').map(c => c.trim()).filter(Boolean), c => el.classList.toggle(c, force));
|
|
108
110
|
return el;
|
|
109
111
|
},
|
|
110
112
|
/**
|
|
@@ -113,39 +115,69 @@ var _default = exports.default = {
|
|
|
113
115
|
* @returns {boolean}
|
|
114
116
|
*/
|
|
115
117
|
hasClass(el, classNames) {
|
|
116
|
-
|
|
118
|
+
if (!classNames) return false;
|
|
119
|
+
let foundClasses = true;
|
|
120
|
+
foreach(classNames.split(' ').map(c => c.trim()).filter(Boolean), c => {
|
|
121
|
+
if (el.classList.contains(c)) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
foundClasses = false;
|
|
125
|
+
return false;
|
|
126
|
+
});
|
|
127
|
+
return foundClasses;
|
|
117
128
|
},
|
|
118
129
|
/**
|
|
119
|
-
* @param {
|
|
120
|
-
* @param {
|
|
121
|
-
* @returns {
|
|
130
|
+
* @param {Node} node
|
|
131
|
+
* @param {...Node} children
|
|
132
|
+
* @returns {Node}
|
|
122
133
|
*/
|
|
123
|
-
append(
|
|
124
|
-
|
|
125
|
-
return
|
|
134
|
+
append(node, ...children) {
|
|
135
|
+
node.append(...children);
|
|
136
|
+
return node;
|
|
126
137
|
},
|
|
127
138
|
/**
|
|
128
|
-
* @param {
|
|
129
|
-
* @param {
|
|
130
|
-
* @returns {
|
|
139
|
+
* @param {Node} node
|
|
140
|
+
* @param {...Node} children
|
|
141
|
+
* @returns {Node}
|
|
131
142
|
*/
|
|
132
|
-
prepend(
|
|
133
|
-
|
|
134
|
-
return
|
|
143
|
+
prepend(node, ...children) {
|
|
144
|
+
node.prepend(...children);
|
|
145
|
+
return node;
|
|
135
146
|
},
|
|
136
147
|
/**
|
|
137
|
-
* @param {Element}
|
|
148
|
+
* @param {Element|NodeList|Array<Element>|string} els
|
|
138
149
|
* @returns {void}
|
|
139
150
|
*/
|
|
140
|
-
remove(
|
|
141
|
-
|
|
151
|
+
remove(...els) {
|
|
152
|
+
els.forEach(el => {
|
|
153
|
+
if (el instanceof Element) {
|
|
154
|
+
el.remove();
|
|
155
|
+
} else if (el instanceof NodeList || isArray(el)) {
|
|
156
|
+
Array.from(el).forEach(e => e.remove());
|
|
157
|
+
} else {
|
|
158
|
+
this.remove(this.find(el));
|
|
159
|
+
}
|
|
160
|
+
});
|
|
142
161
|
},
|
|
143
162
|
/**
|
|
144
163
|
* @param {Element} el
|
|
145
|
-
* @param {string}
|
|
164
|
+
* @param {string|Element} selector
|
|
146
165
|
* @returns {Element|null}
|
|
147
166
|
*/
|
|
148
167
|
closest(el, selector) {
|
|
168
|
+
if (selector instanceof Element) {
|
|
169
|
+
if (el === selector) {
|
|
170
|
+
return el;
|
|
171
|
+
}
|
|
172
|
+
let parentEl = el.parentElement;
|
|
173
|
+
while (parentEl) {
|
|
174
|
+
if (parentEl === selector) {
|
|
175
|
+
return parentEl;
|
|
176
|
+
}
|
|
177
|
+
parentEl = parentEl.parentElement;
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
149
181
|
return el.closest(selector);
|
|
150
182
|
},
|
|
151
183
|
/**
|
|
@@ -159,7 +191,7 @@ var _default = exports.default = {
|
|
|
159
191
|
if (sibling && sibling.matches(selector)) {
|
|
160
192
|
return sibling;
|
|
161
193
|
}
|
|
162
|
-
return
|
|
194
|
+
return null;
|
|
163
195
|
},
|
|
164
196
|
/**
|
|
165
197
|
* @param {Element} el
|
|
@@ -206,6 +238,46 @@ var _default = exports.default = {
|
|
|
206
238
|
}
|
|
207
239
|
return siblings;
|
|
208
240
|
},
|
|
241
|
+
/**
|
|
242
|
+
* @param {Element} el
|
|
243
|
+
* @param {Element|string} selector
|
|
244
|
+
* @returns {Element[]}
|
|
245
|
+
*/
|
|
246
|
+
nextUntil(el, selector) {
|
|
247
|
+
let selectorIsElement = false;
|
|
248
|
+
const list = [];
|
|
249
|
+
if (selector instanceof Element) {
|
|
250
|
+
selectorIsElement = true;
|
|
251
|
+
}
|
|
252
|
+
let nextSibling = el.nextElementSibling;
|
|
253
|
+
while (nextSibling) {
|
|
254
|
+
const found = selectorIsElement ? nextSibling === selector : nextSibling.matches(selector);
|
|
255
|
+
if (found) break;
|
|
256
|
+
list.push(nextSibling);
|
|
257
|
+
nextSibling = nextSibling.nextElementSibling;
|
|
258
|
+
}
|
|
259
|
+
return list;
|
|
260
|
+
},
|
|
261
|
+
/**
|
|
262
|
+
* @param {Element} el
|
|
263
|
+
* @param {Element|string} selector
|
|
264
|
+
* @returns {Element[]}
|
|
265
|
+
*/
|
|
266
|
+
prevUntil(el, selector) {
|
|
267
|
+
let selectorIsElement = false;
|
|
268
|
+
const list = [];
|
|
269
|
+
if (selector instanceof Element) {
|
|
270
|
+
selectorIsElement = true;
|
|
271
|
+
}
|
|
272
|
+
let prevSibling = el.previousElementSibling;
|
|
273
|
+
while (prevSibling) {
|
|
274
|
+
const found = selectorIsElement ? prevSibling === selector : prevSibling.matches(selector);
|
|
275
|
+
if (found) break;
|
|
276
|
+
list.push(prevSibling);
|
|
277
|
+
prevSibling = prevSibling.previousElementSibling;
|
|
278
|
+
}
|
|
279
|
+
return list;
|
|
280
|
+
},
|
|
209
281
|
/**
|
|
210
282
|
* @param {Element} el
|
|
211
283
|
* @param {Element} wrappingElement
|
|
@@ -303,17 +375,23 @@ var _default = exports.default = {
|
|
|
303
375
|
* @param {Element} el
|
|
304
376
|
* @param {Object<string, string>|string} name
|
|
305
377
|
* @param {string} [value]
|
|
306
|
-
* @returns {Element}
|
|
378
|
+
* @returns {Element|DOMStringMap}
|
|
307
379
|
*/
|
|
308
380
|
data(el, name, value) {
|
|
309
|
-
if (
|
|
310
|
-
|
|
381
|
+
if (undefined === name && undefined === value) {
|
|
382
|
+
return el.dataset;
|
|
383
|
+
}
|
|
384
|
+
if (webf.isPlainObject(name)) {
|
|
385
|
+
webf.each(name, (k, v) => this.data(el, k, v));
|
|
311
386
|
return el;
|
|
312
387
|
}
|
|
313
388
|
const isAttr = /^data-/.test(name + '');
|
|
314
|
-
const key =
|
|
389
|
+
const key = camelCase(isAttr ? (name + '').replace(/^data-/, '') : name + '');
|
|
315
390
|
if (undefined === value) return el.dataset[key];
|
|
316
|
-
if (null === value)
|
|
391
|
+
if (null === value) {
|
|
392
|
+
this.removeData(el, key);
|
|
393
|
+
return el;
|
|
394
|
+
}
|
|
317
395
|
el.dataset[key] = value;
|
|
318
396
|
return el;
|
|
319
397
|
},
|
|
@@ -323,7 +401,7 @@ var _default = exports.default = {
|
|
|
323
401
|
* @returns {Element|*}
|
|
324
402
|
*/
|
|
325
403
|
removeData(el, name) {
|
|
326
|
-
const key = (name + '').replace(/^data-/, '')
|
|
404
|
+
const key = camelCase((name + '').replace(/^data-/, ''));
|
|
327
405
|
delete el.dataset[key];
|
|
328
406
|
return el;
|
|
329
407
|
},
|
|
@@ -351,29 +429,27 @@ var _default = exports.default = {
|
|
|
351
429
|
},
|
|
352
430
|
/**
|
|
353
431
|
* @param {HTMLElement} el
|
|
354
|
-
* @param {Object<string, string>|string}
|
|
432
|
+
* @param {Object<string, string>|string} style
|
|
355
433
|
* @param {string} [value]
|
|
356
434
|
* @returns {Element}
|
|
357
435
|
*/
|
|
358
|
-
css(el,
|
|
359
|
-
if (
|
|
436
|
+
css(el, style, value) {
|
|
437
|
+
if (isString(style)) {
|
|
438
|
+
const prop = style.startsWith('--') ? style : camelCase(style);
|
|
360
439
|
if (undefined === value) {
|
|
361
|
-
return
|
|
440
|
+
return getStyle(el, prop);
|
|
362
441
|
}
|
|
363
|
-
if (
|
|
364
|
-
el.style.setProperty(
|
|
442
|
+
if (prop.startsWith('--')) {
|
|
443
|
+
el.style.setProperty(prop, String(value));
|
|
365
444
|
} else {
|
|
366
|
-
|
|
445
|
+
if (typeof value === "number" && !inArray(prop, cssNumber)) value += 'px';
|
|
446
|
+
el.style[prop] = value;
|
|
367
447
|
}
|
|
368
|
-
|
|
448
|
+
} else {
|
|
449
|
+
each(style, (name, v) => {
|
|
450
|
+
this.css(el, name, v);
|
|
451
|
+
});
|
|
369
452
|
}
|
|
370
|
-
(0, _traversal.each)(styles, (name, v) => {
|
|
371
|
-
if (name.startsWith('--')) {
|
|
372
|
-
el.style.setProperty(name, String(v));
|
|
373
|
-
} else {
|
|
374
|
-
el.style[name] = v;
|
|
375
|
-
}
|
|
376
|
-
});
|
|
377
453
|
return el;
|
|
378
454
|
},
|
|
379
455
|
/**
|
|
@@ -410,20 +486,21 @@ var _default = exports.default = {
|
|
|
410
486
|
return nodeList?.length ? nodeList.item(0) : null;
|
|
411
487
|
},
|
|
412
488
|
/**
|
|
413
|
-
* @param {NodeList} nodeList
|
|
489
|
+
* @param {NodeList|Array<Element>} nodeList
|
|
414
490
|
* @returns {Element|null}
|
|
415
491
|
*/
|
|
416
492
|
last(nodeList) {
|
|
417
|
-
|
|
493
|
+
const arr = Array.from(nodeList)[0];
|
|
494
|
+
return arr[arr.length - 1];
|
|
418
495
|
},
|
|
419
496
|
/**
|
|
420
497
|
* @param {string} html
|
|
421
|
-
* @returns {Element}
|
|
498
|
+
* @returns {Element|null}
|
|
422
499
|
*/
|
|
423
500
|
create(html) {
|
|
424
501
|
const tpl = document.createElement('template');
|
|
425
502
|
tpl.innerHTML = html.trim();
|
|
426
|
-
return tpl.content.firstElementChild;
|
|
503
|
+
return tpl.content.firstElementChild ?? null;
|
|
427
504
|
},
|
|
428
505
|
/**
|
|
429
506
|
* @param {NodeList} nodeList
|
|
@@ -462,5 +539,18 @@ var _default = exports.default = {
|
|
|
462
539
|
el.removeChild(el.firstChild);
|
|
463
540
|
}
|
|
464
541
|
return el;
|
|
542
|
+
},
|
|
543
|
+
/**
|
|
544
|
+
* @param {Element|NodeList} el
|
|
545
|
+
* @param {string|Element} selector
|
|
546
|
+
* @return {Element[]}
|
|
547
|
+
*/
|
|
548
|
+
not(el, selector) {
|
|
549
|
+
const elements = el instanceof Element ? [el] : Array.from(el);
|
|
550
|
+
const selectorIsString = webf.isString(selector);
|
|
551
|
+
return elements.filter(e => {
|
|
552
|
+
if (!(e instanceof Element)) return false;
|
|
553
|
+
return selectorIsString ? !e.matches(selector) : e !== selector;
|
|
554
|
+
});
|
|
465
555
|
}
|
|
466
556
|
};
|
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _is = require("./is.js");
|
|
8
|
-
var _traversal = require("./traversal.js");
|
|
1
|
+
import { isFunction, isString, isUndefined } from "./is.js";
|
|
2
|
+
import { each, map } from "./traversal.js";
|
|
9
3
|
class EventDispatcher {
|
|
10
4
|
#listeners = {};
|
|
11
5
|
addListener(eventsName, callback, context, ...args) {
|
|
12
|
-
if (!
|
|
6
|
+
if (!isFunction(callback)) {
|
|
13
7
|
throw new Error('Callback must be a function.');
|
|
14
8
|
}
|
|
15
|
-
if (!
|
|
9
|
+
if (!isString(eventsName)) {
|
|
16
10
|
throw new Error('Events name must be a string separated by comma.');
|
|
17
11
|
}
|
|
18
12
|
const listener = {
|
|
@@ -20,7 +14,7 @@ class EventDispatcher {
|
|
|
20
14
|
context,
|
|
21
15
|
args
|
|
22
16
|
};
|
|
23
|
-
|
|
17
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
24
18
|
if (!eventName) return true; // continue
|
|
25
19
|
|
|
26
20
|
eventName = eventName.trim();
|
|
@@ -33,7 +27,7 @@ class EventDispatcher {
|
|
|
33
27
|
return this;
|
|
34
28
|
}
|
|
35
29
|
addListenerOnce(eventsName, callback, context, ...listenerArgs) {
|
|
36
|
-
|
|
30
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
37
31
|
eventName = eventName.trim();
|
|
38
32
|
if (!eventName) return true; // continue
|
|
39
33
|
|
|
@@ -46,10 +40,10 @@ class EventDispatcher {
|
|
|
46
40
|
return this;
|
|
47
41
|
}
|
|
48
42
|
dispatch(eventsName, ...args) {
|
|
49
|
-
if (!
|
|
43
|
+
if (!isString(eventsName)) {
|
|
50
44
|
throw new Error('Events name must be a string seperated by comma.');
|
|
51
45
|
}
|
|
52
|
-
|
|
46
|
+
each(eventsName.split(','), (i, eventName) => {
|
|
53
47
|
eventName = eventName.trim();
|
|
54
48
|
if (!eventName) return true; // continue
|
|
55
49
|
|
|
@@ -57,26 +51,26 @@ class EventDispatcher {
|
|
|
57
51
|
console.warn(`No listeners found for event: ${eventName}`);
|
|
58
52
|
return true; // continue
|
|
59
53
|
}
|
|
60
|
-
|
|
54
|
+
each(this.#listeners[eventName], (i, listener) => {
|
|
61
55
|
listener.callback.apply(listener.context, [eventName].concat(listener.args).concat(args));
|
|
62
56
|
});
|
|
63
57
|
});
|
|
64
58
|
return this;
|
|
65
59
|
}
|
|
66
60
|
hasListener(eventName, callback, context) {
|
|
67
|
-
if (
|
|
68
|
-
return !
|
|
61
|
+
if (isUndefined(callback)) {
|
|
62
|
+
return !isUndefined(this.#listeners[eventName]);
|
|
69
63
|
}
|
|
70
|
-
return !!
|
|
64
|
+
return !!map(this.#listeners[eventName], (i, listener) => {
|
|
71
65
|
return listener.callback === callback && listener.context === context ? true : null;
|
|
72
66
|
}).length;
|
|
73
67
|
}
|
|
74
68
|
removeListener(eventName, callback, context) {
|
|
75
69
|
if (this.hasListener(eventName, callback, context)) {
|
|
76
|
-
if (
|
|
70
|
+
if (isUndefined(callback)) {
|
|
77
71
|
this.#listeners[eventName].splice(0);
|
|
78
72
|
} else {
|
|
79
|
-
|
|
73
|
+
each(this.#listeners[eventName], i => {
|
|
80
74
|
this.#listeners[eventName].splice(i, 1);
|
|
81
75
|
delete this.#listeners[eventName];
|
|
82
76
|
return false; // break
|
|
@@ -93,4 +87,4 @@ class EventDispatcher {
|
|
|
93
87
|
}
|
|
94
88
|
}
|
|
95
89
|
const eventDispatcher = new EventDispatcher();
|
|
96
|
-
|
|
90
|
+
export default eventDispatcher;
|