uni-select-field 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/LICENSE +21 -0
- package/README.md +268 -0
- package/index.html +382 -0
- package/mjs/common-css.js +171 -0
- package/mjs/common-lib.js +1158 -0
- package/mjs/uni-css.js +586 -0
- package/mjs/wc-uni-select-field.js +496 -0
- package/mjs/wc-uni-select-field.min.js +258 -0
- package/package.json +23 -0
|
@@ -0,0 +1,1158 @@
|
|
|
1
|
+
export const _wcl = {};
|
|
2
|
+
|
|
3
|
+
let iidForConsoleLoading = '';
|
|
4
|
+
|
|
5
|
+
Object.defineProperties(_wcl, {
|
|
6
|
+
getDonutMask: {
|
|
7
|
+
configurable: true,
|
|
8
|
+
enumerable: true,
|
|
9
|
+
value: function({ canvasSize, size }) {
|
|
10
|
+
const cW = canvasSize;
|
|
11
|
+
const cH = canvasSize;
|
|
12
|
+
const r1 = cW / 2;
|
|
13
|
+
const r2 = (cW - size * 2) / 2;
|
|
14
|
+
const pointes = [
|
|
15
|
+
{
|
|
16
|
+
x: cW,
|
|
17
|
+
y: cH / 2
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
x: 0,
|
|
21
|
+
y: cH / 2
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
x: cW - size,
|
|
25
|
+
y: cH / 2
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
x: size,
|
|
29
|
+
y: cH / 2
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
return `path('M${pointes[0].x} ${pointes[0].y} A${r1} ${r1} 0 0 0 ${pointes[1].x} ${pointes[1].y} A${r1} ${r1} 0 0 0 ${pointes[0].x} ${pointes[0].y} L${pointes[2].x} ${pointes[2].y} A${r2} ${r2} 0 0 1 ${pointes[3].x} ${pointes[3].y} A${r2} ${r2} 0 0 1 ${pointes[2].x} ${pointes[2].y}Z')`;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
getUUID: {
|
|
37
|
+
configurable: true,
|
|
38
|
+
enumerable: true,
|
|
39
|
+
value: function() {
|
|
40
|
+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, (c) =>
|
|
41
|
+
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
getWCConfig: {
|
|
46
|
+
configurable: true,
|
|
47
|
+
enumerable: true,
|
|
48
|
+
value: async function(host) {
|
|
49
|
+
// gather web component's config from [remoteconfig] or inner script[type=application/json]
|
|
50
|
+
let error, config;
|
|
51
|
+
|
|
52
|
+
const remoteconfig = host.getAttribute('remoteconfig');
|
|
53
|
+
const script = host.querySelector(':scope > script[type="application/json"]');
|
|
54
|
+
|
|
55
|
+
if (remoteconfig) {
|
|
56
|
+
// fetch remote config once [remoteconfig] exist
|
|
57
|
+
try {
|
|
58
|
+
const configUrl = new URL(remoteconfig);
|
|
59
|
+
config = await fetch(configUrl.toString(), {
|
|
60
|
+
headers: {
|
|
61
|
+
'content-type': 'application/json'
|
|
62
|
+
},
|
|
63
|
+
method: 'GET',
|
|
64
|
+
mode: 'cors'
|
|
65
|
+
})
|
|
66
|
+
.then(
|
|
67
|
+
(response) => {
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
throw new Error('Network response was not OK');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return response.json();
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
.catch(
|
|
76
|
+
(err) => {
|
|
77
|
+
error = err.message;
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
} catch(err) {
|
|
82
|
+
error = err.message;
|
|
83
|
+
}
|
|
84
|
+
} else if (script) {
|
|
85
|
+
// apply inner script's config
|
|
86
|
+
try {
|
|
87
|
+
config = JSON.parse(script.textContent.replace(/\n/g, '').trim());
|
|
88
|
+
} catch(err) {
|
|
89
|
+
error = err.message;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { config, error };
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
numberFormat: {
|
|
97
|
+
configurable: true,
|
|
98
|
+
enumerable: true,
|
|
99
|
+
value: function(num) {
|
|
100
|
+
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
whenDefined: {
|
|
104
|
+
configurable: true,
|
|
105
|
+
enumerable: true,
|
|
106
|
+
value: function() {
|
|
107
|
+
/**
|
|
108
|
+
* Check lib ready
|
|
109
|
+
* @example
|
|
110
|
+
whenDefined('document.body', 'YT.Player').then(...)
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
const units = Array.from(arguments);
|
|
114
|
+
|
|
115
|
+
if (units.indexOf('document.body') === -1) {
|
|
116
|
+
units.push('document.body');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const promises = units.map(
|
|
120
|
+
(unit) => {
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
let c, iid;
|
|
123
|
+
const max = 10000;
|
|
124
|
+
|
|
125
|
+
c = 0;
|
|
126
|
+
iid = setInterval(() => {
|
|
127
|
+
let _root, parts;
|
|
128
|
+
c += 5;
|
|
129
|
+
if (c > max) {
|
|
130
|
+
clearInterval(iid);
|
|
131
|
+
return reject(new Error(`"${unit}" unit missing.`));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
_root = window;
|
|
135
|
+
parts = unit.split('.');
|
|
136
|
+
while (parts.length) {
|
|
137
|
+
const prop = parts.shift();
|
|
138
|
+
if (typeof _root[prop] === 'undefined') {
|
|
139
|
+
_root = null;
|
|
140
|
+
break;
|
|
141
|
+
} else {
|
|
142
|
+
_root = _root[prop];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (_root !== null && document.readyState && document.readyState !== 'loading') {
|
|
147
|
+
clearInterval(iid);
|
|
148
|
+
return resolve();
|
|
149
|
+
}
|
|
150
|
+
}, 5);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
return Promise.all(promises);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
loadScript: {
|
|
159
|
+
configurable: true,
|
|
160
|
+
enumerable: true,
|
|
161
|
+
value: function() {
|
|
162
|
+
/**
|
|
163
|
+
* Load script(s) to the document
|
|
164
|
+
* @example
|
|
165
|
+
loadScript('script-path')
|
|
166
|
+
|
|
167
|
+
loadScript('script-path1', 'script-path2')
|
|
168
|
+
*/
|
|
169
|
+
const currentScripts = Array.from(document.getElementsByTagName('script')).map(
|
|
170
|
+
(script) => script.src
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
Array.from(arguments).forEach(
|
|
174
|
+
(path) => {
|
|
175
|
+
if (currentScripts.includes(path)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const script = document.createElement('script');
|
|
180
|
+
document.head.appendChild(script);
|
|
181
|
+
script.async = true;
|
|
182
|
+
script.src = path;
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
camelCase: {
|
|
188
|
+
configurable: true,
|
|
189
|
+
enumerable: true,
|
|
190
|
+
value: function(str) {
|
|
191
|
+
return str.replace(/-([a-z])/ig,
|
|
192
|
+
function(all, letter) {
|
|
193
|
+
return letter.toUpperCase();
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
capitalize: {
|
|
199
|
+
configurable: true,
|
|
200
|
+
enumerable: true,
|
|
201
|
+
value: function(str) {
|
|
202
|
+
return str.replace(/^[a-z]{1}/,
|
|
203
|
+
function($1) {
|
|
204
|
+
return $1.toUpperCase();
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
checkVisibility: {
|
|
210
|
+
configurable: true,
|
|
211
|
+
enumerable: true,
|
|
212
|
+
value: function(element) {
|
|
213
|
+
let result = true;
|
|
214
|
+
|
|
215
|
+
if (element.checkVisibility) {
|
|
216
|
+
result = element.checkVisibility({ contentVisibilityAuto:true, opacityProperty:true, visibilityProperty:true });
|
|
217
|
+
} else if (element.computedStyleMap) {
|
|
218
|
+
const allComputedStyles = element.computedStyleMap();
|
|
219
|
+
const { value: display } = allComputedStyles.get('display');
|
|
220
|
+
const { value: opacity } = allComputedStyles.get('opacity');
|
|
221
|
+
const { value: visibility } = allComputedStyles.get('visibility');
|
|
222
|
+
const { value: contentVisibility } = allComputedStyles.get('content-visibility');
|
|
223
|
+
|
|
224
|
+
result =
|
|
225
|
+
opacity === 0 ||
|
|
226
|
+
['none', 'contents'].includes(display) ||
|
|
227
|
+
visibility === 'hidden' ||
|
|
228
|
+
contentVisibility === 'auto'
|
|
229
|
+
? false
|
|
230
|
+
: true;
|
|
231
|
+
} else if (window.getComputedStyle) {
|
|
232
|
+
const allComputedStyles = window.getComputedStyle(element);
|
|
233
|
+
const display = allComputedStyles.getPropertyValue('display');
|
|
234
|
+
const opacity = allComputedStyles.getPropertyValue('opacity');
|
|
235
|
+
const visibility = allComputedStyles.getPropertyValue('visibility');
|
|
236
|
+
const contentVisibility = allComputedStyles.getPropertyValue('content-visibility');
|
|
237
|
+
|
|
238
|
+
result =
|
|
239
|
+
+opacity === 0 ||
|
|
240
|
+
['none', 'contents'].includes(display) ||
|
|
241
|
+
visibility === 'hidden' ||
|
|
242
|
+
contentVisibility === 'auto'
|
|
243
|
+
? false
|
|
244
|
+
: true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
rgbToHex: {
|
|
251
|
+
configurable: true,
|
|
252
|
+
enumerable: true,
|
|
253
|
+
value: function(r, g, b) {
|
|
254
|
+
/*
|
|
255
|
+
* https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
|
256
|
+
* ex:
|
|
257
|
+
* rgbToHex(255, 0, 0)
|
|
258
|
+
*/
|
|
259
|
+
|
|
260
|
+
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
hexToRgb: {
|
|
264
|
+
configurable: true,
|
|
265
|
+
enumerable: true,
|
|
266
|
+
value: function(hex) {
|
|
267
|
+
/*
|
|
268
|
+
* https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
|
269
|
+
* ex:
|
|
270
|
+
* hexToRgb("#0033ff").g
|
|
271
|
+
* hexToRgb("#03f").g
|
|
272
|
+
*/
|
|
273
|
+
|
|
274
|
+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
|
275
|
+
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
|
|
276
|
+
return r + r + g + g + b + b;
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
280
|
+
return result ? {
|
|
281
|
+
r: parseInt(result[1], 16),
|
|
282
|
+
g: parseInt(result[2], 16),
|
|
283
|
+
b: parseInt(result[3], 16)
|
|
284
|
+
} : null;
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
getLuminosity: {
|
|
288
|
+
configurable: true,
|
|
289
|
+
enumerable: true,
|
|
290
|
+
value: function(hex) {
|
|
291
|
+
/*
|
|
292
|
+
* https://codepen.io/borlyjenkins/pen/dyPXjPr
|
|
293
|
+
* https://github.com/Qix-/color
|
|
294
|
+
* ex:
|
|
295
|
+
* getLuminosity("#0033ff")
|
|
296
|
+
* luminosity < 0.57 ? white-text : black-text
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
const { r, g, b } = this.hexToRgb(hex);
|
|
300
|
+
return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
toggleConsoleLoading: {
|
|
304
|
+
configurable: true,
|
|
305
|
+
enumerable: true,
|
|
306
|
+
value: (force = true) => {
|
|
307
|
+
clearInterval(iidForConsoleLoading);
|
|
308
|
+
|
|
309
|
+
if (force) {
|
|
310
|
+
const signs = ['|', '/', '-', '\\'];
|
|
311
|
+
const count = signs.length - 1;
|
|
312
|
+
let i = 0;
|
|
313
|
+
|
|
314
|
+
iidForConsoleLoading = setInterval(
|
|
315
|
+
() => {
|
|
316
|
+
i = (i > count) ? 0 : i;
|
|
317
|
+
console.clear();
|
|
318
|
+
console.log(signs[i]);
|
|
319
|
+
i++;
|
|
320
|
+
}
|
|
321
|
+
, 250);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return iidForConsoleLoading;
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
classToTagName: {
|
|
328
|
+
configurable: true,
|
|
329
|
+
enumerable: true,
|
|
330
|
+
value: function(str) {
|
|
331
|
+
return str.replace(/([A-Z])/g,
|
|
332
|
+
function(all, letter) {
|
|
333
|
+
return '-' + letter.toLowerCase();
|
|
334
|
+
}
|
|
335
|
+
).replace(/^-(.*)/, '$1');
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
isObject: {
|
|
339
|
+
configurable: true,
|
|
340
|
+
enumerable: true,
|
|
341
|
+
value: (value) => value && typeof value === 'object' && value.constructor === Object
|
|
342
|
+
},
|
|
343
|
+
isNumeric: {
|
|
344
|
+
configurable: true,
|
|
345
|
+
enumerable: true,
|
|
346
|
+
value: (value) => !isNaN(value - parseFloat(value))
|
|
347
|
+
},
|
|
348
|
+
isAPISupport: {
|
|
349
|
+
configurable: true,
|
|
350
|
+
enumerable: true,
|
|
351
|
+
value: function(apiName, element) {
|
|
352
|
+
let node, flag;
|
|
353
|
+
|
|
354
|
+
navigator.supports = navigator.supports || {};
|
|
355
|
+
navigator.supports.api = navigator.supports.api || {};
|
|
356
|
+
|
|
357
|
+
if (typeof navigator.supports.api[apiName] !== 'undefined') {
|
|
358
|
+
flag = navigator.supports.api[apiName];
|
|
359
|
+
} else {
|
|
360
|
+
if (element) {
|
|
361
|
+
node = (element.tagName) ? element.cloneNode(true) : element;
|
|
362
|
+
} else {
|
|
363
|
+
node = window;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
flag = ['', 'webkit', 'moz', 'o', 'ms'].find(
|
|
367
|
+
(key) => {
|
|
368
|
+
let s;
|
|
369
|
+
s = key + (key ? this.capitalize(apiName) : apiName);
|
|
370
|
+
return node[s];
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
flag = (flag !== undefined) ? true : false;
|
|
375
|
+
|
|
376
|
+
//localStorage
|
|
377
|
+
if (['localStorage', 'sessionStorage'].includes(apiName) && flag) {
|
|
378
|
+
try {
|
|
379
|
+
localStorage.setItem('isapisupport', 'dummy');
|
|
380
|
+
localStorage.removeItem('isapisupport');
|
|
381
|
+
} catch(err) {
|
|
382
|
+
flag = false;
|
|
383
|
+
}
|
|
384
|
+
}//end if
|
|
385
|
+
|
|
386
|
+
navigator.supports.api[apiName] = flag;
|
|
387
|
+
node = null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return flag;
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
isEventSupport: {
|
|
394
|
+
configurable: true,
|
|
395
|
+
enumerable: true,
|
|
396
|
+
value: function(eventName, element) {
|
|
397
|
+
/**
|
|
398
|
+
* reference:
|
|
399
|
+
* http://perfectionkills.com/detecting-event-support-without-browser-sniffing/
|
|
400
|
+
* @example
|
|
401
|
+
* isEventSupport('touchstart');
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
let node, flag;
|
|
405
|
+
|
|
406
|
+
navigator.supports = navigator.supports || {};
|
|
407
|
+
navigator.supports.event = navigator.supports.event || {};
|
|
408
|
+
|
|
409
|
+
if (typeof navigator.supports.event[eventName] !== 'undefined') {
|
|
410
|
+
flag = navigator.supports.event[eventName];
|
|
411
|
+
} else {
|
|
412
|
+
if (element) {
|
|
413
|
+
node = (element.tagName) ? element.cloneNode(true) : element;
|
|
414
|
+
} else {
|
|
415
|
+
node = document.createElement('div');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
flag = `on${eventName}` in node;
|
|
419
|
+
|
|
420
|
+
if (!flag && node.setAttribute) {
|
|
421
|
+
node.setAttribute(eventName, 'return;');
|
|
422
|
+
flag = typeof node[eventName] == 'function';
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
navigator.supports.event[eventName] = flag;
|
|
426
|
+
node = null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return flag;
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
isCSSPropertySupport: {
|
|
433
|
+
configurable: true,
|
|
434
|
+
enumerable: true,
|
|
435
|
+
value: function(property) {
|
|
436
|
+
/**
|
|
437
|
+
* @example
|
|
438
|
+
* isCSSPropertySupport('overscroll-behavior');
|
|
439
|
+
*
|
|
440
|
+
* otherwise, maybe we can try CSS.supports
|
|
441
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/CSS/supports
|
|
442
|
+
*/
|
|
443
|
+
|
|
444
|
+
let node, flag;
|
|
445
|
+
|
|
446
|
+
navigator.supports = navigator.supports || {};
|
|
447
|
+
navigator.supports.css = navigator.supports.css || {};
|
|
448
|
+
|
|
449
|
+
property = (/^-ms/.test(property)) ? ('ms' + this.camelCase(property.replace(/-ms/,''))) : this.camelCase(property);
|
|
450
|
+
|
|
451
|
+
if (typeof navigator.supports.css[property] !== 'undefined') {
|
|
452
|
+
flag = navigator.supports.css[property];
|
|
453
|
+
} else {
|
|
454
|
+
node = document.createElement('div');
|
|
455
|
+
flag = property in node.style;
|
|
456
|
+
navigator.supports.css[property] = flag;
|
|
457
|
+
node = null;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return flag;
|
|
461
|
+
}
|
|
462
|
+
},
|
|
463
|
+
isStaticImportSupport:{
|
|
464
|
+
configurable: true,
|
|
465
|
+
enumerable: true,
|
|
466
|
+
value: () => {
|
|
467
|
+
/*
|
|
468
|
+
* https://gist.github.com/ebidel/3201b36f59f26525eb606663f7b487d0
|
|
469
|
+
*/
|
|
470
|
+
navigator.supports = navigator.supports || {};
|
|
471
|
+
|
|
472
|
+
if (typeof navigator.supports.staticImport === 'undefined') {
|
|
473
|
+
const script = document.createElement('script');
|
|
474
|
+
navigator.supports.staticImport = 'noModule' in script;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return navigator.supports.staticImport;
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
isDynamicImportSupport:{
|
|
481
|
+
configurable: true,
|
|
482
|
+
enumerable: true,
|
|
483
|
+
value: () => {
|
|
484
|
+
/*
|
|
485
|
+
* https://gist.github.com/ebidel/3201b36f59f26525eb606663f7b487d0
|
|
486
|
+
*/
|
|
487
|
+
|
|
488
|
+
navigator.supports = navigator.supports || {};
|
|
489
|
+
|
|
490
|
+
if (typeof navigator.supports.dynamicImport === 'undefined') {
|
|
491
|
+
try {
|
|
492
|
+
new Function('import("")');
|
|
493
|
+
navigator.supports.dynamicImport = true;
|
|
494
|
+
} catch (err) {
|
|
495
|
+
navigator.supports.dynamicImport = false;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return navigator.supports.dynamicImport;
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
isPrefersColorSchemeSet: {
|
|
503
|
+
configurable: true,
|
|
504
|
+
enumerable: true,
|
|
505
|
+
get: () => {
|
|
506
|
+
return Array.from(document.styleSheets).some(
|
|
507
|
+
(styleSheet) => {
|
|
508
|
+
try {
|
|
509
|
+
return Array.from(styleSheet.rules).some(
|
|
510
|
+
(rule) => {
|
|
511
|
+
return rule.media && /prefers-color-scheme:.*dark/i.test(rule.conditionText);
|
|
512
|
+
}
|
|
513
|
+
);
|
|
514
|
+
} catch(err) {
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
isIOS: {
|
|
522
|
+
configurable: true,
|
|
523
|
+
enumerable: true,
|
|
524
|
+
get: () => !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)
|
|
525
|
+
},
|
|
526
|
+
isMobile: {
|
|
527
|
+
configurable: true,
|
|
528
|
+
enumerable: true,
|
|
529
|
+
value: () => navigator.userAgent.match(/(iPhone|iPod|iPad|Android|webOS|BlackBerry|IEMobile|Opera Mini)/i) || false
|
|
530
|
+
},
|
|
531
|
+
grabEventLock: {
|
|
532
|
+
configurable: true,
|
|
533
|
+
enumerable: true,
|
|
534
|
+
value: function() {
|
|
535
|
+
return navigator.eventLock || (function() {
|
|
536
|
+
this.addStylesheetRules('.scroll-lock', {
|
|
537
|
+
'overflow': 'hidden',
|
|
538
|
+
'pointer-events': 'none'
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
navigator.eventLock = (evt) => {
|
|
542
|
+
evt.preventDefault();
|
|
543
|
+
evt.stopImmediatePropagation();
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
return navigator.eventLock;
|
|
547
|
+
}).bind(this)();
|
|
548
|
+
}
|
|
549
|
+
},
|
|
550
|
+
scrollLock: {
|
|
551
|
+
configurable: true,
|
|
552
|
+
enumerable: true,
|
|
553
|
+
value: function(isLock = true, eventLock = this.grabEventLock()) {
|
|
554
|
+
isLock = Boolean(isLock);
|
|
555
|
+
if (isLock) {
|
|
556
|
+
document.body.classList.add('scroll-lock');
|
|
557
|
+
window.addEventListener('touchmove', eventLock, { passive: false });
|
|
558
|
+
} else {
|
|
559
|
+
document.body.classList.remove('scroll-lock');
|
|
560
|
+
window.removeEventListener('touchmove', eventLock);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
scrollX: {
|
|
565
|
+
configurable: true,
|
|
566
|
+
enumerable: true,
|
|
567
|
+
get: () => {
|
|
568
|
+
return _wcl.getScroll().x;
|
|
569
|
+
},
|
|
570
|
+
set: (x) => {
|
|
571
|
+
if (document.documentElement && document.documentElement.scrollLeft) {
|
|
572
|
+
document.documentElement.scrollLeft = x;
|
|
573
|
+
} else {
|
|
574
|
+
document.body.scrollLeft = x;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
scrollY: {
|
|
579
|
+
configurable: true,
|
|
580
|
+
enumerable: true,
|
|
581
|
+
get: () => {
|
|
582
|
+
return _wcl.getScroll().y;
|
|
583
|
+
},
|
|
584
|
+
set: (y) => {
|
|
585
|
+
if (document.documentElement && document.documentElement.scrollTop) {
|
|
586
|
+
document.documentElement.scrollTop = y;
|
|
587
|
+
} else {
|
|
588
|
+
document.body.scrollTop = y;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
rollTo: {
|
|
593
|
+
configurable: true,
|
|
594
|
+
enumerable: true,
|
|
595
|
+
value: function(y, offset = 0) {
|
|
596
|
+
/*
|
|
597
|
+
* y could be y-coord or DOM element
|
|
598
|
+
*/
|
|
599
|
+
const { height } = this.getPageSize();
|
|
600
|
+
const { height:winHeight } = this.getViewportSize();
|
|
601
|
+
|
|
602
|
+
if (typeof y.nodeType !== 'undefined' && y.nodeType === 1 && typeof y.getBoundingClientRect === 'function') {
|
|
603
|
+
y = this.getPosition(y).y;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
y += offset;
|
|
607
|
+
|
|
608
|
+
return new Promise((resolve) => {
|
|
609
|
+
let iid;
|
|
610
|
+
const scroll = () => {
|
|
611
|
+
let shift = Math.ceil((y - this.scrollY) * 0.15);
|
|
612
|
+
|
|
613
|
+
cancelAnimationFrame(iid);
|
|
614
|
+
scrollBy(0, shift);
|
|
615
|
+
|
|
616
|
+
if (this.scrollY < 0 || this.scrollY + winHeight >= height || shift === 0 || Math.abs(this.scrollY - y) <= Math.abs(shift)) {
|
|
617
|
+
this.scrollY = y;
|
|
618
|
+
resolve();
|
|
619
|
+
return;
|
|
620
|
+
} else {
|
|
621
|
+
iid = requestAnimationFrame(scroll);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
iid = requestAnimationFrame(scroll);
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
maxZIndex: {
|
|
630
|
+
configurable: true,
|
|
631
|
+
enumerable: true,
|
|
632
|
+
get: () => {
|
|
633
|
+
return Array.from(document.querySelectorAll('body *'))
|
|
634
|
+
.map((a) => parseFloat(window.getComputedStyle(a).zIndex))
|
|
635
|
+
.filter((a) => !isNaN(a))
|
|
636
|
+
.sort((a,b) => a - b)
|
|
637
|
+
.pop();
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
pointer: {
|
|
641
|
+
configurable: true,
|
|
642
|
+
enumerable: true,
|
|
643
|
+
value: function(e) {
|
|
644
|
+
let x, y, clientX, clientY, docElement, body;
|
|
645
|
+
|
|
646
|
+
docElement = document.documentElement;
|
|
647
|
+
|
|
648
|
+
if (e?.touches && e?.touches?.length > 0) {
|
|
649
|
+
clientX = e?.touches?.[0]?.clientX;
|
|
650
|
+
clientY = e?.touches?.[0]?.clientY;
|
|
651
|
+
} else {
|
|
652
|
+
clientX = e.clientX;
|
|
653
|
+
clientY = e.clientY;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
//x
|
|
657
|
+
body = document.body || { scrollLeft: 0 };
|
|
658
|
+
x = e?.pageX || (clientX + (docElement.scrollLeft || body.scrollLeft) - (docElement.clientLeft || 0));
|
|
659
|
+
|
|
660
|
+
//y
|
|
661
|
+
body = document.body || { scrollTop: 0 };
|
|
662
|
+
y = e?.pageY || (clientY + (docElement.scrollTop || body.scrollTop) - (docElement.clientTop || 0));
|
|
663
|
+
|
|
664
|
+
return { x, y };
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
pursuer: {
|
|
668
|
+
configurable: true,
|
|
669
|
+
enumerable: true,
|
|
670
|
+
value: function() {
|
|
671
|
+
let down, move, up;
|
|
672
|
+
|
|
673
|
+
if (this.isEventSupport('touchstart')) {
|
|
674
|
+
down = 'touchstart';
|
|
675
|
+
move = 'touchmove';
|
|
676
|
+
up = 'touchend';
|
|
677
|
+
} else if (typeof navigator.msPointerEnabled != 'undefined' && navigator.msPointerEnabled) {
|
|
678
|
+
down = 'MSPointerDown';
|
|
679
|
+
move = 'MSPointerMove';
|
|
680
|
+
up = 'MSPointerUp';
|
|
681
|
+
} else {
|
|
682
|
+
down = 'mousedown';
|
|
683
|
+
move = 'mousemove';
|
|
684
|
+
up = 'mouseup';
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
return {down, move, up};
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
purgeObject: {
|
|
691
|
+
configurable: true,
|
|
692
|
+
enumerable: true,
|
|
693
|
+
value: function(targetObject) {
|
|
694
|
+
if (this.isObject(targetObject)) {
|
|
695
|
+
Object.keys(targetObject).forEach(
|
|
696
|
+
(key) => {
|
|
697
|
+
let { [key]:prop } = targetObject;
|
|
698
|
+
|
|
699
|
+
if (Array.isArray(prop)) {
|
|
700
|
+
while (prop.length) {
|
|
701
|
+
prop.pop();
|
|
702
|
+
}// end while
|
|
703
|
+
}// end if
|
|
704
|
+
|
|
705
|
+
targetObject[key] = null;
|
|
706
|
+
}
|
|
707
|
+
);
|
|
708
|
+
}// end if
|
|
709
|
+
}
|
|
710
|
+
},
|
|
711
|
+
isPiPSupport: {
|
|
712
|
+
configurable: true,
|
|
713
|
+
enumerable: true,
|
|
714
|
+
value: function() {
|
|
715
|
+
navigator.supports = navigator.supports || {};
|
|
716
|
+
if (typeof navigator.supports.PiP === 'undefined') {
|
|
717
|
+
const node = document.createElement('video');
|
|
718
|
+
|
|
719
|
+
navigator.supports.PiP = this.isAPISupport('requestPictureInPicture', node) || (node.webkitSupportsPresentationMode && typeof node.webkitSetPresentationMode === 'function');
|
|
720
|
+
}
|
|
721
|
+
return navigator.supports.PiP;
|
|
722
|
+
}
|
|
723
|
+
},
|
|
724
|
+
isFullscreenSupport: {
|
|
725
|
+
configurable: true,
|
|
726
|
+
enumerable: true,
|
|
727
|
+
value: function() {
|
|
728
|
+
navigator.supports = navigator.supports || {};
|
|
729
|
+
if (!navigator.supports.fullscreen) {
|
|
730
|
+
const node = document.createElement('div');
|
|
731
|
+
let request = false;
|
|
732
|
+
let exit = false;
|
|
733
|
+
let element = '';
|
|
734
|
+
let event = '';
|
|
735
|
+
|
|
736
|
+
if (node.requestFullscreen) {
|
|
737
|
+
request = 'requestFullscreen';
|
|
738
|
+
exit = 'exitFullscreen';
|
|
739
|
+
element = 'fullscreenElement';
|
|
740
|
+
event = 'fullscreenchange';
|
|
741
|
+
// } else if (node.msRequestFullscreen) {
|
|
742
|
+
// request = 'msRequestFullscreen';
|
|
743
|
+
// exit = 'msExitFullscreen';
|
|
744
|
+
} else if (node.webkitRequestFullscreen) {
|
|
745
|
+
request = 'webkitRequestFullscreen';
|
|
746
|
+
exit = 'webkitExitFullscreen';
|
|
747
|
+
element = 'webkitFullscreenElement';
|
|
748
|
+
event = 'webkitfullscreenchange';
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
navigator.supports.fullscreen = {
|
|
752
|
+
request,
|
|
753
|
+
exit,
|
|
754
|
+
element,
|
|
755
|
+
event
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
return navigator.supports.fullscreen;
|
|
759
|
+
}
|
|
760
|
+
},
|
|
761
|
+
supports: {
|
|
762
|
+
configurable: true,
|
|
763
|
+
enumerable: true,
|
|
764
|
+
value: function() {
|
|
765
|
+
// let flag;
|
|
766
|
+
navigator.supports = navigator.supports || {};
|
|
767
|
+
if (!navigator.supports.wp) {
|
|
768
|
+
// flag = true;
|
|
769
|
+
// try {
|
|
770
|
+
// class DummyClass {}
|
|
771
|
+
// } catch (err) {
|
|
772
|
+
// flag = false;
|
|
773
|
+
// }
|
|
774
|
+
|
|
775
|
+
navigator.supports.wp = {
|
|
776
|
+
// classes: flag,
|
|
777
|
+
customElements: 'customElements' in window,
|
|
778
|
+
import: 'import' in document.createElement('link'),
|
|
779
|
+
shadowDOM: !!HTMLElement.prototype.attachShadow,
|
|
780
|
+
template: 'content' in document.createElement('template')
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
return navigator.supports.wp;
|
|
784
|
+
}
|
|
785
|
+
},
|
|
786
|
+
getScroll: {
|
|
787
|
+
configurable: true,
|
|
788
|
+
enumerable: true,
|
|
789
|
+
value: () => {
|
|
790
|
+
let x, y;
|
|
791
|
+
x = (self.pageXOffset) ? self.pageXOffset : (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
|
|
792
|
+
y = (self.pageYOffset) ? self.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
|
|
793
|
+
return { x, y };
|
|
794
|
+
}
|
|
795
|
+
},
|
|
796
|
+
getPosition: {
|
|
797
|
+
configurable: true,
|
|
798
|
+
enumerable: true,
|
|
799
|
+
value: (element) => {
|
|
800
|
+
let x, y;
|
|
801
|
+
x = 0;
|
|
802
|
+
y = 0;
|
|
803
|
+
while (element != null) {
|
|
804
|
+
x += element.offsetLeft;
|
|
805
|
+
y += element.offsetTop;
|
|
806
|
+
element = element.offsetParent;
|
|
807
|
+
}
|
|
808
|
+
return { x, y };
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
getPageSize: {
|
|
812
|
+
configurable: true,
|
|
813
|
+
enumerable: true,
|
|
814
|
+
value: function() {
|
|
815
|
+
let xScroll, yScroll, width, height;
|
|
816
|
+
const { width:winWidth, height:winHeight } = this.getViewportSize();
|
|
817
|
+
|
|
818
|
+
if (window.innerHeight && window.scrollMaxY) {
|
|
819
|
+
xScroll = document.body.scrollWidth;
|
|
820
|
+
yScroll = window.innerHeight + window.scrollMaxY;
|
|
821
|
+
} else if (document.body.scrollHeight > document.body.offsetHeight) {
|
|
822
|
+
xScroll = document.body.scrollWidth;
|
|
823
|
+
yScroll = document.body.scrollHeight;
|
|
824
|
+
} else {
|
|
825
|
+
xScroll = document.body.offsetWidth;
|
|
826
|
+
yScroll = document.body.offsetHeight;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
width = (xScroll < winWidth) ? winWidth : xScroll;
|
|
830
|
+
height = (yScroll < winHeight) ? winHeight : yScroll;
|
|
831
|
+
|
|
832
|
+
return { width, height };
|
|
833
|
+
}
|
|
834
|
+
},
|
|
835
|
+
getViewportSize: {
|
|
836
|
+
configurable: true,
|
|
837
|
+
enumerable: true,
|
|
838
|
+
value: () => {
|
|
839
|
+
let width, height;
|
|
840
|
+
|
|
841
|
+
if (self.innerHeight) {
|
|
842
|
+
width = self.innerWidth;
|
|
843
|
+
height = self.innerHeight;
|
|
844
|
+
} else if (document.documentElement && document.documentElement.clientHeight) {
|
|
845
|
+
width = document.documentElement.clientWidth;
|
|
846
|
+
height = document.documentElement.clientHeight;
|
|
847
|
+
} else if (document.body) {
|
|
848
|
+
width = document.body.clientWidth;
|
|
849
|
+
height = document.body.clientHeight;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
return { width, height };
|
|
853
|
+
}
|
|
854
|
+
},
|
|
855
|
+
getSize: {
|
|
856
|
+
configurable: true,
|
|
857
|
+
enumerable: true,
|
|
858
|
+
value: (element) => {
|
|
859
|
+
let width, height;
|
|
860
|
+
|
|
861
|
+
width = element.offsetWidth;
|
|
862
|
+
height = element.offsetHeight;
|
|
863
|
+
return { width, height };
|
|
864
|
+
}
|
|
865
|
+
},
|
|
866
|
+
getRand: {
|
|
867
|
+
configurable: true,
|
|
868
|
+
enumerable: true,
|
|
869
|
+
value: (a, b) => {
|
|
870
|
+
let min, max;
|
|
871
|
+
|
|
872
|
+
if (a > b) {
|
|
873
|
+
min = a;
|
|
874
|
+
max = b;
|
|
875
|
+
} else {
|
|
876
|
+
min = b;
|
|
877
|
+
max = a;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
881
|
+
}
|
|
882
|
+
},
|
|
883
|
+
getRandomIntInclusive: {
|
|
884
|
+
configurable: true,
|
|
885
|
+
enumerable: true,
|
|
886
|
+
value: (min, max) => {
|
|
887
|
+
min = Math.ceil(min);
|
|
888
|
+
max = Math.floor(max);
|
|
889
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
890
|
+
}
|
|
891
|
+
},
|
|
892
|
+
grabStyleSheet: {
|
|
893
|
+
configurable: true,
|
|
894
|
+
enumerable: true,
|
|
895
|
+
value: function() {
|
|
896
|
+
return navigator.customStyleSheet || (function() {
|
|
897
|
+
navigator.customStyleSheet = document.createElement('style');
|
|
898
|
+
document.head.appendChild(navigator.customStyleSheet);
|
|
899
|
+
return navigator.customStyleSheet;
|
|
900
|
+
})();
|
|
901
|
+
}
|
|
902
|
+
},
|
|
903
|
+
removeAllChildNodes: {
|
|
904
|
+
configurable: true,
|
|
905
|
+
enumerable: true,
|
|
906
|
+
value: function(parent) {
|
|
907
|
+
while (parent.firstChild) {
|
|
908
|
+
parent.removeChild(parent.firstChild);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
},
|
|
912
|
+
isScrollbarShow: {
|
|
913
|
+
configurable: true,
|
|
914
|
+
enumerable: true,
|
|
915
|
+
value: function() {
|
|
916
|
+
/* https://davidwalsh.name/detect-scrollbar-width */
|
|
917
|
+
const scrollDiv = document.createElement('div');
|
|
918
|
+
scrollDiv.style.cssText = 'inline-size:100px;block-size:100px;overflow:scroll;position:absolute;top:-9999px;';
|
|
919
|
+
document.body.appendChild(scrollDiv);
|
|
920
|
+
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
|
|
921
|
+
scrollDiv.remove();
|
|
922
|
+
|
|
923
|
+
return scrollbarWidth > 0;
|
|
924
|
+
}
|
|
925
|
+
},
|
|
926
|
+
|
|
927
|
+
isObjectsEqual: {
|
|
928
|
+
configurable: true,
|
|
929
|
+
enumerable: true,
|
|
930
|
+
value: function(obj1, obj2) {
|
|
931
|
+
const obj1Keys = Object.keys(obj1).sort();
|
|
932
|
+
const obj2Keys = Object.keys(obj2).sort();
|
|
933
|
+
let objEqual = false;
|
|
934
|
+
|
|
935
|
+
if (obj1Keys.length !== obj2Keys.length) {
|
|
936
|
+
objEqual = false;
|
|
937
|
+
} else {
|
|
938
|
+
objEqual = obj1Keys.every((key, index) => {
|
|
939
|
+
const objValue1 = obj1[key];
|
|
940
|
+
const objValue2 = obj2[obj2Keys[index]];
|
|
941
|
+
return objValue1 === objValue2;
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
return objEqual;
|
|
946
|
+
}
|
|
947
|
+
},
|
|
948
|
+
|
|
949
|
+
isValidURL: {
|
|
950
|
+
configurable: true,
|
|
951
|
+
enumerable: true,
|
|
952
|
+
value: function(url) {
|
|
953
|
+
try {
|
|
954
|
+
return Boolean(new URL(url));
|
|
955
|
+
}
|
|
956
|
+
catch(e){
|
|
957
|
+
return false;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
},
|
|
961
|
+
|
|
962
|
+
loadImage: {
|
|
963
|
+
configurable: true,
|
|
964
|
+
enumerable: true,
|
|
965
|
+
value: function(url, crossOrigin) {
|
|
966
|
+
return new Promise(
|
|
967
|
+
(resolve, reject) => {
|
|
968
|
+
const img = new Image();
|
|
969
|
+
|
|
970
|
+
img.onload = () => {
|
|
971
|
+
resolve(img);
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
img.onerror = (e) => {
|
|
975
|
+
reject(e);
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
if (typeof crossOrigin !== 'undefined') {
|
|
979
|
+
img.crossOrigin = crossOrigin;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
img.src = url;
|
|
983
|
+
}
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
},
|
|
987
|
+
|
|
988
|
+
prepareFetch: {
|
|
989
|
+
configurable: true,
|
|
990
|
+
enumerable: true,
|
|
991
|
+
value: function(timeout = 5000) {
|
|
992
|
+
const fetchController = new AbortController();
|
|
993
|
+
const signal = fetchController.signal;
|
|
994
|
+
|
|
995
|
+
// timeout
|
|
996
|
+
setTimeout(() => fetchController.abort(), timeout);
|
|
997
|
+
|
|
998
|
+
return signal;
|
|
999
|
+
}
|
|
1000
|
+
},
|
|
1001
|
+
|
|
1002
|
+
cloneStyleSheetsToDocument: {
|
|
1003
|
+
configurable: true,
|
|
1004
|
+
enumerable: true,
|
|
1005
|
+
value: function(targetDocument) {
|
|
1006
|
+
[...document.styleSheets].forEach((styleSheet) => {
|
|
1007
|
+
try {
|
|
1008
|
+
const cssRules = [...styleSheet.cssRules].map((rule) => rule.cssText).join('');
|
|
1009
|
+
const style = document.createElement('style');
|
|
1010
|
+
|
|
1011
|
+
style.textContent = cssRules;
|
|
1012
|
+
targetDocument.head.appendChild(style);
|
|
1013
|
+
} catch (e) {
|
|
1014
|
+
const link = document.createElement('link');
|
|
1015
|
+
|
|
1016
|
+
link.rel = 'stylesheet';
|
|
1017
|
+
link.type = styleSheet.type;
|
|
1018
|
+
link.media = styleSheet.media;
|
|
1019
|
+
link.href = styleSheet.href;
|
|
1020
|
+
targetDocument.head.appendChild(link);
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
},
|
|
1025
|
+
|
|
1026
|
+
addStylesheetRules: {
|
|
1027
|
+
configurable: true,
|
|
1028
|
+
enumerable: true,
|
|
1029
|
+
value: function(selector = '', props = {}, styleSheet = this.grabStyleSheet()) {
|
|
1030
|
+
/**
|
|
1031
|
+
* Add a stylesheet rule to the document
|
|
1032
|
+
* @example
|
|
1033
|
+
addStylesheetRules(
|
|
1034
|
+
'body',
|
|
1035
|
+
{
|
|
1036
|
+
background: '#f00',
|
|
1037
|
+
color: '#0f0'
|
|
1038
|
+
}
|
|
1039
|
+
[, styleSheet]
|
|
1040
|
+
)
|
|
1041
|
+
|
|
1042
|
+
addStylesheetRules(
|
|
1043
|
+
'@keyframes fancy-anchor-ripple',
|
|
1044
|
+
{
|
|
1045
|
+
'0%': '{transform:scale(1);opacity:1;}',
|
|
1046
|
+
'100%': '{transform:scale(100);opacity:0;}'
|
|
1047
|
+
}
|
|
1048
|
+
[, styleSheet]
|
|
1049
|
+
)
|
|
1050
|
+
*/
|
|
1051
|
+
if (!selector || !Object.keys(props).length || !styleSheet.sheet) return;
|
|
1052
|
+
|
|
1053
|
+
let propStr, findIndex, sign;
|
|
1054
|
+
|
|
1055
|
+
sign = (/keyframes/i.test(selector)) ? '' : ';';
|
|
1056
|
+
styleSheet = styleSheet.sheet;
|
|
1057
|
+
propStr = Object.keys(props).reduce(
|
|
1058
|
+
(acc, cur) => {
|
|
1059
|
+
let sign;
|
|
1060
|
+
|
|
1061
|
+
sign = /^\{.*\}$/.test(props[cur]) ? '' : ':';
|
|
1062
|
+
return acc.concat([`${cur}${sign}${props[cur]}`]);
|
|
1063
|
+
}
|
|
1064
|
+
, []).join(sign);
|
|
1065
|
+
findIndex = Array.from(styleSheet.cssRules).findIndex((rule) => rule.selectorText == selector);
|
|
1066
|
+
|
|
1067
|
+
if (findIndex !== -1) {
|
|
1068
|
+
try {
|
|
1069
|
+
styleSheet.cssRules[findIndex].style.cssText = propStr;
|
|
1070
|
+
} catch(err) { /*error*/ }
|
|
1071
|
+
} else {
|
|
1072
|
+
try {
|
|
1073
|
+
styleSheet.insertRule(`${selector}{${propStr}}`, styleSheet.cssRules.length);
|
|
1074
|
+
} catch(err) { /*error*/ }
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
// addStylesheetRules: {
|
|
1079
|
+
// configurable: true,
|
|
1080
|
+
// enumerable: true,
|
|
1081
|
+
// value: function(selector = '', props = {}, ...others) {
|
|
1082
|
+
// /**
|
|
1083
|
+
// * Add a stylesheet rule to the document
|
|
1084
|
+
// * @example
|
|
1085
|
+
// addStylesheetRules(
|
|
1086
|
+
// 'body',
|
|
1087
|
+
// {
|
|
1088
|
+
// background: '#f00',
|
|
1089
|
+
// color: '#0f0'
|
|
1090
|
+
// }
|
|
1091
|
+
// [, styleSheet]
|
|
1092
|
+
// )
|
|
1093
|
+
|
|
1094
|
+
// addStylesheetRules(
|
|
1095
|
+
// '@keyframes fancy-anchor-ripple',
|
|
1096
|
+
// {
|
|
1097
|
+
// '0%': '{transform:scale(1);opacity:1;}',
|
|
1098
|
+
// '100%': '{transform:scale(100);opacity:0;}'
|
|
1099
|
+
// }
|
|
1100
|
+
// [, styleSheet]
|
|
1101
|
+
// )
|
|
1102
|
+
|
|
1103
|
+
// addStylesheetRules(
|
|
1104
|
+
// 'body',
|
|
1105
|
+
// {
|
|
1106
|
+
// background: '#f00',
|
|
1107
|
+
// color: '#0f0'
|
|
1108
|
+
// }
|
|
1109
|
+
// 'components.heros'
|
|
1110
|
+
// [, styleSheet]
|
|
1111
|
+
// )
|
|
1112
|
+
// */
|
|
1113
|
+
|
|
1114
|
+
// const [
|
|
1115
|
+
// layerName = undefined,
|
|
1116
|
+
// styleSheet = this.grabStyleSheet()
|
|
1117
|
+
// ] = others;
|
|
1118
|
+
|
|
1119
|
+
// const sheet = styleSheet.sheet;
|
|
1120
|
+
// const propStr = Object.keys(props).reduce(
|
|
1121
|
+
// (acc, cur) => {
|
|
1122
|
+
// let sign;
|
|
1123
|
+
|
|
1124
|
+
// sign = /^\{.*\}$/.test(props[cur]) ? '' : ':';
|
|
1125
|
+
// return acc.concat([`${cur}${sign}${props[cur]}`]);
|
|
1126
|
+
// }
|
|
1127
|
+
// , []).join((/keyframes/i.test(selector)) ? '' : ';');
|
|
1128
|
+
|
|
1129
|
+
// if (layerName) {
|
|
1130
|
+
// // add rules to layer
|
|
1131
|
+
// const layer = Array.from(sheet.cssRules).find((rule) => rule?.name === layerName);
|
|
1132
|
+
|
|
1133
|
+
// if (layer !== undefined) {
|
|
1134
|
+
// try {
|
|
1135
|
+
// const idx = Array.from(layer.cssRules).findIndex((rule) => rule.selectorText === selector);
|
|
1136
|
+
// layer.cssRules[idx].style.cssText = propStr;
|
|
1137
|
+
// } catch(err) { /*error*/ }
|
|
1138
|
+
// } else {
|
|
1139
|
+
// try {
|
|
1140
|
+
// sheet.insertRule(`@layer ${layerName}{${selector}{${propStr}}}`, sheet.cssRules.length);
|
|
1141
|
+
// } catch(err) { /*error*/ }
|
|
1142
|
+
// }
|
|
1143
|
+
// } else {
|
|
1144
|
+
// const idx = Array.from(sheet.cssRules).findIndex((rule) => rule.selectorText === selector);
|
|
1145
|
+
|
|
1146
|
+
// if (idx !== -1) {
|
|
1147
|
+
// try {
|
|
1148
|
+
// sheet.cssRules[idx].style.cssText = propStr;
|
|
1149
|
+
// } catch(err) { /*error*/ }
|
|
1150
|
+
// } else {
|
|
1151
|
+
// try {
|
|
1152
|
+
// sheet.insertRule(`${selector}{${propStr}}`, sheet.cssRules.length);
|
|
1153
|
+
// } catch(err) { /*error*/ }
|
|
1154
|
+
// }
|
|
1155
|
+
// }
|
|
1156
|
+
// }
|
|
1157
|
+
// }
|
|
1158
|
+
});
|