x4js 1.4.2

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