x4js 2.0.4 → 2.0.6

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 (222) hide show
  1. package/build/x4.css +1 -0
  2. package/build/x4.js +1 -0
  3. package/lib/README.txt +15 -0
  4. package/lib/cjs/x4.css +1 -0
  5. package/lib/cjs/x4.js +1 -0
  6. package/lib/esm/x4.css +1 -0
  7. package/lib/esm/x4.mjs +1 -0
  8. package/lib/src/components/base.scss +26 -0
  9. package/lib/src/components/boxes/boxes.module.scss +37 -0
  10. package/lib/src/components/boxes/boxes.ts +125 -0
  11. package/lib/src/components/btngroup/btngroup.module.scss +29 -0
  12. package/lib/src/components/btngroup/btngroup.ts +106 -0
  13. package/lib/src/components/button/button.module.scss +154 -0
  14. package/lib/src/components/button/button.ts +117 -0
  15. package/lib/src/components/calendar/calendar-check-sharp-light.svg +1 -0
  16. package/lib/src/components/calendar/calendar.module.scss +163 -0
  17. package/lib/src/components/calendar/calendar.ts +326 -0
  18. package/lib/src/components/calendar/chevron-left-sharp-light.svg +1 -0
  19. package/lib/src/components/calendar/chevron-right-sharp-light.svg +1 -0
  20. package/lib/src/components/checkbox/check.svg +4 -0
  21. package/lib/src/components/checkbox/checkbox.module.scss +142 -0
  22. package/lib/src/components/checkbox/checkbox.ts +125 -0
  23. package/lib/src/components/colorinput/colorinput.module.scss +65 -0
  24. package/lib/src/components/colorinput/colorinput.ts +88 -0
  25. package/lib/src/components/colorinput/crosshairs-simple-sharp-light.svg +1 -0
  26. package/lib/src/components/colorpicker/colorpicker.module.scss +133 -0
  27. package/lib/src/components/colorpicker/colorpicker.ts +477 -0
  28. package/lib/src/components/combobox/combobox.module.scss +121 -0
  29. package/lib/src/components/combobox/combobox.ts +190 -0
  30. package/lib/src/components/combobox/updown.svg +4 -0
  31. package/lib/src/components/dialog/dialog.module.scss +71 -0
  32. package/lib/src/components/dialog/dialog.ts +91 -0
  33. package/lib/src/components/dialog/xmark-sharp-light.svg +1 -0
  34. package/lib/src/components/form/form.module.scss +34 -0
  35. package/lib/src/components/form/form.ts +36 -0
  36. package/lib/src/components/header/header.module.scss +40 -0
  37. package/lib/src/components/header/header.ts +124 -0
  38. package/lib/src/components/icon/icon.module.scss +30 -0
  39. package/lib/src/components/icon/icon.ts +134 -0
  40. package/lib/src/components/image/image.module.scss +21 -0
  41. package/lib/src/components/image/image.ts +67 -0
  42. package/lib/src/components/input/input.module.scss +69 -0
  43. package/lib/src/components/input/input.ts +274 -0
  44. package/lib/src/components/label/label.module.scss +52 -0
  45. package/lib/src/components/label/label.ts +55 -0
  46. package/lib/src/components/listbox/listbox.module.scss +103 -0
  47. package/lib/src/components/listbox/listbox.ts +427 -0
  48. package/lib/src/components/menu/caret-right-solid.svg +1 -0
  49. package/lib/src/components/menu/menu.module.scss +108 -0
  50. package/lib/src/components/menu/menu.ts +168 -0
  51. package/lib/src/components/messages/circle-exclamation.svg +1 -0
  52. package/lib/src/components/messages/messages.module.scss +47 -0
  53. package/lib/src/components/messages/messages.ts +64 -0
  54. package/lib/src/components/normalize.scss +386 -0
  55. package/lib/src/components/notification/circle-check-solid.svg +1 -0
  56. package/lib/src/components/notification/circle-exclamation-solid.svg +1 -0
  57. package/lib/src/components/notification/circle-notch-light.svg +1 -0
  58. package/lib/src/components/notification/notification.module.scss +82 -0
  59. package/lib/src/components/notification/notification.ts +108 -0
  60. package/lib/src/components/notification/xmark-sharp-light.svg +1 -0
  61. package/lib/src/components/panel/panel.module.scss +48 -0
  62. package/lib/src/components/panel/panel.ts +57 -0
  63. package/lib/src/components/popup/popup.module.scss +43 -0
  64. package/lib/src/components/popup/popup.ts +395 -0
  65. package/lib/src/components/progress/progress.module.scss +57 -0
  66. package/lib/src/components/progress/progress.ts +43 -0
  67. package/lib/src/components/rating/rating.module.scss +23 -0
  68. package/lib/src/components/rating/rating.ts +125 -0
  69. package/lib/src/components/rating/star-sharp-light.svg +1 -0
  70. package/lib/src/components/rating/star-sharp-solid.svg +1 -0
  71. package/lib/src/components/shared.scss +76 -0
  72. package/lib/src/components/sizers/sizer.module.scss +90 -0
  73. package/lib/src/components/sizers/sizer.ts +120 -0
  74. package/lib/src/components/slider/slider.module.scss +71 -0
  75. package/lib/src/components/slider/slider.ts +143 -0
  76. package/lib/src/components/switch/switch.module.scss +127 -0
  77. package/lib/src/components/switch/switch.ts +56 -0
  78. package/lib/src/components/tabs/tabs.module.scss +46 -0
  79. package/lib/src/components/tabs/tabs.ts +157 -0
  80. package/lib/src/components/textarea/textarea.module.scss +59 -0
  81. package/lib/src/components/textarea/textarea.ts +54 -0
  82. package/lib/src/components/textedit/textedit.module.scss +114 -0
  83. package/lib/src/components/textedit/textedit.ts +82 -0
  84. package/lib/src/components/themes.scss +77 -0
  85. package/lib/src/components/tooltips/circle-info-sharp-light.svg +1 -0
  86. package/lib/src/components/tooltips/tooltips.scss +51 -0
  87. package/lib/src/components/tooltips/tooltips.ts +103 -0
  88. package/lib/src/components/treeview/chevron-down-light.svg +1 -0
  89. package/lib/src/components/treeview/treeview.module.scss +116 -0
  90. package/lib/src/components/treeview/treeview.ts +403 -0
  91. package/lib/src/components/viewport/viewport.module.scss +25 -0
  92. package/lib/src/components/viewport/viewport.ts +38 -0
  93. package/lib/src/core/component.ts +979 -0
  94. package/lib/src/core/core_colors.ts +250 -0
  95. package/lib/src/core/core_dom.ts +471 -0
  96. package/lib/src/core/core_dragdrop.ts +201 -0
  97. package/lib/src/core/core_element.ts +98 -0
  98. package/lib/src/core/core_events.ts +149 -0
  99. package/lib/src/core/core_i18n.ts +377 -0
  100. package/lib/src/core/core_router.ts +221 -0
  101. package/lib/src/core/core_styles.ts +215 -0
  102. package/lib/src/core/core_svg.ts +550 -0
  103. package/lib/src/core/core_tools.ts +673 -0
  104. package/lib/src/demo/assets/house-light.svg +1 -0
  105. package/lib/src/demo/assets/radio.svg +4 -0
  106. package/lib/src/demo/index.html +12 -0
  107. package/lib/src/demo/main.scss +21 -0
  108. package/lib/src/demo/main.tsx +323 -0
  109. package/lib/src/x4.scss +19 -0
  110. package/lib/src/x4.ts +60 -0
  111. package/lib/styles/x4.css +1 -0
  112. package/lib/types/x4.d.ts +45060 -0
  113. package/package.json +3 -12
  114. package/scripts/build.mjs +362 -0
  115. package/scripts/prepack.mjs +51 -0
  116. package/src/components/base.scss +26 -0
  117. package/src/components/boxes/boxes.module.scss +37 -0
  118. package/src/components/boxes/boxes.ts +125 -0
  119. package/src/components/btngroup/btngroup.module.scss +29 -0
  120. package/src/components/btngroup/btngroup.ts +106 -0
  121. package/src/components/button/button.module.scss +154 -0
  122. package/src/components/button/button.ts +117 -0
  123. package/src/components/calendar/calendar-check-sharp-light.svg +1 -0
  124. package/src/components/calendar/calendar.module.scss +163 -0
  125. package/src/components/calendar/calendar.ts +326 -0
  126. package/src/components/calendar/chevron-left-sharp-light.svg +1 -0
  127. package/src/components/calendar/chevron-right-sharp-light.svg +1 -0
  128. package/src/components/checkbox/check.svg +4 -0
  129. package/src/components/checkbox/checkbox.module.scss +142 -0
  130. package/src/components/checkbox/checkbox.ts +125 -0
  131. package/src/components/colorinput/colorinput.module.scss +65 -0
  132. package/src/components/colorinput/colorinput.ts +88 -0
  133. package/src/components/colorinput/crosshairs-simple-sharp-light.svg +1 -0
  134. package/src/components/colorpicker/colorpicker.module.scss +133 -0
  135. package/src/components/colorpicker/colorpicker.ts +477 -0
  136. package/src/components/combobox/combobox.module.scss +121 -0
  137. package/src/components/combobox/combobox.ts +190 -0
  138. package/src/components/combobox/updown.svg +4 -0
  139. package/src/components/dialog/dialog.module.scss +71 -0
  140. package/src/components/dialog/dialog.ts +91 -0
  141. package/src/components/dialog/xmark-sharp-light.svg +1 -0
  142. package/src/components/form/form.module.scss +34 -0
  143. package/src/components/form/form.ts +36 -0
  144. package/src/components/header/header.module.scss +40 -0
  145. package/src/components/header/header.ts +124 -0
  146. package/src/components/icon/icon.module.scss +30 -0
  147. package/src/components/icon/icon.ts +134 -0
  148. package/src/components/image/image.module.scss +21 -0
  149. package/src/components/image/image.ts +67 -0
  150. package/src/components/input/input.module.scss +69 -0
  151. package/src/components/input/input.ts +274 -0
  152. package/src/components/label/label.module.scss +52 -0
  153. package/src/components/label/label.ts +55 -0
  154. package/src/components/listbox/listbox.module.scss +103 -0
  155. package/src/components/listbox/listbox.ts +427 -0
  156. package/src/components/menu/caret-right-solid.svg +1 -0
  157. package/src/components/menu/menu.module.scss +108 -0
  158. package/src/components/menu/menu.ts +168 -0
  159. package/src/components/messages/circle-exclamation.svg +1 -0
  160. package/src/components/messages/messages.module.scss +47 -0
  161. package/src/components/messages/messages.ts +64 -0
  162. package/src/components/normalize.scss +386 -0
  163. package/src/components/notification/circle-check-solid.svg +1 -0
  164. package/src/components/notification/circle-exclamation-solid.svg +1 -0
  165. package/src/components/notification/circle-notch-light.svg +1 -0
  166. package/src/components/notification/notification.module.scss +82 -0
  167. package/src/components/notification/notification.ts +108 -0
  168. package/src/components/notification/xmark-sharp-light.svg +1 -0
  169. package/src/components/panel/panel.module.scss +48 -0
  170. package/src/components/panel/panel.ts +57 -0
  171. package/src/components/popup/popup.module.scss +43 -0
  172. package/src/components/popup/popup.ts +395 -0
  173. package/src/components/progress/progress.module.scss +57 -0
  174. package/src/components/progress/progress.ts +43 -0
  175. package/src/components/rating/rating.module.scss +23 -0
  176. package/src/components/rating/rating.ts +125 -0
  177. package/src/components/rating/star-sharp-light.svg +1 -0
  178. package/src/components/rating/star-sharp-solid.svg +1 -0
  179. package/src/components/shared.scss +76 -0
  180. package/src/components/sizers/sizer.module.scss +90 -0
  181. package/src/components/sizers/sizer.ts +120 -0
  182. package/src/components/slider/slider.module.scss +71 -0
  183. package/src/components/slider/slider.ts +143 -0
  184. package/src/components/switch/switch.module.scss +127 -0
  185. package/src/components/switch/switch.ts +56 -0
  186. package/src/components/tabs/tabs.module.scss +46 -0
  187. package/src/components/tabs/tabs.ts +157 -0
  188. package/src/components/textarea/textarea.module.scss +59 -0
  189. package/src/components/textarea/textarea.ts +54 -0
  190. package/src/components/textedit/textedit.module.scss +114 -0
  191. package/src/components/textedit/textedit.ts +82 -0
  192. package/src/components/themes.scss +77 -0
  193. package/src/components/tooltips/circle-info-sharp-light.svg +1 -0
  194. package/src/components/tooltips/tooltips.scss +51 -0
  195. package/src/components/tooltips/tooltips.ts +103 -0
  196. package/src/components/treeview/chevron-down-light.svg +1 -0
  197. package/src/components/treeview/treeview.module.scss +116 -0
  198. package/src/components/treeview/treeview.ts +403 -0
  199. package/src/components/viewport/viewport.module.scss +25 -0
  200. package/src/components/viewport/viewport.ts +38 -0
  201. package/src/core/component.ts +979 -0
  202. package/src/core/core_colors.ts +250 -0
  203. package/src/core/core_dom.ts +471 -0
  204. package/src/core/core_dragdrop.ts +201 -0
  205. package/src/core/core_element.ts +98 -0
  206. package/src/core/core_events.ts +149 -0
  207. package/src/core/core_i18n.ts +377 -0
  208. package/src/core/core_router.ts +221 -0
  209. package/src/core/core_styles.ts +215 -0
  210. package/src/core/core_svg.ts +550 -0
  211. package/src/core/core_tools.ts +673 -0
  212. package/src/demo/assets/house-light.svg +1 -0
  213. package/src/demo/assets/radio.svg +4 -0
  214. package/src/demo/index.html +12 -0
  215. package/src/demo/main.scss +21 -0
  216. package/src/demo/main.tsx +323 -0
  217. package/src/x4.scss +19 -0
  218. package/src/x4.ts +60 -0
  219. package/tsconfig.json +14 -0
  220. package/types/scss.d.ts +4 -0
  221. package/types/svg.d.ts +4 -0
  222. package/types/x4react.d.ts +9 -0
@@ -0,0 +1,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 } 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 extends IComponmentInterface>( 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
+