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.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,3221 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
JsonEditor - v26.11
|
|
6
|
+
|
|
7
|
+
UI COMPONENT
|
|
8
|
+
- A list or an object of JSON written in a table: the items of a menu, the products of a shop, the settings of a
|
|
9
|
+
program. + adds a row, a click on a cell writes in it, a title sorts, a row is dragged to another place.
|
|
10
|
+
- Every shape of a list is kept: [{ a, b }], ["A", "B"], [[1, 2], [3, 4]] and mixed ones ["A", { text: "B" }, "-"].
|
|
11
|
+
An object is a table of names and values.
|
|
12
|
+
- A cell that holds a list or an object opens as a table of its own; the path at the top goes back.
|
|
13
|
+
- The kind of a column comes from its values and its name: a text, a number, on / off (a switch), a color (a swatch:
|
|
14
|
+
a click on it opens the color picker of the browser, or onPickColor), a picture (a small picture: onPickPicture
|
|
15
|
+
chooses one), a choice (columns: options, or optionsOf). The menu of a column changes its kind, sorts, renames, moves
|
|
16
|
+
and deletes it.
|
|
17
|
+
- Paste: a block of a spreadsheet (from the chosen cell), or a JSON list (its items become rows).
|
|
18
|
+
Cmd/Ctrl+C copies the chosen rows as lines of cells.
|
|
19
|
+
- The JSON view shows the same value as a text. A text that is not JSON says where its fault is; the table waits.
|
|
20
|
+
- Its own undo (Cmd/Ctrl+Z, Shift for redo) and a search of the rows. Only the rows in sight are drawn, so a list of
|
|
21
|
+
thousands of rows opens at once.
|
|
22
|
+
- The functions of the rows (onClick...) stay in their rows: a table and a JSON text can not hold their code.
|
|
23
|
+
- JsonEditor.open(): the editor in a window over the page, with Cancel and Apply.
|
|
24
|
+
- NEEDS comp/context-menu.js and comp/search-input.js, loaded first.
|
|
25
|
+
- Style packages: "light" (default), "dark", "minimal", "minimalDark" (the minimal theme, _designs/minimal-theme.htm).
|
|
26
|
+
Select with styleName (JsonEditor.styles).
|
|
27
|
+
|
|
28
|
+
USAGE:
|
|
29
|
+
const editor = JsonEditor({
|
|
30
|
+
height: 360,
|
|
31
|
+
titleText: "Products",
|
|
32
|
+
value: [{ name: "Apple", price: 1.2, unit: "kg", color: "#D64545", inStock: true }],
|
|
33
|
+
columns: [{ key: "unit", options: ["kg", "piece"] }],
|
|
34
|
+
onChange: function (self, value) { println(JSON.stringify(value)); },
|
|
35
|
+
});
|
|
36
|
+
editor.getValue(); // The list or the object. null: the table can not make one now (editor.errorText)
|
|
37
|
+
editor.setValue({ a: 1 }); // A new value; its undo starts again. (onChange is not called)
|
|
38
|
+
editor.setJsonText('[1, 2]'); // A text that is not JSON shows its error, the table stays.
|
|
39
|
+
editor.getJsonText(); // The value as a JSON text of 2 spaces
|
|
40
|
+
editor.setView("json"); // "table" or "json"
|
|
41
|
+
editor.undo();
|
|
42
|
+
editor.redo();
|
|
43
|
+
const win = JsonEditor.open({
|
|
44
|
+
titleText: "Products",
|
|
45
|
+
value: products,
|
|
46
|
+
onApply: function (value) { products = value; }, // Apply, when something was changed
|
|
47
|
+
onClose: function () { }, // after Apply, Cancel or win.close()
|
|
48
|
+
});
|
|
49
|
+
win.close(); // win.editor is the JsonEditor of the window
|
|
50
|
+
JsonEditor({
|
|
51
|
+
pictureBase: "https://example.com/shop/", // the relative pictures of the cells are read from here
|
|
52
|
+
onPickPicture: function (self, cell) { myPictureList(cell.value, cell.setValue); },
|
|
53
|
+
colorOf: function (value) { return myTheme.colorOf(value); }, // "var(--brand)" -> "#E0A038"
|
|
54
|
+
optionsOf: function (key) { return (key === "size") ? ["S", "M", "L"] : null; },
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
A COLUMN (columns): { key, kind, options, note, columns }
|
|
58
|
+
- key: the name of the column. An empty list shows these columns. In an object: the name of a row.
|
|
59
|
+
- kind: "text", "number", "flag" (on / off), "color", "picture", "option". (None: from its values and its name)
|
|
60
|
+
- options: the choices of a cell: ["kg", "piece"]. A cell takes another value too, when it is written.
|
|
61
|
+
- note: a soft text on the title of the column.
|
|
62
|
+
- columns: [A COLUMN]: the columns of the list in its cells (the table it opens as).
|
|
63
|
+
|
|
64
|
+
A CELL (onPickColor, onPickPicture): { value, key, title, x, y, rect, setValue(value) }
|
|
65
|
+
- key: the name of the value (its column, or the name of the row of an object). x, y: the click. rect: the cell on the screen.
|
|
66
|
+
- setValue(): writes the cell (the calls of one pick are one step of the undo: a picker may write while it is dragged).
|
|
67
|
+
|
|
68
|
+
KEYS:
|
|
69
|
+
- A cell: a click or a letter writes in it. Tab: the next cell. Enter: the next row. Shift+Enter: a new line in it.
|
|
70
|
+
↑ ↓: the next row. Esc: gives up. Delete: empties it. Enter on a list or an object opens it.
|
|
71
|
+
- The rows: a click on a number chooses it (Shift: up to it, Cmd/Ctrl: one more). Delete deletes them,
|
|
72
|
+
Cmd/Ctrl+D duplicates them, Alt+↑ ↓ moves them. Right click: the menu of a row.
|
|
73
|
+
- The window: Cmd/Ctrl+Enter applies. Esc closes it (when something was changed, at the second press).
|
|
74
|
+
|
|
75
|
+
Started Date: November 2026
|
|
76
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
77
|
+
|
|
78
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
79
|
+
Licensed under the Apache License, Version 2.0
|
|
80
|
+
SPDX-License-Identifier: Apache-2.0
|
|
81
|
+
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
"use strict";
|
|
85
|
+
|
|
86
|
+
// Default values:
|
|
87
|
+
const JsonEditorDefaults = {
|
|
88
|
+
key: "0", // Your name for this object: read it in a callback (self.key)
|
|
89
|
+
left: 0,
|
|
90
|
+
top: 0,
|
|
91
|
+
width: "100%",
|
|
92
|
+
height: 400,
|
|
93
|
+
value: [], // A list or an object (or its JSON text). The editor reads it into its rows: the value of the page does not change.
|
|
94
|
+
titleText: "", // The name of the value at the start of the path. "": texts.list or texts.object
|
|
95
|
+
noteText: "", // A soft line under the title (Ex: what a row holds)
|
|
96
|
+
columns: [], // [A COLUMN] (See A COLUMN above): the columns of an empty list; the kind and the choices of a column
|
|
97
|
+
showTitleBar: "auto", // "auto": when there is a title, a note or a table opened in a cell. 1: always
|
|
98
|
+
showHint: 1, // The keys of the cells at the bottom
|
|
99
|
+
pictureBase: "", // The address the relative pictures of the cells are read from. "": the page
|
|
100
|
+
ariaLabel: "", // "": the titleText
|
|
101
|
+
texts: {
|
|
102
|
+
list: "List",
|
|
103
|
+
object: "Object",
|
|
104
|
+
name: "name",
|
|
105
|
+
value: "value",
|
|
106
|
+
addRow: "Row",
|
|
107
|
+
addColumn: "Column",
|
|
108
|
+
addRowAtEnd: "Add a row",
|
|
109
|
+
search: "Search the rows",
|
|
110
|
+
table: "Table",
|
|
111
|
+
json: "JSON",
|
|
112
|
+
copyJson: "Copy as JSON",
|
|
113
|
+
duplicate: "Duplicate",
|
|
114
|
+
duplicateRows: "Duplicate {count} rows",
|
|
115
|
+
moveUp: "Move up",
|
|
116
|
+
moveDown: "Move down",
|
|
117
|
+
delete: "Delete",
|
|
118
|
+
deleteRows: "Delete {count} rows",
|
|
119
|
+
undo: "Undo",
|
|
120
|
+
redo: "Redo",
|
|
121
|
+
addAbove: "Add a row above",
|
|
122
|
+
addBelow: "Add a row below",
|
|
123
|
+
sortUp: "Sort A → Z",
|
|
124
|
+
sortDown: "Sort Z → A",
|
|
125
|
+
firstOrder: "Back to the first order",
|
|
126
|
+
rename: "Rename",
|
|
127
|
+
type: "Type",
|
|
128
|
+
moveLeft: "Move left",
|
|
129
|
+
moveRight: "Move right",
|
|
130
|
+
deleteColumn: "Delete the column",
|
|
131
|
+
columnMenu: "The menu of the column",
|
|
132
|
+
sortHint: "{title} · a click sorts",
|
|
133
|
+
newName: "A new name…",
|
|
134
|
+
columnName: "The name of the column",
|
|
135
|
+
newColumnName: "The name of the new column",
|
|
136
|
+
fromValues: "From its values",
|
|
137
|
+
kindText: "Text",
|
|
138
|
+
kindNumber: "Number",
|
|
139
|
+
kindFlag: "On / off",
|
|
140
|
+
kindColor: "Color",
|
|
141
|
+
kindPicture: "Picture",
|
|
142
|
+
empty: "(empty)",
|
|
143
|
+
function: "ƒ function",
|
|
144
|
+
pictureData: "(in the code: {kind}, {size} KB)",
|
|
145
|
+
rowOfPath: "row {number} · {column}",
|
|
146
|
+
rows: "{count} rows",
|
|
147
|
+
oneRow: "1 row",
|
|
148
|
+
shownRows: "{shown} of {count} rows",
|
|
149
|
+
columns: "{count} columns",
|
|
150
|
+
oneColumn: "1 column",
|
|
151
|
+
hint: "Tab: next cell · Enter: next row · Cmd/Ctrl+V: rows from a spreadsheet or JSON",
|
|
152
|
+
columnExists: "The column \"{name}\" is already there.",
|
|
153
|
+
lastColumn: "A table keeps one column at least.",
|
|
154
|
+
clearSearch: "Clear the search to move a row.",
|
|
155
|
+
pasteList: "Paste a list into a list.",
|
|
156
|
+
rowsAdded: "{count} rows added.",
|
|
157
|
+
oneRowAdded: "1 row added.",
|
|
158
|
+
linesPasted: "{count} lines pasted.",
|
|
159
|
+
oneLinePasted: "1 line pasted.",
|
|
160
|
+
rowsCopied: "{count} rows were copied.",
|
|
161
|
+
oneRowCopied: "A row was copied.",
|
|
162
|
+
jsonCopied: "The JSON was copied.",
|
|
163
|
+
notJson: "Not JSON: {error}",
|
|
164
|
+
objectNeeded: "An object is needed: { ... }",
|
|
165
|
+
listNeeded: "A list is needed: [ ... ]",
|
|
166
|
+
noName: "A row has no name.",
|
|
167
|
+
sameName: "The name \"{name}\" is used twice.",
|
|
168
|
+
cellValue: "The value of the cell",
|
|
169
|
+
cancel: "Cancel", // The window (JsonEditor.open)
|
|
170
|
+
apply: "Apply",
|
|
171
|
+
discard: "Discard the changes?",
|
|
172
|
+
close: "Close",
|
|
173
|
+
},
|
|
174
|
+
onChange: function (self, value) { }, // The value changed in the table: a list or an object. (Not at create time, not with setValue())
|
|
175
|
+
onPickColor: null, // function (self, cell) { }: your own color picker for a color cell (See A CELL above). null: the one of the browser
|
|
176
|
+
onPickPicture: null, // function (self, cell) { }: a list of pictures for a picture cell (a click on its picture). null: the address is written
|
|
177
|
+
colorOf: null, // function (value, self) { return a color }: the color a swatch paints, when only the page can read it (Ex: a token of another document). null: the value
|
|
178
|
+
optionsOf: null, // function (key, value, self) { return [choices] or null }: the choices of a text of this name (a column, a row of an object), besides columns
|
|
179
|
+
styleName: "light", // "light", "dark", "minimal", "minimalDark" or a name added to JsonEditor.styles
|
|
180
|
+
style: { // Light style package (default)
|
|
181
|
+
box: { color: "#FFFFFF", border: 1, borderColor: Black(0.12), round: 10 },
|
|
182
|
+
colors: {
|
|
183
|
+
text: "#1F2328", // The values
|
|
184
|
+
headText: "#1F2328", // The titles of the columns
|
|
185
|
+
soft: "#6B6F76", // The note, the counts
|
|
186
|
+
dim: "#9EA2A8", // The numbers of the rows, an empty value, a function
|
|
187
|
+
icon: "#5A5E66", // The icons of the toolbar
|
|
188
|
+
line: "#EBEBEA", // The lines of the bars
|
|
189
|
+
rowLine: "#EBEBEA", // Between the rows
|
|
190
|
+
columnLine: "#EBEBEA", // Between the columns
|
|
191
|
+
headLine: "#DEDEDC", // Under the titles of the columns
|
|
192
|
+
strongLine: "#CFCFCC", // A switch that is off, the chosen part of Table | JSON
|
|
193
|
+
ground: "#FFFFFF", // Behind the rows (the numbers of the rows take it: they stay over the cells)
|
|
194
|
+
head: "#F7F7F5", // The titles of the columns
|
|
195
|
+
field: "#F0F0EE", // A button, the search
|
|
196
|
+
fieldHover: "#E6E6E3",
|
|
197
|
+
fieldLine: "", // A line around a button and around Table | JSON. "": none
|
|
198
|
+
fieldLineHover: "",
|
|
199
|
+
hover: "#F3F6F9", // A row under the mouse
|
|
200
|
+
selected: "#E1ECF9", // A chosen row
|
|
201
|
+
segment: "#FFFFFF", // The chosen part of Table | JSON
|
|
202
|
+
accent: "#0C8CE9", // The ring of a cell, Apply, a switch that is on
|
|
203
|
+
accentHover: "#2E9DEC",
|
|
204
|
+
accentText: "#0B6CB5", // + Add a row, the path, a list in a cell, the messages
|
|
205
|
+
onAccent: "#FFFFFF",
|
|
206
|
+
focus: "#0C8CE9", // The ring of the keyboard
|
|
207
|
+
editor: "#FFFFFF", // The field of a cell
|
|
208
|
+
knob: "#FFFFFF", // The knob of a switch that is off (on: onAccent)
|
|
209
|
+
swatchLine: "rgba(0, 0, 0, 0.15)",
|
|
210
|
+
thumb: "#8A8A8A", // Behind a small picture: black and white icons both show on it
|
|
211
|
+
scrollBar: Black(0.2),
|
|
212
|
+
scrollBarHover: Black(0.35),
|
|
213
|
+
placeholder: Black(0.38),
|
|
214
|
+
},
|
|
215
|
+
fontSize: 12,
|
|
216
|
+
headFontSize: 12, // The titles of the columns
|
|
217
|
+
titleFontSize: 14,
|
|
218
|
+
codeFontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace',
|
|
219
|
+
rowHeight: 30,
|
|
220
|
+
headHeight: 32,
|
|
221
|
+
popupShadow: "0 6px 18px rgba(0, 0, 0, 0.18)", // The field of a new name
|
|
222
|
+
searchStyleName: "ozden", // The package of the search (SearchInput.styles)
|
|
223
|
+
search: { // Over that package
|
|
224
|
+
height: 28,
|
|
225
|
+
fontSize: 12,
|
|
226
|
+
searchIconSize: 14,
|
|
227
|
+
searchIconLeft: 8,
|
|
228
|
+
searchIconGap: 6,
|
|
229
|
+
clearIconRight: 8,
|
|
230
|
+
round: 6,
|
|
231
|
+
color: "#F0F0EE",
|
|
232
|
+
border: 0,
|
|
233
|
+
borderBottomStyle: "none",
|
|
234
|
+
textOffsetY: 0,
|
|
235
|
+
clearIconOffsetY: 0,
|
|
236
|
+
},
|
|
237
|
+
menuStyleName: "light", // The package of the menus (ContextMenu.styles)
|
|
238
|
+
window: { // JsonEditor.open()
|
|
239
|
+
coverColor: Black(0.35),
|
|
240
|
+
shadow: "0 20px 60px rgba(0, 0, 0, 0.25)",
|
|
241
|
+
zIndex: 3900, // WHY: under a Dialog (4000): a dialog the page opens from the window comes over it
|
|
242
|
+
margin: 24,
|
|
243
|
+
narrowMargin: 8, // On a page narrower than 600 px
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const JsonEditor = function (params = {}) {
|
|
249
|
+
|
|
250
|
+
// Merge style package: params.style > JsonEditor.styles[styleName] > JsonEditorDefaults.style (light)
|
|
251
|
+
basic.applyStylePackage(params, JsonEditor.styles, JsonEditorDefaults);
|
|
252
|
+
|
|
253
|
+
// Merge params:
|
|
254
|
+
mergeIntoIfMissing(params, JsonEditorDefaults);
|
|
255
|
+
|
|
256
|
+
// Edit params:
|
|
257
|
+
params.value = JsonEditor.rootValueOf(params.value);
|
|
258
|
+
params.columns = (Array.isArray(params.columns)) ? params.columns.slice() : [];
|
|
259
|
+
|
|
260
|
+
// BOX: Component container
|
|
261
|
+
const box = startObject(params);
|
|
262
|
+
|
|
263
|
+
// *** PRIVATE VARIABLES:
|
|
264
|
+
const _s = box.style;
|
|
265
|
+
const C = _s.colors;
|
|
266
|
+
const SIZE = JsonEditor.SIZES;
|
|
267
|
+
const PLAIN_KEY = JsonEditor.PLAIN_KEY;
|
|
268
|
+
const NAME_KEY = JsonEditor.NAME_KEY;
|
|
269
|
+
const VALUE_KEY = JsonEditor.VALUE_KEY;
|
|
270
|
+
const ROW = _s.rowHeight;
|
|
271
|
+
const HEAD = _s.headHeight;
|
|
272
|
+
const GUTTER = SIZE.HANDLE_WIDTH + SIZE.NUMBER_WIDTH;
|
|
273
|
+
const LETTER_WIDTH = _s.fontSize * 0.58; // About the width of a letter of a cell: the first width of a column
|
|
274
|
+
const looks = JsonEditor.looksOf(_s);
|
|
275
|
+
|
|
276
|
+
const levels = []; // The table of the value and the cells opened in it: { name, rows, columns, undoList... }
|
|
277
|
+
let level = null; // The table in sight
|
|
278
|
+
let viewRows = []; // The rows of the level after the search
|
|
279
|
+
let searchText = "";
|
|
280
|
+
let editing = null; // The cell being written: { rowId, columnKey, startText }
|
|
281
|
+
let isJsonView = false;
|
|
282
|
+
let jsonStartText = "";
|
|
283
|
+
let hoveredRowId = null;
|
|
284
|
+
let drawnFirst = -1;
|
|
285
|
+
let drawnLast = -1;
|
|
286
|
+
let rowsLayer = null;
|
|
287
|
+
let rowViews = new Map(); // The rows in sight: id -> { box, gutter, end }
|
|
288
|
+
let dragRow = null;
|
|
289
|
+
let menu = null;
|
|
290
|
+
|
|
291
|
+
// The parts of the view
|
|
292
|
+
let groupPath = null;
|
|
293
|
+
let lblNote = null;
|
|
294
|
+
let headBox = null;
|
|
295
|
+
let headInner = null;
|
|
296
|
+
let scroller = null;
|
|
297
|
+
let content = null;
|
|
298
|
+
let activeRing = null;
|
|
299
|
+
let dropLine = null;
|
|
300
|
+
let editorBox = null;
|
|
301
|
+
let editorArea = null;
|
|
302
|
+
let colorInput = null;
|
|
303
|
+
let jsonBox = null;
|
|
304
|
+
let jsonArea = null;
|
|
305
|
+
let lblInfo = null;
|
|
306
|
+
let lblStatus = null;
|
|
307
|
+
let searchInput = null;
|
|
308
|
+
let viewSwitch = null;
|
|
309
|
+
let viewSwitchHolder = null;
|
|
310
|
+
let btnAddColumn = null;
|
|
311
|
+
let btnDuplicate = null;
|
|
312
|
+
let btnDelete = null;
|
|
313
|
+
let btnUp = null;
|
|
314
|
+
let btnDown = null;
|
|
315
|
+
let btnUndo = null;
|
|
316
|
+
let btnRedo = null;
|
|
317
|
+
let tableTools = []; // The parts of the toolbar that the JSON view hides
|
|
318
|
+
|
|
319
|
+
// *** PUBLIC VARIABLES:
|
|
320
|
+
box.errorText = ""; // Why the table can not make a value now (a name used twice...). "": it can
|
|
321
|
+
box.isChanged = 0; // 1: something was changed since the value was given (create, setValue)
|
|
322
|
+
// NOTE: Default values are also public variables. (box.value, box.titleText, box.columns...)
|
|
323
|
+
// NOTE: box.titleBar, box.titleRow, box.toolbar and box.footer take objects of the page: createIn(editor.footer, ...)
|
|
324
|
+
|
|
325
|
+
// *** PRIVATE FUNCTIONS:
|
|
326
|
+
|
|
327
|
+
// A text of texts with its values: textOf("rows", { count: 3 }) -> "3 rows"
|
|
328
|
+
const textOf = function (name, values) {
|
|
329
|
+
return JsonEditor.fillText(box.texts[name], values);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const messageOf = function (error) {
|
|
333
|
+
if (error.textKey) return textOf(error.textKey, { name: error.valueName });
|
|
334
|
+
return error.message;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// What the page said about a column of the table in sight (columns; a cell opened in it: the columns of its column).
|
|
338
|
+
const hintOf = function (key) {
|
|
339
|
+
return level.hints.find(function (hint) { return hint.key === key; }) || null;
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const titleOf = function (column) {
|
|
343
|
+
if (column.key === PLAIN_KEY || column.key === VALUE_KEY) return box.texts.value;
|
|
344
|
+
if (column.key === NAME_KEY) return box.texts.name;
|
|
345
|
+
if (/^\d+$/.test(column.key)) return String(Number(column.key) + 1);
|
|
346
|
+
return column.key;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// The text a cell shows.
|
|
350
|
+
const shownTextOf = function (value) {
|
|
351
|
+
if (typeof value === "function") return box.texts.function;
|
|
352
|
+
return JsonEditor.textOfValue(value);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// A point of the mouse in an element, in the px of the page (page.fit() zooms the page).
|
|
356
|
+
const pointIn = function (event, elem) {
|
|
357
|
+
const rect = elem.getBoundingClientRect();
|
|
358
|
+
return { x: withPageZoom(event.clientX - rect.left), y: withPageZoom(event.clientY - rect.top) };
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// *** THE LEVELS: a cell that holds a list or an object opens as a table of its own, and goes back into its cell.
|
|
362
|
+
|
|
363
|
+
// hints: [A COLUMN]. openedCell: { rowId, columnKey } of the cell in the level under it. (null: the value of the page)
|
|
364
|
+
const openLevel = function (name, value, hints, openedCell) {
|
|
365
|
+
const data = JsonEditor.levelOf(value, hints);
|
|
366
|
+
data.name = name;
|
|
367
|
+
data.hints = hints;
|
|
368
|
+
data.openedCell = openedCell;
|
|
369
|
+
data.undoList = [];
|
|
370
|
+
data.redoList = [];
|
|
371
|
+
data.selected = new Set();
|
|
372
|
+
data.active = null; // { rowId, columnKey }
|
|
373
|
+
data.sort = null; // { key, direction, order: the ids before the sort }
|
|
374
|
+
data.scrollTop = 0;
|
|
375
|
+
data.scrollLeft = 0;
|
|
376
|
+
levels.push(data);
|
|
377
|
+
level = data;
|
|
378
|
+
fitColumns();
|
|
379
|
+
showLevel();
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// Back to the level at this place: each level goes into its cell.
|
|
383
|
+
const closeLevelsTo = function (index) {
|
|
384
|
+
if (!commitEdit()) return false;
|
|
385
|
+
while (levels.length - 1 > index) {
|
|
386
|
+
let value;
|
|
387
|
+
try {
|
|
388
|
+
value = JsonEditor.valueOfLevel(level);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
showStatus(messageOf(error));
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
const finished = levels.pop();
|
|
394
|
+
level = levels[levels.length - 1];
|
|
395
|
+
if (finished.undoList.length) writeIntoCell(finished.openedCell, value);
|
|
396
|
+
}
|
|
397
|
+
showLevel();
|
|
398
|
+
return true;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
// The value of a closed level goes into its cell: one step of the undo of the level under it.
|
|
402
|
+
const writeIntoCell = function (cell, value) {
|
|
403
|
+
level.undoList.push(copyOfLevel(level));
|
|
404
|
+
level.redoList = [];
|
|
405
|
+
const row = rowById(cell.rowId);
|
|
406
|
+
if (row) row.cells[cell.columnKey] = value;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// The value of the whole table. The levels that are open go into their cells here (when they close, for real).
|
|
410
|
+
// Throws when a table can not make a value (a name used twice).
|
|
411
|
+
const readValue = function () {
|
|
412
|
+
const top = levels[levels.length - 1];
|
|
413
|
+
let value = JsonEditor.valueOfLevel(top);
|
|
414
|
+
let isChangedAbove = (top.undoList.length > 0);
|
|
415
|
+
for (let i = levels.length - 1; i > 0; i--) {
|
|
416
|
+
const below = levels[i - 1];
|
|
417
|
+
const openCell = { rowId: levels[i].openedCell.rowId, columnKey: levels[i].openedCell.columnKey, value: value };
|
|
418
|
+
value = JsonEditor.valueOfLevel(below, (isChangedAbove) ? openCell : null);
|
|
419
|
+
isChangedAbove = isChangedAbove || below.undoList.length > 0;
|
|
420
|
+
}
|
|
421
|
+
return value;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// After a change of the data: the value goes to the page. A table that can not make a value says why (not while a
|
|
425
|
+
// cell is being written: a new row of an object has no name until its name is written).
|
|
426
|
+
const reportChange = function () {
|
|
427
|
+
let value;
|
|
428
|
+
try {
|
|
429
|
+
value = readValue();
|
|
430
|
+
} catch (error) {
|
|
431
|
+
box.errorText = messageOf(error);
|
|
432
|
+
if (!editing) showStatus(box.errorText);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
box.errorText = "";
|
|
436
|
+
box.value = value;
|
|
437
|
+
box.onChange(box, value);
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const showLevel = function () {
|
|
441
|
+
searchText = "";
|
|
442
|
+
searchInput.setValue("", 1);
|
|
443
|
+
drawPath();
|
|
444
|
+
drawHead();
|
|
445
|
+
refreshViewRows();
|
|
446
|
+
scroller.elem.scrollTop = level.scrollTop;
|
|
447
|
+
scroller.elem.scrollLeft = level.scrollLeft;
|
|
448
|
+
syncHead();
|
|
449
|
+
drawRows(true);
|
|
450
|
+
placeActive();
|
|
451
|
+
paintButtons();
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// *** HISTORY: inside the editor. A step is a copy of the rows and the columns.
|
|
455
|
+
|
|
456
|
+
const copyOfLevel = function (source) {
|
|
457
|
+
return {
|
|
458
|
+
rows: source.rows.map(function (row) { return { id: row.id, shape: row.shape, cells: Object.assign({}, row.cells) }; }),
|
|
459
|
+
columns: source.columns.map(function (column) { return Object.assign({}, column); }),
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
const remember = function () {
|
|
464
|
+
level.undoList.push(copyOfLevel(level));
|
|
465
|
+
if (level.undoList.length > SIZE.HISTORY_LIMIT) level.undoList.shift();
|
|
466
|
+
level.redoList = [];
|
|
467
|
+
box.isChanged = 1;
|
|
468
|
+
showStatus("");
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
const goToStep = function (fromList, toList) {
|
|
472
|
+
if (!fromList.length) return;
|
|
473
|
+
commitEdit();
|
|
474
|
+
toList.push(copyOfLevel(level));
|
|
475
|
+
const step = fromList.pop();
|
|
476
|
+
level.rows = step.rows;
|
|
477
|
+
level.columns = step.columns;
|
|
478
|
+
level.selected.clear();
|
|
479
|
+
level.sort = null;
|
|
480
|
+
drawHead();
|
|
481
|
+
finishChange();
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const undo = function () { goToStep(level.undoList, level.redoList); };
|
|
485
|
+
const redo = function () { goToStep(level.redoList, level.undoList); };
|
|
486
|
+
|
|
487
|
+
// *** ROWS AND COLUMNS:
|
|
488
|
+
|
|
489
|
+
const refreshViewRows = function () {
|
|
490
|
+
const word = searchText.trim().toLowerCase();
|
|
491
|
+
viewRows = level.rows.filter(function (row) {
|
|
492
|
+
if (!word) return true;
|
|
493
|
+
return Object.keys(row.cells).some(function (key) {
|
|
494
|
+
return JsonEditor.textOfValue(row.cells[key]).toLowerCase().indexOf(word) !== -1;
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
const afterRowsChange = function () {
|
|
500
|
+
refreshViewRows();
|
|
501
|
+
drawRows(true);
|
|
502
|
+
placeActive();
|
|
503
|
+
paintButtons();
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
// The data changed: draw it, and give the value to the page.
|
|
507
|
+
const finishChange = function () {
|
|
508
|
+
afterRowsChange();
|
|
509
|
+
reportChange();
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
const rowById = function (id) {
|
|
513
|
+
return level.rows.find(function (row) { return row.id === id; }) || null;
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const columnByKey = function (key) {
|
|
517
|
+
return level.columns.find(function (column) { return column.key === key; }) || null;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
const isIndexTable = function () {
|
|
521
|
+
return level.rows.some(function (row) { return row.shape === "array"; });
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
// A new row has the shape of the table.
|
|
525
|
+
const newRow = function () {
|
|
526
|
+
if (level.isObject) {
|
|
527
|
+
const cells = {};
|
|
528
|
+
cells[NAME_KEY] = "";
|
|
529
|
+
cells[VALUE_KEY] = "";
|
|
530
|
+
return JsonEditor.newRow("pair", cells);
|
|
531
|
+
}
|
|
532
|
+
if (isIndexTable()) return JsonEditor.newRow("array", {});
|
|
533
|
+
const isPlainTable = (level.columns.length === 1 && level.columns[0].key === PLAIN_KEY);
|
|
534
|
+
return JsonEditor.newRow((isPlainTable) ? "plain" : "object", {});
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
// WHY the first cell opens: + is usually followed by the first value of the row.
|
|
538
|
+
const addRow = function (place) {
|
|
539
|
+
commitEdit();
|
|
540
|
+
remember();
|
|
541
|
+
searchText = "";
|
|
542
|
+
searchInput.setValue("", 1);
|
|
543
|
+
const row = newRow();
|
|
544
|
+
const index = (place === undefined) ? level.rows.length : place;
|
|
545
|
+
level.rows.splice(index, 0, row);
|
|
546
|
+
level.selected.clear();
|
|
547
|
+
afterRowsChange();
|
|
548
|
+
const column = level.columns[0];
|
|
549
|
+
setActive(row.id, column.key);
|
|
550
|
+
scrollToActive();
|
|
551
|
+
startEdit(row, column);
|
|
552
|
+
reportChange();
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const selectedRows = function () {
|
|
556
|
+
const list = level.rows.filter(function (row) { return level.selected.has(row.id); });
|
|
557
|
+
if (list.length) return list;
|
|
558
|
+
const active = level.active && rowById(level.active.rowId);
|
|
559
|
+
return (active) ? [active] : [];
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const deleteRows = function (rows) {
|
|
563
|
+
if (!rows.length) return;
|
|
564
|
+
commitEdit();
|
|
565
|
+
remember();
|
|
566
|
+
const ids = new Set(rows.map(function (row) { return row.id; }));
|
|
567
|
+
const firstIndex = level.rows.findIndex(function (row) { return ids.has(row.id); });
|
|
568
|
+
level.rows = level.rows.filter(function (row) { return !ids.has(row.id); });
|
|
569
|
+
level.selected.clear();
|
|
570
|
+
const next = level.rows[Math.min(firstIndex, level.rows.length - 1)];
|
|
571
|
+
level.active = (next && level.active) ? { rowId: next.id, columnKey: level.active.columnKey } : null;
|
|
572
|
+
finishChange();
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const duplicateRows = function (rows) {
|
|
576
|
+
if (!rows.length) return;
|
|
577
|
+
commitEdit();
|
|
578
|
+
remember();
|
|
579
|
+
let lastCopy = null;
|
|
580
|
+
rows.forEach(function (row) {
|
|
581
|
+
lastCopy = JsonEditor.newRow(row.shape, Object.assign({}, row.cells));
|
|
582
|
+
if (level.isObject) lastCopy.cells[NAME_KEY] = String(row.cells[NAME_KEY]) + "Copy";
|
|
583
|
+
level.rows.splice(level.rows.indexOf(row) + 1, 0, lastCopy);
|
|
584
|
+
});
|
|
585
|
+
level.selected.clear();
|
|
586
|
+
if (level.active) level.active.rowId = lastCopy.id;
|
|
587
|
+
finishChange();
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
// direction: -1 up, 1 down. The rows move together and keep their order.
|
|
591
|
+
const moveRows = function (rows, direction) {
|
|
592
|
+
if (!rows.length) return;
|
|
593
|
+
commitEdit();
|
|
594
|
+
const indexes = rows.map(function (row) { return level.rows.indexOf(row); }).sort(function (a, b) { return a - b; });
|
|
595
|
+
const isAtEnd = (direction < 0) ? indexes[0] === 0 : indexes[indexes.length - 1] === level.rows.length - 1;
|
|
596
|
+
if (isAtEnd) return;
|
|
597
|
+
remember();
|
|
598
|
+
const ordered = (direction < 0) ? indexes : indexes.slice().reverse();
|
|
599
|
+
ordered.forEach(function (index) {
|
|
600
|
+
const row = level.rows.splice(index, 1)[0];
|
|
601
|
+
level.rows.splice(index + direction, 0, row);
|
|
602
|
+
});
|
|
603
|
+
level.sort = null;
|
|
604
|
+
drawHead();
|
|
605
|
+
finishChange();
|
|
606
|
+
scrollToActive();
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const setCell = function (row, columnKey, value) {
|
|
610
|
+
if (value === undefined) delete row.cells[columnKey];
|
|
611
|
+
else row.cells[columnKey] = value;
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const addColumn = function (name) {
|
|
615
|
+
const key = String(name).trim();
|
|
616
|
+
if (!key) return;
|
|
617
|
+
if (columnByKey(key)) {
|
|
618
|
+
showStatus(textOf("columnExists", { name: key }));
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
commitEdit();
|
|
622
|
+
remember();
|
|
623
|
+
level.columns.push(JsonEditor.newColumn(key, hintOf(key)));
|
|
624
|
+
fitColumns();
|
|
625
|
+
drawHead();
|
|
626
|
+
afterRowsChange();
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
// A list of lists: the next place. Else a name.
|
|
630
|
+
const addColumnFromButton = function (button) {
|
|
631
|
+
if (level.isObject) return;
|
|
632
|
+
if (isIndexTable()) {
|
|
633
|
+
const used = level.columns.map(function (column) { return Number(column.key); }).filter(function (key) { return !isNaN(key); });
|
|
634
|
+
addColumn(String(Math.max.apply(null, used.concat([-1])) + 1));
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
openColumnChooser(button);
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
const deleteColumn = function (column) {
|
|
641
|
+
if (level.columns.length <= 1) {
|
|
642
|
+
showStatus(box.texts.lastColumn);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
commitEdit();
|
|
646
|
+
remember();
|
|
647
|
+
level.columns = level.columns.filter(function (item) { return item !== column; });
|
|
648
|
+
level.rows.forEach(function (row) { delete row.cells[column.key]; });
|
|
649
|
+
fitColumns();
|
|
650
|
+
drawHead();
|
|
651
|
+
finishChange();
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
// WHY the plain column has its own rename: its rows become objects with that name ("A" -> { text: "A" }).
|
|
655
|
+
const renameColumn = function (column, typedName) {
|
|
656
|
+
const name = String(typedName).trim();
|
|
657
|
+
if (!name || name === column.key) return;
|
|
658
|
+
if (columnByKey(name)) {
|
|
659
|
+
showStatus(textOf("columnExists", { name: name }));
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
commitEdit();
|
|
663
|
+
remember();
|
|
664
|
+
level.rows.forEach(function (row) {
|
|
665
|
+
if (row.shape === "plain") row.shape = "object";
|
|
666
|
+
if (column.key in row.cells) {
|
|
667
|
+
row.cells[name] = row.cells[column.key];
|
|
668
|
+
delete row.cells[column.key];
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
column.key = name;
|
|
672
|
+
drawHead();
|
|
673
|
+
finishChange();
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const moveColumn = function (column, direction) {
|
|
677
|
+
const index = level.columns.indexOf(column);
|
|
678
|
+
const target = index + direction;
|
|
679
|
+
if (target < 0 || target >= level.columns.length) return;
|
|
680
|
+
remember();
|
|
681
|
+
level.columns.splice(index, 1);
|
|
682
|
+
level.columns.splice(target, 0, column);
|
|
683
|
+
drawHead();
|
|
684
|
+
finishChange();
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
// direction: "asc", "desc", or "" (the first order)
|
|
688
|
+
const sortBy = function (column, direction) {
|
|
689
|
+
commitEdit();
|
|
690
|
+
remember();
|
|
691
|
+
const isSameKey = (level.sort && level.sort.key === column.key);
|
|
692
|
+
const firstOrder = (isSameKey) ? level.sort.order : level.rows.map(function (row) { return row.id; });
|
|
693
|
+
const compare = function (a, b) { return JsonEditor.compareValues(a.cells[column.key], b.cells[column.key]); };
|
|
694
|
+
if (direction === "asc") level.rows.sort(compare);
|
|
695
|
+
if (direction === "desc") level.rows.sort(function (a, b) { return compare(b, a); });
|
|
696
|
+
if (direction === "") {
|
|
697
|
+
const placeOf = function (row) {
|
|
698
|
+
const place = firstOrder.indexOf(row.id);
|
|
699
|
+
return (place === -1) ? firstOrder.length : place;
|
|
700
|
+
};
|
|
701
|
+
level.rows.sort(function (a, b) { return placeOf(a) - placeOf(b); });
|
|
702
|
+
}
|
|
703
|
+
level.sort = (direction) ? { key: column.key, direction: direction, order: firstOrder } : null;
|
|
704
|
+
drawHead();
|
|
705
|
+
finishChange();
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
// A click on a title: A-Z, Z-A, the first order.
|
|
709
|
+
const sortNext = function (column) {
|
|
710
|
+
const now = (level.sort && level.sort.key === column.key) ? level.sort.direction : "";
|
|
711
|
+
const next = { "": "asc", asc: "desc", desc: "" };
|
|
712
|
+
sortBy(column, next[now]);
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
// *** THE KIND OF A CELL:
|
|
716
|
+
|
|
717
|
+
const valuesOfColumn = function (column) {
|
|
718
|
+
const values = [];
|
|
719
|
+
level.rows.forEach(function (row) {
|
|
720
|
+
const value = row.cells[column.key];
|
|
721
|
+
if (value !== undefined && value !== null) values.push(value);
|
|
722
|
+
});
|
|
723
|
+
return values;
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
const kindOfColumn = function (column) {
|
|
727
|
+
if (column.chosenKind) return column.chosenKind;
|
|
728
|
+
if (column.options) return "option";
|
|
729
|
+
const values = valuesOfColumn(column);
|
|
730
|
+
const kind = JsonEditor.kindOfColumnValues(column.key, values);
|
|
731
|
+
// WHY only a text: a color, a number or a switch keeps its own editor.
|
|
732
|
+
const isText = (kind === "text" || kind === "auto");
|
|
733
|
+
if (isText && askOptions(column.key, values[0]).length) return "option";
|
|
734
|
+
return kind;
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
// An object: the kind of a value comes from its own name, not from its column.
|
|
738
|
+
const kindOfCell = function (row, column) {
|
|
739
|
+
const value = row.cells[column.key];
|
|
740
|
+
if (typeof value === "function" || JsonEditor.isOtherObject(value)) return "function";
|
|
741
|
+
if (JsonEditor.isNestedValue(value)) return "nested";
|
|
742
|
+
if (column.key === NAME_KEY) return "text";
|
|
743
|
+
if (column.key === VALUE_KEY) return kindOfNamedCell(row);
|
|
744
|
+
return column.kindNow || "text";
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
const kindOfNamedCell = function (row) {
|
|
748
|
+
const name = String(row.cells[NAME_KEY] ?? "");
|
|
749
|
+
const value = row.cells[VALUE_KEY];
|
|
750
|
+
const hint = hintOf(name);
|
|
751
|
+
if (hint && hint.kind) return hint.kind;
|
|
752
|
+
if (hint && hint.options) return "option";
|
|
753
|
+
const kind = JsonEditor.kindOfNamedValue(name, value);
|
|
754
|
+
if (kind === "color" || kind === "picture" || kind === "flag") return kind;
|
|
755
|
+
const isText = (kind === "text" || kind === "any");
|
|
756
|
+
if (isText && askOptions(name, value).length) return "option";
|
|
757
|
+
return (typeof value === "number") ? "number" : "auto";
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
// The choices of a cell: the columns of the page (columns), else optionsOf. [] when it has none.
|
|
761
|
+
const optionsOfCell = function (row, column) {
|
|
762
|
+
const isNamed = (column.key === VALUE_KEY);
|
|
763
|
+
const key = (isNamed) ? String(row.cells[NAME_KEY] ?? "") : column.key;
|
|
764
|
+
const hint = (isNamed) ? hintOf(key) : column;
|
|
765
|
+
if (hint && hint.options) return hint.options;
|
|
766
|
+
return askOptions(key, row.cells[column.key]);
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
// The choices optionsOf gives for a value of this name. [] when there are none (or the name is one of the table).
|
|
770
|
+
const askOptions = function (key, value) {
|
|
771
|
+
if (!box.optionsOf || JsonEditor.isOwnKey(key)) return [];
|
|
772
|
+
const options = box.optionsOf(key, value, box);
|
|
773
|
+
return (Array.isArray(options)) ? options : [];
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
// The color a swatch paints (colorOf: a color only the page can read).
|
|
777
|
+
const paintedColorOf = function (value) {
|
|
778
|
+
const text = String(value ?? "").trim();
|
|
779
|
+
if (!box.colorOf || !text) return text;
|
|
780
|
+
return String(box.colorOf(text, box) || text);
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
// The typed text of a cell -> its value. It keeps the type the cell had when it can.
|
|
784
|
+
const valueOfCellText = function (text, row, column) {
|
|
785
|
+
const old = row.cells[column.key];
|
|
786
|
+
const kind = kindOfCell(row, column);
|
|
787
|
+
const isAlwaysThere = (column.key === NAME_KEY || column.key === VALUE_KEY || column.key === PLAIN_KEY || row.shape === "array");
|
|
788
|
+
if (text === "") return (isAlwaysThere) ? "" : undefined;
|
|
789
|
+
if (column.key === NAME_KEY) return text;
|
|
790
|
+
if (text === "null") return null;
|
|
791
|
+
const number = JsonEditor.numberOfText(text);
|
|
792
|
+
if (typeof old === "number" || kind === "number") return (number !== null) ? number : text;
|
|
793
|
+
if (typeof old === "boolean") {
|
|
794
|
+
if (text === "true" || text === "1") return true;
|
|
795
|
+
if (text === "false" || text === "0") return false;
|
|
796
|
+
}
|
|
797
|
+
if (kind === "flag") {
|
|
798
|
+
if (text === "1" || text === "true") return 1;
|
|
799
|
+
if (text === "0" || text === "false") return 0;
|
|
800
|
+
}
|
|
801
|
+
const isTextBefore = (typeof old === "string" && old !== "");
|
|
802
|
+
if (kind === "auto" && isTextBefore) return text;
|
|
803
|
+
if (kind === "nested" || kind === "auto") return JsonEditor.jsonOrText(text);
|
|
804
|
+
const isOptionNumber = (number !== null && optionsOfCell(row, column).indexOf(number) !== -1);
|
|
805
|
+
if (kind === "option" && isOptionNumber) return number;
|
|
806
|
+
return text;
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
// *** THE WIDTHS OF THE COLUMNS: from what they hold; the free room goes to the columns that were not sized by hand.
|
|
810
|
+
|
|
811
|
+
const measuredWidth = function (column) {
|
|
812
|
+
let longest = titleOf(column).length + 3;
|
|
813
|
+
const kind = kindOfColumn(column);
|
|
814
|
+
level.rows.slice(0, SIZE.MEASURED_ROWS).forEach(function (row) {
|
|
815
|
+
longest = Math.max(longest, shownTextOf(row.cells[column.key]).length);
|
|
816
|
+
});
|
|
817
|
+
const hasIcon = (kind === "color" || kind === "picture" || kind === "option");
|
|
818
|
+
const extra = (hasIcon) ? SIZE.CELL_ICON_ROOM : 0;
|
|
819
|
+
const width = longest * LETTER_WIDTH + SIZE.CELL_TEXT_ROOM + extra;
|
|
820
|
+
return Math.max(SIZE.MIN_COLUMN, Math.min(SIZE.MAX_AUTO_COLUMN, width));
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
const fitColumns = function () {
|
|
824
|
+
const room = Math.max(0, scroller.elem.clientWidth - GUTTER - SIZE.END_WIDTH);
|
|
825
|
+
level.columns.forEach(function (column) {
|
|
826
|
+
column.kindNow = kindOfColumn(column);
|
|
827
|
+
if (!column.isSized) column.width = measuredWidth(column);
|
|
828
|
+
});
|
|
829
|
+
const total = level.columns.reduce(function (sum, column) { return sum + column.width; }, 0);
|
|
830
|
+
const free = level.columns.filter(function (column) { return !column.isSized; });
|
|
831
|
+
if (total < room && free.length) {
|
|
832
|
+
const extra = Math.floor((room - total) / free.length);
|
|
833
|
+
free.forEach(function (column) { column.width += extra; });
|
|
834
|
+
}
|
|
835
|
+
allowSideScroll(total > room);
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
// The bar under the rows only when the columns do not fit.
|
|
839
|
+
// WHY: with the bar always allowed, a narrower table showed it for a moment (its columns were the wider ones) and lost
|
|
840
|
+
// it when they were fitted: the scroller changed its size again in the same frame (WebKit, where a scroll bar
|
|
841
|
+
// takes room: "ResizeObserver loop completed with undelivered notifications").
|
|
842
|
+
const allowSideScroll = function (isWider) {
|
|
843
|
+
scroller.scrollX = (isWider) ? 1 : 0;
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
const totalWidth = function () {
|
|
847
|
+
return GUTTER + SIZE.END_WIDTH + level.columns.reduce(function (sum, column) { return sum + column.width; }, 0);
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
const leftOfColumn = function (columnKey) {
|
|
851
|
+
let left = GUTTER;
|
|
852
|
+
for (let i = 0; i < level.columns.length; i++) {
|
|
853
|
+
if (level.columns[i].key === columnKey) return left;
|
|
854
|
+
left += level.columns[i].width;
|
|
855
|
+
}
|
|
856
|
+
return left;
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
// *** THE HEAD OF THE TABLE:
|
|
860
|
+
|
|
861
|
+
const drawHead = function () {
|
|
862
|
+
level.columns.forEach(function (column) { column.kindNow = kindOfColumn(column); });
|
|
863
|
+
if (headInner) headInner.remove();
|
|
864
|
+
createIn(headBox, function () {
|
|
865
|
+
// GROUP: The titles (they move with the table)
|
|
866
|
+
headInner = HGroup({ left: GUTTER, top: 0, width: totalWidth() - GUTTER, height: HEAD, align: "left center", gap: 0, color: "transparent" });
|
|
867
|
+
headInner.clipContent = 0;
|
|
868
|
+
level.columns.forEach(drawHeadCell);
|
|
869
|
+
endGroup();
|
|
870
|
+
});
|
|
871
|
+
syncHead();
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
const drawHeadCell = function (column) {
|
|
875
|
+
const isSorted = (level.sort && level.sort.key === column.key);
|
|
876
|
+
let arrow = "";
|
|
877
|
+
if (isSorted) arrow = (level.sort.direction === "asc") ? " ↑" : " ↓";
|
|
878
|
+
const title = titleOf(column);
|
|
879
|
+
// GROUP: A title
|
|
880
|
+
const cell = HGroup({ width: column.width, height: HEAD, align: "left center", gap: 0, padding: [SIZE.CELL_PADDING, 0], color: "transparent" });
|
|
881
|
+
cell.shrink = 0;
|
|
882
|
+
cell.css = { borderRight: "1px solid " + C.columnLine, boxSizing: "border-box", position: "relative" };
|
|
883
|
+
cell.clickable = 1;
|
|
884
|
+
cell.cursor = "pointer";
|
|
885
|
+
const lbl = Label({ text: "", height: HEAD, fontSize: _s.headFontSize, textColor: (column.key === PLAIN_KEY) ? C.soft : C.headText, fontFamily: "opensans-bold", ellipsis: 1 });
|
|
886
|
+
lbl.plainText = title + arrow;
|
|
887
|
+
lbl.grow = 1;
|
|
888
|
+
lbl.css = { minWidth: "0px", lineHeight: HEAD + "px" };
|
|
889
|
+
// WHY no menu in an object: its two columns are its names and its values.
|
|
890
|
+
if (!level.isObject) drawColumnMenuButton(column);
|
|
891
|
+
// BOX: The edge that changes the width
|
|
892
|
+
const edge = Box({
|
|
893
|
+
left: column.width - SIZE.COLUMN_EDGE_WIDTH - 1,
|
|
894
|
+
top: 0,
|
|
895
|
+
width: SIZE.COLUMN_EDGE_WIDTH,
|
|
896
|
+
height: HEAD,
|
|
897
|
+
color: "transparent",
|
|
898
|
+
clickable: 1,
|
|
899
|
+
cursor: "col-resize",
|
|
900
|
+
});
|
|
901
|
+
edge.css = { position: "absolute" };
|
|
902
|
+
connectColumnEdge(edge, column);
|
|
903
|
+
endGroup();
|
|
904
|
+
cell.elem.title = (column.note) ? title + ": " + column.note : textOf("sortHint", { title: title });
|
|
905
|
+
cell.on("click", function (self, event) {
|
|
906
|
+
if (event.target === cell.elem || event.target === lbl.elem) sortNext(column);
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
// LABEL: ⌄ at the end of a title: the menu of the column
|
|
911
|
+
const drawColumnMenuButton = function (column) {
|
|
912
|
+
const btn = Label({
|
|
913
|
+
text: JsonEditor.iconOf("chevron", SIZE.ICON),
|
|
914
|
+
width: SIZE.HEAD_BUTTON_WIDTH,
|
|
915
|
+
height: SIZE.HEAD_BUTTON_HEIGHT,
|
|
916
|
+
color: "transparent",
|
|
917
|
+
textColor: C.soft,
|
|
918
|
+
round: 4,
|
|
919
|
+
clickable: 1,
|
|
920
|
+
cursor: "pointer",
|
|
921
|
+
lineHeight: "0",
|
|
922
|
+
});
|
|
923
|
+
btn.shrink = 0;
|
|
924
|
+
btn.css = { display: "flex", alignItems: "center", justifyContent: "center", transition: "background-color 0.12s" };
|
|
925
|
+
btn.role = "button";
|
|
926
|
+
btn.aria = { label: box.texts.columnMenu };
|
|
927
|
+
btn.elem.title = box.texts.columnMenu;
|
|
928
|
+
btn.onHoverChange(function (self, isOver) {
|
|
929
|
+
btn.color = (isOver) ? C.fieldHover : "transparent";
|
|
930
|
+
btn.textColor = (isOver) ? C.text : C.soft;
|
|
931
|
+
});
|
|
932
|
+
btn.on("click", function () { openColumnMenu(column, btn); });
|
|
933
|
+
};
|
|
934
|
+
|
|
935
|
+
// DRAG: the edge of a title. The window gets the moves: the head is drawn again with a new edge.
|
|
936
|
+
const connectColumnEdge = function (edge, column) {
|
|
937
|
+
edge.on("pointerdown", function (self, event) {
|
|
938
|
+
if (event.button !== 0) return;
|
|
939
|
+
event.preventDefault();
|
|
940
|
+
event.stopPropagation();
|
|
941
|
+
const startX = event.clientX;
|
|
942
|
+
const startWidth = column.width;
|
|
943
|
+
const removers = [];
|
|
944
|
+
const onMove = function (self, moveEvent) {
|
|
945
|
+
column.width = Math.max(SIZE.MIN_COLUMN, startWidth + withPageZoom(moveEvent.clientX - startX));
|
|
946
|
+
column.isSized = true;
|
|
947
|
+
allowSideScroll(totalWidth() > scroller.elem.clientWidth);
|
|
948
|
+
drawHead();
|
|
949
|
+
drawRows(true);
|
|
950
|
+
placeActive();
|
|
951
|
+
};
|
|
952
|
+
const onUp = function () {
|
|
953
|
+
removers.forEach(function (removeListener) { removeListener(); });
|
|
954
|
+
};
|
|
955
|
+
removers.push(box.listenTo(window, "pointermove", onMove));
|
|
956
|
+
removers.push(box.listenTo(window, "pointerup", onUp));
|
|
957
|
+
removers.push(box.listenTo(window, "pointercancel", onUp));
|
|
958
|
+
});
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
const syncHead = function () {
|
|
962
|
+
if (!headInner) return;
|
|
963
|
+
headInner.css.transform = "translateX(" + (-scroller.elem.scrollLeft) + "px)";
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
// *** THE ROWS: only the ones in sight are drawn, so a long list stays fast.
|
|
967
|
+
|
|
968
|
+
const drawRows = function (isForced) {
|
|
969
|
+
const scrollTop = scroller.elem.scrollTop;
|
|
970
|
+
const viewHeight = scroller.elem.clientHeight;
|
|
971
|
+
const firstSeen = Math.floor(scrollTop / ROW);
|
|
972
|
+
const lastSeen = Math.min(viewRows.length, Math.ceil((scrollTop + viewHeight) / ROW));
|
|
973
|
+
const isDrawn = (firstSeen >= drawnFirst && lastSeen <= drawnLast);
|
|
974
|
+
if (!isForced && isDrawn) return;
|
|
975
|
+
drawnFirst = Math.max(0, firstSeen - SIZE.ROW_BUFFER);
|
|
976
|
+
drawnLast = Math.min(viewRows.length, lastSeen + SIZE.ROW_BUFFER);
|
|
977
|
+
const width = totalWidth();
|
|
978
|
+
const height = (viewRows.length + 1) * ROW;
|
|
979
|
+
content.width = width;
|
|
980
|
+
content.height = height;
|
|
981
|
+
if (rowsLayer) rowsLayer.remove();
|
|
982
|
+
rowViews = new Map();
|
|
983
|
+
rowsLayer = createIn(content, function () {
|
|
984
|
+
// BOX: The rows in sight
|
|
985
|
+
const layer = startBox(0, 0, width, height, { color: "transparent" });
|
|
986
|
+
layer.clipContent = 0;
|
|
987
|
+
for (let i = drawnFirst; i < drawnLast; i++) drawRow(viewRows[i], i);
|
|
988
|
+
drawAddRow();
|
|
989
|
+
endBox();
|
|
990
|
+
return layer;
|
|
991
|
+
});
|
|
992
|
+
// WHY: the ring, the line and the field of a cell stay over the rows (they were made first).
|
|
993
|
+
rowsLayer.zIndex = 0;
|
|
994
|
+
paintCounts();
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
const drawRow = function (row, viewIndex) {
|
|
998
|
+
const view = {};
|
|
999
|
+
// GROUP: A row
|
|
1000
|
+
view.box = HGroup({ left: 0, top: viewIndex * ROW, width: totalWidth(), height: ROW, align: "left center", gap: 0, color: "transparent" });
|
|
1001
|
+
view.box.clipContent = 0;
|
|
1002
|
+
view.box.css = { borderBottom: "1px solid " + C.rowLine, boxSizing: "border-box" };
|
|
1003
|
+
view.gutter = drawGutter(row);
|
|
1004
|
+
level.columns.forEach(function (column) { drawCell(row, column); });
|
|
1005
|
+
view.end = drawRowEnd();
|
|
1006
|
+
endGroup();
|
|
1007
|
+
rowViews.set(row.id, view);
|
|
1008
|
+
paintRow(row.id);
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
// WHY the gutter and the end have the color of the row: they stay over the cells when the table moves to the side.
|
|
1012
|
+
const paintRow = function (rowId) {
|
|
1013
|
+
const view = rowViews.get(rowId);
|
|
1014
|
+
if (!view) return;
|
|
1015
|
+
const isHovered = (hoveredRowId === rowId);
|
|
1016
|
+
let back = "transparent";
|
|
1017
|
+
if (isHovered) back = C.hover;
|
|
1018
|
+
if (level.selected.has(rowId)) back = C.selected;
|
|
1019
|
+
const ground = (back === "transparent") ? C.ground : back;
|
|
1020
|
+
view.box.color = back;
|
|
1021
|
+
view.gutter.color = ground;
|
|
1022
|
+
view.end.color = ground;
|
|
1023
|
+
view.end.text = (isHovered) ? JsonEditor.iconOf("trash", SIZE.ICON) : "";
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
const paintRows = function () {
|
|
1027
|
+
rowViews.forEach(function (view, rowId) { paintRow(rowId); });
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
const drawGutter = function (row) {
|
|
1031
|
+
const place = level.rows.indexOf(row) + 1;
|
|
1032
|
+
// GROUP: The handle and the number. WHY sticky: they stay in sight when the table scrolls to the side.
|
|
1033
|
+
const gutter = HGroup({ width: GUTTER, height: ROW - 1, align: "left center", gap: 0, color: C.ground });
|
|
1034
|
+
gutter.shrink = 0;
|
|
1035
|
+
gutter.clipContent = 0;
|
|
1036
|
+
gutter.css = { position: "sticky", left: "0px", zIndex: "1" };
|
|
1037
|
+
const handle = Label({ text: JsonEditor.iconOf("dots", SIZE.ICON), width: SIZE.HANDLE_WIDTH, height: ROW - 1, textColor: C.dim, lineHeight: "0" });
|
|
1038
|
+
handle.shrink = 0;
|
|
1039
|
+
handle.css = { display: "flex", alignItems: "center", justifyContent: "center" };
|
|
1040
|
+
const lblNumber = Label({ text: String(place), width: SIZE.NUMBER_WIDTH - SIZE.NUMBER_SPACE, height: ROW - 1, fontSize: _s.fontSize - 1, textColor: C.dim });
|
|
1041
|
+
lblNumber.shrink = 0;
|
|
1042
|
+
lblNumber.css = { lineHeight: (ROW - 1) + "px", textAlign: "right" };
|
|
1043
|
+
endGroup();
|
|
1044
|
+
return gutter;
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
const drawRowEnd = function () {
|
|
1048
|
+
const end = Label({ text: "", width: SIZE.END_WIDTH, height: ROW - 1, color: C.ground, textColor: C.soft, lineHeight: "0" });
|
|
1049
|
+
end.shrink = 0;
|
|
1050
|
+
end.css = { display: "flex", alignItems: "center", justifyContent: "center", position: "sticky", right: "0px", zIndex: "1" };
|
|
1051
|
+
return end;
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
// GROUP: A cell. It does not take the mouse: the rows read the place of a click (hitOf).
|
|
1055
|
+
const drawCell = function (row, column) {
|
|
1056
|
+
const kind = kindOfCell(row, column);
|
|
1057
|
+
const value = row.cells[column.key];
|
|
1058
|
+
const cell = HGroup({ width: column.width, height: ROW - 1, align: "left center", gap: SIZE.CELL_GAP, padding: [SIZE.CELL_PADDING, 0], color: "transparent" });
|
|
1059
|
+
cell.shrink = 0;
|
|
1060
|
+
cell.css = { borderRight: "1px solid " + C.columnLine, boxSizing: "border-box" };
|
|
1061
|
+
if (kind === "flag" && value !== undefined && value !== null) drawSwitch(basic.isOn(value));
|
|
1062
|
+
if (kind === "color") drawSwatch(value);
|
|
1063
|
+
if (kind === "picture") drawThumb(value);
|
|
1064
|
+
drawCellText(kind, value);
|
|
1065
|
+
if (kind === "option") drawCellIcon("chevron");
|
|
1066
|
+
if (kind === "nested") drawCellIcon("table");
|
|
1067
|
+
endGroup();
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1070
|
+
// BOX: a small switch
|
|
1071
|
+
const drawSwitch = function (isOn) {
|
|
1072
|
+
const knobLeft = (isOn) ? SIZE.SWITCH_WIDTH - SIZE.SWITCH_KNOB - SIZE.SWITCH_SPACE : SIZE.SWITCH_SPACE;
|
|
1073
|
+
const toggle = startBox({ width: SIZE.SWITCH_WIDTH, height: SIZE.SWITCH_HEIGHT, round: SIZE.SWITCH_HEIGHT / 2, color: (isOn) ? C.accent : C.strongLine });
|
|
1074
|
+
toggle.shrink = 0;
|
|
1075
|
+
toggle.css = { position: "relative", left: "0px", top: "0px" };
|
|
1076
|
+
Box({ left: knobLeft, top: SIZE.SWITCH_SPACE, width: SIZE.SWITCH_KNOB, height: SIZE.SWITCH_KNOB, round: SIZE.SWITCH_KNOB / 2, color: (isOn) ? C.onAccent : C.knob });
|
|
1077
|
+
endBox();
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
// BOX: a color over a checkerboard (a transparent color shows it)
|
|
1081
|
+
const drawSwatch = function (value) {
|
|
1082
|
+
const color = paintedColorOf(value) || "transparent";
|
|
1083
|
+
const swatch = Box({ width: SIZE.SWATCH_SIZE, height: SIZE.SWATCH_SIZE, round: 3, color: "transparent" });
|
|
1084
|
+
swatch.shrink = 0;
|
|
1085
|
+
swatch.css = { position: "relative", left: "0px", top: "0px" };
|
|
1086
|
+
swatch.css.background = "linear-gradient(" + color + ", " + color + "), " + JsonEditor.CHECKER;
|
|
1087
|
+
swatch.boxShadow = "inset 0 0 0 1px " + C.swatchLine;
|
|
1088
|
+
};
|
|
1089
|
+
|
|
1090
|
+
// BOX: a small picture
|
|
1091
|
+
const drawThumb = function (value) {
|
|
1092
|
+
const address = JsonEditor.pictureAddress(value, box.pictureBase);
|
|
1093
|
+
const thumb = Box({ width: SIZE.SWATCH_SIZE, height: SIZE.SWATCH_SIZE, round: 3, color: C.thumb });
|
|
1094
|
+
thumb.shrink = 0;
|
|
1095
|
+
thumb.css = { position: "relative", left: "0px", top: "0px", backgroundSize: "contain", backgroundPosition: "center", backgroundRepeat: "no-repeat" };
|
|
1096
|
+
if (address) thumb.css.backgroundImage = 'url("' + address.replace(/"/g, "%22") + '")';
|
|
1097
|
+
};
|
|
1098
|
+
|
|
1099
|
+
const drawCellText = function (kind, value) {
|
|
1100
|
+
const lbl = Label({ text: "", height: ROW - 1, fontSize: _s.fontSize, textColor: C.text, ellipsis: 1 });
|
|
1101
|
+
lbl.grow = 1;
|
|
1102
|
+
lbl.css = { minWidth: "0px", lineHeight: (ROW - 1) + "px" };
|
|
1103
|
+
let text = shownTextOf(value);
|
|
1104
|
+
if (value === null || kind === "function") lbl.textColor = C.dim;
|
|
1105
|
+
if (kind === "picture") text = JsonEditor.shortPicture(text, box.texts.pictureData);
|
|
1106
|
+
if (kind === "nested") {
|
|
1107
|
+
text = JsonEditor.countTextOf(value);
|
|
1108
|
+
lbl.textColor = C.accentText;
|
|
1109
|
+
}
|
|
1110
|
+
lbl.plainText = text;
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1113
|
+
const drawCellIcon = function (iconName) {
|
|
1114
|
+
const icon = Label({ text: JsonEditor.iconOf(iconName, SIZE.ICON), width: SIZE.ICON, height: ROW - 1, textColor: C.dim, lineHeight: "0" });
|
|
1115
|
+
icon.shrink = 0;
|
|
1116
|
+
icon.css = { display: "flex", alignItems: "center" };
|
|
1117
|
+
};
|
|
1118
|
+
|
|
1119
|
+
// GROUP: + at the end of the rows (it stays in sight when the table moves to the side, as the numbers of the rows)
|
|
1120
|
+
const drawAddRow = function () {
|
|
1121
|
+
const row = HGroup({ left: 0, top: viewRows.length * ROW, width: totalWidth(), height: ROW, align: "left center", color: "transparent" });
|
|
1122
|
+
row.clipContent = 0;
|
|
1123
|
+
const lbl = Label({ text: "", height: ROW, fontSize: _s.fontSize, textColor: C.accentText });
|
|
1124
|
+
lbl.plainText = (searchText.trim()) ? "" : "+ " + box.texts.addRowAtEnd;
|
|
1125
|
+
lbl.css = { position: "sticky", left: "0px", paddingLeft: SIZE.ADD_ROW_SPACE + "px", lineHeight: ROW + "px" };
|
|
1126
|
+
endGroup();
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
const paintCounts = function () {
|
|
1130
|
+
const rowCount = level.rows.length;
|
|
1131
|
+
const columnCount = level.columns.length;
|
|
1132
|
+
let rowsText = (rowCount === 1) ? box.texts.oneRow : textOf("rows", { count: rowCount });
|
|
1133
|
+
if (searchText.trim()) rowsText = textOf("shownRows", { shown: viewRows.length, count: rowCount });
|
|
1134
|
+
const columnsText = (columnCount === 1) ? box.texts.oneColumn : textOf("columns", { count: columnCount });
|
|
1135
|
+
lblInfo.plainText = rowsText + " · " + columnsText;
|
|
1136
|
+
};
|
|
1137
|
+
|
|
1138
|
+
// *** THE ACTIVE CELL: a ring on it. Its text is written in a field over it.
|
|
1139
|
+
|
|
1140
|
+
const setActive = function (rowId, columnKey) {
|
|
1141
|
+
level.active = { rowId: rowId, columnKey: columnKey };
|
|
1142
|
+
placeActive();
|
|
1143
|
+
paintButtons();
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
const viewIndexOfActive = function () {
|
|
1147
|
+
const active = level.active;
|
|
1148
|
+
if (!active) return -1;
|
|
1149
|
+
return viewRows.findIndex(function (row) { return row.id === active.rowId; });
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
const placeActive = function () {
|
|
1153
|
+
const viewIndex = viewIndexOfActive();
|
|
1154
|
+
const column = (level.active) ? columnByKey(level.active.columnKey) : null;
|
|
1155
|
+
if (viewIndex === -1 || !column) {
|
|
1156
|
+
activeRing.visible = 0;
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
activeRing.left = leftOfColumn(column.key);
|
|
1160
|
+
activeRing.top = viewIndex * ROW;
|
|
1161
|
+
activeRing.width = column.width;
|
|
1162
|
+
activeRing.height = ROW;
|
|
1163
|
+
activeRing.visible = 1;
|
|
1164
|
+
};
|
|
1165
|
+
|
|
1166
|
+
const scrollToActive = function () {
|
|
1167
|
+
const viewIndex = viewIndexOfActive();
|
|
1168
|
+
const column = (level.active) ? columnByKey(level.active.columnKey) : null;
|
|
1169
|
+
if (viewIndex === -1 || !column) return;
|
|
1170
|
+
const view = scroller.elem;
|
|
1171
|
+
const top = viewIndex * ROW;
|
|
1172
|
+
if (top < view.scrollTop) view.scrollTop = top;
|
|
1173
|
+
if (top + ROW > view.scrollTop + view.clientHeight) view.scrollTop = top + ROW - view.clientHeight;
|
|
1174
|
+
const left = leftOfColumn(column.key);
|
|
1175
|
+
const right = left + column.width + SIZE.END_WIDTH;
|
|
1176
|
+
if (left - GUTTER < view.scrollLeft) view.scrollLeft = left - GUTTER;
|
|
1177
|
+
if (right > view.scrollLeft + view.clientWidth) view.scrollLeft = right - view.clientWidth;
|
|
1178
|
+
drawRows(false);
|
|
1179
|
+
};
|
|
1180
|
+
|
|
1181
|
+
// A step across the rows and the columns of what is shown. Past the end of a row: the next row.
|
|
1182
|
+
const moveActive = function (rowStep, columnStep) {
|
|
1183
|
+
const active = level.active;
|
|
1184
|
+
if (!active || !viewRows.length) return false;
|
|
1185
|
+
let nextRow = viewIndexOfActive() + rowStep;
|
|
1186
|
+
let nextColumn = level.columns.findIndex(function (column) { return column.key === active.columnKey; }) + columnStep;
|
|
1187
|
+
if (nextColumn >= level.columns.length) {
|
|
1188
|
+
nextColumn = 0;
|
|
1189
|
+
nextRow++;
|
|
1190
|
+
}
|
|
1191
|
+
if (nextColumn < 0) {
|
|
1192
|
+
nextColumn = level.columns.length - 1;
|
|
1193
|
+
nextRow--;
|
|
1194
|
+
}
|
|
1195
|
+
if (nextRow < 0 || nextRow >= viewRows.length) return false;
|
|
1196
|
+
setActive(viewRows[nextRow].id, level.columns[nextColumn].key);
|
|
1197
|
+
scrollToActive();
|
|
1198
|
+
return true;
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
// *** WRITING IN A CELL:
|
|
1202
|
+
|
|
1203
|
+
const startEdit = function (row, column, firstText) {
|
|
1204
|
+
if (kindOfCell(row, column) === "function") return;
|
|
1205
|
+
const text = JsonEditor.textOfValue(row.cells[column.key]);
|
|
1206
|
+
editing = { rowId: row.id, columnKey: column.key, startText: text };
|
|
1207
|
+
editorBox.left = leftOfColumn(column.key);
|
|
1208
|
+
editorBox.top = viewRows.indexOf(row) * ROW;
|
|
1209
|
+
editorBox.width = Math.max(column.width, SIZE.EDITOR_MIN_WIDTH);
|
|
1210
|
+
editorArea.value = (firstText !== undefined) ? firstText : text;
|
|
1211
|
+
fitEditorHeight();
|
|
1212
|
+
editorBox.visible = 1;
|
|
1213
|
+
editorArea.focus();
|
|
1214
|
+
editorArea.setSelectionRange(editorArea.value.length, editorArea.value.length);
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
const fitEditorHeight = function () {
|
|
1218
|
+
const lines = editorArea.value.split("\n").length;
|
|
1219
|
+
const shown = Math.min(lines, SIZE.EDITOR_MAX_LINES);
|
|
1220
|
+
editorBox.height = (shown <= 1) ? ROW : shown * SIZE.EDITOR_LINE_HEIGHT + SIZE.EDITOR_SPACE;
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1223
|
+
// Returns true: the cell is not being written any more.
|
|
1224
|
+
const commitEdit = function () {
|
|
1225
|
+
if (!editing) return true;
|
|
1226
|
+
const row = rowById(editing.rowId);
|
|
1227
|
+
const column = columnByKey(editing.columnKey);
|
|
1228
|
+
const text = editorArea.value;
|
|
1229
|
+
const startText = editing.startText;
|
|
1230
|
+
editing = null;
|
|
1231
|
+
editorBox.visible = 0;
|
|
1232
|
+
if (!row || !column || text === startText) return true;
|
|
1233
|
+
remember();
|
|
1234
|
+
setCell(row, column.key, valueOfCellText(text, row, column));
|
|
1235
|
+
level.sort = null;
|
|
1236
|
+
column.kindNow = kindOfColumn(column);
|
|
1237
|
+
drawHead();
|
|
1238
|
+
finishChange();
|
|
1239
|
+
return true;
|
|
1240
|
+
};
|
|
1241
|
+
|
|
1242
|
+
const cancelEdit = function () {
|
|
1243
|
+
editing = null;
|
|
1244
|
+
editorBox.visible = 0;
|
|
1245
|
+
content.elem.focus({ preventScroll: true });
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
// A click on a cell: what it does comes from its kind.
|
|
1249
|
+
const pressCell = function (hit, event) {
|
|
1250
|
+
const row = hit.row;
|
|
1251
|
+
const column = hit.column;
|
|
1252
|
+
const kind = kindOfCell(row, column);
|
|
1253
|
+
setActive(row.id, column.key);
|
|
1254
|
+
if (kind === "flag") {
|
|
1255
|
+
toggleFlagCell(row, column);
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
if (kind === "nested") {
|
|
1259
|
+
openNestedCell(row, column);
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const isOnSwatch = (hit.xInCell < SIZE.SWATCH_HIT);
|
|
1263
|
+
if (kind === "color" && isOnSwatch) {
|
|
1264
|
+
openColorCell(row, column, hit.viewIndex, event);
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
if (kind === "picture" && isOnSwatch && box.onPickPicture) {
|
|
1268
|
+
box.onPickPicture(box, cellOf(row, column, hit.viewIndex, event));
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
const isOnEnd = (hit.xInCell > column.width - SIZE.OPTION_HIT);
|
|
1272
|
+
if (kind === "option" && isOnEnd) {
|
|
1273
|
+
openOptionCell(row, column, event);
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
startEdit(row, column);
|
|
1277
|
+
};
|
|
1278
|
+
|
|
1279
|
+
const toggleFlagCell = function (row, column) {
|
|
1280
|
+
commitEdit();
|
|
1281
|
+
remember();
|
|
1282
|
+
const value = row.cells[column.key];
|
|
1283
|
+
const isBoolean = (typeof value === "boolean");
|
|
1284
|
+
const on = (isBoolean) ? true : 1;
|
|
1285
|
+
const off = (isBoolean) ? false : 0;
|
|
1286
|
+
setCell(row, column.key, (basic.isOn(value)) ? off : on);
|
|
1287
|
+
finishChange();
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
const openNestedCell = function (row, column) {
|
|
1291
|
+
commitEdit();
|
|
1292
|
+
level.scrollTop = scroller.elem.scrollTop;
|
|
1293
|
+
level.scrollLeft = scroller.elem.scrollLeft;
|
|
1294
|
+
const place = level.rows.indexOf(row) + 1;
|
|
1295
|
+
let name = textOf("rowOfPath", { number: place, column: titleOf(column) });
|
|
1296
|
+
if (level.isObject) name = String(row.cells[NAME_KEY]);
|
|
1297
|
+
const hint = hintOf((level.isObject) ? name : column.key);
|
|
1298
|
+
const hints = (hint && Array.isArray(hint.columns)) ? hint.columns : [];
|
|
1299
|
+
openLevel(name, row.cells[column.key], hints, { rowId: row.id, columnKey: column.key });
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
// A cell for a picker (See A CELL above). The writes of one pick are one step of the undo.
|
|
1303
|
+
const cellOf = function (row, column, viewIndex, event) {
|
|
1304
|
+
const ownLevel = level;
|
|
1305
|
+
let isRemembered = false;
|
|
1306
|
+
const contentRect = content.elem.getBoundingClientRect();
|
|
1307
|
+
const left = contentRect.left + leftOfColumn(column.key) * page.zoom;
|
|
1308
|
+
const top = contentRect.top + viewIndex * ROW * page.zoom;
|
|
1309
|
+
const width = column.width * page.zoom;
|
|
1310
|
+
const height = ROW * page.zoom;
|
|
1311
|
+
return {
|
|
1312
|
+
value: row.cells[column.key],
|
|
1313
|
+
key: (column.key === VALUE_KEY) ? String(row.cells[NAME_KEY]) : column.key,
|
|
1314
|
+
title: titleOf(column),
|
|
1315
|
+
x: event.clientX,
|
|
1316
|
+
y: event.clientY,
|
|
1317
|
+
rect: { left: left, top: top, right: left + width, bottom: top + height, width: width, height: height },
|
|
1318
|
+
setValue: function (value) {
|
|
1319
|
+
// WHY: a picker of the page can stay open after the table of the cell was closed (or the editor removed).
|
|
1320
|
+
if (box._isRemoved || level !== ownLevel) return;
|
|
1321
|
+
if (!isRemembered) remember(); // WHY once: a picker writes while it is dragged
|
|
1322
|
+
isRemembered = true;
|
|
1323
|
+
setCell(row, column.key, value);
|
|
1324
|
+
finishChange();
|
|
1325
|
+
},
|
|
1326
|
+
};
|
|
1327
|
+
};
|
|
1328
|
+
|
|
1329
|
+
// The color picker of the page (onPickColor), or the one of the browser at the cell. A drag in it changes the cell at once.
|
|
1330
|
+
const openColorCell = function (row, column, viewIndex, event) {
|
|
1331
|
+
const cell = cellOf(row, column, viewIndex, event);
|
|
1332
|
+
if (box.onPickColor) {
|
|
1333
|
+
box.onPickColor(box, cell);
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
colorInput.value = JsonEditor.hexOfColor(paintedColorOf(cell.value));
|
|
1337
|
+
colorInput.style.left = leftOfColumn(column.key) + "px";
|
|
1338
|
+
colorInput.style.top = (viewIndex + 1) * ROW + "px";
|
|
1339
|
+
colorInput.oninput = function () { cell.setValue(colorInput.value.toUpperCase()); };
|
|
1340
|
+
JsonEditor.openPicker(colorInput);
|
|
1341
|
+
};
|
|
1342
|
+
|
|
1343
|
+
const openOptionCell = function (row, column, event) {
|
|
1344
|
+
const current = row.cells[column.key];
|
|
1345
|
+
const values = JsonEditor.optionValues(optionsOfCell(row, column), current);
|
|
1346
|
+
const items = values.map(function (value) {
|
|
1347
|
+
return {
|
|
1348
|
+
text: (value === "") ? box.texts.empty : String(value),
|
|
1349
|
+
checked: (value === current) ? 1 : 0,
|
|
1350
|
+
run: function () {
|
|
1351
|
+
remember();
|
|
1352
|
+
setCell(row, column.key, value);
|
|
1353
|
+
finishChange();
|
|
1354
|
+
},
|
|
1355
|
+
};
|
|
1356
|
+
});
|
|
1357
|
+
openMenu(items, event.clientX, event.clientY);
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
// *** THE MOUSE ON THE ROWS: the objects of the rows do not take it, the place tells what was pressed.
|
|
1361
|
+
|
|
1362
|
+
const hitOf = function (event) {
|
|
1363
|
+
const point = pointIn(event, content.elem);
|
|
1364
|
+
const xInView = pointIn(event, scroller.elem).x;
|
|
1365
|
+
const viewIndex = Math.floor(point.y / ROW);
|
|
1366
|
+
const hit = { viewIndex: viewIndex, row: viewRows[viewIndex] || null, part: "", column: null, xInCell: 0 };
|
|
1367
|
+
if (viewIndex >= viewRows.length) {
|
|
1368
|
+
hit.part = (xInView < SIZE.ADD_ROW_WIDTH) ? "add" : "";
|
|
1369
|
+
return hit;
|
|
1370
|
+
}
|
|
1371
|
+
if (!hit.row) return hit;
|
|
1372
|
+
const endLeft = Math.min(scroller.elem.clientWidth, totalWidth() - scroller.elem.scrollLeft) - SIZE.END_WIDTH;
|
|
1373
|
+
if (xInView < SIZE.HANDLE_WIDTH) hit.part = "handle";
|
|
1374
|
+
else if (xInView < GUTTER) hit.part = "number";
|
|
1375
|
+
else if (xInView >= endLeft) hit.part = "delete";
|
|
1376
|
+
if (hit.part) return hit;
|
|
1377
|
+
let left = 0;
|
|
1378
|
+
const x = point.x - GUTTER;
|
|
1379
|
+
for (let i = 0; i < level.columns.length; i++) {
|
|
1380
|
+
const column = level.columns[i];
|
|
1381
|
+
if (x < left + column.width) {
|
|
1382
|
+
hit.part = "cell";
|
|
1383
|
+
hit.column = column;
|
|
1384
|
+
hit.xInCell = x - left;
|
|
1385
|
+
break;
|
|
1386
|
+
}
|
|
1387
|
+
left += column.width;
|
|
1388
|
+
}
|
|
1389
|
+
return hit;
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
const selectRowByNumber = function (row, event) {
|
|
1393
|
+
const isAdding = (event.metaKey || event.ctrlKey);
|
|
1394
|
+
if (event.shiftKey && level.active) {
|
|
1395
|
+
const from = level.rows.findIndex(function (item) { return item.id === level.active.rowId; });
|
|
1396
|
+
const to = level.rows.indexOf(row);
|
|
1397
|
+
const start = Math.min(from, to);
|
|
1398
|
+
const end = Math.max(from, to);
|
|
1399
|
+
level.selected.clear();
|
|
1400
|
+
level.rows.slice(start, end + 1).forEach(function (item) { level.selected.add(item.id); });
|
|
1401
|
+
} else if (isAdding) {
|
|
1402
|
+
if (level.selected.has(row.id)) level.selected.delete(row.id);
|
|
1403
|
+
else level.selected.add(row.id);
|
|
1404
|
+
} else {
|
|
1405
|
+
const isOnlyThis = (level.selected.size === 1 && level.selected.has(row.id));
|
|
1406
|
+
level.selected.clear();
|
|
1407
|
+
if (!isOnlyThis) level.selected.add(row.id);
|
|
1408
|
+
}
|
|
1409
|
+
const columnKey = (level.active) ? level.active.columnKey : level.columns[0].key;
|
|
1410
|
+
if (!event.shiftKey) level.active = { rowId: row.id, columnKey: columnKey };
|
|
1411
|
+
paintRows();
|
|
1412
|
+
placeActive();
|
|
1413
|
+
paintButtons();
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
const onContentClick = function (self, event) {
|
|
1417
|
+
if (dragRow) return;
|
|
1418
|
+
if (event.target === editorArea) return;
|
|
1419
|
+
const hit = hitOf(event);
|
|
1420
|
+
commitEdit();
|
|
1421
|
+
if (hit.part === "add") {
|
|
1422
|
+
if (!searchText.trim()) addRow();
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
if (!hit.row) return;
|
|
1426
|
+
if (hit.part === "number") selectRowByNumber(hit.row, event);
|
|
1427
|
+
if (hit.part === "delete") deleteRows([hit.row]);
|
|
1428
|
+
if (hit.part === "cell") pressCell(hit, event);
|
|
1429
|
+
if (hit.part !== "cell") content.elem.focus({ preventScroll: true });
|
|
1430
|
+
};
|
|
1431
|
+
|
|
1432
|
+
const onContentMove = function (self, event) {
|
|
1433
|
+
const hit = hitOf(event);
|
|
1434
|
+
const nextId = (hit.row) ? hit.row.id : null;
|
|
1435
|
+
let cursor = "pointer";
|
|
1436
|
+
if (hit.part === "handle") cursor = "grab";
|
|
1437
|
+
if (hit.part === "cell") cursor = "text";
|
|
1438
|
+
content.cursor = cursor;
|
|
1439
|
+
if (nextId === hoveredRowId) return;
|
|
1440
|
+
const previousId = hoveredRowId;
|
|
1441
|
+
hoveredRowId = nextId;
|
|
1442
|
+
paintRow(previousId);
|
|
1443
|
+
paintRow(nextId);
|
|
1444
|
+
};
|
|
1445
|
+
|
|
1446
|
+
// DRAG: the handle of a row: a line shows its new place. Not while a search hides rows: the place would be a guess.
|
|
1447
|
+
const startRowDrag = function (event, row) {
|
|
1448
|
+
if (searchText.trim()) {
|
|
1449
|
+
showStatus(box.texts.clearSearch);
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
1452
|
+
event.preventDefault();
|
|
1453
|
+
commitEdit();
|
|
1454
|
+
dragRow = { row: row, place: level.rows.indexOf(row) };
|
|
1455
|
+
const removers = [];
|
|
1456
|
+
const onMove = function (self, moveEvent) {
|
|
1457
|
+
const viewRect = scroller.elem.getBoundingClientRect();
|
|
1458
|
+
if (moveEvent.clientY < viewRect.top + SIZE.DRAG_SCROLL_EDGE) scroller.elem.scrollTop -= SIZE.DRAG_SCROLL_STEP;
|
|
1459
|
+
if (moveEvent.clientY > viewRect.bottom - SIZE.DRAG_SCROLL_EDGE) scroller.elem.scrollTop += SIZE.DRAG_SCROLL_STEP;
|
|
1460
|
+
const place = Math.round(pointIn(moveEvent, content.elem).y / ROW);
|
|
1461
|
+
dragRow.place = Math.max(0, Math.min(level.rows.length, place));
|
|
1462
|
+
dropLine.top = Math.max(0, dragRow.place * ROW - 1);
|
|
1463
|
+
dropLine.width = totalWidth();
|
|
1464
|
+
dropLine.visible = 1;
|
|
1465
|
+
};
|
|
1466
|
+
const onUp = function () {
|
|
1467
|
+
removers.forEach(function (removeListener) { removeListener(); });
|
|
1468
|
+
dropLine.visible = 0;
|
|
1469
|
+
finishRowDrag();
|
|
1470
|
+
// WHY after the click of this pointerup: it must not act on the row under the mouse.
|
|
1471
|
+
box.after(0, function () { dragRow = null; });
|
|
1472
|
+
};
|
|
1473
|
+
removers.push(box.listenTo(window, "pointermove", onMove));
|
|
1474
|
+
removers.push(box.listenTo(window, "pointerup", onUp));
|
|
1475
|
+
removers.push(box.listenTo(window, "pointercancel", onUp));
|
|
1476
|
+
};
|
|
1477
|
+
|
|
1478
|
+
const finishRowDrag = function () {
|
|
1479
|
+
const from = level.rows.indexOf(dragRow.row);
|
|
1480
|
+
const to = (dragRow.place > from) ? dragRow.place - 1 : dragRow.place;
|
|
1481
|
+
if (to === from) return;
|
|
1482
|
+
remember();
|
|
1483
|
+
level.rows.splice(from, 1);
|
|
1484
|
+
level.rows.splice(to, 0, dragRow.row);
|
|
1485
|
+
level.sort = null;
|
|
1486
|
+
const columnKey = (level.active) ? level.active.columnKey : level.columns[0].key;
|
|
1487
|
+
level.active = { rowId: dragRow.row.id, columnKey: columnKey };
|
|
1488
|
+
drawHead();
|
|
1489
|
+
finishChange();
|
|
1490
|
+
};
|
|
1491
|
+
|
|
1492
|
+
// *** MENUS:
|
|
1493
|
+
|
|
1494
|
+
const openMenu = function (items, x, y) {
|
|
1495
|
+
if (!menu) {
|
|
1496
|
+
menu = box.own(createIn(page, function () {
|
|
1497
|
+
return ContextMenu({ styleName: _s.menuStyleName, minWidth: SIZE.MENU_MIN_WIDTH, onClick: runMenuItem });
|
|
1498
|
+
}));
|
|
1499
|
+
}
|
|
1500
|
+
menu.setItems(items);
|
|
1501
|
+
menu.openAt(x, y, box);
|
|
1502
|
+
};
|
|
1503
|
+
|
|
1504
|
+
const runMenuItem = function (self, item) {
|
|
1505
|
+
if (item.run) item.run();
|
|
1506
|
+
};
|
|
1507
|
+
|
|
1508
|
+
const openRowMenu = function (row, x, y) {
|
|
1509
|
+
const place = level.rows.indexOf(row);
|
|
1510
|
+
const rows = (level.selected.has(row.id)) ? selectedRows() : [row];
|
|
1511
|
+
const isMany = (rows.length > 1);
|
|
1512
|
+
const count = { count: rows.length };
|
|
1513
|
+
openMenu([
|
|
1514
|
+
{ text: box.texts.addAbove, run: function () { addRow(place); } },
|
|
1515
|
+
{ text: box.texts.addBelow, run: function () { addRow(place + 1); } },
|
|
1516
|
+
"-",
|
|
1517
|
+
{ text: (isMany) ? textOf("duplicateRows", count) : box.texts.duplicate, shortcut: "Cmd/Ctrl+D", run: function () { duplicateRows(rows); } },
|
|
1518
|
+
{ text: box.texts.moveUp, shortcut: "Alt+↑", run: function () { moveRows(rows, -1); } },
|
|
1519
|
+
{ text: box.texts.moveDown, shortcut: "Alt+↓", run: function () { moveRows(rows, 1); } },
|
|
1520
|
+
"-",
|
|
1521
|
+
{ text: (isMany) ? textOf("deleteRows", count) : box.texts.delete, run: function () { deleteRows(rows); } },
|
|
1522
|
+
], x, y);
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
const openColumnMenu = function (column, button) {
|
|
1526
|
+
const rect = button.elem.getBoundingClientRect();
|
|
1527
|
+
const kindTexts = { "": "fromValues", text: "kindText", number: "kindNumber", flag: "kindFlag", color: "kindColor", picture: "kindPicture" };
|
|
1528
|
+
const typeItems = JsonEditor.TYPES.map(function (type) {
|
|
1529
|
+
return {
|
|
1530
|
+
text: box.texts[kindTexts[type]],
|
|
1531
|
+
checked: ((column.chosenKind || "") === type) ? 1 : 0,
|
|
1532
|
+
run: function () {
|
|
1533
|
+
remember();
|
|
1534
|
+
column.chosenKind = type;
|
|
1535
|
+
column.kindNow = kindOfColumn(column);
|
|
1536
|
+
afterRowsChange();
|
|
1537
|
+
},
|
|
1538
|
+
};
|
|
1539
|
+
});
|
|
1540
|
+
const canRename = (!/^\d+$/.test(column.key));
|
|
1541
|
+
openMenu([
|
|
1542
|
+
{ text: box.texts.sortUp, run: function () { sortBy(column, "asc"); } },
|
|
1543
|
+
{ text: box.texts.sortDown, run: function () { sortBy(column, "desc"); } },
|
|
1544
|
+
{ text: box.texts.firstOrder, enabled: (level.sort) ? 1 : 0, run: function () { sortBy(column, ""); } },
|
|
1545
|
+
"-",
|
|
1546
|
+
{ text: box.texts.rename, enabled: (canRename) ? 1 : 0, run: function () { openColumnNamer(column); } },
|
|
1547
|
+
{ text: box.texts.type, items: typeItems },
|
|
1548
|
+
{ text: box.texts.moveLeft, run: function () { moveColumn(column, -1); } },
|
|
1549
|
+
{ text: box.texts.moveRight, run: function () { moveColumn(column, 1); } },
|
|
1550
|
+
"-",
|
|
1551
|
+
{ text: box.texts.deleteColumn, run: function () { deleteColumn(column); } },
|
|
1552
|
+
], rect.left, rect.bottom + SIZE.MENU_SPACE);
|
|
1553
|
+
};
|
|
1554
|
+
|
|
1555
|
+
// A small field for a name, over the table: rect is a place on the screen. onDone(text)
|
|
1556
|
+
const askName = function (rect, placeholder, value, onDone) {
|
|
1557
|
+
let isFinished = false;
|
|
1558
|
+
const boxRect = box.elem.getBoundingClientRect();
|
|
1559
|
+
const field = createIn(box, function () {
|
|
1560
|
+
// GROUP: The field
|
|
1561
|
+
const group = HGroup({
|
|
1562
|
+
left: withPageZoom(rect.left - boxRect.left),
|
|
1563
|
+
top: withPageZoom(rect.top - boxRect.top),
|
|
1564
|
+
width: SIZE.NAME_FIELD_WIDTH,
|
|
1565
|
+
height: SIZE.NAME_FIELD_HEIGHT,
|
|
1566
|
+
align: "left center",
|
|
1567
|
+
color: C.editor,
|
|
1568
|
+
round: 5,
|
|
1569
|
+
border: 1,
|
|
1570
|
+
borderColor: C.accent,
|
|
1571
|
+
});
|
|
1572
|
+
group.zIndex = SIZE.NAME_FIELD_Z_INDEX;
|
|
1573
|
+
group.boxShadow = _s.popupShadow;
|
|
1574
|
+
endGroup();
|
|
1575
|
+
return group;
|
|
1576
|
+
});
|
|
1577
|
+
const input = createField(field, "input");
|
|
1578
|
+
input.value = value;
|
|
1579
|
+
input.placeholder = placeholder;
|
|
1580
|
+
input.style.padding = "0px 6px";
|
|
1581
|
+
const finish = function (text) {
|
|
1582
|
+
if (isFinished) return;
|
|
1583
|
+
isFinished = true;
|
|
1584
|
+
field.remove();
|
|
1585
|
+
if (text !== null) onDone(text);
|
|
1586
|
+
};
|
|
1587
|
+
input.addEventListener("keydown", function (event) {
|
|
1588
|
+
event.stopPropagation();
|
|
1589
|
+
if (event.key === "Enter") finish(input.value);
|
|
1590
|
+
if (event.key === "Escape") finish(null);
|
|
1591
|
+
});
|
|
1592
|
+
input.addEventListener("blur", function () { finish(input.value); });
|
|
1593
|
+
input.focus();
|
|
1594
|
+
input.select();
|
|
1595
|
+
};
|
|
1596
|
+
|
|
1597
|
+
const openColumnNamer = function (column) {
|
|
1598
|
+
const contentRect = content.elem.getBoundingClientRect();
|
|
1599
|
+
const viewRect = scroller.elem.getBoundingClientRect();
|
|
1600
|
+
const left = contentRect.left + leftOfColumn(column.key) * page.zoom;
|
|
1601
|
+
const value = (column.key === PLAIN_KEY) ? "" : column.key;
|
|
1602
|
+
const place = { left: Math.max(viewRect.left, left), top: viewRect.top - (HEAD - 2) * page.zoom };
|
|
1603
|
+
askName(place, box.texts.columnName, value, function (text) {
|
|
1604
|
+
renameColumn(column, text);
|
|
1605
|
+
});
|
|
1606
|
+
};
|
|
1607
|
+
|
|
1608
|
+
// + Column: the names the page gave (columns), or a new one.
|
|
1609
|
+
const openColumnChooser = function (button) {
|
|
1610
|
+
const rect = button.elem.getBoundingClientRect();
|
|
1611
|
+
const free = level.hints.filter(function (hint) { return !columnByKey(hint.key); });
|
|
1612
|
+
const askNewName = function () {
|
|
1613
|
+
askName({ left: rect.left, top: rect.bottom + SIZE.MENU_SPACE }, box.texts.newColumnName, "", addColumn);
|
|
1614
|
+
};
|
|
1615
|
+
if (!free.length) {
|
|
1616
|
+
askNewName();
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
const items = free.map(function (hint) {
|
|
1620
|
+
return { text: hint.key, run: function () { addColumn(hint.key); } };
|
|
1621
|
+
});
|
|
1622
|
+
items.push("-");
|
|
1623
|
+
items.push({ text: box.texts.newName, run: askNewName });
|
|
1624
|
+
openMenu(items, rect.left, rect.bottom + SIZE.MENU_SPACE);
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
// *** COPY AND PASTE: a block of a spreadsheet goes into the cells from the active one; JSON adds rows.
|
|
1628
|
+
|
|
1629
|
+
const pasteRows = function (list) {
|
|
1630
|
+
if (level.isObject) {
|
|
1631
|
+
showStatus(box.texts.pasteList);
|
|
1632
|
+
return;
|
|
1633
|
+
}
|
|
1634
|
+
if (!list.length) return;
|
|
1635
|
+
remember();
|
|
1636
|
+
const rows = JsonEditor.rowsOfList(list);
|
|
1637
|
+
JsonEditor.columnsOfRows(rows, level.hints).forEach(function (column) {
|
|
1638
|
+
const isNewPlainColumn = (column.key === PLAIN_KEY && rows.every(function (row) { return row.shape !== "plain"; }));
|
|
1639
|
+
if (!columnByKey(column.key) && !isNewPlainColumn) level.columns.push(column);
|
|
1640
|
+
});
|
|
1641
|
+
level.rows = level.rows.concat(rows);
|
|
1642
|
+
fitColumns();
|
|
1643
|
+
drawHead();
|
|
1644
|
+
finishChange();
|
|
1645
|
+
const message = (rows.length === 1) ? box.texts.oneRowAdded : textOf("rowsAdded", { count: rows.length });
|
|
1646
|
+
if (!box.errorText) showStatus(message);
|
|
1647
|
+
};
|
|
1648
|
+
|
|
1649
|
+
const pasteGrid = function (grid) {
|
|
1650
|
+
remember();
|
|
1651
|
+
const active = level.active;
|
|
1652
|
+
const firstColumn = (active) ? level.columns.findIndex(function (column) { return column.key === active.columnKey; }) : 0;
|
|
1653
|
+
const firstRow = (active) ? level.rows.findIndex(function (row) { return row.id === active.rowId; }) : level.rows.length;
|
|
1654
|
+
const isHeadOfNames = grid.length > 1 && grid[0].every(function (text) { return !!columnByKey(text.trim()); });
|
|
1655
|
+
const dataLines = (isHeadOfNames) ? grid.slice(1) : grid;
|
|
1656
|
+
dataLines.forEach(function (line, lineIndex) {
|
|
1657
|
+
const rowIndex = (isHeadOfNames) ? level.rows.length : firstRow + lineIndex;
|
|
1658
|
+
if (rowIndex >= level.rows.length) level.rows.push(newRow());
|
|
1659
|
+
const row = level.rows[rowIndex];
|
|
1660
|
+
line.forEach(function (text, cellIndex) {
|
|
1661
|
+
const column = (isHeadOfNames) ? columnByKey(grid[0][cellIndex].trim()) : level.columns[firstColumn + cellIndex];
|
|
1662
|
+
if (column) setCell(row, column.key, valueOfCellText(text, row, column));
|
|
1663
|
+
});
|
|
1664
|
+
});
|
|
1665
|
+
fitColumns();
|
|
1666
|
+
drawHead();
|
|
1667
|
+
finishChange();
|
|
1668
|
+
const message = (dataLines.length === 1) ? box.texts.oneLinePasted : textOf("linesPasted", { count: dataLines.length });
|
|
1669
|
+
if (!box.errorText) showStatus(message);
|
|
1670
|
+
};
|
|
1671
|
+
|
|
1672
|
+
const onPaste = function (self, event) {
|
|
1673
|
+
const target = event.target;
|
|
1674
|
+
const isOtherField = (target !== editorArea && /^(INPUT|TEXTAREA)$/.test(target.tagName));
|
|
1675
|
+
if (isJsonView || isOtherField) return;
|
|
1676
|
+
const text = (event.clipboardData) ? event.clipboardData.getData("text") : "";
|
|
1677
|
+
const pasted = JsonEditor.readPastedText(text);
|
|
1678
|
+
if (!pasted) {
|
|
1679
|
+
// WHY: one text: a field being written takes it by itself; a chosen cell starts writing with it.
|
|
1680
|
+
const activeRow = level.active && rowById(level.active.rowId);
|
|
1681
|
+
if (!editing && activeRow) {
|
|
1682
|
+
event.preventDefault();
|
|
1683
|
+
startEdit(activeRow, columnByKey(level.active.columnKey), text);
|
|
1684
|
+
}
|
|
1685
|
+
return;
|
|
1686
|
+
}
|
|
1687
|
+
event.preventDefault();
|
|
1688
|
+
commitEdit();
|
|
1689
|
+
if (pasted.list) pasteRows(pasted.list);
|
|
1690
|
+
else pasteGrid(pasted.grid);
|
|
1691
|
+
};
|
|
1692
|
+
|
|
1693
|
+
const onCopy = function (self, event) {
|
|
1694
|
+
if (isJsonView || editing) return;
|
|
1695
|
+
if (/^(INPUT|TEXTAREA)$/.test(event.target.tagName)) return;
|
|
1696
|
+
const rows = selectedRows();
|
|
1697
|
+
if (!rows.length) return;
|
|
1698
|
+
const lines = rows.map(function (row) {
|
|
1699
|
+
return level.columns.map(function (column) { return JsonEditor.textOfValue(row.cells[column.key]); }).join("\t");
|
|
1700
|
+
});
|
|
1701
|
+
event.preventDefault();
|
|
1702
|
+
event.clipboardData.setData("text/plain", lines.join("\n"));
|
|
1703
|
+
showStatus((rows.length === 1) ? box.texts.oneRowCopied : textOf("rowsCopied", { count: rows.length }));
|
|
1704
|
+
};
|
|
1705
|
+
|
|
1706
|
+
// *** THE JSON VIEW: the same data as a text.
|
|
1707
|
+
|
|
1708
|
+
const jsonTextOfLevel = function () {
|
|
1709
|
+
return JSON.stringify(JsonEditor.valueOfLevel(level), null, 2);
|
|
1710
|
+
};
|
|
1711
|
+
|
|
1712
|
+
const showJsonView = function () {
|
|
1713
|
+
if (isJsonView) return;
|
|
1714
|
+
commitEdit();
|
|
1715
|
+
let text;
|
|
1716
|
+
try {
|
|
1717
|
+
text = jsonTextOfLevel();
|
|
1718
|
+
} catch (error) {
|
|
1719
|
+
showStatus(messageOf(error));
|
|
1720
|
+
return;
|
|
1721
|
+
}
|
|
1722
|
+
isJsonView = true;
|
|
1723
|
+
jsonArea.value = text;
|
|
1724
|
+
jsonStartText = text;
|
|
1725
|
+
jsonBox.visible = 1;
|
|
1726
|
+
drawViewSwitch();
|
|
1727
|
+
paintButtons();
|
|
1728
|
+
};
|
|
1729
|
+
|
|
1730
|
+
// The text of the JSON view -> { data } or { error }.
|
|
1731
|
+
const readJsonText = function (text) {
|
|
1732
|
+
let data;
|
|
1733
|
+
try {
|
|
1734
|
+
data = JSON.parse(text);
|
|
1735
|
+
} catch (error) {
|
|
1736
|
+
return { error: textOf("notJson", { error: error.message }) };
|
|
1737
|
+
}
|
|
1738
|
+
const isRightShape = (level.isObject) ? JsonEditor.isPlain(data) : Array.isArray(data);
|
|
1739
|
+
if (!isRightShape) return { error: (level.isObject) ? box.texts.objectNeeded : box.texts.listNeeded };
|
|
1740
|
+
return { data: data };
|
|
1741
|
+
};
|
|
1742
|
+
|
|
1743
|
+
// The text of the JSON view goes into the table. false: it is not JSON of the right shape (the status says why).
|
|
1744
|
+
const applyJsonText = function () {
|
|
1745
|
+
const text = jsonArea.value;
|
|
1746
|
+
if (text === jsonStartText) return true;
|
|
1747
|
+
const result = readJsonText(text);
|
|
1748
|
+
if (result.error) {
|
|
1749
|
+
box.errorText = result.error;
|
|
1750
|
+
showStatus(result.error);
|
|
1751
|
+
return false;
|
|
1752
|
+
}
|
|
1753
|
+
const old = JsonEditor.valueOfLevel(level);
|
|
1754
|
+
remember();
|
|
1755
|
+
const fresh = JsonEditor.levelOf(JsonEditor.keepFunctions(result.data, old), level.hints);
|
|
1756
|
+
level.rows = fresh.rows;
|
|
1757
|
+
level.columns = fresh.columns;
|
|
1758
|
+
level.selected.clear();
|
|
1759
|
+
level.active = null;
|
|
1760
|
+
level.sort = null;
|
|
1761
|
+
jsonStartText = text;
|
|
1762
|
+
fitColumns();
|
|
1763
|
+
drawHead();
|
|
1764
|
+
finishChange();
|
|
1765
|
+
return true;
|
|
1766
|
+
};
|
|
1767
|
+
|
|
1768
|
+
const leaveJsonView = function () {
|
|
1769
|
+
if (!isJsonView) return true;
|
|
1770
|
+
if (!applyJsonText()) return false;
|
|
1771
|
+
closeJsonView();
|
|
1772
|
+
return true;
|
|
1773
|
+
};
|
|
1774
|
+
|
|
1775
|
+
const closeJsonView = function () {
|
|
1776
|
+
isJsonView = false;
|
|
1777
|
+
jsonBox.visible = 0;
|
|
1778
|
+
drawViewSwitch();
|
|
1779
|
+
paintButtons();
|
|
1780
|
+
if (!box.errorText) showStatus("");
|
|
1781
|
+
};
|
|
1782
|
+
|
|
1783
|
+
// *** THE PATH: the value and the cells opened in it. A click goes back.
|
|
1784
|
+
|
|
1785
|
+
const drawPath = function () {
|
|
1786
|
+
if (groupPath.parts) groupPath.parts.forEach(function (part) { part.remove(); });
|
|
1787
|
+
groupPath.parts = createIn(groupPath, function () {
|
|
1788
|
+
const parts = [];
|
|
1789
|
+
levels.forEach(function (item, index) {
|
|
1790
|
+
if (index) parts.push(drawPathArrow());
|
|
1791
|
+
parts.push(drawPathPart(item, index));
|
|
1792
|
+
});
|
|
1793
|
+
return parts;
|
|
1794
|
+
});
|
|
1795
|
+
const note = (levels.length === 1) ? box.noteText : "";
|
|
1796
|
+
lblNote.plainText = note;
|
|
1797
|
+
lblNote.elem.title = note;
|
|
1798
|
+
lblNote.visible = (note) ? 1 : 0;
|
|
1799
|
+
const hasTitle = (box.titleText !== "" || box.noteText !== "" || levels.length > 1);
|
|
1800
|
+
box.titleBar.visible = (hasTitle || box.showTitleBar == 1) ? 1 : 0;
|
|
1801
|
+
};
|
|
1802
|
+
|
|
1803
|
+
const drawPathArrow = function () {
|
|
1804
|
+
const lblArrow = Label({ text: "›", fontSize: _s.titleFontSize, textColor: C.dim, height: SIZE.TITLE_HEIGHT });
|
|
1805
|
+
lblArrow.css = { lineHeight: SIZE.TITLE_HEIGHT + "px" };
|
|
1806
|
+
lblArrow.shrink = 0;
|
|
1807
|
+
return lblArrow;
|
|
1808
|
+
};
|
|
1809
|
+
|
|
1810
|
+
const drawPathPart = function (item, index) {
|
|
1811
|
+
const isLast = (index === levels.length - 1);
|
|
1812
|
+
const rootName = box.titleText || ((item.isObject) ? box.texts.object : box.texts.list);
|
|
1813
|
+
const lbl = Label({ text: "", fontSize: _s.titleFontSize, textColor: (isLast) ? C.text : C.accentText, fontFamily: "opensans-bold", height: SIZE.TITLE_HEIGHT, ellipsis: 1 });
|
|
1814
|
+
lbl.plainText = (index) ? item.name : rootName;
|
|
1815
|
+
lbl.css = { lineHeight: SIZE.TITLE_HEIGHT + "px", minWidth: "0px" };
|
|
1816
|
+
if (isLast) return lbl;
|
|
1817
|
+
lbl.shrink = 0;
|
|
1818
|
+
lbl.clickable = 1;
|
|
1819
|
+
lbl.cursor = "pointer";
|
|
1820
|
+
lbl.role = "button";
|
|
1821
|
+
lbl.tabIndex = 0;
|
|
1822
|
+
lbl.setFocusRing(C.focus);
|
|
1823
|
+
lbl.on("click", function () { goBackTo(index); });
|
|
1824
|
+
lbl.on("keydown", function (self, event) {
|
|
1825
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
1826
|
+
event.preventDefault();
|
|
1827
|
+
goBackTo(index);
|
|
1828
|
+
});
|
|
1829
|
+
return lbl;
|
|
1830
|
+
};
|
|
1831
|
+
|
|
1832
|
+
const goBackTo = function (index) {
|
|
1833
|
+
if (!leaveJsonView()) return;
|
|
1834
|
+
closeLevelsTo(index);
|
|
1835
|
+
};
|
|
1836
|
+
|
|
1837
|
+
// *** THE TOOLBAR, THE FOOTER:
|
|
1838
|
+
|
|
1839
|
+
const paintButtons = function () {
|
|
1840
|
+
const hasRows = selectedRows().length > 0;
|
|
1841
|
+
tableTools.forEach(function (tool) {
|
|
1842
|
+
tool.visible = (isJsonView) ? 0 : 1;
|
|
1843
|
+
});
|
|
1844
|
+
// WHY: an object has its names and values: a new name is a new row.
|
|
1845
|
+
btnAddColumn.visible = (isJsonView || level.isObject) ? 0 : 1;
|
|
1846
|
+
btnDuplicate.setEnabled(hasRows);
|
|
1847
|
+
btnDelete.setEnabled(hasRows);
|
|
1848
|
+
btnUp.setEnabled(hasRows);
|
|
1849
|
+
btnDown.setEnabled(hasRows);
|
|
1850
|
+
btnUndo.setEnabled(level.undoList.length > 0);
|
|
1851
|
+
btnRedo.setEnabled(level.redoList.length > 0);
|
|
1852
|
+
};
|
|
1853
|
+
|
|
1854
|
+
const showStatus = function (text) {
|
|
1855
|
+
lblStatus.plainText = text;
|
|
1856
|
+
lblStatus.elem.title = text;
|
|
1857
|
+
};
|
|
1858
|
+
|
|
1859
|
+
// GROUP: Table | JSON
|
|
1860
|
+
const drawViewSwitch = function () {
|
|
1861
|
+
if (viewSwitch) viewSwitch.remove();
|
|
1862
|
+
viewSwitch = createIn(viewSwitchHolder, function () {
|
|
1863
|
+
const group = HGroup({ width: SIZE.VIEW_SWITCH_WIDTH, height: SIZE.VIEW_SWITCH_HEIGHT, align: "left center", gap: 0, color: C.field, round: SIZE.VIEW_SWITCH_ROUND });
|
|
1864
|
+
group.shrink = 0;
|
|
1865
|
+
// WHY the room inside the line: the ring of the chosen part lay on the line of the group (a double edge, corners that did not meet)
|
|
1866
|
+
if (C.fieldLine) group.props({ border: 1, borderColor: C.fieldLine, padding: SIZE.VIEW_SWITCH_INSET });
|
|
1867
|
+
group.role = "radiogroup";
|
|
1868
|
+
drawViewOption("table", box.texts.table);
|
|
1869
|
+
drawViewOption("json", box.texts.json);
|
|
1870
|
+
endGroup();
|
|
1871
|
+
return group;
|
|
1872
|
+
});
|
|
1873
|
+
};
|
|
1874
|
+
|
|
1875
|
+
const drawViewOption = function (view, text) {
|
|
1876
|
+
const isSelected = ((view === "json") === isJsonView);
|
|
1877
|
+
const inset = (C.fieldLine) ? 1 + SIZE.VIEW_SWITCH_INSET : 0; // the line of the group and the room inside it
|
|
1878
|
+
const option = Label({
|
|
1879
|
+
text: "",
|
|
1880
|
+
height: SIZE.VIEW_SWITCH_HEIGHT - inset * 2,
|
|
1881
|
+
fontSize: _s.fontSize - 1,
|
|
1882
|
+
round: SIZE.VIEW_SWITCH_ROUND - inset, // WHY: its corners follow the corners of the group
|
|
1883
|
+
textColor: (isSelected) ? C.text : C.soft,
|
|
1884
|
+
color: (isSelected) ? C.segment : "transparent",
|
|
1885
|
+
clickable: 1,
|
|
1886
|
+
cursor: "pointer",
|
|
1887
|
+
});
|
|
1888
|
+
option.plainText = text;
|
|
1889
|
+
option.grow = 1;
|
|
1890
|
+
option.css = { flexBasis: "0px", display: "flex", alignItems: "center", justifyContent: "center", whiteSpace: "nowrap" };
|
|
1891
|
+
option.boxShadow = (isSelected) ? "inset 0 0 0 1px " + C.strongLine : "none";
|
|
1892
|
+
option.role = "radio";
|
|
1893
|
+
option.tabIndex = 0;
|
|
1894
|
+
option.aria = { checked: (isSelected) ? "true" : "false" };
|
|
1895
|
+
option.setFocusRing(C.focus);
|
|
1896
|
+
if (!isSelected) option.onHoverChange(function (self, isOver) { option.color = (isOver) ? C.fieldHover : "transparent"; });
|
|
1897
|
+
option.on("click", function () { box.setView(view); });
|
|
1898
|
+
option.on("keydown", function (self, event) {
|
|
1899
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
1900
|
+
event.preventDefault();
|
|
1901
|
+
box.setView(view);
|
|
1902
|
+
});
|
|
1903
|
+
};
|
|
1904
|
+
|
|
1905
|
+
const copyJson = function () {
|
|
1906
|
+
commitEdit();
|
|
1907
|
+
let text;
|
|
1908
|
+
try {
|
|
1909
|
+
text = (isJsonView) ? jsonArea.value : jsonTextOfLevel();
|
|
1910
|
+
} catch (error) {
|
|
1911
|
+
showStatus(messageOf(error));
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
JsonEditor.copyText(text)
|
|
1915
|
+
.then(function () { showStatus(box.texts.jsonCopied); })
|
|
1916
|
+
.catch(function (error) { showStatus(error.message); });
|
|
1917
|
+
};
|
|
1918
|
+
|
|
1919
|
+
// A raw field (basic.js has no field that fills a box of the editor): "input" or "textarea".
|
|
1920
|
+
const createField = function (parentBox, tagName) {
|
|
1921
|
+
const field = document.createElement(tagName);
|
|
1922
|
+
field.spellcheck = false;
|
|
1923
|
+
field.className = "json-editor-field";
|
|
1924
|
+
field.style.fontSize = _s.fontSize + "px";
|
|
1925
|
+
parentBox.elem.appendChild(field);
|
|
1926
|
+
return field;
|
|
1927
|
+
};
|
|
1928
|
+
|
|
1929
|
+
// GROUP: The title: the path of the table in sight and a note
|
|
1930
|
+
const buildTitleBar = function () {
|
|
1931
|
+
box.titleBar = VGroup({ width: "100%", height: "auto", align: "left top", gap: 2, padding: [SIZE.BAR_SPACE, SIZE.TITLE_SPACE], color: "transparent" });
|
|
1932
|
+
box.titleBar.css = { alignItems: "stretch", boxSizing: "border-box" };
|
|
1933
|
+
box.titleBar.shrink = 0;
|
|
1934
|
+
box.titleRow = HGroup({ width: "100%", height: SIZE.TITLE_HEIGHT, align: "left center", gap: 4, color: "transparent" });
|
|
1935
|
+
box.titleRow.shrink = 0;
|
|
1936
|
+
groupPath = HGroup({ width: 10, height: SIZE.TITLE_HEIGHT, align: "left center", gap: 8, color: "transparent" });
|
|
1937
|
+
groupPath.grow = 1;
|
|
1938
|
+
groupPath.css = { minWidth: "0px" };
|
|
1939
|
+
endGroup();
|
|
1940
|
+
endGroup();
|
|
1941
|
+
lblNote = Label({ text: "", height: SIZE.NOTE_HEIGHT, fontSize: _s.fontSize - 1, textColor: C.soft, ellipsis: 1 });
|
|
1942
|
+
lblNote.css = { lineHeight: SIZE.NOTE_HEIGHT + "px" };
|
|
1943
|
+
lblNote.shrink = 0;
|
|
1944
|
+
endGroup();
|
|
1945
|
+
};
|
|
1946
|
+
|
|
1947
|
+
// GROUP: The buttons of the table, the search, the view (they go to a second line on a narrow editor)
|
|
1948
|
+
const buildToolbar = function () {
|
|
1949
|
+
box.toolbar = HGroup({ width: "100%", height: "auto", align: "left center", gap: 4, padding: [SIZE.BAR_SPACE, SIZE.TOOLBAR_SPACE], wrap: 1, color: "transparent" });
|
|
1950
|
+
box.toolbar.shrink = 0;
|
|
1951
|
+
box.toolbar.clipContent = 0;
|
|
1952
|
+
box.toolbar.css = { borderBottom: "1px solid " + C.line, boxSizing: "border-box", rowGap: SIZE.TOOLBAR_SPACE + "px" };
|
|
1953
|
+
const btnAddRow = JsonEditor.textButton(JsonEditor.iconOf("plus", SIZE.ICON) + JsonEditor.spanOf(box.texts.addRow), looks.button, function () { addRow(); });
|
|
1954
|
+
btnAddColumn = JsonEditor.textButton(JsonEditor.iconOf("plus", SIZE.ICON) + JsonEditor.spanOf(box.texts.addColumn), looks.button, addColumnFromButton);
|
|
1955
|
+
|
|
1956
|
+
// GROUP: The buttons of the chosen rows. WHY groups: a narrow editor puts a group on the next line, not half of it.
|
|
1957
|
+
const rowTools = startToolGroup();
|
|
1958
|
+
btnDuplicate = JsonEditor.iconButton("copy", box.texts.duplicate + " (Cmd/Ctrl+D)", looks.icon, function () { duplicateRows(selectedRows()); });
|
|
1959
|
+
btnUp = JsonEditor.iconButton("up", box.texts.moveUp + " (Alt+↑)", looks.icon, function () { moveRows(selectedRows(), -1); });
|
|
1960
|
+
btnDown = JsonEditor.iconButton("down", box.texts.moveDown + " (Alt+↓)", looks.icon, function () { moveRows(selectedRows(), 1); });
|
|
1961
|
+
btnDelete = JsonEditor.iconButton("trash", box.texts.delete + " (Delete)", looks.icon, function () { deleteRows(selectedRows()); });
|
|
1962
|
+
endGroup();
|
|
1963
|
+
|
|
1964
|
+
// GROUP: Undo, redo
|
|
1965
|
+
const historyTools = startToolGroup();
|
|
1966
|
+
btnUndo = JsonEditor.iconButton("back", box.texts.undo + " (Cmd/Ctrl+Z)", looks.icon, undo);
|
|
1967
|
+
btnRedo = JsonEditor.iconButton("forward", box.texts.redo + " (Shift+Cmd/Ctrl+Z)", looks.icon, redo);
|
|
1968
|
+
endGroup();
|
|
1969
|
+
|
|
1970
|
+
// GROUP: The search
|
|
1971
|
+
const searchHolder = HGroup({ width: SIZE.SEARCH_WIDTH, height: _s.search.height, align: "left center", color: "transparent" });
|
|
1972
|
+
// WHY the basis under its width: a narrow editor keeps it on the line of the buttons; it grows up to its width.
|
|
1973
|
+
searchHolder.css = { flex: "1 1 " + SIZE.SEARCH_BASIS + "px", maxWidth: SIZE.SEARCH_WIDTH + "px", minWidth: SIZE.SEARCH_MIN_WIDTH + "px", marginLeft: SIZE.SEARCH_SPACE + "px" };
|
|
1974
|
+
searchInput = SearchInput({
|
|
1975
|
+
..._s.search,
|
|
1976
|
+
styleName: _s.searchStyleName,
|
|
1977
|
+
left: 0,
|
|
1978
|
+
top: 0,
|
|
1979
|
+
width: "100%",
|
|
1980
|
+
placeholder: box.texts.search,
|
|
1981
|
+
onChange: function (self) { searchRows(self.getSearchText()); },
|
|
1982
|
+
});
|
|
1983
|
+
endGroup();
|
|
1984
|
+
|
|
1985
|
+
// GROUP: Copy and Table | JSON. WHY the auto margin: at the right end, also when it goes to the next line.
|
|
1986
|
+
const viewTools = HGroup({ width: "auto", height: SIZE.VIEW_SWITCH_HEIGHT, align: "left center", gap: 4, color: "transparent" });
|
|
1987
|
+
viewTools.shrink = 0;
|
|
1988
|
+
viewTools.clipContent = 0;
|
|
1989
|
+
viewTools.css = { marginLeft: "auto" };
|
|
1990
|
+
JsonEditor.iconButton("copy", box.texts.copyJson, looks.icon, copyJson);
|
|
1991
|
+
viewSwitchHolder = HGroup({ width: SIZE.VIEW_SWITCH_WIDTH, height: SIZE.VIEW_SWITCH_HEIGHT, align: "left center", color: "transparent" });
|
|
1992
|
+
viewSwitchHolder.shrink = 0;
|
|
1993
|
+
endGroup();
|
|
1994
|
+
endGroup();
|
|
1995
|
+
endGroup();
|
|
1996
|
+
tableTools = [btnAddRow, rowTools, historyTools, searchHolder];
|
|
1997
|
+
drawViewSwitch();
|
|
1998
|
+
};
|
|
1999
|
+
|
|
2000
|
+
// GROUP: a few buttons of the toolbar after a thin line. endGroup() closes it.
|
|
2001
|
+
const startToolGroup = function () {
|
|
2002
|
+
const group = HGroup({ width: "auto", height: SIZE.ICON_BUTTON, align: "left center", gap: 4, color: "transparent" });
|
|
2003
|
+
group.shrink = 0;
|
|
2004
|
+
group.clipContent = 0;
|
|
2005
|
+
// BOX: The line
|
|
2006
|
+
const line = Box({ width: 1, height: SIZE.TOOLBAR_LINE_HEIGHT, color: C.line });
|
|
2007
|
+
line.shrink = 0;
|
|
2008
|
+
line.css = { position: "relative", left: "0px", top: "0px", margin: "0px 6px" };
|
|
2009
|
+
return group;
|
|
2010
|
+
};
|
|
2011
|
+
|
|
2012
|
+
const searchRows = function (text) {
|
|
2013
|
+
commitEdit();
|
|
2014
|
+
searchText = String(text || "");
|
|
2015
|
+
refreshViewRows();
|
|
2016
|
+
scroller.elem.scrollTop = 0;
|
|
2017
|
+
drawRows(true);
|
|
2018
|
+
placeActive();
|
|
2019
|
+
};
|
|
2020
|
+
|
|
2021
|
+
// BOX: The titles, the rows and the JSON text
|
|
2022
|
+
const buildBody = function () {
|
|
2023
|
+
const body = startBox({ width: "100%", height: 10, color: "transparent" });
|
|
2024
|
+
body.grow = 1;
|
|
2025
|
+
body.css = { position: "relative", left: "0px", top: "0px", minHeight: "0px" };
|
|
2026
|
+
|
|
2027
|
+
// BOX: The titles
|
|
2028
|
+
headBox = startBox(0, 0, "100%", HEAD, { color: C.head });
|
|
2029
|
+
headBox.css = { borderBottom: "1px solid " + C.headLine, boxSizing: "border-box" };
|
|
2030
|
+
endBox();
|
|
2031
|
+
// BOX: Over the titles that move to the side, as the numbers of the rows
|
|
2032
|
+
const headGutter = Box(0, 0, GUTTER, HEAD, { color: C.head });
|
|
2033
|
+
headGutter.css = { borderBottom: "1px solid " + C.headLine, boxSizing: "border-box" };
|
|
2034
|
+
headGutter.zIndex = 1;
|
|
2035
|
+
|
|
2036
|
+
// BOX: The rows
|
|
2037
|
+
scroller = startBox(0, HEAD, "100%", "calc(100% - " + HEAD + "px)", { color: "transparent" });
|
|
2038
|
+
scroller.scrollY = 1;
|
|
2039
|
+
scroller.scrollX = 1;
|
|
2040
|
+
scroller.elem.classList.add("json-editor-scroll");
|
|
2041
|
+
content = startBox(0, 0, 100, 100, { color: "transparent" });
|
|
2042
|
+
content.clipContent = 0;
|
|
2043
|
+
content.clickable = 1;
|
|
2044
|
+
content.tabIndex = 0;
|
|
2045
|
+
content.css = { outline: "none" };
|
|
2046
|
+
|
|
2047
|
+
// BOX: The ring of the active cell, the line of a dragged row, the field of a cell
|
|
2048
|
+
activeRing = Box(0, 0, 0, 0, { color: "transparent", visible: 0 });
|
|
2049
|
+
activeRing.boxShadow = "inset 0 0 0 2px " + C.accent;
|
|
2050
|
+
activeRing.zIndex = 2;
|
|
2051
|
+
dropLine = Box(0, 0, 0, 2, { color: C.accent, visible: 0 });
|
|
2052
|
+
dropLine.zIndex = 3;
|
|
2053
|
+
|
|
2054
|
+
editorBox = startBox(0, 0, 0, ROW, { color: C.editor, visible: 0 });
|
|
2055
|
+
editorBox.boxShadow = "inset 0 0 0 2px " + C.accent;
|
|
2056
|
+
editorBox.zIndex = 4;
|
|
2057
|
+
editorBox.clickable = 1;
|
|
2058
|
+
editorArea = createField(editorBox, "textarea");
|
|
2059
|
+
editorArea.style.padding = SIZE.EDITOR_PADDING;
|
|
2060
|
+
editorArea.style.overflow = "hidden";
|
|
2061
|
+
editorArea.style.whiteSpace = "pre";
|
|
2062
|
+
editorArea.setAttribute("aria-label", box.texts.cellValue);
|
|
2063
|
+
endBox();
|
|
2064
|
+
endBox();
|
|
2065
|
+
// WHY a field of the browser: its color picker; it opens at the cell (a hidden field opens nowhere).
|
|
2066
|
+
colorInput = document.createElement("input");
|
|
2067
|
+
colorInput.type = "color";
|
|
2068
|
+
colorInput.className = "json-editor-color";
|
|
2069
|
+
colorInput.tabIndex = -1;
|
|
2070
|
+
content.elem.appendChild(colorInput);
|
|
2071
|
+
endBox();
|
|
2072
|
+
|
|
2073
|
+
// BOX: The JSON text (over the table)
|
|
2074
|
+
jsonBox = startBox(0, 0, "100%", "100%", { color: C.ground, visible: 0 });
|
|
2075
|
+
jsonBox.zIndex = 6;
|
|
2076
|
+
jsonArea = createField(jsonBox, "textarea");
|
|
2077
|
+
jsonArea.classList.add("json-editor-scroll");
|
|
2078
|
+
jsonArea.style.fontFamily = _s.codeFontFamily;
|
|
2079
|
+
jsonArea.style.padding = SIZE.JSON_PADDING;
|
|
2080
|
+
jsonArea.style.whiteSpace = "pre";
|
|
2081
|
+
jsonArea.setAttribute("aria-label", box.texts.json);
|
|
2082
|
+
endBox();
|
|
2083
|
+
endBox();
|
|
2084
|
+
};
|
|
2085
|
+
|
|
2086
|
+
// GROUP: At the bottom: what the table holds, a message, the keys (box.footer takes the buttons of a window)
|
|
2087
|
+
const buildFooter = function () {
|
|
2088
|
+
box.footer = HGroup({ width: "100%", height: SIZE.FOOTER_HEIGHT, align: "left center", gap: 12, padding: [SIZE.BAR_SPACE, 0], color: "transparent" });
|
|
2089
|
+
box.footer.shrink = 0;
|
|
2090
|
+
box.footer.css = { borderTop: "1px solid " + C.line, boxSizing: "border-box" };
|
|
2091
|
+
lblInfo = Label({ text: "", fontSize: _s.fontSize, textColor: C.soft });
|
|
2092
|
+
lblInfo.shrink = 0;
|
|
2093
|
+
// WHY these flex values: a message is read whole first; the keys take the room that is left.
|
|
2094
|
+
lblStatus = Label({ text: "", fontSize: _s.fontSize, textColor: C.accentText, ellipsis: 1 });
|
|
2095
|
+
lblStatus.css = { minWidth: "0px", flex: "0 1 auto" };
|
|
2096
|
+
const lblHint = Label({ text: "", fontSize: _s.fontSize - 1, textColor: C.dim, ellipsis: 1 });
|
|
2097
|
+
lblHint.plainText = box.texts.hint;
|
|
2098
|
+
lblHint.css = { minWidth: "0px", flex: "1 1 0px", textAlign: "right" };
|
|
2099
|
+
lblHint.visible = (box.showHint == 1) ? 1 : 0;
|
|
2100
|
+
endGroup();
|
|
2101
|
+
};
|
|
2102
|
+
|
|
2103
|
+
// *** THE KEYS: the field of a cell first, then the cells. The other fields keep their own keys.
|
|
2104
|
+
|
|
2105
|
+
const onEditorKey = function (event) {
|
|
2106
|
+
const isCommand = (event.metaKey || event.ctrlKey);
|
|
2107
|
+
if (event.key === "Enter" && isCommand) return false;
|
|
2108
|
+
if (event.key === "Enter" && event.shiftKey) {
|
|
2109
|
+
box.after(0, fitEditorHeight);
|
|
2110
|
+
return true;
|
|
2111
|
+
}
|
|
2112
|
+
if (event.key === "Enter") {
|
|
2113
|
+
event.preventDefault();
|
|
2114
|
+
commitEdit();
|
|
2115
|
+
const hasNextRow = moveActive(1, 0);
|
|
2116
|
+
if (hasNextRow) startEditActive();
|
|
2117
|
+
else content.elem.focus({ preventScroll: true });
|
|
2118
|
+
return true;
|
|
2119
|
+
}
|
|
2120
|
+
if (event.key === "Tab") {
|
|
2121
|
+
event.preventDefault();
|
|
2122
|
+
commitEdit();
|
|
2123
|
+
const hasNextCell = moveActive(0, (event.shiftKey) ? -1 : 1);
|
|
2124
|
+
if (hasNextCell) startEditActive();
|
|
2125
|
+
else content.elem.focus({ preventScroll: true });
|
|
2126
|
+
return true;
|
|
2127
|
+
}
|
|
2128
|
+
if (event.key === "Escape") {
|
|
2129
|
+
event.preventDefault();
|
|
2130
|
+
cancelEdit();
|
|
2131
|
+
return true;
|
|
2132
|
+
}
|
|
2133
|
+
const isOneLine = (editorArea.value.indexOf("\n") === -1);
|
|
2134
|
+
if ((event.key === "ArrowUp" || event.key === "ArrowDown") && isOneLine) {
|
|
2135
|
+
event.preventDefault();
|
|
2136
|
+
commitEdit();
|
|
2137
|
+
moveActive((event.key === "ArrowUp") ? -1 : 1, 0);
|
|
2138
|
+
startEditActive();
|
|
2139
|
+
return true;
|
|
2140
|
+
}
|
|
2141
|
+
return false;
|
|
2142
|
+
};
|
|
2143
|
+
|
|
2144
|
+
const startEditActive = function () {
|
|
2145
|
+
const active = level.active;
|
|
2146
|
+
const row = active && rowById(active.rowId);
|
|
2147
|
+
const column = active && columnByKey(active.columnKey);
|
|
2148
|
+
if (row && column) startEdit(row, column);
|
|
2149
|
+
};
|
|
2150
|
+
|
|
2151
|
+
const onGridKey = function (event) {
|
|
2152
|
+
const isCommand = (event.metaKey || event.ctrlKey);
|
|
2153
|
+
const key = event.key;
|
|
2154
|
+
if (isCommand && key === "Enter") return false; // WHY: it is for the page (Apply in the window)
|
|
2155
|
+
if (isCommand && (key === "z" || key === "Z")) {
|
|
2156
|
+
event.preventDefault();
|
|
2157
|
+
if (event.shiftKey) redo();
|
|
2158
|
+
else undo();
|
|
2159
|
+
return true;
|
|
2160
|
+
}
|
|
2161
|
+
if (isCommand && (key === "y" || key === "Y")) {
|
|
2162
|
+
event.preventDefault();
|
|
2163
|
+
redo();
|
|
2164
|
+
return true;
|
|
2165
|
+
}
|
|
2166
|
+
if (isCommand && (key === "d" || key === "D")) {
|
|
2167
|
+
event.preventDefault();
|
|
2168
|
+
duplicateRows(selectedRows());
|
|
2169
|
+
return true;
|
|
2170
|
+
}
|
|
2171
|
+
if (event.altKey && (key === "ArrowUp" || key === "ArrowDown")) {
|
|
2172
|
+
event.preventDefault();
|
|
2173
|
+
moveRows(selectedRows(), (key === "ArrowUp") ? -1 : 1);
|
|
2174
|
+
return true;
|
|
2175
|
+
}
|
|
2176
|
+
const arrows = { ArrowUp: [-1, 0], ArrowDown: [1, 0], ArrowLeft: [0, -1], ArrowRight: [0, 1] };
|
|
2177
|
+
if (arrows[key]) {
|
|
2178
|
+
event.preventDefault();
|
|
2179
|
+
if (!level.active && viewRows.length) setActive(viewRows[0].id, level.columns[0].key);
|
|
2180
|
+
else moveActive(arrows[key][0], arrows[key][1]);
|
|
2181
|
+
return true;
|
|
2182
|
+
}
|
|
2183
|
+
// WHY not at the last cell: Tab leaves the table there (a table in a page must not keep the focus).
|
|
2184
|
+
if (key === "Tab" && level.active && moveActive(0, (event.shiftKey) ? -1 : 1)) {
|
|
2185
|
+
event.preventDefault();
|
|
2186
|
+
return true;
|
|
2187
|
+
}
|
|
2188
|
+
const activeRow = level.active && rowById(level.active.rowId);
|
|
2189
|
+
const activeColumn = level.active && columnByKey(level.active.columnKey);
|
|
2190
|
+
if (key === "Enter" && activeRow) {
|
|
2191
|
+
event.preventDefault();
|
|
2192
|
+
if (kindOfCell(activeRow, activeColumn) === "nested") openNestedCell(activeRow, activeColumn);
|
|
2193
|
+
else startEdit(activeRow, activeColumn);
|
|
2194
|
+
return true;
|
|
2195
|
+
}
|
|
2196
|
+
if ((key === "Delete" || key === "Backspace") && level.selected.size) {
|
|
2197
|
+
event.preventDefault();
|
|
2198
|
+
deleteRows(selectedRows());
|
|
2199
|
+
return true;
|
|
2200
|
+
}
|
|
2201
|
+
if ((key === "Delete" || key === "Backspace") && activeRow) {
|
|
2202
|
+
event.preventDefault();
|
|
2203
|
+
remember();
|
|
2204
|
+
setCell(activeRow, activeColumn.key, valueOfCellText("", activeRow, activeColumn));
|
|
2205
|
+
finishChange();
|
|
2206
|
+
return true;
|
|
2207
|
+
}
|
|
2208
|
+
const isTypedLetter = (key.length === 1 && !isCommand && !event.altKey);
|
|
2209
|
+
if (isTypedLetter && activeRow) {
|
|
2210
|
+
event.preventDefault();
|
|
2211
|
+
startEdit(activeRow, activeColumn, key);
|
|
2212
|
+
return true;
|
|
2213
|
+
}
|
|
2214
|
+
return false;
|
|
2215
|
+
};
|
|
2216
|
+
|
|
2217
|
+
// A key the editor used does not go on to the page (or to the window of JsonEditor.open).
|
|
2218
|
+
const onKeyDown = function (self, event) {
|
|
2219
|
+
if (event.defaultPrevented) return; // WHY: a button of the editor or an open menu took it
|
|
2220
|
+
if (menu && menu.isOpen()) return;
|
|
2221
|
+
const target = event.target;
|
|
2222
|
+
if (target === editorArea) {
|
|
2223
|
+
if (onEditorKey(event)) event.stopPropagation();
|
|
2224
|
+
return;
|
|
2225
|
+
}
|
|
2226
|
+
if (target === jsonArea) return;
|
|
2227
|
+
const isOtherField = /^(INPUT|TEXTAREA)$/.test(target.tagName);
|
|
2228
|
+
if (isOtherField && event.key === "Escape") {
|
|
2229
|
+
event.stopPropagation();
|
|
2230
|
+
target.blur();
|
|
2231
|
+
content.elem.focus({ preventScroll: true });
|
|
2232
|
+
}
|
|
2233
|
+
if (isOtherField) return;
|
|
2234
|
+
if (onGridKey(event)) event.stopPropagation();
|
|
2235
|
+
};
|
|
2236
|
+
|
|
2237
|
+
// *** PUBLIC FUNCTIONS:
|
|
2238
|
+
|
|
2239
|
+
// The list or the object of the table. null: the table can not make one now (box.errorText says why).
|
|
2240
|
+
// NOTE: A cell being written is written first, and the text of the JSON view goes into the table.
|
|
2241
|
+
box.getValue = function () {
|
|
2242
|
+
commitEdit();
|
|
2243
|
+
if (isJsonView && !applyJsonText()) return null;
|
|
2244
|
+
try {
|
|
2245
|
+
const value = readValue();
|
|
2246
|
+
box.errorText = "";
|
|
2247
|
+
return value;
|
|
2248
|
+
} catch (error) {
|
|
2249
|
+
box.errorText = messageOf(error);
|
|
2250
|
+
showStatus(box.errorText);
|
|
2251
|
+
return null;
|
|
2252
|
+
}
|
|
2253
|
+
};
|
|
2254
|
+
// USAGE: const products = editor.getValue(); if (products === null) println(editor.errorText);
|
|
2255
|
+
|
|
2256
|
+
// A new value: a list or an object (or its JSON text). Its undo starts again; onChange is not called.
|
|
2257
|
+
box.setValue = function (value) {
|
|
2258
|
+
editing = null;
|
|
2259
|
+
editorBox.visible = 0;
|
|
2260
|
+
if (isJsonView) closeJsonView();
|
|
2261
|
+
box.value = JsonEditor.rootValueOf(value);
|
|
2262
|
+
box.isChanged = 0;
|
|
2263
|
+
box.errorText = "";
|
|
2264
|
+
levels.length = 0;
|
|
2265
|
+
openLevel("", box.value, box.columns, null);
|
|
2266
|
+
showStatus("");
|
|
2267
|
+
};
|
|
2268
|
+
// USAGE: editor.setValue([{ name: "Apple", price: 1.2 }])
|
|
2269
|
+
|
|
2270
|
+
// A text that is not JSON (or not a list or an object) shows its error; the table stays.
|
|
2271
|
+
box.setJsonText = function (text) {
|
|
2272
|
+
let data;
|
|
2273
|
+
try {
|
|
2274
|
+
data = JSON.parse(text);
|
|
2275
|
+
} catch (error) {
|
|
2276
|
+
box.errorText = textOf("notJson", { error: error.message });
|
|
2277
|
+
showStatus(box.errorText);
|
|
2278
|
+
return false;
|
|
2279
|
+
}
|
|
2280
|
+
if (!Array.isArray(data) && !JsonEditor.isPlain(data)) {
|
|
2281
|
+
box.errorText = box.texts.listNeeded;
|
|
2282
|
+
showStatus(box.errorText);
|
|
2283
|
+
return false;
|
|
2284
|
+
}
|
|
2285
|
+
box.setValue(data);
|
|
2286
|
+
return true;
|
|
2287
|
+
};
|
|
2288
|
+
// USAGE: editor.setJsonText('[{"name": "Apple"}]')
|
|
2289
|
+
|
|
2290
|
+
// The value as a JSON text of 2 spaces. "": the table can not make a value now.
|
|
2291
|
+
box.getJsonText = function () {
|
|
2292
|
+
const value = box.getValue();
|
|
2293
|
+
if (value === null) return "";
|
|
2294
|
+
return JSON.stringify(value, null, 2);
|
|
2295
|
+
};
|
|
2296
|
+
|
|
2297
|
+
// "table" or "json". false: the text of the JSON view is not right (it stays, the bottom bar says why).
|
|
2298
|
+
box.setView = function (view) {
|
|
2299
|
+
if (view === "json") {
|
|
2300
|
+
showJsonView();
|
|
2301
|
+
return isJsonView;
|
|
2302
|
+
}
|
|
2303
|
+
return leaveJsonView();
|
|
2304
|
+
};
|
|
2305
|
+
// USAGE: editor.setView("json")
|
|
2306
|
+
|
|
2307
|
+
box.getView = function () {
|
|
2308
|
+
return (isJsonView) ? "json" : "table";
|
|
2309
|
+
};
|
|
2310
|
+
|
|
2311
|
+
box.undo = function () { undo(); };
|
|
2312
|
+
box.redo = function () { redo(); };
|
|
2313
|
+
|
|
2314
|
+
// 1: something was changed (also a text written in the JSON view that is not in the table yet).
|
|
2315
|
+
box.hasChanges = function () {
|
|
2316
|
+
const hasJsonChange = (isJsonView && jsonArea.value !== jsonStartText);
|
|
2317
|
+
return (box.isChanged == 1 || hasJsonChange) ? 1 : 0;
|
|
2318
|
+
};
|
|
2319
|
+
|
|
2320
|
+
box.setTitleText = function (titleText, noteText = box.noteText) {
|
|
2321
|
+
box.titleText = titleText;
|
|
2322
|
+
box.noteText = noteText;
|
|
2323
|
+
box.aria = { label: box.ariaLabel || box.titleText || box.texts.table };
|
|
2324
|
+
drawPath();
|
|
2325
|
+
};
|
|
2326
|
+
// USAGE: editor.setTitleText("Products", "A row is a product")
|
|
2327
|
+
|
|
2328
|
+
// The keys go to the table (Ex: after the editor opens in a window).
|
|
2329
|
+
box.focus = function () {
|
|
2330
|
+
content.elem.focus({ preventScroll: true });
|
|
2331
|
+
};
|
|
2332
|
+
|
|
2333
|
+
// *** OBJECT VIEW:
|
|
2334
|
+
|
|
2335
|
+
box.props(_s.box);
|
|
2336
|
+
box.role = "group";
|
|
2337
|
+
box.aria = { label: box.ariaLabel || box.titleText || box.texts.table };
|
|
2338
|
+
|
|
2339
|
+
// GROUP: The parts one under the other: the title, the toolbar, the table, the bottom bar
|
|
2340
|
+
box.layout = VGroup({ left: 0, top: 0, width: "100%", height: "100%", align: "left top", gap: 0, color: "transparent" });
|
|
2341
|
+
box.layout.css = { alignItems: "stretch" };
|
|
2342
|
+
buildTitleBar();
|
|
2343
|
+
buildToolbar();
|
|
2344
|
+
buildBody();
|
|
2345
|
+
buildFooter();
|
|
2346
|
+
endGroup();
|
|
2347
|
+
|
|
2348
|
+
// *** OBJECT INIT CODE:
|
|
2349
|
+
|
|
2350
|
+
basic.addCss("json-editor",
|
|
2351
|
+
".json-editor-field { display: block; width: 100%; height: 100%; box-sizing: border-box; margin: 0; border: 0; outline: none;" +
|
|
2352
|
+
" resize: none; background: transparent; color: var(--json-editor-text); caret-color: var(--json-editor-text);" +
|
|
2353
|
+
" font-family: inherit; line-height: 18px; pointer-events: auto; user-select: text; -webkit-user-select: text; }" +
|
|
2354
|
+
".json-editor-field::placeholder { color: var(--json-editor-placeholder); }" +
|
|
2355
|
+
".json-editor-scroll::-webkit-scrollbar { width: 10px; height: 10px; }" +
|
|
2356
|
+
".json-editor-scroll::-webkit-scrollbar-thumb { background: var(--json-editor-bar); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }" +
|
|
2357
|
+
".json-editor-scroll::-webkit-scrollbar-thumb:hover { background: var(--json-editor-bar-hover); border: 2px solid transparent; background-clip: padding-box; }" +
|
|
2358
|
+
".json-editor-scroll::-webkit-scrollbar-corner { background: transparent; }" +
|
|
2359
|
+
".json-editor-color { position: absolute; width: 1px; height: 1px; padding: 0; margin: 0; border: 0; opacity: 0; pointer-events: none; }");
|
|
2360
|
+
box.elem.style.setProperty("--json-editor-text", C.text);
|
|
2361
|
+
box.elem.style.setProperty("--json-editor-placeholder", C.placeholder);
|
|
2362
|
+
box.elem.style.setProperty("--json-editor-bar", C.scrollBar);
|
|
2363
|
+
box.elem.style.setProperty("--json-editor-bar-hover", C.scrollBarHover);
|
|
2364
|
+
|
|
2365
|
+
content.on("click", onContentClick);
|
|
2366
|
+
content.on("mousemove", onContentMove);
|
|
2367
|
+
content.on("mouseleave", function () {
|
|
2368
|
+
const previousId = hoveredRowId;
|
|
2369
|
+
hoveredRowId = null;
|
|
2370
|
+
paintRow(previousId);
|
|
2371
|
+
});
|
|
2372
|
+
content.on("pointerdown", function (self, event) {
|
|
2373
|
+
if (event.button !== 0 || event.target === editorArea) return;
|
|
2374
|
+
const hit = hitOf(event);
|
|
2375
|
+
if (hit.part === "handle" && hit.row) startRowDrag(event, hit.row);
|
|
2376
|
+
});
|
|
2377
|
+
content.on("contextmenu", function (self, event) {
|
|
2378
|
+
const hit = hitOf(event);
|
|
2379
|
+
event.preventDefault();
|
|
2380
|
+
if (hit.row) openRowMenu(hit.row, event.clientX, event.clientY);
|
|
2381
|
+
});
|
|
2382
|
+
scroller.on("scroll", function () {
|
|
2383
|
+
syncHead();
|
|
2384
|
+
drawRows(false);
|
|
2385
|
+
});
|
|
2386
|
+
scroller.onResize(function () {
|
|
2387
|
+
fitColumns();
|
|
2388
|
+
drawHead();
|
|
2389
|
+
drawRows(true);
|
|
2390
|
+
placeActive();
|
|
2391
|
+
});
|
|
2392
|
+
editorArea.addEventListener("input", fitEditorHeight);
|
|
2393
|
+
editorArea.addEventListener("blur", function () {
|
|
2394
|
+
if (!box._isRemoved) commitEdit();
|
|
2395
|
+
});
|
|
2396
|
+
jsonArea.addEventListener("keydown", function (event) {
|
|
2397
|
+
if (event.key !== "Tab") return;
|
|
2398
|
+
event.preventDefault();
|
|
2399
|
+
jsonArea.setRangeText(" ", jsonArea.selectionStart, jsonArea.selectionEnd, "end");
|
|
2400
|
+
});
|
|
2401
|
+
box.on("keydown", onKeyDown);
|
|
2402
|
+
box.on("paste", onPaste);
|
|
2403
|
+
box.on("copy", onCopy);
|
|
2404
|
+
|
|
2405
|
+
openLevel("", box.value, box.columns, null);
|
|
2406
|
+
|
|
2407
|
+
return endObject(box);
|
|
2408
|
+
|
|
2409
|
+
};
|
|
2410
|
+
|
|
2411
|
+
// *** STATIC VARIABLES AND FUNCTIONS:
|
|
2412
|
+
|
|
2413
|
+
JsonEditor.SIZES = {
|
|
2414
|
+
HANDLE_WIDTH: 22,
|
|
2415
|
+
NUMBER_WIDTH: 38,
|
|
2416
|
+
NUMBER_SPACE: 8, // Between a number and its row
|
|
2417
|
+
END_WIDTH: 30,
|
|
2418
|
+
MIN_COLUMN: 64,
|
|
2419
|
+
MAX_AUTO_COLUMN: 320,
|
|
2420
|
+
COLUMN_EDGE_WIDTH: 7,
|
|
2421
|
+
CELL_PADDING: 8,
|
|
2422
|
+
CELL_GAP: 6,
|
|
2423
|
+
CELL_TEXT_ROOM: 20, // The padding of a cell, in its first width
|
|
2424
|
+
CELL_ICON_ROOM: 26, // A swatch, a picture or ⌄ in a cell
|
|
2425
|
+
SWATCH_SIZE: 14,
|
|
2426
|
+
SWATCH_HIT: 26, // A click at the start of a color cell opens the color picker
|
|
2427
|
+
OPTION_HIT: 26, // A click at the end of a cell of choices opens its list
|
|
2428
|
+
SWITCH_WIDTH: 24,
|
|
2429
|
+
SWITCH_HEIGHT: 14,
|
|
2430
|
+
SWITCH_KNOB: 10,
|
|
2431
|
+
SWITCH_SPACE: 2,
|
|
2432
|
+
ICON: 14,
|
|
2433
|
+
HEAD_BUTTON_WIDTH: 20,
|
|
2434
|
+
HEAD_BUTTON_HEIGHT: 22,
|
|
2435
|
+
ADD_ROW_WIDTH: 120,
|
|
2436
|
+
ADD_ROW_SPACE: 12,
|
|
2437
|
+
ROW_BUFFER: 12, // Rows drawn out of sight: a small scroll draws nothing
|
|
2438
|
+
MEASURED_ROWS: 200, // The first width of a column is read from these rows
|
|
2439
|
+
HISTORY_LIMIT: 100,
|
|
2440
|
+
DRAG_SCROLL_EDGE: 28,
|
|
2441
|
+
DRAG_SCROLL_STEP: 14,
|
|
2442
|
+
EDITOR_LINE_HEIGHT: 18,
|
|
2443
|
+
EDITOR_MAX_LINES: 6,
|
|
2444
|
+
EDITOR_SPACE: 12, // Over and under the lines of a field of many lines
|
|
2445
|
+
EDITOR_MIN_WIDTH: 140,
|
|
2446
|
+
EDITOR_PADDING: "5px 8px",
|
|
2447
|
+
JSON_PADDING: "12px 16px",
|
|
2448
|
+
NAME_FIELD_WIDTH: 200,
|
|
2449
|
+
NAME_FIELD_HEIGHT: 28,
|
|
2450
|
+
NAME_FIELD_Z_INDEX: 5,
|
|
2451
|
+
MENU_MIN_WIDTH: 200,
|
|
2452
|
+
MENU_SPACE: 2, // Between a button and its menu
|
|
2453
|
+
BAR_SPACE: 14, // At the sides of the bars
|
|
2454
|
+
TITLE_SPACE: 10, // Over and under the title
|
|
2455
|
+
TITLE_HEIGHT: 26,
|
|
2456
|
+
NOTE_HEIGHT: 16,
|
|
2457
|
+
TOOLBAR_SPACE: 8,
|
|
2458
|
+
TOOLBAR_LINE_HEIGHT: 20,
|
|
2459
|
+
SEARCH_WIDTH: 220,
|
|
2460
|
+
SEARCH_BASIS: 160,
|
|
2461
|
+
SEARCH_MIN_WIDTH: 120,
|
|
2462
|
+
SEARCH_SPACE: 8, // Before the search (the buttons before it end with no line)
|
|
2463
|
+
VIEW_SWITCH_WIDTH: 130,
|
|
2464
|
+
VIEW_SWITCH_HEIGHT: 24,
|
|
2465
|
+
VIEW_SWITCH_ROUND: 5,
|
|
2466
|
+
VIEW_SWITCH_INSET: 2, // px between a line around Table | JSON and its chosen part
|
|
2467
|
+
FOOTER_HEIGHT: 46,
|
|
2468
|
+
BUTTON_HEIGHT: 28,
|
|
2469
|
+
ICON_BUTTON: 24,
|
|
2470
|
+
CLOSE_BUTTON: 26,
|
|
2471
|
+
SURE_TIME: 5000, // ms: Cancel asks "Discard the changes?" this long
|
|
2472
|
+
NARROW_PAGE: 600, // px: the window has a small margin on a narrower page
|
|
2473
|
+
};
|
|
2474
|
+
|
|
2475
|
+
JsonEditor.PLAIN_KEY = "(value)"; // The column of a row that is a plain value ("A", 12)
|
|
2476
|
+
JsonEditor.NAME_KEY = "(name)"; // An object: the name of a row
|
|
2477
|
+
JsonEditor.VALUE_KEY = "(data)"; // An object: the value of a row
|
|
2478
|
+
JsonEditor.TYPES = ["", "text", "number", "flag", "color", "picture"]; // The types of the menu of a column ("": from its values)
|
|
2479
|
+
JsonEditor.CHECKER = "repeating-conic-gradient(#BBBBBB 0% 25%, #FFFFFF 0% 50%) 0 0 / 6px 6px";
|
|
2480
|
+
JsonEditor.nextRowId = 1;
|
|
2481
|
+
|
|
2482
|
+
// *** THE DATA OF A TABLE: a list or an object <-> rows and columns. Nothing here draws.
|
|
2483
|
+
|
|
2484
|
+
// The value of a new editor: a list or an object. A JSON text is read; anything else is an empty list.
|
|
2485
|
+
JsonEditor.rootValueOf = function (value) {
|
|
2486
|
+
if (Array.isArray(value) || JsonEditor.isPlain(value)) return value;
|
|
2487
|
+
if (typeof value !== "string") return [];
|
|
2488
|
+
const data = JsonEditor.jsonOrText(value);
|
|
2489
|
+
if (Array.isArray(data) || JsonEditor.isPlain(data)) return data;
|
|
2490
|
+
return [];
|
|
2491
|
+
};
|
|
2492
|
+
|
|
2493
|
+
// WHY the prototype of the prototype: an object of another window (an iframe) has another Object.
|
|
2494
|
+
JsonEditor.isPlain = function (value) {
|
|
2495
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2496
|
+
const proto = Object.getPrototypeOf(value);
|
|
2497
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
2498
|
+
};
|
|
2499
|
+
|
|
2500
|
+
// A Date, an object of basic.js... It stays in its cell, but it can not be written.
|
|
2501
|
+
JsonEditor.isOtherObject = function (value) {
|
|
2502
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && !JsonEditor.isPlain(value);
|
|
2503
|
+
};
|
|
2504
|
+
|
|
2505
|
+
// WHY not a short list of numbers: [14, 10] is read and written faster as a text than as a table of its own.
|
|
2506
|
+
JsonEditor.isNestedValue = function (value) {
|
|
2507
|
+
const isNumbers = (Array.isArray(value) && value.every(function (item) { return typeof item === "number"; }));
|
|
2508
|
+
if (isNumbers && value.length && value.length <= 4) return false;
|
|
2509
|
+
return Array.isArray(value) || JsonEditor.isPlain(value);
|
|
2510
|
+
};
|
|
2511
|
+
|
|
2512
|
+
// shape: "object" { a: 1 }, "array" [1, 2], "plain" "A", "pair" a row of an object.
|
|
2513
|
+
JsonEditor.newRow = function (shape, cells) {
|
|
2514
|
+
return { id: JsonEditor.nextRowId++, shape: shape, cells: cells };
|
|
2515
|
+
};
|
|
2516
|
+
|
|
2517
|
+
JsonEditor.rowsOfList = function (list) {
|
|
2518
|
+
return list.map(function (item) {
|
|
2519
|
+
if (JsonEditor.isPlain(item)) return JsonEditor.newRow("object", Object.assign({}, item));
|
|
2520
|
+
if (Array.isArray(item)) {
|
|
2521
|
+
const cells = {};
|
|
2522
|
+
item.forEach(function (value, index) { cells[index] = value; });
|
|
2523
|
+
return JsonEditor.newRow("array", cells);
|
|
2524
|
+
}
|
|
2525
|
+
const plainCells = {};
|
|
2526
|
+
plainCells[JsonEditor.PLAIN_KEY] = item;
|
|
2527
|
+
return JsonEditor.newRow("plain", plainCells);
|
|
2528
|
+
});
|
|
2529
|
+
};
|
|
2530
|
+
|
|
2531
|
+
// hint: what the page said about the column (A COLUMN)
|
|
2532
|
+
JsonEditor.newColumn = function (key, hint) {
|
|
2533
|
+
const column = { key: key, width: JsonEditor.SIZES.MIN_COLUMN, note: "", options: null, chosenKind: "" };
|
|
2534
|
+
if (!hint) return column;
|
|
2535
|
+
column.note = hint.note || "";
|
|
2536
|
+
if (Array.isArray(hint.options) && hint.options.length) column.options = hint.options.slice();
|
|
2537
|
+
if (hint.kind) column.chosenKind = hint.kind;
|
|
2538
|
+
return column;
|
|
2539
|
+
};
|
|
2540
|
+
|
|
2541
|
+
// The columns of the rows, in the order of their first use. An empty list: the columns the page named.
|
|
2542
|
+
JsonEditor.columnsOfRows = function (rows, hints) {
|
|
2543
|
+
const columns = [];
|
|
2544
|
+
const hintOf = function (key) { return hints.find(function (hint) { return hint.key === key; }); };
|
|
2545
|
+
const add = function (key) {
|
|
2546
|
+
const isKnown = columns.some(function (column) { return column.key === key; });
|
|
2547
|
+
if (!isKnown) columns.push(JsonEditor.newColumn(key, hintOf(key)));
|
|
2548
|
+
};
|
|
2549
|
+
if (rows.some(function (row) { return row.shape === "plain"; })) add(JsonEditor.PLAIN_KEY);
|
|
2550
|
+
rows.forEach(function (row) {
|
|
2551
|
+
if (row.shape === "array") Object.keys(row.cells).forEach(add);
|
|
2552
|
+
});
|
|
2553
|
+
rows.forEach(function (row) {
|
|
2554
|
+
if (row.shape === "object") Object.keys(row.cells).forEach(add);
|
|
2555
|
+
});
|
|
2556
|
+
if (!columns.length) hints.forEach(function (hint) { add(hint.key); });
|
|
2557
|
+
if (!columns.length) add(JsonEditor.PLAIN_KEY);
|
|
2558
|
+
return columns;
|
|
2559
|
+
};
|
|
2560
|
+
|
|
2561
|
+
// A list or an object -> { isObject, rows, columns }
|
|
2562
|
+
JsonEditor.levelOf = function (value, hints) {
|
|
2563
|
+
if (Array.isArray(value)) {
|
|
2564
|
+
const listRows = JsonEditor.rowsOfList(value);
|
|
2565
|
+
return { isObject: false, rows: listRows, columns: JsonEditor.columnsOfRows(listRows, hints || []) };
|
|
2566
|
+
}
|
|
2567
|
+
const pairRows = Object.keys(value).map(function (name) {
|
|
2568
|
+
const cells = {};
|
|
2569
|
+
cells[JsonEditor.NAME_KEY] = name;
|
|
2570
|
+
cells[JsonEditor.VALUE_KEY] = value[name];
|
|
2571
|
+
return JsonEditor.newRow("pair", cells);
|
|
2572
|
+
});
|
|
2573
|
+
const columns = [JsonEditor.newColumn(JsonEditor.NAME_KEY), JsonEditor.newColumn(JsonEditor.VALUE_KEY)];
|
|
2574
|
+
return { isObject: true, rows: pairRows, columns: columns };
|
|
2575
|
+
};
|
|
2576
|
+
|
|
2577
|
+
// A row -> its item. WHY the plain cell can be next to other cells: a plain row that got a value in another column becomes
|
|
2578
|
+
// an object, and its value stays (with the name "value"; renaming the column turns all of them into that name).
|
|
2579
|
+
JsonEditor.valueOfRow = function (row, columns) {
|
|
2580
|
+
const PLAIN_KEY = JsonEditor.PLAIN_KEY;
|
|
2581
|
+
if (row.shape === "array") {
|
|
2582
|
+
const indexColumns = columns.filter(function (column) { return /^\d+$/.test(column.key); });
|
|
2583
|
+
const list = indexColumns.map(function (column) { return row.cells[column.key]; });
|
|
2584
|
+
while (list.length && list[list.length - 1] === undefined) list.pop();
|
|
2585
|
+
return list.map(function (item) { return (item === undefined) ? null : item; });
|
|
2586
|
+
}
|
|
2587
|
+
const named = columns.filter(function (column) { return column.key !== PLAIN_KEY && row.cells[column.key] !== undefined; });
|
|
2588
|
+
const plain = row.cells[PLAIN_KEY];
|
|
2589
|
+
if (!named.length && plain !== undefined) return plain;
|
|
2590
|
+
if (!named.length && row.shape === "plain") return "";
|
|
2591
|
+
const item = {};
|
|
2592
|
+
if (plain !== undefined) item.value = plain;
|
|
2593
|
+
named.forEach(function (column) { item[column.key] = row.cells[column.key]; });
|
|
2594
|
+
// WHY: a function of a column that is not shown any more (a row of another shape) is not lost.
|
|
2595
|
+
Object.keys(row.cells).forEach(function (key) {
|
|
2596
|
+
const isHiddenFunction = (key !== PLAIN_KEY && !(key in item) && typeof row.cells[key] === "function");
|
|
2597
|
+
if (isHiddenFunction) item[key] = row.cells[key];
|
|
2598
|
+
});
|
|
2599
|
+
return item;
|
|
2600
|
+
};
|
|
2601
|
+
|
|
2602
|
+
// The table -> its list or its object. openCell: { rowId, columnKey, value }: a cell whose table is open now.
|
|
2603
|
+
// A name that is empty or used twice throws (error.textKey: a key of texts).
|
|
2604
|
+
JsonEditor.valueOfLevel = function (level, openCell) {
|
|
2605
|
+
const cellsOf = function (row) {
|
|
2606
|
+
if (!openCell || row.id !== openCell.rowId) return row.cells;
|
|
2607
|
+
const cells = Object.assign({}, row.cells);
|
|
2608
|
+
cells[openCell.columnKey] = openCell.value;
|
|
2609
|
+
return cells;
|
|
2610
|
+
};
|
|
2611
|
+
if (!level.isObject) {
|
|
2612
|
+
return level.rows.map(function (row) {
|
|
2613
|
+
return JsonEditor.valueOfRow({ shape: row.shape, cells: cellsOf(row) }, level.columns);
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
const object = {};
|
|
2617
|
+
level.rows.forEach(function (row) {
|
|
2618
|
+
const cells = cellsOf(row);
|
|
2619
|
+
const name = String(cells[JsonEditor.NAME_KEY] ?? "").trim();
|
|
2620
|
+
if (name === "") throw JsonEditor.tableError("noName", "");
|
|
2621
|
+
if (Object.prototype.hasOwnProperty.call(object, name)) throw JsonEditor.tableError("sameName", name);
|
|
2622
|
+
const value = cells[JsonEditor.VALUE_KEY];
|
|
2623
|
+
object[name] = (value === undefined) ? "" : value;
|
|
2624
|
+
});
|
|
2625
|
+
return object;
|
|
2626
|
+
};
|
|
2627
|
+
|
|
2628
|
+
JsonEditor.tableError = function (textKey, valueName) {
|
|
2629
|
+
const error = new Error(textKey);
|
|
2630
|
+
error.textKey = textKey;
|
|
2631
|
+
error.valueName = valueName;
|
|
2632
|
+
return error;
|
|
2633
|
+
};
|
|
2634
|
+
|
|
2635
|
+
// A value as the text of its cell. A function has no text: its code is not data.
|
|
2636
|
+
JsonEditor.textOfValue = function (value) {
|
|
2637
|
+
if (value === undefined || typeof value === "function") return "";
|
|
2638
|
+
if (typeof value === "string") return value;
|
|
2639
|
+
if (JsonEditor.isOtherObject(value)) return "[" + ((value.constructor && value.constructor.name) || "object") + "]";
|
|
2640
|
+
try {
|
|
2641
|
+
return JSON.stringify(value);
|
|
2642
|
+
} catch (error) {
|
|
2643
|
+
return String(value); // WHY: a value that holds itself
|
|
2644
|
+
}
|
|
2645
|
+
};
|
|
2646
|
+
|
|
2647
|
+
// A list or an object in a cell: [ 3 ] or { 2 }
|
|
2648
|
+
JsonEditor.countTextOf = function (value) {
|
|
2649
|
+
if (Array.isArray(value)) return "[ " + value.length + " ]";
|
|
2650
|
+
return "{ " + Object.keys(value).length + " }";
|
|
2651
|
+
};
|
|
2652
|
+
|
|
2653
|
+
// A picture written in the code (a data address, often long) is shown short: what it is and how big.
|
|
2654
|
+
JsonEditor.shortPicture = function (text, template) {
|
|
2655
|
+
if (!/^data:/i.test(text)) return text;
|
|
2656
|
+
const kind = (text.match(/^data:([^;,]+)/i) || [])[1] || "data";
|
|
2657
|
+
const size = Math.round(text.length / 1024 * 10) / 10;
|
|
2658
|
+
return JsonEditor.fillText(template, { kind: kind, size: size });
|
|
2659
|
+
};
|
|
2660
|
+
|
|
2661
|
+
// The address of a picture: a text of an SVG becomes a data address; a relative path is read from base (when given).
|
|
2662
|
+
JsonEditor.pictureAddress = function (value, base) {
|
|
2663
|
+
const text = String(value ?? "").trim();
|
|
2664
|
+
if (/^<svg\b/i.test(text)) return "data:image/svg+xml;utf8," + encodeURIComponent(text);
|
|
2665
|
+
if (!text || !base) return text;
|
|
2666
|
+
try {
|
|
2667
|
+
return new URL(text, base).href;
|
|
2668
|
+
} catch (error) {
|
|
2669
|
+
return text; // WHY: not an address (a typed text); the picture shows nothing
|
|
2670
|
+
}
|
|
2671
|
+
};
|
|
2672
|
+
|
|
2673
|
+
// What sorts first: numbers by their size, texts as people read them, an empty cell at the end.
|
|
2674
|
+
JsonEditor.compareValues = function (a, b) {
|
|
2675
|
+
const isAEmpty = (a === undefined || a === null || a === "");
|
|
2676
|
+
const isBEmpty = (b === undefined || b === null || b === "");
|
|
2677
|
+
if (isAEmpty || isBEmpty) return (isAEmpty ? 1 : 0) - (isBEmpty ? 1 : 0);
|
|
2678
|
+
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
2679
|
+
return JsonEditor.textOfValue(a).localeCompare(JsonEditor.textOfValue(b), undefined, { numeric: true, sensitivity: "base" });
|
|
2680
|
+
};
|
|
2681
|
+
|
|
2682
|
+
// A pasted text -> { list } (JSON) or { grid } (lines of cells: a spreadsheet, or a text of lines), or null (one plain text).
|
|
2683
|
+
JsonEditor.readPastedText = function (text) {
|
|
2684
|
+
const trimmed = String(text).trim();
|
|
2685
|
+
if (/^[[{]/.test(trimmed)) {
|
|
2686
|
+
const data = JsonEditor.jsonOrText(trimmed);
|
|
2687
|
+
if (Array.isArray(data)) return { list: data };
|
|
2688
|
+
if (JsonEditor.isPlain(data)) return { list: [data] };
|
|
2689
|
+
}
|
|
2690
|
+
const hasTabs = (trimmed.indexOf("\t") !== -1);
|
|
2691
|
+
const hasLines = (trimmed.indexOf("\n") !== -1);
|
|
2692
|
+
if (!hasTabs && !hasLines) return null;
|
|
2693
|
+
return { grid: trimmed.split(/\r?\n/).map(function (line) { return line.split("\t"); }) };
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
JsonEditor.jsonOrText = function (text) {
|
|
2697
|
+
try {
|
|
2698
|
+
return JSON.parse(text);
|
|
2699
|
+
} catch (error) {
|
|
2700
|
+
return text;
|
|
2701
|
+
}
|
|
2702
|
+
};
|
|
2703
|
+
|
|
2704
|
+
JsonEditor.numberOfText = function (text) {
|
|
2705
|
+
const trimmed = String(text).trim();
|
|
2706
|
+
if (trimmed === "" || isNaN(Number(trimmed))) return null;
|
|
2707
|
+
return Number(trimmed);
|
|
2708
|
+
};
|
|
2709
|
+
|
|
2710
|
+
// The choices of a cell, with the value it holds now at the top when it is not one of them.
|
|
2711
|
+
JsonEditor.optionValues = function (values, current) {
|
|
2712
|
+
const list = values.slice();
|
|
2713
|
+
const isValueNow = (typeof current === "string" && current !== "") || typeof current === "number";
|
|
2714
|
+
if (isValueNow && list.indexOf(current) === -1) list.unshift(current);
|
|
2715
|
+
return list;
|
|
2716
|
+
};
|
|
2717
|
+
|
|
2718
|
+
// *** THE KIND OF A VALUE: from its name and itself. WHY a guess is enough: every cell can still be written as a text.
|
|
2719
|
+
|
|
2720
|
+
JsonEditor.COLOR_TEXT = /^(#[0-9a-f]{3,8}|(rgb|hsl)a?\(.*\)|var\(--.*\)|color-mix\(.*\))$/i;
|
|
2721
|
+
JsonEditor.COLOR_KEY = /(^|[a-z_])colou?r$/i;
|
|
2722
|
+
JsonEditor.PICTURE_TEXT = /^(data:image\/|<svg\b)|\.(png|jpe?g|gif|svg|webp|avif|ico)(\?.*)?$/i;
|
|
2723
|
+
JsonEditor.PICTURE_KEY = /(^|[a-z_])(file|src|image|picture|photo|avatar|thumbnail|logo)$/i;
|
|
2724
|
+
JsonEditor.FLAG_START = /^(is|has|can|use|show|allow|must|auto|enable|hide|keep|need)[A-Z_]/;
|
|
2725
|
+
JsonEditor.NUMBER_END = /(time|delay|duration|size|gap|count|round|opacity|index|step|length|width|height)$/i;
|
|
2726
|
+
JsonEditor.FLAG_KEYS = ["enabled", "disabled", "active", "visible", "hidden", "selected", "checked", "done", "opened",
|
|
2727
|
+
"required", "editable", "removable", "animated", "bold", "italic", "published", "featured", "paid", "inStock", "available"];
|
|
2728
|
+
|
|
2729
|
+
JsonEditor.isOnOffValue = function (value) {
|
|
2730
|
+
return value === 0 || value === 1 || value === true || value === false || value === null || value === undefined;
|
|
2731
|
+
};
|
|
2732
|
+
|
|
2733
|
+
// A name that says "on or off": active, isOpen, showPrices...
|
|
2734
|
+
JsonEditor.isFlagName = function (name) {
|
|
2735
|
+
if (JsonEditor.FLAG_KEYS.indexOf(name) !== -1) return true;
|
|
2736
|
+
if (JsonEditor.NUMBER_END.test(name)) return false;
|
|
2737
|
+
return JsonEditor.FLAG_START.test(name);
|
|
2738
|
+
};
|
|
2739
|
+
|
|
2740
|
+
JsonEditor.isCssColor = function (text) {
|
|
2741
|
+
return !!(window.CSS && CSS.supports("color", text));
|
|
2742
|
+
};
|
|
2743
|
+
|
|
2744
|
+
// One value: "function", "color", "picture", "flag", "number", "text" or "any".
|
|
2745
|
+
JsonEditor.kindOfNamedValue = function (name, value) {
|
|
2746
|
+
if (typeof value === "function") return "function";
|
|
2747
|
+
const text = (typeof value === "string") ? value.trim() : "";
|
|
2748
|
+
const isEmpty = (value === null || value === undefined || value === "");
|
|
2749
|
+
const isNumberOrFlag = (typeof value === "number" || typeof value === "boolean");
|
|
2750
|
+
if (JsonEditor.COLOR_TEXT.test(text)) return "color";
|
|
2751
|
+
if (JsonEditor.COLOR_KEY.test(name) && (isEmpty || JsonEditor.isCssColor(text))) return "color";
|
|
2752
|
+
if (JsonEditor.PICTURE_TEXT.test(text)) return "picture";
|
|
2753
|
+
if (JsonEditor.PICTURE_KEY.test(name) && !isNumberOrFlag) return "picture";
|
|
2754
|
+
if (typeof value === "boolean") return "flag";
|
|
2755
|
+
if (JsonEditor.isFlagName(name) && JsonEditor.isOnOffValue(value)) return "flag";
|
|
2756
|
+
if (typeof value === "number") return "number";
|
|
2757
|
+
if (typeof value === "string") return "text";
|
|
2758
|
+
return "any";
|
|
2759
|
+
};
|
|
2760
|
+
|
|
2761
|
+
// A name the table gives a column (a plain value, the name and the value of an object, a place in a list): not of the page.
|
|
2762
|
+
JsonEditor.isOwnKey = function (key) {
|
|
2763
|
+
return key === JsonEditor.PLAIN_KEY || key === JsonEditor.NAME_KEY || key === JsonEditor.VALUE_KEY || /^\d+$/.test(key);
|
|
2764
|
+
};
|
|
2765
|
+
|
|
2766
|
+
// The kind of a column: "flag", "color", "picture", "function", "number", "text" or "auto" (an empty column).
|
|
2767
|
+
JsonEditor.kindOfColumnValues = function (key, values) {
|
|
2768
|
+
const name = (JsonEditor.isOwnKey(key)) ? "" : key;
|
|
2769
|
+
const sample = values.find(function (value) { return value !== null && value !== undefined; });
|
|
2770
|
+
const allAre = function (check) { return values.length > 0 && values.every(check); };
|
|
2771
|
+
if (allAre(function (value) { return typeof value === "boolean"; })) return "flag";
|
|
2772
|
+
const kind = JsonEditor.kindOfNamedValue(name, sample);
|
|
2773
|
+
if (kind === "color" || kind === "picture" || kind === "function") return kind;
|
|
2774
|
+
if (kind === "flag" && allAre(JsonEditor.isOnOffValue)) return "flag";
|
|
2775
|
+
if (allAre(function (value) { return typeof value === "number"; })) return "number";
|
|
2776
|
+
if (!values.length) return (JsonEditor.isFlagName(name)) ? "flag" : "auto";
|
|
2777
|
+
return "text";
|
|
2778
|
+
};
|
|
2779
|
+
|
|
2780
|
+
// *** THE FUNCTIONS OF THE ROWS: a table and a JSON text can not hold a function; the callbacks of the rows (onClick...)
|
|
2781
|
+
// of the old value stay in the new value.
|
|
2782
|
+
|
|
2783
|
+
JsonEditor.keepFunctions = function (value, old) {
|
|
2784
|
+
if (Array.isArray(value) && Array.isArray(old)) {
|
|
2785
|
+
return value.map(function (item, index) { return JsonEditor.keepFunctions(item, JsonEditor.sameRowOf(item, old, index)); });
|
|
2786
|
+
}
|
|
2787
|
+
if (!JsonEditor.isPlain(value) || !JsonEditor.isPlain(old)) return value;
|
|
2788
|
+
const result = {};
|
|
2789
|
+
Object.keys(value).forEach(function (key) {
|
|
2790
|
+
result[key] = JsonEditor.keepFunctions(value[key], old[key]);
|
|
2791
|
+
});
|
|
2792
|
+
Object.keys(old).forEach(function (key) {
|
|
2793
|
+
if (typeof old[key] === "function" && !(key in result)) result[key] = old[key];
|
|
2794
|
+
});
|
|
2795
|
+
return result;
|
|
2796
|
+
};
|
|
2797
|
+
|
|
2798
|
+
// The row of the old list that a new row came from: the same key (a moved row), or the same place.
|
|
2799
|
+
JsonEditor.ROW_ID_KEYS = ["key", "id", "value", "text", "title"];
|
|
2800
|
+
JsonEditor.sameRowOf = function (item, oldRows, index) {
|
|
2801
|
+
if (!JsonEditor.isPlain(item)) return oldRows[index];
|
|
2802
|
+
const idKey = JsonEditor.ROW_ID_KEYS.find(function (key) { return item[key] !== undefined; });
|
|
2803
|
+
if (!idKey) return oldRows[index];
|
|
2804
|
+
const same = oldRows.find(function (row) { return JsonEditor.isPlain(row) && row[idKey] === item[idKey]; });
|
|
2805
|
+
return (same !== undefined) ? same : oldRows[index];
|
|
2806
|
+
};
|
|
2807
|
+
|
|
2808
|
+
// *** HELPERS OF THE VIEW:
|
|
2809
|
+
|
|
2810
|
+
// "{count} rows" + { count: 3 } -> "3 rows"
|
|
2811
|
+
JsonEditor.fillText = function (template, values = {}) {
|
|
2812
|
+
return String(template).replace(/\{(\w+)\}/g, function (whole, name) {
|
|
2813
|
+
return (values[name] === undefined) ? whole : String(values[name]);
|
|
2814
|
+
});
|
|
2815
|
+
};
|
|
2816
|
+
|
|
2817
|
+
JsonEditor.spanOf = function (text) {
|
|
2818
|
+
return '<span style="margin-left:6px">' + basic.escapeHtml(text) + "</span>";
|
|
2819
|
+
};
|
|
2820
|
+
|
|
2821
|
+
// Line icons on a grid of 16, in the color of the text.
|
|
2822
|
+
JsonEditor.ICONS = {
|
|
2823
|
+
chevron: "M4.5 6.5L8 10l3.5-3.5",
|
|
2824
|
+
table: "M2 3h12v10H2zM2 6.5h12M2 10h12M6.5 3v10",
|
|
2825
|
+
back: "M5 3.5L2 6.5l3 3M2 6.5h7.5a3.5 3.5 0 0 1 0 7H7",
|
|
2826
|
+
forward: "M11 3.5l3 3-3 3M14 6.5H6.5a3.5 3.5 0 0 0 0 7H9",
|
|
2827
|
+
close: "M4 4l8 8M12 4l-8 8",
|
|
2828
|
+
plus: "M8 3v10M3 8h10",
|
|
2829
|
+
trash: "M3 4.5h10M6.5 4.5V3h3v1.5M4.5 4.5l.7 8.5h5.6l.7-8.5",
|
|
2830
|
+
copy: "M5.5 5.5h8v8h-8zM2.5 10.5v-8h8",
|
|
2831
|
+
up: "M8 13V3M4 7l4-4 4 4",
|
|
2832
|
+
down: "M8 3v10M4 9l4 4 4-4",
|
|
2833
|
+
dots: "M6 3.5v.01M10 3.5v.01M6 8v.01M10 8v.01M6 12.5v.01M10 12.5v.01",
|
|
2834
|
+
};
|
|
2835
|
+
|
|
2836
|
+
JsonEditor.iconOf = function (name, size) {
|
|
2837
|
+
return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 16 16" fill="none"' +
|
|
2838
|
+
' stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" style="display:block"><path d="' +
|
|
2839
|
+
JsonEditor.ICONS[name] + '"/></svg>';
|
|
2840
|
+
};
|
|
2841
|
+
|
|
2842
|
+
// The looks of the buttons (the toolbar, the bottom bar, the window) from the colors of a style.
|
|
2843
|
+
JsonEditor.looksOf = function (style) {
|
|
2844
|
+
const C = style.colors;
|
|
2845
|
+
return {
|
|
2846
|
+
button: { color: C.field, hoverColor: C.fieldHover, lineColor: C.fieldLine, hoverLineColor: C.fieldLineHover, textColor: C.text, ringColor: C.focus, fontSize: style.fontSize },
|
|
2847
|
+
accent: { color: C.accent, hoverColor: C.accentHover, lineColor: "", hoverLineColor: "", textColor: C.onAccent, ringColor: C.focus, fontSize: style.fontSize },
|
|
2848
|
+
icon: { color: "transparent", hoverColor: C.hover, textColor: C.icon, ringColor: C.focus },
|
|
2849
|
+
};
|
|
2850
|
+
};
|
|
2851
|
+
|
|
2852
|
+
// LABEL: a button of a text (htmlText: an icon may be in it).
|
|
2853
|
+
// look: { color, hoverColor, lineColor, hoverLineColor ("": no line), textColor, ringColor, fontSize }
|
|
2854
|
+
JsonEditor.textButton = function (htmlText, look, onPress) {
|
|
2855
|
+
const btn = Label({
|
|
2856
|
+
text: htmlText,
|
|
2857
|
+
height: JsonEditor.SIZES.BUTTON_HEIGHT,
|
|
2858
|
+
fontSize: look.fontSize,
|
|
2859
|
+
textColor: look.textColor,
|
|
2860
|
+
color: look.color,
|
|
2861
|
+
round: 5,
|
|
2862
|
+
clickable: 1,
|
|
2863
|
+
cursor: "pointer",
|
|
2864
|
+
});
|
|
2865
|
+
btn.shrink = 0;
|
|
2866
|
+
btn.css = { padding: "0px 10px", display: "flex", alignItems: "center", justifyContent: "center", whiteSpace: "nowrap", transition: "background-color 0.12s" };
|
|
2867
|
+
btn.role = "button";
|
|
2868
|
+
btn.tabIndex = 0;
|
|
2869
|
+
btn.setFocusRing(look.ringColor);
|
|
2870
|
+
if (look.lineColor) btn.props({ border: 1, borderColor: look.lineColor });
|
|
2871
|
+
btn.onHoverChange(function (self, isOver) {
|
|
2872
|
+
btn.color = (isOver) ? look.hoverColor : look.color;
|
|
2873
|
+
if (look.lineColor) btn.borderColor = (isOver) ? look.hoverLineColor : look.lineColor;
|
|
2874
|
+
});
|
|
2875
|
+
btn.on("click", function () { onPress(btn); });
|
|
2876
|
+
btn.on("keydown", function (self, event) {
|
|
2877
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
2878
|
+
event.preventDefault();
|
|
2879
|
+
onPress(btn);
|
|
2880
|
+
});
|
|
2881
|
+
return btn;
|
|
2882
|
+
};
|
|
2883
|
+
|
|
2884
|
+
// LABEL: a button of an icon; its name is its tip. setEnabled(isOn): a pale button that does nothing.
|
|
2885
|
+
JsonEditor.iconButton = function (iconName, nameText, look, onPress, size = JsonEditor.SIZES.ICON_BUTTON) {
|
|
2886
|
+
let isEnabled = true;
|
|
2887
|
+
const btn = Label({
|
|
2888
|
+
text: JsonEditor.iconOf(iconName, JsonEditor.SIZES.ICON),
|
|
2889
|
+
width: size,
|
|
2890
|
+
height: size,
|
|
2891
|
+
color: look.color,
|
|
2892
|
+
textColor: look.textColor,
|
|
2893
|
+
round: 6,
|
|
2894
|
+
clickable: 1,
|
|
2895
|
+
cursor: "pointer",
|
|
2896
|
+
lineHeight: "0",
|
|
2897
|
+
});
|
|
2898
|
+
btn.shrink = 0;
|
|
2899
|
+
btn.css = { display: "flex", alignItems: "center", justifyContent: "center", transition: "background-color 0.12s, opacity 0.12s" };
|
|
2900
|
+
btn.role = "button";
|
|
2901
|
+
btn.aria = { label: nameText };
|
|
2902
|
+
btn.elem.title = nameText;
|
|
2903
|
+
btn.setFocusRing(look.ringColor);
|
|
2904
|
+
const paint = function (isOver) {
|
|
2905
|
+
btn.color = (isOver && isEnabled) ? look.hoverColor : look.color;
|
|
2906
|
+
btn.opacity = (isEnabled) ? 1 : 0.35;
|
|
2907
|
+
btn.cursor = (isEnabled) ? "pointer" : "default";
|
|
2908
|
+
btn.tabIndex = (isEnabled) ? 0 : -1;
|
|
2909
|
+
btn.aria = { disabled: !isEnabled };
|
|
2910
|
+
};
|
|
2911
|
+
const press = function () {
|
|
2912
|
+
if (isEnabled) onPress(btn);
|
|
2913
|
+
};
|
|
2914
|
+
btn.setEnabled = function (value) {
|
|
2915
|
+
isEnabled = !!value;
|
|
2916
|
+
paint(false);
|
|
2917
|
+
};
|
|
2918
|
+
btn.onHoverChange(function (self, isOver) { paint(isOver); });
|
|
2919
|
+
btn.on("click", press);
|
|
2920
|
+
btn.on("keydown", function (self, event) {
|
|
2921
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
2922
|
+
event.preventDefault();
|
|
2923
|
+
press();
|
|
2924
|
+
});
|
|
2925
|
+
paint(false);
|
|
2926
|
+
return btn;
|
|
2927
|
+
};
|
|
2928
|
+
|
|
2929
|
+
// A color text -> "#RRGGBB" (the color picker of the browser takes no other form). Not a color: black.
|
|
2930
|
+
JsonEditor.hexOfColor = function (text) {
|
|
2931
|
+
const context = document.createElement("canvas").getContext("2d");
|
|
2932
|
+
context.fillStyle = "#000000";
|
|
2933
|
+
context.fillStyle = String(text ?? "").trim();
|
|
2934
|
+
const color = context.fillStyle;
|
|
2935
|
+
if (color.charAt(0) === "#") return color;
|
|
2936
|
+
const parts = (color.match(/[\d.]+/g) || []).slice(0, 3);
|
|
2937
|
+
return "#" + parts.map(function (part) { return Number(part).toString(16).padStart(2, "0"); }).join("");
|
|
2938
|
+
};
|
|
2939
|
+
|
|
2940
|
+
// Opens the picker of a field of the browser (a color). WHY the old way too: showPicker() is newer than the field.
|
|
2941
|
+
JsonEditor.openPicker = function (field) {
|
|
2942
|
+
try {
|
|
2943
|
+
if (field.showPicker) {
|
|
2944
|
+
field.showPicker();
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
} catch (error) { }
|
|
2948
|
+
field.click();
|
|
2949
|
+
};
|
|
2950
|
+
|
|
2951
|
+
// Copies a text. WHY the old way too: the Clipboard API is refused in a page that is not secure or not in focus.
|
|
2952
|
+
JsonEditor.copyText = function (text) {
|
|
2953
|
+
if (navigator.clipboard && window.isSecureContext) {
|
|
2954
|
+
return navigator.clipboard.writeText(text).catch(function () { return JsonEditor.copyTextOldWay(text); });
|
|
2955
|
+
}
|
|
2956
|
+
return JsonEditor.copyTextOldWay(text);
|
|
2957
|
+
};
|
|
2958
|
+
|
|
2959
|
+
JsonEditor.copyTextOldWay = function (text) {
|
|
2960
|
+
const area = document.createElement("textarea");
|
|
2961
|
+
area.value = text;
|
|
2962
|
+
document.body.appendChild(area);
|
|
2963
|
+
area.select();
|
|
2964
|
+
const isCopied = document.execCommand("copy");
|
|
2965
|
+
area.remove();
|
|
2966
|
+
if (!isCopied) return Promise.reject(new Error("The browser did not copy it."));
|
|
2967
|
+
return Promise.resolve();
|
|
2968
|
+
};
|
|
2969
|
+
|
|
2970
|
+
// *** THE WINDOW: the editor over the page, with Cancel and Apply.
|
|
2971
|
+
|
|
2972
|
+
// options: the params of JsonEditor, onApply(value) (Apply, when something was changed), onCancel() and onClose() (the window
|
|
2973
|
+
// closed: after Apply, Cancel or win.close()). Returns the window: win.editor is its JsonEditor, win.close() closes it.
|
|
2974
|
+
JsonEditor.open = function (options = {}) {
|
|
2975
|
+
|
|
2976
|
+
const params = Object.assign({}, options);
|
|
2977
|
+
const onApply = params.onApply || function (value) { };
|
|
2978
|
+
const onCancel = params.onCancel || function () { };
|
|
2979
|
+
const onClose = params.onClose || function () { };
|
|
2980
|
+
const onChange = params.onChange || function (self, value) { };
|
|
2981
|
+
delete params.onApply;
|
|
2982
|
+
delete params.onCancel;
|
|
2983
|
+
delete params.onClose;
|
|
2984
|
+
const opener = document.activeElement; // WHY: the focus goes back to it, and a window opened from a Dialog goes over it
|
|
2985
|
+
let editor = null;
|
|
2986
|
+
let btnCancel = null;
|
|
2987
|
+
let isSureToCancel = 0;
|
|
2988
|
+
let stopSureTimer = null;
|
|
2989
|
+
|
|
2990
|
+
const win = createIn(page, function () {
|
|
2991
|
+
// BOX: The cover over the page (the window is in it)
|
|
2992
|
+
const cover = startBox(0, 0, "100%", "100%", { color: "transparent" });
|
|
2993
|
+
cover.clickable = 1;
|
|
2994
|
+
editor = JsonEditor(Object.assign(params, {
|
|
2995
|
+
showTitleBar: 1,
|
|
2996
|
+
onChange: function (self, value) {
|
|
2997
|
+
resetCancel(); // WHY: a change after the first press of Cancel asks again
|
|
2998
|
+
onChange(self, value);
|
|
2999
|
+
},
|
|
3000
|
+
}));
|
|
3001
|
+
endBox();
|
|
3002
|
+
return cover;
|
|
3003
|
+
});
|
|
3004
|
+
const look = editor.style.window;
|
|
3005
|
+
const looks = JsonEditor.looksOf(editor.style);
|
|
3006
|
+
const texts = editor.texts;
|
|
3007
|
+
|
|
3008
|
+
// PRIVATE FUNCTIONS:
|
|
3009
|
+
|
|
3010
|
+
const placeWindow = function () {
|
|
3011
|
+
const margin = (page.width < JsonEditor.SIZES.NARROW_PAGE) ? look.narrowMargin : look.margin;
|
|
3012
|
+
editor.left = margin;
|
|
3013
|
+
editor.top = margin;
|
|
3014
|
+
editor.width = "calc(100% - " + (margin * 2) + "px)";
|
|
3015
|
+
editor.height = "calc(100% - " + (margin * 2) + "px)";
|
|
3016
|
+
};
|
|
3017
|
+
|
|
3018
|
+
const close = function () {
|
|
3019
|
+
if (win._isRemoved) return;
|
|
3020
|
+
win.remove();
|
|
3021
|
+
if (opener && opener.focus && opener.isConnected) opener.focus({ preventScroll: true });
|
|
3022
|
+
onClose();
|
|
3023
|
+
};
|
|
3024
|
+
|
|
3025
|
+
const apply = function () {
|
|
3026
|
+
const value = editor.getValue();
|
|
3027
|
+
if (value === null) return; // WHY: the bottom bar of the editor says why (a name used twice...)
|
|
3028
|
+
const isChanged = editor.hasChanges();
|
|
3029
|
+
close();
|
|
3030
|
+
if (isChanged) onApply(value);
|
|
3031
|
+
};
|
|
3032
|
+
|
|
3033
|
+
// WHY two presses: a table can hold many typed values. The first press asks; it ends by itself.
|
|
3034
|
+
const requestCancel = function () {
|
|
3035
|
+
if (!editor.hasChanges() || isSureToCancel) {
|
|
3036
|
+
close();
|
|
3037
|
+
onCancel();
|
|
3038
|
+
return;
|
|
3039
|
+
}
|
|
3040
|
+
isSureToCancel = 1;
|
|
3041
|
+
btnCancel.text = basic.escapeHtml(texts.discard);
|
|
3042
|
+
if (stopSureTimer) stopSureTimer();
|
|
3043
|
+
stopSureTimer = win.after(JsonEditor.SIZES.SURE_TIME, resetCancel);
|
|
3044
|
+
};
|
|
3045
|
+
|
|
3046
|
+
const resetCancel = function () {
|
|
3047
|
+
isSureToCancel = 0;
|
|
3048
|
+
if (btnCancel) btnCancel.text = basic.escapeHtml(texts.cancel);
|
|
3049
|
+
};
|
|
3050
|
+
|
|
3051
|
+
// THE KEYS: the ones the editor did not use (it stops them).
|
|
3052
|
+
const onKeyDown = function (self, event) {
|
|
3053
|
+
if (event.defaultPrevented) return;
|
|
3054
|
+
const isCommand = (event.metaKey || event.ctrlKey);
|
|
3055
|
+
if (event.key === "Enter" && isCommand) {
|
|
3056
|
+
event.preventDefault();
|
|
3057
|
+
apply();
|
|
3058
|
+
return;
|
|
3059
|
+
}
|
|
3060
|
+
if (event.key !== "Escape") return;
|
|
3061
|
+
event.preventDefault();
|
|
3062
|
+
requestCancel();
|
|
3063
|
+
};
|
|
3064
|
+
|
|
3065
|
+
// VIEW:
|
|
3066
|
+
|
|
3067
|
+
win.color = look.coverColor;
|
|
3068
|
+
win.zIndex = basic.zIndexAbove(opener, look.zIndex);
|
|
3069
|
+
win.role = "dialog";
|
|
3070
|
+
win.aria = { label: editor.titleText || texts.table, modal: true };
|
|
3071
|
+
editor.boxShadow = look.shadow;
|
|
3072
|
+
placeWindow();
|
|
3073
|
+
createIn(editor.titleRow, function () {
|
|
3074
|
+
JsonEditor.iconButton("close", texts.close + " (Escape)", looks.icon, requestCancel, JsonEditor.SIZES.CLOSE_BUTTON);
|
|
3075
|
+
});
|
|
3076
|
+
createIn(editor.footer, function () {
|
|
3077
|
+
btnCancel = JsonEditor.textButton(basic.escapeHtml(texts.cancel), looks.button, requestCancel);
|
|
3078
|
+
btnCancel.css = { marginLeft: "auto" }; // WHY: at the right end also without the keys of the cells (showHint: 0)
|
|
3079
|
+
const btnApply = JsonEditor.textButton(basic.escapeHtml(texts.apply), looks.accent, apply);
|
|
3080
|
+
btnApply.elem.title = texts.apply + " (Cmd/Ctrl+Enter)";
|
|
3081
|
+
});
|
|
3082
|
+
|
|
3083
|
+
// INIT:
|
|
3084
|
+
|
|
3085
|
+
win.onPageResize(placeWindow);
|
|
3086
|
+
win.listenTo(document, "keydown", onKeyDown);
|
|
3087
|
+
win.editor = editor;
|
|
3088
|
+
win.close = close;
|
|
3089
|
+
editor.focus();
|
|
3090
|
+
return win;
|
|
3091
|
+
|
|
3092
|
+
};
|
|
3093
|
+
|
|
3094
|
+
// *** STYLE PACKAGES:
|
|
3095
|
+
// USAGE: JsonEditor({ styleName: "dark" })
|
|
3096
|
+
// USAGE: JsonEditor({ styleName: "dark", style: { colors: { accent: "#3DAA81" } } }) // Change only some keys.
|
|
3097
|
+
// NOTE: A new package needs only the keys that differ from the default (light) style.
|
|
3098
|
+
JsonEditor.styles = {
|
|
3099
|
+
|
|
3100
|
+
// White, dark text, soft lines, a blue accent.
|
|
3101
|
+
light: JsonEditorDefaults.style,
|
|
3102
|
+
|
|
3103
|
+
// For a dark page: the table window of basic-designer.
|
|
3104
|
+
dark: {
|
|
3105
|
+
box: { color: "#262626", border: 1, borderColor: "#444444", round: 10 },
|
|
3106
|
+
colors: {
|
|
3107
|
+
text: "#FFFFFF",
|
|
3108
|
+
headText: "#FFFFFF",
|
|
3109
|
+
soft: "#A5A5A5",
|
|
3110
|
+
dim: "#6E6E6E",
|
|
3111
|
+
icon: "#C8C8C8",
|
|
3112
|
+
line: "#444444",
|
|
3113
|
+
rowLine: "#444444",
|
|
3114
|
+
columnLine: "#444444",
|
|
3115
|
+
headLine: "#4D4D4D",
|
|
3116
|
+
strongLine: "#5C5C5C",
|
|
3117
|
+
ground: "#262626",
|
|
3118
|
+
head: "#262626",
|
|
3119
|
+
field: "#383838",
|
|
3120
|
+
fieldHover: "#434343",
|
|
3121
|
+
hover: "#383838",
|
|
3122
|
+
selected: "#34435A",
|
|
3123
|
+
segment: "#2C2C2C",
|
|
3124
|
+
accent: "#0C8CE9",
|
|
3125
|
+
accentHover: "#3AA0F0",
|
|
3126
|
+
accentText: "#7CC4F8",
|
|
3127
|
+
onAccent: "#FFFFFF",
|
|
3128
|
+
focus: "#0C8CE9",
|
|
3129
|
+
editor: "#1E1E1E",
|
|
3130
|
+
swatchLine: "rgba(255, 255, 255, 0.25)",
|
|
3131
|
+
thumb: "#8A8A8A",
|
|
3132
|
+
scrollBar: "rgba(255, 255, 255, 0.22)",
|
|
3133
|
+
scrollBarHover: "rgba(255, 255, 255, 0.35)",
|
|
3134
|
+
placeholder: "rgba(255, 255, 255, 0.35)",
|
|
3135
|
+
},
|
|
3136
|
+
popupShadow: "0 6px 18px rgba(0, 0, 0, 0.45)",
|
|
3137
|
+
searchStyleName: "ozdenDark",
|
|
3138
|
+
search: { color: "#383838" },
|
|
3139
|
+
menuStyleName: "dark",
|
|
3140
|
+
window: { coverColor: "rgba(0, 0, 0, 0.55)", shadow: "0 20px 60px rgba(0, 0, 0, 0.6)" },
|
|
3141
|
+
},
|
|
3142
|
+
|
|
3143
|
+
};
|
|
3144
|
+
|
|
3145
|
+
// The minimal theme (_designs/minimal-theme.htm), as SmartTable "minimal": small (28 px rows, a 13 px text) on the ground of
|
|
3146
|
+
// the window with a line around it: thin lines between the rows and none between the columns, soft titles over a line, a
|
|
3147
|
+
// faint row under the mouse, buttons of lines (Apply is the only filled one: ink), the minimal search and menus.
|
|
3148
|
+
// minimalDark: the same on a dark window.
|
|
3149
|
+
// WHY opaque fills for the rows: the numbers of the rows stay over the cells when the table moves to the side.
|
|
3150
|
+
JsonEditor.createMinimalStyle = function (colors) {
|
|
3151
|
+
return {
|
|
3152
|
+
box: { color: colors.ground, border: 1, borderColor: colors.line, round: 4 },
|
|
3153
|
+
colors: {
|
|
3154
|
+
text: colors.text,
|
|
3155
|
+
headText: colors.softText,
|
|
3156
|
+
soft: colors.softText,
|
|
3157
|
+
dim: colors.faintText,
|
|
3158
|
+
icon: colors.softText,
|
|
3159
|
+
line: colors.line,
|
|
3160
|
+
rowLine: colors.track,
|
|
3161
|
+
columnLine: "transparent",
|
|
3162
|
+
headLine: colors.line,
|
|
3163
|
+
strongLine: colors.edge,
|
|
3164
|
+
ground: colors.ground,
|
|
3165
|
+
head: colors.ground,
|
|
3166
|
+
field: "transparent",
|
|
3167
|
+
fieldHover: "transparent",
|
|
3168
|
+
fieldLine: colors.line,
|
|
3169
|
+
fieldLineHover: colors.lineHover,
|
|
3170
|
+
hover: colors.hover,
|
|
3171
|
+
selected: colors.selected,
|
|
3172
|
+
segment: colors.ground,
|
|
3173
|
+
accent: colors.ink,
|
|
3174
|
+
accentHover: colors.inkHover,
|
|
3175
|
+
accentText: colors.ink,
|
|
3176
|
+
onAccent: colors.onInk,
|
|
3177
|
+
focus: colors.focus,
|
|
3178
|
+
editor: colors.ground,
|
|
3179
|
+
swatchLine: colors.line,
|
|
3180
|
+
thumb: "#8A8A8A",
|
|
3181
|
+
scrollBar: colors.scrollBar,
|
|
3182
|
+
scrollBarHover: colors.scrollBarHover,
|
|
3183
|
+
placeholder: colors.faintText,
|
|
3184
|
+
},
|
|
3185
|
+
fontSize: 13,
|
|
3186
|
+
headFontSize: 12,
|
|
3187
|
+
rowHeight: 28,
|
|
3188
|
+
headHeight: 30,
|
|
3189
|
+
popupShadow: "none",
|
|
3190
|
+
searchStyleName: colors.minimalName,
|
|
3191
|
+
// WHY every key: the keys of the default search (made for its ozden package) would be over the minimal package too.
|
|
3192
|
+
search: {
|
|
3193
|
+
height: 28,
|
|
3194
|
+
fontSize: 13,
|
|
3195
|
+
searchIconSize: 14,
|
|
3196
|
+
searchIconLeft: 10,
|
|
3197
|
+
searchIconGap: 6,
|
|
3198
|
+
clearIconRight: 8,
|
|
3199
|
+
round: 14,
|
|
3200
|
+
color: "transparent",
|
|
3201
|
+
border: 1,
|
|
3202
|
+
borderBottomStyle: "1px solid " + colors.edge,
|
|
3203
|
+
textOffsetY: 0,
|
|
3204
|
+
clearIconOffsetY: 0,
|
|
3205
|
+
},
|
|
3206
|
+
menuStyleName: colors.minimalName,
|
|
3207
|
+
window: { coverColor: colors.cover, shadow: "none" },
|
|
3208
|
+
};
|
|
3209
|
+
};
|
|
3210
|
+
JsonEditor.MINIMAL_COLORS = {
|
|
3211
|
+
ground: "#FFFFFF", text: Black(0.85), softText: Black(0.55), faintText: Black(0.54), line: Black(0.2), lineHover: Black(0.45),
|
|
3212
|
+
edge: Black(0.45), track: Black(0.08), hover: "#F7F7F7", selected: "#F0F0F0", ink: "#1F1F1F", inkHover: "#3A3A3A",
|
|
3213
|
+
onInk: "#FFFFFF", focus: "#3D8BD9", scrollBar: Black(0.2), scrollBarHover: Black(0.35), cover: Black(0.3), minimalName: "minimal",
|
|
3214
|
+
};
|
|
3215
|
+
JsonEditor.styles.minimal = JsonEditor.createMinimalStyle(JsonEditor.MINIMAL_COLORS);
|
|
3216
|
+
JsonEditor.MINIMAL_DARK_COLORS = {
|
|
3217
|
+
ground: "#1A1A19", text: White(0.88), softText: White(0.55), faintText: White(0.48), line: White(0.18), lineHover: White(0.42),
|
|
3218
|
+
edge: White(0.42), track: White(0.08), hover: "#232322", selected: "#2C2C2B", ink: "#EDEDEB", inkHover: "#FFFFFF",
|
|
3219
|
+
onInk: "#1A1A19", focus: "#7FB6DE", scrollBar: White(0.22), scrollBarHover: White(0.35), cover: Black(0.6), minimalName: "minimalDark",
|
|
3220
|
+
};
|
|
3221
|
+
JsonEditor.styles.minimalDark = JsonEditor.createMinimalStyle(JsonEditor.MINIMAL_DARK_COLORS);
|