x4js 1.5.17 → 1.5.20

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