x4js 2.0.11 → 2.0.13

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 (109) hide show
  1. package/README.md +15 -15
  2. package/lib/README.txt +15 -15
  3. package/lib/cjs/x4.css +1 -1
  4. package/lib/cjs/x4.js +1 -1
  5. package/lib/esm/x4.css +1 -1
  6. package/lib/esm/x4.mjs +1 -1
  7. package/lib/src/components/base.scss +25 -26
  8. package/lib/src/components/boxes/boxes.module.scss +37 -37
  9. package/lib/src/components/boxes/boxes.ts +129 -125
  10. package/lib/src/components/breadcrumb/breadcrumb.scss +28 -0
  11. package/lib/src/components/breadcrumb/breadcrumb.ts +84 -0
  12. package/lib/src/components/breadcrumb/chevron-right.svg +1 -0
  13. package/lib/src/components/btngroup/btngroup.module.scss +28 -28
  14. package/lib/src/components/btngroup/btngroup.ts +119 -101
  15. package/lib/src/components/button/button.module.scss +154 -153
  16. package/lib/src/components/button/button.ts +117 -117
  17. package/lib/src/components/calendar/calendar.module.scss +162 -162
  18. package/lib/src/components/calendar/calendar.ts +326 -325
  19. package/lib/src/components/checkbox/check.svg +3 -3
  20. package/lib/src/components/checkbox/checkbox.module.scss +141 -141
  21. package/lib/src/components/checkbox/checkbox.ts +125 -124
  22. package/lib/src/components/colorinput/colorinput.module.scss +64 -64
  23. package/lib/src/components/colorinput/colorinput.ts +90 -87
  24. package/lib/src/components/colorpicker/colorpicker.module.scss +132 -132
  25. package/lib/src/components/colorpicker/colorpicker.ts +481 -476
  26. package/lib/src/components/combobox/combobox.module.scss +123 -120
  27. package/lib/src/components/combobox/combobox.ts +192 -190
  28. package/lib/src/components/combobox/updown.svg +3 -3
  29. package/lib/src/components/components.ts +34 -0
  30. package/lib/src/components/dialog/dialog.module.scss +71 -71
  31. package/lib/src/components/dialog/dialog.ts +94 -92
  32. package/lib/src/components/form/form.module.scss +34 -34
  33. package/lib/src/components/form/form.ts +41 -36
  34. package/lib/src/components/grid/datastore.ts +1298 -0
  35. package/lib/src/components/grid/gridview.ts +1108 -0
  36. package/lib/src/components/grid/memdb.ts +325 -0
  37. package/lib/src/components/header/header.module.scss +39 -39
  38. package/lib/src/components/header/header.ts +129 -123
  39. package/lib/src/components/icon/icon.module.scss +29 -29
  40. package/lib/src/components/icon/icon.ts +136 -134
  41. package/lib/src/components/image/image.module.scss +20 -20
  42. package/lib/src/components/image/image.ts +68 -66
  43. package/lib/src/components/input/input.module.scss +69 -69
  44. package/lib/src/components/input/input.ts +275 -274
  45. package/lib/src/components/label/label.module.scss +58 -52
  46. package/lib/src/components/label/label.ts +64 -55
  47. package/lib/src/components/link/link.ts +78 -0
  48. package/lib/src/components/listbox/listbox.module.scss +103 -103
  49. package/lib/src/components/listbox/listbox.ts +431 -427
  50. package/lib/src/components/menu/menu.module.scss +107 -107
  51. package/lib/src/components/menu/menu.ts +171 -168
  52. package/lib/src/components/messages/messages.module.scss +48 -47
  53. package/lib/src/components/messages/messages.ts +68 -63
  54. package/lib/src/components/normalize.scss +386 -386
  55. package/lib/src/components/notification/notification.module.scss +81 -81
  56. package/lib/src/components/notification/notification.ts +109 -108
  57. package/lib/src/components/panel/panel.module.scss +47 -47
  58. package/lib/src/components/panel/panel.ts +57 -56
  59. package/lib/src/components/popup/popup.module.scss +43 -43
  60. package/lib/src/components/popup/popup.ts +396 -395
  61. package/lib/src/components/progress/progress.module.scss +56 -56
  62. package/lib/src/components/progress/progress.ts +43 -42
  63. package/lib/src/components/rating/rating.module.scss +22 -22
  64. package/lib/src/components/rating/rating.ts +131 -125
  65. package/lib/src/components/shared.scss +90 -76
  66. package/lib/src/components/sizers/sizer.module.scss +89 -89
  67. package/lib/src/components/sizers/sizer.ts +123 -119
  68. package/lib/src/components/slider/slider.module.scss +70 -70
  69. package/lib/src/components/slider/slider.ts +147 -142
  70. package/lib/src/components/switch/switch.module.scss +126 -126
  71. package/lib/src/components/switch/switch.ts +61 -55
  72. package/lib/src/components/tabs/tabs.module.scss +46 -46
  73. package/lib/src/components/tabs/tabs.ts +168 -157
  74. package/lib/src/components/textarea/textarea.module.scss +59 -59
  75. package/lib/src/components/textarea/textarea.ts +60 -54
  76. package/lib/src/components/textedit/textedit.module.scss +113 -113
  77. package/lib/src/components/textedit/textedit.ts +83 -82
  78. package/lib/src/components/themes.scss +81 -77
  79. package/lib/src/components/tooltips/tooltips.scss +50 -50
  80. package/lib/src/components/tooltips/tooltips.ts +103 -102
  81. package/lib/src/components/treeview/treeview.module.scss +115 -115
  82. package/lib/src/components/treeview/treeview.ts +410 -403
  83. package/lib/src/components/viewport/viewport.module.scss +24 -24
  84. package/lib/src/components/viewport/viewport.ts +41 -38
  85. package/lib/src/core/component.ts +1002 -979
  86. package/lib/src/core/core_application.ts +44 -0
  87. package/lib/src/core/core_colors.ts +249 -249
  88. package/lib/src/core/core_dom.ts +471 -471
  89. package/lib/src/core/core_dragdrop.ts +200 -200
  90. package/lib/src/core/core_element.ts +97 -97
  91. package/lib/src/core/core_events.ts +149 -149
  92. package/lib/src/core/core_i18n.ts +377 -377
  93. package/lib/src/core/core_router.ts +221 -221
  94. package/lib/src/core/core_styles.ts +214 -214
  95. package/lib/src/core/core_svg.ts +550 -550
  96. package/lib/src/core/core_tools.ts +688 -673
  97. package/lib/src/demo/assets/radio.svg +3 -3
  98. package/lib/src/demo/index.html +11 -11
  99. package/lib/src/demo/main.scss +21 -21
  100. package/lib/src/demo/main.tsx +323 -323
  101. package/lib/src/types/scss.d.ts +4 -4
  102. package/lib/src/types/x4react.d.ts +8 -8
  103. package/lib/src/x4.scss +18 -18
  104. package/lib/src/x4.ts +31 -60
  105. package/lib/styles/x4.css +1 -1
  106. package/lib/types/x4js.d.ts +100 -49
  107. package/package.json +2 -3
  108. package/src/x4.ts +31 -60
  109. package/lib/output.d.ts +0 -1472
