ozdenui 0.1.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 (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,1179 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ TreeView - v26.10
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - A tree of nested items: a file explorer, a category tree, the menu of an admin panel, the layers of a design tool.
9
+ - One row is one Label (an arrow, an optional icon, the text, a tag and a soft description in it), so a tree of
10
+ thousands of rows stays light.
11
+ - Only the changed branch is drawn again: the children of a closed item are created the first time it opens,
12
+ closing only hides them. updateNode() paints one row, setChildren() draws one branch.
13
+ - Lazy children: hasChildren: 1 and no children -> onLoadChildren(self, node) is called when it opens,
14
+ then the code gives them with self.setChildren(node.key, [...]).
15
+ - Keyboard (the tree takes the focus): ArrowUp / ArrowDown move the selection, ArrowRight opens (or goes to the
16
+ first child), ArrowLeft closes (or goes to the parent), Home, End, PageUp, PageDown, Enter -> onActivate,
17
+ Space opens / closes, typing a letter jumps to the next row that starts with it (typeAhead).
18
+ - Mouse: click selects, a click on the arrow opens / closes, a double click -> onActivate (and opens / closes;
19
+ toggleOnDoubleClick: 0 -> it only activates). onActivate(self, node, event): event.type "dblclick" or "keydown".
20
+ - getRowElem(key): the element of a row (its text: [data-part=text]), Ex: to put a field over it.
21
+ - Accessibility: tree / treeitem / group roles, aria-level, aria-expanded, aria-selected, aria-activedescendant.
22
+ - onSelect is NOT called when the component is created or when the value is set with silent: 1.
23
+ - Style packages: "light" (default; the old name "classic" works too), "modern", "dark", "minimal", "minimalDark". Select with styleName. (TreeView.styles)
24
+ - Everything is drawn with code (no image files needed).
25
+
26
+ NODE: { key, text, iconFile, tag, tagColor, description, descriptionColor, textColor, children, expanded, hasChildren, enabled, data }
27
+ - text: Plain text (never read as HTML).
28
+ - tag: A short second text after the text, in the accent color (Ex: the type of a layer). tagColor: its own color.
29
+ - description: A soft text at the end (Ex: a size, a count, a preview). descriptionColor: its own color. style.note is
30
+ its look. enabled: 0 -> it can not be selected or opened.
31
+ - iconFile: An image (a file or a data:image/svg+xml URI).
32
+ - children: [NODE]. expanded: 1 -> open. hasChildren: 1 -> an arrow without children (lazy, see onLoadChildren).
33
+ - data: Anything of yours. It is kept by reference.
34
+ - A string is also accepted: "Docs" -> { key: "Docs", text: "Docs" }. A node without a key gets one.
35
+
36
+ USAGE:
37
+ const tree = TreeView({
38
+ width: 260,
39
+ height: 400,
40
+ items: [
41
+ { key: "src", text: "src", expanded: 1, children: [
42
+ { key: "src/app.js", text: "app.js", description: "4 KB" },
43
+ { key: "src/lib", text: "lib", children: ["a.js", "b.js"] },
44
+ ] },
45
+ { key: "readme", text: "readme.md" },
46
+ ],
47
+ value: "src/app.js",
48
+ onSelect: function (self, node) { println(node.key); },
49
+ onActivate: function (self, node) { println("open " + node.key); },
50
+ });
51
+ tree.setValue("readme"); // onSelect is called
52
+ tree.updateNode("readme", { description: "changed" }); // paints one row
53
+ tree.setChildren("src/lib", ["c.js"]); // draws one branch
54
+ tree.addNode("src", { key: "src/new.js", text: "new.js" });
55
+ tree.removeNode("readme");
56
+ tree.reveal("src/lib"); // opens the parents and scrolls to it
57
+
58
+ Started Date: October 2026
59
+ Webpage: https://bug7a.github.io/js-components/
60
+
61
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
62
+ Licensed under the Apache License, Version 2.0
63
+ SPDX-License-Identifier: Apache-2.0
64
+
65
+ */
66
+
67
+ "use strict";
68
+
69
+ // Default values:
70
+ const TreeViewDefaults = {
71
+ key: "0",
72
+ width: 260,
73
+ height: 320, // "auto": no scrolling, the tree grows with its rows.
74
+ items: [], // [NODE] (See NODE above)
75
+ value: null, // Key of the selected node. null: nothing is selected.
76
+ emptyText: "No items",
77
+ typeAhead: 1, // 1: Typing a letter jumps to the next row that starts with it.
78
+ useScrollBar: 1, // basic/scroll-bar.js (when it is loaded). 0: the scrollbar of the browser.
79
+ enabled: 1,
80
+ ariaLabel: "Tree",
81
+ onSelect: function (self, node) { }, // self.value, self.previousValue
82
+ onActivate: function (self, node, event) { }, // Double click or Enter (event.type: "dblclick" or "keydown")
83
+ toggleOnDoubleClick: 1, // 0: a double click does not open / close the row (Ex: it renames it)
84
+ onToggle: function (self, node) { }, // node.expanded: 1 or 0
85
+ onHover: function (self, node) { }, // node: null when the mouse leaves the rows
86
+ onLoadChildren: function (self, node) { }, // hasChildren: 1 and no children. Call self.setChildren(node.key, [...]).
87
+ styleName: "light", // "light" ("classic": its old name), "modern", "dark", "minimal", "minimalDark" or a name added to TreeView.styles
88
+ style: { // Light style package (default)
89
+ box: {
90
+ color: "transparent",
91
+ border: 0,
92
+ borderColor: "transparent",
93
+ round: 0,
94
+ padding: [4, 4], // [x, y] around the rows
95
+ },
96
+ row: {
97
+ height: 28,
98
+ fontSize: 13,
99
+ textColor: Black(0.8),
100
+ indent: 16, // Per level
101
+ paddingLeft: 4,
102
+ paddingRight: 8,
103
+ gap: 6, // Between the arrow, the icon, the text, the tag and the description
104
+ round: 5,
105
+ border: 0, // px: an edge around every row (its color: borderColor, rowHover / rowSelected.borderColor). 0: none
106
+ borderColor: "transparent",
107
+ space: 0, // px between two rows (Ex: 2: the edges of two rows do not meet)
108
+ },
109
+ rowHover: {
110
+ color: Black(0.05),
111
+ borderColor: "", // the edge of the row under the mouse (row.border). "": its own
112
+ },
113
+ rowSelected: {
114
+ color: Black(0.09),
115
+ textColor: "#141414",
116
+ borderColor: "", // the edge of the selected row (row.border). "": its own
117
+ },
118
+ rowFocus: { // Ring of the selected row while the tree has the focus of the keyboard (not after a click)
119
+ outlineColor: Black(0.25),
120
+ },
121
+ arrow: {
122
+ color: Black(0.45),
123
+ size: 12,
124
+ motion: 0.15, // Seconds. 0: No animation
125
+ },
126
+ icon: {
127
+ size: 16,
128
+ },
129
+ tag: {
130
+ textColor: "#3A6EA5",
131
+ fontSize: 12,
132
+ },
133
+ note: {
134
+ textColor: Black(0.4),
135
+ fontSize: 12,
136
+ },
137
+ disabled: {
138
+ opacity: 0.4,
139
+ },
140
+ empty: {
141
+ textColor: Black(0.4),
142
+ fontSize: 13,
143
+ },
144
+ scrollBar: { bar_color: "#9A9A98", bar_mouseOverColor: "#6A6A68", bar_width: 4, bar_round: 3, bar_opacity: 0.4, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
145
+ }
146
+ };
147
+
148
+ const TreeView = function (params = {}) {
149
+
150
+ // Merge style package: params.style > TreeView.styles[styleName] > TreeViewDefaults.style (light)
151
+ basic.applyStylePackage(params, TreeView.styles, TreeViewDefaults);
152
+
153
+ // Merge params:
154
+ mergeIntoIfMissing(params, TreeViewDefaults);
155
+
156
+ // Edit params, if needed:
157
+ params.color = params.style.box.color;
158
+ const startItems = params.items;
159
+ const startValue = params.value;
160
+ params.items = [];
161
+ params.value = null;
162
+ const isAutoHeight = (String(params.height) === "auto"); // WHY: Read before startObject: box.height gives the measured height (0), never "auto".
163
+
164
+ // BOX: Component container
165
+ let box = startObject(params);
166
+
167
+ // *** PRIVATE VARIABLES:
168
+ const _s = box.style;
169
+ const id = "tree-view-" + (TreeView._counter++);
170
+ let roots = []; // [rec]
171
+ let recs = new Map(); // key -> rec { node, parent, level, row, group, built } (only the rows that were created)
172
+ let nodeIndex = new Map(); // key -> { node, parent } (every node, also inside closed branches)
173
+ let hoverRec = null;
174
+ let hasFocus = 0;
175
+ let isKeyFocus = 0; // 1: the focus came with the keyboard (Tab) or a key was used in the tree; 0: a click
176
+ let typeBuffer = "";
177
+ let cancelTypeTimer = null; // (box.after: the timer is stopped by remove())
178
+ let keyCounter = 1;
179
+ const TYPE_AHEAD_RESET_MS = 600; // The typed letters are forgotten after this pause.
180
+ const SCROLL_SPACE = 4; // Space above / below a row that is scrolled into view.
181
+ const AUTO_HEIGHT_PAGE_SIZE = 10; // PageUp / PageDown step when the tree does not scroll.
182
+ const ARROW_BOX_EXTRA = 4; // The box of the arrow is this much wider than the arrow.
183
+ const TEXT_SPACE_WITHOUT_ICON = 2; // Space between the arrow and the text when there is no icon.
184
+ const EMPTY_TEXT_TOP = 16;
185
+
186
+ // *** PUBLIC VARIABLES:
187
+ box.previousValue = null;
188
+ // NOTE: Default values are also public variables. (box.value, box.enabled)
189
+
190
+ // *** PRIVATE FUNCTIONS:
191
+
192
+ const escapeText = function (text) {
193
+ return basic.escapeHtml(String((text === undefined || text === null) ? "" : text));
194
+ };
195
+
196
+ const isFilled = function (value) {
197
+ return value !== undefined && value !== null && value !== "";
198
+ };
199
+
200
+ // enabled: 0 -> the node can not be selected or opened (a node without it is enabled)
201
+ const isNodeDisabled = function (node) {
202
+ return node.enabled !== undefined && !basic.isOn(node.enabled);
203
+ };
204
+
205
+ // A <span> with inline styles. styles: ["flex:0 0 auto", "color:red"]
206
+ const span = function (styles, content, attributes = "") {
207
+ return "<span" + attributes + ' style="' + styles.join(";") + '">' + content + "</span>";
208
+ };
209
+
210
+ // NODE -> a copy of the node with a key (children are copied too). data is kept by reference.
211
+ const normalize = function (node) {
212
+ if (typeof node === "string" || typeof node === "number") {
213
+ node = { key: String(node), text: String(node) };
214
+ }
215
+ const out = Object.assign({}, node);
216
+ if (!isFilled(out.key)) out.key = "node-" + (keyCounter++);
217
+ out.key = String(out.key);
218
+ if (out.text === undefined) out.text = out.key;
219
+ out.expanded = (out.expanded == 1) ? 1 : 0;
220
+ out.children = Array.isArray(node.children) ? node.children.map(normalize) : null;
221
+ return out;
222
+ };
223
+
224
+ // Has an arrow: children, or lazy children that are not loaded yet.
225
+ const canExpand = function (rec) {
226
+ const node = rec.node;
227
+ if (node.children && node.children.length > 0) return true;
228
+ return node.hasChildren == 1 && !node.children;
229
+ };
230
+
231
+ const isOpen = function (rec) {
232
+ return rec.node.expanded == 1 && canExpand(rec);
233
+ };
234
+
235
+ const getRowId = function (rec) {
236
+ return id + "-" + TreeView.toId(rec.node.key);
237
+ };
238
+
239
+ const getArrowHtml = function (rec) {
240
+ const arrow = _s.arrow;
241
+ const styles = [
242
+ "display:inline-flex",
243
+ "align-items:center",
244
+ "justify-content:center",
245
+ "flex:0 0 " + (arrow.size + ARROW_BOX_EXTRA) + "px",
246
+ "height:100%",
247
+ "transform:rotate(" + (isOpen(rec) ? 90 : 0) + "deg)",
248
+ ];
249
+ if (arrow.motion) styles.push("transition:transform " + arrow.motion + "s");
250
+ const svg = (canExpand(rec)) ? TreeView.getChevronSvg(arrow.color, arrow.size) : "";
251
+ return span(styles, svg, ' data-tv-arrow="1"');
252
+ };
253
+
254
+ const getIconHtml = function (node) {
255
+ const size = _s.icon.size + "px";
256
+ const styles = [
257
+ "flex:0 0 " + size,
258
+ "width:" + size,
259
+ "height:" + size,
260
+ "margin-left:" + (_s.row.gap - 4) + "px",
261
+ "object-fit:contain",
262
+ ];
263
+ return '<img src="' + escapeText(node.iconFile) + '" alt="" style="' + styles.join(";") + '">';
264
+ };
265
+
266
+ const getTextHtml = function (node) {
267
+ const styles = [
268
+ "margin-left:" + ((node.iconFile) ? _s.row.gap : TEXT_SPACE_WITHOUT_ICON) + "px",
269
+ "flex:0 1 auto",
270
+ "min-width:0",
271
+ "overflow:hidden",
272
+ "text-overflow:ellipsis",
273
+ ];
274
+ if (node.textColor) styles.push("color:" + escapeText(node.textColor));
275
+ return span(styles, escapeText(node.text), ' data-part="text"');
276
+ };
277
+
278
+ const getTagHtml = function (node) {
279
+ const styles = [
280
+ "margin-left:" + _s.row.gap + "px",
281
+ "flex:0 0 auto",
282
+ "color:" + escapeText(node.tagColor || _s.tag.textColor),
283
+ "font-size:" + _s.tag.fontSize + "px",
284
+ ];
285
+ return span(styles, escapeText(node.tag));
286
+ };
287
+
288
+ const getDescriptionHtml = function (node) {
289
+ const styles = [
290
+ "margin-left:" + _s.row.gap + "px",
291
+ "flex:0 1 auto",
292
+ "min-width:0",
293
+ "overflow:hidden",
294
+ "text-overflow:ellipsis",
295
+ "color:" + escapeText(node.descriptionColor || _s.note.textColor),
296
+ "font-size:" + _s.note.fontSize + "px",
297
+ ];
298
+ return span(styles, escapeText(node.description));
299
+ };
300
+
301
+ // The inside of a row: arrow, icon, text, tag, description.
302
+ // WHY: One Label with HTML, not one object for each part: a tree of thousands of rows stays light.
303
+ const getRowHtml = function (rec) {
304
+ const node = rec.node;
305
+ let html = getArrowHtml(rec);
306
+ if (node.iconFile) html += getIconHtml(node);
307
+ html += getTextHtml(node);
308
+ if (isFilled(node.tag)) html += getTagHtml(node);
309
+ if (isFilled(node.description)) html += getDescriptionHtml(node);
310
+ return html;
311
+ };
312
+
313
+ const getRowEdge = function (isSelected, isHover) {
314
+ if (isSelected && _s.rowSelected.borderColor) return _s.rowSelected.borderColor;
315
+ if (isHover && _s.rowHover.borderColor) return _s.rowHover.borderColor;
316
+ return _s.row.borderColor;
317
+ };
318
+
319
+ const getRowColor = function (isSelected, isHover) {
320
+ if (isSelected) return _s.rowSelected.color;
321
+ if (isHover) return _s.rowHover.color;
322
+ return "transparent";
323
+ };
324
+
325
+ // Colors and aria of a row. (The text is not drawn again.)
326
+ const paintRow = function (rec) {
327
+
328
+ if (!rec.row) return;
329
+
330
+ const node = rec.node;
331
+ const row = rec.row;
332
+ const isSelected = (node.key === box.value);
333
+ const isHover = (rec === hoverRec) && !isSelected && !isNodeDisabled(node);
334
+
335
+ row.color = getRowColor(isSelected, isHover);
336
+ if (_s.row.border) row.borderColor = getRowEdge(isSelected, isHover);
337
+ row.textColor = (isSelected) ? _s.rowSelected.textColor : _s.row.textColor;
338
+ row.opacity = (isNodeDisabled(node)) ? _s.disabled.opacity : 1;
339
+ row.boxShadow = (isSelected && hasFocus && isKeyFocus) ? "inset 0 0 0 1px " + _s.rowFocus.outlineColor : "none";
340
+
341
+ row.aria = {
342
+ selected: isSelected,
343
+ disabled: (isNodeDisabled(node)) ? true : false,
344
+ expanded: (canExpand(rec)) ? isOpen(rec) : null, // null: a leaf has no aria-expanded
345
+ };
346
+
347
+ };
348
+
349
+ // Text, arrow and colors of a row.
350
+ const drawRow = function (rec) {
351
+ if (!rec.row) return;
352
+ rec.row.text = getRowHtml(rec);
353
+ paintRow(rec);
354
+ };
355
+
356
+ // Creates the row of a node in the current container (and its open children).
357
+ const buildRec = function (node, parent, level) {
358
+
359
+ const rec = { node: node, parent: parent, level: level, row: null, group: null, built: 0 };
360
+ recs.set(node.key, rec);
361
+
362
+ const rowStyle = _s.row;
363
+
364
+ // LABEL: Row
365
+ rec.row = Label({
366
+ width: "100%",
367
+ height: rowStyle.height,
368
+ text: "",
369
+ fontSize: rowStyle.fontSize,
370
+ textColor: rowStyle.textColor,
371
+ color: "transparent",
372
+ round: rowStyle.round,
373
+ shrink: 0,
374
+ cursor: "default",
375
+ css: {
376
+ position: "relative",
377
+ display: "flex",
378
+ alignItems: "center",
379
+ whiteSpace: "nowrap",
380
+ overflow: "hidden",
381
+ boxSizing: "border-box",
382
+ paddingLeft: (rowStyle.paddingLeft + level * rowStyle.indent) + "px",
383
+ paddingRight: rowStyle.paddingRight + "px",
384
+ },
385
+ role: "treeitem",
386
+ aria: { level: level + 1 },
387
+ });
388
+ if (rowStyle.border) rec.row.props({ border: rowStyle.border, borderColor: rowStyle.borderColor });
389
+ rec.row.elem.id = getRowId(rec);
390
+ rec.row.elem._treeRec = rec; // WHY: One click listener for all the rows finds the rec of a row with it.
391
+ rec.row.clickable = 1;
392
+ drawRow(rec);
393
+
394
+ if (isOpen(rec)) buildChildren(rec);
395
+
396
+ return rec;
397
+
398
+ };
399
+
400
+ // Creates the group of the children right after the row (the first time the node opens).
401
+ const buildChildren = function (rec) {
402
+
403
+ if (rec.built || !rec.node.children) return;
404
+ rec.built = 1;
405
+
406
+ // GROUP: Children
407
+ createIn(rec.row.containerBox, function () {
408
+ rec.group = VGroup({
409
+ width: "100%",
410
+ height: "auto",
411
+ align: "left top",
412
+ gap: _s.row.space,
413
+ color: "transparent",
414
+ position: "relative",
415
+ css: { alignItems: "stretch" },
416
+ role: "group",
417
+ });
418
+ rec.children = rec.node.children.map(function (child) {
419
+ return buildRec(child, rec, rec.level + 1);
420
+ });
421
+ endGroup();
422
+ });
423
+
424
+ // WHY: createIn adds it at the end of the container; it must be right under its row.
425
+ rec.row.elem.after(rec.group.elem);
426
+
427
+ };
428
+
429
+ // Removes a rec and its children from the map (their objects are removed with their group).
430
+ const forget = function (rec) {
431
+ if (rec === hoverRec) hoverRec = null;
432
+ recs.delete(rec.node.key);
433
+ (rec.children || []).forEach(forget);
434
+ };
435
+
436
+ const indexNodes = function (list, parentNode) {
437
+ (list || []).forEach(function (node) {
438
+ nodeIndex.set(node.key, { node: node, parent: parentNode });
439
+ if (node.children) indexNodes(node.children, node);
440
+ });
441
+ };
442
+
443
+ const unindexNode = function (node) {
444
+ nodeIndex.delete(node.key);
445
+ (node.children || []).forEach(unindexNode);
446
+ };
447
+
448
+ // The rec of a key: the closed parents are opened (their rows are created) first.
449
+ const ensureRec = function (key) {
450
+ key = String(key);
451
+ // WHY no early return for a made row: its parent may have been closed since (the row is there but hidden)
452
+ if (!nodeIndex.get(key)) return recs.get(key) || null;
453
+ const path = box.getPath(key);
454
+ for (let i = 0; i < path.length - 1; i++) {
455
+ const rec = recs.get(path[i].key);
456
+ if (!rec) return null;
457
+ setExpanded(rec, 1);
458
+ }
459
+ return recs.get(key) || null;
460
+ };
461
+
462
+ // The selection was removed (it is not in the tree any more).
463
+ const dropLostValue = function () {
464
+ if (box.value === null || nodeIndex.get(box.value)) return;
465
+ box.previousValue = box.value;
466
+ box.value = null;
467
+ box.listArea.aria = { activedescendant: null };
468
+ };
469
+
470
+ const removeRecView = function (rec) {
471
+ if (rec.group) rec.group.remove();
472
+ if (rec.row) rec.row.remove();
473
+ rec.group = null;
474
+ rec.row = null;
475
+ };
476
+
477
+ // Draws the children of a rec again (only that branch).
478
+ const rebuildChildren = function (rec) {
479
+ (rec.children || []).forEach(forget);
480
+ if (rec.group) rec.group.remove();
481
+ rec.group = null;
482
+ rec.children = [];
483
+ rec.built = 0;
484
+ if (isOpen(rec)) buildChildren(rec);
485
+ drawRow(rec);
486
+ };
487
+
488
+ const updateEmpty = function () {
489
+ box.lblEmpty.visible = (roots.length) ? 0 : 1;
490
+ };
491
+
492
+ const refreshScroll = function () {
493
+ if (box && box.scrollBar && box.scrollBar.refreshScroll) box.scrollBar.refreshScroll();
494
+ };
495
+
496
+ // The rows that can be seen (the parents are open), in order.
497
+ const getVisibleRecs = function () {
498
+ const out = [];
499
+ const walk = function (list) {
500
+ list.forEach(function (rec) {
501
+ out.push(rec);
502
+ if (isOpen(rec) && rec.children) walk(rec.children);
503
+ });
504
+ };
505
+ walk(roots);
506
+ return out;
507
+ };
508
+
509
+ const getEnabledVisibleRecs = function () {
510
+ return getVisibleRecs().filter(function (rec) { return !isNodeDisabled(rec.node); });
511
+ };
512
+
513
+ const scrollRowIntoView = function (rec) {
514
+ if (!rec || !rec.row || isAutoHeight) return;
515
+ const area = box.listArea.elem;
516
+ const areaBox = area.getBoundingClientRect();
517
+ const rowBox = rec.row.elem.getBoundingClientRect();
518
+ if (rowBox.top < areaBox.top) area.scrollTop -= (areaBox.top - rowBox.top) + SCROLL_SPACE;
519
+ else if (rowBox.bottom > areaBox.bottom) area.scrollTop += (rowBox.bottom - areaBox.bottom) + SCROLL_SPACE;
520
+ };
521
+
522
+ const setHover = function (rec) {
523
+ if (rec === hoverRec) return;
524
+ const oldRec = hoverRec;
525
+ hoverRec = rec;
526
+ if (oldRec) paintRow(oldRec);
527
+ if (rec) paintRow(rec);
528
+ box.onHover(box, (rec) ? rec.node : null);
529
+ };
530
+
531
+ const openRec = function (rec) {
532
+ if (rec.node.children && rec.built) {
533
+ rec.group.visible = 1;
534
+ return;
535
+ }
536
+ if (rec.node.children) {
537
+ buildChildren(rec);
538
+ return;
539
+ }
540
+ // Lazy: the code gives the children with setChildren().
541
+ drawRow(rec);
542
+ box.onLoadChildren(box, rec.node);
543
+ };
544
+
545
+ const closeRec = function (rec, fromUser) {
546
+ if (rec.group) rec.group.visible = 0;
547
+ // WHY: The selection must stay where it can be seen (keyboard).
548
+ const selected = recs.get(box.value);
549
+ if (fromUser && selected && isInside(selected, rec)) selectRec(rec, 1);
550
+ };
551
+
552
+ const setExpanded = function (rec, expanded, fromUser) {
553
+ if (!rec || !canExpand(rec)) return;
554
+ expanded = (expanded) ? 1 : 0;
555
+ if (rec.node.expanded == expanded) return;
556
+ rec.node.expanded = expanded;
557
+
558
+ if (expanded) openRec(rec);
559
+ else closeRec(rec, fromUser);
560
+ if (!box) return; // WHY: onLoadChildren can remove the tree.
561
+
562
+ drawRow(rec);
563
+ refreshScroll();
564
+ box.onToggle(box, rec.node);
565
+ };
566
+
567
+ const isInside = function (rec, parent) {
568
+ for (let p = rec.parent; p; p = p.parent) {
569
+ if (p === parent) return true;
570
+ }
571
+ return false;
572
+ };
573
+
574
+ // fromUser: onSelect is called. (Not for silent.)
575
+ const selectRec = function (rec, fromUser) {
576
+ if (!rec || isNodeDisabled(rec.node)) return;
577
+ if (rec.node.key === box.value) return;
578
+ const oldRec = recs.get(box.value);
579
+ box.previousValue = box.value;
580
+ box.value = rec.node.key;
581
+ if (oldRec) paintRow(oldRec);
582
+ paintRow(rec);
583
+ box.listArea.aria = { activedescendant: getRowId(rec) };
584
+ scrollRowIntoView(rec);
585
+ if (fromUser) box.onSelect(box, rec.node);
586
+ };
587
+
588
+ const activateRec = function (rec, event) {
589
+ if (!rec || isNodeDisabled(rec.node)) return;
590
+ box.onActivate(box, rec.node, event);
591
+ };
592
+
593
+ // step: +1 down, -1 up; Infinity / -Infinity: the last / the first row.
594
+ const moveSelection = function (step) {
595
+ const list = getEnabledVisibleRecs();
596
+ if (!list.length) return;
597
+ let position = list.indexOf(recs.get(box.value));
598
+ if (position === -1) position = (step > 0) ? -1 : list.length;
599
+ position = basic.clamp(position + step, 0, list.length - 1);
600
+ selectRec(list[position], 1);
601
+ };
602
+
603
+ const getPageSize = function () {
604
+ if (isAutoHeight) return AUTO_HEIGHT_PAGE_SIZE;
605
+ return Math.max(1, Math.floor(box.listArea.elem.clientHeight / _s.row.height) - 1);
606
+ };
607
+
608
+ // ArrowRight: opens a closed node, or goes to the first child of an open one.
609
+ const goRight = function (current) {
610
+ if (!current) {
611
+ moveSelection(1);
612
+ return;
613
+ }
614
+ if (canExpand(current) && !isOpen(current)) {
615
+ setExpanded(current, 1, 1);
616
+ return;
617
+ }
618
+ if (!isOpen(current) || !current.children) return;
619
+ const firstChild = current.children.find(function (child) { return !isNodeDisabled(child.node); });
620
+ selectRec(firstChild, 1);
621
+ };
622
+
623
+ // ArrowLeft: closes an open node, or goes to the parent.
624
+ const goLeft = function (current) {
625
+ if (!current) return;
626
+ if (isOpen(current)) setExpanded(current, 0, 1);
627
+ else if (current.parent) selectRec(current.parent, 1);
628
+ };
629
+
630
+ const isTypeAheadKey = function (event) {
631
+ if (box.typeAhead != 1 || event.key.length !== 1) return false;
632
+ return !event.ctrlKey && !event.metaKey && !event.altKey;
633
+ };
634
+
635
+ // Returns 1 when the key was used by the tree.
636
+ const handleKey = function (event) {
637
+
638
+ const current = recs.get(box.value);
639
+
640
+ switch (event.key) {
641
+ case "ArrowDown": moveSelection(1); return 1;
642
+ case "ArrowUp": moveSelection(-1); return 1;
643
+ case "PageDown": moveSelection(getPageSize()); return 1;
644
+ case "PageUp": moveSelection(-getPageSize()); return 1;
645
+ case "Home": moveSelection(-Infinity); return 1;
646
+ case "End": moveSelection(Infinity); return 1;
647
+ case "ArrowRight": goRight(current); return 1;
648
+ case "ArrowLeft": goLeft(current); return 1;
649
+ case "Enter": activateRec(current, event); return 1;
650
+ case " ":
651
+ if (current && canExpand(current)) setExpanded(current, !isOpen(current), 1);
652
+ return 1;
653
+ }
654
+
655
+ if (!isTypeAheadKey(event)) return 0;
656
+ typeAhead(event.key);
657
+ return 1;
658
+
659
+ };
660
+
661
+ const onKeyDown = function (event) {
662
+ if (box.enabled != 1) return;
663
+ if (!handleKey(event)) return;
664
+ event.preventDefault();
665
+ event.stopPropagation();
666
+ };
667
+
668
+ // Jumps to the next row whose text starts with the typed letters.
669
+ const typeAhead = function (char) {
670
+
671
+ if (cancelTypeTimer) cancelTypeTimer();
672
+ cancelTypeTimer = box.after(TYPE_AHEAD_RESET_MS, function () {
673
+ typeBuffer = "";
674
+ cancelTypeTimer = null;
675
+ });
676
+ typeBuffer += char.toLocaleLowerCase();
677
+
678
+ const list = getEnabledVisibleRecs();
679
+ if (!list.length) return;
680
+
681
+ const start = Math.max(0, list.indexOf(recs.get(box.value)));
682
+ // WHY: One letter: the next match. More letters: the same row, while it still matches.
683
+ const from = (typeBuffer.length === 1) ? start + 1 : start;
684
+ for (let i = 0; i < list.length; i++) {
685
+ const rec = list[(from + i) % list.length];
686
+ const text = String(rec.node.text).toLocaleLowerCase();
687
+ if (text.indexOf(typeBuffer) !== 0) continue;
688
+ selectRec(rec, 1);
689
+ return;
690
+ }
691
+
692
+ };
693
+
694
+ const getRecOfEvent = function (event) {
695
+ for (let element = event.target; element && element !== box.listArea.elem; element = element.parentElement) {
696
+ if (element._treeRec) return element._treeRec;
697
+ }
698
+ return null;
699
+ };
700
+
701
+ const isArrowEvent = function (event) {
702
+ return !!(event.target && event.target.closest && event.target.closest("[data-tv-arrow]"));
703
+ };
704
+
705
+ // Puts the row (and its children group) before the row of next. (No next: it stays at the end.)
706
+ const moveRecTo = function (rec, next) {
707
+ if (!next || !next.row) return;
708
+ next.row.elem.before(rec.row.elem);
709
+ if (rec.group) rec.row.elem.after(rec.group.elem);
710
+ };
711
+
712
+ const getInsertPosition = function (position, length) {
713
+ return (position < 0 || position > length) ? length : position;
714
+ };
715
+
716
+ const addRootNode = function (node, position) {
717
+ const at = getInsertPosition(position, roots.length);
718
+ indexNodes([node], null);
719
+ const rec = createIn(box.list, function () { return buildRec(node, null, 0); });
720
+ roots.splice(at, 0, rec);
721
+ moveRecTo(rec, roots[at + 1]);
722
+ };
723
+
724
+ const addChildNode = function (parentEntry, node, position) {
725
+
726
+ const parentNode = parentEntry.node;
727
+ parentNode.children = parentNode.children || [];
728
+ const at = getInsertPosition(position, parentNode.children.length);
729
+ indexNodes([node], parentNode);
730
+ parentNode.children.splice(at, 0, node);
731
+
732
+ const parent = recs.get(parentNode.key);
733
+ // WHY: an open parent with no group of children yet (it was empty, lazy, or emptied by setChildren): the new
734
+ // child was not drawn, and setValue() and the keys could not reach it
735
+ if (parent && !parent.built && isOpen(parent)) {
736
+ buildChildren(parent);
737
+ drawRow(parent);
738
+ return;
739
+ }
740
+
741
+ // Not drawn yet (closed or not created): drawn when it opens. Only the arrow of the parent can change.
742
+ if (!parent || !parent.built) {
743
+ if (parent) drawRow(parent);
744
+ return;
745
+ }
746
+
747
+ const rec = createIn(parent.group, function () { return buildRec(node, parent, parent.level + 1); });
748
+ parent.children.splice(at, 0, rec);
749
+ moveRecTo(rec, parent.children[at + 1]);
750
+ drawRow(parent);
751
+
752
+ };
753
+
754
+ // *** PUBLIC FUNCTIONS:
755
+
756
+ // The tree again from a NODE list. (Expanded states come from the nodes.)
757
+ box.setItems = function (items, value = box.value) {
758
+ roots.forEach(forget);
759
+ roots.forEach(removeRecView);
760
+ roots = [];
761
+ recs = new Map();
762
+ nodeIndex = new Map();
763
+ hoverRec = null;
764
+ const list = (Array.isArray(items) ? items : []).map(normalize);
765
+ indexNodes(list, null);
766
+ createIn(box.list, function () {
767
+ roots = list.map(function (node) { return buildRec(node, null, 0); });
768
+ });
769
+ box.value = null;
770
+ if (value !== null && value !== undefined) {
771
+ const rec = ensureRec(value);
772
+ if (rec) selectRec(rec, 0);
773
+ }
774
+ updateEmpty();
775
+ refreshScroll();
776
+ };
777
+
778
+ // The nodes with their current expanded states. (A copy; data is kept by reference.)
779
+ box.getItems = function () {
780
+ const copy = function (node) {
781
+ const out = Object.assign({}, node);
782
+ if (node.children) out.children = node.children.map(copy);
783
+ return out;
784
+ };
785
+ return roots.map(function (rec) { return copy(rec.node); });
786
+ };
787
+
788
+ box.getNode = function (key) {
789
+ const entry = nodeIndex.get(String(key));
790
+ return (entry) ? entry.node : null;
791
+ };
792
+
793
+ box.getParent = function (key) {
794
+ const entry = nodeIndex.get(String(key));
795
+ return (entry) ? entry.parent : null;
796
+ };
797
+
798
+ // [root, ..., node]
799
+ box.getPath = function (key) {
800
+ const out = [];
801
+ let entry = nodeIndex.get(String(key));
802
+ while (entry) {
803
+ out.unshift(entry.node);
804
+ entry = (entry.parent) ? nodeIndex.get(entry.parent.key) : null;
805
+ }
806
+ return out;
807
+ };
808
+
809
+ // The element of a row, or null (a closed parent: the row is not made). Its text: [data-part=text]
810
+ box.getRowElem = function (key) {
811
+ const rec = recs.get(key);
812
+ return (rec && rec.row) ? rec.row.elem : null;
813
+ };
814
+
815
+ box.getSelectedNode = function () {
816
+ return box.getNode(box.value);
817
+ };
818
+
819
+ // The node of the row under an event of the mouse, or null. (Ex: a right click selects its row.)
820
+ box.getNodeOfEvent = function (event) {
821
+ const rec = getRecOfEvent(event);
822
+ return (rec) ? rec.node : null;
823
+ };
824
+
825
+ // silent: 1 -> onSelect is not called. null: nothing is selected.
826
+ box.setValue = function (key, silent = 0) {
827
+ if (key === null || key === undefined) {
828
+ const oldRec = recs.get(box.value);
829
+ box.previousValue = box.value;
830
+ box.value = null;
831
+ if (oldRec) paintRow(oldRec);
832
+ box.listArea.aria = { activedescendant: null };
833
+ return;
834
+ }
835
+ const rec = ensureRec(key);
836
+ if (!rec) return;
837
+ selectRec(rec, !silent);
838
+ scrollRowIntoView(rec);
839
+ };
840
+
841
+ // NOTE: A node in a closed branch only keeps the state: it is drawn so when its parent opens.
842
+ const setNodeExpanded = function (key, expanded) {
843
+ const rec = recs.get(String(key));
844
+ if (rec) {
845
+ setExpanded(rec, expanded);
846
+ return;
847
+ }
848
+ const entry = nodeIndex.get(String(key));
849
+ if (entry) entry.node.expanded = (expanded) ? 1 : 0;
850
+ };
851
+
852
+ box.expand = function (key) {
853
+ setNodeExpanded(key, 1);
854
+ };
855
+
856
+ box.collapse = function (key) {
857
+ setNodeExpanded(key, 0);
858
+ };
859
+
860
+ box.toggle = function (key) {
861
+ const node = box.getNode(key);
862
+ if (node) setNodeExpanded(key, !(node.expanded == 1));
863
+ };
864
+
865
+ // NOTE: Opens every node that has children (lazy ones are not loaded).
866
+ box.expandAll = function () {
867
+ const walk = function (list) {
868
+ list.forEach(function (rec) {
869
+ if (!rec.node.children || !rec.node.children.length) return;
870
+ setExpanded(rec, 1);
871
+ walk(rec.children || []);
872
+ });
873
+ };
874
+ walk(roots);
875
+ };
876
+
877
+ box.collapseAll = function () {
878
+ recs.forEach(function (rec) { setExpanded(rec, 0); });
879
+ nodeIndex.forEach(function (entry, key) {
880
+ if (!recs.get(key)) entry.node.expanded = 0;
881
+ });
882
+ };
883
+
884
+ // Opens the parents of a node and scrolls to it.
885
+ box.reveal = function (key) {
886
+ scrollRowIntoView(ensureRec(key));
887
+ };
888
+
889
+ // Changes some fields of a node and paints only its row. ("children" -> use setChildren)
890
+ box.updateNode = function (key, props = {}) {
891
+ const entry = nodeIndex.get(String(key));
892
+ if (!entry) return;
893
+ Object.keys(props).forEach(function (name) {
894
+ if (name === "children" || name === "key" || name === "expanded") return;
895
+ entry.node[name] = props[name];
896
+ });
897
+ const rec = recs.get(String(key));
898
+ if (rec) drawRow(rec);
899
+ if (props.expanded !== undefined) setNodeExpanded(key, props.expanded);
900
+ };
901
+
902
+ // New children for a node (lazy loading, a refresh). Only that branch is drawn again.
903
+ box.setChildren = function (key, children) {
904
+ const entry = nodeIndex.get(String(key));
905
+ if (!entry) return;
906
+ (entry.node.children || []).forEach(unindexNode);
907
+ entry.node.children = (Array.isArray(children) ? children : []).map(normalize);
908
+ indexNodes(entry.node.children, entry.node);
909
+ dropLostValue();
910
+ const rec = recs.get(String(key));
911
+ if (rec) rebuildChildren(rec); // Not created yet: drawn when its parent opens.
912
+ refreshScroll();
913
+ };
914
+
915
+ // Adds a node under parentKey (null: a root) at position (-1: the end). Returns the node.
916
+ box.addNode = function (parentKey, node, position = -1) {
917
+
918
+ const isRoot = (parentKey === null || parentKey === undefined);
919
+ const parentEntry = (isRoot) ? null : nodeIndex.get(String(parentKey));
920
+ if (!isRoot && !parentEntry) return null;
921
+
922
+ const newNode = normalize(node);
923
+ if (nodeIndex.get(newNode.key)) {
924
+ console.warn("TreeView: The key is used: " + newNode.key);
925
+ return null;
926
+ }
927
+
928
+ if (isRoot) addRootNode(newNode, position);
929
+ else addChildNode(parentEntry, newNode, position);
930
+
931
+ updateEmpty();
932
+ refreshScroll();
933
+ return newNode;
934
+
935
+ };
936
+
937
+ box.removeNode = function (key) {
938
+
939
+ const entry = nodeIndex.get(String(key));
940
+ if (!entry) return;
941
+
942
+ if (entry.parent) {
943
+ const siblings = entry.parent.children;
944
+ const position = siblings.indexOf(entry.node);
945
+ if (position !== -1) siblings.splice(position, 1);
946
+ }
947
+ unindexNode(entry.node);
948
+
949
+ const rec = recs.get(String(key));
950
+ if (rec) {
951
+ const list = (rec.parent) ? rec.parent.children : roots;
952
+ const position = list.indexOf(rec);
953
+ if (position !== -1) list.splice(position, 1);
954
+ forget(rec);
955
+ removeRecView(rec);
956
+ }
957
+
958
+ dropLostValue();
959
+ const parentRec = (entry.parent) ? recs.get(entry.parent.key) : null;
960
+ if (parentRec) drawRow(parentRec);
961
+ updateEmpty();
962
+ refreshScroll();
963
+
964
+ };
965
+
966
+ box.setEnabled = function (enabled) {
967
+ const isEnabled = (enabled) ? 1 : 0;
968
+ box.enabled = isEnabled;
969
+ box.listArea.tabIndex = (isEnabled) ? 0 : -1;
970
+ box.opacity = (isEnabled) ? 1 : _s.disabled.opacity;
971
+ box.listArea.css.pointerEvents = (isEnabled) ? "" : "none";
972
+ };
973
+
974
+ box.focus = function () {
975
+ box.listArea.elem.focus();
976
+ };
977
+
978
+ box.setEmptyText = function (text) {
979
+ box.emptyText = text;
980
+ box.lblEmpty.plainText = text;
981
+ };
982
+
983
+ // NOTE: No remove() of its own: basic.js removes the rows, the ScrollBar (a child) and their events,
984
+ // and stops the timer of box.after().
985
+
986
+ // *** OBJECT VIEW:
987
+
988
+ box.clipContent = 1;
989
+ box.border = _s.box.border;
990
+ box.borderColor = _s.box.borderColor;
991
+ box.round = _s.box.round;
992
+
993
+ // BOX: Scrolling area (it takes the focus)
994
+ box.listArea = startBox(0, 0, "100%", (isAutoHeight) ? "auto" : "100%", {
995
+ color: "transparent",
996
+ scrollY: (isAutoHeight) ? 0 : 1,
997
+ clickable: 1,
998
+ css: { outline: "none" },
999
+ tabIndex: 0,
1000
+ role: "tree",
1001
+ aria: { label: box.ariaLabel },
1002
+ });
1003
+ // WHY: With height "auto" the area must be in the flow, so the tree grows with its rows.
1004
+ if (isAutoHeight) box.listArea.css.position = "relative";
1005
+
1006
+ // GROUP: Rows
1007
+ box.list = VGroup({
1008
+ width: "100%",
1009
+ height: "auto",
1010
+ align: "left top",
1011
+ gap: _s.row.space,
1012
+ padding: _s.box.padding,
1013
+ color: "transparent",
1014
+ position: "relative",
1015
+ css: { alignItems: "stretch" },
1016
+ });
1017
+ endGroup();
1018
+
1019
+ // LABEL: Empty text
1020
+ box.lblEmpty = Label({
1021
+ plainText: box.emptyText,
1022
+ left: 0,
1023
+ width: "100%",
1024
+ top: EMPTY_TEXT_TOP,
1025
+ fontSize: _s.empty.fontSize,
1026
+ textColor: _s.empty.textColor,
1027
+ textAlign: "center",
1028
+ visible: 0,
1029
+ });
1030
+
1031
+ endBox();
1032
+
1033
+ if (!isAutoHeight && box.useScrollBar == 1 && typeof ScrollBar !== "undefined") {
1034
+ const scrollBarProps = { scrollableBox: box.listArea, neverHide: 0, showDots: 0 };
1035
+ box.scrollBar = ScrollBar(Object.assign(scrollBarProps, _s.scrollBar));
1036
+ }
1037
+
1038
+ // *** OBJECT INIT CODE:
1039
+
1040
+ const repaintSelected = function () {
1041
+ const rec = recs.get(box.value);
1042
+ if (rec) paintRow(rec);
1043
+ };
1044
+
1045
+ // EVENTS: One listener for all the rows (the rows are drawn again often).
1046
+ box.listArea.on("click", function (self, event) {
1047
+ if (box.enabled != 1) return;
1048
+ const rec = getRecOfEvent(event);
1049
+ if (!rec) return;
1050
+ if (isArrowEvent(event) && canExpand(rec)) {
1051
+ setExpanded(rec, !isOpen(rec), 1);
1052
+ return;
1053
+ }
1054
+ selectRec(rec, 1);
1055
+ });
1056
+ box.listArea.on("dblclick", function (self, event) {
1057
+ if (box.enabled != 1) return;
1058
+ const rec = getRecOfEvent(event);
1059
+ if (!rec || isArrowEvent(event) || isNodeDisabled(rec.node)) return;
1060
+ if (canExpand(rec) && box.toggleOnDoubleClick == 1) setExpanded(rec, !isOpen(rec), 1);
1061
+ activateRec(rec, event);
1062
+ });
1063
+ box.listArea.on("mousemove", function (self, event) {
1064
+ if (box.enabled != 1) return;
1065
+ setHover(getRecOfEvent(event));
1066
+ });
1067
+ box.listArea.on("mouseleave", function () { setHover(null); });
1068
+ // WHY isKeyFocus: the ring of the selected row is for the keyboard; a click focused the tree and showed it too
1069
+ box.listArea.on("keydown", function (self, event) {
1070
+ if (!isKeyFocus) {
1071
+ isKeyFocus = 1;
1072
+ repaintSelected();
1073
+ }
1074
+ onKeyDown(event);
1075
+ });
1076
+ box.listArea.on("mousedown", function () {
1077
+ isKeyFocus = 0;
1078
+ repaintSelected();
1079
+ });
1080
+ box.listArea.on("focus", function () {
1081
+ hasFocus = 1;
1082
+ isKeyFocus = box.listArea.elem.matches(":focus-visible") ? 1 : 0;
1083
+ repaintSelected();
1084
+ });
1085
+ box.listArea.on("blur", function () {
1086
+ hasFocus = 0;
1087
+ repaintSelected();
1088
+ });
1089
+
1090
+ box.setItems(startItems, startValue);
1091
+ if (box.enabled != 1) box.setEnabled(0);
1092
+
1093
+ return endObject(box);
1094
+
1095
+ };
1096
+
1097
+ TreeView._counter = 1;
1098
+
1099
+ // A key -> a safe part of an id.
1100
+ TreeView.toId = function (key) {
1101
+ return String(key).replace(/[^A-Za-z0-9_-]/g, function (c) { return "_" + c.charCodeAt(0).toString(16); });
1102
+ };
1103
+
1104
+ // A small right arrow (rotated 90° when the node is open).
1105
+ TreeView.getChevronSvg = function (color, size) {
1106
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 12 12"' +
1107
+ ' fill="none" stroke="' + color + '" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" style="display:block">' +
1108
+ '<path d="M4.5 2.5 8 6l-3.5 3.5"/></svg>';
1109
+ };
1110
+
1111
+ // *** STYLE PACKAGES:
1112
+ // USAGE: TreeView({ styleName: "modern" })
1113
+ // USAGE: TreeView({ styleName: "dark", style: { rowSelected: { color: "#3D3D3D" } } }) // Change only some keys.
1114
+ // USAGE: TreeView({ style: TreeView.styles.dark }) // The same as styleName: "dark". (The package is copied, never changed.)
1115
+ // NOTE: A new package needs only the keys that differ from the default (classic) style.
1116
+ // USAGE: TreeView.styles.myStyle = { tag: { textColor: "red" } };
1117
+ TreeView.styles = {
1118
+
1119
+ // Dark text, gray hover and selection on a light page.
1120
+ light: TreeViewDefaults.style,
1121
+
1122
+ // Cadetblue, same colors as the modern Tabs, CheckBox, RadioButton and TagInput.
1123
+ modern: {
1124
+ box: { color: "transparent", border: 0, borderColor: "transparent", round: 0, padding: [6, 6] },
1125
+ row: { height: 32, fontSize: 14, textColor: Black(0.75), indent: 18, paddingLeft: 6, paddingRight: 10, gap: 8, round: 100 },
1126
+ rowHover: { color: "#EEF5F5" },
1127
+ rowSelected: { color: "#DFECEC", textColor: "#2F5F61" },
1128
+ rowFocus: { outlineColor: "cadetblue" },
1129
+ arrow: { color: "cadetblue", size: 12, motion: 0.15 },
1130
+ icon: { size: 16 },
1131
+ tag: { textColor: "cadetblue", fontSize: 12 },
1132
+ note: { textColor: Black(0.4), fontSize: 12 },
1133
+ disabled: { opacity: 0.4 },
1134
+ empty: { textColor: Black(0.4), fontSize: 14 },
1135
+ scrollBar: { bar_color: "#9A9A98", bar_mouseOverColor: "#6A6A68", bar_width: 4, bar_round: 3, bar_opacity: 0.4, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
1136
+ },
1137
+
1138
+ // Dark theme: the grays and the blue of a design tool (its layers).
1139
+ dark: {
1140
+ box: { color: "#2C2C2C", border: 0, borderColor: "transparent", round: 0, padding: [4, 4] },
1141
+ row: { height: 24, fontSize: 12, textColor: "#E6E6E6", indent: 12, paddingLeft: 2, paddingRight: 8, gap: 6, round: 4 },
1142
+ rowHover: { color: "#383838" },
1143
+ rowSelected: { color: "#34435A", textColor: "#FFFFFF" },
1144
+ rowFocus: { outlineColor: "#0C8CE9" },
1145
+ arrow: { color: "#A5A5A5", size: 10, motion: 0.12 },
1146
+ icon: { size: 14 },
1147
+ tag: { textColor: "#A5A5A5", fontSize: 12 },
1148
+ note: { textColor: "#6E6E6E", fontSize: 12 },
1149
+ disabled: { opacity: 0.4 },
1150
+ empty: { textColor: "#6E6E6E", fontSize: 12 },
1151
+ scrollBar: { bar_color: "#8A8A88", bar_mouseOverColor: "#BFBFBD", bar_width: 4, bar_round: 3, bar_opacity: 0.4, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
1152
+ },
1153
+
1154
+ };
1155
+ TreeView.styles.classic = TreeView.styles.light; // the old name of light: the pages made with it keep their look
1156
+
1157
+ // The minimal theme (_designs/minimal-theme.htm): a line around the tree, the rows of ItemList minimal moved in by their level: 28 px, a
1158
+ // 13 px text, no fill; a line around the row under the mouse, an ink line around the selected one (with the focus of
1159
+ // the keys: a blue line inside it). minimalDark: the same on a dark window.
1160
+ TreeView.createMinimalStyle = function (colors) {
1161
+ return {
1162
+ box: { color: "transparent", border: 1, borderColor: colors.line, round: 4, padding: [4, 4] },
1163
+ row: { height: 28, fontSize: 13, textColor: colors.text, indent: 16, paddingLeft: 6, paddingRight: 8, gap: 6, round: 3, border: 1, borderColor: "transparent", space: 2 },
1164
+ rowHover: { color: "transparent", borderColor: colors.lineHover },
1165
+ rowSelected: { color: "transparent", textColor: colors.ink, borderColor: colors.ink },
1166
+ rowFocus: { outlineColor: colors.focus },
1167
+ arrow: { color: colors.softText, size: 12, motion: 0.15 },
1168
+ icon: { size: 14 },
1169
+ tag: { textColor: colors.softText, fontSize: 11 },
1170
+ note: { textColor: colors.faintText, fontSize: 12 },
1171
+ disabled: { opacity: 0.4 },
1172
+ empty: { textColor: colors.softText, fontSize: 12 },
1173
+ scrollBar: { bar_color: colors.scrollBar, bar_mouseOverColor: colors.scrollBar, bar_width: 3, bar_round: 2, bar_opacity: 0.45, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
1174
+ };
1175
+ };
1176
+ TreeView.MINIMAL_COLORS = { line: Black(0.2), text: Black(0.85), softText: Black(0.55), faintText: Black(0.54), lineHover: Black(0.45), ink: "#1F1F1F", focus: "#3D8BD9", scrollBar: "#373836" };
1177
+ TreeView.styles.minimal = TreeView.createMinimalStyle(TreeView.MINIMAL_COLORS);
1178
+ TreeView.MINIMAL_DARK_COLORS = { line: White(0.18), text: White(0.88), softText: White(0.55), faintText: White(0.48), lineHover: White(0.42), ink: "#EDEDEB", focus: "#7FB6DE", scrollBar: "#CFCFCD" };
1179
+ TreeView.styles.minimalDark = TreeView.createMinimalStyle(TreeView.MINIMAL_DARK_COLORS);