x4js 1.5.17 → 1.5.20

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.
Files changed (242) hide show
  1. package/lib/{README.md → README.txt} +0 -0
  2. package/lib/cjs/index.js +39 -103
  3. package/lib/cjs/index.js.map +7 -0
  4. package/lib/esm/index.mjs +39 -87
  5. package/lib/esm/index.mjs.map +7 -0
  6. package/lib/{license.md → licence.md} +0 -0
  7. package/lib/src/MIT-license.md +14 -0
  8. package/lib/{types/action.d.ts → src/action.ts} +64 -33
  9. package/lib/src/alpha.jpg +0 -0
  10. package/lib/src/app_sockets.ts +81 -0
  11. package/lib/src/application.ts +251 -0
  12. package/lib/src/autocomplete.ts +197 -0
  13. package/lib/src/base64.ts +166 -0
  14. package/lib/src/base_component.ts +152 -0
  15. package/lib/src/button.ts +355 -0
  16. package/lib/src/calendar.ts +328 -0
  17. package/lib/src/canvas.ts +505 -0
  18. package/lib/src/cardview.ts +227 -0
  19. package/lib/src/checkbox.ts +188 -0
  20. package/lib/src/color.ts +752 -0
  21. package/lib/src/colorpicker.ts +1639 -0
  22. package/lib/src/combobox.ts +465 -0
  23. package/lib/src/component.ts +2329 -0
  24. package/lib/{esm/dom_events.js → src/copyright.txt} +27 -29
  25. package/lib/src/datastore.ts +1322 -0
  26. package/lib/src/dialog.ts +656 -0
  27. package/lib/src/dom_events.ts +315 -0
  28. package/lib/src/drag_manager.ts +199 -0
  29. package/lib/src/drawtext.ts +355 -0
  30. package/lib/src/fileupload.ts +213 -0
  31. package/lib/src/form.ts +375 -0
  32. package/lib/src/formatters.ts +105 -0
  33. package/lib/src/gridview.ts +1185 -0
  34. package/lib/src/i18n.ts +346 -0
  35. package/lib/src/icon.ts +335 -0
  36. package/lib/src/image.ts +204 -0
  37. package/lib/src/index.ts +89 -0
  38. package/lib/src/input.ts +249 -0
  39. package/lib/src/label.ts +128 -0
  40. package/lib/src/layout.ts +430 -0
  41. package/lib/src/link.ts +86 -0
  42. package/lib/src/listview.ts +765 -0
  43. package/lib/{esm/md5.js → src/md5.ts} +438 -403
  44. package/lib/src/menu.ts +425 -0
  45. package/lib/src/messagebox.ts +224 -0
  46. package/lib/src/panel.ts +86 -0
  47. package/lib/src/popup.ts +494 -0
  48. package/lib/src/property_editor.ts +337 -0
  49. package/lib/src/radiobtn.ts +197 -0
  50. package/lib/src/rating.ts +135 -0
  51. package/lib/src/request.ts +300 -0
  52. package/lib/src/router.ts +185 -0
  53. package/lib/src/settings.ts +77 -0
  54. package/lib/src/sidebarview.ts +103 -0
  55. package/lib/src/spreadsheet.ts +1449 -0
  56. package/lib/src/styles.ts +343 -0
  57. package/lib/src/svgcomponent.ts +577 -0
  58. package/lib/src/tabbar.ts +151 -0
  59. package/lib/src/tabview.ts +110 -0
  60. package/lib/src/textarea.ts +235 -0
  61. package/lib/src/textedit.ts +544 -0
  62. package/lib/src/toaster.ts +80 -0
  63. package/lib/src/tools.ts +1473 -0
  64. package/lib/src/tooltips.ts +191 -0
  65. package/lib/src/treeview.ts +716 -0
  66. package/lib/{esm/version.js → src/version.ts} +30 -29
  67. package/lib/{styles → src}/x4.less +0 -0
  68. package/lib/src/x4dom.ts +57 -0
  69. package/lib/src/x4events.ts +585 -0
  70. package/lib/src/x4react.ts +90 -0
  71. package/package.json +52 -49
  72. package/lib/cjs/action.js +0 -58
  73. package/lib/cjs/app_sockets.js +0 -74
  74. package/lib/cjs/application.js +0 -182
  75. package/lib/cjs/autocomplete.js +0 -155
  76. package/lib/cjs/base_component.js +0 -124
  77. package/lib/cjs/button.js +0 -263
  78. package/lib/cjs/calendar.js +0 -254
  79. package/lib/cjs/canvas.js +0 -361
  80. package/lib/cjs/cardview.js +0 -161
  81. package/lib/cjs/checkbox.js +0 -139
  82. package/lib/cjs/color.js +0 -591
  83. package/lib/cjs/colorpicker.js +0 -1468
  84. package/lib/cjs/combobox.js +0 -349
  85. package/lib/cjs/component.js +0 -1719
  86. package/lib/cjs/datastore.js +0 -985
  87. package/lib/cjs/dialog.js +0 -478
  88. package/lib/cjs/dom_events.js +0 -30
  89. package/lib/cjs/drag_manager.js +0 -145
  90. package/lib/cjs/drawtext.js +0 -276
  91. package/lib/cjs/fileupload.js +0 -167
  92. package/lib/cjs/form.js +0 -304
  93. package/lib/cjs/formatters.js +0 -90
  94. package/lib/cjs/gridview.js +0 -881
  95. package/lib/cjs/i18n.js +0 -266
  96. package/lib/cjs/icon.js +0 -272
  97. package/lib/cjs/image.js +0 -157
  98. package/lib/cjs/input.js +0 -174
  99. package/lib/cjs/label.js +0 -95
  100. package/lib/cjs/layout.js +0 -322
  101. package/lib/cjs/link.js +0 -64
  102. package/lib/cjs/listview.js +0 -546
  103. package/lib/cjs/md5.js +0 -403
  104. package/lib/cjs/menu.js +0 -301
  105. package/lib/cjs/messagebox.js +0 -177
  106. package/lib/cjs/panel.js +0 -68
  107. package/lib/cjs/popup.js +0 -383
  108. package/lib/cjs/property_editor.js +0 -252
  109. package/lib/cjs/radiobtn.js +0 -140
  110. package/lib/cjs/rating.js +0 -102
  111. package/lib/cjs/request.js +0 -246
  112. package/lib/cjs/router.js +0 -139
  113. package/lib/cjs/settings.js +0 -69
  114. package/lib/cjs/sidebarview.js +0 -82
  115. package/lib/cjs/spreadsheet.js +0 -1084
  116. package/lib/cjs/styles.js +0 -275
  117. package/lib/cjs/svgcomponent.js +0 -447
  118. package/lib/cjs/tabbar.js +0 -111
  119. package/lib/cjs/tabview.js +0 -85
  120. package/lib/cjs/textarea.js +0 -165
  121. package/lib/cjs/textedit.js +0 -415
  122. package/lib/cjs/toaster.js +0 -64
  123. package/lib/cjs/tools.js +0 -1197
  124. package/lib/cjs/tooltips.js +0 -157
  125. package/lib/cjs/treeview.js +0 -535
  126. package/lib/cjs/version.js +0 -32
  127. package/lib/cjs/x4dom.js +0 -32
  128. package/lib/cjs/x4events.js +0 -384
  129. package/lib/cjs/x4react.js +0 -72
  130. package/lib/esm/action.js +0 -53
  131. package/lib/esm/app_sockets.js +0 -70
  132. package/lib/esm/application.js +0 -184
  133. package/lib/esm/autocomplete.js +0 -155
  134. package/lib/esm/base_component.js +0 -107
  135. package/lib/esm/button.js +0 -257
  136. package/lib/esm/calendar.js +0 -249
  137. package/lib/esm/canvas.js +0 -358
  138. package/lib/esm/cardview.js +0 -159
  139. package/lib/esm/checkbox.js +0 -134
  140. package/lib/esm/color.js +0 -588
  141. package/lib/esm/colorpicker.js +0 -1475
  142. package/lib/esm/combobox.js +0 -351
  143. package/lib/esm/component.js +0 -1711
  144. package/lib/esm/datastore.js +0 -990
  145. package/lib/esm/dialog.js +0 -490
  146. package/lib/esm/drag_manager.js +0 -147
  147. package/lib/esm/drawtext.js +0 -271
  148. package/lib/esm/fileupload.js +0 -163
  149. package/lib/esm/form.js +0 -304
  150. package/lib/esm/formatters.js +0 -81
  151. package/lib/esm/gridview.js +0 -889
  152. package/lib/esm/i18n.js +0 -257
  153. package/lib/esm/icon.js +0 -261
  154. package/lib/esm/image.js +0 -154
  155. package/lib/esm/input.js +0 -182
  156. package/lib/esm/label.js +0 -90
  157. package/lib/esm/layout.js +0 -310
  158. package/lib/esm/link.js +0 -59
  159. package/lib/esm/listview.js +0 -534
  160. package/lib/esm/menu.js +0 -300
  161. package/lib/esm/messagebox.js +0 -161
  162. package/lib/esm/panel.js +0 -65
  163. package/lib/esm/popup.js +0 -379
  164. package/lib/esm/property_editor.js +0 -251
  165. package/lib/esm/radiobtn.js +0 -136
  166. package/lib/esm/rating.js +0 -97
  167. package/lib/esm/request.js +0 -224
  168. package/lib/esm/router.js +0 -136
  169. package/lib/esm/settings.js +0 -66
  170. package/lib/esm/sidebarview.js +0 -79
  171. package/lib/esm/spreadsheet.js +0 -1097
  172. package/lib/esm/styles.js +0 -270
  173. package/lib/esm/svgcomponent.js +0 -450
  174. package/lib/esm/tabbar.js +0 -107
  175. package/lib/esm/tabview.js +0 -83
  176. package/lib/esm/textarea.js +0 -160
  177. package/lib/esm/textedit.js +0 -412
  178. package/lib/esm/toaster.js +0 -62
  179. package/lib/esm/tools.js +0 -1158
  180. package/lib/esm/tooltips.js +0 -153
  181. package/lib/esm/treeview.js +0 -527
  182. package/lib/esm/x4dom.js +0 -29
  183. package/lib/esm/x4events.js +0 -376
  184. package/lib/esm/x4react.js +0 -71
  185. package/lib/types/app_sockets.d.ts +0 -29
  186. package/lib/types/application.d.ts +0 -104
  187. package/lib/types/autocomplete.d.ts +0 -58
  188. package/lib/types/base_component.d.ts +0 -88
  189. package/lib/types/button.d.ts +0 -151
  190. package/lib/types/calendar.d.ts +0 -82
  191. package/lib/types/canvas.d.ts +0 -92
  192. package/lib/types/cardview.d.ts +0 -87
  193. package/lib/types/checkbox.d.ts +0 -77
  194. package/lib/types/color.d.ts +0 -148
  195. package/lib/types/colorpicker.d.ts +0 -107
  196. package/lib/types/combobox.d.ts +0 -107
  197. package/lib/types/component.d.ts +0 -601
  198. package/lib/types/datastore.d.ts +0 -396
  199. package/lib/types/dialog.d.ts +0 -175
  200. package/lib/types/dom_events.d.ts +0 -302
  201. package/lib/types/drag_manager.d.ts +0 -54
  202. package/lib/types/drawtext.d.ts +0 -48
  203. package/lib/types/fileupload.d.ts +0 -64
  204. package/lib/types/form.d.ts +0 -133
  205. package/lib/types/formatters.d.ts +0 -35
  206. package/lib/types/gridview.d.ts +0 -175
  207. package/lib/types/i18n.d.ts +0 -73
  208. package/lib/types/icon.d.ts +0 -64
  209. package/lib/types/image.d.ts +0 -55
  210. package/lib/types/index.d.ts +0 -84
  211. package/lib/types/input.d.ts +0 -91
  212. package/lib/types/label.d.ts +0 -58
  213. package/lib/types/layout.d.ts +0 -87
  214. package/lib/types/link.d.ts +0 -50
  215. package/lib/types/listview.d.ts +0 -178
  216. package/lib/types/md5.d.ts +0 -61
  217. package/lib/types/menu.d.ts +0 -130
  218. package/lib/types/messagebox.d.ts +0 -69
  219. package/lib/types/panel.d.ts +0 -47
  220. package/lib/types/popup.d.ts +0 -75
  221. package/lib/types/property_editor.d.ts +0 -71
  222. package/lib/types/radiobtn.d.ts +0 -73
  223. package/lib/types/rating.d.ts +0 -53
  224. package/lib/types/request.d.ts +0 -52
  225. package/lib/types/router.d.ts +0 -42
  226. package/lib/types/settings.d.ts +0 -37
  227. package/lib/types/sidebarview.d.ts +0 -49
  228. package/lib/types/spreadsheet.d.ts +0 -222
  229. package/lib/types/styles.d.ts +0 -85
  230. package/lib/types/svgcomponent.d.ts +0 -215
  231. package/lib/types/tabbar.d.ts +0 -58
  232. package/lib/types/tabview.d.ts +0 -49
  233. package/lib/types/textarea.d.ts +0 -77
  234. package/lib/types/textedit.d.ts +0 -123
  235. package/lib/types/toaster.d.ts +0 -42
  236. package/lib/types/tools.d.ts +0 -394
  237. package/lib/types/tooltips.d.ts +0 -46
  238. package/lib/types/treeview.d.ts +0 -144
  239. package/lib/types/version.d.ts +0 -29
  240. package/lib/types/x4dom.d.ts +0 -49
  241. package/lib/types/x4events.d.ts +0 -269
  242. package/lib/types/x4react.d.ts +0 -41
