x4js 1.4.53 → 1.5.0

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 (179) hide show
  1. package/lib/cjs/action.d.ts +57 -0
  2. package/lib/cjs/application.d.ts +104 -0
  3. package/lib/cjs/autocomplete.d.ts +58 -0
  4. package/lib/cjs/base_component.d.ts +88 -0
  5. package/lib/cjs/button.d.ts +151 -0
  6. package/lib/cjs/calendar.d.ts +81 -0
  7. package/lib/cjs/canvas.d.ts +92 -0
  8. package/lib/cjs/cardview.d.ts +87 -0
  9. package/lib/cjs/checkbox.d.ts +77 -0
  10. package/lib/cjs/color.d.ts +148 -0
  11. package/lib/cjs/colorpicker.d.ts +107 -0
  12. package/lib/cjs/combobox.d.ts +101 -0
  13. package/lib/cjs/component.d.ts +601 -0
  14. package/lib/cjs/datastore.d.ts +396 -0
  15. package/lib/cjs/dialog.d.ts +175 -0
  16. package/lib/cjs/dom_events.d.ts +302 -0
  17. package/lib/cjs/drag_manager.d.ts +54 -0
  18. package/lib/cjs/drawtext.d.ts +48 -0
  19. package/lib/cjs/fileupload.d.ts +64 -0
  20. package/lib/cjs/form.d.ts +126 -0
  21. package/lib/cjs/formatters.d.ts +35 -0
  22. package/lib/cjs/gridview.d.ts +175 -0
  23. package/lib/cjs/i18n.d.ts +73 -0
  24. package/lib/cjs/icon.d.ts +64 -0
  25. package/lib/cjs/image.d.ts +55 -0
  26. package/lib/cjs/index.d.ts +83 -0
  27. package/lib/cjs/input.d.ts +91 -0
  28. package/lib/cjs/label.d.ts +58 -0
  29. package/lib/cjs/layout.d.ts +87 -0
  30. package/lib/cjs/link.d.ts +50 -0
  31. package/lib/cjs/listview.d.ts +178 -0
  32. package/lib/cjs/md5.d.ts +61 -0
  33. package/lib/cjs/menu.d.ts +130 -0
  34. package/lib/cjs/messagebox.d.ts +68 -0
  35. package/lib/cjs/panel.d.ts +47 -0
  36. package/lib/cjs/popup.d.ts +75 -0
  37. package/lib/cjs/property_editor.d.ts +71 -0
  38. package/lib/cjs/radiobtn.d.ts +72 -0
  39. package/lib/cjs/rating.d.ts +53 -0
  40. package/lib/cjs/request.d.ts +52 -0
  41. package/lib/cjs/router.d.ts +42 -0
  42. package/lib/cjs/settings.d.ts +37 -0
  43. package/lib/cjs/sidebarview.d.ts +49 -0
  44. package/lib/cjs/spreadsheet.d.ts +218 -0
  45. package/lib/cjs/styles.d.ts +85 -0
  46. package/lib/cjs/svgcomponent.d.ts +191 -0
  47. package/lib/cjs/tabbar.d.ts +58 -0
  48. package/lib/cjs/tabview.d.ts +49 -0
  49. package/lib/cjs/textarea.d.ts +63 -0
  50. package/lib/cjs/textedit.d.ts +123 -0
  51. package/lib/cjs/toaster.d.ts +42 -0
  52. package/lib/cjs/tools.d.ts +394 -0
  53. package/lib/cjs/tooltips.d.ts +46 -0
  54. package/lib/cjs/treeview.d.ts +132 -0
  55. package/lib/cjs/version.d.ts +29 -0
  56. package/lib/cjs/x4dom.d.ts +49 -0
  57. package/lib/cjs/x4events.d.ts +269 -0
  58. package/lib/cjs/x4react.d.ts +41 -0
  59. package/lib/esm/action.d.ts +57 -0
  60. package/lib/esm/application.d.ts +104 -0
  61. package/lib/esm/autocomplete.d.ts +58 -0
  62. package/lib/esm/base_component.d.ts +88 -0
  63. package/lib/esm/button.d.ts +151 -0
  64. package/lib/esm/calendar.d.ts +81 -0
  65. package/lib/esm/canvas.d.ts +92 -0
  66. package/lib/esm/cardview.d.ts +87 -0
  67. package/lib/esm/checkbox.d.ts +77 -0
  68. package/lib/esm/color.d.ts +148 -0
  69. package/lib/esm/colorpicker.d.ts +107 -0
  70. package/lib/esm/combobox.d.ts +101 -0
  71. package/lib/esm/component.d.ts +601 -0
  72. package/lib/esm/datastore.d.ts +396 -0
  73. package/lib/esm/dialog.d.ts +175 -0
  74. package/lib/esm/dom_events.d.ts +302 -0
  75. package/lib/esm/drag_manager.d.ts +54 -0
  76. package/lib/esm/drawtext.d.ts +48 -0
  77. package/lib/esm/fileupload.d.ts +64 -0
  78. package/lib/esm/form.d.ts +126 -0
  79. package/lib/esm/formatters.d.ts +35 -0
  80. package/lib/esm/gridview.d.ts +175 -0
  81. package/lib/esm/i18n.d.ts +73 -0
  82. package/lib/esm/icon.d.ts +64 -0
  83. package/lib/esm/image.d.ts +55 -0
  84. package/lib/esm/index.d.ts +83 -0
  85. package/lib/esm/input.d.ts +91 -0
  86. package/lib/esm/label.d.ts +58 -0
  87. package/lib/esm/layout.d.ts +87 -0
  88. package/lib/esm/link.d.ts +50 -0
  89. package/lib/esm/listview.d.ts +178 -0
  90. package/lib/esm/md5.d.ts +61 -0
  91. package/lib/esm/menu.d.ts +130 -0
  92. package/lib/esm/messagebox.d.ts +68 -0
  93. package/lib/esm/panel.d.ts +47 -0
  94. package/lib/esm/popup.d.ts +75 -0
  95. package/lib/esm/property_editor.d.ts +71 -0
  96. package/lib/esm/radiobtn.d.ts +72 -0
  97. package/lib/esm/rating.d.ts +53 -0
  98. package/lib/esm/request.d.ts +52 -0
  99. package/lib/esm/router.d.ts +42 -0
  100. package/lib/esm/settings.d.ts +37 -0
  101. package/lib/esm/sidebarview.d.ts +49 -0
  102. package/lib/esm/spreadsheet.d.ts +218 -0
  103. package/lib/esm/styles.d.ts +85 -0
  104. package/lib/esm/svgcomponent.d.ts +191 -0
  105. package/lib/esm/tabbar.d.ts +58 -0
  106. package/lib/esm/tabview.d.ts +49 -0
  107. package/lib/esm/textarea.d.ts +63 -0
  108. package/lib/esm/textedit.d.ts +123 -0
  109. package/lib/esm/toaster.d.ts +42 -0
  110. package/lib/esm/tools.d.ts +394 -0
  111. package/lib/esm/tooltips.d.ts +46 -0
  112. package/lib/esm/treeview.d.ts +132 -0
  113. package/lib/esm/version.d.ts +29 -0
  114. package/lib/esm/x4dom.d.ts +49 -0
  115. package/lib/esm/x4events.d.ts +269 -0
  116. package/lib/esm/x4react.d.ts +41 -0
  117. package/lib/ts_src/MIT-license.md +14 -0
  118. package/lib/ts_src/action.ts +88 -0
  119. package/lib/ts_src/alpha.jpg +0 -0
  120. package/lib/ts_src/application.ts +251 -0
  121. package/lib/ts_src/autocomplete.ts +197 -0
  122. package/lib/ts_src/base64.ts +166 -0
  123. package/lib/ts_src/base_component.ts +152 -0
  124. package/lib/ts_src/button.ts +355 -0
  125. package/lib/ts_src/calendar.ts +322 -0
  126. package/lib/ts_src/canvas.ts +505 -0
  127. package/lib/ts_src/cardview.ts +227 -0
  128. package/lib/ts_src/checkbox.ts +187 -0
  129. package/lib/ts_src/color.ts +752 -0
  130. package/lib/ts_src/colorpicker.ts +1639 -0
  131. package/lib/ts_src/combobox.ts +362 -0
  132. package/lib/ts_src/component.ts +2329 -0
  133. package/lib/ts_src/datastore.ts +1322 -0
  134. package/lib/ts_src/dialog.ts +656 -0
  135. package/lib/ts_src/dom_events.ts +315 -0
  136. package/lib/ts_src/drag_manager.ts +199 -0
  137. package/lib/ts_src/drawtext.ts +355 -0
  138. package/lib/ts_src/fileupload.ts +213 -0
  139. package/lib/ts_src/form.ts +366 -0
  140. package/lib/ts_src/formatters.ts +105 -0
  141. package/lib/ts_src/gridview.ts +1185 -0
  142. package/lib/ts_src/i18n.ts +346 -0
  143. package/lib/ts_src/icon.ts +329 -0
  144. package/lib/ts_src/image.ts +204 -0
  145. package/lib/ts_src/index.ts +88 -0
  146. package/lib/ts_src/input.ts +249 -0
  147. package/lib/ts_src/label.ts +128 -0
  148. package/lib/ts_src/layout.ts +430 -0
  149. package/lib/ts_src/link.ts +86 -0
  150. package/lib/ts_src/listview.ts +762 -0
  151. package/lib/ts_src/md5.ts +438 -0
  152. package/lib/ts_src/menu.ts +425 -0
  153. package/lib/ts_src/messagebox.ts +203 -0
  154. package/lib/ts_src/panel.ts +86 -0
  155. package/lib/ts_src/popup.ts +494 -0
  156. package/lib/ts_src/property_editor.ts +337 -0
  157. package/lib/ts_src/radiobtn.ts +196 -0
  158. package/lib/ts_src/rating.ts +135 -0
  159. package/lib/ts_src/request.ts +300 -0
  160. package/lib/ts_src/router.ts +185 -0
  161. package/lib/ts_src/settings.ts +77 -0
  162. package/lib/ts_src/sidebarview.ts +103 -0
  163. package/lib/ts_src/spreadsheet.ts +1442 -0
  164. package/lib/ts_src/styles.ts +343 -0
  165. package/lib/ts_src/svgcomponent.ts +537 -0
  166. package/lib/ts_src/tabbar.ts +151 -0
  167. package/lib/ts_src/tabview.ts +110 -0
  168. package/lib/ts_src/textarea.ts +187 -0
  169. package/lib/ts_src/textedit.ts +544 -0
  170. package/lib/ts_src/toaster.ts +80 -0
  171. package/lib/ts_src/tools.ts +1468 -0
  172. package/lib/ts_src/tooltips.ts +191 -0
  173. package/lib/ts_src/treeview.ts +675 -0
  174. package/lib/ts_src/version.ts +30 -0
  175. package/lib/ts_src/x4.less +2236 -0
  176. package/lib/ts_src/x4dom.ts +57 -0
  177. package/lib/ts_src/x4events.ts +585 -0
  178. package/lib/ts_src/x4react.ts +90 -0
  179. package/package.json +3 -2
