@drumee/ui-toolkit 0.0.10 → 0.0.12
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/package.json +1 -1
- package/skin/index.scss +6 -0
- package/utils/contextmenu.js +27 -0
- package/utils/index.js +702 -0
- package/utils/validator.js +61 -0
package/package.json
CHANGED
package/skin/index.scss
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
|
|
2
|
+
export function buildContextmenu(p, trigger, e) {
|
|
3
|
+
let kids;
|
|
4
|
+
if (_.isFunction(p.contextmenuSkeleton)) {
|
|
5
|
+
kids = p.contextmenuSkeleton(p, trigger, e);
|
|
6
|
+
} else if (_.isArray(p.contextmenuSkeleton)) {
|
|
7
|
+
kids = p.contextmenuSkeleton;
|
|
8
|
+
} else if (_.isObject(p.contextmenuSkeleton)) {
|
|
9
|
+
kids = [p.contextmenuSkeleton];
|
|
10
|
+
}
|
|
11
|
+
if (_.isString(kids) || _.isEmpty(kids)) {
|
|
12
|
+
return Skeletons.Box.Y({ volatility: 1, style: { display: _a.none } });
|
|
13
|
+
} else if (!_.isArray(kids)) {
|
|
14
|
+
kids = [kids];
|
|
15
|
+
}
|
|
16
|
+
return Skeletons.Box.Y({
|
|
17
|
+
volatility: 4,
|
|
18
|
+
className: `drumee-contextmenu ${p.fig.family}`,
|
|
19
|
+
kids,
|
|
20
|
+
uiHandler: [p],
|
|
21
|
+
style: {
|
|
22
|
+
left: e.pageX,
|
|
23
|
+
top: e.pageY,
|
|
24
|
+
zIndex: 100000
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|
package/utils/index.js
ADDED
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
const { Autolinker } = require("autolinker");
|
|
2
|
+
const Filesize = require("filesize");
|
|
3
|
+
|
|
4
|
+
let VERBOSITY = parseInt(localStorage.logLevel) || 0;
|
|
5
|
+
const { LOG_LEVEL, BOOLEAN, STATE, RADIO } = require("./constants")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param {*} name
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export function log(name) {
|
|
15
|
+
if (!name) return VERBOSITY;
|
|
16
|
+
return VERBOSITY >= LOG_LEVEL[name];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param {*} c
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
export function clarityToAlpha(c) {
|
|
27
|
+
let a = (100 - c) / 100;
|
|
28
|
+
if (a > 1) {
|
|
29
|
+
a = 1;
|
|
30
|
+
}
|
|
31
|
+
if (a < 0) {
|
|
32
|
+
a = 0;
|
|
33
|
+
}
|
|
34
|
+
return a;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
*
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
export function colon() {
|
|
42
|
+
switch (Visitor.language()) {
|
|
43
|
+
case "fr":
|
|
44
|
+
return " :";
|
|
45
|
+
default:
|
|
46
|
+
return ":";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param {*} name
|
|
54
|
+
* @param {*} s
|
|
55
|
+
* @param {*} l
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
export function colorFromName(name, s, l) {
|
|
59
|
+
if (s == null) {
|
|
60
|
+
s = 40;
|
|
61
|
+
}
|
|
62
|
+
if (l == null) {
|
|
63
|
+
l = 60;
|
|
64
|
+
}
|
|
65
|
+
let hash = 0;
|
|
66
|
+
let i = 0;
|
|
67
|
+
while (i < name.length) {
|
|
68
|
+
hash = name.charCodeAt(i) + (hash << 5) - hash;
|
|
69
|
+
i++;
|
|
70
|
+
}
|
|
71
|
+
const h = hash % 360;
|
|
72
|
+
const r = `hsl(${h}, ${s}%, ${l}%)`;
|
|
73
|
+
return r;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* @param {*} format
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
export function today(format) {
|
|
82
|
+
const f = format || Visitor.dateformat();
|
|
83
|
+
return Dayjs().format(f);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @param {*} val
|
|
91
|
+
* @param {*} opt
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
export function filesize(val, opt) {
|
|
95
|
+
if (val == null || val == undefined) val = 0;
|
|
96
|
+
return Filesize(val, { locale: Visitor.language(), ...opt });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
*
|
|
103
|
+
* @param {*} data
|
|
104
|
+
*/
|
|
105
|
+
export function copyToClipboard(data) {
|
|
106
|
+
const el = document.createElement("textarea");
|
|
107
|
+
el.value = data;
|
|
108
|
+
el.setAttribute("readonly", "");
|
|
109
|
+
el.style.position = "absolute";
|
|
110
|
+
el.style.left = "-9999px";
|
|
111
|
+
|
|
112
|
+
document.body.appendChild(el);
|
|
113
|
+
const selected =
|
|
114
|
+
document.getSelection().rangeCount > 0
|
|
115
|
+
? document.getSelection().getRangeAt(0)
|
|
116
|
+
: false;
|
|
117
|
+
el.select();
|
|
118
|
+
|
|
119
|
+
document.execCommand("copy");
|
|
120
|
+
document.body.removeChild(el);
|
|
121
|
+
if (selected) {
|
|
122
|
+
document.getSelection().removeAllRanges();
|
|
123
|
+
document.getSelection().addRange(selected);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* @param {*} val
|
|
130
|
+
* @returns
|
|
131
|
+
*/
|
|
132
|
+
export function toOnOff(val) {
|
|
133
|
+
if (val) {
|
|
134
|
+
return _a.on;
|
|
135
|
+
}
|
|
136
|
+
return _a.off;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
*
|
|
144
|
+
* @param {*} str
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
export function isHTML(str) {
|
|
148
|
+
const a = document.createElement("div");
|
|
149
|
+
a.innerHTML = str;
|
|
150
|
+
for (let c of Array.from(a.childNodes)) {
|
|
151
|
+
if (c.nodeType === 1) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
*
|
|
161
|
+
* @param {*} sec
|
|
162
|
+
* @returns
|
|
163
|
+
*/
|
|
164
|
+
export function timestamp(sec) {
|
|
165
|
+
if (sec == null) {
|
|
166
|
+
sec = 0;
|
|
167
|
+
}
|
|
168
|
+
const ts = new Date().getTime();
|
|
169
|
+
if (sec) {
|
|
170
|
+
return Math.round(ts / 1000);
|
|
171
|
+
}
|
|
172
|
+
return ts;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
*
|
|
178
|
+
* @param {*} format
|
|
179
|
+
* @returns
|
|
180
|
+
*/
|
|
181
|
+
export function timeNow(format) {
|
|
182
|
+
if (format == null) {
|
|
183
|
+
format = "YY-MM-DD-hh-mm";
|
|
184
|
+
}
|
|
185
|
+
const d = new Date();
|
|
186
|
+
return Dayjs(d.getTime()).format(format);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
*
|
|
191
|
+
* @param {*} time
|
|
192
|
+
* @param {*} format
|
|
193
|
+
* @returns
|
|
194
|
+
*/
|
|
195
|
+
export function dayOfTime(time, format) {
|
|
196
|
+
if (format == null) {
|
|
197
|
+
format = "DD/MM/YYYY";
|
|
198
|
+
}
|
|
199
|
+
return Dayjs(time, "X").format(format);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
* @param {*} url
|
|
205
|
+
* @returns
|
|
206
|
+
*/
|
|
207
|
+
export function loadJS(url) {
|
|
208
|
+
console.log("AAA:208 loding js from", url)
|
|
209
|
+
const a = new Promise(function (resolve, reject) {
|
|
210
|
+
const xhr = new XMLHttpRequest();
|
|
211
|
+
// xhr.allowCORS(xhr, url);
|
|
212
|
+
xhr.open("GET", url, true);
|
|
213
|
+
xhr.onload = function (e) {
|
|
214
|
+
const el = document.createElement(_a.script);
|
|
215
|
+
el.setAttribute(_a.text, "text/javascript");
|
|
216
|
+
el.type = "text/javascript";
|
|
217
|
+
el.setAttribute("charset", "utf-8");
|
|
218
|
+
el.setAttribute("async", "");
|
|
219
|
+
el.innerHTML = xhr.responseText;
|
|
220
|
+
document.head.appendChild(el);
|
|
221
|
+
resolve(xhr);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
xhr.onerror = function (e) {
|
|
225
|
+
reject(xhr);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
xhr.send();
|
|
229
|
+
return xhr;
|
|
230
|
+
});
|
|
231
|
+
return a;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
*
|
|
236
|
+
* @param {*} v
|
|
237
|
+
* @returns
|
|
238
|
+
*/
|
|
239
|
+
export function toggleState(v) {
|
|
240
|
+
if (STATE[v] != null) {
|
|
241
|
+
return STATE[v];
|
|
242
|
+
}
|
|
243
|
+
if (v) {
|
|
244
|
+
return 1;
|
|
245
|
+
}
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
*
|
|
251
|
+
* @param {*} v
|
|
252
|
+
* @returns
|
|
253
|
+
*/
|
|
254
|
+
export function radioState(v) {
|
|
255
|
+
if (RADIO[v] != null) {
|
|
256
|
+
return RADIO[v];
|
|
257
|
+
}
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
*
|
|
263
|
+
* @param {*} v
|
|
264
|
+
* @returns
|
|
265
|
+
*/
|
|
266
|
+
export function toBoolean(v) {
|
|
267
|
+
if (BOOLEAN[v] != null) {
|
|
268
|
+
return BOOLEAN[v];
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
*
|
|
276
|
+
* @param {*} x
|
|
277
|
+
* @returns
|
|
278
|
+
*/
|
|
279
|
+
export function isNumeric(x) {
|
|
280
|
+
if (_.isNumber(x)) {
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
if (_.isString(x)) {
|
|
284
|
+
if (x.match(/^[0-9]+(\.)*[0-9]\%$/)) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
return x.match(/[0-9]\.*[0-9]*/) || x.match(/([0-9]\.*[0-9]*)px/i);
|
|
288
|
+
}
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
*
|
|
294
|
+
* @param {*} outer
|
|
295
|
+
* @param {*} inner
|
|
296
|
+
* @returns
|
|
297
|
+
*/
|
|
298
|
+
export function fitBoxes(outer, inner) {
|
|
299
|
+
let hi, wi;
|
|
300
|
+
outer.height = parseInt(outer.height);
|
|
301
|
+
outer.width = parseInt(outer.width);
|
|
302
|
+
inner.height = parseInt(inner.height) || outer.height;
|
|
303
|
+
inner.width = parseInt(inner.width) || outer.width;
|
|
304
|
+
const innerRatio = inner.width / inner.height;
|
|
305
|
+
const outerRatio = outer.width / outer.height;
|
|
306
|
+
if (outerRatio > innerRatio) {
|
|
307
|
+
wi = (inner.width * outer.height) / inner.height;
|
|
308
|
+
hi = outer.height;
|
|
309
|
+
} else {
|
|
310
|
+
wi = outer.width;
|
|
311
|
+
hi = (inner.height * outer.width) / inner.width;
|
|
312
|
+
}
|
|
313
|
+
const scaled = outerRatio / innerRatio;
|
|
314
|
+
const _box = {
|
|
315
|
+
width: wi,
|
|
316
|
+
height: hi,
|
|
317
|
+
scaled,
|
|
318
|
+
};
|
|
319
|
+
return _box;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
*
|
|
324
|
+
* @param {*} string
|
|
325
|
+
* @returns
|
|
326
|
+
*/
|
|
327
|
+
export function capFirst(string) {
|
|
328
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
*
|
|
333
|
+
* @param {*} sortByFunction
|
|
334
|
+
* @returns
|
|
335
|
+
*/
|
|
336
|
+
export function reverseSortBy(sortByFunction) {
|
|
337
|
+
return function (left, right) {
|
|
338
|
+
var l = sortByFunction(left);
|
|
339
|
+
var r = sortByFunction(right);
|
|
340
|
+
|
|
341
|
+
if (l === void 0) return -1;
|
|
342
|
+
if (r === void 0) return 1;
|
|
343
|
+
|
|
344
|
+
return l < r ? 1 : l > r ? -1 : 0;
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
*
|
|
350
|
+
* @param {*} name
|
|
351
|
+
* @returns
|
|
352
|
+
*/
|
|
353
|
+
export function modelComparator(name) {
|
|
354
|
+
return function modelComparator(model) {
|
|
355
|
+
let v = model.get(name);
|
|
356
|
+
if (v.toLowerCase) {
|
|
357
|
+
return v.toLowerCase();
|
|
358
|
+
}
|
|
359
|
+
return v;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
*
|
|
365
|
+
* @param {*} size
|
|
366
|
+
* @returns
|
|
367
|
+
*/
|
|
368
|
+
export function toPercent(size) {
|
|
369
|
+
size = size * 100;
|
|
370
|
+
return `${Math.round(size)}%`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
*
|
|
378
|
+
* @param {*} val
|
|
379
|
+
* @returns
|
|
380
|
+
*/
|
|
381
|
+
export function arcLength(val) {
|
|
382
|
+
const c = Math.PI * (80 * 2);
|
|
383
|
+
|
|
384
|
+
if (val < 0) {
|
|
385
|
+
val = 0;
|
|
386
|
+
}
|
|
387
|
+
if (val > 100) {
|
|
388
|
+
val = 100;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const pct = ((100 - val) / 100) * c;
|
|
392
|
+
return `${pct}`;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
*
|
|
397
|
+
* @param {*} msg
|
|
398
|
+
*/
|
|
399
|
+
export function openUserMailAgent(msg) {
|
|
400
|
+
let subject = msg.subject || msg.title || "";
|
|
401
|
+
let body = msg.body || msg.message || "";
|
|
402
|
+
subject = encodeURIComponent(subject);
|
|
403
|
+
body = encodeURIComponent(Autolinker.link(body));
|
|
404
|
+
var mailToLink = `?subject=${subject}&body=${body}`;
|
|
405
|
+
if (msg.recipients) {
|
|
406
|
+
if (_.isString(msg.recipients)) {
|
|
407
|
+
mailToLink = `mailto:${msg.recipients}${mailToLink}`;
|
|
408
|
+
} else if (_.isArray(msg.recipients)) {
|
|
409
|
+
let r = msg.recipients.join(";");
|
|
410
|
+
mailToLink = `mailto:${r}${mailToLink}`;
|
|
411
|
+
}
|
|
412
|
+
} else {
|
|
413
|
+
mailToLink = `mailto:${mailToLink}`;
|
|
414
|
+
}
|
|
415
|
+
const a = document.createElement(_K.tag.a);
|
|
416
|
+
a.hidden = "";
|
|
417
|
+
a.setAttribute(_a.href, mailToLink);
|
|
418
|
+
a.setAttribute(_a.target, "_blank");
|
|
419
|
+
a.style.position = _a.absolute;
|
|
420
|
+
a.style.display = _a.none;
|
|
421
|
+
var clickHandler = () => {
|
|
422
|
+
const f = () => {
|
|
423
|
+
a.removeEventListener(_e.click, clickHandler);
|
|
424
|
+
a.remove();
|
|
425
|
+
};
|
|
426
|
+
_.delay(f, 300);
|
|
427
|
+
};
|
|
428
|
+
a.addEventListener(_e.click, clickHandler, false);
|
|
429
|
+
a.click();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
*
|
|
435
|
+
* @param {*} link
|
|
436
|
+
* @returns
|
|
437
|
+
*/
|
|
438
|
+
export function openLink(link) {
|
|
439
|
+
if (window.open(link, "_blank") == null) {
|
|
440
|
+
return alert(LOCALE.WINDOW_BLOCKED);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
*
|
|
446
|
+
* @param {*} len
|
|
447
|
+
* @param {*} sep
|
|
448
|
+
* @returns
|
|
449
|
+
*/
|
|
450
|
+
export function randomString(len, sep) {
|
|
451
|
+
if (len == null) {
|
|
452
|
+
len = 1;
|
|
453
|
+
}
|
|
454
|
+
if (sep == null) {
|
|
455
|
+
sep = "";
|
|
456
|
+
}
|
|
457
|
+
const a = new Uint32Array(len);
|
|
458
|
+
const b = window.crypto.getRandomValues(a);
|
|
459
|
+
const c = [];
|
|
460
|
+
b.forEach((e) => c.push(e.toString(16)));
|
|
461
|
+
return c.join(sep);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
*
|
|
466
|
+
* @param {*} e
|
|
467
|
+
* @returns
|
|
468
|
+
*/
|
|
469
|
+
export function dataTransfer(e) {
|
|
470
|
+
let item;
|
|
471
|
+
let res = { folders: [], files: [] };
|
|
472
|
+
switch (e.type) {
|
|
473
|
+
case _e.drop:
|
|
474
|
+
var dt = e.originalEvent.dataTransfer;
|
|
475
|
+
if (dt == null) {
|
|
476
|
+
return res;
|
|
477
|
+
}
|
|
478
|
+
var items = dt.items || dt.files || [];
|
|
479
|
+
|
|
480
|
+
var folders = [];
|
|
481
|
+
var entries = [];
|
|
482
|
+
var files = [];
|
|
483
|
+
|
|
484
|
+
for (item of Array.from(items)) {
|
|
485
|
+
if (_.isFunction(item.getAsEntry)) {
|
|
486
|
+
entries.push(item.getAsEntry());
|
|
487
|
+
} else if (_.isFunction(item.webkitGetAsEntry)) {
|
|
488
|
+
entries.push(item.webkitGetAsEntry());
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
for (let entry of Array.from(entries)) {
|
|
493
|
+
if (entry && entry.isDirectory) {
|
|
494
|
+
folders.push(entry);
|
|
495
|
+
} else if (entry && entry.isFile) {
|
|
496
|
+
files.push(entry);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
res = { folders, files };
|
|
500
|
+
break;
|
|
501
|
+
case _e.change:
|
|
502
|
+
items = e.target.items || [];
|
|
503
|
+
files = e.target.files || [];
|
|
504
|
+
folders = [];
|
|
505
|
+
for (item of Array.from(items)) {
|
|
506
|
+
if (_.isFunction(item.getAsEntry)) {
|
|
507
|
+
folders.push(item.getAsEntry());
|
|
508
|
+
} else if (_.isFunction(item.webkitGetAsEntry)) {
|
|
509
|
+
folders.push(item.webkitGetAsEntry());
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
res = { folders, files };
|
|
513
|
+
break;
|
|
514
|
+
default:
|
|
515
|
+
console.warn("Got wrong type", e);
|
|
516
|
+
}
|
|
517
|
+
return res;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
*
|
|
522
|
+
* @param {*} fonts
|
|
523
|
+
* @param {*} id
|
|
524
|
+
* @returns
|
|
525
|
+
*/
|
|
526
|
+
export function appendFontFace(fonts, id) {
|
|
527
|
+
if (!_.isArray(fonts)) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const { main_domain } = bootstrap();
|
|
531
|
+
id = id || Host.get(_a.id);
|
|
532
|
+
id = `--font-face-${id}`;
|
|
533
|
+
let el = document.getElementById(id);
|
|
534
|
+
if (el != null) {
|
|
535
|
+
document.head.removeChild(el);
|
|
536
|
+
}
|
|
537
|
+
el = document.createElement(_a.style);
|
|
538
|
+
el.setAttribute(_a.id, id);
|
|
539
|
+
el.setAttribute(_a.type, "text/css");
|
|
540
|
+
el.setAttribute("rel", _a.stylesheet);
|
|
541
|
+
el.setAttribute(_a.media, _a.screen);
|
|
542
|
+
const list = [];
|
|
543
|
+
const { protocol } = bootstrap()
|
|
544
|
+
for (let f of Array.from(fonts)) {
|
|
545
|
+
var l1, url;
|
|
546
|
+
if (_.isEmpty(f.local1)) {
|
|
547
|
+
l1 = f.name;
|
|
548
|
+
} else {
|
|
549
|
+
l1 = f.local1;
|
|
550
|
+
}
|
|
551
|
+
url = `${protocol}://${main_domain}${f.url}`
|
|
552
|
+
const src = `local('${l1}'), local('${f.local2}'), url(${url}) format('${f.format}')`;
|
|
553
|
+
const fn = f.name || f.family;
|
|
554
|
+
const str = `@font-face {\n \
|
|
555
|
+
font-family:'${fn}';\n \
|
|
556
|
+
font-style: ${f.style};\n \
|
|
557
|
+
font-weight: ${f.weight};\n \
|
|
558
|
+
src: ${src};\n \
|
|
559
|
+
}\n`;
|
|
560
|
+
list.push(str);
|
|
561
|
+
}
|
|
562
|
+
el.appendChild(document.createTextNode(list.join(" ")));
|
|
563
|
+
return document.head.appendChild(el);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
*
|
|
569
|
+
* @param {*} url
|
|
570
|
+
* @returns
|
|
571
|
+
*/
|
|
572
|
+
export function appendLink(url) {
|
|
573
|
+
return new Promise((resolve, reject) => {
|
|
574
|
+
const id = this._ensureEl(url, "link");
|
|
575
|
+
// id = opt.id
|
|
576
|
+
if (id == null || id === undefined) {
|
|
577
|
+
this.debug("Injecting undefined link", url);
|
|
578
|
+
return reject();
|
|
579
|
+
}
|
|
580
|
+
const el = document.createElement(_a.link);
|
|
581
|
+
el.setAttribute("id", id);
|
|
582
|
+
el.setAttribute("rel", _a.stylesheet);
|
|
583
|
+
el.setAttribute("href", url);
|
|
584
|
+
el.setAttribute("media", "screen");
|
|
585
|
+
const xhr = new XMLHttpRequest();
|
|
586
|
+
xhr.allowCORS(xhr, url);
|
|
587
|
+
xhr.open("GET", url, true);
|
|
588
|
+
xhr.send();
|
|
589
|
+
return (xhr.onloadend = function (e) {
|
|
590
|
+
document.head.appendChild(el);
|
|
591
|
+
resolve(e);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
*
|
|
599
|
+
* @param {*} url
|
|
600
|
+
* @returns
|
|
601
|
+
*/
|
|
602
|
+
export function createQrcode(opt) {
|
|
603
|
+
const p = 50;
|
|
604
|
+
const { id, text = "", maxWidth = window.innerWidth - p, maxHeight = window.innerHeight - p } = opt;
|
|
605
|
+
let count = 0;
|
|
606
|
+
return new Promise((resolve, reject) => {
|
|
607
|
+
let i = setInterval(async () => {
|
|
608
|
+
count++;
|
|
609
|
+
const canvas = document.getElementById(id);
|
|
610
|
+
if (canvas) {
|
|
611
|
+
clearInterval(i);
|
|
612
|
+
let { toDataURL } = require("qrcode");
|
|
613
|
+
let src = await toDataURL(text);
|
|
614
|
+
if (canvas.width > maxWidth) {
|
|
615
|
+
canvas.width = maxWidth;
|
|
616
|
+
}
|
|
617
|
+
if (canvas.height > maxHeight) {
|
|
618
|
+
canvas.height = maxHeight;
|
|
619
|
+
}
|
|
620
|
+
canvas.src = src;
|
|
621
|
+
resolve(canvas)
|
|
622
|
+
} else {
|
|
623
|
+
if (count > 100) {
|
|
624
|
+
console.warn(`Failed to find provided element id=${id}`);
|
|
625
|
+
reject({ error: `Element not found` })
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}, 300);
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
export function createSafeObject(initial = {}) {
|
|
633
|
+
const data = { ...initial };
|
|
634
|
+
|
|
635
|
+
const handler = {
|
|
636
|
+
get(target, prop) {
|
|
637
|
+
// Handle method calls
|
|
638
|
+
if (methods.hasOwnProperty(prop)) {
|
|
639
|
+
return methods[prop].bind(proxy);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Handle property access
|
|
643
|
+
if (prop in data) {
|
|
644
|
+
return data[prop];
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Return key as string for missing properties
|
|
648
|
+
return String(prop);
|
|
649
|
+
},
|
|
650
|
+
|
|
651
|
+
set(target, prop, value) {
|
|
652
|
+
// Don't allow overriding methods
|
|
653
|
+
if (prop in methods) {
|
|
654
|
+
console.warn(`Cannot override method "${prop}"`);
|
|
655
|
+
return false;
|
|
656
|
+
}
|
|
657
|
+
data[prop] = value;
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
// Built-in methods
|
|
663
|
+
const methods = {
|
|
664
|
+
set(key, value) {
|
|
665
|
+
data[key] = value;
|
|
666
|
+
return this;
|
|
667
|
+
},
|
|
668
|
+
|
|
669
|
+
get(key) {
|
|
670
|
+
return key in data ? data[key] : String(key);
|
|
671
|
+
},
|
|
672
|
+
|
|
673
|
+
extend(newProps) {
|
|
674
|
+
Object.assign(data, newProps);
|
|
675
|
+
return this;
|
|
676
|
+
},
|
|
677
|
+
|
|
678
|
+
delete(key) {
|
|
679
|
+
delete data[key];
|
|
680
|
+
return this;
|
|
681
|
+
},
|
|
682
|
+
|
|
683
|
+
has(key) {
|
|
684
|
+
return key in data;
|
|
685
|
+
},
|
|
686
|
+
|
|
687
|
+
keys() {
|
|
688
|
+
return Object.keys(data);
|
|
689
|
+
},
|
|
690
|
+
|
|
691
|
+
values() {
|
|
692
|
+
return Object.values(data);
|
|
693
|
+
},
|
|
694
|
+
|
|
695
|
+
toObject() {
|
|
696
|
+
return { ...data };
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
const proxy = new Proxy({}, handler);
|
|
701
|
+
return proxy;
|
|
702
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// ==================================================================== *
|
|
2
|
+
// Copyright Xialia.com 2011-2019
|
|
3
|
+
// FILE : src/drumee/core/utils
|
|
4
|
+
// TYPE :
|
|
5
|
+
// ==================================================================== *
|
|
6
|
+
const identRegExp = /^([a-zA-Z0-9_\-])([a-zA-Z0-9_\.\-])([a-zA-Z0-9_\.\-])*$/;
|
|
7
|
+
const hostRegExp = /^[a-zA-Z0-9_\-]+$/;
|
|
8
|
+
const nameRegExp = /^([a-zA-Z0-9_\.\-\'\ xC0-\xFF])+$/;
|
|
9
|
+
|
|
10
|
+
const validator = {
|
|
11
|
+
require(v) {
|
|
12
|
+
if (v.trim() === '') {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
email(v) {
|
|
19
|
+
if (v.trim() !== '') {
|
|
20
|
+
return v.isEmail();
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
phone(v) {
|
|
26
|
+
if (v.trim() !== '') {
|
|
27
|
+
return v.isPhoneNumber();
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
emailOrIdent(v) {
|
|
33
|
+
if (v.trim() !== '') {
|
|
34
|
+
return v.isEmail() || identRegExp.test(v);
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
ident(v) {
|
|
40
|
+
if (v.trim() !== '') {
|
|
41
|
+
return identRegExp.test(v);
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
host(v) {
|
|
47
|
+
if (v.trim() !== '') {
|
|
48
|
+
return hostRegExp.test(v);
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
name(v) {
|
|
54
|
+
if (v.trim() !== '') {
|
|
55
|
+
return nameRegExp.test(v);
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports = validator;
|