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,597 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ JsonView - v26.11
6
+
7
+ UI COMPONENT
8
+ - A value of JSON as a tree: the answer of a server, a settings file, a dictionary of a program. Its keys and its values in
9
+ colors, a branch opens with its arrow.
10
+ - NEEDS comp/tree-view.js, loaded first. comp/context-menu.js, when it is loaded, gives the right click its menu.
11
+ - A value of any size: the rows of a branch are made when it opens, and a long list comes in pages ([0 … 99], [100 … 199]),
12
+ so a list of a million items opens at once.
13
+ - Which value is this? A click on a row gives its path (users[0].name) with onSelect. Right click, or Ctrl+C on a row: its
14
+ value (a list or an object as JSON text) or its path goes to the clipboard.
15
+ - A text of JSON: setJsonText(). A text that is not JSON shows its error, it does not stop the page.
16
+ - Keyboard: the keys of TreeView: the arrows, Home, End, a letter jumps to a key.
17
+ - height: a number or "100%": its rows scroll inside it. "auto": it grows with its rows.
18
+ - Style packages: "light" (default), "dark". Select with styleName (JsonView.styles), or later with setTheme().
19
+
20
+ USAGE:
21
+ const view = JsonView({
22
+ height: 400,
23
+ value: { name: "Ada", age: 36, tags: ["a", "b"], address: { city: "İzmir" }, active: true, phone: null },
24
+ expandLevel: 1,
25
+ onSelect: function (self, entry) { println(entry.pathText + " " + entry.kind); },
26
+ });
27
+ view.setValue({ a: 1 });
28
+ view.setJsonText('{"a": 1}');
29
+ view.getValue();
30
+ view.getSelectedPath(); // ["tags", 0]
31
+ view.getSelectedPathText(); // tags[0]
32
+ view.getSelectedValue(); // "a"
33
+ view.selectPath(["address", "city"]); // opens the rows down to it
34
+ view.expandAll();
35
+ view.collapseAll();
36
+ view.setTheme("dark");
37
+
38
+ AN ENTRY (onSelect): { key, label, kind, path, pathText, value }
39
+ - kind: "object", "array", "string", "number", "boolean", "null", or "page" (a row like [0 … 99]).
40
+ - value: the value of a string, a number, a boolean or null. An object or a list: null (getSelectedValue() gives it).
41
+
42
+ Started Date: November 2026
43
+ Webpage: https://bug7a.github.io/js-components/
44
+
45
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
46
+ Licensed under the Apache License, Version 2.0
47
+ SPDX-License-Identifier: Apache-2.0
48
+
49
+ */
50
+
51
+ "use strict";
52
+
53
+ // Default values:
54
+ const JsonViewDefaults = {
55
+ key: "0", // Your name for this object: read it in a callback (self.key)
56
+ left: 0,
57
+ top: 0,
58
+ width: "100%",
59
+ height: "100%", // A number or "100%": its rows scroll inside it. "auto": it grows with its rows.
60
+ value: null, // A value of JSON. null: emptyText
61
+ expandLevel: 1, // How many levels are open at the start. 0: only the first rows, closed.
62
+ pageSize: 100, // A list or an object with more items than this comes in pages.
63
+ emptyText: "No value",
64
+ copyValueText: "Copy value",
65
+ copyPathText: "Copy path",
66
+ useScrollBar: 1, // basic/scroll-bar.js (when it is loaded), with a height that is not "auto". 0: the scrollbar of the browser
67
+ ariaLabel: "JSON",
68
+ onSelect: function (self, entry) { }, // A row was chosen (See AN ENTRY above). (Not at create time, not with selectPath())
69
+ onCopy: function (self, text) { }, // A value or a path is on the clipboard.
70
+ styleName: "light", // "light", "dark" or a name added to JsonView.styles
71
+ style: { // Light style package (default)
72
+ box: { color: "white" },
73
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace',
74
+ // The colors of the parts of a row
75
+ kinds: {
76
+ key: "#2467B8",
77
+ index: Black(0.45),
78
+ string: "#1F7A3D",
79
+ number: "#A85A00",
80
+ boolean: "#8A2F8E",
81
+ null: "#7A7F87",
82
+ object: Black(0.45),
83
+ array: Black(0.45),
84
+ page: Black(0.5),
85
+ },
86
+ // A style of TreeView, over its "light" package
87
+ tree: {
88
+ box: { color: "transparent", padding: [6, 6] },
89
+ row: { height: 26, fontSize: 13, textColor: Black(0.8), indent: 16, gap: 8 },
90
+ rowHover: { color: Black(0.05) },
91
+ rowSelected: { color: Black(0.09), textColor: "#141414" },
92
+ rowFocus: { outlineColor: "rgba(63, 141, 148, 0.8)" },
93
+ arrow: { color: Black(0.45) },
94
+ note: { fontSize: 13 },
95
+ empty: { textColor: Black(0.45), fontSize: 14 },
96
+ },
97
+ error: { textColor: "#C0392B" }, // A text that is not JSON
98
+ menuStyleName: "light", // The package of its menu
99
+ menu: { item: { height: 30, fontSize: 13 } }, // Over that package
100
+ menuMinWidth: 150,
101
+ },
102
+ };
103
+
104
+ const JsonView = function (params = {}) {
105
+
106
+ // Merge style package: params.style > JsonView.styles[styleName] > JsonViewDefaults.style (light)
107
+ const pageStyle = params.style; // WHY: setTheme() puts it over another package.
108
+ basic.applyStylePackage(params, JsonView.styles, JsonViewDefaults);
109
+
110
+ // Merge params:
111
+ mergeIntoIfMissing(params, JsonViewDefaults);
112
+
113
+ // WHY before startObject: box.height never reads "auto".
114
+ const isAutoHeight = (String(params.height) === "auto");
115
+
116
+ // BOX: Component container
117
+ const box = startObject(params);
118
+
119
+ // *** PRIVATE VARIABLES:
120
+ let _s = box.style;
121
+ const openKeys = new Set(); // The rows that are open: setTheme() makes the tree again with them.
122
+ let isFirstDraw = 1; // 1: expandLevel says which rows are open.
123
+ let rootKeys = [];
124
+
125
+ // *** PUBLIC VARIABLES:
126
+ box.errorText = ""; // The error of a text that is not JSON (setJsonText)
127
+ // NOTE: Default values are also public variables. (box.value, box.expandLevel...)
128
+
129
+ // *** PRIVATE FUNCTIONS:
130
+
131
+ // The style of a package, with the style of the page over it. (setTheme)
132
+ const styleOfPackage = function (styleName) {
133
+ const fresh = { styleName: styleName, style: pageStyle };
134
+ basic.applyStylePackage(fresh, JsonView.styles, JsonViewDefaults);
135
+ mergeIntoIfMissing(fresh, JsonViewDefaults);
136
+ return fresh.style;
137
+ };
138
+
139
+ // NODES: a part of the value as the nodes of the tree, only as far as it is open.
140
+
141
+ const shouldOpen = function (key, level) {
142
+ const isOpen = (isFirstDraw) ? level < box.expandLevel : openKeys.has(key);
143
+ if (isOpen) openKeys.add(key);
144
+ return isOpen;
145
+ };
146
+
147
+ // What a node with children needs to make them, when it opens.
148
+ const ownerOf = function (value, path, kind) {
149
+ const names = (kind === "object") ? Object.keys(value) : null;
150
+ return { key: JsonView.pathText(path), value: value, path: path, names: names, count: (names) ? names.length : value.length };
151
+ };
152
+
153
+ const createPageNode = function (owner, from, to, level) {
154
+ const key = owner.key + "<" + from + "-" + to + ">";
155
+ const node = {
156
+ key: key,
157
+ text: "[" + from + " … " + (to - 1) + "]",
158
+ textColor: _s.kinds.page,
159
+ hasChildren: 1,
160
+ data: { kind: "page", path: owner.path, owner: owner, from: from, to: to },
161
+ };
162
+ // WHY openKeys and not expandLevel: a page is not opened by itself: a long list stays closed.
163
+ if (openKeys.has(key)) {
164
+ node.expanded = 1;
165
+ node.children = createNodesOfRange(owner, from, to, level + 1);
166
+ }
167
+ return node;
168
+ };
169
+
170
+ const createValueNode = function (value, path, level) {
171
+ const kind = JsonView.kindOf(value);
172
+ const label = (path.length > 0) ? path[path.length - 1] : "";
173
+ const node = {
174
+ key: JsonView.pathText(path),
175
+ text: String(label),
176
+ textColor: (typeof label === "number") ? _s.kinds.index : _s.kinds.key,
177
+ description: JsonView.valueText(value, kind),
178
+ descriptionColor: _s.kinds[kind],
179
+ data: { kind: kind, path: path, value: value },
180
+ };
181
+ if (kind !== "object" && kind !== "array") return node;
182
+ const owner = ownerOf(value, path, kind);
183
+ node.data.owner = owner;
184
+ if (owner.count === 0) return node;
185
+ node.hasChildren = 1;
186
+ if (shouldOpen(node.key, level)) {
187
+ node.expanded = 1;
188
+ node.children = createNodesOfRange(owner, 0, owner.count, level + 1);
189
+ }
190
+ return node;
191
+ };
192
+
193
+ // The nodes of one part of a list or an object: its items, or pages of them.
194
+ const createNodesOfRange = function (owner, from, to, level) {
195
+ const nodes = [];
196
+ const step = JsonView.pageStep(to - from, box.pageSize);
197
+ if (step > 1) {
198
+ for (let start = from; start < to; start += step) {
199
+ nodes.push(createPageNode(owner, start, Math.min(start + step, to), level));
200
+ }
201
+ return nodes;
202
+ }
203
+ for (let index = from; index < to; index++) {
204
+ const name = (owner.names) ? owner.names[index] : index;
205
+ nodes.push(createValueNode(owner.value[name], owner.path.concat([name]), level));
206
+ }
207
+ return nodes;
208
+ };
209
+
210
+ const createRootNodes = function () {
211
+ const kind = JsonView.kindOf(box.value);
212
+ if (kind === "null") return [];
213
+ if (kind !== "object" && kind !== "array") return [createValueNode(box.value, [], 0)];
214
+ const owner = ownerOf(box.value, [], kind);
215
+ if (owner.count === 0) return [createValueNode(box.value, [], 0)];
216
+ return createNodesOfRange(owner, 0, owner.count, 0);
217
+ };
218
+
219
+ const onLoadChildren = function (tree, node) {
220
+ const data = node.data;
221
+ const from = (data.kind === "page") ? data.from : 0;
222
+ const to = (data.kind === "page") ? data.to : data.owner.count;
223
+ tree.setChildren(node.key, createNodesOfRange(data.owner, from, to, 0));
224
+ };
225
+
226
+ const onToggle = function (tree, node) {
227
+ if (node.expanded == 1) openKeys.add(node.key);
228
+ else openKeys.delete(node.key);
229
+ };
230
+
231
+ const entryOf = function (node) {
232
+ const data = node.data;
233
+ const hasOwnValue = (data.kind !== "object" && data.kind !== "array" && data.kind !== "page");
234
+ return {
235
+ key: node.key,
236
+ label: node.text,
237
+ kind: data.kind,
238
+ path: data.path,
239
+ pathText: JsonView.pathText(data.path),
240
+ value: (hasOwnValue) ? data.value : null,
241
+ };
242
+ };
243
+
244
+ const onTreeSelect = function (tree, node) {
245
+ box.onSelect(box, entryOf(node));
246
+ };
247
+
248
+ // COPY
249
+
250
+ const copyText = function (text) {
251
+ JsonView.copyToClipboard(text).then(function () {
252
+ if (!box.isRemoved) box.onCopy(box, text);
253
+ }).catch(function (error) {
254
+ console.warn("JsonView: It could not be copied. " + error);
255
+ });
256
+ };
257
+
258
+ const copySelected = function (what) {
259
+ const node = box.tree.getSelectedNode();
260
+ if (!node) return;
261
+ copyText((what === "path") ? JsonView.pathText(node.data.path) : JsonView.textToCopy(box.getSelectedValue()));
262
+ };
263
+
264
+ const onTreeKeyDown = function (self, event) {
265
+ if ((event.ctrlKey || event.metaKey) && !event.shiftKey && event.key.toLowerCase() === "c") {
266
+ event.preventDefault(); // WHY: A MenuBar with a Copy must not run it too.
267
+ copySelected("value");
268
+ }
269
+ };
270
+
271
+ // The row under the mouse is the row the menu copies. No row: no menu.
272
+ const onTreeRightClick = function (self, event) {
273
+ const node = box.tree.getNodeOfEvent(event);
274
+ box.menu.setEnabled((node) ? 1 : 0);
275
+ if (node) box.tree.setValue(node.key);
276
+ };
277
+
278
+ const createMenu = function () {
279
+ if (typeof ContextMenu === "undefined") return null;
280
+ return ContextMenu({
281
+ items: [{ key: "value", text: box.copyValueText }, { key: "path", text: box.copyPathText }],
282
+ styleName: _s.menuStyleName,
283
+ style: _s.menu,
284
+ minWidth: _s.menuMinWidth,
285
+ ariaLabel: box.ariaLabel,
286
+ onClick: function (self, item) { copySelected(item.key); },
287
+ });
288
+ };
289
+
290
+ // The tree and its menu. WHY a new tree for a theme: a TreeView keeps the style it was made with.
291
+ const createTree = function (nodes, selectedKey) {
292
+ createIn(box, function () {
293
+ box.tree = TreeView({
294
+ left: 0,
295
+ top: 0,
296
+ width: "100%",
297
+ height: (isAutoHeight) ? "auto" : "100%",
298
+ position: (isAutoHeight) ? "relative" : "absolute",
299
+ items: nodes,
300
+ value: selectedKey,
301
+ emptyText: box.emptyText,
302
+ useScrollBar: box.useScrollBar,
303
+ ariaLabel: box.ariaLabel,
304
+ styleName: "light",
305
+ style: _s.tree,
306
+ onSelect: onTreeSelect,
307
+ onToggle: onToggle,
308
+ onLoadChildren: onLoadChildren,
309
+ });
310
+ });
311
+ box.tree.elem.classList.add("json-view-tree");
312
+ box.tree.elem.style.setProperty("--json-view-font", _s.fontFamily);
313
+ box.tree.listArea.on("keydown", onTreeKeyDown);
314
+ box.menu = createMenu();
315
+ if (!box.menu) return;
316
+ box.tree.listArea.on("contextmenu", onTreeRightClick, true); // WHY capture: before the menu opens.
317
+ box.menu.attachTo(box.tree.listArea);
318
+ };
319
+
320
+ const removeTree = function () {
321
+ if (box.menu) box.menu.remove();
322
+ box.tree.remove();
323
+ };
324
+
325
+ const paintEmptyText = function () {
326
+ box.tree.setEmptyText(box.errorText || box.emptyText);
327
+ box.tree.lblEmpty.textColor = (box.errorText) ? _s.error.textColor : _s.tree.empty.textColor;
328
+ };
329
+
330
+ const drawValue = function () {
331
+ openKeys.clear();
332
+ isFirstDraw = 1;
333
+ const nodes = createRootNodes();
334
+ isFirstDraw = 0;
335
+ rootKeys = nodes.map(function (node) { return node.key; });
336
+ box.tree.setItems(nodes, null);
337
+ paintEmptyText();
338
+ };
339
+
340
+ // The pages of a long list open down to the one with this place of it.
341
+ const openPageOf = function (nodes, position) {
342
+ let list = nodes;
343
+ let page = null;
344
+ const isHere = function (node) {
345
+ return (node.data.kind === "page" && node.data.from <= position && position < node.data.to);
346
+ };
347
+ for (page = list.find(isHere); page; page = list.find(isHere)) {
348
+ box.tree.expand(page.key);
349
+ list = box.tree.getNode(page.key).children || [];
350
+ }
351
+ };
352
+
353
+ const nodesUnder = function (key) {
354
+ if (key !== null) return box.tree.getNode(key).children || [];
355
+ return rootKeys.map(function (rootKey) { return box.tree.getNode(rootKey); });
356
+ };
357
+
358
+ // *** PUBLIC FUNCTIONS:
359
+
360
+ // A new value: the rows open as expandLevel says.
361
+ box.setValue = function (value) {
362
+ box.value = (value === undefined) ? null : value;
363
+ box.errorText = "";
364
+ drawValue();
365
+ };
366
+ // USAGE: view.setValue({ a: 1 })
367
+
368
+ box.getValue = function () {
369
+ return box.value;
370
+ };
371
+
372
+ // Returns 1, or 0 with the error on the view (box.errorText).
373
+ box.setJsonText = function (text) {
374
+ try {
375
+ box.setValue(JSON.parse(text));
376
+ return 1;
377
+ } catch (error) {
378
+ box.setValue(null);
379
+ box.errorText = error.message;
380
+ paintEmptyText();
381
+ return 0;
382
+ }
383
+ };
384
+ // USAGE: view.setJsonText('{"a": 1}')
385
+
386
+ // null: no row is selected.
387
+ box.getSelectedPath = function () {
388
+ const node = box.tree.getSelectedNode();
389
+ return (node) ? node.data.path : null;
390
+ };
391
+
392
+ box.getSelectedPathText = function () {
393
+ const path = box.getSelectedPath();
394
+ return (path) ? JsonView.pathText(path) : "";
395
+ };
396
+
397
+ // The value of the selected row. A page: its part of the list. undefined: no row is selected.
398
+ box.getSelectedValue = function () {
399
+ const node = box.tree.getSelectedNode();
400
+ if (!node) return undefined;
401
+ if (node.data.kind === "page") return JsonView.pageValue(node.data);
402
+ return JsonView.valueAt(box.value, node.data.path);
403
+ };
404
+
405
+ // The rows down to a place of the value open; it is selected and seen. onSelect is not called.
406
+ box.selectPath = function (path) {
407
+ let value = box.value;
408
+ for (let step = 0; step < path.length; step++) {
409
+ if (value === null || typeof value !== "object") return;
410
+ const ownerKey = (step === 0) ? null : JsonView.pathText(path.slice(0, step));
411
+ if (ownerKey !== null) box.tree.expand(ownerKey);
412
+ const position = (Array.isArray(value)) ? path[step] : Object.keys(value).indexOf(String(path[step]));
413
+ openPageOf(nodesUnder(ownerKey), position);
414
+ value = value[path[step]];
415
+ }
416
+ box.tree.setValue(JsonView.pathText(path), 1);
417
+ };
418
+ // USAGE: view.selectPath(["users", 0, "name"])
419
+
420
+ // WHY the limits: every row of a big value would be one object on the page, and a value of the page can hold itself:
421
+ // thousands of groups in each other take seconds to draw.
422
+ box.expandAll = function () {
423
+ let rowCount = 0;
424
+ const openAll = function (nodes, depth) {
425
+ nodes.forEach(function (node) {
426
+ rowCount++;
427
+ if (rowCount > JsonView.MAX_ROWS_OF_EXPAND_ALL || depth >= JsonView.MAX_DEPTH_OF_EXPAND_ALL || !node.hasChildren) return;
428
+ box.tree.expand(node.key);
429
+ openAll(box.tree.getNode(node.key).children || [], depth + 1);
430
+ });
431
+ };
432
+ openAll(nodesUnder(null), 0);
433
+ };
434
+
435
+ box.collapseAll = function () {
436
+ box.tree.collapseAll();
437
+ };
438
+
439
+ // Another style package while it is on the page. A style given by the page stays over it. The open rows, the selection and
440
+ // the scroll stay.
441
+ box.setTheme = function (styleName) {
442
+ const selectedKey = box.tree.value;
443
+ const scrollTop = box.tree.listArea.elem.scrollTop;
444
+ box.styleName = styleName;
445
+ _s = styleOfPackage(styleName);
446
+ box.style = _s;
447
+ box.props(_s.box);
448
+ removeTree();
449
+ createTree(createRootNodes(), selectedKey);
450
+ paintEmptyText();
451
+ box.tree.listArea.elem.scrollTop = scrollTop;
452
+ };
453
+ // USAGE: view.setTheme("dark")
454
+
455
+ // WHY: box.superRemove is overwritten by a component that extends this one, so the local copy is called below.
456
+ const superRemove = box.remove;
457
+ box.superRemove = superRemove;
458
+ box.remove = function () {
459
+ if (box.isRemoved) return; // WHY: remove() can be called twice (also by the parent's remove()).
460
+ if (box.menu) box.menu.remove(); // WHY: not with box.own(): setTheme() makes a new one, and own() would keep the old one to the end.
461
+ superRemove.call(box);
462
+ };
463
+ // USAGE: view.remove();
464
+
465
+ // *** OBJECT VIEW:
466
+
467
+ basic.addCss("json-view", ".json-view-tree .basic_label { font-family: var(--json-view-font); }");
468
+ box.props(_s.box);
469
+ if (isAutoHeight) box.css.position = "relative";
470
+ createTree([], null);
471
+
472
+ // *** OBJECT INIT CODE:
473
+
474
+ drawValue();
475
+
476
+ return endObject(box);
477
+
478
+ };
479
+
480
+ // *** STATIC VARIABLES AND FUNCTIONS:
481
+
482
+ JsonView.MAX_LETTERS_OF_TEXT = 300; // A longer text is cut on its row. WHY: Its row is one line; the whole of it is copied.
483
+ JsonView.MAX_ROWS_OF_EXPAND_ALL = 3000;
484
+ JsonView.MAX_DEPTH_OF_EXPAND_ALL = 20;
485
+ JsonView.IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
486
+
487
+ JsonView.kindOf = function (value) {
488
+ if (value === null || value === undefined) return "null";
489
+ if (Array.isArray(value)) return "array";
490
+ if (value instanceof Date) return "string";
491
+ const type = typeof value;
492
+ if (type === "bigint") return "number";
493
+ return (type === "string" || type === "number" || type === "boolean" || type === "object") ? type : "null";
494
+ };
495
+
496
+ // The text of a value on its row. A list: [3], an object: {3}.
497
+ JsonView.valueText = function (value, kind) {
498
+ if (kind === "array") return "[" + value.length + "]";
499
+ if (kind === "object") return "{" + Object.keys(value).length + "}";
500
+ if (value instanceof Date) return JSON.stringify(value);
501
+ if (typeof value !== "string") return String(value);
502
+ const isCut = (value.length > JsonView.MAX_LETTERS_OF_TEXT);
503
+ return (isCut) ? JSON.stringify(value.slice(0, JsonView.MAX_LETTERS_OF_TEXT)) + " …" : JSON.stringify(value);
504
+ };
505
+
506
+ // ["users", 0, "a b"] -> users[0]["a b"]. The first level of a value that has no keys: $
507
+ JsonView.pathText = function (path) {
508
+ if (path.length === 0) return "$";
509
+ return path.map(function (part, index) {
510
+ if (typeof part === "number") return "[" + part + "]";
511
+ if (JsonView.IDENTIFIER.test(part)) return (index === 0) ? part : "." + part;
512
+ return "[" + JSON.stringify(part) + "]";
513
+ }).join("");
514
+ };
515
+
516
+ JsonView.valueAt = function (value, path) {
517
+ return path.reduce(function (part, name) { return part[name]; }, value);
518
+ };
519
+
520
+ // How many items are in each page of this many: 1: no pages. A million items in pages of 100: 10 pages, in each 100 pages...
521
+ JsonView.pageStep = function (count, pageSize) {
522
+ if (count <= pageSize) return 1;
523
+ let step = pageSize;
524
+ while (count / step > pageSize) step *= pageSize;
525
+ return step;
526
+ };
527
+
528
+ JsonView.pageValue = function (page) {
529
+ const owner = page.owner;
530
+ if (!owner.names) return owner.value.slice(page.from, page.to);
531
+ const part = {};
532
+ owner.names.slice(page.from, page.to).forEach(function (name) { part[name] = owner.value[name]; });
533
+ return part;
534
+ };
535
+
536
+ // A text as it is; anything else as JSON. WHY the catch: a value of the page can hold itself.
537
+ JsonView.textToCopy = function (value) {
538
+ if (typeof value === "string") return value;
539
+ try {
540
+ return JSON.stringify(value, null, 2);
541
+ } catch (error) {
542
+ return String(value);
543
+ }
544
+ };
545
+
546
+ // Returns a Promise.
547
+ JsonView.copyToClipboard = function (text) {
548
+ if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text);
549
+ // WHY: A page opened from a file has no navigator.clipboard.
550
+ const area = document.createElement("textarea");
551
+ area.value = text;
552
+ area.style.cssText = "position: fixed; left: -9999px; top: 0;";
553
+ document.body.appendChild(area);
554
+ area.select();
555
+ const isCopied = document.execCommand("copy");
556
+ area.remove();
557
+ return (isCopied) ? Promise.resolve() : Promise.reject(new Error("The browser refused it."));
558
+ };
559
+
560
+ // *** STYLE PACKAGES:
561
+ // USAGE: JsonView({ styleName: "dark" })
562
+ // USAGE: JsonView({ style: { kinds: { string: "tomato" } } }) // Change only some keys.
563
+ // NOTE: A new package needs only the keys that differ from the default (light) style.
564
+ // USAGE: JsonView.styles.myStyle = { box: { color: "#FFFDF5" } };
565
+ JsonView.styles = {
566
+
567
+ // A white page, the colors of the code of CodeEditor.
568
+ light: JsonViewDefaults.style,
569
+
570
+ // For a dark window.
571
+ dark: {
572
+ box: { color: "#1C1C1B" },
573
+ kinds: {
574
+ key: "#8CC4EE",
575
+ index: White(0.5),
576
+ string: "#8FD694",
577
+ number: "#F0B56A",
578
+ boolean: "#D7A2E8",
579
+ null: "#8D939B",
580
+ object: White(0.5),
581
+ array: White(0.5),
582
+ page: White(0.55),
583
+ },
584
+ tree: {
585
+ row: { textColor: White(0.85) },
586
+ rowHover: { color: White(0.06) },
587
+ rowSelected: { color: White(0.12), textColor: "#FFFFFF" },
588
+ rowFocus: { outlineColor: "rgba(111, 198, 205, 0.8)" },
589
+ arrow: { color: White(0.5) },
590
+ empty: { textColor: White(0.5) },
591
+ scrollBar: { bar_color: "#8A8A88", bar_mouseOverColor: "#BFBFBD" },
592
+ },
593
+ error: { textColor: "#FF9F93" },
594
+ menuStyleName: "dark",
595
+ },
596
+
597
+ };