@@ -0,0 +1,675 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file treeview.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
+ import { Component, Container, ContainerEventMap, ContainerProps, EvDblClick, EventHandler } from './component';
31
+ import { HtmlString, Rect } from './tools';
32
+ import { Icon, IconID } from './icon';
33
+ import { Label } from './label';
34
+ import { HLayout, VLayout } from './layout';
35
+ import { EvClick, BasicEvent, EvDrag, EvSelectionChange, EvContextMenu } from './x4events';
36
+
37
+ export interface EvExpand extends BasicEvent {
38
+ node: TreeNode;
39
+ }
40
+
41
+ function EvExpand(node: TreeNode) {
42
+ return BasicEvent<EvExpand>({ node });
43
+ }
44
+
45
+ export interface HierarchicalNode {
46
+ id: number;
47
+ parent: number;
48
+ name: string;
49
+ leaf: boolean
50
+ }
51
+
52
+ export interface TreeNode {
53
+ id: any;
54
+ text?: string | HtmlString;
55
+ icon?: IconID;
56
+ children?: TreeNode[];
57
+ open?: boolean;
58
+ data?: any;
59
+ parent?: number;
60
+ cls?: string;
61
+ }
62
+
63
+ interface TreeViewEventMap extends ContainerEventMap {
64
+ click: EvClick;
65
+ dblclick: EvDblClick;
66
+ expand: EvExpand;
67
+ drag: EvDrag;
68
+ selectionchange: EvSelectionChange;
69
+ contextMenu: EvContextMenu;
70
+ }
71
+
72
+ export interface TreeViewProps extends ContainerProps<TreeViewEventMap> {
73
+ root: TreeNode; // root node
74
+ indent?: number; // items indentation
75
+ gadgets?: Component[];
76
+ sort?: boolean; // sort items
77
+ canDragItems?: boolean;
78
+ renderItem?: ( itm: TreeNode ) => Component;
79
+
80
+ dblclick?: EventHandler<EvDblClick>;
81
+ selectionchange?: EventHandler<EvSelectionChange>;
82
+ drag?: EventHandler<EvDrag>;
83
+ contextMenu?: EventHandler<EvContextMenu>;
84
+ }
85
+
86
+
87
+ /**
88
+ *
89
+ */
90
+
91
+ export class TreeView extends VLayout<TreeViewProps, TreeViewEventMap> {
92
+
93
+ m_view: Container;
94
+ m_container: Container;
95
+ m_selection: {
96
+ id: any;
97
+ el: Component
98
+ };
99
+
100
+ constructor(props: TreeViewProps) {
101
+ super(props);
102
+
103
+ props.root = props.root;
104
+ props.indent = props.indent ?? 8;
105
+ props.gadgets = props.gadgets;
106
+ props.sort = props.sort ?? false;
107
+
108
+ this.m_selection = null;
109
+
110
+ this.m_container = new Container({ cls: '@scroll-container' });
111
+ this.m_view = new Container({
112
+ cls: '@scroll-view',
113
+ flex: 1,
114
+ content: this.m_container
115
+ });
116
+
117
+ this.setContent([
118
+ this.m_view,
119
+ props.gadgets ? new HLayout({
120
+ cls: 'gadgets',
121
+ content: props.gadgets
122
+ }) : null,
123
+ ])
124
+
125
+ this.setDomEvent( 'click', (e) => this._click(e) );
126
+ this.setDomEvent( 'dblclick', (e) => this._click(e) );
127
+ this.setDomEvent('contextmenu', (e) => this._handleCtxMenu(e));
128
+
129
+ if (props.canDragItems) {
130
+ this.setDomEvent('dragstart', (ev: DragEvent) => {
131
+ let hit = Component.getElement(ev.target as HTMLElement, Component);
132
+ let node = hit?.getData("node");
133
+
134
+ if (node) {
135
+ ev.dataTransfer.effectAllowed = 'move';
136
+ ev.dataTransfer.items.add(JSON.stringify({
137
+ type: 'treeview',
138
+ id: node.id
139
+ }), 'string');
140
+ }
141
+ else {
142
+ ev.preventDefault();
143
+ ev.stopPropagation();
144
+ }
145
+ });
146
+
147
+ this.setDomEvent('dragover', ev => this._dragEnter(ev));
148
+ this.setDomEvent('dragenter', ev => this._dragEnter(ev));
149
+
150
+ this.setDomEvent('dragend', ev => this._dragLeave(ev));
151
+ this.setDomEvent('dragleave', ev => this._dragLeave(ev));
152
+ this.setDomEvent('drop', ev => this._dragLeave(ev));
153
+
154
+ this.setDomEvent('drop', ev => this._drop(ev) );
155
+ }
156
+
157
+ this.mapPropEvents(props, 'dblclick', 'drag', 'selectionchange', 'contextMenu' );
158
+ }
159
+
160
+ private _dragEnter(ev: DragEvent) {
161
+ ev.preventDefault();
162
+
163
+ let hit = Component.getElement(ev.target as HTMLElement, Component);
164
+ let node = hit?.getData("node");
165
+
166
+ if (node) {
167
+ hit.addClass('@drag-over');
168
+ ev.dataTransfer.dropEffect = 'move';
169
+ }
170
+ }
171
+
172
+ private _dragLeave(ev: Event) {
173
+
174
+ let hit = Component.getElement(ev.target as HTMLElement, Component);
175
+ let node = hit?.getData("node");
176
+
177
+ if (node) {
178
+ hit.removeClass('@drag-over');
179
+ }
180
+ }
181
+
182
+ private _drop(ev: DragEvent) {
183
+ let hit = Component.getElement(ev.target as HTMLElement, Component);
184
+ let node = hit?.getData("node");
185
+
186
+ if (!node) {
187
+ node = this.m_props.root;
188
+ }
189
+
190
+ if (node) {
191
+ let parent: TreeNode;
192
+
193
+ // is a folder
194
+ if (node.children) {
195
+ parent = node;
196
+ }
197
+ // in it's parent node
198
+ else {
199
+ parent = hit.getData("parent");
200
+ }
201
+
202
+ for (let i = 0; i < ev.dataTransfer.items.length; i++) {
203
+ ev.dataTransfer.items[0].getAsString((value) => {
204
+ let data = JSON.parse(value);
205
+ this.emit('drag', EvDrag( node, data, parent) );
206
+ });
207
+ }
208
+ }
209
+ }
210
+
211
+ render() {
212
+ this.__update( );
213
+ }
214
+
215
+ private __update( ) {
216
+ if (this.m_props.root) {
217
+ let items = [];
218
+ this._buildBranch(this.m_props.root, -1, items, this.m_props.root);
219
+ this.m_container.setContent(items);
220
+ }
221
+ }
222
+
223
+ updateElement( id: any ) {
224
+ const { node: child, item } = this._getNode( id );
225
+ if( child ) {
226
+ const pn = child.dom.parentNode;
227
+ const newchild = this._makeNode( item, child.dom.classList.value, child.getData('icon'), child.getData('level') );
228
+ const dm = newchild._build( );
229
+ pn.replaceChild( dm, child.dom );
230
+
231
+ if( this.m_selection?.el===child ) {
232
+ this.m_selection.el = newchild;
233
+ }
234
+ }
235
+ }
236
+
237
+ set root(root: TreeNode) {
238
+ this.m_props.root = root;
239
+ this.update();
240
+ }
241
+
242
+ /**
243
+ * same as root = xxx but keep elements open
244
+ */
245
+
246
+ refreshRoot(root: TreeNode) {
247
+
248
+ let openList = [];
249
+
250
+ this.forEach((node: TreeNode): boolean => {
251
+ if (node.open) {
252
+ openList.push(node.id);
253
+ }
254
+ return false;
255
+ });
256
+
257
+ let oldSel = this.selection;
258
+ this.m_props.root = root;
259
+
260
+ this.forEach((node: TreeNode): boolean => {
261
+
262
+ if (openList.indexOf(node.id) >= 0) {
263
+ node.open = true;
264
+ }
265
+
266
+ return false;
267
+ });
268
+
269
+ this.__update( );
270
+ }
271
+
272
+ private _buildBranch(node: TreeNode, level: number, items: Component[], parent: TreeNode) {
273
+
274
+ let cls = '@tree-item';
275
+
276
+ if( node.cls ) {
277
+ cls += ' '+node.cls;
278
+ }
279
+
280
+ if (!node.open && node.children) {
281
+ cls += ' collapsed';
282
+ }
283
+
284
+ if (node.children) {
285
+ cls += ' folder';
286
+ if (node.children.length == 0) {
287
+ cls += ' empty';
288
+ }
289
+ }
290
+
291
+ let icon: IconID = node.icon;
292
+ if (icon === undefined ) {
293
+ if (node.children) {
294
+ if( node.open===true ) {
295
+ icon = 'var(--x4-icon-chevron-down)';
296
+ }
297
+ else {
298
+ icon = 'var(--x4-icon-chevron-right)';
299
+ }
300
+ }
301
+ }
302
+
303
+ if (level >= 0) {
304
+
305
+ const item = this._makeNode( node, cls, icon, level );
306
+
307
+ if (this.m_selection?.id == node.id) {
308
+ this.m_selection.el = item;
309
+ item.addClass('selected');
310
+ }
311
+
312
+ items.push(item);
313
+ }
314
+
315
+ if (level == -1 || node.open) {
316
+ if (node.children) {
317
+
318
+ if (this.m_props.sort) {
319
+ // sort items case insensitive:
320
+ // first folders
321
+ // then items
322
+ node.children = node.children.sort((a, b) => {
323
+ let at = (a.children ? '0' + a.text : a.text)?.toLocaleLowerCase();
324
+ let bt = (b.children ? '0' + b.text : b.text)?.toLocaleLowerCase();
325
+ return at < bt ? -1 : at > bt ? 1 : 0;
326
+ });
327
+ }
328
+
329
+ node.children.forEach((c) => {
330
+ this._buildBranch(c, level + 1, items, node)
331
+ });
332
+ }
333
+ }
334
+ }
335
+
336
+ private _renderDef( node: TreeNode ) {
337
+ return new Label({ cls: 'tree-label', flex:1, text: node.text });
338
+ }
339
+
340
+ private _makeNode( node: TreeNode, cls: string, icon: IconID, level: number ): Component {
341
+
342
+ const item = new HLayout({
343
+ cls,
344
+ content: [
345
+ new Icon({ cls: 'tree-icon', icon }),
346
+ this.m_props.renderItem ? this.m_props.renderItem( node ) : this._renderDef( node ),
347
+ ],
348
+ data: {
349
+ 'node': node,
350
+ 'level': level,
351
+ 'icon': icon,
352
+ },
353
+ style: {
354
+ paddingLeft: 4 + level * this.m_props.indent
355
+ },
356
+ attrs: {
357
+ draggable: this.m_props.canDragItems ? true : undefined
358
+ },
359
+ });
360
+
361
+
362
+ return item;
363
+ }
364
+
365
+ /**
366
+ *
367
+ */
368
+
369
+ forEach(cb: (node) => boolean) {
370
+ let found = null;
371
+
372
+ function scan(node) {
373
+ if (cb(node) == true) {
374
+ return true;
375
+ }
376
+
377
+ if (node.children) {
378
+ for (let i = 0; i < node.children.length; i++) {
379
+ if (scan(node.children[i])) {
380
+ return true;
381
+ }
382
+ }
383
+ }
384
+ }
385
+
386
+ if (this.m_props.root) {
387
+ scan(this.m_props.root);
388
+ }
389
+
390
+ return found;
391
+ }
392
+
393
+ ensureVisible( id: any ) {
394
+ const { node } = this._getNode( id );
395
+ if( node ) {
396
+ node.scrollIntoView( );
397
+ }
398
+ }
399
+
400
+ set selection(id: any) {
401
+
402
+ if (this.m_selection?.el) {
403
+ this.m_selection.el.removeClass('selected');
404
+ }
405
+
406
+ this.m_selection = null;
407
+
408
+ if (id !== undefined) {
409
+ const { node: sel } = this._getNode( id );
410
+ if( sel ) {
411
+ this.m_selection = {
412
+ id: id,
413
+ el: sel
414
+ };
415
+
416
+ sel.addClass('selected');
417
+ sel.scrollIntoView( );
418
+ }
419
+ }
420
+ }
421
+
422
+ private _getNode( id ): {node:Component,item:TreeNode} {
423
+ let found = { node: null, item: null };
424
+
425
+ this.m_container.enumChilds((c) => {
426
+ let node = c.getData('node');
427
+ if (node?.id == id) {
428
+ found = {node:c,item:node};
429
+ return true;
430
+ }
431
+ });
432
+
433
+ return found;
434
+ }
435
+
436
+ get selection() {
437
+ return this.m_selection?.id;
438
+ }
439
+
440
+ getNodeWithId(id: any): TreeNode {
441
+ return this.forEach((node) => node.id == id);
442
+ }
443
+
444
+ /**
445
+ *
446
+ */
447
+
448
+ private _click(ev: MouseEvent) {
449
+
450
+ let dom = ev.target as HTMLElement;
451
+ let idom = dom;
452
+ let onsub = false;
453
+
454
+ // avoid getting click on sub childs
455
+ if( dom.tabIndex!==-1 ) {
456
+ onsub = true;
457
+ }
458
+
459
+ while (dom != this.dom) {
460
+
461
+ let el = Component.getElement(dom);
462
+ let nd = el?.getData('node') as TreeNode;
463
+ if (nd) {
464
+
465
+ if (nd.children && !onsub ) {
466
+ // on text or on expando ?
467
+ if (el.hasClass('selected') || idom.classList.contains('tree-icon') ) { //expando
468
+ nd.open = nd.open ? false : true;
469
+ }
470
+
471
+ this.m_selection = { id: nd.id, el: null };
472
+
473
+ let offset = this.m_view?.dom?.scrollTop;
474
+ this.update( );
475
+ if (offset) {
476
+ this.m_view.dom.scrollTo(0, offset);
477
+ }
478
+
479
+ this.emit('expand', EvExpand(nd));
480
+ }
481
+ else {
482
+ this.selection = nd.id;
483
+
484
+ if( !onsub ) {
485
+ if (ev.type == 'click') {
486
+ this.emit('click', EvClick(nd));
487
+ }
488
+ else {
489
+ this.emit('dblclick', EvDblClick(nd));
490
+ }
491
+ }
492
+ }
493
+
494
+ this.emit('selectionchange', EvSelectionChange(nd));
495
+ return;
496
+ }
497
+
498
+ dom = dom.parentElement;
499
+ }
500
+
501
+ if (ev.type == 'click') {
502
+ this.m_selection = null;
503
+ this.update( );
504
+ this.emit('selectionchange', EvSelectionChange(null) );
505
+ }
506
+ }
507
+
508
+ private _handleCtxMenu(ev: MouseEvent) {
509
+ ev.preventDefault();
510
+
511
+ let dom = ev.target as HTMLElement;
512
+ let idom = dom;
513
+
514
+ while (dom != this.dom) {
515
+
516
+ let el = Component.getElement(dom);
517
+ let nd = el?.getData('node') as TreeNode;
518
+ if (nd) {
519
+ if (nd.children) {
520
+ // on text or on expando ?
521
+ if ( idom.classList.contains('tree-icon') ) { //expando
522
+ return;
523
+ }
524
+ }
525
+
526
+ this.selection = nd.id;
527
+
528
+ this.emit('click', EvClick(nd) );
529
+ this.emit('contextMenu', EvContextMenu(ev, nd));
530
+
531
+ return;
532
+ }
533
+
534
+ dom = dom.parentElement;
535
+ }
536
+
537
+ this.selection = null;
538
+ this.emit('contextMenu', EvContextMenu(ev, null));
539
+ }
540
+
541
+ /**
542
+ * constructs a tree node from an array of strings
543
+ * elements are organized like folders (separator = /)
544
+ * @example
545
+ * let root = TreeView.buildFromString( [
546
+ * 'this/is/a/final/file'
547
+ * 'this/is/another/file'
548
+ * ] );
549
+ */
550
+
551
+ static buildFromStrings(paths: string[], separator = '/'): TreeNode {
552
+
553
+ let root: TreeNode = {
554
+ id: 0,
555
+ text: '<root>',
556
+ children: []
557
+ };
558
+
559
+ function insert(elements: TreeNode[], path: string) {
560
+
561
+ let pos = path.indexOf(separator);
562
+ let main = path.substr(0, pos < 0 ? undefined : pos);
563
+
564
+ let elem: TreeNode;
565
+
566
+ if (pos >= 0) {
567
+ elem = elements.find((el) => {
568
+ return el.text == main;
569
+ });
570
+ }
571
+
572
+ if (!elem) {
573
+ elem = {
574
+ id: path,
575
+ text: main,
576
+ };
577
+
578
+ elements.push(elem);
579
+ }
580
+
581
+ if (pos >= 0) {
582
+ if (!elem.children) {
583
+ elem.children = [];
584
+ }
585
+
586
+ insert(elem.children, path.substr(pos + separator.length));
587
+ }
588
+ }
589
+
590
+ paths.forEach((path) => {
591
+ insert(root.children, path);
592
+ });
593
+
594
+ return root;
595
+ }
596
+
597
+ /**
598
+ * constructs a tree node from an array of nodes like
599
+ * node {
600
+ * id: number,
601
+ * parent: number,
602
+ * name: string
603
+ * }
604
+ */
605
+
606
+ static buildFromHierarchy(nodes: HierarchicalNode[], cb?: (node: TreeNode) => void): TreeNode {
607
+
608
+ let root: TreeNode = {
609
+ id: 0,
610
+ text: '<root>',
611
+ children: []
612
+ };
613
+
614
+ let tree_nodes: TreeNode[] = [];
615
+
616
+ function insert(node: HierarchicalNode) {
617
+
618
+ let elem: TreeNode;
619
+ let pelem: TreeNode;
620
+
621
+ if (node.parent > 0) {
622
+ pelem = tree_nodes.find((tnode: TreeNode) => tnode.id == node.parent);
623
+
624
+ if (!pelem) {
625
+ pelem = {
626
+ id: node.parent,
627
+ text: '',
628
+ children: []
629
+ };
630
+
631
+ tree_nodes.push(pelem);
632
+ }
633
+
634
+ if (!pelem.children) {
635
+ pelem.children = [];
636
+ }
637
+ }
638
+ else {
639
+ pelem = root;
640
+ }
641
+
642
+ elem = tree_nodes.find((tnode: TreeNode) => tnode.id == node.id);
643
+ if (!elem) {
644
+ elem = {
645
+ id: node.id,
646
+ text: node.name,
647
+ parent: node.parent,
648
+ };
649
+
650
+ if (!node.leaf) {
651
+ elem.children = [];
652
+ }
653
+ else {
654
+ elem.icon = null;
655
+ }
656
+ }
657
+ else {
658
+ elem.text = node.name;
659
+ elem.parent = node.parent;
660
+ }
661
+
662
+ tree_nodes.push(elem);
663
+ pelem.children.push(elem)
664
+ }
665
+
666
+ nodes.forEach(insert);
667
+ if (cb) {
668
+ tree_nodes.forEach(cb);
669
+ }
670
+
671
+ return root;
672
+ }
673
+ }
674
+
675
+
@@ -0,0 +1,30 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file version.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
+ export const x4js_version = "1.4";