@@ -1,979 +1,1002 @@
1
- /**
2
- * ___ ___ __
3
- * \ \/ / / _
4
- * \ / /_| |_
5
- * / \____ _|
6
- * /__/\__\ |_|
7
- *
8
- * @file component.ts
9
- * @author Etienne Cochard
10
- *
11
- * @copyright (c) 2024 R-libre ingenierie
12
- *
13
- * Use of this source code is governed by an MIT-style license
14
- * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
15
- **/
16
-
17
- import { isArray, UnsafeHtml, isNumber, Rect, Constructor } from './core_tools';
18
- import { CoreElement } from './core_element';
19
- import { ariaValues, unitless } from './core_styles';
20
- import { CoreEvent, EventMap } from './core_events';
21
- import { addEvent, DOMEventHandler, GlobalDOMEvents } from './core_dom';
22
-
23
- interface RefType<T extends Component> {
24
- dom: T;
25
- }
26
-
27
- type ComponentAttributes = Record<string,string|number|boolean>;
28
-
29
- const FRAGMENT = Symbol( "fragment" );
30
- const COMPONENT = Symbol( "component" );
31
-
32
- const RE_NUMBER = /^-?\d+(\.\d*)?$/;
33
-
34
- /**
35
- *
36
- */
37
-
38
- function genClassNames( x: any ) {
39
-
40
- let classes = [];
41
- let self = Object.getPrototypeOf(x);
42
-
43
- while (self && self.constructor !== Component ) {
44
- let clsname:string = self.constructor.name;
45
- classes.push( 'x4'+clsname.toLowerCase() );
46
- self = Object.getPrototypeOf(self);
47
- }
48
-
49
- return classes;
50
- }
51
-
52
- /**
53
- *
54
- */
55
-
56
- export type ComponentContent = Component | string | UnsafeHtml | number | boolean | Component[];
57
-
58
- let gen_id = 1000;
59
-
60
- export const makeUniqueComponentId = ( ) => {
61
- return `x4-${gen_id++}`;
62
- }
63
-
64
- /**
65
- *
66
- */
67
-
68
- export interface ComponentProps {
69
- tag?: string;
70
- ns?: string;
71
-
72
- style?: Partial<CSSStyleDeclaration>;
73
- attrs?: Record<string,string|number|boolean>;
74
- content?: ComponentContent;
75
- dom_events?: GlobalDOMEvents;
76
- cls?: string;
77
- id?: string;
78
- ref?: RefType<any>;
79
-
80
- // shortcuts
81
- width?: string | number;
82
- height?: string | number;
83
- disabled?: true,
84
- hidden?: true,
85
-
86
- tooltip?: string;
87
-
88
- // wrapper
89
- existingDOM?: HTMLElement;
90
-
91
- // index signature
92
- // to avoid errors: Type 'X' has no properties in common with type 'Y'
93
- // because all memebers here are optional.
94
- // this allow TS to recongnize derived props as ComponentProps
95
- //[key: string]: any;
96
- };
97
-
98
-
99
- /**
100
- *
101
- */
102
-
103
- export interface ComponentEvent extends CoreEvent {
104
- }
105
-
106
- /**
107
- *
108
- */
109
-
110
- export interface ComponentEvents extends EventMap {
111
- }
112
-
113
- /**
114
- *
115
- */
116
-
117
- export class Component<P extends ComponentProps = ComponentProps, E extends ComponentEvents = ComponentEvents>
118
- extends CoreElement<E> {
119
-
120
- readonly dom: Element;
121
- readonly props: P;
122
- private store: Map<string|Symbol,any>;
123
-
124
- constructor( props: P ) {
125
- super( );
126
-
127
- this.props = props; // copy ?
128
-
129
- if( props.existingDOM ) {
130
- this.dom = props.existingDOM;
131
- }
132
- else {
133
- if( props.ns ) {
134
- this.dom = document.createElementNS( props.ns, props.tag ?? "div" );
135
- }
136
- else {
137
- this.dom = document.createElement( props.tag ?? "div" );
138
- }
139
-
140
- if (props.attrs) {
141
- this.setAttributes( props.attrs );
142
- }
143
-
144
- if( props.cls ) {
145
- this.addClass( props.cls );
146
- }
147
-
148
- if( props.hidden ) {
149
- this.show( false );
150
- }
151
-
152
- if( props.id!==undefined ) {
153
- this.setAttribute( "id", props.id );
154
- }
155
-
156
- // small shortcut
157
- if( props.width!==undefined ) {
158
- this.setStyleValue( "width", props.width );
159
- }
160
-
161
- if( props.height!==undefined ) {
162
- this.setStyleValue( "height", props.height );
163
- }
164
-
165
- if( props.tooltip ) {
166
- this.setAttribute( "tooltip", props.tooltip );
167
- }
168
-
169
- if( props.style ) {
170
- this.setStyle( props.style );
171
- }
172
-
173
- if( props.content ) {
174
- this.setContent( props.content );
175
- }
176
-
177
- if( props.dom_events ) {
178
- this.setDOMEvents( props.dom_events );
179
- }
180
-
181
- const classes = genClassNames( this );
182
- this.dom.classList.add( ...classes );
183
-
184
- // need to have children for next statements
185
- // and children way be created in caller
186
- if( props.disabled ) {
187
- this.addDOMEvent( "created", ( ) => {
188
- this.enable( false );
189
- } );
190
- }
191
- }
192
-
193
- (this.dom as any)[COMPONENT] = this;
194
- }
195
-
196
-
197
- // :: CLASSES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
198
-
199
- /**
200
- *
201
- */
202
-
203
- hasClass( cls: string ) {
204
- return this.dom.classList.contains( cls );
205
- }
206
-
207
- /**
208
- *
209
- */
210
-
211
- addClass( cls: string ) {
212
- if( !cls ) return;
213
-
214
- if( cls.indexOf(' ')>=0 ) {
215
- const ccs = cls.split( " " );
216
- this.dom.classList.add(...ccs);
217
- }
218
- else {
219
- this.dom.classList.add(cls);
220
- }
221
- }
222
-
223
- /**
224
- *
225
- */
226
-
227
- removeClass( cls: string ) {
228
- if( !cls ) return;
229
-
230
- if( cls.indexOf(' ')>=0 ) {
231
- const ccs = cls.split( " " );
232
- this.dom.classList.remove(...ccs);
233
- }
234
- else {
235
- this.dom.classList.remove(cls);
236
- }
237
- }
238
-
239
- /**
240
- *
241
- */
242
-
243
- toggleClass( cls: string ) {
244
- if( !cls ) return;
245
-
246
- const toggle = ( x: string ) => {
247
- this.dom.classList.toggle(x);
248
- }
249
-
250
- if( cls.indexOf(' ')>=0 ) {
251
- const ccs = cls.split( " " );
252
- ccs.forEach( toggle );
253
- }
254
- else {
255
- toggle( cls );
256
- }
257
- }
258
-
259
- /**
260
- *
261
- */
262
-
263
- setClass( cls: string, set: boolean = true ) {
264
- if( set ) this.addClass(cls);
265
- else this.removeClass( cls );
266
- }
267
-
268
- // :: ATTRIBUTES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
269
-
270
- /**
271
- * attributes
272
- */
273
-
274
- setAttributes( attrs: ComponentAttributes ) {
275
-
276
- for( const name in attrs ) {
277
- const value = attrs[name];
278
- this.setAttribute( name, value );
279
- }
280
- }
281
-
282
- /**
283
- *
284
- */
285
-
286
- setAttribute( name: string, value: string | number | boolean ) {
287
- if( value===null || value===undefined ) {
288
- this.dom.removeAttribute( name );
289
- }
290
- else {
291
- this.dom.setAttribute( name, ""+value );
292
- }
293
- }
294
-
295
- /**
296
- *
297
- */
298
-
299
- getAttribute( name: string ): string {
300
- return this.dom.getAttribute( name );
301
- }
302
-
303
- /**
304
- *
305
- */
306
-
307
- getData( name: string ) : string {
308
- return this.getAttribute( "data-"+name );
309
- }
310
-
311
- /**
312
- *
313
- */
314
-
315
- setData( name: string, value: string ) {
316
- return this.setAttribute( "data-"+name, value );
317
- }
318
-
319
- /**
320
- * idem as setData but onot on dom, you can store anything
321
- */
322
-
323
- setInternalData( name: string|Symbol, value: any ): this {
324
- if( !this.store ) {
325
- this.store = new Map( );
326
- }
327
-
328
- this.store.set( name, value );
329
- return this;
330
- }
331
-
332
- getInternalData( name: string|Symbol ): any {
333
- return this.store?.get(name);
334
- }
335
-
336
-
337
- // :: DOM EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
338
-
339
- /**
340
- *
341
- */
342
-
343
- addDOMEvent<K extends keyof GlobalDOMEvents>( name: K, listener: GlobalDOMEvents[K], prepend = false ) {
344
- addEvent( this.dom, name, listener as DOMEventHandler, prepend );
345
- }
346
-
347
- /**
348
- *
349
- */
350
-
351
- setDOMEvents( events: GlobalDOMEvents ) {
352
- for( const name in events ) {
353
- this.addDOMEvent( name as any, (events as any)[name] );
354
- }
355
- }
356
-
357
- // :: HILEVEL EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
358
-
359
- /**
360
- * tool to move named events to internal event map
361
- * @internal
362
- */
363
-
364
- protected mapPropEvents<N extends keyof E>(props: P, ...elements: N[] ) {
365
- const p = props as any;
366
- elements.forEach( n => {
367
- if (p.hasOwnProperty(n) ) {
368
- this.on( n, p[n] );
369
- }
370
- });
371
- }
372
-
373
- // :: CONTENT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
374
-
375
- /**
376
- * remove all content from component
377
- */
378
-
379
- clearContent( ) {
380
- const d = this.dom;
381
- while( d.firstChild ) {
382
- d.removeChild( d.firstChild );
383
- }
384
- }
385
-
386
- /**
387
- * change the whole content of the component
388
- * clear the content before
389
- * @param content new content
390
- */
391
-
392
- setContent( content: ComponentContent ) {
393
- this.clearContent( );
394
- this.appendContent( content );
395
- }
396
-
397
- /**
398
- * cf. appendContent
399
- * @param content content to append
400
- */
401
-
402
- appendContent( content: ComponentContent ) {
403
- const set = ( d: any, c: Component | string | UnsafeHtml | number | boolean ) => {
404
-
405
- if (c instanceof Component ) {
406
- d.appendChild( c.dom );
407
- }
408
- else if( c instanceof UnsafeHtml) {
409
- d.insertAdjacentHTML( 'beforeend' , c.toString() );
410
- }
411
- else if (typeof c === "string" || typeof c === "number") {
412
- const tnode = document.createTextNode(c.toString());
413
- d.appendChild( tnode );
414
- }
415
- else if( c ) {
416
- console.warn("Unknown type to append: ", c);
417
- }
418
- }
419
-
420
- if( !isArray(content) ) {
421
- set( this.dom, content );
422
- }
423
- else if( content.length<=8 ) {
424
- for( const c of content ) {
425
- set( this.dom, c );
426
- }
427
- }
428
- else {
429
- const fragment = document.createDocumentFragment( );
430
- for (const child of content ) {
431
- set( fragment, child );
432
- }
433
-
434
- this.dom.appendChild( fragment );
435
- }
436
- }
437
-
438
- /**
439
- * cf. appendContent
440
- * @param content content to append
441
- */
442
-
443
- prependContent( content: ComponentContent ) {
444
- const d = this.dom;
445
-
446
- const set = ( c: Component | string | UnsafeHtml | number | boolean ) => {
447
- if (c instanceof Component ) {
448
- d.insertBefore( d.firstChild, c.dom );
449
- }
450
- else if( c instanceof UnsafeHtml) {
451
- d.insertAdjacentHTML( 'beforebegin', c.toString() );
452
- }
453
- else if (typeof c === "string" || typeof c === "number") {
454
- const tnode = document.createTextNode(c.toString());
455
- d.insertBefore( d.firstChild, tnode );
456
- }
457
- else {
458
- console.warn("Unknown type to append: ", c);
459
- }
460
- }
461
-
462
- if( !isArray(content) ) {
463
- set( content );
464
- }
465
- else {
466
- const fragment = document.createDocumentFragment( );
467
- for (const child of content ) {
468
- set( child );
469
- }
470
-
471
- d.insertBefore( d.firstChild, fragment );
472
- }
473
- }
474
-
475
- /**
476
- * remove a single child
477
- * @see clearContent
478
- */
479
-
480
- removeChild( child: Component ) {
481
- this.dom.removeChild( child.dom );
482
- }
483
-
484
-
485
- /**
486
- * query all elements by selector
487
- */
488
-
489
- queryAll( selector: string ): Component[] {
490
- const all = this.dom.querySelectorAll( selector );
491
- const rc = new Array( all.length );
492
- all.forEach( (x,i) => rc[i]=componentFromDOM(x) );
493
- return rc;
494
- }
495
-
496
- /**
497
- *
498
- */
499
-
500
- query<T extends Component = Component>( selector: string ): T {
501
- const r = this.dom.querySelector( selector );
502
- return componentFromDOM<T>(r);
503
- }
504
-
505
- // :: STYLES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
506
-
507
-
508
- /**
509
- *
510
- */
511
-
512
- setAria( name: keyof ariaValues, value: string | number | boolean ): this {
513
- this.setAttribute( name, value );
514
- return this;
515
- }
516
-
517
-
518
- /**
519
- *
520
- */
521
-
522
- setStyle( style: Partial<CSSStyleDeclaration> ): this {
523
- const _style = (this.dom as HTMLElement).style;
524
-
525
- for( const name in style ) {
526
-
527
- let value = style[name];
528
- if( !unitless[name] && (isNumber(value) || RE_NUMBER.test(value)) ) {
529
- value += "px";
530
- }
531
-
532
- _style[name] = value;
533
- }
534
-
535
- return this;
536
- }
537
-
538
- /**
539
- *
540
- */
541
-
542
- setStyleValue<K extends keyof CSSStyleDeclaration>( name: K, value: CSSStyleDeclaration[K] | number ): this {
543
-
544
- const _style = (this.dom as HTMLElement).style;
545
-
546
- if( isNumber(value) ) {
547
- let v = value+"";
548
- if( !unitless[name as string] ) {
549
- v += "px";
550
- }
551
-
552
- (_style as any)[name] = v;
553
- }
554
- else {
555
- _style[name] = value;
556
- }
557
-
558
- return this;
559
- }
560
-
561
- /**
562
- *
563
- * @param name
564
- * @returns
565
- */
566
-
567
- getStyleValue<K extends keyof CSSStyleDeclaration>( name: K ) {
568
- const _style = (this.dom as HTMLElement).style;
569
- return _style[name];
570
- }
571
-
572
- setWidth( w: number | string ) {
573
- this.setStyleValue( "width", isNumber(w) ? w+"px" : w );
574
- }
575
-
576
- setHeight( h: number | string ) {
577
- this.setStyleValue( "height", isNumber(h) ? h+"px" : h );
578
- }
579
-
580
- /**
581
- *
582
- */
583
-
584
- setStyleVariable( name: string, value: string ) {
585
- (this.dom as HTMLElement).style.setProperty( name, value );
586
- }
587
-
588
- /**
589
- *
590
- */
591
-
592
- getStyleVariable( name: string ) {
593
- const style = this.getComputedStyle( );
594
- return style.getPropertyValue( name );
595
- }
596
-
597
- /**
598
- *
599
- * @returns
600
- */
601
-
602
- getComputedStyle( ) {
603
- return getComputedStyle( this.dom );
604
- }
605
-
606
- /**
607
- *
608
- */
609
-
610
- setCapture( pointerId: number ) {
611
- this.dom.setPointerCapture( pointerId );
612
- }
613
-
614
- /**
615
- *
616
- */
617
-
618
- releaseCapture( pointerId: number ) {
619
- this.dom.releasePointerCapture( pointerId );
620
- }
621
-
622
- /**
623
- *
624
- */
625
-
626
- getBoundingRect( ): Rect {
627
- const rc = this.dom.getBoundingClientRect( );
628
- return new Rect( rc.x, rc.y, rc.width, rc.height );
629
- }
630
-
631
- // :: MISC ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
632
-
633
- /**
634
- *
635
- */
636
-
637
- focus( ) {
638
- (this.dom as HTMLElement).focus( );
639
- }
640
-
641
- /**
642
- *
643
- */
644
-
645
- scrollIntoView(arg?: boolean | ScrollIntoViewOptions) {
646
- this.dom.scrollIntoView(arg);
647
- }
648
-
649
- /**
650
- *
651
- */
652
-
653
- isVisible( ) {
654
- return (this.dom as HTMLElement).offsetParent !== null;
655
- }
656
-
657
- /**
658
- *
659
- */
660
-
661
- show( vis = true ) {
662
- this.setClass( 'x4hidden', !vis );
663
- }
664
-
665
- /**
666
- *
667
- */
668
-
669
- hide( ) {
670
- this.show( false );
671
- }
672
-
673
- /**
674
- * enable or disable a component (all sub HTMLElement will be also disabled)
675
- */
676
-
677
- enable( ena = true ) {
678
- this.setAttribute( "disabled", !ena );
679
-
680
- // propagate diable state to all input children
681
- const nodes = this.enumChildNodes( true );
682
- nodes.forEach( x => {
683
- if( x instanceof HTMLInputElement ) {
684
- x.disabled = !ena;
685
- }
686
- });
687
- }
688
-
689
- /**
690
- *
691
- */
692
-
693
- disable( ) {
694
- this.enable( false );
695
- }
696
-
697
- /**
698
- * check if element is marked disabled
699
- */
700
-
701
- isDisabled( ) {
702
- return this.getAttribute('disabled');
703
- }
704
-
705
- /**
706
- *
707
- */
708
-
709
- nextElement<T extends Component = Component>( ): T {
710
- const nxt = this.dom.nextElementSibling;
711
- return componentFromDOM<T>( nxt );
712
- }
713
-
714
- /**
715
- *
716
- * @returns
717
- */
718
-
719
- prevElement<T extends Component = Component>( ): T {
720
- const nxt = this.dom.previousElementSibling;
721
- return componentFromDOM<T>( nxt );
722
- }
723
-
724
- /**
725
- * search for parent that match the given contructor
726
- */
727
-
728
- parentElement<T extends Component>( cls?: Constructor<T> ): T {
729
- let p = this.dom;
730
-
731
- while( p.parentElement ) {
732
- const cp = componentFromDOM( p.parentElement );
733
- if( !cls ) {
734
- return cp as T;
735
- }
736
-
737
- if( cp && cp instanceof cls ) {
738
- return cp;
739
- }
740
-
741
- p = p.parentElement;
742
- }
743
-
744
- return null;
745
- }
746
-
747
- /**
748
- *
749
- * @returns
750
- */
751
-
752
- firstChild<T extends Component = Component>( ) : T {
753
- const nxt = this.dom.firstElementChild;
754
- return componentFromDOM<T>( nxt );
755
- }
756
-
757
- /**
758
- *
759
- * @returns
760
- */
761
-
762
- lastChild<T extends Component = Component>( ) : T {
763
- const nxt = this.dom.lastElementChild;
764
- return componentFromDOM( nxt );
765
- }
766
-
767
- /**
768
- * renvoie la liste des Composants enfants
769
- */
770
-
771
- enumChildComponents( recursive: boolean ) {
772
-
773
- let children: Component[] = [];
774
-
775
- const nodes = this.enumChildNodes( recursive );
776
- nodes.forEach( ( c: Node ) => {
777
- const cc = componentFromDOM( c as HTMLElement );
778
- if( cc ) {
779
- children.push(cc);
780
- }
781
- } );
782
-
783
- return children;
784
- }
785
-
786
- /**
787
- * return children list of node (not all should be components)
788
- */
789
-
790
- enumChildNodes( recursive: boolean ) {
791
- let children: Node[] = Array.from( recursive ? this.dom.querySelectorAll( '*' ) : this.dom.children );
792
- return children;
793
- }
794
-
795
- /**
796
- *
797
- */
798
-
799
- animate( keyframes: Keyframe[], duration: number ) {
800
- this.dom.animate(keyframes,duration);
801
- }
802
-
803
-
804
- // :: TSX/REACT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
805
-
806
- /**
807
- * called by the compiler when a jsx element is seen
808
- */
809
-
810
- static createElement( clsOrTag: string | ComponentConstructor | Symbol | Function, attrs: any, ...children: Component[] ): Component | Component[] {
811
-
812
- let comp: Component;
813
-
814
- // fragment
815
- if( clsOrTag==this.createFragment || clsOrTag===FRAGMENT ) {
816
- return children;
817
- }
818
-
819
- // class constructor, yes : dirty
820
- if( clsOrTag instanceof Function ) {
821
- attrs = attrs ?? {};
822
- if( !attrs.children && children && children.length ) {
823
- attrs.content = children;
824
- }
825
-
826
- comp = new (clsOrTag as any)( attrs ?? {} );
827
- }
828
- // basic tag
829
- else {
830
- comp = new Component( {
831
- tag: clsOrTag,
832
- content: children,
833
- ...attrs,
834
- });
835
- }
836
-
837
- if( children && children.length ) {
838
- //comp.setContent( children );
839
- }
840
-
841
- return comp;
842
- }
843
-
844
- /**
845
- *
846
- */
847
-
848
- static createFragment( ): Component[] {
849
- return this.createElement( FRAGMENT, null ) as Component[];
850
- }
851
-
852
- // :: SPECIALS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
853
-
854
- /**
855
- *
856
- */
857
-
858
- queryInterface<T>( name: string ): T {
859
- return null;
860
- }
861
- }
862
-
863
-
864
- /**
865
- *
866
- */
867
-
868
- type ComponentConstructor = {
869
- new(...params: any[]): Component;
870
- };
871
-
872
- /**
873
- * get a component element from it's DOM counterpart
874
- */
875
-
876
- export function componentFromDOM<T extends Component = Component>( node: Element ) {
877
- return node ? (node as any)[COMPONENT] as T : null;
878
- }
879
-
880
- /**
881
- * create a component from an existing DOM
882
- */
883
-
884
- export function wrapDOM( el: HTMLElement ): Component {
885
- const com = componentFromDOM(el);
886
- if( com ) {
887
- return com;
888
- }
889
-
890
- return new Component( { existingDOM: el } );
891
- }
892
-
893
-
894
- // :: Special components ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
895
-
896
- // just a flexible element that push other
897
- export class Flex extends Component {
898
- constructor( ) {
899
- super({})
900
- }
901
- }
902
-
903
-
904
- // :: HIGH LEVEL BASIC EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
905
-
906
-
907
-
908
- /**
909
- * Click Event
910
- * click event do not have any additional parameters
911
- */
912
-
913
- export interface EvClick extends ComponentEvent {
914
- }
915
-
916
- /**
917
- * Change Event
918
- * value is the the element value
919
- */
920
-
921
- export interface EvChange extends ComponentEvent {
922
- readonly value: any;
923
- }
924
-
925
- /**
926
- * Selection Event
927
- * value is the new selection or null
928
- */
929
-
930
- interface ISelection {
931
- }
932
-
933
- export interface EvSelectionChange extends ComponentEvent {
934
- readonly selection: ISelection;
935
- }
936
-
937
-
938
- /**
939
- * ContextMenu Event
940
- */
941
-
942
- export interface EvContextMenu extends ComponentEvent {
943
- uievent: UIEvent; // UI event that fire this event
944
- }
945
-
946
- /**
947
- * Simple message
948
- */
949
-
950
- export interface EvMessage extends ComponentEvent {
951
- readonly msg: string;
952
- readonly params?: any;
953
- }
954
-
955
- /**
956
- * Drag/Drop event
957
- */
958
-
959
- export interface EvDrag extends ComponentEvent {
960
- element: unknown;
961
- data: any;
962
- }
963
-
964
- /**
965
- * Errors
966
- */
967
-
968
- export interface EvError extends ComponentEvent {
969
- code: number;
970
- message: string;
971
- }
972
-
973
- /**
974
- * DblClick Event
975
- */
976
-
977
- export interface EvDblClick extends ComponentEvent {
978
- }
979
-
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file component.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * @copyright (c) 2024 R-libre ingenierie
12
+ *
13
+ * Use of this source code is governed by an MIT-style license
14
+ * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
15
+ **/
16
+
17
+ import { isArray, UnsafeHtml, isNumber, Rect, Constructor, class_ns, x4_class_ns_sym } from './core_tools';
18
+ import { CoreElement } from './core_element';
19
+ import { ariaValues, unitless } from './core_styles';
20
+ import { CoreEvent, EventMap } from './core_events';
21
+ import { addEvent, DOMEventHandler, GlobalDOMEvents } from './core_dom';
22
+
23
+ interface RefType<T extends Component> {
24
+ dom: T;
25
+ }
26
+
27
+ type ComponentAttributes = Record<string,string|number|boolean>;
28
+
29
+ const FRAGMENT = Symbol( "fragment" );
30
+ const COMPONENT = Symbol( "component" );
31
+
32
+ const RE_NUMBER = /^-?\d+(\.\d*)?$/;
33
+
34
+ /**
35
+ * you can change css classname prefix by adding
36
+ *
37
+ * ```
38
+ * static "$cls-ns" = "<your prefix>";
39
+ * ```
40
+ *
41
+ * to your class to avoid autogenerated css class names conflicts
42
+ */
43
+
44
+ function genClassNames( x: any ) {
45
+
46
+ let classes = [];
47
+ let self = Object.getPrototypeOf(x);
48
+
49
+ while (self && self.constructor !== Component ) {
50
+ const clsname:string = self.constructor.name;
51
+ const clsns: string = self.constructor.hasOwnProperty(x4_class_ns_sym) ? self.constructor[x4_class_ns_sym] : "";
52
+ classes.push( clsns+clsname.toLowerCase() );
53
+ self = Object.getPrototypeOf(self);
54
+ }
55
+
56
+ return classes;
57
+ }
58
+
59
+ /**
60
+ *
61
+ */
62
+
63
+ export type ComponentContent = Component | string | UnsafeHtml | number | boolean | Component[];
64
+
65
+ let gen_id = 1000;
66
+
67
+ export const makeUniqueComponentId = ( ) => {
68
+ return `x4-${gen_id++}`;
69
+ }
70
+
71
+ /**
72
+ *
73
+ */
74
+
75
+ export interface ComponentProps {
76
+ tag?: string;
77
+ ns?: string;
78
+
79
+ style?: Partial<CSSStyleDeclaration>;
80
+ attrs?: Record<string,string|number|boolean>;
81
+ content?: ComponentContent;
82
+ dom_events?: GlobalDOMEvents;
83
+ cls?: string;
84
+ id?: string;
85
+ ref?: RefType<any>;
86
+
87
+ // shortcuts
88
+ width?: string | number;
89
+ height?: string | number;
90
+ disabled?: true,
91
+ hidden?: true,
92
+
93
+ tooltip?: string;
94
+
95
+ // wrapper
96
+ existingDOM?: HTMLElement;
97
+
98
+ // index signature
99
+ // to avoid errors: Type 'X' has no properties in common with type 'Y'
100
+ // because all memebers here are optional.
101
+ // this allow TS to recongnize derived props as ComponentProps
102
+ //[key: string]: any;
103
+ };
104
+
105
+
106
+ /**
107
+ *
108
+ */
109
+
110
+ export interface ComponentEvent extends CoreEvent {
111
+ }
112
+
113
+ /**
114
+ *
115
+ */
116
+
117
+ export interface ComponentEvents extends EventMap {
118
+ }
119
+
120
+ /**
121
+ *
122
+ */
123
+
124
+ @class_ns( "x4" )
125
+ export class Component<P extends ComponentProps = ComponentProps, E extends ComponentEvents = ComponentEvents>
126
+ extends CoreElement<E> {
127
+
128
+ readonly dom: Element;
129
+ readonly props: P;
130
+ protected readonly clsprefix: string; // internal class name prefix (x4 internal)
131
+
132
+ private store: Map<string|Symbol,any>;
133
+
134
+ constructor( props: P ) {
135
+ super( );
136
+
137
+ this.props = props; // copy ?
138
+
139
+ if( props.existingDOM ) {
140
+ this.dom = props.existingDOM;
141
+ }
142
+ else {
143
+ if( props.ns ) {
144
+ this.dom = document.createElementNS( props.ns, props.tag ?? "div" );
145
+ }
146
+ else {
147
+ this.dom = document.createElement( props.tag ?? "div" );
148
+ }
149
+
150
+ if (props.attrs) {
151
+ this.setAttributes( props.attrs );
152
+ }
153
+
154
+ if( props.cls ) {
155
+ this.addClass( props.cls );
156
+ }
157
+
158
+ if( props.hidden ) {
159
+ this.show( false );
160
+ }
161
+
162
+ if( props.id!==undefined ) {
163
+ this.setAttribute( "id", props.id );
164
+ }
165
+
166
+ // small shortcut
167
+ if( props.width!==undefined ) {
168
+ this.setStyleValue( "width", props.width );
169
+ }
170
+
171
+ if( props.height!==undefined ) {
172
+ this.setStyleValue( "height", props.height );
173
+ }
174
+
175
+ if( props.tooltip ) {
176
+ this.setAttribute( "tooltip", props.tooltip );
177
+ }
178
+
179
+ if( props.style ) {
180
+ this.setStyle( props.style );
181
+ }
182
+
183
+ if( props.content ) {
184
+ this.setContent( props.content );
185
+ }
186
+
187
+ if( props.dom_events ) {
188
+ this.setDOMEvents( props.dom_events );
189
+ }
190
+
191
+ const classes = genClassNames( this );
192
+ this.dom.classList.add( ...classes );
193
+
194
+ // need to have children for next statements
195
+ // and children way be created in caller
196
+ if( props.disabled ) {
197
+ this.addDOMEvent( "created", ( ) => {
198
+ this.enable( false );
199
+ } );
200
+ }
201
+ }
202
+
203
+ (this.dom as any)[COMPONENT] = this;
204
+ }
205
+
206
+
207
+ // :: CLASSES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
208
+
209
+ /**
210
+ *
211
+ */
212
+
213
+ hasClass( cls: string ) {
214
+ return this.dom.classList.contains( cls );
215
+ }
216
+
217
+ /**
218
+ *
219
+ */
220
+
221
+ addClass( cls: string ) {
222
+ if( !cls ) return;
223
+
224
+ if( cls.indexOf(' ')>=0 ) {
225
+ const ccs = cls.split( " " );
226
+ this.dom.classList.add(...ccs);
227
+ }
228
+ else {
229
+ this.dom.classList.add(cls);
230
+ }
231
+ }
232
+
233
+ /**
234
+ *
235
+ */
236
+
237
+ removeClass( cls: string ) {
238
+ if( !cls ) return;
239
+
240
+ if( cls.indexOf(' ')>=0 ) {
241
+ const ccs = cls.split( " " );
242
+ this.dom.classList.remove(...ccs);
243
+ }
244
+ else {
245
+ this.dom.classList.remove(cls);
246
+ }
247
+ }
248
+
249
+ /**
250
+ *
251
+ */
252
+
253
+ removeClassEx( re: RegExp ) {
254
+ const all = Array.from( this.dom.classList );
255
+ all.forEach( x => {
256
+ if( x.match(re) ) {
257
+ this.dom.classList.remove( x );
258
+ }
259
+ });
260
+ }
261
+
262
+ /**
263
+ *
264
+ */
265
+
266
+ toggleClass( cls: string ) {
267
+ if( !cls ) return;
268
+
269
+ const toggle = ( x: string ) => {
270
+ this.dom.classList.toggle(x);
271
+ }
272
+
273
+ if( cls.indexOf(' ')>=0 ) {
274
+ const ccs = cls.split( " " );
275
+ ccs.forEach( toggle );
276
+ }
277
+ else {
278
+ toggle( cls );
279
+ }
280
+ }
281
+
282
+ /**
283
+ *
284
+ */
285
+
286
+ setClass( cls: string, set: boolean = true ) {
287
+ if( set ) this.addClass(cls);
288
+ else this.removeClass( cls );
289
+ }
290
+
291
+ // :: ATTRIBUTES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
292
+
293
+ /**
294
+ * attributes
295
+ */
296
+
297
+ setAttributes( attrs: ComponentAttributes ) {
298
+
299
+ for( const name in attrs ) {
300
+ const value = attrs[name];
301
+ this.setAttribute( name, value );
302
+ }
303
+ }
304
+
305
+ /**
306
+ *
307
+ */
308
+
309
+ setAttribute( name: string, value: string | number | boolean ) {
310
+ if( value===null || value===undefined ) {
311
+ this.dom.removeAttribute( name );
312
+ }
313
+ else {
314
+ this.dom.setAttribute( name, ""+value );
315
+ }
316
+ }
317
+
318
+ /**
319
+ *
320
+ */
321
+
322
+ getAttribute( name: string ): string {
323
+ return this.dom.getAttribute( name );
324
+ }
325
+
326
+ /**
327
+ *
328
+ */
329
+
330
+ getData( name: string ) : string {
331
+ return this.getAttribute( "data-"+name );
332
+ }
333
+
334
+ /**
335
+ *
336
+ */
337
+
338
+ setData( name: string, value: string ) {
339
+ return this.setAttribute( "data-"+name, value );
340
+ }
341
+
342
+ /**
343
+ * idem as setData but onot on dom, you can store anything
344
+ */
345
+
346
+ setInternalData( name: string|Symbol, value: any ): this {
347
+ if( !this.store ) {
348
+ this.store = new Map( );
349
+ }
350
+
351
+ this.store.set( name, value );
352
+ return this;
353
+ }
354
+
355
+ getInternalData( name: string|Symbol ): any {
356
+ return this.store?.get(name);
357
+ }
358
+
359
+
360
+ // :: DOM EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
361
+
362
+ /**
363
+ *
364
+ */
365
+
366
+ addDOMEvent<K extends keyof GlobalDOMEvents>( name: K, listener: GlobalDOMEvents[K], prepend = false ) {
367
+ addEvent( this.dom, name, listener as DOMEventHandler, prepend );
368
+ }
369
+
370
+ /**
371
+ *
372
+ */
373
+
374
+ setDOMEvents( events: GlobalDOMEvents ) {
375
+ for( const name in events ) {
376
+ this.addDOMEvent( name as any, (events as any)[name] );
377
+ }
378
+ }
379
+
380
+ // :: HILEVEL EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
381
+
382
+ /**
383
+ * tool to move named events to internal event map
384
+ * @internal
385
+ */
386
+
387
+ protected mapPropEvents<N extends keyof E>(props: P, ...elements: N[] ) {
388
+ const p = props as any;
389
+ elements.forEach( n => {
390
+ if (p.hasOwnProperty(n) ) {
391
+ this.on( n, p[n] );
392
+ }
393
+ });
394
+ }
395
+
396
+ // :: CONTENT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
397
+
398
+ /**
399
+ * remove all content from component
400
+ */
401
+
402
+ clearContent( ) {
403
+ const d = this.dom;
404
+ while( d.firstChild ) {
405
+ d.removeChild( d.firstChild );
406
+ }
407
+ }
408
+
409
+ /**
410
+ * change the whole content of the component
411
+ * clear the content before
412
+ * @param content new content
413
+ */
414
+
415
+ setContent( content: ComponentContent ) {
416
+ this.clearContent( );
417
+ this.appendContent( content );
418
+ }
419
+
420
+ /**
421
+ * cf. appendContent
422
+ * @param content content to append
423
+ */
424
+
425
+ appendContent( content: ComponentContent ) {
426
+ const set = ( d: any, c: Component | string | UnsafeHtml | number | boolean ) => {
427
+
428
+ if (c instanceof Component ) {
429
+ d.appendChild( c.dom );
430
+ }
431
+ else if( c instanceof UnsafeHtml) {
432
+ d.insertAdjacentHTML( 'beforeend' , c.toString() );
433
+ }
434
+ else if (typeof c === "string" || typeof c === "number") {
435
+ const tnode = document.createTextNode(c.toString());
436
+ d.appendChild( tnode );
437
+ }
438
+ else if( c ) {
439
+ console.warn("Unknown type to append: ", c);
440
+ }
441
+ }
442
+
443
+ if( !isArray(content) ) {
444
+ set( this.dom, content );
445
+ }
446
+ else if( content.length<=8 ) {
447
+ for( const c of content ) {
448
+ set( this.dom, c );
449
+ }
450
+ }
451
+ else {
452
+ const fragment = document.createDocumentFragment( );
453
+ for (const child of content ) {
454
+ set( fragment, child );
455
+ }
456
+
457
+ this.dom.appendChild( fragment );
458
+ }
459
+ }
460
+
461
+ /**
462
+ * cf. appendContent
463
+ * @param content content to append
464
+ */
465
+
466
+ prependContent( content: ComponentContent ) {
467
+ const d = this.dom;
468
+
469
+ const set = ( c: Component | string | UnsafeHtml | number | boolean ) => {
470
+ if (c instanceof Component ) {
471
+ d.insertBefore( d.firstChild, c.dom );
472
+ }
473
+ else if( c instanceof UnsafeHtml) {
474
+ d.insertAdjacentHTML( 'beforebegin', c.toString() );
475
+ }
476
+ else if (typeof c === "string" || typeof c === "number") {
477
+ const tnode = document.createTextNode(c.toString());
478
+ d.insertBefore( d.firstChild, tnode );
479
+ }
480
+ else {
481
+ console.warn("Unknown type to append: ", c);
482
+ }
483
+ }
484
+
485
+ if( !isArray(content) ) {
486
+ set( content );
487
+ }
488
+ else {
489
+ const fragment = document.createDocumentFragment( );
490
+ for (const child of content ) {
491
+ set( child );
492
+ }
493
+
494
+ d.insertBefore( d.firstChild, fragment );
495
+ }
496
+ }
497
+
498
+ /**
499
+ * remove a single child
500
+ * @see clearContent
501
+ */
502
+
503
+ removeChild( child: Component ) {
504
+ this.dom.removeChild( child.dom );
505
+ }
506
+
507
+
508
+ /**
509
+ * query all elements by selector
510
+ */
511
+
512
+ queryAll( selector: string ): Component[] {
513
+ const all = this.dom.querySelectorAll( selector );
514
+ const rc = new Array( all.length );
515
+ all.forEach( (x,i) => rc[i]=componentFromDOM(x) );
516
+ return rc;
517
+ }
518
+
519
+ /**
520
+ *
521
+ */
522
+
523
+ query<T extends Component = Component>( selector: string ): T {
524
+ const r = this.dom.querySelector( selector );
525
+ return componentFromDOM<T>(r);
526
+ }
527
+
528
+ // :: STYLES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
529
+
530
+
531
+ /**
532
+ *
533
+ */
534
+
535
+ setAria( name: keyof ariaValues, value: string | number | boolean ): this {
536
+ this.setAttribute( name, value );
537
+ return this;
538
+ }
539
+
540
+
541
+ /**
542
+ *
543
+ */
544
+
545
+ setStyle( style: Partial<CSSStyleDeclaration> ): this {
546
+ const _style = (this.dom as HTMLElement).style;
547
+
548
+ for( const name in style ) {
549
+
550
+ let value = style[name];
551
+ if( !unitless[name] && (isNumber(value) || RE_NUMBER.test(value)) ) {
552
+ value += "px";
553
+ }
554
+
555
+ _style[name] = value;
556
+ }
557
+
558
+ return this;
559
+ }
560
+
561
+ /**
562
+ *
563
+ */
564
+
565
+ setStyleValue<K extends keyof CSSStyleDeclaration>( name: K, value: CSSStyleDeclaration[K] | number ): this {
566
+
567
+ const _style = (this.dom as HTMLElement).style;
568
+
569
+ if( isNumber(value) ) {
570
+ let v = value+"";
571
+ if( !unitless[name as string] ) {
572
+ v += "px";
573
+ }
574
+
575
+ (_style as any)[name] = v;
576
+ }
577
+ else {
578
+ _style[name] = value;
579
+ }
580
+
581
+ return this;
582
+ }
583
+
584
+ /**
585
+ *
586
+ * @param name
587
+ * @returns
588
+ */
589
+
590
+ getStyleValue<K extends keyof CSSStyleDeclaration>( name: K ) {
591
+ const _style = (this.dom as HTMLElement).style;
592
+ return _style[name];
593
+ }
594
+
595
+ setWidth( w: number | string ) {
596
+ this.setStyleValue( "width", isNumber(w) ? w+"px" : w );
597
+ }
598
+
599
+ setHeight( h: number | string ) {
600
+ this.setStyleValue( "height", isNumber(h) ? h+"px" : h );
601
+ }
602
+
603
+ /**
604
+ *
605
+ */
606
+
607
+ setStyleVariable( name: string, value: string ) {
608
+ (this.dom as HTMLElement).style.setProperty( name, value );
609
+ }
610
+
611
+ /**
612
+ *
613
+ */
614
+
615
+ getStyleVariable( name: string ) {
616
+ const style = this.getComputedStyle( );
617
+ return style.getPropertyValue( name );
618
+ }
619
+
620
+ /**
621
+ *
622
+ * @returns
623
+ */
624
+
625
+ getComputedStyle( ) {
626
+ return getComputedStyle( this.dom );
627
+ }
628
+
629
+ /**
630
+ *
631
+ */
632
+
633
+ setCapture( pointerId: number ) {
634
+ this.dom.setPointerCapture( pointerId );
635
+ }
636
+
637
+ /**
638
+ *
639
+ */
640
+
641
+ releaseCapture( pointerId: number ) {
642
+ this.dom.releasePointerCapture( pointerId );
643
+ }
644
+
645
+ /**
646
+ *
647
+ */
648
+
649
+ getBoundingRect( ): Rect {
650
+ const rc = this.dom.getBoundingClientRect( );
651
+ return new Rect( rc.x, rc.y, rc.width, rc.height );
652
+ }
653
+
654
+ // :: MISC ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
655
+
656
+ /**
657
+ *
658
+ */
659
+
660
+ focus( ) {
661
+ (this.dom as HTMLElement).focus( );
662
+ }
663
+
664
+ /**
665
+ *
666
+ */
667
+
668
+ scrollIntoView(arg?: boolean | ScrollIntoViewOptions) {
669
+ this.dom.scrollIntoView(arg);
670
+ }
671
+
672
+ /**
673
+ *
674
+ */
675
+
676
+ isVisible( ) {
677
+ return (this.dom as HTMLElement).offsetParent !== null;
678
+ }
679
+
680
+ /**
681
+ *
682
+ */
683
+
684
+ show( vis = true ) {
685
+ this.setClass( 'x4hidden', !vis );
686
+ }
687
+
688
+ /**
689
+ *
690
+ */
691
+
692
+ hide( ) {
693
+ this.show( false );
694
+ }
695
+
696
+ /**
697
+ * enable or disable a component (all sub HTMLElement will be also disabled)
698
+ */
699
+
700
+ enable( ena = true ) {
701
+ this.setAttribute( "disabled", !ena );
702
+
703
+ // propagate diable state to all input children
704
+ const nodes = this.enumChildNodes( true );
705
+ nodes.forEach( x => {
706
+ if( x instanceof HTMLInputElement ) {
707
+ x.disabled = !ena;
708
+ }
709
+ });
710
+ }
711
+
712
+ /**
713
+ *
714
+ */
715
+
716
+ disable( ) {
717
+ this.enable( false );
718
+ }
719
+
720
+ /**
721
+ * check if element is marked disabled
722
+ */
723
+
724
+ isDisabled( ) {
725
+ return this.getAttribute('disabled');
726
+ }
727
+
728
+ /**
729
+ *
730
+ */
731
+
732
+ nextElement<T extends Component = Component>( ): T {
733
+ const nxt = this.dom.nextElementSibling;
734
+ return componentFromDOM<T>( nxt );
735
+ }
736
+
737
+ /**
738
+ *
739
+ * @returns
740
+ */
741
+
742
+ prevElement<T extends Component = Component>( ): T {
743
+ const nxt = this.dom.previousElementSibling;
744
+ return componentFromDOM<T>( nxt );
745
+ }
746
+
747
+ /**
748
+ * search for parent that match the given contructor
749
+ */
750
+
751
+ parentElement<T extends Component>( cls?: Constructor<T> ): T {
752
+ let p = this.dom;
753
+
754
+ while( p.parentElement ) {
755
+ const cp = componentFromDOM( p.parentElement );
756
+ if( !cls ) {
757
+ return cp as T;
758
+ }
759
+
760
+ if( cp && cp instanceof cls ) {
761
+ return cp;
762
+ }
763
+
764
+ p = p.parentElement;
765
+ }
766
+
767
+ return null;
768
+ }
769
+
770
+ /**
771
+ *
772
+ * @returns
773
+ */
774
+
775
+ firstChild<T extends Component = Component>( ) : T {
776
+ const nxt = this.dom.firstElementChild;
777
+ return componentFromDOM<T>( nxt );
778
+ }
779
+
780
+ /**
781
+ *
782
+ * @returns
783
+ */
784
+
785
+ lastChild<T extends Component = Component>( ) : T {
786
+ const nxt = this.dom.lastElementChild;
787
+ return componentFromDOM( nxt );
788
+ }
789
+
790
+ /**
791
+ * renvoie la liste des Composants enfants
792
+ */
793
+
794
+ enumChildComponents( recursive: boolean ) {
795
+
796
+ let children: Component[] = [];
797
+
798
+ const nodes = this.enumChildNodes( recursive );
799
+ nodes.forEach( ( c: Node ) => {
800
+ const cc = componentFromDOM( c as HTMLElement );
801
+ if( cc ) {
802
+ children.push(cc);
803
+ }
804
+ } );
805
+
806
+ return children;
807
+ }
808
+
809
+ /**
810
+ * return children list of node (not all should be components)
811
+ */
812
+
813
+ enumChildNodes( recursive: boolean ) {
814
+ let children: Node[] = Array.from( recursive ? this.dom.querySelectorAll( '*' ) : this.dom.children );
815
+ return children;
816
+ }
817
+
818
+ /**
819
+ *
820
+ */
821
+
822
+ animate( keyframes: Keyframe[], duration: number ) {
823
+ this.dom.animate(keyframes,duration);
824
+ }
825
+
826
+
827
+ // :: TSX/REACT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
828
+
829
+ /**
830
+ * called by the compiler when a jsx element is seen
831
+ */
832
+
833
+ static createElement( clsOrTag: string | ComponentConstructor | Symbol | Function, attrs: any, ...children: Component[] ): Component | Component[] {
834
+
835
+ let comp: Component;
836
+
837
+ // fragment
838
+ if( clsOrTag==this.createFragment || clsOrTag===FRAGMENT ) {
839
+ return children;
840
+ }
841
+
842
+ // class constructor, yes : dirty
843
+ if( clsOrTag instanceof Function ) {
844
+ attrs = attrs ?? {};
845
+ if( !attrs.children && children && children.length ) {
846
+ attrs.content = children;
847
+ }
848
+
849
+ comp = new (clsOrTag as any)( attrs ?? {} );
850
+ }
851
+ // basic tag
852
+ else {
853
+ comp = new Component( {
854
+ tag: clsOrTag,
855
+ content: children,
856
+ ...attrs,
857
+ });
858
+ }
859
+
860
+ if( children && children.length ) {
861
+ //comp.setContent( children );
862
+ }
863
+
864
+ return comp;
865
+ }
866
+
867
+ /**
868
+ *
869
+ */
870
+
871
+ static createFragment( ): Component[] {
872
+ return this.createElement( FRAGMENT, null ) as Component[];
873
+ }
874
+
875
+ // :: SPECIALS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
876
+
877
+ /**
878
+ *
879
+ */
880
+
881
+ queryInterface<T>( name: string ): T {
882
+ return null;
883
+ }
884
+ }
885
+
886
+
887
+ /**
888
+ *
889
+ */
890
+
891
+ type ComponentConstructor = {
892
+ new(...params: any[]): Component;
893
+ };
894
+
895
+ /**
896
+ * get a component element from it's DOM counterpart
897
+ */
898
+
899
+ export function componentFromDOM<T extends Component = Component>( node: Element ) {
900
+ return node ? (node as any)[COMPONENT] as T : null;
901
+ }
902
+
903
+ /**
904
+ * create a component from an existing DOM
905
+ */
906
+
907
+ export function wrapDOM( el: HTMLElement ): Component {
908
+ const com = componentFromDOM(el);
909
+ if( com ) {
910
+ return com;
911
+ }
912
+
913
+ return new Component( { existingDOM: el } );
914
+ }
915
+
916
+
917
+ // :: Special components ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
918
+
919
+ // just a flexible element that push other
920
+ export class Flex extends Component {
921
+ constructor( ) {
922
+ super({})
923
+ }
924
+ }
925
+
926
+
927
+ // :: HIGH LEVEL BASIC EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
928
+
929
+
930
+
931
+ /**
932
+ * Click Event
933
+ * click event do not have any additional parameters
934
+ */
935
+
936
+ export interface EvClick extends ComponentEvent {
937
+ }
938
+
939
+ /**
940
+ * Change Event
941
+ * value is the the element value
942
+ */
943
+
944
+ export interface EvChange extends ComponentEvent {
945
+ readonly value: any;
946
+ }
947
+
948
+ /**
949
+ * Selection Event
950
+ * value is the new selection or null
951
+ */
952
+
953
+ interface ISelection {
954
+ }
955
+
956
+ export interface EvSelectionChange extends ComponentEvent {
957
+ readonly selection: ISelection;
958
+ }
959
+
960
+
961
+ /**
962
+ * ContextMenu Event
963
+ */
964
+
965
+ export interface EvContextMenu extends ComponentEvent {
966
+ uievent: UIEvent; // UI event that fire this event
967
+ }
968
+
969
+ /**
970
+ * Simple message
971
+ */
972
+
973
+ export interface EvMessage extends ComponentEvent {
974
+ readonly msg: string;
975
+ readonly params?: any;
976
+ }
977
+
978
+ /**
979
+ * Drag/Drop event
980
+ */
981
+
982
+ export interface EvDrag extends ComponentEvent {
983
+ element: unknown;
984
+ data: any;
985
+ }
986
+
987
+ /**
988
+ * Errors
989
+ */
990
+
991
+ export interface EvError extends ComponentEvent {
992
+ code: number;
993
+ message: string;
994
+ }
995
+
996
+ /**
997
+ * DblClick Event
998
+ */
999
+
1000
+ export interface EvDblClick extends ComponentEvent {
1001
+ }
1002
+