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.
Files changed (85) hide show
  1. package/lib/application.d.ts +96 -0
  2. package/lib/application.js +146 -0
  3. package/lib/base64.d.ts +31 -0
  4. package/lib/base64.js +139 -0
  5. package/lib/base_component.d.ts +64 -0
  6. package/lib/base_component.js +81 -0
  7. package/lib/button.d.ts +145 -0
  8. package/lib/button.js +241 -0
  9. package/lib/calendar.d.ts +77 -0
  10. package/lib/calendar.js +241 -0
  11. package/lib/canvas.d.ts +88 -0
  12. package/lib/canvas.js +358 -0
  13. package/lib/cardview.d.ts +83 -0
  14. package/lib/cardview.js +156 -0
  15. package/lib/checkbox.d.ts +72 -0
  16. package/lib/checkbox.js +130 -0
  17. package/lib/color.d.ts +144 -0
  18. package/lib/color.js +588 -0
  19. package/lib/colorpicker.js +86 -80
  20. package/lib/combobox.js +26 -22
  21. package/lib/component.d.ts +572 -0
  22. package/lib/component.js +1729 -0
  23. package/lib/datastore.js +29 -20
  24. package/lib/dialog.js +41 -36
  25. package/lib/dom_events.d.ts +284 -0
  26. package/lib/dom_events.js +14 -0
  27. package/lib/drag_manager.js +4 -1
  28. package/lib/drawtext.js +9 -5
  29. package/lib/fileupload.js +19 -12
  30. package/lib/form.js +29 -25
  31. package/lib/formatters.js +19 -10
  32. package/lib/gridview.js +40 -35
  33. package/lib/hosts/host.d.ts +44 -0
  34. package/lib/hosts/host.js +73 -0
  35. package/lib/i18n.d.ts +67 -0
  36. package/lib/i18n.js +175 -0
  37. package/lib/icon.d.ts +56 -0
  38. package/lib/icon.js +178 -0
  39. package/lib/image.js +7 -3
  40. package/lib/index.js +71 -55
  41. package/lib/input.d.ts +86 -0
  42. package/lib/input.js +176 -0
  43. package/lib/label.d.ts +54 -0
  44. package/lib/label.js +90 -0
  45. package/lib/layout.d.ts +77 -0
  46. package/lib/layout.js +271 -0
  47. package/lib/link.js +9 -5
  48. package/lib/listview.js +34 -27
  49. package/lib/md5.js +5 -1
  50. package/lib/menu.d.ts +122 -0
  51. package/lib/menu.js +284 -0
  52. package/lib/messagebox.js +22 -17
  53. package/lib/panel.js +13 -9
  54. package/lib/popup.d.ts +71 -0
  55. package/lib/popup.js +378 -0
  56. package/lib/property_editor.js +20 -16
  57. package/lib/radiobtn.js +13 -9
  58. package/lib/rating.js +13 -9
  59. package/lib/request.js +16 -9
  60. package/lib/router.js +5 -1
  61. package/lib/settings.d.ts +33 -0
  62. package/lib/settings.js +67 -0
  63. package/lib/sidebarview.js +12 -8
  64. package/lib/smartedit.js +16 -11
  65. package/lib/spreadsheet.js +35 -31
  66. package/lib/styles.d.ts +81 -0
  67. package/lib/styles.js +268 -0
  68. package/lib/svgcomponent.js +8 -3
  69. package/lib/tabbar.js +10 -6
  70. package/lib/tabview.js +10 -6
  71. package/lib/textarea.js +10 -6
  72. package/lib/textedit.js +39 -35
  73. package/lib/texthiliter.js +8 -4
  74. package/lib/toaster.js +8 -4
  75. package/lib/tools.d.ts +382 -0
  76. package/lib/tools.js +1142 -0
  77. package/lib/tooltips.js +14 -9
  78. package/lib/treeview.js +30 -26
  79. package/lib/x4_events.d.ts +253 -0
  80. package/lib/x4_events.js +375 -0
  81. package/package.json +1 -1
  82. package/src/application.ts +9 -4
  83. package/src/x4.less +143 -107
  84. package/tsconfig.json +1 -1
  85. package/x4.css +1541 -0
