x4js 1.4.51 → 1.4.53

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 (305) hide show
  1. package/lib/{action.js → cjs/action.js} +0 -0
  2. package/lib/{application.js → cjs/application.js} +13 -19
  3. package/lib/{autocomplete.js → cjs/autocomplete.js} +0 -4
  4. package/lib/cjs/base_component.js +124 -0
  5. package/lib/{button.js → cjs/button.js} +4 -4
  6. package/lib/{calendar.js → cjs/calendar.js} +22 -22
  7. package/lib/{canvas.js → cjs/canvas.js} +7 -8
  8. package/lib/{cardview.js → cjs/cardview.js} +3 -5
  9. package/lib/{checkbox.js → cjs/checkbox.js} +6 -5
  10. package/lib/cjs/color.js +591 -0
  11. package/lib/{colorpicker.js → cjs/colorpicker.js} +1 -14
  12. package/lib/{combobox.js → cjs/combobox.js} +2 -6
  13. package/lib/{component.js → cjs/component.js} +20 -41
  14. package/lib/{datastore.js → cjs/datastore.js} +18 -32
  15. package/lib/{dialog.js → cjs/dialog.js} +4 -21
  16. package/lib/{dom_events.js → cjs/dom_events.js} +0 -0
  17. package/lib/{drag_manager.js → cjs/drag_manager.js} +0 -5
  18. package/lib/{drawtext.js → cjs/drawtext.js} +5 -4
  19. package/lib/{fileupload.js → cjs/fileupload.js} +0 -3
  20. package/lib/{form.js → cjs/form.js} +2 -6
  21. package/lib/{formatters.js → cjs/formatters.js} +0 -0
  22. package/lib/{gridview.js → cjs/gridview.js} +20 -33
  23. package/lib/{i18n.js → cjs/i18n.js} +0 -0
  24. package/lib/{icon.js → cjs/icon.js} +17 -11
  25. package/lib/{image.js → cjs/image.js} +4 -5
  26. package/lib/{index.js → cjs/index.js} +0 -0
  27. package/lib/cjs/input.js +174 -0
  28. package/lib/{label.js → cjs/label.js} +2 -1
  29. package/lib/{layout.js → cjs/layout.js} +9 -8
  30. package/lib/{link.js → cjs/link.js} +3 -2
  31. package/lib/cjs/listview.js +544 -0
  32. package/{src/md5.ts → lib/cjs/md5.js} +403 -438
  33. package/lib/{menu.js → cjs/menu.js} +4 -11
  34. package/lib/{messagebox.js → cjs/messagebox.js} +4 -4
  35. package/lib/{panel.js → cjs/panel.js} +3 -4
  36. package/lib/{popup.js → cjs/popup.js} +4 -5
  37. package/lib/{property_editor.js → cjs/property_editor.js} +2 -5
  38. package/lib/{radiobtn.js → cjs/radiobtn.js} +6 -6
  39. package/lib/{rating.js → cjs/rating.js} +7 -6
  40. package/lib/cjs/request.js +246 -0
  41. package/lib/{router.js → cjs/router.js} +0 -1
  42. package/lib/cjs/settings.js +69 -0
  43. package/lib/{sidebarview.js → cjs/sidebarview.js} +2 -3
  44. package/lib/{spreadsheet.js → cjs/spreadsheet.js} +10 -27
  45. package/lib/{styles.js → cjs/styles.js} +5 -6
  46. package/lib/{svgcomponent.js → cjs/svgcomponent.js} +3 -11
  47. package/lib/{tabbar.js → cjs/tabbar.js} +5 -5
  48. package/lib/{tabview.js → cjs/tabview.js} +0 -2
  49. package/lib/{textarea.js → cjs/textarea.js} +4 -3
  50. package/lib/{textedit.js → cjs/textedit.js} +7 -8
  51. package/lib/{toaster.js → cjs/toaster.js} +0 -2
  52. package/lib/{tools.js → cjs/tools.js} +7 -16
  53. package/lib/{tooltips.js → cjs/tooltips.js} +0 -1
  54. package/lib/{treeview.js → cjs/treeview.js} +23 -19
  55. package/lib/{action.d.ts → cjs/types/action.d.ts} +0 -0
  56. package/lib/{application.d.ts → cjs/types/application.d.ts} +0 -0
  57. package/lib/{autocomplete.d.ts → cjs/types/autocomplete.d.ts} +0 -0
  58. package/lib/{base_component.d.ts → cjs/types/base_component.d.ts} +0 -0
  59. package/lib/{button.d.ts → cjs/types/button.d.ts} +0 -0
  60. package/lib/{calendar.d.ts → cjs/types/calendar.d.ts} +0 -0
  61. package/lib/{canvas.d.ts → cjs/types/canvas.d.ts} +0 -0
  62. package/lib/{cardview.d.ts → cjs/types/cardview.d.ts} +0 -0
  63. package/lib/{checkbox.d.ts → cjs/types/checkbox.d.ts} +0 -0
  64. package/lib/{color.d.ts → cjs/types/color.d.ts} +0 -0
  65. package/lib/{colorpicker.d.ts → cjs/types/colorpicker.d.ts} +0 -0
  66. package/lib/{combobox.d.ts → cjs/types/combobox.d.ts} +0 -0
  67. package/lib/{component.d.ts → cjs/types/component.d.ts} +1 -7
  68. package/lib/{datastore.d.ts → cjs/types/datastore.d.ts} +0 -0
  69. package/lib/{dialog.d.ts → cjs/types/dialog.d.ts} +0 -0
  70. package/lib/{dom_events.d.ts → cjs/types/dom_events.d.ts} +0 -0
  71. package/lib/{drag_manager.d.ts → cjs/types/drag_manager.d.ts} +0 -0
  72. package/lib/{drawtext.d.ts → cjs/types/drawtext.d.ts} +0 -0
  73. package/lib/{fileupload.d.ts → cjs/types/fileupload.d.ts} +0 -0
  74. package/lib/{form.d.ts → cjs/types/form.d.ts} +0 -0
  75. package/lib/{formatters.d.ts → cjs/types/formatters.d.ts} +0 -0
  76. package/lib/{gridview.d.ts → cjs/types/gridview.d.ts} +0 -0
  77. package/lib/{i18n.d.ts → cjs/types/i18n.d.ts} +0 -0
  78. package/lib/{icon.d.ts → cjs/types/icon.d.ts} +0 -0
  79. package/lib/{image.d.ts → cjs/types/image.d.ts} +0 -0
  80. package/lib/{index.d.ts → cjs/types/index.d.ts} +0 -0
  81. package/lib/{input.d.ts → cjs/types/input.d.ts} +0 -0
  82. package/lib/{label.d.ts → cjs/types/label.d.ts} +0 -0
  83. package/lib/{layout.d.ts → cjs/types/layout.d.ts} +0 -0
  84. package/lib/{link.d.ts → cjs/types/link.d.ts} +0 -0
  85. package/lib/{listview.d.ts → cjs/types/listview.d.ts} +0 -0
  86. package/lib/{md5.d.ts → cjs/types/md5.d.ts} +0 -0
  87. package/lib/{menu.d.ts → cjs/types/menu.d.ts} +0 -0
  88. package/lib/{messagebox.d.ts → cjs/types/messagebox.d.ts} +0 -0
  89. package/lib/{panel.d.ts → cjs/types/panel.d.ts} +0 -0
  90. package/lib/{popup.d.ts → cjs/types/popup.d.ts} +0 -0
  91. package/lib/{property_editor.d.ts → cjs/types/property_editor.d.ts} +0 -0
  92. package/lib/{radiobtn.d.ts → cjs/types/radiobtn.d.ts} +0 -0
  93. package/lib/{rating.d.ts → cjs/types/rating.d.ts} +0 -0
  94. package/lib/{request.d.ts → cjs/types/request.d.ts} +0 -0
  95. package/lib/{router.d.ts → cjs/types/router.d.ts} +0 -0
  96. package/lib/{settings.d.ts → cjs/types/settings.d.ts} +0 -0
  97. package/lib/{sidebarview.d.ts → cjs/types/sidebarview.d.ts} +0 -0
  98. package/lib/{spreadsheet.d.ts → cjs/types/spreadsheet.d.ts} +0 -0
  99. package/lib/{styles.d.ts → cjs/types/styles.d.ts} +0 -0
  100. package/lib/{svgcomponent.d.ts → cjs/types/svgcomponent.d.ts} +0 -0
  101. package/lib/{tabbar.d.ts → cjs/types/tabbar.d.ts} +0 -0
  102. package/lib/{tabview.d.ts → cjs/types/tabview.d.ts} +0 -0
  103. package/lib/{textarea.d.ts → cjs/types/textarea.d.ts} +0 -0
  104. package/lib/{textedit.d.ts → cjs/types/textedit.d.ts} +0 -0
  105. package/lib/{toaster.d.ts → cjs/types/toaster.d.ts} +0 -0
  106. package/lib/{tools.d.ts → cjs/types/tools.d.ts} +0 -0
  107. package/lib/{tooltips.d.ts → cjs/types/tooltips.d.ts} +0 -0
  108. package/lib/{treeview.d.ts → cjs/types/treeview.d.ts} +0 -0
  109. package/lib/{version.d.ts → cjs/types/version.d.ts} +0 -0
  110. package/lib/{x4dom.d.ts → cjs/types/x4dom.d.ts} +0 -0
  111. package/lib/{x4events.d.ts → cjs/types/x4events.d.ts} +0 -0
  112. package/lib/cjs/types/x4react.d.ts +41 -0
  113. package/lib/{version.js → cjs/version.js} +0 -0
  114. package/lib/{x4dom.js → cjs/x4dom.js} +0 -0
  115. package/lib/{x4events.js → cjs/x4events.js} +6 -11
  116. package/lib/cjs/x4react.js +72 -0
  117. package/lib/esm/action.js +53 -0
  118. package/lib/esm/application.js +184 -0
  119. package/lib/esm/autocomplete.js +155 -0
  120. package/lib/{base_component.js → esm/base_component.js} +3 -7
  121. package/lib/esm/button.js +257 -0
  122. package/lib/esm/calendar.js +244 -0
  123. package/lib/esm/canvas.js +358 -0
  124. package/lib/esm/cardview.js +159 -0
  125. package/lib/esm/checkbox.js +133 -0
  126. package/lib/{color.js → esm/color.js} +3 -7
  127. package/lib/esm/colorpicker.js +1475 -0
  128. package/lib/esm/combobox.js +259 -0
  129. package/lib/esm/component.js +1711 -0
  130. package/lib/esm/datastore.js +990 -0
  131. package/lib/esm/dialog.js +490 -0
  132. package/lib/{base64.d.ts → esm/dom_events.js} +2 -8
  133. package/lib/esm/drag_manager.js +147 -0
  134. package/lib/esm/drawtext.js +271 -0
  135. package/lib/esm/fileupload.js +163 -0
  136. package/lib/esm/form.js +297 -0
  137. package/lib/esm/formatters.js +81 -0
  138. package/lib/esm/gridview.js +889 -0
  139. package/lib/esm/i18n.js +257 -0
  140. package/lib/esm/icon.js +256 -0
  141. package/lib/esm/image.js +154 -0
  142. package/lib/esm/index.mjs +86 -0
  143. package/lib/{input.js → esm/input.js} +2 -6
  144. package/lib/esm/label.js +90 -0
  145. package/lib/esm/layout.js +310 -0
  146. package/lib/esm/link.js +59 -0
  147. package/lib/{listview.js → esm/listview.js} +30 -36
  148. package/lib/{md5.js → esm/md5.js} +1 -5
  149. package/lib/esm/menu.js +300 -0
  150. package/lib/esm/messagebox.js +145 -0
  151. package/lib/esm/panel.js +65 -0
  152. package/lib/esm/popup.js +379 -0
  153. package/lib/esm/property_editor.js +251 -0
  154. package/lib/esm/radiobtn.js +136 -0
  155. package/lib/esm/rating.js +97 -0
  156. package/lib/{request.js → esm/request.js} +9 -16
  157. package/lib/esm/router.js +136 -0
  158. package/lib/{settings.js → esm/settings.js} +1 -5
  159. package/lib/esm/sidebarview.js +79 -0
  160. package/lib/esm/spreadsheet.js +1091 -0
  161. package/lib/esm/styles.js +270 -0
  162. package/lib/esm/svgcomponent.js +422 -0
  163. package/lib/esm/tabbar.js +107 -0
  164. package/lib/esm/tabview.js +83 -0
  165. package/lib/esm/textarea.js +123 -0
  166. package/lib/esm/textedit.js +412 -0
  167. package/lib/esm/toaster.js +62 -0
  168. package/lib/esm/tools.js +1155 -0
  169. package/lib/esm/tooltips.js +153 -0
  170. package/lib/esm/treeview.js +495 -0
  171. package/lib/esm/types/action.d.ts +57 -0
  172. package/lib/esm/types/application.d.ts +104 -0
  173. package/lib/esm/types/autocomplete.d.ts +58 -0
  174. package/lib/esm/types/base_component.d.ts +88 -0
  175. package/lib/esm/types/button.d.ts +151 -0
  176. package/lib/esm/types/calendar.d.ts +81 -0
  177. package/lib/esm/types/canvas.d.ts +92 -0
  178. package/lib/esm/types/cardview.d.ts +87 -0
  179. package/lib/esm/types/checkbox.d.ts +77 -0
  180. package/lib/esm/types/color.d.ts +148 -0
  181. package/lib/esm/types/colorpicker.d.ts +107 -0
  182. package/lib/esm/types/combobox.d.ts +101 -0
  183. package/lib/esm/types/component.d.ts +601 -0
  184. package/lib/esm/types/datastore.d.ts +396 -0
  185. package/lib/esm/types/dialog.d.ts +175 -0
  186. package/lib/esm/types/dom_events.d.ts +302 -0
  187. package/lib/esm/types/drag_manager.d.ts +54 -0
  188. package/lib/esm/types/drawtext.d.ts +48 -0
  189. package/lib/esm/types/fileupload.d.ts +64 -0
  190. package/lib/esm/types/form.d.ts +126 -0
  191. package/lib/esm/types/formatters.d.ts +35 -0
  192. package/lib/esm/types/gridview.d.ts +175 -0
  193. package/lib/esm/types/i18n.d.ts +73 -0
  194. package/lib/esm/types/icon.d.ts +64 -0
  195. package/lib/esm/types/image.d.ts +55 -0
  196. package/lib/esm/types/index.d.ts +83 -0
  197. package/lib/esm/types/input.d.ts +91 -0
  198. package/lib/esm/types/label.d.ts +58 -0
  199. package/lib/esm/types/layout.d.ts +87 -0
  200. package/lib/esm/types/link.d.ts +50 -0
  201. package/lib/esm/types/listview.d.ts +178 -0
  202. package/lib/esm/types/md5.d.ts +61 -0
  203. package/lib/esm/types/menu.d.ts +130 -0
  204. package/lib/esm/types/messagebox.d.ts +68 -0
  205. package/lib/esm/types/panel.d.ts +47 -0
  206. package/lib/esm/types/popup.d.ts +75 -0
  207. package/lib/esm/types/property_editor.d.ts +71 -0
  208. package/lib/esm/types/radiobtn.d.ts +72 -0
  209. package/lib/esm/types/rating.d.ts +53 -0
  210. package/lib/esm/types/request.d.ts +52 -0
  211. package/lib/esm/types/router.d.ts +42 -0
  212. package/lib/esm/types/settings.d.ts +37 -0
  213. package/lib/esm/types/sidebarview.d.ts +49 -0
  214. package/lib/esm/types/spreadsheet.d.ts +218 -0
  215. package/lib/esm/types/styles.d.ts +85 -0
  216. package/lib/esm/types/svgcomponent.d.ts +191 -0
  217. package/lib/esm/types/tabbar.d.ts +58 -0
  218. package/lib/esm/types/tabview.d.ts +49 -0
  219. package/lib/{texthiliter.d.ts → esm/types/textarea.d.ts} +26 -23
  220. package/lib/esm/types/textedit.d.ts +123 -0
  221. package/lib/esm/types/toaster.d.ts +42 -0
  222. package/lib/esm/types/tools.d.ts +394 -0
  223. package/lib/esm/types/tooltips.d.ts +46 -0
  224. package/lib/esm/types/treeview.d.ts +132 -0
  225. package/lib/esm/types/version.d.ts +29 -0
  226. package/lib/esm/types/x4dom.d.ts +49 -0
  227. package/lib/{x4_events.d.ts → esm/types/x4events.d.ts} +6 -3
  228. package/lib/esm/types/x4react.d.ts +41 -0
  229. package/lib/esm/version.js +29 -0
  230. package/lib/esm/x4dom.js +29 -0
  231. package/lib/{x4_events.js → esm/x4events.js} +11 -24
  232. package/lib/esm/x4react.js +71 -0
  233. package/lib/{x4.css → styles/x4.css} +11 -0
  234. package/{src → lib/styles}/x4.less +14 -0
  235. package/package.json +35 -14
  236. package/build.sh +0 -5
  237. package/lib/base64.js +0 -143
  238. package/lib/hosts/host.d.ts +0 -44
  239. package/lib/hosts/host.js +0 -73
  240. package/lib/smartedit.d.ts +0 -107
  241. package/lib/smartedit.js +0 -390
  242. package/lib/texthiliter.js +0 -227
  243. package/lib/x4react.d.ts +0 -6
  244. package/lib/x4react.js +0 -35
  245. package/src/MIT-license.md +0 -14
  246. package/src/action.ts +0 -88
  247. package/src/alpha.jpg +0 -0
  248. package/src/application.ts +0 -251
  249. package/src/autocomplete.ts +0 -197
  250. package/src/base64.ts +0 -166
  251. package/src/base_component.ts +0 -152
  252. package/src/button.ts +0 -355
  253. package/src/calendar.ts +0 -322
  254. package/src/canvas.ts +0 -505
  255. package/src/cardview.ts +0 -227
  256. package/src/checkbox.ts +0 -187
  257. package/src/color.ts +0 -752
  258. package/src/colorpicker.ts +0 -1639
  259. package/src/combobox.ts +0 -362
  260. package/src/component.ts +0 -2344
  261. package/src/datastore.ts +0 -1322
  262. package/src/dialog.ts +0 -656
  263. package/src/dom_events.ts +0 -315
  264. package/src/drag_manager.ts +0 -199
  265. package/src/drawtext.ts +0 -355
  266. package/src/fileupload.ts +0 -213
  267. package/src/form.ts +0 -366
  268. package/src/formatters.ts +0 -105
  269. package/src/gridview.ts +0 -1185
  270. package/src/i18n.ts +0 -346
  271. package/src/icon.ts +0 -329
  272. package/src/image.ts +0 -204
  273. package/src/index.ts +0 -88
  274. package/src/input.ts +0 -249
  275. package/src/label.ts +0 -128
  276. package/src/layout.ts +0 -430
  277. package/src/link.ts +0 -86
  278. package/src/listview.ts +0 -762
  279. package/src/menu.ts +0 -425
  280. package/src/messagebox.ts +0 -203
  281. package/src/panel.ts +0 -86
  282. package/src/popup.ts +0 -494
  283. package/src/property_editor.ts +0 -337
  284. package/src/radiobtn.ts +0 -196
  285. package/src/rating.ts +0 -135
  286. package/src/request.ts +0 -300
  287. package/src/router.ts +0 -185
  288. package/src/settings.ts +0 -77
  289. package/src/sidebarview.ts +0 -103
  290. package/src/spreadsheet.ts +0 -1442
  291. package/src/styles.ts +0 -343
  292. package/src/svgcomponent.ts +0 -537
  293. package/src/tabbar.ts +0 -151
  294. package/src/tabview.ts +0 -110
  295. package/src/textarea.ts +0 -187
  296. package/src/textedit.ts +0 -544
  297. package/src/toaster.ts +0 -80
  298. package/src/tools.ts +0 -1468
  299. package/src/tooltips.ts +0 -191
  300. package/src/treeview.ts +0 -675
  301. package/src/version.ts +0 -30
  302. package/src/x4dom.ts +0 -57
  303. package/src/x4events.ts +0 -585
  304. package/src/x4react.ts +0 -42
  305. package/tsconfig.json +0 -21
