x4js 1.4.3 → 1.4.4

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