x4js 1.4.4 → 1.4.7
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/lib/application.d.ts +96 -0
- package/lib/application.js +146 -0
- package/lib/base64.d.ts +31 -0
- package/lib/base64.js +139 -0
- package/lib/base_component.d.ts +64 -0
- package/lib/base_component.js +81 -0
- package/lib/button.d.ts +145 -0
- package/lib/button.js +241 -0
- package/lib/calendar.d.ts +77 -0
- package/lib/calendar.js +241 -0
- package/lib/canvas.d.ts +88 -0
- package/lib/canvas.js +358 -0
- package/lib/cardview.d.ts +83 -0
- package/lib/cardview.js +156 -0
- package/lib/checkbox.d.ts +72 -0
- package/lib/checkbox.js +130 -0
- package/lib/color.d.ts +144 -0
- package/lib/color.js +588 -0
- package/lib/colorpicker.js +86 -80
- package/lib/combobox.js +26 -22
- package/lib/component.d.ts +572 -0
- package/lib/component.js +1729 -0
- package/lib/datastore.js +29 -20
- package/lib/dialog.js +41 -36
- package/lib/dom_events.d.ts +284 -0
- package/lib/dom_events.js +14 -0
- package/lib/drag_manager.js +4 -1
- package/lib/drawtext.js +9 -5
- package/lib/fileupload.js +19 -12
- package/lib/form.js +29 -25
- package/lib/formatters.js +19 -10
- package/lib/gridview.js +40 -35
- package/lib/hosts/host.d.ts +44 -0
- package/lib/hosts/host.js +73 -0
- package/lib/i18n.d.ts +67 -0
- package/lib/i18n.js +175 -0
- package/lib/icon.d.ts +56 -0
- package/lib/icon.js +178 -0
- package/lib/image.js +7 -3
- package/lib/index.js +71 -55
- package/lib/input.d.ts +86 -0
- package/lib/input.js +176 -0
- package/lib/label.d.ts +54 -0
- package/lib/label.js +90 -0
- package/lib/layout.d.ts +77 -0
- package/lib/layout.js +271 -0
- package/lib/link.js +9 -5
- package/lib/listview.js +34 -27
- package/lib/md5.js +5 -1
- package/lib/menu.d.ts +122 -0
- package/lib/menu.js +284 -0
- package/lib/messagebox.js +22 -17
- package/lib/panel.js +13 -9
- package/lib/popup.d.ts +71 -0
- package/lib/popup.js +378 -0
- package/lib/property_editor.js +20 -16
- package/lib/radiobtn.js +13 -9
- package/lib/rating.js +13 -9
- package/lib/request.js +16 -9
- package/lib/router.js +5 -1
- package/lib/settings.d.ts +33 -0
- package/lib/settings.js +67 -0
- package/lib/sidebarview.js +12 -8
- package/lib/smartedit.js +16 -11
- package/lib/spreadsheet.js +35 -31
- package/lib/styles.d.ts +81 -0
- package/lib/styles.js +268 -0
- package/lib/svgcomponent.js +8 -3
- package/lib/tabbar.js +10 -6
- package/lib/tabview.js +10 -6
- package/lib/textarea.js +10 -6
- package/lib/textedit.js +39 -35
- package/lib/texthiliter.js +8 -4
- package/lib/toaster.js +8 -4
- package/lib/tools.d.ts +382 -0
- package/lib/tools.js +1142 -0
- package/lib/tooltips.js +14 -9
- package/lib/treeview.js +30 -26
- package/lib/x4_events.d.ts +253 -0
- package/lib/x4_events.js +375 -0
- package/package.json +1 -1
- package/src/application.ts +9 -4
- package/src/x4.less +143 -107
- package/tsconfig.json +1 -1
- package/x4.css +1541 -0
package/lib/tools.js
ADDED
|
@@ -0,0 +1,1142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ___ ___ __
|
|
4
|
+
* \ \_/ / / _
|
|
5
|
+
* \ / /_| |_
|
|
6
|
+
* / _ \____ _|
|
|
7
|
+
* /__/ \__\ |_|
|
|
8
|
+
*
|
|
9
|
+
* @file tools.ts
|
|
10
|
+
* @author Etienne Cochard
|
|
11
|
+
* @license
|
|
12
|
+
* Copyright (c) 2019-2021 R-libre ingenierie
|
|
13
|
+
*
|
|
14
|
+
* This program is free software; you can redistribute it and/or modify
|
|
15
|
+
* it under the terms of the GNU General Public License as published by
|
|
16
|
+
* the Free Software Foundation; either version 3 of the License, or
|
|
17
|
+
* (at your option) any later version.
|
|
18
|
+
*
|
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22
|
+
* GNU General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
25
|
+
**/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.generatePassword = exports.classNames = exports.mix = exports.crc32 = exports.Clipboard = exports.isHtmlString = exports.html = exports.HtmlString = exports.clamp = exports.getMousePos = exports.NetworkError = exports.markdownToHtml = exports.asap = exports.deferCall = exports.waitFontLoading = exports.isNumber = exports.pad = exports.isLiteralObject = exports.isFunction = exports.isArray = exports.isString = exports.downloadData = exports.calcAge = exports.formatIntlDate = exports.parseIntlDate = exports.date_calc_weeknum = exports.date_clone = exports.date_hash = exports.date_sql_utc = exports.date_to_sql = exports.date_diff = exports.date_format = exports._date_set_locale = exports.removeHtmlTags = exports.escapeHtml = exports.sprintf = exports.Rect = exports.Size = exports.Point = exports.pascalCase = exports.parseIntlFloat = exports.roundTo = exports.isTouchDevice = void 0;
|
|
28
|
+
const i18n_1 = require("./i18n"); // you MUST create a file named translation.js
|
|
29
|
+
// :: ENVIRONMENT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
30
|
+
/**
|
|
31
|
+
* @return true is the device has touch
|
|
32
|
+
*/
|
|
33
|
+
function isTouchDevice() {
|
|
34
|
+
return 'ontouchstart' in window;
|
|
35
|
+
}
|
|
36
|
+
exports.isTouchDevice = isTouchDevice;
|
|
37
|
+
// :: NUMBERS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
38
|
+
/**
|
|
39
|
+
* round to a given number of decimals
|
|
40
|
+
* @param num numbre to round
|
|
41
|
+
* @param ndec number of decimals
|
|
42
|
+
* @returns number rounded
|
|
43
|
+
*/
|
|
44
|
+
function roundTo(num, ndec) {
|
|
45
|
+
let mul = Math.pow(10, ndec);
|
|
46
|
+
let res = Math.round(num * mul) / mul;
|
|
47
|
+
if (Object.is(res, -0)) { // res===-0.0
|
|
48
|
+
res = 0;
|
|
49
|
+
}
|
|
50
|
+
return res;
|
|
51
|
+
}
|
|
52
|
+
exports.roundTo = roundTo;
|
|
53
|
+
/**
|
|
54
|
+
* parse an intl formatted number
|
|
55
|
+
* understand grouping and ',' separator
|
|
56
|
+
* @review us format: grouping = ','
|
|
57
|
+
* @param num
|
|
58
|
+
*/
|
|
59
|
+
function parseIntlFloat(num) {
|
|
60
|
+
num = num.replace(/\s*/g, ''); // group separator
|
|
61
|
+
num = num.replace(',', '.'); // decimal separator
|
|
62
|
+
// accept empty & return 0.0
|
|
63
|
+
if (num.length == 0) {
|
|
64
|
+
return 0.0;
|
|
65
|
+
}
|
|
66
|
+
return parseFloat(num);
|
|
67
|
+
}
|
|
68
|
+
exports.parseIntlFloat = parseIntlFloat;
|
|
69
|
+
// :: STRING MANIP ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
70
|
+
const RE_PCASE = /([a-z][A-Z])/g;
|
|
71
|
+
const RE_PCASE2 = /[^-a-z0-9]+/g;
|
|
72
|
+
/**
|
|
73
|
+
* inverse of camel case
|
|
74
|
+
* theThingToCase -> the-thing-to-case
|
|
75
|
+
* @param {String} str
|
|
76
|
+
*/
|
|
77
|
+
function pascalCase(string) {
|
|
78
|
+
let result = string;
|
|
79
|
+
result = result.replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
80
|
+
result = result.toLowerCase();
|
|
81
|
+
result = result.replace(/[^- a-z0-9]+/g, ' ');
|
|
82
|
+
if (result.indexOf(' ') < 0) {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
result = result.trim();
|
|
86
|
+
return result.replace(/ /g, '-');
|
|
87
|
+
}
|
|
88
|
+
exports.pascalCase = pascalCase;
|
|
89
|
+
class Point {
|
|
90
|
+
x;
|
|
91
|
+
y;
|
|
92
|
+
constructor(x = 0, y = 0) {
|
|
93
|
+
this.x = x;
|
|
94
|
+
this.y = y;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.Point = Point;
|
|
98
|
+
class Size {
|
|
99
|
+
width;
|
|
100
|
+
height;
|
|
101
|
+
constructor(w = 0, h = 0) {
|
|
102
|
+
this.width = w;
|
|
103
|
+
this.height = h;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.Size = Size;
|
|
107
|
+
class Rect {
|
|
108
|
+
left;
|
|
109
|
+
top;
|
|
110
|
+
width;
|
|
111
|
+
height;
|
|
112
|
+
constructor(left, top, width, height) {
|
|
113
|
+
if (left === undefined) {
|
|
114
|
+
this.left = this.top = this.right = this.bottom = 0;
|
|
115
|
+
}
|
|
116
|
+
else if (left instanceof Rect || left instanceof DOMRect) {
|
|
117
|
+
let rc = left;
|
|
118
|
+
this.left = rc.left;
|
|
119
|
+
this.top = rc.top;
|
|
120
|
+
this.width = rc.width;
|
|
121
|
+
this.height = rc.height;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
this.left = left;
|
|
125
|
+
this.top = top;
|
|
126
|
+
this.width = width;
|
|
127
|
+
this.height = height;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
set(left, top, width, height) {
|
|
131
|
+
this.left = left;
|
|
132
|
+
this.top = top;
|
|
133
|
+
this.width = width;
|
|
134
|
+
this.height = height;
|
|
135
|
+
}
|
|
136
|
+
get bottom() {
|
|
137
|
+
return this.top + this.height;
|
|
138
|
+
}
|
|
139
|
+
set bottom(bottom) {
|
|
140
|
+
this.height = bottom - this.top;
|
|
141
|
+
}
|
|
142
|
+
get right() {
|
|
143
|
+
return this.left + this.width;
|
|
144
|
+
}
|
|
145
|
+
set right(right) {
|
|
146
|
+
this.width = right - this.left;
|
|
147
|
+
}
|
|
148
|
+
get topLeft() {
|
|
149
|
+
return new Point(this.left, this.top);
|
|
150
|
+
}
|
|
151
|
+
get bottomRight() {
|
|
152
|
+
return new Point(this.right, this.bottom);
|
|
153
|
+
}
|
|
154
|
+
get size() {
|
|
155
|
+
return new Size(this.width, this.height);
|
|
156
|
+
}
|
|
157
|
+
moveTo(left, top) {
|
|
158
|
+
this.left = left;
|
|
159
|
+
this.top = top;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
movedTo(left, top) {
|
|
163
|
+
return new Rect(left, top, this.width, this.height);
|
|
164
|
+
}
|
|
165
|
+
moveBy(dx, dy) {
|
|
166
|
+
this.left += dx;
|
|
167
|
+
this.top += dy;
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
movedBy(dx, dy) {
|
|
171
|
+
return new Rect(this.left + dx, this.top + dy, this.width, this.height);
|
|
172
|
+
}
|
|
173
|
+
isEmpty() {
|
|
174
|
+
return this.width == 0 && this.height == 0;
|
|
175
|
+
}
|
|
176
|
+
normalize() {
|
|
177
|
+
let w = this.width, h = this.height;
|
|
178
|
+
if (w < 0) {
|
|
179
|
+
this.left += w;
|
|
180
|
+
this.width = -w;
|
|
181
|
+
}
|
|
182
|
+
if (h < 0) {
|
|
183
|
+
this.top += h;
|
|
184
|
+
this.height = -h;
|
|
185
|
+
}
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
normalized() {
|
|
189
|
+
let rc = new Rect(this);
|
|
190
|
+
let w = rc.width, h = rc.height;
|
|
191
|
+
if (w < 0) {
|
|
192
|
+
rc.left += w;
|
|
193
|
+
rc.width = -w;
|
|
194
|
+
}
|
|
195
|
+
if (h < 0) {
|
|
196
|
+
rc.top += h;
|
|
197
|
+
rc.height = -h;
|
|
198
|
+
}
|
|
199
|
+
return rc;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* @deprecated
|
|
203
|
+
*/
|
|
204
|
+
containsPt(x, y) {
|
|
205
|
+
return x >= this.left && x <= this.right && y >= this.top && y <= this.bottom;
|
|
206
|
+
}
|
|
207
|
+
contains(arg) {
|
|
208
|
+
if (arg instanceof Rect) {
|
|
209
|
+
return arg.left >= this.left && arg.right <= this.right && arg.top >= this.top && arg.bottom <= this.bottom;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
return arg.x >= this.left && arg.x < this.right && arg.y >= this.top && arg.y < this.bottom;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
touches(rc) {
|
|
216
|
+
if (this.left > rc.right || this.right < rc.left || this.top > rc.bottom || this.bottom < rc.top) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
inflate(dx, dy) {
|
|
222
|
+
if (dy === undefined) {
|
|
223
|
+
dy = dx;
|
|
224
|
+
}
|
|
225
|
+
this.left -= dx;
|
|
226
|
+
this.top -= dy;
|
|
227
|
+
this.width += dx + dx;
|
|
228
|
+
this.height += dy + dy;
|
|
229
|
+
}
|
|
230
|
+
inflatedBy(dx, dy) {
|
|
231
|
+
if (dy === undefined) {
|
|
232
|
+
dy = dx;
|
|
233
|
+
}
|
|
234
|
+
return new Rect(this.left - dx, this.top - dy, this.width + dx + dx, this.height + dy + dy);
|
|
235
|
+
}
|
|
236
|
+
combine(rc) {
|
|
237
|
+
let left = Math.min(this.left, rc.left);
|
|
238
|
+
let top = Math.min(this.top, rc.top);
|
|
239
|
+
let right = Math.max(this.right, rc.right);
|
|
240
|
+
let bottom = Math.max(this.bottom, rc.bottom);
|
|
241
|
+
this.left = left;
|
|
242
|
+
this.top = top;
|
|
243
|
+
this.right = right;
|
|
244
|
+
this.bottom = bottom;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
exports.Rect = Rect;
|
|
248
|
+
/**
|
|
249
|
+
* replace {0..9} by given arguments
|
|
250
|
+
* @param format string
|
|
251
|
+
* @param args
|
|
252
|
+
*
|
|
253
|
+
* @example ```ts
|
|
254
|
+
*
|
|
255
|
+
* console.log( sprintf( 'here is arg 1 {1} and arg 0 {0}', 'argument 0', 'argument 1' ) )
|
|
256
|
+
*/
|
|
257
|
+
function sprintf(format, ...args) {
|
|
258
|
+
return format.replace(/{(\d+)}/g, function (match, index) {
|
|
259
|
+
return typeof args[index] != 'undefined' ? args[index] : match;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
exports.sprintf = sprintf;
|
|
263
|
+
/**
|
|
264
|
+
* replace special characters for display
|
|
265
|
+
* @param unsafe
|
|
266
|
+
*
|
|
267
|
+
* console.log( escapeHtml('<div style="width:50px; height: 50px; background-color:red"></div>') );
|
|
268
|
+
*/
|
|
269
|
+
function escapeHtml(unsafe, nl_br = false) {
|
|
270
|
+
if (!unsafe || unsafe.length == 0) {
|
|
271
|
+
return unsafe;
|
|
272
|
+
}
|
|
273
|
+
let result = unsafe.replace(/[<>\&\"\']/g, function (c) {
|
|
274
|
+
return '&#' + c.charCodeAt(0) + ';';
|
|
275
|
+
});
|
|
276
|
+
if (nl_br) {
|
|
277
|
+
result = result.replace(/(\r\n|\n\r|\r|\n)/g, '<br/>');
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
exports.escapeHtml = escapeHtml;
|
|
282
|
+
/**
|
|
283
|
+
* replace special characters for display
|
|
284
|
+
* @author Steven Levithan <http://slevithan.com/>
|
|
285
|
+
* @param unsafe
|
|
286
|
+
*
|
|
287
|
+
* console.log( removeHtmlTags('<h1>sss</h1>') );
|
|
288
|
+
*/
|
|
289
|
+
function removeHtmlTags(unsafe, nl_br = false) {
|
|
290
|
+
if (!unsafe || unsafe.length == 0) {
|
|
291
|
+
return unsafe;
|
|
292
|
+
}
|
|
293
|
+
debugger;
|
|
294
|
+
let ret_val = '';
|
|
295
|
+
for (let i = 0; i < unsafe.length; i++) {
|
|
296
|
+
const ch = unsafe.codePointAt(i);
|
|
297
|
+
if (ch > 127) {
|
|
298
|
+
ret_val += '&#' + ch + ';';
|
|
299
|
+
}
|
|
300
|
+
else if (ch == 60 /*<*/) {
|
|
301
|
+
ret_val += '<';
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
ret_val += unsafe.charAt(i);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return ret_val;
|
|
308
|
+
}
|
|
309
|
+
exports.removeHtmlTags = removeHtmlTags;
|
|
310
|
+
// :: DATES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
311
|
+
let cur_locale = 'fr-FR';
|
|
312
|
+
/**
|
|
313
|
+
* change the current locale for misc translations (date...)
|
|
314
|
+
* @param locale
|
|
315
|
+
*/
|
|
316
|
+
function _date_set_locale(locale) {
|
|
317
|
+
cur_locale = locale;
|
|
318
|
+
}
|
|
319
|
+
exports._date_set_locale = _date_set_locale;
|
|
320
|
+
/**
|
|
321
|
+
*
|
|
322
|
+
* @param date
|
|
323
|
+
* @param options
|
|
324
|
+
* @example
|
|
325
|
+
* let date = new Date( );
|
|
326
|
+
* let options = { day: 'numeric', month: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric' };
|
|
327
|
+
* let text = date_format( date, options );
|
|
328
|
+
*/
|
|
329
|
+
function date_format(date, options) {
|
|
330
|
+
//return new Intl.DateTimeFormat(cur_locale, options).format( date );
|
|
331
|
+
return formatIntlDate(date);
|
|
332
|
+
}
|
|
333
|
+
exports.date_format = date_format;
|
|
334
|
+
/**
|
|
335
|
+
*
|
|
336
|
+
* @param date
|
|
337
|
+
* @param options
|
|
338
|
+
*/
|
|
339
|
+
function date_diff(date1, date2, options) {
|
|
340
|
+
var dt = (date1.getTime() - date2.getTime()) / 1000;
|
|
341
|
+
// seconds
|
|
342
|
+
let sec = dt;
|
|
343
|
+
if (sec < 60) {
|
|
344
|
+
return sprintf(i18n_1._tr.global.diff_date_seconds, Math.round(sec));
|
|
345
|
+
}
|
|
346
|
+
// minutes
|
|
347
|
+
let min = Math.floor(sec / 60);
|
|
348
|
+
if (min < 60) {
|
|
349
|
+
return sprintf(i18n_1._tr.global.diff_date_minutes, Math.round(min));
|
|
350
|
+
}
|
|
351
|
+
// hours
|
|
352
|
+
let hrs = Math.floor(min / 60);
|
|
353
|
+
return sprintf(i18n_1._tr.global.diff_date_hours, hrs, min % 60);
|
|
354
|
+
}
|
|
355
|
+
exports.date_diff = date_diff;
|
|
356
|
+
function date_to_sql(date, withHours) {
|
|
357
|
+
if (withHours) {
|
|
358
|
+
return formatIntlDate(date, 'Y-M-D H:I:S');
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return formatIntlDate(date, 'Y-M-D');
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
exports.date_to_sql = date_to_sql;
|
|
365
|
+
/**
|
|
366
|
+
* construct a date from an utc date time (sql format)
|
|
367
|
+
* YYYY-MM-DD HH:MM:SS
|
|
368
|
+
*/
|
|
369
|
+
function date_sql_utc(date) {
|
|
370
|
+
let result = new Date(date + ' GMT');
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
exports.date_sql_utc = date_sql_utc;
|
|
374
|
+
/**
|
|
375
|
+
* return a number that is a representation of the date
|
|
376
|
+
* this number can be compared with another hash
|
|
377
|
+
*/
|
|
378
|
+
function date_hash(date) {
|
|
379
|
+
return date.getFullYear() << 16 | date.getMonth() << 8 | date.getDate();
|
|
380
|
+
}
|
|
381
|
+
exports.date_hash = date_hash;
|
|
382
|
+
Date.prototype.hash = function () {
|
|
383
|
+
return date_hash(this);
|
|
384
|
+
};
|
|
385
|
+
/**
|
|
386
|
+
* return a copy of a date
|
|
387
|
+
*/
|
|
388
|
+
function date_clone(date) {
|
|
389
|
+
return new Date(date.getTime());
|
|
390
|
+
}
|
|
391
|
+
exports.date_clone = date_clone;
|
|
392
|
+
/**
|
|
393
|
+
* return the week number of a date
|
|
394
|
+
*/
|
|
395
|
+
function date_calc_weeknum(date) {
|
|
396
|
+
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
|
|
397
|
+
const pastDaysOfYear = (date.valueOf() - firstDayOfYear.valueOf()) / 86400000;
|
|
398
|
+
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
|
|
399
|
+
}
|
|
400
|
+
exports.date_calc_weeknum = date_calc_weeknum;
|
|
401
|
+
/**
|
|
402
|
+
* parse a date according to the given format
|
|
403
|
+
* @param value - string date to parse
|
|
404
|
+
* @param fmts - format list - i18 tranlation by default
|
|
405
|
+
* allowed format specifiers:
|
|
406
|
+
* d or D: date (1 or 2 digits)
|
|
407
|
+
* m or M: month (1 or 2 digits)
|
|
408
|
+
* y or Y: year (2 or 4 digits)
|
|
409
|
+
* h or H: hours (1 or 2 digits)
|
|
410
|
+
* i or I: minutes (1 or 2 digits)
|
|
411
|
+
* s or S: seconds (1 or 2 digits)
|
|
412
|
+
* <space>: 1 or more spaces
|
|
413
|
+
* any other char: <0 or more spaces><the char><0 or more spaces>
|
|
414
|
+
* each specifiers is separated from other by a pipe (|)
|
|
415
|
+
* more specific at first
|
|
416
|
+
* @example
|
|
417
|
+
* 'd/m/y|d m Y|dmy|y-m-d h:i:s|y-m-d'
|
|
418
|
+
*/
|
|
419
|
+
function parseIntlDate(value, fmts = i18n_1._tr.global.date_input_formats) {
|
|
420
|
+
let formats = fmts.split('|');
|
|
421
|
+
for (let fmatch of formats) {
|
|
422
|
+
//review: could do that only once & keep result
|
|
423
|
+
//review: add hours, minutes, seconds
|
|
424
|
+
let smatch = '';
|
|
425
|
+
for (let c of fmatch) {
|
|
426
|
+
if (c == 'd' || c == 'D') {
|
|
427
|
+
smatch += '(?<day>\\d{1,2})';
|
|
428
|
+
}
|
|
429
|
+
else if (c == 'm' || c == 'M') {
|
|
430
|
+
smatch += '(?<month>\\d{1,2})';
|
|
431
|
+
}
|
|
432
|
+
else if (c == 'y' || c == 'Y') {
|
|
433
|
+
smatch += '(?<year>\\d{1,4})';
|
|
434
|
+
}
|
|
435
|
+
else if (c == 'h' || c == 'H') {
|
|
436
|
+
smatch += '(?<hour>\\d{1,2})';
|
|
437
|
+
}
|
|
438
|
+
else if (c == 'i' || c == 'I') {
|
|
439
|
+
smatch += '(?<min>\\d{1,2})';
|
|
440
|
+
}
|
|
441
|
+
else if (c == 's' || c == 'S') {
|
|
442
|
+
smatch += '(?<sec>\\d{1,2})';
|
|
443
|
+
}
|
|
444
|
+
else if (c == ' ') {
|
|
445
|
+
smatch += '\\s+';
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
smatch += '\\s*\\' + c + '\\s*';
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
let rematch = new RegExp('^' + smatch + '$', 'm');
|
|
452
|
+
let match = rematch.exec(value);
|
|
453
|
+
if (match) {
|
|
454
|
+
let d = parseInt(match.groups.day ?? '1');
|
|
455
|
+
let m = parseInt(match.groups.month ?? '1');
|
|
456
|
+
let y = parseInt(match.groups.year ?? '1970');
|
|
457
|
+
let h = parseInt(match.groups.hour ?? '0');
|
|
458
|
+
let i = parseInt(match.groups.min ?? '0');
|
|
459
|
+
let s = parseInt(match.groups.sec ?? '0');
|
|
460
|
+
if (y > 0 && y < 100) {
|
|
461
|
+
y += 2000;
|
|
462
|
+
}
|
|
463
|
+
let result = new Date(y, m - 1, d, h, i, s, 0);
|
|
464
|
+
// we test the vdate validity (without adjustments)
|
|
465
|
+
// without this test, date ( 0, 0, 0) is accepted and transformed to 1969/11/31 (not fun)
|
|
466
|
+
let ty = result.getFullYear(), tm = result.getMonth() + 1, td = result.getDate();
|
|
467
|
+
if (ty != y || tm != m || td != d) {
|
|
468
|
+
//debugger;
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
exports.parseIntlDate = parseIntlDate;
|
|
477
|
+
/**
|
|
478
|
+
* format a date as string
|
|
479
|
+
* @param date - date to format
|
|
480
|
+
* @param fmt - format
|
|
481
|
+
* format specifiers:
|
|
482
|
+
* d: date
|
|
483
|
+
* D: 2 digits date padded with 0
|
|
484
|
+
* j: day of week short mode 'mon'
|
|
485
|
+
* J: day of week long mode 'monday'
|
|
486
|
+
* w: week number
|
|
487
|
+
* m: month
|
|
488
|
+
* M: 2 digits month padded with 0
|
|
489
|
+
* o: month short mode 'jan'
|
|
490
|
+
* O: month long mode 'january'
|
|
491
|
+
* y or Y: year
|
|
492
|
+
* h: hour (24 format)
|
|
493
|
+
* H: 2 digits hour (24 format) padded with 0
|
|
494
|
+
* i: minutes
|
|
495
|
+
* I: 2 digits minutes padded with 0
|
|
496
|
+
* s: seconds
|
|
497
|
+
* S: 2 digits seconds padded with 0
|
|
498
|
+
* a: am or pm
|
|
499
|
+
* anything else is inserted
|
|
500
|
+
* if you need to insert some text, put it between {}
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
*
|
|
504
|
+
* 01/01/1970 11:25:00 with '{this is my demo date formatter: }H-i*M'
|
|
505
|
+
* "this is my demo date formatter: 11-25*january"
|
|
506
|
+
*/
|
|
507
|
+
function formatIntlDate(date, fmt = i18n_1._tr.global.date_format) {
|
|
508
|
+
if (!date) {
|
|
509
|
+
return '';
|
|
510
|
+
}
|
|
511
|
+
let now = {
|
|
512
|
+
year: date.getFullYear(),
|
|
513
|
+
month: date.getMonth() + 1,
|
|
514
|
+
day: date.getDate(),
|
|
515
|
+
wday: date.getDay(),
|
|
516
|
+
hours: date.getHours(),
|
|
517
|
+
minutes: date.getMinutes(),
|
|
518
|
+
seconds: date.getSeconds(),
|
|
519
|
+
milli: date.getMilliseconds()
|
|
520
|
+
};
|
|
521
|
+
let result = '';
|
|
522
|
+
let esc = 0;
|
|
523
|
+
for (let c of fmt) {
|
|
524
|
+
if (c == '{') {
|
|
525
|
+
if (++esc == 1) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
else if (c == '}') {
|
|
530
|
+
if (--esc == 0) {
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (esc) {
|
|
535
|
+
result += c;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if (c == 'd') {
|
|
539
|
+
result += now.day;
|
|
540
|
+
}
|
|
541
|
+
else if (c == 'D') {
|
|
542
|
+
result += pad(now.day, -2);
|
|
543
|
+
}
|
|
544
|
+
else if (c == 'j') { // day short
|
|
545
|
+
result += i18n_1._tr.global.day_short[now.wday];
|
|
546
|
+
}
|
|
547
|
+
else if (c == 'J') { // day long
|
|
548
|
+
result += i18n_1._tr.global.day_long[now.wday];
|
|
549
|
+
}
|
|
550
|
+
else if (c == 'w') { // week
|
|
551
|
+
result += date_calc_weeknum(date);
|
|
552
|
+
}
|
|
553
|
+
else if (c == 'W') { // week
|
|
554
|
+
result += pad(date_calc_weeknum(date), -2);
|
|
555
|
+
}
|
|
556
|
+
else if (c == 'm') {
|
|
557
|
+
result += now.month;
|
|
558
|
+
}
|
|
559
|
+
else if (c == 'M') {
|
|
560
|
+
result += pad(now.month, -2);
|
|
561
|
+
}
|
|
562
|
+
else if (c == 'o') { // month short
|
|
563
|
+
result += i18n_1._tr.global.month_short[now.month - 1];
|
|
564
|
+
}
|
|
565
|
+
else if (c == 'O') { // month long
|
|
566
|
+
result += i18n_1._tr.global.month_long[now.month - 1];
|
|
567
|
+
}
|
|
568
|
+
else if (c == 'y' || c == 'Y') {
|
|
569
|
+
result += pad(now.year, -4);
|
|
570
|
+
}
|
|
571
|
+
else if (c == 'a' || c == 'A') {
|
|
572
|
+
result += now.hours < 12 ? 'am' : 'pm';
|
|
573
|
+
}
|
|
574
|
+
else if (c == 'h') {
|
|
575
|
+
result += now.hours;
|
|
576
|
+
}
|
|
577
|
+
else if (c == 'H') {
|
|
578
|
+
result += pad(now.hours, -2);
|
|
579
|
+
}
|
|
580
|
+
else if (c == 'i') {
|
|
581
|
+
result += now.minutes;
|
|
582
|
+
}
|
|
583
|
+
else if (c == 'I') {
|
|
584
|
+
result += pad(now.minutes, -2);
|
|
585
|
+
}
|
|
586
|
+
else if (c == 's') {
|
|
587
|
+
result += now.seconds;
|
|
588
|
+
}
|
|
589
|
+
else if (c == 'S') {
|
|
590
|
+
result += pad(now.seconds, -2);
|
|
591
|
+
}
|
|
592
|
+
else if (c == 'l') {
|
|
593
|
+
result += now.milli;
|
|
594
|
+
}
|
|
595
|
+
else if (c == 'L') {
|
|
596
|
+
result += pad(now.milli, -3);
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
result += c;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
exports.formatIntlDate = formatIntlDate;
|
|
605
|
+
function calcAge(birth, ref) {
|
|
606
|
+
if (ref === undefined) {
|
|
607
|
+
ref = new Date();
|
|
608
|
+
}
|
|
609
|
+
if (!birth) {
|
|
610
|
+
return 0;
|
|
611
|
+
}
|
|
612
|
+
let age = ref.getFullYear() - birth.getFullYear();
|
|
613
|
+
if (ref.getMonth() < birth.getMonth() || (ref.getMonth() == birth.getMonth() && ref.getDate() < birth.getDate())) {
|
|
614
|
+
age--;
|
|
615
|
+
}
|
|
616
|
+
return age;
|
|
617
|
+
}
|
|
618
|
+
exports.calcAge = calcAge;
|
|
619
|
+
Date.prototype.isSameDay = function (date) {
|
|
620
|
+
return this.getDate() == date.getDate() && this.getMonth() == date.getMonth() && this.getFullYear() == date.getFullYear();
|
|
621
|
+
};
|
|
622
|
+
Date.prototype.hash = function () {
|
|
623
|
+
return date_hash(this);
|
|
624
|
+
};
|
|
625
|
+
Date.prototype.clone = function () {
|
|
626
|
+
return date_clone(this);
|
|
627
|
+
};
|
|
628
|
+
Date.prototype.weekNum = function () {
|
|
629
|
+
return date_calc_weeknum(this);
|
|
630
|
+
};
|
|
631
|
+
Date.prototype.format = function (fmt) {
|
|
632
|
+
return formatIntlDate(this, fmt);
|
|
633
|
+
};
|
|
634
|
+
Date.prototype.addDays = function (days) {
|
|
635
|
+
this.setDate(this.getDate() + days);
|
|
636
|
+
return this;
|
|
637
|
+
};
|
|
638
|
+
// :: FILE CREATION ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
639
|
+
/**
|
|
640
|
+
*
|
|
641
|
+
* @param data data to export
|
|
642
|
+
* @param mimetype - 'text/plain'
|
|
643
|
+
*/
|
|
644
|
+
function downloadData(data, mimetype, filename) {
|
|
645
|
+
//if (data !== null && navigator.msSaveBlob) {
|
|
646
|
+
// return navigator.msSaveBlob(new Blob([data], { type: mimetype }), filename);
|
|
647
|
+
//}
|
|
648
|
+
let blob = new Blob([data], { type: mimetype });
|
|
649
|
+
let url = window.URL.createObjectURL(blob);
|
|
650
|
+
let a = document.createElement("a");
|
|
651
|
+
a.style['display'] = "none";
|
|
652
|
+
a.href = url;
|
|
653
|
+
a.download = filename;
|
|
654
|
+
document.body.appendChild(a);
|
|
655
|
+
a.click();
|
|
656
|
+
window.URL.revokeObjectURL(url);
|
|
657
|
+
}
|
|
658
|
+
exports.downloadData = downloadData;
|
|
659
|
+
// :: MISC ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
660
|
+
/**
|
|
661
|
+
* check if a value is a string
|
|
662
|
+
* @param val
|
|
663
|
+
*/
|
|
664
|
+
function isString(val) {
|
|
665
|
+
return typeof val === 'string';
|
|
666
|
+
}
|
|
667
|
+
exports.isString = isString;
|
|
668
|
+
/**
|
|
669
|
+
* check is a value is an array
|
|
670
|
+
* @param val
|
|
671
|
+
*/
|
|
672
|
+
function isArray(val) {
|
|
673
|
+
return val instanceof Array;
|
|
674
|
+
}
|
|
675
|
+
exports.isArray = isArray;
|
|
676
|
+
/**
|
|
677
|
+
*
|
|
678
|
+
*/
|
|
679
|
+
function isFunction(val) {
|
|
680
|
+
return val instanceof Function;
|
|
681
|
+
}
|
|
682
|
+
exports.isFunction = isFunction;
|
|
683
|
+
/**
|
|
684
|
+
*
|
|
685
|
+
*/
|
|
686
|
+
function isLiteralObject(val) {
|
|
687
|
+
return (!!val) && (val.constructor === Object);
|
|
688
|
+
}
|
|
689
|
+
exports.isLiteralObject = isLiteralObject;
|
|
690
|
+
;
|
|
691
|
+
/**
|
|
692
|
+
* prepend 0 to a value to a given length
|
|
693
|
+
* @param value
|
|
694
|
+
* @param length
|
|
695
|
+
*/
|
|
696
|
+
function pad(what, size, ch = '0') {
|
|
697
|
+
let value;
|
|
698
|
+
if (!isString(what)) {
|
|
699
|
+
value = '' + what;
|
|
700
|
+
}
|
|
701
|
+
else {
|
|
702
|
+
value = what;
|
|
703
|
+
}
|
|
704
|
+
if (size > 0) {
|
|
705
|
+
return value.padEnd(size, ch);
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
return value.padStart(-size, ch);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
exports.pad = pad;
|
|
712
|
+
/**
|
|
713
|
+
* return true if val is a finite number
|
|
714
|
+
*/
|
|
715
|
+
function isNumber(val) {
|
|
716
|
+
return typeof val === 'number' && isFinite(val);
|
|
717
|
+
}
|
|
718
|
+
exports.isNumber = isNumber;
|
|
719
|
+
/**
|
|
720
|
+
*
|
|
721
|
+
* @param name
|
|
722
|
+
*/
|
|
723
|
+
function waitFontLoading(name) {
|
|
724
|
+
// tip for waiting font loading:
|
|
725
|
+
// by default, body is created invisible ((opacity = 0)
|
|
726
|
+
// we create a div inside with the font we need to wait the loading
|
|
727
|
+
// as soon as the font is loaded, it's size will change, the browser end font loading
|
|
728
|
+
// we can remove the div.
|
|
729
|
+
// pitfall: if the font is already loaded, ne never end.
|
|
730
|
+
// @review that
|
|
731
|
+
let ct = document.createElement('div');
|
|
732
|
+
ct.style.position = 'absolute';
|
|
733
|
+
ct.style.fontFamily = name;
|
|
734
|
+
ct.style.fontSize = '44px';
|
|
735
|
+
ct.style.opacity = '0.001';
|
|
736
|
+
ct.innerText = 'X';
|
|
737
|
+
document.body.appendChild(ct);
|
|
738
|
+
return new Promise((resolve) => {
|
|
739
|
+
let irc = ct.getBoundingClientRect();
|
|
740
|
+
let tm = setInterval(() => {
|
|
741
|
+
let nrc = ct.getBoundingClientRect();
|
|
742
|
+
if (nrc.height != irc.height) {
|
|
743
|
+
clearInterval(tm);
|
|
744
|
+
document.body.removeChild(ct);
|
|
745
|
+
resolve();
|
|
746
|
+
}
|
|
747
|
+
}, 0);
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
exports.waitFontLoading = waitFontLoading;
|
|
751
|
+
/**
|
|
752
|
+
*
|
|
753
|
+
* @param fn
|
|
754
|
+
* @param tm
|
|
755
|
+
*
|
|
756
|
+
* @example:
|
|
757
|
+
*
|
|
758
|
+
* defer( ( ) => {
|
|
759
|
+
* console.log( x );
|
|
760
|
+
* } )( );
|
|
761
|
+
*/
|
|
762
|
+
function deferCall(fn, tm = 0, ...args) {
|
|
763
|
+
setTimeout(fn, tm, ...args);
|
|
764
|
+
}
|
|
765
|
+
exports.deferCall = deferCall;
|
|
766
|
+
/**
|
|
767
|
+
*
|
|
768
|
+
*/
|
|
769
|
+
function asap(cb) {
|
|
770
|
+
requestAnimationFrame(cb);
|
|
771
|
+
}
|
|
772
|
+
exports.asap = asap;
|
|
773
|
+
/**
|
|
774
|
+
* micro md to html
|
|
775
|
+
*
|
|
776
|
+
* understand:
|
|
777
|
+
* **bold**
|
|
778
|
+
* *italic*
|
|
779
|
+
*
|
|
780
|
+
* > quote
|
|
781
|
+
* - list
|
|
782
|
+
* # title lvl 1
|
|
783
|
+
* ## title lvl 2
|
|
784
|
+
* ### title lvl 3 ...
|
|
785
|
+
*
|
|
786
|
+
*/
|
|
787
|
+
function markdownToHtml(text) {
|
|
788
|
+
if (!text) {
|
|
789
|
+
return '';
|
|
790
|
+
}
|
|
791
|
+
let lines = text.split('\n');
|
|
792
|
+
let state = 'para';
|
|
793
|
+
let div = 'p';
|
|
794
|
+
let result = [];
|
|
795
|
+
lines.forEach((l) => {
|
|
796
|
+
let txt = l.trim();
|
|
797
|
+
if (state == 'para') {
|
|
798
|
+
// entree en mode list
|
|
799
|
+
if (txt[0] == '-') {
|
|
800
|
+
txt = txt.substr(1);
|
|
801
|
+
result.push('<ul>');
|
|
802
|
+
state = 'list';
|
|
803
|
+
div = 'li';
|
|
804
|
+
}
|
|
805
|
+
else if (txt[0] == '>') {
|
|
806
|
+
txt = txt.substr(1);
|
|
807
|
+
result.push('<blockquote>');
|
|
808
|
+
state = 'quote';
|
|
809
|
+
div = 'p';
|
|
810
|
+
}
|
|
811
|
+
else if (txt[0] == '#') {
|
|
812
|
+
let lvl = 0;
|
|
813
|
+
do {
|
|
814
|
+
txt = txt.substr(1);
|
|
815
|
+
lvl++;
|
|
816
|
+
} while (txt[0] == '#' && lvl < 5);
|
|
817
|
+
div = 'h' + lvl;
|
|
818
|
+
state = 'title';
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
else if (state == 'list') {
|
|
822
|
+
// sortie du mode list
|
|
823
|
+
if (txt[0] != '-') {
|
|
824
|
+
result.push('</ul>');
|
|
825
|
+
state = 'para';
|
|
826
|
+
div = 'p';
|
|
827
|
+
}
|
|
828
|
+
else {
|
|
829
|
+
txt = txt.substr(1);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
else if (state == 'quote') {
|
|
833
|
+
// sortie du mode blockquote
|
|
834
|
+
if (txt[0] != '>') {
|
|
835
|
+
result.push('</blockquote>');
|
|
836
|
+
state = 'para';
|
|
837
|
+
div = 'p';
|
|
838
|
+
}
|
|
839
|
+
else {
|
|
840
|
+
txt = txt.substr(1);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
let reBold = /\*\*([^*]+)\*\*/gi;
|
|
844
|
+
txt = escapeHtml(txt, false);
|
|
845
|
+
txt = txt.replace(reBold, (sub, ...a) => {
|
|
846
|
+
return '<b>' + sub.substr(2, sub.length - 4) + '</b>';
|
|
847
|
+
});
|
|
848
|
+
let reItalic = /\*([^*]+)\*/gi;
|
|
849
|
+
txt = txt.replace(reItalic, (sub, ...a) => {
|
|
850
|
+
return '<i>' + sub.substr(1, sub.length - 2) + '</i>';
|
|
851
|
+
});
|
|
852
|
+
// keep empty lines
|
|
853
|
+
if (txt == '') {
|
|
854
|
+
txt = ' ';
|
|
855
|
+
}
|
|
856
|
+
result.push(`<${div}>` + txt + `</${div}>`);
|
|
857
|
+
if (state == 'title') {
|
|
858
|
+
state = 'para';
|
|
859
|
+
div = 'p';
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
if (state == 'list') {
|
|
863
|
+
result.push('</ul>');
|
|
864
|
+
}
|
|
865
|
+
else if (state == 'quote') {
|
|
866
|
+
result.push('</blockquote>');
|
|
867
|
+
}
|
|
868
|
+
return result.join('');
|
|
869
|
+
}
|
|
870
|
+
exports.markdownToHtml = markdownToHtml;
|
|
871
|
+
/**
|
|
872
|
+
*
|
|
873
|
+
*/
|
|
874
|
+
class NetworkError extends Error {
|
|
875
|
+
m_code;
|
|
876
|
+
constructor(a, b) {
|
|
877
|
+
if (a instanceof Response) {
|
|
878
|
+
super(a.statusText);
|
|
879
|
+
this.m_code = a.status;
|
|
880
|
+
}
|
|
881
|
+
else {
|
|
882
|
+
super(b);
|
|
883
|
+
this.m_code = a;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
get code() {
|
|
887
|
+
return this.m_code;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
exports.NetworkError = NetworkError;
|
|
891
|
+
/**
|
|
892
|
+
* return the mouse pos in client coordinates
|
|
893
|
+
* handle correctly touch & mouse
|
|
894
|
+
*/
|
|
895
|
+
function getMousePos(ev, fromDoc) {
|
|
896
|
+
let x_name = 'offsetX', y_name = 'offsetY';
|
|
897
|
+
if (fromDoc) {
|
|
898
|
+
x_name = 'clientX';
|
|
899
|
+
y_name = 'clientY';
|
|
900
|
+
}
|
|
901
|
+
if (ev.type == 'mousemove' || ev.type == 'mousedown' || ev.type == 'mouseup') {
|
|
902
|
+
let em = ev;
|
|
903
|
+
return new Point(em[x_name], em[y_name]);
|
|
904
|
+
}
|
|
905
|
+
else if (ev.type == 'pointermove' || ev.type == 'pointerdown' || ev.type == 'pointerup') {
|
|
906
|
+
let em = ev;
|
|
907
|
+
return new Point(em[x_name], em[y_name]);
|
|
908
|
+
}
|
|
909
|
+
else if (ev.type == 'touchmove' || ev.type == 'touchstart') {
|
|
910
|
+
let et = ev;
|
|
911
|
+
return new Point(et.touches[0][x_name], et.touches[0][y_name]);
|
|
912
|
+
}
|
|
913
|
+
else if (ev.type == 'contextmenu') {
|
|
914
|
+
let em = ev;
|
|
915
|
+
return new Point(em[x_name], em[y_name]);
|
|
916
|
+
}
|
|
917
|
+
else {
|
|
918
|
+
return new Point(0, 0);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
exports.getMousePos = getMousePos;
|
|
922
|
+
/**
|
|
923
|
+
* clamp a value
|
|
924
|
+
* @param v - value to clamp
|
|
925
|
+
* @param min - min value
|
|
926
|
+
* @param max - max value
|
|
927
|
+
* @returns clamped value
|
|
928
|
+
*/
|
|
929
|
+
function clamp(v, min, max) {
|
|
930
|
+
return Math.min(Math.max(v, min), max);
|
|
931
|
+
}
|
|
932
|
+
exports.clamp = clamp;
|
|
933
|
+
// :: HTML strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
934
|
+
class HtmlString extends String {
|
|
935
|
+
constructor(text) { super(text); }
|
|
936
|
+
static from(text) {
|
|
937
|
+
return new HtmlString(text);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
exports.HtmlString = HtmlString;
|
|
941
|
+
function html(a, ...b) {
|
|
942
|
+
return HtmlString.from(String.raw(a, ...b));
|
|
943
|
+
}
|
|
944
|
+
exports.html = html;
|
|
945
|
+
function isHtmlString(val) {
|
|
946
|
+
return val instanceof HtmlString;
|
|
947
|
+
}
|
|
948
|
+
exports.isHtmlString = isHtmlString;
|
|
949
|
+
// :: CLIPBOARD ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
950
|
+
class Clipboard {
|
|
951
|
+
static copy(data) {
|
|
952
|
+
if (navigator.clipboard) {
|
|
953
|
+
navigator.clipboard.writeText(JSON.stringify(data));
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
static paste(cb) {
|
|
957
|
+
if (navigator.clipboard) {
|
|
958
|
+
navigator.clipboard.readText().then(v => cb(v));
|
|
959
|
+
}
|
|
960
|
+
else {
|
|
961
|
+
console.error('no clipboard, are you in https ?');
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
exports.Clipboard = Clipboard;
|
|
966
|
+
// :: CRC32 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
967
|
+
/**
|
|
968
|
+
* Calculates the CRC32 checksum of a string.
|
|
969
|
+
* taken from: https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
|
|
970
|
+
*
|
|
971
|
+
* @param {String} str
|
|
972
|
+
* @param {Boolean} hex
|
|
973
|
+
* @return {String} checksum
|
|
974
|
+
* @api public
|
|
975
|
+
*/
|
|
976
|
+
function crc32(str) {
|
|
977
|
+
let crc = ~0;
|
|
978
|
+
for (let i = 0, l = str.length; i < l; i++) {
|
|
979
|
+
crc = (crc >>> 8) ^ crc32tab[(crc ^ str.charCodeAt(i)) & 0xff];
|
|
980
|
+
}
|
|
981
|
+
return Math.abs(crc ^ -1);
|
|
982
|
+
}
|
|
983
|
+
exports.crc32 = crc32;
|
|
984
|
+
var crc32tab = [
|
|
985
|
+
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
|
|
986
|
+
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
|
|
987
|
+
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
|
988
|
+
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
|
|
989
|
+
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
|
|
990
|
+
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
|
991
|
+
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
|
|
992
|
+
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
|
|
993
|
+
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
|
994
|
+
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
|
995
|
+
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
|
|
996
|
+
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
|
997
|
+
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
|
|
998
|
+
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
|
|
999
|
+
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
|
1000
|
+
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
|
|
1001
|
+
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
|
|
1002
|
+
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
|
1003
|
+
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
|
|
1004
|
+
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
|
1005
|
+
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
|
1006
|
+
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
|
|
1007
|
+
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
|
|
1008
|
+
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
|
1009
|
+
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
|
|
1010
|
+
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
|
|
1011
|
+
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
|
1012
|
+
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
|
|
1013
|
+
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
|
|
1014
|
+
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
|
1015
|
+
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
|
|
1016
|
+
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
|
|
1017
|
+
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
|
1018
|
+
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
|
|
1019
|
+
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
|
|
1020
|
+
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
|
1021
|
+
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
|
|
1022
|
+
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
|
|
1023
|
+
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
|
1024
|
+
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
|
1025
|
+
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
|
|
1026
|
+
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
|
1027
|
+
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
|
|
1028
|
+
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
|
|
1029
|
+
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
|
1030
|
+
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
|
|
1031
|
+
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
|
|
1032
|
+
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
|
1033
|
+
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
|
|
1034
|
+
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
|
1035
|
+
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
|
1036
|
+
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
|
|
1037
|
+
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
|
|
1038
|
+
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
|
1039
|
+
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
|
|
1040
|
+
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
|
|
1041
|
+
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
|
1042
|
+
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
|
|
1043
|
+
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
|
|
1044
|
+
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
|
1045
|
+
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
|
|
1046
|
+
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
|
1047
|
+
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
|
1048
|
+
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
|
1049
|
+
];
|
|
1050
|
+
// :: MIXINS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
1051
|
+
/**
|
|
1052
|
+
* taken from this excellent article:
|
|
1053
|
+
* https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
|
|
1054
|
+
*
|
|
1055
|
+
* @example:
|
|
1056
|
+
* class MyClass extends mix(MyBaseClass).with(Mixin1, Mixin2) {
|
|
1057
|
+
* }
|
|
1058
|
+
**/
|
|
1059
|
+
const mix = (superclass) => new MixinBuilder(superclass);
|
|
1060
|
+
exports.mix = mix;
|
|
1061
|
+
class MixinBuilder {
|
|
1062
|
+
superclass;
|
|
1063
|
+
constructor(superclass) {
|
|
1064
|
+
this.superclass = superclass;
|
|
1065
|
+
}
|
|
1066
|
+
with(...mixins) {
|
|
1067
|
+
return mixins.reduce((c, mixin) => mixin(c), this.superclass);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* @example
|
|
1072
|
+
*
|
|
1073
|
+
* ```
|
|
1074
|
+
* const cls = classNames( 'class1 class2', {
|
|
1075
|
+
* 'class3': false,
|
|
1076
|
+
* 'class4': true,
|
|
1077
|
+
* });
|
|
1078
|
+
*
|
|
1079
|
+
* // even shorter
|
|
1080
|
+
* const class1 = true, class2 = false;
|
|
1081
|
+
* const cls = classNames( { class1, class2 } ); // cls = "class1"
|
|
1082
|
+
*
|
|
1083
|
+
* ```
|
|
1084
|
+
*
|
|
1085
|
+
* @returns
|
|
1086
|
+
*/
|
|
1087
|
+
function classNames(...args) {
|
|
1088
|
+
let result = '';
|
|
1089
|
+
for (const cls of args) {
|
|
1090
|
+
if (typeof cls === 'string') {
|
|
1091
|
+
result += ' ' + cls;
|
|
1092
|
+
}
|
|
1093
|
+
else if (cls) {
|
|
1094
|
+
for (const c in cls) {
|
|
1095
|
+
if (cls[c])
|
|
1096
|
+
result += ' ' + c;
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
return result;
|
|
1101
|
+
}
|
|
1102
|
+
exports.classNames = classNames;
|
|
1103
|
+
function generatePassword(length, rules) {
|
|
1104
|
+
if (!length || length == undefined) {
|
|
1105
|
+
length = 8;
|
|
1106
|
+
}
|
|
1107
|
+
if (!rules) {
|
|
1108
|
+
rules = [
|
|
1109
|
+
{ chars: "abcdefghijklmnopqrstuvwxyz", min: 3 },
|
|
1110
|
+
{ chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 2 },
|
|
1111
|
+
{ chars: "0123456789", min: 2 },
|
|
1112
|
+
{ chars: "!@#$*|%+-_.;", min: 2 } // At least 1 special char
|
|
1113
|
+
];
|
|
1114
|
+
}
|
|
1115
|
+
let allChars = "";
|
|
1116
|
+
let allMin = 0;
|
|
1117
|
+
rules.forEach(function (rule) {
|
|
1118
|
+
allChars += rule.chars;
|
|
1119
|
+
allMin += rule.min;
|
|
1120
|
+
});
|
|
1121
|
+
if (length < allMin) {
|
|
1122
|
+
length = allMin;
|
|
1123
|
+
}
|
|
1124
|
+
rules.push({ chars: allChars, min: length - allMin });
|
|
1125
|
+
let pswd = "";
|
|
1126
|
+
rules.forEach(function (rule) {
|
|
1127
|
+
if (rule.min > 0) {
|
|
1128
|
+
pswd += shuffle(rule.chars, rule.min);
|
|
1129
|
+
}
|
|
1130
|
+
});
|
|
1131
|
+
return shuffle(pswd);
|
|
1132
|
+
}
|
|
1133
|
+
exports.generatePassword = generatePassword;
|
|
1134
|
+
function shuffle(str, maxlength) {
|
|
1135
|
+
let shuffled = str.split('').sort(() => {
|
|
1136
|
+
return 0.5 - Math.random();
|
|
1137
|
+
}).join('');
|
|
1138
|
+
if (maxlength > 0) {
|
|
1139
|
+
shuffled = shuffled.substr(0, maxlength);
|
|
1140
|
+
}
|
|
1141
|
+
return shuffled;
|
|
1142
|
+
}
|