package/src/component.ts DELETED
@@ -1,2344 +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
-
31
- /**
32
- * @todo
33
- * create Container class
34
- */
35
-
36
- import { pascalCase, Rect, isString, isArray, Size, isNumber, asap, HtmlString, isHtmlString, Constructor, getMousePos } from './tools';
37
- import { x4document } from './x4dom';
38
-
39
- import { Stylesheet, ComputedStyle } from './styles';
40
- import { _tr } from './i18n';
41
- import { BasicEvent, EventCallback } from './x4events';
42
- import { BaseComponent, BaseComponentProps, BaseComponentEventMap } from './base_component';
43
- import { IDOMEvents, X4ElementEventMap } from './dom_events';
44
-
45
- export { HtmlString, isHtmlString, html } from './tools'
46
-
47
- export interface EventHandler<T> {
48
- (ev: T): any;
49
- }
50
-
51
- export interface ICaptureInfo {
52
- initiator: Component;
53
- handler: EventHandler<UIEvent>;
54
- iframes: NodeListOf<HTMLIFrameElement>;
55
- }
56
-
57
- /** @ignore classname prefix for system classes */
58
- const _x4_ns_prefix = 'x-';
59
-
60
- // -- elements -----------
61
-
62
- /** @ignore where event handlers are stored */
63
- const _x4_el_store = Symbol();
64
-
65
- /** @ignore where Component is stored in dom */
66
- const _x4_el_sym = Symbol();
67
-
68
- /** @ignore properties without 'px' unit */
69
- export const _x4_unitless = {
70
- animationIterationCount: 1, borderImageOutset: 1, borderImageSlice: 1, borderImageWidth: 1, boxFlex: 1, boxFlexGroup: 1,
71
- boxOrdinalGroup: 1, columnCount: 1, flex: 1, flexGrow: 1, flexPositive: 1, flexShrink: 1, flexNegative: 1, flexOrder: 1,
72
- gridRow: 1, gridColumn: 1, fontWeight: 1, lineClamp: 1, lineHeight: 1, opacity: 1, order: 1, orphans: 1, tabSize: 1, widows: 1,
73
- zIndex: 1, zoom: 1,
74
-
75
- // SVG-related _properties
76
- fillOpacity: 1, floodOpacity: 1, stopOpacity: 1, strokeDasharray: 1, strokeDashoffset: 1, strokeMiterlimit: 1, strokeOpacity: 1,
77
- strokeWidth: 1,
78
- };
79
-
80
- /** @ignore this events must be defined on domNode (do not bubble) */
81
- const unbubbleEvents = {
82
- mouseleave: 1, mouseenter: 1, load: 1, unload: 1, scroll: 1, focus: 1, blur: 1, rowexit: 1, beforeunload: 1, stop: 1,
83
- dragdrop: 1, dragenter: 1, dragexit: 1, draggesture: 1, dragover: 1, contextmenu: 1, create: 2, sizechange: 2
84
- };
85
-
86
- /** @ignore */
87
- const passiveEvents = {
88
- touchstart: 1, touchmove: 1, touchend: 1,
89
- //pointerdown: 1, pointermove: 1, pointerup: 1,
90
- };
91
-
92
- /** ignore */
93
- const reNumber = /^-?\d+(\.\d+)?$/;
94
-
95
-
96
- /**
97
- *
98
- */
99
-
100
- export type VoidCallback = () => void;
101
-
102
- export type ComponentOrString = Component | string | HtmlString;
103
- export type ComponentContent = ComponentOrString | ComponentOrString[];
104
-
105
- interface IMap<T> {
106
- [key: string]: T;
107
- }
108
-
109
- /**
110
- * DblClick Event
111
- * double click event
112
- */
113
-
114
- export interface EvDblClick extends BasicEvent {
115
- }
116
-
117
- export function EvDblClick(context = null) {
118
- return BasicEvent<EvDblClick>({ context });
119
- }
120
-
121
- /**
122
- * DblFocus Event
123
- * double click event
124
- */
125
-
126
- export interface EvFocus extends BasicEvent {
127
- readonly focus: boolean;
128
- }
129
-
130
- export function EvFocus( focus = true, context = null) {
131
- return BasicEvent<EvFocus>({ focus, context });
132
- }
133
-
134
- /**
135
- * basic event map of Component
136
- */
137
-
138
- export interface CEventMap extends BaseComponentEventMap {
139
- }
140
-
141
-
142
-
143
-
144
- /**
145
- * Basic properties of every Component
146
- */
147
-
148
- export interface CProps<T extends CEventMap = CEventMap> extends BaseComponentProps<T> {
149
- tag?: string; // dom Tag <div> if not specified
150
- ns?: string; // namespace (usefull for svg)
151
- cls?: string; // elements classes (space separated), prefix class name with @ to make it system wide
152
- id?: string; // element id
153
- style?: IMap<string | number>; // element style
154
- attrs?: object; // element attributes
155
-
156
- dom_events?: IDOMEvents; // DOM event handlers
157
- data?: any; // element user data (you can store everything you need here)
158
- content?: ComponentContent; // array of sub components
159
- tooltip?: string; // tooltip text
160
- ref?: string; // internal reference for itemWithRef
161
-
162
- // shortcuts for element style
163
- left?: number;
164
- top?: number;
165
- width?: number | string;
166
- height?: number | string;
167
- tabIndex?: number | boolean;
168
-
169
- flex?: number | string; // add @flex class to the element
170
- enabled?: boolean; // add @disabled to the element if false
171
- }
172
-
173
- interface CInternalProps {
174
- dom_events: any;
175
- classes: IMap<boolean>;
176
- uid: number;
177
- inrender: boolean;
178
- }
179
-
180
- /**
181
- *
182
- */
183
-
184
- export class Component<P extends CProps<CEventMap> = CProps<CEventMap>, E extends CEventMap = CEventMap> extends BaseComponent<P, E> {
185
- private m_dom: HTMLElement;
186
- private m_iprops: CInternalProps;
187
-
188
- private static __sb_width; // scrollbar width
189
- private static __comp_guid = 1000; // component global unique id
190
- private static __privateEvents: any = {};
191
- private static __sizeObserver: ResizeObserver; // resize observer
192
- private static __createObserver: MutationObserver; // creation observer
193
- //private static __intersectionObserver: IntersectionObserver; // visibility observer
194
-
195
- private static __capture: ICaptureInfo = null;
196
- private static __capture_mask = null;
197
- private static __css = null;
198
-
199
- constructor(props: P = null ) {
200
- super(props ?? {} as P );
201
-
202
- this.m_iprops = {
203
- classes: {},
204
- dom_events: {},
205
- uid: Component.__comp_guid++,
206
- inrender: false,
207
- };
208
-
209
- // prepare iprops
210
- if( this.m_props.cls ) {
211
- this.addClass( this.m_props.cls );
212
- }
213
- }
214
-
215
- /**
216
- *
217
- */
218
-
219
- get uid() {
220
- return this.m_iprops.uid;
221
- }
222
-
223
- /**
224
- * change the component content
225
- * @param content new content or null
226
- */
227
-
228
- setContent(content: ComponentContent, refreshAll = false ) {
229
- this.m_props.content = content ?? [];
230
-
231
- if( this.m_iprops.inrender || !this.m_dom ) {
232
- return;
233
- }
234
-
235
- if (refreshAll) {
236
- this.update();
237
- }
238
- else {
239
- this._updateContent();
240
- }
241
- }
242
-
243
-
244
- /**
245
- * add a new child to the component content
246
- * @param content
247
- */
248
-
249
- appendChild(content: ComponentContent) {
250
-
251
- if (!content) {
252
- return;
253
- }
254
-
255
- const append = (c) => {
256
-
257
- if (!this.m_props.content) {
258
- this.m_props.content = [];
259
- }
260
- else if (!isArray(this.m_props.content)) {
261
- this.m_props.content = [this.m_props.content];
262
- }
263
-
264
- this.m_props.content.push(c);
265
- if (this.m_dom) {
266
- this._appendChild(c);
267
- }
268
- }
269
-
270
- if (isArray(content)) {
271
- content.forEach(append);
272
- }
273
- else {
274
- append(content);
275
- }
276
- }
277
-
278
- /**
279
- *
280
- */
281
-
282
- setTag( tag: string, namespace?: string ) {
283
- this.m_props.tag = tag;
284
- if( namespace ) {
285
- this.m_props.ns = namespace;
286
- }
287
- }
288
-
289
- /**
290
- * get the Component data value
291
- * @param name name to get
292
- */
293
-
294
- getData(name: string | Symbol): any {
295
- if (this.m_props.data !== undefined) {
296
- return this.m_props.data[name.toString()];
297
- }
298
-
299
- return undefined;
300
- }
301
-
302
- /**
303
- * set the Component data value
304
- * @param name name to get
305
- * @param value
306
- */
307
-
308
- setData(name: string | Symbol , value: any) {
309
-
310
- let data = this.m_props.data;
311
- if (data === undefined) {
312
- data = this.m_props.data = {};
313
- }
314
-
315
- data[name.toString()] = value;
316
- }
317
-
318
-
319
- /**
320
- * return the DOM associated with the Component (if any)
321
- */
322
-
323
- get dom(): HTMLElement {
324
- return this.m_dom;
325
- }
326
-
327
- /**
328
- * shows the element
329
- * @param show
330
- */
331
-
332
- show(show?: boolean) {
333
- if (show === undefined || show === true) {
334
- this.removeClass('@hidden');
335
- }
336
- else {
337
- this.addClass('@hidden');
338
- }
339
- }
340
-
341
- /**
342
- * hides the element
343
- */
344
- hide() {
345
- this.addClass('@hidden');
346
- }
347
-
348
- /**
349
- * enable or disable the element
350
- * @param enable
351
- */
352
-
353
- enable(enable?: boolean) {
354
- if (enable === undefined || enable === true) {
355
- this.removeClass('@disable');
356
- this.removeAttribute('disabled');
357
- }
358
- else {
359
- this.disable();
360
- }
361
- }
362
-
363
- /**
364
- * disable the element
365
- */
366
-
367
- disable() {
368
- this.addClass('@disable');
369
- this.setAttribute('disabled', '');
370
- }
371
-
372
- /**
373
- * set the focus on the element
374
- */
375
-
376
- focus() {
377
- console.assert(!!this.m_dom);
378
- this.m_dom.focus();
379
- }
380
-
381
- /**
382
- * change the object style
383
- * @param style - style to add
384
- * @example ```typescript
385
- * el.setStyle( {left:100} );
386
- */
387
-
388
- public setStyle(style: object) {
389
- for (let s in style) {
390
- this.setStyleValue(s, style[s]);
391
- }
392
- }
393
-
394
- /**
395
- * change a style value
396
- * @param name string style nale
397
- * @param value any style value or null to remove style
398
- */
399
-
400
- setStyleValue(name: string, value: any) {
401
- let style = this.m_props.style;
402
- if (!style) {
403
- style = this.m_props.style = {};
404
- }
405
-
406
- style[name] = value;
407
- this._setDomStyleValue(name, value);
408
- }
409
-
410
- private _setDomStyleValue(name: string, value: string | number) {
411
- if (this.m_dom) {
412
-
413
- if (value === undefined) {
414
- value = null;
415
- }
416
- else if (!_x4_unitless[name] && (isNumber(value) || reNumber.test(value))) {
417
- value = value + 'px';
418
- }
419
-
420
- this.m_dom.style[name] = value;
421
- }
422
- }
423
-
424
- /**
425
- * compute the element style
426
- * @return all styles computed
427
- */
428
-
429
- public getComputedStyle(pseudoElt?: string): ComputedStyle {
430
- if (this.dom) {
431
- return new ComputedStyle(getComputedStyle(this.dom, pseudoElt ?? null));
432
- }
433
-
434
- return new ComputedStyle(this.m_props.style as any);
435
- }
436
-
437
- /**
438
- * return a single stype value
439
- * @param name - value to get
440
- */
441
-
442
- public getStyleValue(name: string) {
443
- return this.getComputedStyle()[name];
444
- }
445
-
446
- /**
447
- * define the elements attributes
448
- * @param attrs
449
- */
450
-
451
- public setAttributes(attrs: any) {
452
- for (let a in attrs) {
453
- this.setAttribute(a, attrs[a]);
454
- }
455
- }
456
-
457
- /**
458
- * change a single attribute
459
- * @param name attribute name
460
- * @param value new value
461
- */
462
-
463
- public setAttribute(name: string, value: any) {
464
-
465
- if (value === false || value === undefined) {
466
- this.removeAttribute(name);
467
- }
468
- else {
469
- if (value === true) {
470
- value = '';
471
- }
472
- else if (isNumber(value)) {
473
- value = '' + value;
474
- }
475
-
476
- let attrs = this.m_props.attrs;
477
- if (!attrs) {
478
- attrs = this.m_props.attrs = {};
479
- }
480
-
481
- attrs[name] = value;
482
- this._setDomAttribute(name, value);
483
- }
484
- }
485
-
486
- private _setDomAttribute(name: string, value: string) {
487
- if (this.m_dom) {
488
- this.m_dom.setAttribute(name, value);
489
- }
490
- }
491
-
492
- /**
493
- * remove an atrribute
494
- * @param name name of the attribute
495
- */
496
- public removeAttribute(name: string) {
497
- let attrs = this.m_props.attrs;
498
- if (!attrs) {
499
- return;
500
- }
501
-
502
- delete attrs[name];
503
-
504
- if (this.m_dom) {
505
- this.m_dom.removeAttribute(name);
506
- }
507
- }
508
-
509
- /**
510
- * get an attribute value
511
- * @param {string} name - attribute name
512
- * @return {string} attribute value
513
- * @example ```typescript
514
- * let chk = el.getAttribute( 'checked' );
515
- * @review double cache
516
- */
517
-
518
- public getAttribute(name: string): string {
519
- if (this.m_dom) {
520
- return this.m_dom.getAttribute(name);
521
- }
522
- else {
523
- if (!this.m_props.attrs) {
524
- return undefined;
525
- }
526
-
527
- return this.m_props.attrs[name];
528
- }
529
- }
530
-
531
- /**
532
- * check if the element has an attribute
533
- * @param name attribute name
534
- * @return true is attribute is present
535
- * @example ```typescript
536
- * if( el.hasAttribute('checked') ) {
537
- * }
538
- */
539
-
540
- public hasAttribute(name: string): boolean {
541
- if (this.m_dom) {
542
- return this.m_dom.hasAttribute(name);
543
- }
544
- else {
545
- return this.m_props.attrs.hasOwnProperty(name);
546
- }
547
- }
548
-
549
-
550
- /**
551
- * a some classnames to the component
552
- * classes can be separated by a space
553
- * @param cls class to add
554
- * @example ```typescript
555
- * addClass( 'my class name @flex' );
556
- */
557
-
558
- public addClass(name: string) {
559
-
560
- if (name === null || name === undefined) {
561
- return;
562
- }
563
-
564
- name = name.trim();
565
- if (name === '') {
566
- return;
567
- }
568
-
569
- let add = (c) => {
570
-
571
- if (c === undefined || c === null || c === '') {
572
- return;
573
- }
574
-
575
- c = this._makeCls(c);
576
-
577
- // update vdom
578
- classes[c] = true;
579
-
580
- // update dom
581
- if (this.m_dom) {
582
- this.m_dom.classList.add(c);
583
- }
584
- }
585
-
586
- let classes = this.m_iprops.classes;
587
- if (name.indexOf(' ') < 0) {
588
- add(name);
589
- }
590
- else {
591
- let names = name.split(' ');
592
- names.forEach((n) => add(n));
593
- }
594
- }
595
-
596
- /**
597
- * Remove a class from the element
598
- * @param {string|array} name - classes in string form can be space separated
599
- *
600
- * @example ```typescript
601
- * el.removeClass( 'myclass' );
602
- * el.removeClass( 'myclass1 myclass2' );
603
- */
604
-
605
- public removeClass(name: string): void {
606
-
607
- if (name === undefined) {
608
- return;
609
- }
610
-
611
- let remove = (c) => {
612
- if (c === undefined || c === null || c === '') {
613
- return;
614
- }
615
-
616
- c = this._makeCls(c);
617
-
618
- delete this.m_iprops.classes[c];
619
- if (this.m_dom) {
620
- this.m_dom.classList.remove(c);
621
- }
622
- }
623
-
624
- // faster
625
- if (name.indexOf(' ') < 0) {
626
- remove(name);
627
- }
628
- else {
629
- // build class list
630
- let classes = name.trim().split(' ');
631
- for (let c of classes) {
632
- if (c !== undefined && c !== null && c !== '') {
633
- remove(c);
634
- }
635
- }
636
- }
637
- }
638
-
639
- /**
640
- *
641
- * @param cls
642
- * @param set
643
- */
644
-
645
- public setClass(cls: string, set: boolean) {
646
- if (set) { this.addClass(cls); }
647
- else { this.removeClass(cls); }
648
- return this;
649
- }
650
-
651
- /**
652
- * Toggle a class from the element (if present remove, if absent add)
653
- * @param {string|string[]} name - classes in string form can be space separated
654
- * @example ```typescript
655
- * el.toggleClass( 'myclass' );
656
- * el.toggleClass( 'myclass1 myclass2');
657
- * el.toggleClass( ['myclass1','myclass2']);
658
- */
659
-
660
- public toggleClass(name: string): void {
661
-
662
- let toggle = (c) => {
663
- if (c === undefined && c === null && c === '') {
664
- return;
665
- }
666
-
667
- c = this._makeCls(c);
668
- if (this.m_iprops.classes[c]) {
669
- delete this.m_iprops.classes[c]
670
- }
671
- else {
672
- this.m_iprops.classes[c] = true;
673
- }
674
-
675
- if (this.m_dom) {
676
- this.m_dom.classList.toggle(c);
677
- }
678
- }
679
-
680
- // faster
681
- if (name.indexOf(' ') < 0) {
682
- toggle(name);
683
- }
684
- else {
685
-
686
- // build class list
687
- let classes = name.trim().split(' ');
688
- for (let c of classes) {
689
- toggle(c);
690
- }
691
- }
692
- }
693
-
694
- /**
695
- * check if the object has the class
696
- * @param cls
697
- */
698
-
699
- public hasClass(cls: string): boolean {
700
-
701
- let c = this._makeCls(cls);
702
- if (this.m_dom) {
703
- return this.dom.classList.contains(c);
704
- }
705
- else {
706
- return !!this.m_iprops.classes[c];
707
- }
708
- }
709
-
710
- /**
711
- * remove all classes from the object
712
- * this is usefull for component recycling & reusing
713
- */
714
-
715
- public clearClasses() {
716
- this.m_iprops.classes = {};
717
- if (this.m_dom) {
718
- return this.m_dom.classList.value = '';
719
- }
720
- }
721
-
722
- public _build(): HTMLElement {
723
- if (this.m_dom) {
724
- return this.m_dom;
725
- }
726
-
727
- this._createDOM();
728
- return this.m_dom;
729
- }
730
-
731
- public render(props: P) {
732
- }
733
-
734
- public _createDOM(): HTMLElement {
735
-
736
- if (this.m_dom) {
737
- return this.m_dom;
738
- }
739
-
740
- // setup props
741
- const props = this.m_props;
742
-
743
- if( props.tabIndex!==undefined ) {
744
- this._setTabIndex( props.tabIndex );
745
- }
746
- this.render(props);
747
-
748
- // shortcuts ---------
749
- if (props.left !== undefined) { this.setStyleValue('left', props.left); }
750
- if (props.top !== undefined) { this.setStyleValue('top', props.top); }
751
- if (props.width !== undefined) { this.setStyleValue('width', props.width); }
752
- if (props.height !== undefined) { this.setStyleValue('height', props.height); }
753
-
754
- if (props.flex !== undefined) {
755
- this.addClass('@flex');
756
- if (props.flex != 1) {
757
- this.setStyleValue('flex', props.flex);
758
- }
759
- }
760
-
761
- if (props.enabled === false) {
762
- this.disable();
763
- }
764
-
765
- // shortcut: tip
766
- if (props.tooltip !== undefined) {
767
- this.setAttribute('tip', props.tooltip.replace(/\n/gi, '<br/>'));
768
- }
769
-
770
-
771
- // prepare iprops
772
- if (props.dom_events) {
773
- for (let ename in props.dom_events) {
774
- this._setDomEvent(ename, props.dom_events[ename]);
775
- }
776
- }
777
-
778
- this._genClassName();
779
- this.m_props.cls = undefined; // now classes are tranfered to m_iprops
780
-
781
- // create self
782
- let vdom = this.m_iprops;
783
-
784
- if (props.ns) {
785
- this.m_dom = <HTMLElement>x4document.createElementNS(props.ns, props.tag ?? 'div');
786
- }
787
- else {
788
- this.m_dom = x4document.createElement( (props.tag ?? 'div') as any );
789
- }
790
-
791
- this.m_dom[_x4_el_sym] = this;
792
-
793
- //let me = Object.getPrototypeOf(this);
794
- //console.log( 'create', this.m_iprops.uid, me.constructor.name );
795
-
796
- // classes
797
- this.m_dom.classList.add(...Object.keys(vdom.classes));
798
-
799
- // styles
800
- let sty = props.style;
801
- if (sty) {
802
- for (let s in sty) {
803
- this._setDomStyleValue(s, sty[s]);
804
- }
805
- }
806
-
807
- // attributes
808
- let att = props.attrs;
809
- if (att) {
810
- for (let a in att) {
811
- const attr = att[a];
812
- if( attr!==false && attr!==undefined ) {
813
- this._setDomAttribute(a, att[a]);
814
- }
815
- }
816
- }
817
-
818
- // special properties
819
- if (this.m_props.id) {
820
- this._setDomAttribute('id', this.m_props.id);
821
- }
822
-
823
- // events
824
- let evt = this.m_iprops.dom_events;
825
- if (evt) {
826
- for (let e in evt) {
827
- let handlers = evt[e];
828
- for (let h of handlers) {
829
- this.createEvent(e, h);
830
- }
831
- }
832
- }
833
-
834
- // create children
835
- let content = props.content;
836
- if (content) {
837
-
838
- if (!isArray(content)) {
839
- content = [content];
840
- }
841
-
842
- content.forEach((el) => {
843
- if (!el) {
844
- return;
845
- }
846
-
847
- if (isString(el)) {
848
- this.m_dom.insertAdjacentText('beforeend', el);
849
- }
850
- else if (isHtmlString(el)) {
851
- this.m_dom.insertAdjacentHTML('beforeend', el as string);
852
- }
853
- else if (el instanceof Component) {
854
- this.m_dom.append(el._build());
855
- }
856
- else {
857
- console.log( 'unknown element type: ', el );
858
- }
859
- });
860
- }
861
-
862
- // wait for dom insertion inside document.body
863
- if (!Component.__createObserver) {
864
- Component.__createObserver = new MutationObserver(Component._observeCreation);
865
- Component.__createObserver.observe(x4document.body, { childList: true, subtree: true });
866
- }
867
-
868
- return this.m_dom;
869
- }
870
-
871
- protected _setTabIndex(tabIndex: number | boolean, defValue = 0) {
872
-
873
- if (tabIndex === true) {
874
- tabIndex = 0;
875
- }
876
- else if (tabIndex === undefined) {
877
- tabIndex = defValue;
878
- }
879
-
880
- if (tabIndex !== false && tabIndex !== undefined) {
881
- this.setAttribute('tabindex', tabIndex);
882
- }
883
-
884
- this.m_props.tabIndex = tabIndex;
885
- }
886
-
887
- private static _observeCreation(mutations: MutationRecord[]) {
888
-
889
- // notify descendants that we have been created (dom exists)
890
-
891
- for (let mutation of mutations) {
892
-
893
- if (mutation.type == 'childList') {
894
-
895
- for (let i = 0, n = mutation.addedNodes.length; i < n; i++) {
896
-
897
- let add = mutation.addedNodes[i] as HTMLElement;
898
- let el = add[_x4_el_sym] as Component;
899
-
900
- if (el) {
901
- el.enumChilds((c: Component) => {
902
-
903
- if (c.dom && c.m_iprops.dom_events && c.m_iprops.dom_events.create) {
904
- c.dom.dispatchEvent(new Event('create'));
905
- }
906
-
907
- c.componentCreated();
908
-
909
- }, true);
910
-
911
- if (el.m_iprops.dom_events && el.m_iprops.dom_events.create) {
912
- el.dom.dispatchEvent(new Event('create'));
913
- }
914
-
915
- el.componentCreated();
916
- }
917
- }
918
- }
919
- }
920
- }
921
-
922
- public dispose() {
923
- if (this.m_dom) {
924
- this._dispose(true,true);
925
- }
926
- }
927
-
928
- protected _dispose(with_dom: boolean, timers: boolean ) {
929
-
930
- let _dom = this.m_dom;
931
-
932
- // free attached resources
933
- delete _dom[_x4_el_sym];
934
- delete _dom[_x4_el_store];
935
-
936
- //
937
- if (with_dom) {
938
- _dom.remove();
939
- }
940
-
941
- // notify every child that they will be removed
942
- this.enumChilds((c: Component) => {
943
- c._dispose(false,true);
944
- });
945
-
946
- this.m_dom = null;
947
-
948
- if( timers ) {
949
- this.disposeTimers();
950
- }
951
-
952
- this.componentDisposed();
953
- // todo: pb on update this.removeAllListeners( null );
954
- }
955
-
956
- componentDisposed() {
957
- }
958
-
959
- componentCreated() {
960
- }
961
-
962
-
963
-
964
-
965
-
966
-
967
-
968
- /**
969
- *
970
- */
971
-
972
- public update(delay = 0) {
973
-
974
- if (this.m_dom) {
975
-
976
- const _update = () => {
977
- let oldDOM = this.m_dom;
978
- this._dispose(false,false);
979
-
980
- let newDOM = this._build();
981
- console.assert( !!oldDOM.parentNode, 'update in componentCreated is not allowed, use updateContent' );
982
-
983
- oldDOM.parentNode.replaceChild(newDOM, oldDOM);
984
- }
985
-
986
- if (delay) {
987
- this.singleShot(_update, delay);
988
- }
989
- else {
990
- _update();
991
- }
992
- }
993
- }
994
-
995
- /**
996
- * empty the node
997
- */
998
- public _empty( ) {
999
- //this.m_dom.innerHTML = '';
1000
-
1001
- const el = this.m_dom;
1002
- if( !el ) {
1003
- return;
1004
- }
1005
-
1006
- while (el.firstChild) {
1007
- el.removeChild(el.firstChild);
1008
- }
1009
- }
1010
-
1011
- public _updateContent() {
1012
-
1013
- if (!this.m_dom) {
1014
- return;
1015
- }
1016
-
1017
- this._empty( );
1018
-
1019
- let content = this.m_props.content;
1020
-
1021
- // create children
1022
- if (content) {
1023
-
1024
- if (!isArray(content)) {
1025
- content = [content];
1026
- }
1027
-
1028
- content.forEach((el) => {
1029
- if (!el) {
1030
- return;
1031
- }
1032
-
1033
- if (isHtmlString(el)) {
1034
- this.m_dom.insertAdjacentHTML('beforeend', el as string);
1035
- }
1036
- else if (el instanceof Component) {
1037
- this.m_dom.append(el._build());
1038
- }
1039
- else {
1040
- this.m_dom.insertAdjacentText('beforeend', el + '');
1041
- }
1042
- });
1043
- }
1044
-
1045
- }
1046
-
1047
- /**
1048
- * @return the bounding rectangle
1049
- * @example ```typescript
1050
- * let rc = el.getBoundingRect( );
1051
- * console.log( rc.left, rc.top, rc.right, rc.bottom );
1052
- */
1053
-
1054
- public getBoundingRect(withMargins = false): Rect {
1055
- console.assert(this.dom != null, 'cannot get bounding rect of an non DOM element');
1056
- let r = this.dom.getBoundingClientRect();
1057
-
1058
- let rc = new Rect(r.left, r.top, r.width, r.height);
1059
-
1060
- if (withMargins) {
1061
-
1062
- let st = this.getComputedStyle();
1063
-
1064
- let tm = st.parse('marginTop'),
1065
- bm = st.parse('marginBottom'),
1066
- lm = st.parse('marginLeft'),
1067
- rm = st.parse('marginRight');
1068
-
1069
- rc.left -= lm;
1070
- rc.width += lm + rm;
1071
-
1072
- rc.top -= tm;
1073
- rc.height += tm + bm;
1074
- }
1075
-
1076
- return rc;
1077
- }
1078
-
1079
- /**
1080
- * append a new dom event handler
1081
- * @param name - you can specify multiple names separated by a space
1082
- * @param handler
1083
- * @example
1084
- *
1085
- * this.setDomEvent( 'drag drop', this._handleDrag, this );
1086
- * this.setDomEvent( 'dblclick', this._handleDblClick, this );
1087
- */
1088
-
1089
- public setDomEvent<K extends keyof X4ElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: X4ElementEventMap[K]) => void) {
1090
- let _listener = listener as EventListener;
1091
- this._setDomEvent(type as string, _listener);
1092
- }
1093
-
1094
- private _setDomEvent(type: string, listener: EventListener) {
1095
-
1096
- // add event to the vdom
1097
- if (!this.m_iprops.dom_events) {
1098
- this.m_iprops.dom_events = {};
1099
- }
1100
-
1101
- let listeners = this.m_iprops.dom_events[type];
1102
- if (!listeners) {
1103
- listeners = this.m_iprops.dom_events[type] = [listener];
1104
- }
1105
- else {
1106
- listeners.push(listener);
1107
- }
1108
-
1109
- if (this.m_dom) {
1110
- //this.m_dom.addEventListener(type, listener);
1111
- this.createEvent(type, listener);
1112
- }
1113
- }
1114
-
1115
- /**
1116
- *
1117
- */
1118
-
1119
- public clearDomEvent<K extends keyof X4ElementEventMap>(type: K) {
1120
- if (!this.m_iprops.dom_events) {
1121
- return;
1122
- }
1123
-
1124
- delete this.m_iprops.dom_events[type];
1125
-
1126
- let _dom = this.m_dom;
1127
- if (_dom) {
1128
- let store = _dom[_x4_el_store];
1129
- if (store) {
1130
- delete store[type];
1131
- }
1132
- }
1133
- }
1134
-
1135
- /**
1136
- *
1137
- * @param name
1138
- * @param handler
1139
- */
1140
-
1141
- public createEvent(name: string, handler: Function) {
1142
-
1143
- let _dom = this.m_dom;
1144
- let store = _dom[_x4_el_store];
1145
-
1146
- if (!store) {
1147
- store = _dom[_x4_el_store] = {};
1148
- }
1149
-
1150
- if (!store[name]) {
1151
- // no handler for this event...
1152
- store[name] = [handler];
1153
- }
1154
- else {
1155
- // append the handler
1156
- store[name].push(handler);
1157
- }
1158
-
1159
- if (unbubbleEvents[name] === 1) {
1160
- _dom['on' + name] = Component._dispatchUnbubbleEvent;
1161
- }
1162
- else if (!Component.__privateEvents[name]) {
1163
- Component.__privateEvents[name] = true; // todo count it
1164
-
1165
- if (passiveEvents[name]) {
1166
- x4document.addEventListener(name as any, Component._dispatchEvent, { passive: false, capture: true });
1167
- }
1168
- else {
1169
- x4document.addEventListener(name as any, Component._dispatchEvent, true);
1170
- }
1171
- }
1172
-
1173
- if (name === 'sizechange') {
1174
- if (!Component.__sizeObserver) {
1175
- Component.__sizeObserver = new ResizeObserver(Component._observeSize);
1176
- }
1177
-
1178
- Component.__sizeObserver.observe(this.m_dom);
1179
- }
1180
- }
1181
-
1182
- /**
1183
- * dispatch a dom event to the appropriated component
1184
- * called by the system
1185
- */
1186
-
1187
- private static _dispatchEvent(ev: any) {
1188
-
1189
- let target = ev.target,
1190
- noup = unbubbleEvents[ev.type] === 2;
1191
-
1192
- while (target) {
1193
- if (target[_x4_el_store]) {
1194
- let store = target[_x4_el_store][ev.type];
1195
- if (store) {
1196
- let el = target[_x4_el_sym];
1197
- let root = el?.root ?? null;
1198
-
1199
- if (store instanceof Array) {
1200
- store.some((fn) => {
1201
- fn(ev, root);
1202
- if (!el.dom) {
1203
- return true;
1204
- }
1205
- });
1206
- }
1207
- else {
1208
- store(ev, root);
1209
- }
1210
-
1211
- if (ev.cancelBubble || ev.defaultPrevented || noup) {
1212
- break;
1213
- }
1214
- }
1215
- }
1216
-
1217
- target = target.parentNode;
1218
-
1219
- // no need to go above
1220
- if (target == document) {
1221
- break;
1222
- }
1223
- }
1224
- }
1225
-
1226
- /**
1227
- * dispatch a dom event to the appropriated component
1228
- * called by the system
1229
- */
1230
-
1231
- private static _dispatchUnbubbleEvent(ev: any) {
1232
-
1233
- let target = ev.currentTarget || ev.target,
1234
- eventType = ev.type;
1235
-
1236
- let eventStore = target[_x4_el_store],
1237
- store = eventStore && eventStore[eventType];
1238
-
1239
- if (store) {
1240
-
1241
- let el = target[_x4_el_sym];
1242
- let root = el?.root ?? null;
1243
-
1244
- if (store instanceof Array) {
1245
- store.forEach((fn) => {
1246
- fn(ev, root);
1247
- });
1248
- }
1249
- else {
1250
- store(ev, root);
1251
- }
1252
- }
1253
- }
1254
-
1255
- /**
1256
- * called when a size change on an observed component
1257
- */
1258
-
1259
- private static _observeSize(entries: ResizeObserverEntry[]) {
1260
-
1261
- entries.forEach((entry) => {
1262
- let dom = entry.target as HTMLElement;
1263
- if (dom.offsetParent !== null) {
1264
- dom.dispatchEvent(new Event('sizechange'));
1265
- }
1266
- });
1267
- }
1268
-
1269
- /**
1270
- * enum all children recursively
1271
- * @param recursive - if true do a full sub-child search
1272
- * @param cb - callback
1273
- * return true to stop enumeration
1274
- */
1275
-
1276
- enumChilds(cb: (child: Component) => boolean | void, recursive = false): boolean {
1277
-
1278
- // use dom if available
1279
- if (this.m_dom) {
1280
-
1281
- let el = this.m_dom.firstChild;
1282
-
1283
- while (el) {
1284
- // get component (if any)
1285
- let cel = el[_x4_el_sym];
1286
- if (cel) {
1287
- cb(cel);
1288
-
1289
- if (recursive && cel.enumChilds(cb, true) === true) {
1290
- return true;
1291
- }
1292
- }
1293
-
1294
- el = el.nextSibling;
1295
- }
1296
- }
1297
- else {
1298
- let content = this.m_props.content;
1299
- if (!content) {
1300
- return;
1301
- }
1302
-
1303
- if (!isArray(content)) {
1304
- content = [content];
1305
- }
1306
-
1307
- content.some((el) => {
1308
- if (!el || isString(el) || isHtmlString(el)) {
1309
- return;
1310
- }
1311
-
1312
- if (cb(el)) {
1313
- return true;
1314
- }
1315
-
1316
- if (recursive && el.enumChilds(cb, true) === true) {
1317
- return true;
1318
- }
1319
- });
1320
- }
1321
-
1322
- return false;
1323
- }
1324
-
1325
- /**
1326
- * apprend a child to the DOM
1327
- * @param props child to append (or string)
1328
- */
1329
-
1330
- private _appendChild(el: ComponentOrString) {
1331
-
1332
- if (isString(el)) {
1333
- this.m_dom.insertAdjacentText('beforeend', el);
1334
- }
1335
- else if (isHtmlString(el)) {
1336
- this.m_dom.insertAdjacentHTML('beforeend', el as string);
1337
- }
1338
- else {
1339
- let component: Component = el;
1340
-
1341
- try {
1342
- component._build();
1343
- this.m_dom.appendChild(component.m_dom);
1344
- }
1345
- catch (e) {
1346
- console.error(e);
1347
- }
1348
- }
1349
- }
1350
-
1351
- /**
1352
- * generate classes from the component inheritance
1353
- * @example
1354
- * Button extends Component will give
1355
- * x-comp x-button
1356
- */
1357
-
1358
- private _genClassName() {
1359
-
1360
- this.addClass('@comp');
1361
-
1362
- let me = Object.getPrototypeOf(this);
1363
- while (me && me.constructor !== Component) {
1364
- let clsname = me.constructor.name;
1365
- this.addClass('@' + pascalCase(clsname));
1366
-
1367
- me = Object.getPrototypeOf(me);
1368
- }
1369
-
1370
- //done in ctor now
1371
- //this.addClass(this.m_props.cls);
1372
- }
1373
-
1374
- /**
1375
- * prepend the system class name prefix on a name if needed (if class starts with @)
1376
- */
1377
-
1378
- private _makeCls(cls: string): string {
1379
- if (cls[0] == '@') {
1380
- return cls = _x4_ns_prefix + cls.substring(1);
1381
- }
1382
- else {
1383
- return cls;
1384
- }
1385
- }
1386
-
1387
- /**
1388
- *
1389
- */
1390
-
1391
- private static dispatchCaptures(event: Event) {
1392
- Component.__capture.handler(event as UIEvent);
1393
- }
1394
-
1395
- /**
1396
- * capture mouse events
1397
- * @param capture name of the current capture
1398
- * @param callback funciton to call on captured mouse events
1399
- *
1400
- * @example
1401
- * Component.setCapture( this, ( ev: MouseEvent, initiator: Component ) => {
1402
- * if( ev.type=='mousemove' ) {
1403
- * this.setStyle( {
1404
- * left: ev.clientX,
1405
- * top: ev.clientY
1406
- * } );
1407
- * }
1408
- * else if( ev.type=='mouseup' ) {
1409
- * Component.releaseCapture( );
1410
- * }
1411
- * } );
1412
- */
1413
-
1414
- protected static setCapture(initiator: Component, listener: EventHandler<UIEvent>) {
1415
-
1416
- console.assert(!Component.__capture);
1417
- if (Component.__capture) {
1418
- debugger;
1419
- }
1420
-
1421
- // todo: review that
1422
-
1423
- let iframes = x4document.querySelectorAll<HTMLIFrameElement>("iframe");
1424
- iframes.forEach( f => {
1425
- flyWrap(f).setStyleValue( 'pointer-events', 'none' );
1426
- });
1427
-
1428
- let overs = x4document.querySelectorAll(":hover");
1429
-
1430
- let cursor = null;
1431
- if (overs.length) {
1432
- let elementOver = <HTMLElement>overs[overs.length - 1];
1433
- let style = window.getComputedStyle(elementOver);
1434
- cursor = style.cursor;
1435
- }
1436
-
1437
- Component.__capture_mask = x4document.createElement('div');
1438
- let mask = flyWrap(Component.__capture_mask);
1439
- mask.addClass('@capture-mask');
1440
-
1441
- if (cursor) {
1442
- mask.setStyleValue('cursor', cursor);
1443
- }
1444
-
1445
- x4document.body.appendChild(mask.dom);
1446
-
1447
- x4document.addEventListener('mousedown', Component.dispatchCaptures);
1448
- x4document.addEventListener('mousemove', Component.dispatchCaptures);
1449
- x4document.addEventListener('mouseup', Component.dispatchCaptures);
1450
-
1451
- x4document.addEventListener('touchstart', Component.dispatchCaptures);
1452
- x4document.addEventListener('touchmove', Component.dispatchCaptures);
1453
- x4document.addEventListener('touchend', Component.dispatchCaptures);
1454
-
1455
- Component.__capture = {
1456
- initiator,
1457
- handler: listener,
1458
- iframes
1459
- };
1460
- }
1461
-
1462
- protected static releaseCapture() {
1463
-
1464
- console.assert(!!Component.__capture);
1465
-
1466
- x4document.removeEventListener('touchstart', Component.dispatchCaptures);
1467
- x4document.removeEventListener('touchmove', Component.dispatchCaptures);
1468
- x4document.removeEventListener('touchend', Component.dispatchCaptures);
1469
-
1470
- x4document.removeEventListener('mousedown', Component.dispatchCaptures);
1471
- x4document.removeEventListener('mousemove', Component.dispatchCaptures);
1472
- x4document.removeEventListener('mouseup', Component.dispatchCaptures);
1473
-
1474
- Component.__capture.iframes.forEach( f => {
1475
- flyWrap(f).setStyleValue( 'pointer-events', null );
1476
- })
1477
-
1478
- Component.__capture = null;
1479
- if (Component.__capture_mask) {
1480
- x4document.body.removeChild(Component.__capture_mask);
1481
- Component.__capture_mask = null;
1482
- }
1483
- }
1484
-
1485
- /**
1486
- * ensure the component is visible
1487
- * @param: alignToTop
1488
- */
1489
-
1490
- public scrollIntoView(arg?: boolean | ScrollIntoViewOptions) {
1491
- if (this.m_dom) {
1492
-
1493
- const rel = new Rect( this.dom.getBoundingClientRect( ) );
1494
-
1495
- let top = undefined;
1496
- let bot = undefined;
1497
- let left = undefined;
1498
- let right = undefined;
1499
-
1500
- let pn = this.dom.parentElement;
1501
- const bdy = x4document.body;
1502
-
1503
- while( pn && pn!=bdy ) {
1504
-
1505
- const pr = pn.getBoundingClientRect( );
1506
-
1507
- if( top===undefined || top<pr.top ) {
1508
- top = pr.top;
1509
- }
1510
-
1511
- if( bot===undefined || bot>pr.bottom ) {
1512
- bot = pr.bottom;
1513
- }
1514
-
1515
- if( left===undefined || left<pr.left ) {
1516
- left = pr.left;
1517
- }
1518
-
1519
- if( right===undefined || right>pr.right ) {
1520
- right = pr.right;
1521
- }
1522
-
1523
- pn = pn.parentElement;
1524
- }
1525
-
1526
- if( top===undefined || rel.top<top || rel.bottom>bot || rel.left<left || rel.right>right ) {
1527
- //this.m_dom.scrollIntoView( true );
1528
- this.m_dom.scrollIntoView({ behavior: 'auto', block: 'nearest', inline: 'start' });
1529
- }
1530
-
1531
- //this.m_dom.scrollIntoView(arg);
1532
- }
1533
- }
1534
-
1535
- /**
1536
- * search for a given css selector
1537
- * @param selector
1538
- * @returns child or null
1539
- */
1540
-
1541
- public queryItem<T extends Component>(selector: string): T {
1542
- let result = <HTMLElement>this.dom.querySelector(selector);
1543
- return result ? Component.getElement<T>(result) : null;
1544
- }
1545
-
1546
- public queryAll(selector: string, cb?: (el: Component) => void): HTMLElement[] {
1547
- let elements:HTMLElement[] = Array.from( this.m_dom.querySelectorAll<HTMLElement>(selector) );
1548
-
1549
- if( cb ) {
1550
- elements.forEach((el) => {
1551
- cb(flyWrap(el as HTMLElement));
1552
- });
1553
- }
1554
-
1555
- return elements;
1556
- }
1557
-
1558
- /**
1559
- * find a child with the given ID
1560
- * @param id id (without '#')
1561
- * @returns child or null
1562
- *
1563
- * @example
1564
- * let btn = this.childWithId<Button>( 'myButtonId' );
1565
- */
1566
- public itemWithId<T extends Component>(id: string): T {
1567
- let result = <HTMLElement>this.dom.querySelector('#' + id);
1568
- return result ? Component.getElement<T>(result) : null;
1569
- }
1570
-
1571
- /**
1572
- * find a child with given ref
1573
- * @param ref
1574
- * @return found child or null
1575
- */
1576
-
1577
- public itemWithRef<T = Component>(ref: string): T {
1578
-
1579
- let result = null;
1580
- this.enumChilds((c: Component) => {
1581
- if (c.m_props.ref === ref) {
1582
- result = c;
1583
- return true;
1584
- }
1585
- }, true);
1586
-
1587
- return result as T;
1588
- }
1589
-
1590
- /**
1591
- *
1592
- */
1593
-
1594
- get ref() {
1595
- return this.m_props.ref;
1596
- }
1597
-
1598
- /**
1599
- *
1600
- */
1601
-
1602
- static getCss(): Stylesheet {
1603
- if (!Component.__css) {
1604
- Component.__css = new Stylesheet();
1605
- }
1606
-
1607
- return Component.__css;
1608
- }
1609
-
1610
- /**
1611
- * return the parent element
1612
- * care, object must have been created (dom!=null)
1613
- */
1614
-
1615
- public getParent(): Component {
1616
- console.assert(!!this.m_dom);
1617
-
1618
- let elParent = this.dom.parentNode;
1619
- return Component.getElement(<HTMLElement>elParent);
1620
- }
1621
-
1622
- /**
1623
- * get a component from a DOM element
1624
- * move up to the hierarchy to find the request class type.
1625
- * @param dom
1626
- * @param classname
1627
- * @returns
1628
- *
1629
- * @example
1630
- *
1631
- * with a DOM like that:
1632
- * Button
1633
- * Label
1634
- * Icon <- the DOM you have (dom-icon)
1635
- *
1636
- * let btn = Component.getElement( dom-icon, Button );
1637
- */
1638
-
1639
- static getElement<T extends Component>(dom: HTMLElement | Element, classname?: Constructor<T> | string ): T {
1640
-
1641
- if (classname) {
1642
-
1643
- const srhCls = isString(classname);
1644
-
1645
- while (dom) {
1646
- let el: Component = dom[_x4_el_sym];
1647
-
1648
- if( srhCls ) {
1649
- if( el && el.hasClass(classname) ) {
1650
- return el as T;
1651
- }
1652
- }
1653
- else if (el instanceof classname) {
1654
- return el;
1655
- }
1656
-
1657
- dom = dom.parentElement;
1658
- }
1659
-
1660
- return null;
1661
- }
1662
- else {
1663
- return dom ? dom[_x4_el_sym] : null;
1664
- }
1665
- }
1666
-
1667
- /**
1668
- * compute the scrollbar size ( width = height)
1669
- */
1670
-
1671
- static getScrollbarSize() {
1672
-
1673
- if (Component.__sb_width === undefined) {
1674
- let outerDiv = x4document.createElement('div');
1675
- outerDiv.style.cssText = 'overflow:auto;position:absolute;top:0;width:100px;height:100px';
1676
-
1677
- let innerDiv = x4document.createElement('div');
1678
- innerDiv.style.width = '200px';
1679
- innerDiv.style.height = '200px';
1680
-
1681
- outerDiv.appendChild(innerDiv);
1682
- x4document.body.appendChild(outerDiv);
1683
-
1684
- Component.__sb_width = outerDiv.offsetWidth - outerDiv.clientWidth;
1685
- x4document.body.removeChild(outerDiv);
1686
- }
1687
-
1688
- return Component.__sb_width;
1689
- }
1690
-
1691
- /**
1692
- * check if the Component is visible to the user
1693
- */
1694
-
1695
- isUserVisible(): boolean {
1696
- if (!this.m_dom) {
1697
- return false;
1698
- }
1699
-
1700
- return (this.m_dom.offsetParent !== null);
1701
- }
1702
- }
1703
-
1704
- /** @ignore */
1705
- let fly_element: Component = null;
1706
-
1707
- /**
1708
- * warp <b>temporarily</b> a DOM element to be able to acces to exact API
1709
- * @param dom dom element to wrap
1710
- * @review qui libere le fly_element ? -> timeout
1711
- */
1712
-
1713
- export function flyWrap<T extends Component>(dom: HTMLElement | EventTarget): T {
1714
-
1715
- if (dom[_x4_el_sym]) {
1716
- return dom[_x4_el_sym];
1717
- }
1718
-
1719
- let f = fly_element;
1720
- if (!f) { f = fly_element = new Component({}); }
1721
- (<any>f).m_dom = dom;
1722
-
1723
- return f as T;
1724
- }
1725
-
1726
-
1727
-
1728
-
1729
-
1730
-
1731
-
1732
-
1733
- /**
1734
- * simple flex spacer
1735
- */
1736
-
1737
- export class Flex extends Component {
1738
-
1739
- constructor(props: CProps = {}) {
1740
- if (!props.flex) {
1741
- props.flex = 1;
1742
- }
1743
-
1744
- super(props);
1745
- }
1746
- }
1747
-
1748
- /**
1749
- * simple space between 2 elements
1750
- */
1751
-
1752
- export class Space extends Component {
1753
-
1754
- m_size: number | string;
1755
-
1756
- constructor(size: number | string) {
1757
- super({});
1758
-
1759
- this.m_size = size;
1760
- }
1761
-
1762
- componentCreated() {
1763
-
1764
- // try to find if we are in a hz / vt / abs container
1765
- let dom = this.dom;
1766
- let style = null;
1767
-
1768
- while (dom) {
1769
- let el: Component = dom[_x4_el_sym];
1770
- if (el.hasClass('@hlayout')) {
1771
- style = { width: this.m_size };
1772
- break;
1773
- }
1774
- else if (el.hasClass('@vlayout')) {
1775
- style = { height: this.m_size };
1776
- break;
1777
- }
1778
-
1779
- dom = dom.parentElement;
1780
- }
1781
-
1782
- if (!style) {
1783
- style = { width: this.m_size, height: this.m_size };
1784
- }
1785
-
1786
- this.setStyle(style);
1787
- }
1788
- }
1789
-
1790
- /**
1791
- * sizable separator
1792
- */
1793
-
1794
- type SizeMode = null | 'minimize' | 'maximize' | 'restore';
1795
-
1796
- export interface EvSize extends BasicEvent {
1797
- readonly size: Size;
1798
- readonly mode: SizeMode;
1799
- }
1800
-
1801
- export function EvSize(size: Size, mode: SizeMode = null, context = null ): EvSize {
1802
- return BasicEvent<EvSize>({ size, mode, context });
1803
- }
1804
-
1805
- interface SeparatorEventMap extends CEventMap {
1806
- resize?: EvSize;
1807
- }
1808
-
1809
- interface SeparatorProps extends CProps<SeparatorEventMap> {
1810
- readonly orientation: 'vertical' | 'horizontal'; // vertical means vertical sizer so it resize horizontally
1811
- readonly sizing: 'before' | 'after';
1812
- readonly collapsible?: boolean;
1813
- }
1814
-
1815
- export class Separator extends Component<SeparatorProps, SeparatorEventMap> {
1816
-
1817
- m_irect: Rect;
1818
- m_delta: number;
1819
- m_target: Component;
1820
-
1821
- constructor(props: SeparatorProps) {
1822
- super(props);
1823
-
1824
- this.setDomEvent('mousedown', (e) => this._mousedown(e));
1825
- this.setDomEvent('touchstart', (e) => this._mousedown(e));
1826
- this.setDomEvent('dblclick', (e) => this._collapse(e) );
1827
- }
1828
-
1829
- render() {
1830
- this.addClass(this.m_props.orientation);
1831
- }
1832
-
1833
- private _collapse( ev: UIEvent ) {
1834
- if( this.m_props.collapsible ) {
1835
- this._findTarget();
1836
- if( this.m_target ) {
1837
- this.m_target.toggleClass( '@collapsed' );
1838
- }
1839
- }
1840
- }
1841
-
1842
- private _mousedown(ev: UIEvent) {
1843
-
1844
- if (ev.type == 'touchstart') {
1845
- let te = ev as TouchEvent;
1846
- if (te.touches.length == 1) {
1847
- this._startMoving(te.touches[0].pageX, te.touches[0].pageY, ev);
1848
- }
1849
- }
1850
- else {
1851
- let me = ev as MouseEvent;
1852
- this._startMoving(me.pageX, me.pageY, ev);
1853
- }
1854
- }
1855
-
1856
- _startMoving(x: number, y: number, ev: UIEvent) {
1857
- //if( this.m_props.callback ) {
1858
- // this.m_props.callback( ev, this );
1859
- //}
1860
- //else
1861
- {
1862
- this._findTarget();
1863
-
1864
- if (this.m_target) {
1865
-
1866
- if (this.m_props.orientation == 'horizontal') {
1867
- if (this.m_props.sizing == 'before') {
1868
- this.m_delta = x - this.m_irect.right;
1869
- }
1870
- else {
1871
- this.m_delta = x - this.m_irect.left;
1872
- }
1873
- }
1874
- else {
1875
- if (this.m_props.sizing == 'before') {
1876
- this.m_delta = y - this.m_irect.bottom;
1877
- }
1878
- else {
1879
- this.m_delta = y - this.m_irect.top;
1880
- }
1881
- }
1882
-
1883
- ev.preventDefault();
1884
- ev.stopPropagation();
1885
-
1886
- this.m_target.addClass('sizing');
1887
-
1888
- Component.setCapture(this, (e) => this._pointerMoved(e));
1889
- }
1890
- }
1891
- }
1892
-
1893
- private _pointerMoved(ev: UIEvent) {
1894
-
1895
- let __move = (ex, ey) => {
1896
-
1897
- if (this.m_props.orientation == 'horizontal') {
1898
-
1899
- let width;
1900
- if (this.m_props.sizing == 'after') {
1901
- width = this.m_irect.right - (ex - this.m_delta);
1902
- }
1903
- else {
1904
- width = (ex - this.m_delta) - this.m_irect.left
1905
- }
1906
-
1907
- if (width > 0) {
1908
- let size = new Size(width, 0);
1909
- this.emit('resize', EvSize(size));
1910
-
1911
- this.m_target.setStyleValue('width', size.width);
1912
- this.m_target.setStyleValue('flex', null); // for flex>1
1913
- this.m_target.removeClass('@flex');
1914
- }
1915
- }
1916
- else {
1917
-
1918
- let height;
1919
- if (this.m_props.sizing == 'after') {
1920
- height = this.m_irect.bottom - (ey - this.m_delta);
1921
- }
1922
- else {
1923
- height = (ey - this.m_delta) - this.m_irect.top;
1924
- }
1925
-
1926
- if (height > 0) {
1927
- let size = new Size(0, height);
1928
- this.emit('resize', EvSize(size));
1929
-
1930
- this.m_target.setStyleValue('height', size.height);
1931
- this.m_target.setStyleValue('flex', null); // for flex>1
1932
- this.m_target.removeClass('@flex');
1933
- }
1934
- }
1935
- }
1936
-
1937
- if (ev.type == 'mousemove') {
1938
-
1939
- let mev = ev as MouseEvent;
1940
- __move(mev.pageX, mev.pageY);
1941
-
1942
- ev.preventDefault();
1943
- ev.stopPropagation();
1944
- }
1945
- else if (ev.type == 'touchmove') {
1946
-
1947
- let tev = ev as TouchEvent;
1948
- __move(tev.touches[0].pageX, tev.touches[0].pageY);
1949
-
1950
- ev.preventDefault();
1951
- ev.stopPropagation();
1952
- }
1953
- else if (ev.type == 'mouseup' || ev.type == 'touchend') {
1954
- this.m_target.removeClass('sizing');
1955
-
1956
- Component.releaseCapture();
1957
- ev.preventDefault();
1958
- ev.stopPropagation();
1959
- }
1960
- }
1961
-
1962
- private _findTarget() {
1963
-
1964
- if (!this.m_target) {
1965
-
1966
- if (this.m_props.sizing == 'before') {
1967
- let prevDom = this.dom.previousElementSibling;
1968
- let prevEl = prevDom ? Component.getElement(prevDom as HTMLElement) : null;
1969
- this.m_target = prevEl;
1970
- }
1971
- else {
1972
- let nextDom = this.dom.nextElementSibling;
1973
- let nextEl = nextDom ? Component.getElement(nextDom as HTMLElement) : null;
1974
- this.m_target = nextEl;
1975
- }
1976
- }
1977
-
1978
- if (this.m_target) {
1979
- this.m_irect = this.m_target.getBoundingRect();
1980
- }
1981
- else {
1982
- this.m_irect = null;
1983
- }
1984
- }
1985
- }
1986
-
1987
-
1988
-
1989
-
1990
- // :: SIZERBAR ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1991
-
1992
- /**
1993
- * properties
1994
- */
1995
-
1996
- type SizerOverlaySens = 'left' | 'top' | 'right' | 'bottom' | 'topleft' | 'topright' | 'bottomleft' | 'bottomright';
1997
-
1998
- export interface EvOverlayResize extends BasicEvent {
1999
- ui_event: UIEvent;
2000
- sens: SizerOverlaySens;
2001
- }
2002
-
2003
- export function EvOverlayResize(ui_event: UIEvent, sens: SizerOverlaySens, context: any = null) {
2004
- return BasicEvent<EvOverlayResize>({ ui_event, sens, context });
2005
- }
2006
-
2007
- interface SizerOverlayEventMap extends CEventMap {
2008
- resize: EvSize;
2009
- rawresize: EvOverlayResize;
2010
- }
2011
-
2012
-
2013
- export interface SizerOverlayProps extends CProps<SizerOverlayEventMap> {
2014
- sens: SizerOverlaySens;
2015
- target: Component;
2016
-
2017
- resize?: EventCallback<EvSize>; // shortcut to events: { size: ... }
2018
- }
2019
-
2020
- export class SizerOverlay extends Component<SizerOverlayProps, SizerOverlayEventMap> {
2021
-
2022
- private m_delta: number;
2023
- private m_irect: Rect;
2024
-
2025
- constructor(props: SizerOverlayProps) {
2026
- super(props);
2027
-
2028
- this.addClass(props.sens);
2029
- this.setDomEvent('mousedown', (e) => this._mousedown(e));
2030
- this.setDomEvent('touchstart', (e) => this._mousedown(e));
2031
- this.setDomEvent('dblclick', (e) => this.resetflex(e)); // todo: add option for that
2032
-
2033
- props.target.appendChild(this);
2034
-
2035
- if( props.resize ) {
2036
- this.on( 'resize', this.m_props.resize );
2037
- }
2038
- }
2039
-
2040
- resetflex(event: UIEvent) {
2041
- this.m_props.target.addClass('@flex');
2042
- this.emit('resize', EvSize( { width: -1, height: 0})); // todo: see that
2043
- event.preventDefault();
2044
- event.stopPropagation();
2045
- }
2046
-
2047
- // @review move that in component
2048
-
2049
- _mousedown(ev: UIEvent) {
2050
-
2051
- ev.preventDefault();
2052
- ev.stopPropagation();
2053
-
2054
- let eev = EvOverlayResize(ev, this.m_props.sens);
2055
- this.emit('rawresize', eev);
2056
- if (eev.defaultPrevented) {
2057
- return;
2058
- }
2059
-
2060
- let pos = getMousePos(ev, true);
2061
- this.m_irect = this.m_props.target.getBoundingRect();
2062
-
2063
- if (this.m_props.sens == 'right') {
2064
- this.m_delta = pos.x - this.m_irect.right;
2065
- }
2066
- else if (this.m_props.sens == 'left') {
2067
- this.m_delta = pos.x - this.m_irect.left;
2068
- }
2069
- else if (this.m_props.sens == 'bottom') {
2070
- this.m_delta = pos.y - this.m_irect.bottom;
2071
- }
2072
- else if (this.m_props.sens == 'top') {
2073
- this.m_delta = pos.y - this.m_irect.top;
2074
- }
2075
-
2076
- this.m_props.target.addClass('sizing');
2077
- Component.setCapture(this, (e) => this._handle_mouse(e));
2078
- }
2079
-
2080
- private _is_horz() {
2081
- return this.m_props.sens == 'left' || this.m_props.sens == 'right';
2082
- }
2083
-
2084
- public get sens() {
2085
- return this.m_props.sens;
2086
- }
2087
-
2088
- private _handle_mouse(ev: UIEvent) {
2089
- let __move = (ex, ey) => {
2090
- if (this._is_horz()) {
2091
-
2092
- let width;
2093
- if (this.m_props.sens == 'left') {
2094
- width = this.m_irect.right - (ex - this.m_delta);
2095
- }
2096
- else {
2097
- width = (ex - this.m_delta) - this.m_irect.left
2098
- }
2099
-
2100
- if (width > 0) {
2101
- let size = {
2102
- width,
2103
- height: undefined
2104
- };
2105
-
2106
- this.emit('resize', EvSize(size));
2107
-
2108
- this.m_props.target.setStyleValue('width', size.width);
2109
- this.m_props.target.setStyleValue('flex', null); // for flex>1
2110
- this.m_props.target.removeClass('@flex');
2111
- }
2112
- }
2113
- else {
2114
-
2115
- let height;
2116
- if (this.m_props.sens == 'top') {
2117
- height = this.m_irect.bottom - (ey - this.m_delta);
2118
- }
2119
- else {
2120
- height = (ey - this.m_delta) - this.m_irect.top;
2121
- }
2122
-
2123
- if (height > 0) {
2124
- let size = new Size(0, height);
2125
- this.emit('resize', EvSize(size));
2126
-
2127
- this.m_props.target.setStyleValue('height', size.height);
2128
- this.m_props.target.setStyleValue('flex', null); // for flex>1
2129
- this.m_props.target.removeClass('@flex');
2130
- }
2131
- }
2132
- }
2133
-
2134
- if (ev.type == 'mousemove') {
2135
-
2136
- let mev = ev as MouseEvent;
2137
- __move(mev.pageX, mev.pageY);
2138
-
2139
- ev.preventDefault();
2140
- ev.stopPropagation();
2141
- }
2142
- else if (ev.type == 'touchmove') {
2143
-
2144
- let tev = ev as TouchEvent;
2145
- __move(tev.touches[0].pageX, tev.touches[0].pageY);
2146
-
2147
- ev.preventDefault();
2148
- ev.stopPropagation();
2149
- }
2150
- else if (ev.type == 'mouseup' || ev.type == 'touchend') {
2151
- this.m_props.target.removeClass('sizing');
2152
-
2153
- Component.releaseCapture();
2154
- ev.preventDefault();
2155
- ev.stopPropagation();
2156
- }
2157
- }
2158
- }
2159
-
2160
- /**
2161
- * sequence: Shift+Ctrl+Alt+A
2162
- */
2163
-
2164
- export interface Shortcut {
2165
- sequence: string;
2166
- name: string;
2167
- immediate: boolean;
2168
- callback?: (domTarget) => void;
2169
- }
2170
-
2171
- interface EvShortcut extends BasicEvent {
2172
- name: string;
2173
- }
2174
-
2175
- function EvShortcut(name: string) {
2176
- return BasicEvent<EvShortcut>({ name });
2177
- }
2178
-
2179
- export interface ContainerEventMap extends CEventMap {
2180
- shortcut: EvShortcut;
2181
- }
2182
-
2183
- export interface ContainerProps<E extends ContainerEventMap = ContainerEventMap> extends CProps<E> {
2184
- }
2185
-
2186
- /**
2187
- * you can construct a Container as usual with it's properties but also directly with it's children array
2188
- *
2189
- * @example
2190
- * new Container( [
2191
- * child1,
2192
- * child2
2193
- * ])
2194
- */
2195
-
2196
- export class Container<P extends ContainerProps = ContainerProps, E extends ContainerEventMap = ContainerEventMap> extends Component<P, E> {
2197
-
2198
- private m_shortcuts: Shortcut[];
2199
-
2200
- constructor( props: P | ComponentOrString[] ) {
2201
- if( isArray(props) ) {
2202
- super( {content: props} as P );
2203
- }
2204
- else {
2205
- super( props );
2206
- }
2207
- }
2208
-
2209
- /**
2210
- * add an application shortcut
2211
- * @param sequence key sequence Shift+Ctrl+Alt+K
2212
- * @param callback callback to call
2213
- */
2214
-
2215
- public addShortcut(sequence: string | string[], name: string, callback: EventHandler<KeyboardEvent> = null, immediate = false) {
2216
-
2217
- // first time
2218
- if (!this.m_shortcuts) {
2219
- this.m_shortcuts = [];
2220
- this.setDomEvent('keydown', (e) => this._handleKeydown(e));
2221
- }
2222
-
2223
- if (!isArray(sequence)) {
2224
- sequence = [sequence];
2225
- }
2226
-
2227
- sequence.forEach((seq: string) => {
2228
- let reseq = '';
2229
-
2230
- let shift = seq.match(/SHIFT/i);
2231
- if (shift) {
2232
- seq = seq.replace(/SHIFT/i, '');
2233
- reseq += 'shift+';
2234
- }
2235
-
2236
- let ctrl = seq.match(/CTRL/i);
2237
- if (ctrl) {
2238
- seq = seq.replace(/CTRL/i, '');
2239
- reseq += 'ctrl+';
2240
- }
2241
-
2242
- let cmd = seq.match(/CMD/i);
2243
- if (cmd) {
2244
- seq = seq.replace(/CMD/i, '');
2245
- reseq += 'cmd+';
2246
- }
2247
-
2248
- let alt = seq.match(/ALT/i);
2249
- if (alt) {
2250
- seq = seq.replace(/ALT/i, '');
2251
- reseq += 'alt+';
2252
- }
2253
-
2254
- reseq += seq.replace('+', '').toLowerCase();
2255
-
2256
- this.m_shortcuts.push({
2257
- sequence: reseq,
2258
- name,
2259
- immediate,
2260
- callback
2261
- });
2262
- });
2263
- }
2264
-
2265
- /**
2266
- * remove all shortcuts for a target
2267
- */
2268
-
2269
- removeShortcuts() {
2270
- if (this.m_shortcuts) {
2271
- this.m_shortcuts = [];
2272
- }
2273
- }
2274
-
2275
- /** @ignore this function is binded */
2276
- private _handleKeydown(e: KeyboardEvent) {
2277
-
2278
- if (!this.m_shortcuts) {
2279
- return;
2280
- }
2281
-
2282
- let seq = '';
2283
-
2284
- if (e.shiftKey) {
2285
- seq += 'shift+';
2286
- }
2287
-
2288
- if (e.ctrlKey) {
2289
- seq += 'ctrl+';
2290
- }
2291
-
2292
- if (e.metaKey) {
2293
- seq += 'cmd+';
2294
- }
2295
-
2296
- if (e.altKey) {
2297
- seq += 'alt+';
2298
- }
2299
-
2300
- seq += e.key.toLowerCase();
2301
- //console.log( seq );
2302
-
2303
- this.m_shortcuts.some((sk) => {
2304
- if (sk.sequence == seq) {
2305
-
2306
- if (sk.callback) {
2307
- if (sk.immediate) {
2308
- sk.callback(e);
2309
- }
2310
- else {
2311
- asap(() => { sk.callback(e); });
2312
- }
2313
- }
2314
- else {
2315
- this.emit('shortcut', EvShortcut(sk.name));
2316
- }
2317
-
2318
- e.preventDefault();
2319
- e.stopPropagation();
2320
- return true;
2321
- }
2322
- });
2323
- }
2324
- }
2325
-
2326
- export type ComponentConstructor<T> = new (props: CProps) => T;
2327
-
2328
- /**
2329
- *
2330
- */
2331
-
2332
- export class TSXComponent<P extends CProps<CEventMap> = CProps<CEventMap>, E extends CEventMap = CEventMap> extends Component<P,E> {
2333
- public render(props: P) {
2334
- const tsx = this.renderTSX( props );
2335
- if( tsx ) {
2336
- this.setContent( tsx );
2337
- }
2338
- }
2339
-
2340
- public renderTSX( props: P ): Component | Component[] {
2341
- return undefined;
2342
- }
2343
- }
2344
-