@@ -0,0 +1,1729 @@
1
+ "use strict";
2
+ /**
3
+ * ___ ___ __
4
+ * \ \/ / / _
5
+ * \ / /_| |_
6
+ * / \____ _|
7
+ * /__/\__\ |_|
8
+ *
9
+ * @file components.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.Container = exports.SizerOverlay = exports.EvOverlayResize = exports.Separator = exports.EvSize = exports.Space = exports.Flex = exports.flyWrap = exports.Component = exports.EvFocus = exports.EvDblClick = exports.html = exports.isHtmlString = exports.HtmlString = void 0;
28
+ /**
29
+ * @todo
30
+ * create Container class
31
+ */
32
+ const tools_1 = require("./tools");
33
+ const styles_1 = require("./styles");
34
+ const x4_events_1 = require("./x4_events");
35
+ const base_component_1 = require("./base_component");
36
+ var tools_2 = require("./tools");
37
+ Object.defineProperty(exports, "HtmlString", { enumerable: true, get: function () { return tools_2.HtmlString; } });
38
+ Object.defineProperty(exports, "isHtmlString", { enumerable: true, get: function () { return tools_2.isHtmlString; } });
39
+ Object.defineProperty(exports, "html", { enumerable: true, get: function () { return tools_2.html; } });
40
+ /** @ignore classname prefix for system classes */
41
+ const _x4_ns_prefix = 'x-';
42
+ // -- elements -----------
43
+ /** @ignore where event handlers are stored */
44
+ const _x4_el_store = Symbol();
45
+ /** @ignore where Component is stored in dom */
46
+ const _x4_el_sym = Symbol();
47
+ /** @ignore properties without 'px' unit */
48
+ const _x4_unitless = {
49
+ animationIterationCount: 1, borderImageOutset: 1, borderImageSlice: 1, borderImageWidth: 1, boxFlex: 1, boxFlexGroup: 1,
50
+ boxOrdinalGroup: 1, columnCount: 1, flex: 1, flexGrow: 1, flexPositive: 1, flexShrink: 1, flexNegative: 1, flexOrder: 1,
51
+ gridRow: 1, gridColumn: 1, fontWeight: 1, lineClamp: 1, lineHeight: 1, opacity: 1, order: 1, orphans: 1, tabSize: 1, widows: 1,
52
+ zIndex: 1, zoom: 1,
53
+ // SVG-related _properties
54
+ fillOpacity: 1, floodOpacity: 1, stopOpacity: 1, strokeDasharray: 1, strokeDashoffset: 1, strokeMiterlimit: 1, strokeOpacity: 1,
55
+ strokeWidth: 1,
56
+ };
57
+ /** @ignore this events must be defined on domNode (do not bubble) */
58
+ const unbubbleEvents = {
59
+ mouseleave: 1, mouseenter: 1, load: 1, unload: 1, scroll: 1, focus: 1, blur: 1, rowexit: 1, beforeunload: 1, stop: 1,
60
+ dragdrop: 1, dragenter: 1, dragexit: 1, draggesture: 1, dragover: 1, contextmenu: 1, create: 2, sizechange: 2
61
+ };
62
+ /** @ignore */
63
+ const passiveEvents = {
64
+ touchstart: 1, touchmove: 1, touchend: 1,
65
+ //pointerdown: 1, pointermove: 1, pointerup: 1,
66
+ };
67
+ /** ignore */
68
+ const reNumber = /^-?\d+(\.\d+)?$/;
69
+ function EvDblClick(context = null) {
70
+ return (0, x4_events_1.BasicEvent)({ context });
71
+ }
72
+ exports.EvDblClick = EvDblClick;
73
+ function EvFocus(focus = true, context = null) {
74
+ return (0, x4_events_1.BasicEvent)({ focus, context });
75
+ }
76
+ exports.EvFocus = EvFocus;
77
+ /**
78
+ *
79
+ */
80
+ class Component extends base_component_1.BaseComponent {
81
+ m_dom;
82
+ m_iprops;
83
+ static __sb_width; // scrollbar width
84
+ static __comp_guid = 1000; // component global unique id
85
+ static __privateEvents = {};
86
+ static __sizeObserver; // resize observer
87
+ static __createObserver; // creation observer
88
+ static __intersectionObserver; // visibility observer
89
+ static __capture = null;
90
+ static __capture_mask = null;
91
+ static __css = null;
92
+ constructor(props = null) {
93
+ super(props ?? {});
94
+ this.m_iprops = {
95
+ classes: {},
96
+ dom_events: {},
97
+ uid: Component.__comp_guid++,
98
+ inrender: false,
99
+ };
100
+ }
101
+ /**
102
+ *
103
+ */
104
+ get uid() {
105
+ return this.m_iprops.uid;
106
+ }
107
+ /**
108
+ * change the component content
109
+ * @param content new content or null
110
+ */
111
+ setContent(content, refreshAll = false) {
112
+ this.m_props.content = content ?? [];
113
+ if (this.m_iprops.inrender || !this.m_dom) {
114
+ return;
115
+ }
116
+ if (refreshAll) {
117
+ this.update();
118
+ }
119
+ else {
120
+ this._updateContent();
121
+ }
122
+ }
123
+ /**
124
+ * add a new child to the component content
125
+ * @param content
126
+ */
127
+ appendChild(content) {
128
+ if (!content) {
129
+ return;
130
+ }
131
+ const append = (c) => {
132
+ if (!this.m_props.content) {
133
+ this.m_props.content = [];
134
+ }
135
+ else if (!(0, tools_1.isArray)(this.m_props.content)) {
136
+ this.m_props.content = [this.m_props.content];
137
+ }
138
+ this.m_props.content.push(c);
139
+ if (this.m_dom) {
140
+ this._appendChild(c);
141
+ }
142
+ };
143
+ if ((0, tools_1.isArray)(content)) {
144
+ content.forEach(append);
145
+ }
146
+ else {
147
+ append(content);
148
+ }
149
+ }
150
+ /**
151
+ * get the Component value
152
+ * @param name name to get
153
+ */
154
+ getProp(name) {
155
+ return this.m_props[name];
156
+ }
157
+ /**
158
+ * change a Component value
159
+ * @param name name to set
160
+ * @param value new value
161
+ */
162
+ setProp(name, value) {
163
+ this.m_props[name] = value;
164
+ }
165
+ /**
166
+ * get the Component data value
167
+ * @param name name to get
168
+ */
169
+ getData(name) {
170
+ if (this.m_props.data !== undefined) {
171
+ return this.m_props.data[name.toString()];
172
+ }
173
+ return undefined;
174
+ }
175
+ /**
176
+ * set the Component data value
177
+ * @param name name to get
178
+ * @param value
179
+ */
180
+ setData(name, value) {
181
+ let data = this.m_props.data;
182
+ if (data === undefined) {
183
+ data = this.m_props.data = {};
184
+ }
185
+ data[name.toString()] = value;
186
+ }
187
+ /**
188
+ * return the DOM associated with the Component (if any)
189
+ */
190
+ get dom() {
191
+ return this.m_dom;
192
+ }
193
+ /**
194
+ * shows the element
195
+ * @param show
196
+ */
197
+ show(show) {
198
+ if (show === undefined || show === true) {
199
+ this.removeClass('@hidden');
200
+ }
201
+ else {
202
+ this.addClass('@hidden');
203
+ }
204
+ }
205
+ /**
206
+ * hides the element
207
+ */
208
+ hide() {
209
+ this.addClass('@hidden');
210
+ }
211
+ /**
212
+ * enable or disable the element
213
+ * @param enable
214
+ */
215
+ enable(enable) {
216
+ if (enable === undefined || enable === true) {
217
+ this.removeClass('@disable');
218
+ this.removeAttribute('disabled');
219
+ }
220
+ else {
221
+ this.disable();
222
+ }
223
+ }
224
+ /**
225
+ * disable the element
226
+ */
227
+ disable() {
228
+ this.addClass('@disable');
229
+ this.setAttribute('disabled', '');
230
+ }
231
+ /**
232
+ * set the focus on the element
233
+ */
234
+ focus() {
235
+ console.assert(!!this.m_dom);
236
+ this.m_dom.focus();
237
+ }
238
+ /**
239
+ * change the object style
240
+ * @param style - style to add
241
+ * @example ```typescript
242
+ * el.setStyle( {left:100} );
243
+ */
244
+ setStyle(style) {
245
+ for (let s in style) {
246
+ this.setStyleValue(s, style[s]);
247
+ }
248
+ }
249
+ /**
250
+ * change a style value
251
+ * @param name string style nale
252
+ * @param value any style value or null to remove style
253
+ */
254
+ setStyleValue(name, value) {
255
+ let style = this.m_props.style;
256
+ if (!style) {
257
+ style = this.m_props.style = {};
258
+ }
259
+ style[name] = value;
260
+ this._setDomStyleValue(name, value);
261
+ }
262
+ _setDomStyleValue(name, value) {
263
+ if (this.m_dom) {
264
+ if (value === undefined) {
265
+ value = null;
266
+ }
267
+ else if (!_x4_unitless[name] && ((0, tools_1.isNumber)(value) || reNumber.test(value))) {
268
+ value = value + 'px';
269
+ }
270
+ this.m_dom.style[name] = value;
271
+ }
272
+ }
273
+ /**
274
+ * compute the element style
275
+ * @return all styles computed
276
+ */
277
+ getComputedStyle(pseudoElt) {
278
+ if (this.dom) {
279
+ return new styles_1.ComputedStyle(getComputedStyle(this.dom, pseudoElt ?? null));
280
+ }
281
+ return new styles_1.ComputedStyle(this.m_props.style);
282
+ }
283
+ /**
284
+ * return a single stype value
285
+ * @param name - value to get
286
+ */
287
+ getStyleValue(name) {
288
+ return this.getComputedStyle()[name];
289
+ }
290
+ /**
291
+ * define the elements attributes
292
+ * @param attrs
293
+ */
294
+ setAttributes(attrs) {
295
+ for (let a in attrs) {
296
+ this.setAttribute(a, attrs[a]);
297
+ }
298
+ }
299
+ /**
300
+ * change a single attribute
301
+ * @param name attribute name
302
+ * @param value new value
303
+ */
304
+ setAttribute(name, value) {
305
+ if (value === false || value === undefined) {
306
+ this.removeAttribute(name);
307
+ }
308
+ else {
309
+ if (value === true) {
310
+ value = '';
311
+ }
312
+ else if ((0, tools_1.isNumber)(value)) {
313
+ value = '' + value;
314
+ }
315
+ let attrs = this.m_props.attrs;
316
+ if (!attrs) {
317
+ attrs = this.m_props.attrs = {};
318
+ }
319
+ attrs[name] = value;
320
+ this._setDomAttribute(name, value);
321
+ }
322
+ }
323
+ _setDomAttribute(name, value) {
324
+ if (this.m_dom) {
325
+ this.m_dom.setAttribute(name, value);
326
+ }
327
+ }
328
+ /**
329
+ * remove an atrribute
330
+ * @param name name of the attribute
331
+ */
332
+ removeAttribute(name) {
333
+ let attrs = this.m_props.attrs;
334
+ if (!attrs) {
335
+ return;
336
+ }
337
+ delete attrs[name];
338
+ if (this.m_dom) {
339
+ this.m_dom.removeAttribute(name);
340
+ }
341
+ }
342
+ /**
343
+ * get an attribute value
344
+ * @param {string} name - attribute name
345
+ * @return {string} attribute value
346
+ * @example ```typescript
347
+ * let chk = el.getAttribute( 'checked' );
348
+ * @review double cache
349
+ */
350
+ getAttribute(name) {
351
+ if (this.m_dom) {
352
+ return this.m_dom.getAttribute(name);
353
+ }
354
+ else {
355
+ if (!this.m_props.attrs) {
356
+ return undefined;
357
+ }
358
+ return this.m_props.attrs[name];
359
+ }
360
+ }
361
+ /**
362
+ * check if the element has an attribute
363
+ * @param name attribute name
364
+ * @return true is attribute is present
365
+ * @example ```typescript
366
+ * if( el.hasAttribute('checked') ) {
367
+ * }
368
+ */
369
+ hasAttribute(name) {
370
+ if (this.m_dom) {
371
+ return this.m_dom.hasAttribute(name);
372
+ }
373
+ else {
374
+ return this.m_props.attrs.hasOwnProperty(name);
375
+ }
376
+ }
377
+ /**
378
+ * a some classnames to the component
379
+ * classes can be separated by a space
380
+ * @param cls class to add
381
+ * @example ```typescript
382
+ * addClass( 'my class name @flex' );
383
+ */
384
+ addClass(name) {
385
+ if (name === null || name === undefined) {
386
+ return;
387
+ }
388
+ name = name.trim();
389
+ if (name === '') {
390
+ return;
391
+ }
392
+ let add = (c) => {
393
+ if (c === undefined || c === null || c === '') {
394
+ return;
395
+ }
396
+ c = this._makeCls(c);
397
+ // update vdom
398
+ classes[c] = true;
399
+ // update dom
400
+ if (this.m_dom) {
401
+ this.m_dom.classList.add(c);
402
+ }
403
+ };
404
+ let classes = this.m_iprops.classes;
405
+ if (name.indexOf(' ') < 0) {
406
+ add(name);
407
+ }
408
+ else {
409
+ let names = name.split(' ');
410
+ names.forEach((n) => add(n));
411
+ }
412
+ }
413
+ /**
414
+ * Remove a class from the element
415
+ * @param {string|array} name - classes in string form can be space separated
416
+ *
417
+ * @example ```typescript
418
+ * el.removeClass( 'myclass' );
419
+ * el.removeClass( 'myclass1 myclass2' );
420
+ */
421
+ removeClass(name) {
422
+ if (name === undefined) {
423
+ return;
424
+ }
425
+ let remove = (c) => {
426
+ if (c === undefined || c === null || c === '') {
427
+ return;
428
+ }
429
+ c = this._makeCls(c);
430
+ delete this.m_iprops.classes[c];
431
+ if (this.m_dom) {
432
+ this.m_dom.classList.remove(c);
433
+ }
434
+ };
435
+ // faster
436
+ if (name.indexOf(' ') < 0) {
437
+ remove(name);
438
+ }
439
+ else {
440
+ // build class list
441
+ let classes = name.trim().split(' ');
442
+ for (let c of classes) {
443
+ if (c !== undefined && c !== null && c !== '') {
444
+ remove(c);
445
+ }
446
+ }
447
+ }
448
+ }
449
+ /**
450
+ *
451
+ * @param cls
452
+ * @param set
453
+ */
454
+ setClass(cls, set) {
455
+ if (set) {
456
+ this.addClass(cls);
457
+ }
458
+ else {
459
+ this.removeClass(cls);
460
+ }
461
+ return this;
462
+ }
463
+ /**
464
+ * Toggle a class from the element (if present remove, if absent add)
465
+ * @param {string|string[]} name - classes in string form can be space separated
466
+ * @example ```typescript
467
+ * el.toggleClass( 'myclass' );
468
+ * el.toggleClass( 'myclass1 myclass2');
469
+ * el.toggleClass( ['myclass1','myclass2']);
470
+ */
471
+ toggleClass(name) {
472
+ let toggle = (c) => {
473
+ if (c === undefined && c === null && c === '') {
474
+ return;
475
+ }
476
+ c = this._makeCls(c);
477
+ if (this.m_iprops.classes[c]) {
478
+ delete this.m_iprops.classes[c];
479
+ }
480
+ else {
481
+ this.m_iprops.classes[c] = true;
482
+ }
483
+ if (this.m_dom) {
484
+ this.m_dom.classList.toggle(c);
485
+ }
486
+ };
487
+ // faster
488
+ if (name.indexOf(' ') < 0) {
489
+ toggle(name);
490
+ }
491
+ else {
492
+ // build class list
493
+ let classes = name.trim().split(' ');
494
+ for (let c of classes) {
495
+ toggle(c);
496
+ }
497
+ }
498
+ }
499
+ /**
500
+ * check if the object has the class
501
+ * @param cls
502
+ */
503
+ hasClass(cls) {
504
+ let c = this._makeCls(cls);
505
+ if (this.m_dom) {
506
+ return this.dom.classList.contains(c);
507
+ }
508
+ else {
509
+ return !!this.m_iprops.classes[c];
510
+ }
511
+ }
512
+ /**
513
+ * remove all classes from the object
514
+ * this is usefull for component recycling & reusing
515
+ */
516
+ clearClasses() {
517
+ this.m_iprops.classes = {};
518
+ if (this.m_dom) {
519
+ return this.m_dom.classList.value = '';
520
+ }
521
+ }
522
+ ///@deprecated
523
+ //private build(): void {}
524
+ /**
525
+ * @deprecated
526
+ */
527
+ Build() { }
528
+ _build() {
529
+ if (this.m_dom) {
530
+ return this.m_dom;
531
+ }
532
+ this._createDOM();
533
+ return this.m_dom;
534
+ }
535
+ render(props) {
536
+ if (this.m_props.tag == 'footer') {
537
+ debugger;
538
+ }
539
+ }
540
+ _createDOM() {
541
+ if (this.m_dom) {
542
+ return this.m_dom;
543
+ }
544
+ // setup props
545
+ const props = this.m_props;
546
+ if (props.tabIndex !== undefined) {
547
+ this._setTabIndex(props.tabIndex);
548
+ }
549
+ this.render(props);
550
+ // shortcuts ---------
551
+ if (props.left !== undefined) {
552
+ this.setStyleValue('left', props.left);
553
+ }
554
+ if (props.top !== undefined) {
555
+ this.setStyleValue('top', props.top);
556
+ }
557
+ if (props.width !== undefined) {
558
+ this.setStyleValue('width', props.width);
559
+ }
560
+ if (props.height !== undefined) {
561
+ this.setStyleValue('height', props.height);
562
+ }
563
+ if (props.flex !== undefined) {
564
+ this.addClass('@flex');
565
+ if (props.flex != 1) {
566
+ this.setStyleValue('flex', props.flex);
567
+ }
568
+ }
569
+ if (props.enabled === false) {
570
+ this.disable();
571
+ }
572
+ // shortcut: tip
573
+ if (props.tooltip !== undefined) {
574
+ this.setAttribute('tip', props.tooltip.replace(/\n/gi, '<br/>'));
575
+ }
576
+ // prepare iprops
577
+ if (props.dom_events) {
578
+ for (let ename in props.dom_events) {
579
+ this._setDomEvent(ename, props.dom_events[ename]);
580
+ }
581
+ }
582
+ this._genClassName();
583
+ this.m_props.cls = undefined; // now classes are tranfered to m_iprops
584
+ // create self
585
+ let vdom = this.m_iprops;
586
+ if (props.ns) {
587
+ this.m_dom = document.createElementNS(props.ns, props.tag ?? 'div');
588
+ }
589
+ else {
590
+ this.m_dom = document.createElement(props.tag ?? 'div');
591
+ }
592
+ this.m_dom[_x4_el_sym] = this;
593
+ //let me = Object.getPrototypeOf(this);
594
+ //console.log( 'create', this.m_iprops.uid, me.constructor.name );
595
+ // classes
596
+ this.m_dom.classList.add(...Object.keys(vdom.classes));
597
+ // styles
598
+ let sty = props.style;
599
+ if (sty) {
600
+ for (let s in sty) {
601
+ this._setDomStyleValue(s, sty[s]);
602
+ }
603
+ }
604
+ // attributes
605
+ let att = props.attrs;
606
+ if (att) {
607
+ for (let a in att) {
608
+ const attr = att[a];
609
+ if (attr !== false && attr !== undefined) {
610
+ this._setDomAttribute(a, att[a]);
611
+ }
612
+ }
613
+ }
614
+ // special properties
615
+ if (this.m_props.id) {
616
+ this._setDomAttribute('id', this.m_props.id);
617
+ }
618
+ // events
619
+ let evt = this.m_iprops.dom_events;
620
+ if (evt) {
621
+ for (let e in evt) {
622
+ let handlers = evt[e];
623
+ for (let h of handlers) {
624
+ this.createEvent(e, h);
625
+ }
626
+ }
627
+ }
628
+ // create children
629
+ let content = props.content;
630
+ if (content) {
631
+ if (!(0, tools_1.isArray)(content)) {
632
+ content = [content];
633
+ }
634
+ content.forEach((el) => {
635
+ if (!el) {
636
+ return;
637
+ }
638
+ if ((0, tools_1.isString)(el)) {
639
+ this.m_dom.insertAdjacentText('beforeend', el);
640
+ }
641
+ else if ((0, tools_1.isHtmlString)(el)) {
642
+ this.m_dom.insertAdjacentHTML('beforeend', el);
643
+ }
644
+ else if (el instanceof Component) {
645
+ this.m_dom.append(el._build());
646
+ }
647
+ else {
648
+ console.log('unknown element type: ', el);
649
+ }
650
+ });
651
+ }
652
+ // wait for dom insertion inside document.body
653
+ if (!Component.__createObserver) {
654
+ Component.__createObserver = new MutationObserver(Component._observeCreation);
655
+ Component.__createObserver.observe(document.body, { childList: true, subtree: true });
656
+ }
657
+ return this.m_dom;
658
+ }
659
+ _setTabIndex(tabIndex, defValue = 0) {
660
+ if (tabIndex === true) {
661
+ tabIndex = 0;
662
+ }
663
+ else if (tabIndex === undefined) {
664
+ tabIndex = defValue;
665
+ }
666
+ if (tabIndex !== false && tabIndex !== undefined) {
667
+ this.setAttribute('tabindex', tabIndex);
668
+ }
669
+ this.m_props.tabIndex = tabIndex;
670
+ }
671
+ static _observeCreation(mutations) {
672
+ // notify descendants that we have been created (dom exists)
673
+ for (let mutation of mutations) {
674
+ if (mutation.type == 'childList') {
675
+ for (let i = 0, n = mutation.addedNodes.length; i < n; i++) {
676
+ let add = mutation.addedNodes[i];
677
+ let el = add[_x4_el_sym];
678
+ if (el) {
679
+ el.enumChilds((c) => {
680
+ if (c.dom && c.m_iprops.dom_events && c.m_iprops.dom_events.create) {
681
+ c.dom.dispatchEvent(new Event('create'));
682
+ }
683
+ c.componentCreated();
684
+ }, true);
685
+ if (el.m_iprops.dom_events && el.m_iprops.dom_events.create) {
686
+ el.dom.dispatchEvent(new Event('create'));
687
+ }
688
+ el.componentCreated();
689
+ }
690
+ }
691
+ }
692
+ }
693
+ }
694
+ dispose() {
695
+ if (this.m_dom) {
696
+ this._dispose(true, true);
697
+ }
698
+ }
699
+ _dispose(with_dom, timers) {
700
+ let _dom = this.m_dom;
701
+ // free attached resources
702
+ delete _dom[_x4_el_sym];
703
+ delete _dom[_x4_el_store];
704
+ //
705
+ if (with_dom) {
706
+ _dom.remove();
707
+ }
708
+ // notify every child that they will be removed
709
+ this.enumChilds((c) => {
710
+ c._dispose(false, true);
711
+ });
712
+ this.m_dom = null;
713
+ if (timers) {
714
+ this.disposeTimers();
715
+ }
716
+ this.componentDisposed();
717
+ // todo: pb on update this.removeAllListeners( null );
718
+ }
719
+ componentDisposed() {
720
+ }
721
+ componentCreated() {
722
+ }
723
+ /**
724
+ *
725
+ */
726
+ update(delay = 0) {
727
+ if (this.m_dom) {
728
+ const _update = () => {
729
+ let oldDOM = this.m_dom;
730
+ this._dispose(false, false);
731
+ let newDOM = this._build();
732
+ console.assert(!!oldDOM.parentNode, 'update in componentCreated is not allowed, use updateContent');
733
+ oldDOM.parentNode.replaceChild(newDOM, oldDOM);
734
+ };
735
+ if (delay) {
736
+ this.singleShot(_update, delay);
737
+ }
738
+ else {
739
+ _update();
740
+ }
741
+ }
742
+ }
743
+ /**
744
+ * empty the node
745
+ */
746
+ _empty() {
747
+ //this.m_dom.innerHTML = '';
748
+ const el = this.m_dom;
749
+ if (!el) {
750
+ return;
751
+ }
752
+ while (el.firstChild) {
753
+ el.removeChild(el.firstChild);
754
+ }
755
+ }
756
+ _updateContent() {
757
+ if (!this.m_dom) {
758
+ return;
759
+ }
760
+ this._empty();
761
+ let content = this.m_props.content;
762
+ // create children
763
+ if (content) {
764
+ if (!(0, tools_1.isArray)(content)) {
765
+ content = [content];
766
+ }
767
+ content.forEach((el) => {
768
+ if (!el) {
769
+ return;
770
+ }
771
+ if ((0, tools_1.isHtmlString)(el)) {
772
+ this.m_dom.insertAdjacentHTML('beforeend', el);
773
+ }
774
+ else if (el instanceof Component) {
775
+ this.m_dom.append(el._build());
776
+ }
777
+ else {
778
+ this.m_dom.insertAdjacentText('beforeend', el + '');
779
+ }
780
+ });
781
+ }
782
+ }
783
+ /**
784
+ * @return the bounding rectangle
785
+ * @example ```typescript
786
+ * let rc = el.getBoundingRect( );
787
+ * console.log( rc.left, rc.top, rc.right, rc.bottom );
788
+ */
789
+ getBoundingRect(withMargins = false) {
790
+ console.assert(this.dom != null, 'cannot get bounding rect of an non DOM element');
791
+ let r = this.dom.getBoundingClientRect();
792
+ let rc = new tools_1.Rect(r.left, r.top, r.width, r.height);
793
+ if (withMargins) {
794
+ let st = this.getComputedStyle();
795
+ let tm = st.parse('marginTop'), bm = st.parse('marginBottom'), lm = st.parse('marginLeft'), rm = st.parse('marginRight');
796
+ rc.left -= lm;
797
+ rc.width += lm + rm;
798
+ rc.top -= tm;
799
+ rc.height += tm + bm;
800
+ }
801
+ return rc;
802
+ }
803
+ /**
804
+ * append a new dom event handler
805
+ * @param name - you can specify multiple names separated by a space
806
+ * @param handler
807
+ * @example
808
+ *
809
+ * this.setDomEvent( 'drag drop', this._handleDrag, this );
810
+ * this.setDomEvent( 'dblclick', this._handleDblClick, this );
811
+ */
812
+ setDomEvent(type, listener) {
813
+ let _listener = listener;
814
+ this._setDomEvent(type, _listener);
815
+ }
816
+ _setDomEvent(type, listener) {
817
+ // add event to the vdom
818
+ if (!this.m_iprops.dom_events) {
819
+ this.m_iprops.dom_events = {};
820
+ }
821
+ let listeners = this.m_iprops.dom_events[type];
822
+ if (!listeners) {
823
+ listeners = this.m_iprops.dom_events[type] = [listener];
824
+ }
825
+ else {
826
+ listeners.push(listener);
827
+ }
828
+ if (this.m_dom) {
829
+ //this.m_dom.addEventListener(type, listener);
830
+ this.createEvent(type, listener);
831
+ }
832
+ }
833
+ /**
834
+ *
835
+ */
836
+ clearDomEvent(type) {
837
+ if (!this.m_iprops.dom_events) {
838
+ return;
839
+ }
840
+ delete this.m_iprops.dom_events[type];
841
+ let _dom = this.m_dom;
842
+ if (_dom) {
843
+ let store = _dom[_x4_el_store];
844
+ if (store) {
845
+ delete store[type];
846
+ }
847
+ }
848
+ }
849
+ mapPropEvents(props, ...elements) {
850
+ elements.forEach(name => {
851
+ const n = name;
852
+ if (props[n]) {
853
+ this._on(n, props[n]);
854
+ }
855
+ });
856
+ }
857
+ /**
858
+ *
859
+ * @param name
860
+ * @param handler
861
+ */
862
+ createEvent(name, handler) {
863
+ let _dom = this.m_dom;
864
+ let store = _dom[_x4_el_store];
865
+ if (!store) {
866
+ store = _dom[_x4_el_store] = {};
867
+ }
868
+ if (!store[name]) {
869
+ // no handler for this event...
870
+ store[name] = [handler];
871
+ }
872
+ else {
873
+ // append the handler
874
+ store[name].push(handler);
875
+ }
876
+ if (unbubbleEvents[name] === 1) {
877
+ _dom['on' + name] = Component._dispatchUnbubbleEvent;
878
+ }
879
+ else if (!Component.__privateEvents[name]) {
880
+ Component.__privateEvents[name] = true; // todo count it
881
+ if (passiveEvents[name]) {
882
+ document.addEventListener(name, Component._dispatchEvent, { passive: false, capture: true });
883
+ }
884
+ else {
885
+ document.addEventListener(name, Component._dispatchEvent, true);
886
+ }
887
+ }
888
+ if (name === 'sizechange') {
889
+ if (!Component.__sizeObserver) {
890
+ Component.__sizeObserver = new ResizeObserver(Component._observeSize);
891
+ }
892
+ Component.__sizeObserver.observe(this.m_dom);
893
+ }
894
+ }
895
+ /**
896
+ * dispatch a dom event to the appropriated component
897
+ * called by the system
898
+ */
899
+ static _dispatchEvent(ev) {
900
+ let target = ev.target, noup = unbubbleEvents[ev.type] === 2;
901
+ while (target) {
902
+ if (target[_x4_el_store]) {
903
+ let store = target[_x4_el_store][ev.type];
904
+ if (store) {
905
+ let el = target[_x4_el_sym];
906
+ let root = el?.root ?? null;
907
+ if (store instanceof Array) {
908
+ store.some((fn) => {
909
+ fn(ev, root);
910
+ if (!el.dom) {
911
+ return true;
912
+ }
913
+ });
914
+ }
915
+ else {
916
+ store(ev, root);
917
+ }
918
+ if (ev.cancelBubble || ev.defaultPrevented || noup) {
919
+ break;
920
+ }
921
+ }
922
+ }
923
+ target = target.parentNode;
924
+ // no need to go above
925
+ if (target == document) {
926
+ break;
927
+ }
928
+ }
929
+ }
930
+ /**
931
+ * dispatch a dom event to the appropriated component
932
+ * called by the system
933
+ */
934
+ static _dispatchUnbubbleEvent(ev) {
935
+ let target = ev.currentTarget || ev.target, eventType = ev.type;
936
+ let eventStore = target[_x4_el_store], store = eventStore && eventStore[eventType];
937
+ if (store) {
938
+ let el = target[_x4_el_sym];
939
+ let root = el?.root ?? null;
940
+ if (store instanceof Array) {
941
+ store.forEach((fn) => {
942
+ fn(ev, root);
943
+ });
944
+ }
945
+ else {
946
+ store(ev, root);
947
+ }
948
+ }
949
+ }
950
+ /**
951
+ * called when a size change on an observed component
952
+ */
953
+ static _observeSize(entries) {
954
+ entries.forEach((entry) => {
955
+ let dom = entry.target;
956
+ if (dom.offsetParent !== null) {
957
+ dom.dispatchEvent(new Event('sizechange'));
958
+ }
959
+ });
960
+ }
961
+ /**
962
+ * enum all children recursively
963
+ * @param recursive - if true do a full sub-child search
964
+ * @param cb - callback
965
+ * return true to stop enumeration
966
+ */
967
+ enumChilds(cb, recursive = false) {
968
+ // use dom if available
969
+ if (this.m_dom) {
970
+ let el = this.m_dom.firstChild;
971
+ while (el) {
972
+ // get component (if any)
973
+ let cel = el[_x4_el_sym];
974
+ if (cel) {
975
+ cb(cel);
976
+ if (recursive && cel.enumChilds(cb, true) === true) {
977
+ return true;
978
+ }
979
+ }
980
+ el = el.nextSibling;
981
+ }
982
+ }
983
+ else {
984
+ let content = this.m_props.content;
985
+ if (!content) {
986
+ return;
987
+ }
988
+ if (!(0, tools_1.isArray)(content)) {
989
+ content = [content];
990
+ }
991
+ content.some((el) => {
992
+ if (!el || (0, tools_1.isString)(el) || (0, tools_1.isHtmlString)(el)) {
993
+ return;
994
+ }
995
+ if (cb(el)) {
996
+ return true;
997
+ }
998
+ if (recursive && el.enumChilds(cb, true) === true) {
999
+ return true;
1000
+ }
1001
+ });
1002
+ }
1003
+ return false;
1004
+ }
1005
+ /**
1006
+ * apprend a child to the DOM
1007
+ * @param props child to append (or string)
1008
+ */
1009
+ _appendChild(el) {
1010
+ if ((0, tools_1.isString)(el)) {
1011
+ this.m_dom.insertAdjacentText('beforeend', el);
1012
+ }
1013
+ else if ((0, tools_1.isHtmlString)(el)) {
1014
+ this.m_dom.insertAdjacentHTML('beforeend', el);
1015
+ }
1016
+ else {
1017
+ let component = el;
1018
+ try {
1019
+ component._build();
1020
+ this.m_dom.appendChild(component.m_dom);
1021
+ }
1022
+ catch (e) {
1023
+ console.error(e);
1024
+ }
1025
+ }
1026
+ }
1027
+ /**
1028
+ * generate classes from the component inheritance
1029
+ * @example
1030
+ * Button extends Component will give
1031
+ * x-comp x-button
1032
+ */
1033
+ _genClassName() {
1034
+ this.addClass('@comp');
1035
+ let me = Object.getPrototypeOf(this);
1036
+ while (me && me.constructor !== Component) {
1037
+ let clsname = me.constructor.name;
1038
+ this.addClass('@' + (0, tools_1.pascalCase)(clsname));
1039
+ me = Object.getPrototypeOf(me);
1040
+ }
1041
+ this.addClass(this.m_props.cls);
1042
+ }
1043
+ /**
1044
+ * prepend the system class name prefix on a name if needed (if class starts with @)
1045
+ */
1046
+ _makeCls(cls) {
1047
+ if (cls[0] == '@') {
1048
+ return cls = _x4_ns_prefix + cls.substring(1);
1049
+ }
1050
+ else {
1051
+ return cls;
1052
+ }
1053
+ }
1054
+ /**
1055
+ *
1056
+ */
1057
+ static dispatchCaptures(event) {
1058
+ Component.__capture.handler(event);
1059
+ }
1060
+ /**
1061
+ * capture mouse events
1062
+ * @param capture name of the current capture
1063
+ * @param callback funciton to call on captured mouse events
1064
+ *
1065
+ * @example
1066
+ * Component.setCapture( this, ( ev: MouseEvent, initiator: Component ) => {
1067
+ * if( ev.type=='mousemove' ) {
1068
+ * this.setStyle( {
1069
+ * left: ev.clientX,
1070
+ * top: ev.clientY
1071
+ * } );
1072
+ * }
1073
+ * else if( ev.type=='mouseup' ) {
1074
+ * Component.releaseCapture( );
1075
+ * }
1076
+ * } );
1077
+ */
1078
+ static setCapture(initiator, listener) {
1079
+ console.assert(!Component.__capture);
1080
+ if (Component.__capture) {
1081
+ debugger;
1082
+ }
1083
+ // todo: review that
1084
+ let iframes = document.querySelectorAll("iframe");
1085
+ iframes.forEach(f => {
1086
+ flyWrap(f).setStyleValue('pointer-events', 'none');
1087
+ });
1088
+ let overs = document.querySelectorAll(":hover");
1089
+ let cursor = null;
1090
+ if (overs.length) {
1091
+ let elementOver = overs[overs.length - 1];
1092
+ let style = window.getComputedStyle(elementOver);
1093
+ cursor = style.cursor;
1094
+ }
1095
+ Component.__capture_mask = document.createElement('div');
1096
+ let mask = flyWrap(Component.__capture_mask);
1097
+ mask.addClass('@capture-mask');
1098
+ if (cursor) {
1099
+ mask.setStyleValue('cursor', cursor);
1100
+ }
1101
+ document.body.appendChild(mask.dom);
1102
+ document.addEventListener('mousedown', Component.dispatchCaptures);
1103
+ document.addEventListener('mousemove', Component.dispatchCaptures);
1104
+ document.addEventListener('mouseup', Component.dispatchCaptures);
1105
+ document.addEventListener('touchstart', Component.dispatchCaptures);
1106
+ document.addEventListener('touchmove', Component.dispatchCaptures);
1107
+ document.addEventListener('touchend', Component.dispatchCaptures);
1108
+ Component.__capture = {
1109
+ initiator,
1110
+ handler: listener,
1111
+ iframes
1112
+ };
1113
+ }
1114
+ static releaseCapture() {
1115
+ console.assert(!!Component.__capture);
1116
+ document.removeEventListener('touchstart', Component.dispatchCaptures);
1117
+ document.removeEventListener('touchmove', Component.dispatchCaptures);
1118
+ document.removeEventListener('touchend', Component.dispatchCaptures);
1119
+ document.removeEventListener('mousedown', Component.dispatchCaptures);
1120
+ document.removeEventListener('mousemove', Component.dispatchCaptures);
1121
+ document.removeEventListener('mouseup', Component.dispatchCaptures);
1122
+ Component.__capture.iframes.forEach(f => {
1123
+ flyWrap(f).setStyleValue('pointer-events', null);
1124
+ });
1125
+ Component.__capture = null;
1126
+ if (Component.__capture_mask) {
1127
+ document.body.removeChild(Component.__capture_mask);
1128
+ Component.__capture_mask = null;
1129
+ }
1130
+ }
1131
+ /**
1132
+ * ensure the component is visible
1133
+ * @param: alignToTop
1134
+ */
1135
+ scrollIntoView(arg) {
1136
+ if (this.m_dom) {
1137
+ const rel = new tools_1.Rect(this.dom.getBoundingClientRect());
1138
+ let top = undefined;
1139
+ let bot = undefined;
1140
+ let left = undefined;
1141
+ let right = undefined;
1142
+ let pn = this.dom.parentElement;
1143
+ while (pn && pn != document.body) {
1144
+ const pr = pn.getBoundingClientRect();
1145
+ if (top === undefined || top < pr.top) {
1146
+ top = pr.top;
1147
+ }
1148
+ if (bot === undefined || bot > pr.bottom) {
1149
+ bot = pr.bottom;
1150
+ }
1151
+ if (left === undefined || left < pr.left) {
1152
+ left = pr.left;
1153
+ }
1154
+ if (right === undefined || right > pr.right) {
1155
+ right = pr.right;
1156
+ }
1157
+ pn = pn.parentElement;
1158
+ }
1159
+ if (top === undefined || rel.top < top || rel.bottom > bot || rel.left < left || rel.right > right) {
1160
+ //this.m_dom.scrollIntoView( true );
1161
+ this.m_dom.scrollIntoView({ behavior: 'auto', block: 'nearest', inline: 'start' });
1162
+ }
1163
+ //this.m_dom.scrollIntoView(arg);
1164
+ }
1165
+ }
1166
+ /**
1167
+ * search for a given css selector
1168
+ * @param selector
1169
+ * @returns child or null
1170
+ */
1171
+ queryItem(selector) {
1172
+ let result = this.dom.querySelector(selector);
1173
+ return result ? Component.getElement(result) : null;
1174
+ }
1175
+ queryAll(selector, cb) {
1176
+ const elements = this.m_dom.querySelectorAll(selector);
1177
+ if (cb) {
1178
+ elements.forEach((el) => {
1179
+ cb(flyWrap(el));
1180
+ });
1181
+ }
1182
+ return elements;
1183
+ }
1184
+ /**
1185
+ * find a child with the given ID
1186
+ * @param id id (without '#')
1187
+ * @returns child or null
1188
+ *
1189
+ * @example
1190
+ * let btn = this.childWithId<Button>( 'myButtonId' );
1191
+ */
1192
+ itemWithId(id) {
1193
+ let result = this.dom.querySelector('#' + id);
1194
+ return result ? Component.getElement(result) : null;
1195
+ }
1196
+ /**
1197
+ * find a child with given ref
1198
+ * @param ref
1199
+ * @return found child or null
1200
+ */
1201
+ itemWithRef(ref) {
1202
+ let result = null;
1203
+ this.enumChilds((c) => {
1204
+ if (c.m_props.ref === ref) {
1205
+ result = c;
1206
+ return true;
1207
+ }
1208
+ }, true);
1209
+ return result;
1210
+ }
1211
+ /**
1212
+ *
1213
+ */
1214
+ get ref() {
1215
+ return this.m_props.ref;
1216
+ }
1217
+ /**
1218
+ *
1219
+ */
1220
+ static getCss() {
1221
+ if (!Component.__css) {
1222
+ Component.__css = new styles_1.Stylesheet();
1223
+ }
1224
+ return Component.__css;
1225
+ }
1226
+ /**
1227
+ * return the parent element
1228
+ * care, object must have been created (dom!=null)
1229
+ */
1230
+ getParent() {
1231
+ console.assert(!!this.m_dom);
1232
+ let elParent = this.dom.parentNode;
1233
+ return Component.getElement(elParent);
1234
+ }
1235
+ /**
1236
+ * get a component from a DOM element
1237
+ * move up to the hierarchy to find the request class type.
1238
+ * @param dom
1239
+ * @param classname
1240
+ * @returns
1241
+ *
1242
+ * @example
1243
+ *
1244
+ * with a DOM like that:
1245
+ * Button
1246
+ * Label
1247
+ * Icon <- the DOM you have (dom-icon)
1248
+ *
1249
+ * let btn = Component.getElement( dom-icon, Button );
1250
+ */
1251
+ static getElement(dom, classname) {
1252
+ if (classname) {
1253
+ const srhCls = (0, tools_1.isString)(classname);
1254
+ while (dom) {
1255
+ let el = dom[_x4_el_sym];
1256
+ if (srhCls) {
1257
+ if (el && el.hasClass(classname)) {
1258
+ return el;
1259
+ }
1260
+ }
1261
+ else if (el instanceof classname) {
1262
+ return el;
1263
+ }
1264
+ dom = dom.parentElement;
1265
+ }
1266
+ return null;
1267
+ }
1268
+ else {
1269
+ return dom ? dom[_x4_el_sym] : null;
1270
+ }
1271
+ }
1272
+ /**
1273
+ * compute the scrollbar size ( width = height)
1274
+ */
1275
+ static getScrollbarSize() {
1276
+ if (Component.__sb_width === undefined) {
1277
+ let outerDiv = document.createElement('div');
1278
+ outerDiv.style.cssText = 'overflow:auto;position:absolute;top:0;width:100px;height:100px';
1279
+ let innerDiv = document.createElement('div');
1280
+ innerDiv.style.width = '200px';
1281
+ innerDiv.style.height = '200px';
1282
+ outerDiv.appendChild(innerDiv);
1283
+ document.body.appendChild(outerDiv);
1284
+ Component.__sb_width = outerDiv.offsetWidth - outerDiv.clientWidth;
1285
+ document.body.removeChild(outerDiv);
1286
+ }
1287
+ return Component.__sb_width;
1288
+ }
1289
+ /**
1290
+ * check if the Component is visible to the user
1291
+ */
1292
+ isUserVisible() {
1293
+ if (!this.m_dom) {
1294
+ return false;
1295
+ }
1296
+ return (this.m_dom.offsetParent !== null);
1297
+ }
1298
+ }
1299
+ exports.Component = Component;
1300
+ /** @ignore */
1301
+ let fly_element = null;
1302
+ /**
1303
+ * warp <b>temporarily</b> a DOM element to be able to acces to exact API
1304
+ * @param dom dom element to wrap
1305
+ * @review qui libere le fly_element ? -> timeout
1306
+ */
1307
+ function flyWrap(dom) {
1308
+ if (dom[_x4_el_sym]) {
1309
+ return dom[_x4_el_sym];
1310
+ }
1311
+ let f = fly_element;
1312
+ if (!f) {
1313
+ f = fly_element = new Component({});
1314
+ }
1315
+ f.m_dom = dom;
1316
+ return f;
1317
+ }
1318
+ exports.flyWrap = flyWrap;
1319
+ /**
1320
+ * simple flex spacer
1321
+ */
1322
+ class Flex extends Component {
1323
+ constructor(props = {}) {
1324
+ if (!props.flex) {
1325
+ props.flex = 1;
1326
+ }
1327
+ super(props);
1328
+ }
1329
+ }
1330
+ exports.Flex = Flex;
1331
+ /**
1332
+ * simple space between 2 elements
1333
+ */
1334
+ class Space extends Component {
1335
+ m_size;
1336
+ constructor(size) {
1337
+ super({});
1338
+ this.m_size = size;
1339
+ }
1340
+ componentCreated() {
1341
+ // try to find if we are in a hz / vt / abs container
1342
+ let dom = this.dom;
1343
+ let style = null;
1344
+ while (dom) {
1345
+ let el = dom[_x4_el_sym];
1346
+ if (el.hasClass('@hlayout')) {
1347
+ style = { width: this.m_size };
1348
+ break;
1349
+ }
1350
+ else if (el.hasClass('@vlayout')) {
1351
+ style = { height: this.m_size };
1352
+ break;
1353
+ }
1354
+ dom = dom.parentElement;
1355
+ }
1356
+ if (!style) {
1357
+ style = { width: this.m_size, height: this.m_size };
1358
+ }
1359
+ this.setStyle(style);
1360
+ }
1361
+ }
1362
+ exports.Space = Space;
1363
+ function EvSize(size, mode = null, context = null) {
1364
+ return (0, x4_events_1.BasicEvent)({ size, mode, context });
1365
+ }
1366
+ exports.EvSize = EvSize;
1367
+ class Separator extends Component {
1368
+ m_irect;
1369
+ m_delta;
1370
+ m_target;
1371
+ constructor(props) {
1372
+ super(props);
1373
+ this.setDomEvent('mousedown', (e) => this._mousedown(e));
1374
+ this.setDomEvent('touchstart', (e) => this._mousedown(e));
1375
+ this.setDomEvent('dblclick', (e) => this._collapse(e));
1376
+ }
1377
+ render() {
1378
+ this.addClass(this.m_props.orientation);
1379
+ }
1380
+ _collapse(ev) {
1381
+ if (this.m_props.collapsible) {
1382
+ this._findTarget();
1383
+ if (this.m_target) {
1384
+ this.m_target.toggleClass('@collapsed');
1385
+ }
1386
+ }
1387
+ }
1388
+ _mousedown(ev) {
1389
+ if (ev.type == 'touchstart') {
1390
+ let te = ev;
1391
+ if (te.touches.length == 1) {
1392
+ this._startMoving(te.touches[0].pageX, te.touches[0].pageY, ev);
1393
+ }
1394
+ }
1395
+ else {
1396
+ let me = ev;
1397
+ this._startMoving(me.pageX, me.pageY, ev);
1398
+ }
1399
+ }
1400
+ _startMoving(x, y, ev) {
1401
+ //if( this.m_props.callback ) {
1402
+ // this.m_props.callback( ev, this );
1403
+ //}
1404
+ //else
1405
+ {
1406
+ this._findTarget();
1407
+ if (this.m_target) {
1408
+ if (this.m_props.orientation == 'horizontal') {
1409
+ if (this.m_props.sizing == 'before') {
1410
+ this.m_delta = x - this.m_irect.right;
1411
+ }
1412
+ else {
1413
+ this.m_delta = x - this.m_irect.left;
1414
+ }
1415
+ }
1416
+ else {
1417
+ if (this.m_props.sizing == 'before') {
1418
+ this.m_delta = y - this.m_irect.bottom;
1419
+ }
1420
+ else {
1421
+ this.m_delta = y - this.m_irect.top;
1422
+ }
1423
+ }
1424
+ ev.preventDefault();
1425
+ ev.stopPropagation();
1426
+ this.m_target.addClass('sizing');
1427
+ Component.setCapture(this, (e) => this._pointerMoved(e));
1428
+ }
1429
+ }
1430
+ }
1431
+ _pointerMoved(ev) {
1432
+ let __move = (ex, ey) => {
1433
+ if (this.m_props.orientation == 'horizontal') {
1434
+ let width;
1435
+ if (this.m_props.sizing == 'after') {
1436
+ width = this.m_irect.right - (ex - this.m_delta);
1437
+ }
1438
+ else {
1439
+ width = (ex - this.m_delta) - this.m_irect.left;
1440
+ }
1441
+ if (width > 0) {
1442
+ let size = new tools_1.Size(width, 0);
1443
+ this.emit('resize', EvSize(size));
1444
+ this.m_target.setStyleValue('width', size.width);
1445
+ this.m_target.setStyleValue('flex', null); // for flex>1
1446
+ this.m_target.removeClass('@flex');
1447
+ }
1448
+ }
1449
+ else {
1450
+ let height;
1451
+ if (this.m_props.sizing == 'after') {
1452
+ height = this.m_irect.bottom - (ey - this.m_delta);
1453
+ }
1454
+ else {
1455
+ height = (ey - this.m_delta) - this.m_irect.top;
1456
+ }
1457
+ if (height > 0) {
1458
+ let size = new tools_1.Size(0, height);
1459
+ this.emit('resize', EvSize(size));
1460
+ this.m_target.setStyleValue('height', size.height);
1461
+ this.m_target.setStyleValue('flex', null); // for flex>1
1462
+ this.m_target.removeClass('@flex');
1463
+ }
1464
+ }
1465
+ };
1466
+ if (ev.type == 'mousemove') {
1467
+ let mev = ev;
1468
+ __move(mev.pageX, mev.pageY);
1469
+ ev.preventDefault();
1470
+ ev.stopPropagation();
1471
+ }
1472
+ else if (ev.type == 'touchmove') {
1473
+ let tev = ev;
1474
+ __move(tev.touches[0].pageX, tev.touches[0].pageY);
1475
+ ev.preventDefault();
1476
+ ev.stopPropagation();
1477
+ }
1478
+ else if (ev.type == 'mouseup' || ev.type == 'touchend') {
1479
+ this.m_target.removeClass('sizing');
1480
+ Component.releaseCapture();
1481
+ ev.preventDefault();
1482
+ ev.stopPropagation();
1483
+ }
1484
+ }
1485
+ _findTarget() {
1486
+ if (!this.m_target) {
1487
+ if (this.m_props.sizing == 'before') {
1488
+ let prevDom = this.dom.previousElementSibling;
1489
+ let prevEl = prevDom ? Component.getElement(prevDom) : null;
1490
+ this.m_target = prevEl;
1491
+ }
1492
+ else {
1493
+ let nextDom = this.dom.nextElementSibling;
1494
+ let nextEl = nextDom ? Component.getElement(nextDom) : null;
1495
+ this.m_target = nextEl;
1496
+ }
1497
+ }
1498
+ if (this.m_target) {
1499
+ this.m_irect = this.m_target.getBoundingRect();
1500
+ }
1501
+ else {
1502
+ this.m_irect = null;
1503
+ }
1504
+ }
1505
+ }
1506
+ exports.Separator = Separator;
1507
+ function EvOverlayResize(ui_event, sens, context = null) {
1508
+ return (0, x4_events_1.BasicEvent)({ ui_event, sens, context });
1509
+ }
1510
+ exports.EvOverlayResize = EvOverlayResize;
1511
+ class SizerOverlay extends Component {
1512
+ m_delta;
1513
+ m_irect;
1514
+ constructor(props) {
1515
+ super(props);
1516
+ this.addClass(props.sens);
1517
+ this.setDomEvent('mousedown', (e) => this._mousedown(e));
1518
+ this.setDomEvent('touchstart', (e) => this._mousedown(e));
1519
+ this.setDomEvent('dblclick', (e) => this.resetflex(e)); // todo: add option for that
1520
+ props.target.appendChild(this);
1521
+ if (props.resize) {
1522
+ this.on('resize', this.m_props.resize);
1523
+ }
1524
+ }
1525
+ resetflex(event) {
1526
+ this.m_props.target.addClass('@flex');
1527
+ this.emit('resize', EvSize({ width: -1, height: 0 })); // todo: see that
1528
+ event.preventDefault();
1529
+ event.stopPropagation();
1530
+ }
1531
+ // @review move that in component
1532
+ _mousedown(ev) {
1533
+ ev.preventDefault();
1534
+ ev.stopPropagation();
1535
+ let eev = EvOverlayResize(ev, this.m_props.sens);
1536
+ this.emit('rawresize', eev);
1537
+ if (eev.defaultPrevented) {
1538
+ return;
1539
+ }
1540
+ let pos = (0, tools_1.getMousePos)(ev, true);
1541
+ this.m_irect = this.m_props.target.getBoundingRect();
1542
+ if (this.m_props.sens == 'right') {
1543
+ this.m_delta = pos.x - this.m_irect.right;
1544
+ }
1545
+ else if (this.m_props.sens == 'left') {
1546
+ this.m_delta = pos.x - this.m_irect.left;
1547
+ }
1548
+ else if (this.m_props.sens == 'bottom') {
1549
+ this.m_delta = pos.y - this.m_irect.bottom;
1550
+ }
1551
+ else if (this.m_props.sens == 'top') {
1552
+ this.m_delta = pos.y - this.m_irect.top;
1553
+ }
1554
+ this.m_props.target.addClass('sizing');
1555
+ Component.setCapture(this, (e) => this._handle_mouse(e));
1556
+ }
1557
+ _is_horz() {
1558
+ return this.m_props.sens == 'left' || this.m_props.sens == 'right';
1559
+ }
1560
+ get sens() {
1561
+ return this.m_props.sens;
1562
+ }
1563
+ _handle_mouse(ev) {
1564
+ let __move = (ex, ey) => {
1565
+ if (this._is_horz()) {
1566
+ let width;
1567
+ if (this.m_props.sens == 'left') {
1568
+ width = this.m_irect.right - (ex - this.m_delta);
1569
+ }
1570
+ else {
1571
+ width = (ex - this.m_delta) - this.m_irect.left;
1572
+ }
1573
+ if (width > 0) {
1574
+ let size = {
1575
+ width,
1576
+ height: undefined
1577
+ };
1578
+ this.emit('resize', EvSize(size));
1579
+ this.m_props.target.setStyleValue('width', size.width);
1580
+ this.m_props.target.setStyleValue('flex', null); // for flex>1
1581
+ this.m_props.target.removeClass('@flex');
1582
+ }
1583
+ }
1584
+ else {
1585
+ let height;
1586
+ if (this.m_props.sens == 'top') {
1587
+ height = this.m_irect.bottom - (ey - this.m_delta);
1588
+ }
1589
+ else {
1590
+ height = (ey - this.m_delta) - this.m_irect.top;
1591
+ }
1592
+ if (height > 0) {
1593
+ let size = new tools_1.Size(0, height);
1594
+ this.emit('resize', EvSize(size));
1595
+ this.m_props.target.setStyleValue('height', size.height);
1596
+ this.m_props.target.setStyleValue('flex', null); // for flex>1
1597
+ this.m_props.target.removeClass('@flex');
1598
+ }
1599
+ }
1600
+ };
1601
+ if (ev.type == 'mousemove') {
1602
+ let mev = ev;
1603
+ __move(mev.pageX, mev.pageY);
1604
+ ev.preventDefault();
1605
+ ev.stopPropagation();
1606
+ }
1607
+ else if (ev.type == 'touchmove') {
1608
+ let tev = ev;
1609
+ __move(tev.touches[0].pageX, tev.touches[0].pageY);
1610
+ ev.preventDefault();
1611
+ ev.stopPropagation();
1612
+ }
1613
+ else if (ev.type == 'mouseup' || ev.type == 'touchend') {
1614
+ this.m_props.target.removeClass('sizing');
1615
+ Component.releaseCapture();
1616
+ ev.preventDefault();
1617
+ ev.stopPropagation();
1618
+ }
1619
+ }
1620
+ }
1621
+ exports.SizerOverlay = SizerOverlay;
1622
+ function EvShortcut(name) {
1623
+ return (0, x4_events_1.BasicEvent)({ name });
1624
+ }
1625
+ /**
1626
+ * you can construct a Container as usual with it's properties but also directly with it's children array
1627
+ *
1628
+ * @example
1629
+ * new Container( [
1630
+ * child1,
1631
+ * child2
1632
+ * ])
1633
+ */
1634
+ class Container extends Component {
1635
+ m_shortcuts;
1636
+ constructor(props) {
1637
+ if ((0, tools_1.isArray)(props)) {
1638
+ super({ content: props });
1639
+ }
1640
+ else {
1641
+ super(props);
1642
+ }
1643
+ }
1644
+ /**
1645
+ * add an application shortcut
1646
+ * @param sequence key sequence Shift+Ctrl+Alt+K
1647
+ * @param callback callback to call
1648
+ */
1649
+ addShortcut(sequence, name, callback = null, immediate = false) {
1650
+ // first time
1651
+ if (!this.m_shortcuts) {
1652
+ this.m_shortcuts = [];
1653
+ this.setDomEvent('keydown', (e) => this._handleKeydown(e));
1654
+ }
1655
+ if (!(0, tools_1.isArray)(sequence)) {
1656
+ sequence = [sequence];
1657
+ }
1658
+ sequence.forEach((seq) => {
1659
+ let reseq = '';
1660
+ let shift = seq.match(/SHIFT/i);
1661
+ if (shift) {
1662
+ seq = seq.replace(/SHIFT/i, '');
1663
+ reseq += 'shift+';
1664
+ }
1665
+ let ctrl = seq.match(/CTRL/i);
1666
+ if (ctrl) {
1667
+ seq = seq.replace(/CTRL/i, '');
1668
+ reseq += 'ctrl+';
1669
+ }
1670
+ let alt = seq.match(/ALT/i);
1671
+ if (alt) {
1672
+ seq = seq.replace(/ALT/i, '');
1673
+ reseq += 'alt+';
1674
+ }
1675
+ reseq += seq.replace('+', '').toLowerCase();
1676
+ this.m_shortcuts.push({
1677
+ sequence: reseq,
1678
+ name,
1679
+ immediate,
1680
+ callback
1681
+ });
1682
+ });
1683
+ }
1684
+ /**
1685
+ * remove all shortcuts for a target
1686
+ */
1687
+ removeShortcuts() {
1688
+ if (this.m_shortcuts) {
1689
+ this.m_shortcuts = [];
1690
+ }
1691
+ }
1692
+ /** @ignore this function is binded */
1693
+ _handleKeydown(e) {
1694
+ if (!this.m_shortcuts) {
1695
+ return;
1696
+ }
1697
+ let seq = '';
1698
+ if (e.shiftKey) {
1699
+ seq += 'shift+';
1700
+ }
1701
+ if (e.ctrlKey) {
1702
+ seq += 'ctrl+';
1703
+ }
1704
+ if (e.altKey) {
1705
+ seq += 'alt+';
1706
+ }
1707
+ seq += e.key.toLowerCase();
1708
+ //console.log( seq );
1709
+ this.m_shortcuts.some((sk) => {
1710
+ if (sk.sequence == seq) {
1711
+ if (sk.callback) {
1712
+ if (sk.immediate) {
1713
+ sk.callback(e);
1714
+ }
1715
+ else {
1716
+ (0, tools_1.asap)(() => { sk.callback(e); });
1717
+ }
1718
+ }
1719
+ else {
1720
+ this.emit('shortcut', EvShortcut(sk.name));
1721
+ }
1722
+ e.preventDefault();
1723
+ e.stopPropagation();
1724
+ return true;
1725
+ }
1726
+ });
1727
+ }
1728
+ }
1729
+ exports.Container = Container;