x4js 2.0.13 → 2.0.15

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