@@ -0,0 +1,1473 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file tools.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * Copyright (c) 2019-2022 R-libre ingenierie
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ * of this software and associated documentation files (the "Software"), to deal
15
+ * in the Software without restriction, including without limitation the rights
16
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
17
+ * of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ * The above copyright notice and this permission notice shall be included in all copies
20
+ * or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
23
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24
+ * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ **/
29
+
30
+ import { x4document } from './x4dom'
31
+
32
+ import { _tr } from './i18n'; // you MUST create a file named translation.js
33
+
34
+ // :: ENVIRONMENT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35
+
36
+ /**
37
+ * @return true is the device has touch
38
+ */
39
+
40
+ export function isTouchDevice() {
41
+ return 'ontouchstart' in window;
42
+ }
43
+
44
+ // :: NUMBERS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
45
+
46
+
47
+ /**
48
+ * round to a given number of decimals
49
+ * @param num numbre to round
50
+ * @param ndec number of decimals
51
+ * @returns number rounded
52
+ */
53
+
54
+ export function roundTo(num: number, ndec: number) {
55
+ let mul = Math.pow(10, ndec);
56
+ let res = Math.round(num * mul) / mul;
57
+ if (Object.is(res, -0)) { // res===-0.0
58
+ res = 0;
59
+ }
60
+ return res;
61
+ }
62
+
63
+ /**
64
+ * parse an intl formatted number
65
+ * understand grouping and ',' separator
66
+ * @review us format: grouping = ','
67
+ * @param num
68
+ */
69
+
70
+ export function parseIntlFloat(num: string) {
71
+ num = num.replace(/\s*/g, ''); // group separator
72
+ num = num.replace(',', '.'); // decimal separator
73
+
74
+ // accept empty & return 0.0
75
+ if (num.length == 0) {
76
+ return 0.0;
77
+ }
78
+
79
+ return parseFloat(num);
80
+ }
81
+
82
+ // :: STRING MANIP ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
83
+
84
+ const RE_PCASE = /([a-z][A-Z])/g;
85
+ const RE_PCASE2 = /[^-a-z0-9]+/g;
86
+
87
+ /**
88
+ * inverse of camel case
89
+ * theThingToCase -> the-thing-to-case
90
+ * @param {String} str
91
+ */
92
+
93
+ export function pascalCase(string: string): string {
94
+
95
+ let result = string;
96
+
97
+ result = result.replace(/([a-z])([A-Z])/g, "$1 $2");
98
+ result = result.toLowerCase();
99
+ result = result.replace(/[^- a-z0-9]+/g, ' ');
100
+
101
+ if (result.indexOf(' ') < 0) {
102
+ return result;
103
+ }
104
+
105
+ result = result.trim();
106
+ return result.replace(/ /g, '-');
107
+ }
108
+
109
+ export function camelCase( text: string ) {
110
+ let result = text.toLowerCase( );
111
+ result = result.replace( /[^a-zA-Z0-9]+(.)/g, (m,chr) => {
112
+ return chr.toUpperCase();
113
+ } );
114
+ return result;
115
+ }
116
+
117
+ // :: MISC CLASSES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
118
+
119
+ export type Constructor<T> = new (...args: any[]) => T;
120
+
121
+ export class Point {
122
+ x: number;
123
+ y: number;
124
+
125
+ constructor(x = 0, y = 0) {
126
+ this.x = x;
127
+ this.y = y;
128
+ }
129
+ }
130
+
131
+ export class Size {
132
+ width: number;
133
+ height: number;
134
+
135
+ constructor(w = 0, h = 0) {
136
+ this.width = w;
137
+ this.height = h;
138
+ }
139
+ }
140
+
141
+ export class Rect {
142
+
143
+ left: number;
144
+ top: number;
145
+ width: number;
146
+ height: number;
147
+
148
+ constructor();
149
+ constructor(rc: Rect);
150
+ constructor(rc: DOMRect);
151
+ constructor(left: number, top: number, width: number, height: number);
152
+ constructor(left?: number | Rect | DOMRect, top?: number, width?: number, height?: number) {
153
+
154
+ if (left === undefined) {
155
+ this.left = this.top = this.right = this.bottom = 0;
156
+ }
157
+ else if (left instanceof Rect || left instanceof DOMRect) {
158
+ let rc = left;
159
+ this.left = rc.left;
160
+ this.top = rc.top;
161
+ this.width = rc.width;
162
+ this.height = rc.height;
163
+ }
164
+ else {
165
+ this.left = left;
166
+ this.top = top;
167
+ this.width = width;
168
+ this.height = height;
169
+ }
170
+ }
171
+
172
+ set(left: number, top: number, width: number, height: number) {
173
+ this.left = left;
174
+ this.top = top;
175
+ this.width = width;
176
+ this.height = height;
177
+ }
178
+
179
+ get bottom(): number {
180
+ return this.top + this.height;
181
+ }
182
+
183
+ set bottom(bottom: number) {
184
+ this.height = bottom - this.top;
185
+ }
186
+
187
+ get right(): number {
188
+ return this.left + this.width;
189
+ }
190
+
191
+ set right(right: number) {
192
+ this.width = right - this.left;
193
+ }
194
+
195
+ get topLeft(): Point {
196
+ return new Point(this.left, this.top);
197
+ }
198
+
199
+ get bottomRight(): Point {
200
+ return new Point(this.right, this.bottom);
201
+ }
202
+
203
+ get size(): Size {
204
+ return new Size(this.width, this.height);
205
+ }
206
+
207
+ moveTo(left: number, top: number): this {
208
+ this.left = left;
209
+ this.top = top;
210
+ return this;
211
+ }
212
+
213
+ movedTo(left: number, top: number): Rect {
214
+ return new Rect(left, top, this.width, this.height);
215
+ }
216
+
217
+ moveBy(dx: number, dy: number): this {
218
+ this.left += dx;
219
+ this.top += dy;
220
+ return this;
221
+ }
222
+
223
+ movedBy(dx: number, dy: number): Rect {
224
+ return new Rect(this.left + dx, this.top + dy, this.width, this.height);
225
+ }
226
+
227
+ isEmpty() {
228
+ return this.width == 0 && this.height == 0;
229
+ }
230
+
231
+ normalize(): this {
232
+ let w = this.width,
233
+ h = this.height;
234
+
235
+ if (w < 0) {
236
+ this.left += w;
237
+ this.width = -w;
238
+ }
239
+
240
+ if (h < 0) {
241
+ this.top += h;
242
+ this.height = -h;
243
+ }
244
+
245
+ return this;
246
+ }
247
+
248
+ normalized(): Rect {
249
+
250
+ let rc = new Rect(this);
251
+ let w = rc.width,
252
+ h = rc.height;
253
+
254
+
255
+ if (w < 0) {
256
+ rc.left += w;
257
+ rc.width = -w;
258
+ }
259
+
260
+ if (h < 0) {
261
+ rc.top += h;
262
+ rc.height = -h;
263
+ }
264
+
265
+ return rc;
266
+ }
267
+
268
+ /**
269
+ * @deprecated
270
+ */
271
+ containsPt(x: number, y: number): boolean {
272
+ return x >= this.left && x <= this.right && y >= this.top && y <= this.bottom;
273
+ }
274
+
275
+ contains(pt: Point): boolean;
276
+ contains(rc: Rect): boolean;
277
+ contains(arg: any): boolean {
278
+ if (arg instanceof Rect) {
279
+ return arg.left >= this.left && arg.right <= this.right && arg.top >= this.top && arg.bottom <= this.bottom;
280
+ }
281
+ else {
282
+ return arg.x >= this.left && arg.x < this.right && arg.y >= this.top && arg.y < this.bottom;
283
+ }
284
+ }
285
+
286
+ touches(rc: Rect): boolean {
287
+ if (this.left > rc.right || this.right < rc.left || this.top > rc.bottom || this.bottom < rc.top) {
288
+ return false;
289
+ }
290
+
291
+ return true;
292
+ }
293
+
294
+ inflate(dx: number, dy?: number) {
295
+ if (dy === undefined) {
296
+ dy = dx;
297
+ }
298
+
299
+ this.left -= dx;
300
+ this.top -= dy;
301
+ this.width += dx + dx;
302
+ this.height += dy + dy;
303
+ }
304
+
305
+ inflatedBy(dx: number, dy?: number) {
306
+ if (dy === undefined) {
307
+ dy = dx;
308
+ }
309
+ return new Rect(this.left - dx, this.top - dy, this.width + dx + dx, this.height + dy + dy);
310
+ }
311
+
312
+ combine(rc: Rect) {
313
+ let left = Math.min(this.left, rc.left);
314
+ let top = Math.min(this.top, rc.top);
315
+ let right = Math.max(this.right, rc.right);
316
+ let bottom = Math.max(this.bottom, rc.bottom);
317
+
318
+ this.left = left;
319
+ this.top = top;
320
+ this.right = right;
321
+ this.bottom = bottom;
322
+ }
323
+ }
324
+
325
+ /**
326
+ * replace {0..9} by given arguments
327
+ * @param format string
328
+ * @param args
329
+ *
330
+ * @example ```ts
331
+ *
332
+ * console.log( sprintf( 'here is arg 1 {1} and arg 0 {0}', 'argument 0', 'argument 1' ) )
333
+ */
334
+
335
+ export function sprintf(format: string, ...args) {
336
+ return format.replace(/{(\d+)}/g, function (match, index) {
337
+ return typeof args[index] != 'undefined' ? args[index] : match;
338
+ });
339
+ }
340
+
341
+ /**
342
+ * replace special characters for display
343
+ * @param unsafe
344
+ *
345
+ * console.log( escapeHtml('<div style="width:50px; height: 50px; background-color:red"></div>') );
346
+ */
347
+ export function escapeHtml(unsafe: string, nl_br = false): string {
348
+ if (!unsafe || unsafe.length == 0) {
349
+ return "";
350
+ }
351
+
352
+ let result = unsafe.replace(/[<>\&\"\']/g, function (c) {
353
+ return '&#' + c.charCodeAt(0) + ';';
354
+ });
355
+
356
+ if (nl_br) {
357
+ result = result.replace(/(\r\n|\n\r|\r|\n)/g, '<br/>');
358
+ }
359
+
360
+ return result;
361
+ }
362
+
363
+ /**
364
+ * replace special characters for display
365
+ * @author Steven Levithan <http://slevithan.com/>
366
+ * @param unsafe
367
+ *
368
+ * console.log( removeHtmlTags('<h1>sss</h1>') );
369
+ */
370
+
371
+ export function removeHtmlTags(unsafe: string, nl_br = false): string {
372
+
373
+ if ( unsafe===undefined || unsafe===null || !isString(unsafe) || unsafe.length == 0 ) {
374
+ return "";
375
+ }
376
+
377
+ let ret_val = '';
378
+ for (let i = 0; i < unsafe.length; i++) {
379
+ const ch = unsafe.codePointAt(i);
380
+ if (ch > 127) {
381
+ ret_val += '&#' + ch + ';';
382
+ }
383
+ else if (ch == 60/*<*/) {
384
+ ret_val += '&lt;'
385
+ }
386
+ else {
387
+ ret_val += unsafe.charAt(i);
388
+ }
389
+ }
390
+ return ret_val;
391
+ }
392
+
393
+
394
+ // :: DATES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
395
+
396
+ let cur_locale: string = 'fr-FR';
397
+
398
+ /**
399
+ * change the current locale for misc translations (date...)
400
+ * @param locale
401
+ */
402
+
403
+ export function _date_set_locale(locale: string) {
404
+ cur_locale = locale;
405
+ }
406
+
407
+ /**
408
+ *
409
+ * @param date
410
+ * @param options
411
+ * @example
412
+ * let date = new Date( );
413
+ * let options = { day: 'numeric', month: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric' };
414
+ * let text = date_format( date, options );
415
+ */
416
+
417
+ export function date_format(date: Date, options?: any): string {
418
+ //return new Intl.DateTimeFormat(cur_locale, options).format( date );
419
+ return formatIntlDate(date);
420
+ }
421
+
422
+ /**
423
+ *
424
+ * @param date
425
+ * @param options
426
+ */
427
+
428
+ export function date_diff(date1: Date, date2: Date, options?: any): string {
429
+
430
+ var dt = (date1.getTime() - date2.getTime()) / 1000;
431
+
432
+ // seconds
433
+ let sec = dt;
434
+ if (sec < 60) {
435
+ return sprintf(_tr.global.diff_date_seconds, Math.round(sec));
436
+ }
437
+
438
+ // minutes
439
+ let min = Math.floor(sec / 60);
440
+ if (min < 60) {
441
+ return sprintf(_tr.global.diff_date_minutes, Math.round(min));
442
+ }
443
+
444
+ // hours
445
+ let hrs = Math.floor(min / 60);
446
+ return sprintf(_tr.global.diff_date_hours, hrs, min % 60);
447
+ }
448
+
449
+ export function date_to_sql(date: Date, withHours: boolean) {
450
+
451
+ if (withHours) {
452
+ return formatIntlDate(date, 'Y-M-D H:I:S');
453
+ }
454
+ else {
455
+ return formatIntlDate(date, 'Y-M-D');
456
+ }
457
+ }
458
+
459
+ /**
460
+ * construct a date from an utc date time (sql format)
461
+ * YYYY-MM-DD HH:MM:SS
462
+ */
463
+
464
+ export function date_sql_utc(date: string): Date {
465
+ let result = new Date(date + ' GMT');
466
+ return result;
467
+ }
468
+
469
+
470
+
471
+ /**
472
+ * return a number that is a representation of the date
473
+ * this number can be compared with another hash
474
+ */
475
+
476
+ export function date_hash(date: Date): number {
477
+ return date.getFullYear() << 16 | date.getMonth() << 8 | date.getDate();
478
+ }
479
+
480
+ (<any>Date.prototype).hash = function (this: Date) {
481
+ return date_hash(this);
482
+ }
483
+
484
+ /**
485
+ * return a copy of a date
486
+ */
487
+
488
+ export function date_clone(date: Date): Date {
489
+ return new Date(date.getTime());
490
+ }
491
+
492
+ /**
493
+ * return the week number of a date
494
+ */
495
+
496
+ export function date_calc_weeknum(date: Date): number {
497
+ const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
498
+ const pastDaysOfYear = (date.valueOf() - firstDayOfYear.valueOf()) / 86400000;
499
+ return Math.floor((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
500
+ }
501
+
502
+
503
+
504
+ /**
505
+ * parse a date according to the given format
506
+ * @param value - string date to parse
507
+ * @param fmts - format list - i18 tranlation by default
508
+ * allowed format specifiers:
509
+ * d or D: date (1 or 2 digits)
510
+ * m or M: month (1 or 2 digits)
511
+ * y or Y: year (2 or 4 digits)
512
+ * h or H: hours (1 or 2 digits)
513
+ * i or I: minutes (1 or 2 digits)
514
+ * s or S: seconds (1 or 2 digits)
515
+ * <space>: 1 or more spaces
516
+ * any other char: <0 or more spaces><the char><0 or more spaces>
517
+ * each specifiers is separated from other by a pipe (|)
518
+ * more specific at first
519
+ * @example
520
+ * 'd/m/y|d m Y|dmy|y-m-d h:i:s|y-m-d'
521
+ */
522
+
523
+ export function parseIntlDate(value: string, fmts: string = _tr.global.date_input_formats): Date {
524
+
525
+ let formats = fmts.split('|');
526
+ for (let fmatch of formats) {
527
+
528
+ //review: could do that only once & keep result
529
+ //review: add hours, minutes, seconds
530
+
531
+ let smatch = '';
532
+ for (let c of fmatch) {
533
+
534
+ if (c == 'd' || c == 'D') {
535
+ smatch += '(?<day>\\d{1,2})';
536
+ }
537
+ else if (c == 'm' || c == 'M') {
538
+ smatch += '(?<month>\\d{1,2})';
539
+ }
540
+ else if (c == 'y' || c == 'Y') {
541
+ smatch += '(?<year>\\d{1,4})';
542
+ }
543
+ else if (c == 'h' || c == 'H') {
544
+ smatch += '(?<hour>\\d{1,2})';
545
+ }
546
+ else if (c == 'i' || c == 'I') {
547
+ smatch += '(?<min>\\d{1,2})';
548
+ }
549
+ else if (c == 's' || c == 'S') {
550
+ smatch += '(?<sec>\\d{1,2})';
551
+ }
552
+ else if (c == ' ') {
553
+ smatch += '\\s+';
554
+ }
555
+ else {
556
+ smatch += '\\s*\\' + c + '\\s*';
557
+ }
558
+ }
559
+
560
+ let rematch = new RegExp('^' + smatch + '$', 'm');
561
+
562
+ let match = rematch.exec(value);
563
+
564
+ if (match) {
565
+ const now = new Date( );
566
+
567
+ let d = parseInt(match.groups.day ?? '1');
568
+ let m = parseInt(match.groups.month ?? '1');
569
+ let y = parseInt(match.groups.year ?? now.getFullYear()+'');
570
+ let h = parseInt(match.groups.hour ?? '0');
571
+ let i = parseInt(match.groups.min ?? '0');
572
+ let s = parseInt(match.groups.sec ?? '0');
573
+
574
+ if (y > 0 && y < 100) {
575
+ y += 2000;
576
+ }
577
+
578
+ let result = new Date(y, m - 1, d, h, i, s, 0);
579
+
580
+ // we test the vdate validity (without adjustments)
581
+ // without this test, date ( 0, 0, 0) is accepted and transformed to 1969/11/31 (not fun)
582
+ let ty = result.getFullYear(),
583
+ tm = result.getMonth() + 1,
584
+ td = result.getDate();
585
+
586
+ if (ty != y || tm != m || td != d) {
587
+ //debugger;
588
+ return null;
589
+ }
590
+
591
+ return result;
592
+ }
593
+ }
594
+
595
+ return null;
596
+ }
597
+
598
+ /**
599
+ * format a date as string
600
+ * @param date - date to format
601
+ * @param fmt - format
602
+ * format specifiers:
603
+ * d: date
604
+ * D: 2 digits date padded with 0
605
+ * j: day of week short mode 'mon'
606
+ * J: day of week long mode 'monday'
607
+ * w: week number
608
+ * m: month
609
+ * M: 2 digits month padded with 0
610
+ * o: month short mode 'jan'
611
+ * O: month long mode 'january'
612
+ * y or Y: year
613
+ * h: hour (24 format)
614
+ * H: 2 digits hour (24 format) padded with 0
615
+ * i: minutes
616
+ * I: 2 digits minutes padded with 0
617
+ * s: seconds
618
+ * S: 2 digits seconds padded with 0
619
+ * a: am or pm
620
+ * anything else is inserted
621
+ * if you need to insert some text, put it between {}
622
+ *
623
+ * @example
624
+ *
625
+ * 01/01/1970 11:25:00 with '{this is my demo date formatter: }H-i*M'
626
+ * "this is my demo date formatter: 11-25*january"
627
+ */
628
+
629
+ export function formatIntlDate(date: Date, fmt: string = _tr.global.date_format) {
630
+
631
+ if (!date) {
632
+ return '';
633
+ }
634
+
635
+ let now = {
636
+ year: date.getFullYear(),
637
+ month: date.getMonth() + 1,
638
+ day: date.getDate(),
639
+ wday: date.getDay(),
640
+ hours: date.getHours(),
641
+ minutes: date.getMinutes(),
642
+ seconds: date.getSeconds(),
643
+ milli: date.getMilliseconds()
644
+ };
645
+
646
+
647
+ let result = '';
648
+ let esc = 0;
649
+
650
+ for (let c of fmt) {
651
+
652
+ if (c == '{') {
653
+ if (++esc == 1) {
654
+ continue;
655
+ }
656
+ }
657
+ else if (c == '}') {
658
+ if (--esc == 0) {
659
+ continue;
660
+ }
661
+ }
662
+
663
+ if (esc) {
664
+ result += c;
665
+ continue;
666
+ }
667
+
668
+ if (c == 'd') {
669
+ result += now.day;
670
+ }
671
+ else if (c == 'D') {
672
+ result += pad(now.day, -2);
673
+ }
674
+ else if (c == 'j') { // day short
675
+ result += _tr.global.day_short[now.wday];
676
+ }
677
+ else if (c == 'J') { // day long
678
+ result += _tr.global.day_long[now.wday];
679
+ }
680
+ else if (c == 'w') { // week
681
+ result += date_calc_weeknum(date);
682
+ }
683
+ else if (c == 'W') { // week
684
+ result += pad(date_calc_weeknum(date), -2);
685
+ }
686
+ else if (c == 'm') {
687
+ result += now.month;
688
+ }
689
+ else if (c == 'M') {
690
+ result += pad(now.month, -2);
691
+ }
692
+ else if (c == 'o') { // month short
693
+ result += _tr.global.month_short[now.month - 1];
694
+ }
695
+ else if (c == 'O') { // month long
696
+ result += _tr.global.month_long[now.month - 1];
697
+ }
698
+ else if (c == 'y' || c == 'Y') {
699
+ result += pad(now.year, -4);
700
+ }
701
+ else if (c == 'a' || c == 'A') {
702
+ result += now.hours < 12 ? 'am' : 'pm';
703
+ }
704
+ else if (c == 'h') {
705
+ result += now.hours;
706
+ }
707
+ else if (c == 'H') {
708
+ result += pad(now.hours, -2);
709
+ }
710
+ else if (c == 'i') {
711
+ result += now.minutes;
712
+ }
713
+ else if (c == 'I') {
714
+ result += pad(now.minutes, -2);
715
+ }
716
+ else if (c == 's') {
717
+ result += now.seconds;
718
+ }
719
+ else if (c == 'S') {
720
+ result += pad(now.seconds, -2);
721
+ }
722
+ else if (c == 'l') {
723
+ result += now.milli;
724
+ }
725
+ else if (c == 'L') {
726
+ result += pad(now.milli, -3);
727
+ }
728
+ else {
729
+ result += c;
730
+ }
731
+ }
732
+
733
+ return result;
734
+ }
735
+
736
+ export function calcAge(birth: Date, ref?: Date) {
737
+ if (ref === undefined) {
738
+ ref = new Date();
739
+ }
740
+
741
+ if (!birth) {
742
+ return 0;
743
+ }
744
+
745
+ let age = ref.getFullYear() - birth.getFullYear();
746
+ if (ref.getMonth() < birth.getMonth() || (ref.getMonth() == birth.getMonth() && ref.getDate() < birth.getDate())) {
747
+ age--;
748
+ }
749
+
750
+ return age;
751
+ }
752
+
753
+
754
+ /**
755
+ * date object patch
756
+ */
757
+
758
+ declare global {
759
+ interface Date {
760
+ hash(): number;
761
+ clone(): Date;
762
+ weekNum(): number;
763
+ format(format: string): string;
764
+ isSameDay(date: Date): boolean;
765
+ addDays( days: number );
766
+ }
767
+ }
768
+
769
+ Date.prototype.isSameDay = function (date: Date) {
770
+ return this.getDate() == date.getDate() && this.getMonth() == date.getMonth() && this.getFullYear() == date.getFullYear();
771
+ }
772
+
773
+ Date.prototype.hash = function () {
774
+ return date_hash(this);
775
+ }
776
+
777
+ Date.prototype.clone = function () {
778
+ return date_clone(this);
779
+ }
780
+
781
+ Date.prototype.weekNum = function () {
782
+ return date_calc_weeknum(this);
783
+ }
784
+
785
+ Date.prototype.format = function (fmt: string) {
786
+ return formatIntlDate(this, fmt);
787
+ }
788
+
789
+ Date.prototype.addDays = function ( days: number ): Date {
790
+ this.setDate( this.getDate() + days );
791
+ return this;
792
+ }
793
+
794
+
795
+
796
+ // :: FILE CREATION ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
797
+
798
+
799
+ /**
800
+ *
801
+ * @param data data to export
802
+ * @param mimetype - 'text/plain'
803
+ */
804
+
805
+ export function downloadData(data, mimetype: string, filename: string) {
806
+
807
+ //if (data !== null && navigator.msSaveBlob) {
808
+ // return navigator.msSaveBlob(new Blob([data], { type: mimetype }), filename);
809
+ //}
810
+
811
+ let blob = new Blob([data], { type: mimetype });
812
+ let url = window.URL.createObjectURL(blob);
813
+
814
+ let a = x4document.createElement("a");
815
+ a.style['display'] = "none";
816
+ a.href = url;
817
+ a.download = filename;
818
+ x4document.body.appendChild(a);
819
+
820
+ a.click();
821
+ window.URL.revokeObjectURL(url);
822
+ }
823
+
824
+ // :: MISC ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
825
+
826
+
827
+ /**
828
+ * check if a value is a string
829
+ * @param val
830
+ */
831
+ export function isString(val: any): val is string {
832
+ return typeof val === 'string';
833
+ }
834
+
835
+ /**
836
+ * check is a value is an array
837
+ * @param val
838
+ */
839
+ export function isArray(val: any): val is any[] {
840
+ return val instanceof Array;
841
+ }
842
+
843
+ /**
844
+ *
845
+ */
846
+
847
+ export function isFunction(val: any): val is Function {
848
+ return val instanceof Function;
849
+ }
850
+
851
+ /**
852
+ *
853
+ */
854
+
855
+ export function isLiteralObject(val: any) {
856
+ return (!!val) && (val.constructor === Object);
857
+ };
858
+
859
+ /**
860
+ * prepend 0 to a value to a given length
861
+ * @param value
862
+ * @param length
863
+ */
864
+
865
+ export function pad(what: unknown, size: number, ch: string = '0') {
866
+
867
+ let value: string;
868
+
869
+ if (!isString(what)) {
870
+ value = '' + what;
871
+ }
872
+ else {
873
+ value = what;
874
+ }
875
+
876
+ if (size > 0) {
877
+ return value.padEnd(size, ch);
878
+ }
879
+ else {
880
+ return value.padStart(-size, ch);
881
+ }
882
+ }
883
+
884
+
885
+ /**
886
+ * return true if val is a finite number
887
+ */
888
+
889
+ export function isNumber(val: any): val is number {
890
+ return typeof val === 'number' && isFinite(val);
891
+ }
892
+
893
+ /**
894
+ *
895
+ * @param name
896
+ */
897
+
898
+ export function waitFontLoading(name: string) {
899
+
900
+ // tip for waiting font loading:
901
+ // by default, body is created invisible ((opacity = 0)
902
+ // we create a div inside with the font we need to wait the loading
903
+ // as soon as the font is loaded, it's size will change, the browser end font loading
904
+ // we can remove the div.
905
+ // pitfall: if the font is already loaded, ne never end.
906
+ // @review that
907
+
908
+ let ct = x4document.createElement('div');
909
+
910
+ ct.style.position = 'absolute';
911
+ ct.style.fontFamily = name;
912
+ ct.style.fontSize = '44px';
913
+ ct.style.opacity = '0.001';
914
+ ct.innerText = 'X';
915
+
916
+ x4document.body.appendChild(ct);
917
+
918
+ return new Promise<void>((resolve) => {
919
+
920
+ let irc = ct.getBoundingClientRect();
921
+ let tm = setInterval(() => {
922
+
923
+ let nrc = ct.getBoundingClientRect();
924
+ if (nrc.height != irc.height) {
925
+ clearInterval(tm);
926
+ x4document.body.removeChild(ct);
927
+
928
+ resolve();
929
+ }
930
+ }, 0);
931
+ });
932
+ }
933
+
934
+ /**
935
+ *
936
+ * @param fn
937
+ * @param tm
938
+ *
939
+ * @example:
940
+ *
941
+ * defer( ( ) => {
942
+ * console.log( x );
943
+ * } )( );
944
+ */
945
+
946
+ export function deferCall(fn: Function, tm = 0, ...args) {
947
+ setTimeout(fn, tm, ...args);
948
+ }
949
+
950
+ /**
951
+ *
952
+ */
953
+
954
+ export function asap(cb: (time: number) => void) {
955
+ requestAnimationFrame(cb);
956
+ }
957
+
958
+
959
+
960
+ /**
961
+ * micro md to html
962
+ *
963
+ * understand:
964
+ * **bold**
965
+ * *italic*
966
+ *
967
+ * > quote
968
+ * - list
969
+ * # title lvl 1
970
+ * ## title lvl 2
971
+ * ### title lvl 3 ...
972
+ *
973
+ */
974
+
975
+ export function markdownToHtml(text: string): string {
976
+
977
+ if (!text) {
978
+ return '';
979
+ }
980
+
981
+ let lines = text.split('\n');
982
+
983
+ let state = 'para';
984
+ let div = 'p';
985
+ let result: string[] = [];
986
+
987
+ lines.forEach((l) => {
988
+
989
+ let txt = l.trim();
990
+
991
+ if (state == 'para') {
992
+ // entree en mode list
993
+ if (txt[0] == '-') {
994
+ txt = txt.substr(1);
995
+ result.push('<ul>')
996
+ state = 'list';
997
+ div = 'li';
998
+ }
999
+ else if (txt[0] == '>') {
1000
+ txt = txt.substr(1);
1001
+ result.push('<blockquote>')
1002
+ state = 'quote';
1003
+ div = 'p';
1004
+ }
1005
+ else if (txt[0] == '#') {
1006
+ let lvl = 0;
1007
+ do {
1008
+ txt = txt.substr(1);
1009
+ lvl++;
1010
+ } while (txt[0] == '#' && lvl < 5);
1011
+
1012
+ div = 'h' + lvl;
1013
+ state = 'title';
1014
+ }
1015
+ }
1016
+ else if (state == 'list') {
1017
+ // sortie du mode list
1018
+ if (txt[0] != '-') {
1019
+ result.push('</ul>')
1020
+ state = 'para';
1021
+ div = 'p';
1022
+ }
1023
+ else {
1024
+ txt = txt.substr(1);
1025
+ }
1026
+ }
1027
+ else if (state == 'quote') {
1028
+
1029
+ // sortie du mode blockquote
1030
+ if (txt[0] != '>') {
1031
+ result.push('</blockquote>')
1032
+ state = 'para';
1033
+ div = 'p';
1034
+ }
1035
+ else {
1036
+ txt = txt.substr(1);
1037
+ }
1038
+ }
1039
+
1040
+ let reBold = /\*\*([^*]+)\*\*/gi;
1041
+
1042
+ txt = escapeHtml(txt, false);
1043
+ txt = txt.replace(reBold, (sub: string, ...a): string => {
1044
+ return '<b>' + sub.substr(2, sub.length - 4) + '</b>';
1045
+ });
1046
+
1047
+ let reItalic = /\*([^*]+)\*/gi;
1048
+ txt = txt.replace(reItalic, (sub: string, ...a): string => {
1049
+ return '<i>' + sub.substr(1, sub.length - 2) + '</i>';
1050
+ });
1051
+
1052
+ // keep empty lines
1053
+ if (txt == '') {
1054
+ txt = '&nbsp;';
1055
+ }
1056
+
1057
+ result.push(`<${div}>` + txt + `</${div}>`);
1058
+
1059
+ if (state == 'title') {
1060
+ state = 'para';
1061
+ div = 'p';
1062
+ }
1063
+ });
1064
+
1065
+ if (state == 'list') {
1066
+ result.push('</ul>')
1067
+ }
1068
+ else if (state == 'quote') {
1069
+ result.push('</blockquote>')
1070
+ }
1071
+
1072
+ return result.join('');
1073
+ }
1074
+
1075
+
1076
+ /**
1077
+ *
1078
+ */
1079
+
1080
+ export class NetworkError extends Error {
1081
+
1082
+ private m_code: number;
1083
+
1084
+ constructor(response: Response);
1085
+ constructor(code: number, text: string);
1086
+ constructor(a: number | Response, b?: string) {
1087
+
1088
+ if (a instanceof Response) {
1089
+ super(a.statusText);
1090
+ this.m_code = a.status;
1091
+ }
1092
+ else {
1093
+ super(b);
1094
+ this.m_code = a;
1095
+ }
1096
+ }
1097
+
1098
+ get code(): number {
1099
+ return this.m_code;
1100
+ }
1101
+ }
1102
+
1103
+ /**
1104
+ * return the mouse pos in client coordinates
1105
+ * handle correctly touch & mouse
1106
+ */
1107
+
1108
+ export function getMousePos(ev: UIEvent, fromDoc: boolean): Point {
1109
+
1110
+ let x_name = 'offsetX',
1111
+ y_name = 'offsetY';
1112
+
1113
+ if (fromDoc) {
1114
+ x_name = 'clientX';
1115
+ y_name = 'clientY';
1116
+ }
1117
+
1118
+ if (ev.type == 'mousemove' || ev.type == 'mousedown' || ev.type == 'mouseup') {
1119
+ let em = ev as MouseEvent;
1120
+ return new Point(em[x_name], em[y_name]);
1121
+ }
1122
+ else if (ev.type == 'pointermove' || ev.type == 'pointerdown' || ev.type == 'pointerup') {
1123
+ let em = ev as MouseEvent;
1124
+ return new Point(em[x_name], em[y_name]);
1125
+ }
1126
+ else if (ev.type == 'touchmove' || ev.type == 'touchstart') {
1127
+ let et = ev as TouchEvent;
1128
+ return new Point(et.touches[0][x_name], et.touches[0][y_name]);
1129
+ }
1130
+ else if (ev.type == 'contextmenu') {
1131
+ let em = ev as MouseEvent;
1132
+ return new Point(em[x_name], em[y_name]);
1133
+ }
1134
+ else {
1135
+ return new Point(0, 0);
1136
+ }
1137
+ }
1138
+
1139
+ /**
1140
+ * clamp a value
1141
+ * @param v - value to clamp
1142
+ * @param min - min value
1143
+ * @param max - max value
1144
+ * @returns clamped value
1145
+ */
1146
+
1147
+ export function clamp(v: number, min: number, max: number) {
1148
+ return Math.min(Math.max(v, min), max);
1149
+ }
1150
+
1151
+
1152
+ /**
1153
+ *
1154
+ */
1155
+
1156
+ export interface IDisposable {
1157
+ dispose();
1158
+ }
1159
+
1160
+ // :: HTML strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1161
+
1162
+ export class HtmlString extends String {
1163
+ constructor(text: string) { super(text); }
1164
+
1165
+ static from(text: string) {
1166
+ return new HtmlString(text);
1167
+ }
1168
+ }
1169
+
1170
+ export function html(a, ...b): HtmlString {
1171
+ return HtmlString.from(String.raw(a, ...b));
1172
+ }
1173
+
1174
+ export function isHtmlString(val: any): val is HtmlString {
1175
+ return val instanceof HtmlString;
1176
+ }
1177
+
1178
+ // :: CLIPBOARD ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1179
+
1180
+ export class Clipboard {
1181
+ static copy(data: any) {
1182
+ if (navigator.clipboard) {
1183
+ navigator.clipboard.writeText(JSON.stringify(data));
1184
+ }
1185
+ }
1186
+
1187
+ static paste(cb: (data: string) => void) {
1188
+ if (navigator.clipboard) {
1189
+ navigator.clipboard.readText().then(v => cb(v));
1190
+ }
1191
+ else {
1192
+ console.error('no clipboard, are you in https ?');
1193
+ }
1194
+ }
1195
+ }
1196
+
1197
+
1198
+ // :: CRC32 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1199
+
1200
+ /**
1201
+ * Calculates the CRC32 checksum of a string.
1202
+ * taken from: https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
1203
+ *
1204
+ * @param {String} str
1205
+ * @param {Boolean} hex
1206
+ * @return {String} checksum
1207
+ * @api public
1208
+ */
1209
+
1210
+ export function crc32(str) {
1211
+ let crc = ~0;
1212
+ let buf = Buffer.from( str );
1213
+
1214
+ let i = 0,
1215
+ l = buf.length;
1216
+ while( i<l ) {
1217
+ crc = (crc >>> 8) ^ crc32tab[(crc ^ buf[i]) & 0xff];
1218
+ i++;
1219
+ }
1220
+
1221
+ return (crc ^ -1)>>>0;
1222
+ }
1223
+
1224
+ var crc32tab = new Int32Array([
1225
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
1226
+ 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
1227
+ 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
1228
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
1229
+ 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
1230
+ 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
1231
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
1232
+ 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
1233
+ 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
1234
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
1235
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
1236
+ 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
1237
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
1238
+ 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
1239
+ 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
1240
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
1241
+ 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
1242
+ 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
1243
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
1244
+ 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
1245
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
1246
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
1247
+ 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
1248
+ 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
1249
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
1250
+ 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
1251
+ 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
1252
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
1253
+ 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
1254
+ 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
1255
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
1256
+ 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
1257
+ 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
1258
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
1259
+ 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
1260
+ 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
1261
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
1262
+ 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
1263
+ 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
1264
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
1265
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
1266
+ 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
1267
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
1268
+ 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
1269
+ 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
1270
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
1271
+ 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
1272
+ 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
1273
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
1274
+ 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
1275
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
1276
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
1277
+ 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
1278
+ 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
1279
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
1280
+ 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
1281
+ 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
1282
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
1283
+ 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
1284
+ 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
1285
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
1286
+ 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
1287
+ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
1288
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
1289
+ ]);
1290
+
1291
+
1292
+ // :: MIXINS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1293
+
1294
+ /**
1295
+ * taken from this excellent article:
1296
+ * https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
1297
+ *
1298
+ * @example:
1299
+ * class MyClass extends mix(MyBaseClass).with(Mixin1, Mixin2) {
1300
+ * }
1301
+ **/
1302
+
1303
+ export const mix = (superclass) => new MixinBuilder(superclass);
1304
+
1305
+ class MixinBuilder {
1306
+
1307
+ private superclass: any;
1308
+
1309
+ constructor(superclass) {
1310
+ this.superclass = superclass;
1311
+ }
1312
+
1313
+ with(...mixins) {
1314
+ return mixins.reduce((c, mixin) => mixin(c), this.superclass);
1315
+ }
1316
+ }
1317
+
1318
+ /**
1319
+ * @example
1320
+ *
1321
+ * ```
1322
+ * const cls = classNames( 'class1 class2', {
1323
+ * 'class3': false,
1324
+ * 'class4': true,
1325
+ * });
1326
+ *
1327
+ * // even shorter
1328
+ * const class1 = true, class2 = false;
1329
+ * const cls = classNames( { class1, class2 } ); // cls = "class1"
1330
+ *
1331
+ * ```
1332
+ *
1333
+ * @returns
1334
+ */
1335
+
1336
+ export function classNames(...args: (string | any)[]) {
1337
+
1338
+ let result = '';
1339
+
1340
+ for (const cls of args) {
1341
+ if (typeof cls === 'string') {
1342
+ result += ' ' + cls;
1343
+ }
1344
+ else if (cls) {
1345
+ for (const c in cls) {
1346
+ if (cls[c])
1347
+ result += ' ' + c;
1348
+ }
1349
+ }
1350
+ }
1351
+
1352
+ return result;
1353
+ }
1354
+
1355
+ /**
1356
+ *
1357
+ */
1358
+
1359
+ interface PasswordRule {
1360
+ chars: string;
1361
+ min: number;
1362
+ }
1363
+
1364
+ export function generatePassword(length: number, rules?: PasswordRule[]) {
1365
+
1366
+ if (!length || length == undefined) {
1367
+ length = 8;
1368
+ }
1369
+
1370
+ if (!rules) {
1371
+ rules = [
1372
+ { chars: "abcdefghijklmnopqrstuvwxyz", min: 3 }, // As least 3 lowercase letters
1373
+ { chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 2 }, // At least 2 uppercase letters
1374
+ { chars: "0123456789", min: 2 }, // At least 2 digits
1375
+ { chars: "!@#$*|%+-_.;", min: 2 } // At least 1 special char
1376
+ ];
1377
+ }
1378
+
1379
+ let allChars = "";
1380
+ let allMin = 0;
1381
+
1382
+ rules.forEach(function (rule) {
1383
+ allChars += rule.chars;
1384
+ allMin += rule.min;
1385
+ });
1386
+
1387
+ if (length < allMin) {
1388
+ length = allMin;
1389
+ }
1390
+
1391
+ rules.push({ chars: allChars, min: length - allMin });
1392
+
1393
+ let pswd = "";
1394
+ rules.forEach(function (rule) {
1395
+ if (rule.min > 0) {
1396
+ pswd += shuffle(rule.chars, rule.min);
1397
+ }
1398
+ });
1399
+
1400
+ return shuffle(pswd);
1401
+ }
1402
+
1403
+ function shuffle(str: string, maxlength?: number) {
1404
+ let shuffled = str.split('').sort(() => {
1405
+ return 0.5 - Math.random()
1406
+ }).join('');
1407
+
1408
+ if (maxlength > 0) {
1409
+ shuffled = shuffled.substr(0, maxlength);
1410
+ }
1411
+
1412
+ return shuffled;
1413
+ }
1414
+
1415
+
1416
+
1417
+ /**
1418
+ * taken from live-server
1419
+ * https://github.com/tapio/live-server
1420
+ * @param host
1421
+ * @param port
1422
+ */
1423
+
1424
+ export function installHMR(host = "127.0.0.1", port = "9876", reloadCallback?: Function ) {
1425
+
1426
+ let tm;
1427
+
1428
+ function refreshCSS() {
1429
+
1430
+ document.body.style.visibility = "hidden";
1431
+
1432
+ let sheets = [].slice.call(document.getElementsByTagName("link"));
1433
+ let head = document.getElementsByTagName("head")[0];
1434
+
1435
+ for (let i = 0; i < sheets.length; ++i) {
1436
+ let elem = sheets[i];
1437
+ head.removeChild(elem);
1438
+
1439
+ let rel = elem.rel;
1440
+ if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
1441
+ let url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
1442
+ elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
1443
+ }
1444
+
1445
+ head.appendChild(elem);
1446
+ }
1447
+
1448
+ if( tm ) { clearTimeout(tm); }
1449
+ tm = setTimeout( () => {
1450
+ document.body.style.visibility = "unset";
1451
+ }, 50 );
1452
+ }
1453
+
1454
+ const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
1455
+ const address = `${protocol}${host}:${port}/ws`;
1456
+ const socket = new WebSocket(address);
1457
+
1458
+ socket.onmessage = function (msg) {
1459
+ if (msg.data == 'reload') {
1460
+ if( reloadCallback ) {
1461
+ reloadCallback( );
1462
+ }
1463
+ else {
1464
+ window.location.reload();
1465
+ }
1466
+ }
1467
+ else if (msg.data == 'refreshcss') {
1468
+ refreshCSS();
1469
+ }
1470
+ };
1471
+
1472
+ console.log('Live reload enabled.');
1473
+ }