ozdenui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,1181 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ SortableList - v26.10
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - A list whose order can be changed.
9
+ - Items: strings or objects { key, text, description, locked }. getItems() / getKeys() give the new order.
10
+ - Mouse and touch: drag the handle (dragBy: "handle", default) or the whole row (dragBy: "row").
11
+ The other rows slide out of the way. Near the top / bottom edge the list scrolls by itself.
12
+ - Keyboard: ArrowUp / ArrowDown move the focus. Space or Enter grabs the row, the arrow keys move it,
13
+ Space / Enter drops it, Escape puts it back. Alt (or Ctrl) + ArrowUp / ArrowDown moves the row at once.
14
+ Screen readers hear the moves (aria-live). Texts are in "texts" (English by default).
15
+ - showButtons: 1 -> Up / down buttons on every row. removable: 1 -> A remove (x) button (and the Delete key).
16
+ - locked: 1 on an item -> It does not move and the other rows can not pass it (Ex: a fixed first row).
17
+ - showNumbers: 1 -> The position number (1, 2, 3...) at the left of the row.
18
+ - createItemView(item, index, self) -> Draw your own row content (instead of the text and the description).
19
+ - height: "auto" -> The list grows with its rows. Otherwise it scrolls with basic/scroll-bar.js (if it is loaded).
20
+ - onChange(self, item, fromIndex, toIndex) is NOT called when the component is created,
21
+ by setItems() or by the silent: 1 parameter.
22
+ - Everything is drawn with code (no image files needed).
23
+ - Style packages: "light" (default; the old name "classic" works too), "modern", "dark". Select with styleName. (SortableList.styles)
24
+
25
+ USAGE:
26
+ const list = SortableList({
27
+ width: 360,
28
+ height: 320,
29
+ items: ["Sales", "Stock", "Collections", "Branches"],
30
+ onChange: function (self, item, fromIndex, toIndex) { println(self.getKeys()); },
31
+ });
32
+ list.getItems(); // Items in the new order (the same objects / strings you gave)
33
+ list.moveItem(0, 2); // onChange is called
34
+ list.addItem({ key: "hr", text: "Staff", description: "Headcount by department" });
35
+ list.removeItem(1);
36
+ SortableList({ items: [{ key: "home", text: "Home", locked: 1 }, "Orders", "Reports"], showButtons: 1, removable: 1 });
37
+ SortableList({ height: "auto", dragBy: "row", showNumbers: 0, styleName: "modern" });
38
+
39
+ Started Date: September 2026
40
+ Webpage: https://bug7a.github.io/js-components/
41
+
42
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
43
+ Licensed under the Apache License, Version 2.0
44
+ SPDX-License-Identifier: Apache-2.0
45
+
46
+ */
47
+
48
+ "use strict";
49
+
50
+ // Default values:
51
+ const SortableListDefaults = {
52
+ key: "0",
53
+ width: 340,
54
+ height: 320, // "auto": grows with the rows (no scrolling)
55
+ items: [], // Strings or { key, text, description, locked } (style.desc is the look of the description)
56
+ dragBy: "handle", // "handle": drag by the grip at the left, "row": drag by the whole row (touch: the list then scrolls only by the scroll bar)
57
+ showHandle: 1, // The grip at the left. (dragBy: "row" can hide it)
58
+ showNumbers: 1, // Position numbers
59
+ showButtons: 0, // Up / down buttons
60
+ removable: 0, // A remove button and the Delete key
61
+ enabled: 1,
62
+ rowGap: 6, // Space between the rows
63
+ emptyText: "No items",
64
+ useScrollBar: 1, // basic/scroll-bar.js. 0: the scrollbar of the browser.
65
+ ariaLabel: "Sortable list",
66
+ createItemView: null, // function (item, index, self) { ... } Objects created here go into the content group of the row.
67
+ texts: {
68
+ handle: "Drag to reorder",
69
+ moveUp: "Move up",
70
+ moveDown: "Move down",
71
+ remove: "Remove",
72
+ locked: "Locked",
73
+ grabbed: "{text} grabbed. Position {position} of {count}. Use the arrow keys to move, Space to drop, Escape to cancel.",
74
+ moved: "{text}: position {position} of {count}.",
75
+ dropped: "{text} dropped at position {position} of {count}.",
76
+ canceled: "Move canceled. {text} is back at position {position}.",
77
+ removed: "{text} removed.",
78
+ },
79
+ onChange: function (self, item, fromIndex, toIndex) { }, // self.getItems() for the new order
80
+ onRemove: function (self, item, index) { },
81
+ onItemClick: function (self, item, index) { },
82
+ onDragStart: function (self, item, index) { },
83
+ onDragEnd: function (self, item, index) { }, // index: the new position
84
+ styleName: "light", // "light" ("classic": its old name), "modern", "dark" or a name added to SortableList.styles
85
+ style: { // Light style package (default)
86
+ box: {
87
+ color: "transparent",
88
+ border: 0,
89
+ borderColor: "transparent",
90
+ round: 0,
91
+ padding: 4, // Room around the rows (the shadow of the dragged row)
92
+ },
93
+ row: {
94
+ color: White(1),
95
+ hoverColor: "#FAFAF9",
96
+ border: 1,
97
+ borderColor: Black(0.12),
98
+ round: 8,
99
+ padding: [12, 10],
100
+ gap: 10, // Space between the parts of a row
101
+ minHeight: 48,
102
+ },
103
+ rowFocus: {
104
+ borderColor: "#141414",
105
+ },
106
+ rowDragging: {
107
+ color: White(1),
108
+ borderColor: Black(0.3),
109
+ boxShadow: "0px 10px 24px rgba(0, 0, 0, 0.16)",
110
+ scale: 1.02,
111
+ },
112
+ rowLocked: {
113
+ color: "#F4F4F3",
114
+ },
115
+ handle: {
116
+ iconColor: Black(0.3),
117
+ hoverColor: Black(0.65),
118
+ size: 18,
119
+ },
120
+ number: {
121
+ width: 22,
122
+ fontSize: 13,
123
+ textColor: Black(0.4),
124
+ fontFamily: "opensans-bold",
125
+ },
126
+ text: {
127
+ fontSize: 15,
128
+ textColor: "#141414",
129
+ fontFamily: "",
130
+ },
131
+ desc: {
132
+ fontSize: 12,
133
+ textColor: Black(0.45),
134
+ },
135
+ button: {
136
+ size: 28,
137
+ iconSize: 14,
138
+ iconColor: Black(0.55),
139
+ hoverColor: Black(0.06),
140
+ round: 6,
141
+ disabledOpacity: 0.25,
142
+ },
143
+ empty: {
144
+ fontSize: 14,
145
+ textColor: Black(0.4),
146
+ },
147
+ disabled: {
148
+ opacity: 0.5,
149
+ },
150
+ scrollBar: {
151
+ bar_color: "#9A9A98",
152
+ bar_mouseOverColor: "#6A6A68",
153
+ bar_width: 4,
154
+ bar_round: 3,
155
+ bar_opacity: 0.4,
156
+ bar_mouseOverOpacity: 0.9,
157
+ bar_padding: 2,
158
+ },
159
+ }
160
+ };
161
+
162
+ const SortableList = function (params = {}) {
163
+
164
+ // Merge style package: params.style > SortableList.styles[styleName] > SortableListDefaults.style (light)
165
+ basic.applyStylePackage(params, SortableList.styles, SortableListDefaults);
166
+
167
+ // Merge params:
168
+ mergeIntoIfMissing(params, SortableListDefaults);
169
+
170
+ // Edit params, if needed:
171
+ const _bs = params.style.box;
172
+ params.color = _bs.color;
173
+ params.border = _bs.border;
174
+ params.borderColor = _bs.borderColor;
175
+ params.round = _bs.round;
176
+ const startItems = params.items;
177
+ params.items = []; // WHY: The default array must not be shared by the instances. box.items is set below.
178
+ const isAutoHeight = (String(params.height) === "auto"); // WHY: box.height reads 0 for "auto", so it is read here.
179
+
180
+ // BOX: Component container
181
+ let box = startObject(params);
182
+
183
+ // *** PRIVATE VARIABLES:
184
+ const _s = box.style;
185
+ const ROW_TRANSITION = "background-color 0.15s, border-color 0.15s, box-shadow 0.15s";
186
+ const SHIFT_TRANSITION = ROW_TRANSITION + ", transform 0.18s ease"; // The other rows slide out of the way.
187
+ const SETTLE_TRANSITION = ROW_TRANSITION + ", transform 0.16s ease"; // The dropped row slides to its place.
188
+ const SETTLE_DURATION = 170; // ms: a little longer than the transform of SETTLE_TRANSITION
189
+ const DRAG_THRESHOLD = 4; // px: a smaller move is still a click
190
+ const DRAGGED_Z_INDEX = 10;
191
+ const AUTO_SCROLL_EDGE = 48; // px: the edge (top / bottom) where the list scrolls by itself
192
+ const AUTO_SCROLL_SLOWDOWN = 3; // The speed is the depth into the edge divided by this.
193
+ const AUTO_SCROLL_MAX_SPEED = 18; // px per frame
194
+ let entries = []; // { item, key, text, description, locked, row, ... } in the current order
195
+ let drag = null; // Pointer drag state
196
+ let grab = null; // Keyboard grab state: { entry, startIndex }
197
+ let isSettling = 0; // The dropped row is sliding to its place
198
+ let cancelSettle = null; // (box.after: stopped by remove())
199
+ let autoScrollFrame = null;
200
+ let isReordering = 0; // WHY: Moving a focused row in the DOM fires blur. It must not drop the grabbed row.
201
+ let suppressClick = 0; // WHY: The pointerup of a drag also fires a click on the row.
202
+ let dragPreview = null;
203
+
204
+ // *** PUBLIC VARIABLES:
205
+ // NOTE: Default values are also public variables. (box.items is the current order, the same as getItems())
206
+
207
+ // *** PRIVATE FUNCTIONS:
208
+
209
+ // "Sales" or { key, text, description, locked } -> entry
210
+ const toEntry = function (item) {
211
+ const isObject = (item !== null && typeof item === "object");
212
+ const text = isObject ? String(item.text ?? item.key ?? "") : String(item ?? "");
213
+ return {
214
+ item: item,
215
+ key: isObject ? String(item.key ?? text) : text,
216
+ text: text,
217
+ description: isObject ? String(item.description ?? "") : "",
218
+ locked: (isObject && basic.isOn(item.locked)) ? 1 : 0,
219
+ row: null,
220
+ };
221
+ };
222
+
223
+ const indexOf = function (entry) {
224
+ return entries.indexOf(entry);
225
+ };
226
+
227
+ const syncItems = function () {
228
+ box.items = entries.map(function (e) { return e.item; });
229
+ };
230
+
231
+ const formatText = function (template, entry) {
232
+ return String(template || "")
233
+ .replace("{text}", entry.text)
234
+ .replace("{position}", String(indexOf(entry) + 1))
235
+ .replace("{count}", String(entries.length));
236
+ };
237
+
238
+ const announce = function (template, entry) {
239
+ box.lblLive.plainText = formatText(template, entry);
240
+ };
241
+
242
+ // Can the row at index move one step in direction (-1 / 1)?
243
+ const canMove = function (index, direction) {
244
+ const target = index + direction;
245
+ if (box.enabled != 1) return 0;
246
+ if (index < 0 || target < 0 || target >= entries.length) return 0;
247
+ return (!entries[index].locked && !entries[target].locked) ? 1 : 0;
248
+ };
249
+
250
+ const getRowBorderColor = function (isDragged, isFocused) {
251
+ if (isDragged) return _s.rowDragging.borderColor;
252
+ if (isFocused) return _s.rowFocus.borderColor;
253
+ return _s.row.borderColor;
254
+ };
255
+
256
+ const paintRow = function (entry) {
257
+ const row = entry.row;
258
+ if (!row) return;
259
+ const isGrabbed = (grab && grab.entry === entry);
260
+ const isDragged = (drag && drag.active && drag.entry === entry) || isGrabbed;
261
+ const isFocused = (document.activeElement === row.elem);
262
+ let color = entry.locked ? _s.rowLocked.color : _s.row.color;
263
+ if (entry.isHover && !entry.locked && box.enabled == 1) color = _s.row.hoverColor;
264
+ if (isDragged) color = _s.rowDragging.color;
265
+ row.color = color;
266
+ row.borderColor = getRowBorderColor(isDragged, isFocused);
267
+ row.boxShadow = isDragged ? _s.rowDragging.boxShadow : "none";
268
+ row.zIndex = isDragged ? DRAGGED_Z_INDEX : "";
269
+ row.aria = { grabbed: isGrabbed ? "true" : "false" };
270
+ };
271
+
272
+ const paintButton = function (btn, isEnabled) {
273
+ if (!btn) return;
274
+ btn.css.opacity = isEnabled ? "1" : String(_s.button.disabledOpacity);
275
+ btn.cursor = isEnabled ? "pointer" : "default";
276
+ btn.aria = { disabled: isEnabled ? "false" : "true" };
277
+ if (!isEnabled) btn.color = "transparent";
278
+ };
279
+
280
+ // Numbers, aria and the button states after a change of the order.
281
+ const updateRows = function () {
282
+ entries.forEach(function (entry, index) {
283
+ const row = entry.row;
284
+ if (!row) return;
285
+ if (row.lblNumber) row.lblNumber.plainText = String(index + 1);
286
+ row.aria = { posinset: String(index + 1), setsize: String(entries.length) };
287
+ paintButton(row.btnUp, canMove(index, -1));
288
+ paintButton(row.btnDown, canMove(index, 1));
289
+ paintRow(entry);
290
+ });
291
+ box.lblEmpty.visible = (entries.length == 0) ? 1 : 0;
292
+ syncItems();
293
+ if (box.scrollBar) box.scrollBar.refreshScroll();
294
+ };
295
+
296
+ // Changes the order of the data and the DOM. (No onChange)
297
+ const isInList = function (index) {
298
+ return (index >= 0 && index < entries.length);
299
+ };
300
+
301
+ const moveEntry = function (from, to) {
302
+ if (from === to || !isInList(from) || !isInList(to)) return 0;
303
+ const entry = entries.splice(from, 1)[0];
304
+ entries.splice(to, 0, entry);
305
+ const elem = entry.row.elem;
306
+ const hadFocus = (document.activeElement === elem);
307
+ const next = entries[to + 1];
308
+ isReordering = 1;
309
+ box.wrapper.elem.insertBefore(elem, next ? next.row.elem : null);
310
+ if (hadFocus) elem.focus({ preventScroll: true });
311
+ isReordering = 0;
312
+ updateRows();
313
+ return 1;
314
+ };
315
+
316
+ const moveBy = function (entry, direction, fireChange) {
317
+ const from = indexOf(entry);
318
+ if (!canMove(from, direction)) return 0;
319
+ moveEntry(from, from + direction);
320
+ entry.row.elem.scrollIntoView({ block: "nearest" });
321
+ if (fireChange) box.onChange(box, entry.item, from, from + direction);
322
+ return 1;
323
+ };
324
+
325
+ const focusRow = function (index) {
326
+ const entry = entries[Math.max(0, Math.min(index, entries.length - 1))];
327
+ if (!entry) return;
328
+ entry.row.elem.focus({ preventScroll: true });
329
+ entry.row.elem.scrollIntoView({ block: "nearest" });
330
+ };
331
+
332
+ // *** KEYBOARD GRAB:
333
+
334
+ const startGrab = function (entry) {
335
+ if (box.enabled != 1 || entry.locked || grab || drag || isSettling) return;
336
+ grab = { entry: entry, startIndex: indexOf(entry) };
337
+ paintRow(entry);
338
+ announce(box.texts.grabbed, entry);
339
+ box.onDragStart(box, entry.item, grab.startIndex);
340
+ };
341
+
342
+ // apply: 1 -> Keep the new position. 0 -> Back to the start position.
343
+ const endGrab = function (apply) {
344
+ if (!grab) return;
345
+ const g = grab;
346
+ grab = null;
347
+ const index = indexOf(g.entry);
348
+ if (!apply && index !== g.startIndex) moveEntry(index, g.startIndex);
349
+ paintRow(g.entry);
350
+ const finalIndex = indexOf(g.entry);
351
+ if (finalIndex < 0) return; // WHY: The row may be removed while it is grabbed.
352
+ announce(apply ? box.texts.dropped : box.texts.canceled, g.entry);
353
+ box.onDragEnd(box, g.entry.item, finalIndex);
354
+ if (apply && finalIndex !== g.startIndex) box.onChange(box, g.entry.item, g.startIndex, finalIndex);
355
+ };
356
+
357
+ // ArrowUp -> -1, ArrowDown -> 1
358
+ const getArrowDirection = function (key) {
359
+ return (key === "ArrowUp") ? -1 : 1;
360
+ };
361
+
362
+ // The keys of a grabbed row: the arrows move it, Space / Enter / Tab drop it, Escape puts it back.
363
+ const onGrabKeyDown = function (entry, event) {
364
+ const key = event.key;
365
+ if (key === "ArrowUp" || key === "ArrowDown") {
366
+ event.preventDefault();
367
+ if (moveBy(entry, getArrowDirection(key), 0)) announce(box.texts.moved, entry);
368
+ } else if (key === " " || key === "Enter") {
369
+ event.preventDefault();
370
+ endGrab(1);
371
+ } else if (key === "Escape") {
372
+ event.preventDefault();
373
+ endGrab(0);
374
+ entry.row.elem.scrollIntoView({ block: "nearest" });
375
+ } else if (key === "Tab") {
376
+ endGrab(1);
377
+ }
378
+ };
379
+
380
+ const onRowKeyDown = function (entry, event) {
381
+
382
+ // WHY: the keys of a field inside the row (createItemView) are its own: Backspace in it removed the row,
383
+ // a space could not be typed (DataTable, SmartTable and Stepper check the target too)
384
+ if (event.target !== entry.row.elem) return;
385
+ if (box.enabled != 1 || isSettling) return;
386
+ const key = event.key;
387
+ const index = indexOf(entry);
388
+
389
+ if (grab && grab.entry === entry) {
390
+ onGrabKeyDown(entry, event);
391
+ return;
392
+ }
393
+
394
+ switch (key) {
395
+ case "ArrowUp":
396
+ case "ArrowDown":
397
+ event.preventDefault();
398
+ if (event.altKey || event.ctrlKey || event.metaKey) {
399
+ if (moveBy(entry, getArrowDirection(key), 1)) announce(box.texts.moved, entry);
400
+ } else {
401
+ focusRow(index + getArrowDirection(key));
402
+ }
403
+ break;
404
+ case "Home":
405
+ event.preventDefault();
406
+ focusRow(0);
407
+ break;
408
+ case "End":
409
+ event.preventDefault();
410
+ focusRow(entries.length - 1);
411
+ break;
412
+ case " ":
413
+ case "Enter":
414
+ event.preventDefault();
415
+ startGrab(entry);
416
+ break;
417
+ case "Delete":
418
+ case "Backspace":
419
+ if (box.removable != 1) break;
420
+ event.preventDefault();
421
+ removeEntry(entry, 0, 1);
422
+ break;
423
+ }
424
+
425
+ };
426
+
427
+ // *** POINTER DRAG:
428
+
429
+ const getScrollTop = function () {
430
+ return isAutoHeight ? 0 : box.scrollBox.elem.scrollTop;
431
+ };
432
+
433
+ const onPointerDown = function (entry, self, event) {
434
+ if (box.enabled != 1 || entry.locked || drag || grab || isSettling) return;
435
+ if (event.pointerType === "mouse" && event.button !== 0) return;
436
+ event.preventDefault(); // WHY: No text selection, no native drag.
437
+ entry.row.elem.focus({ preventScroll: true });
438
+ const y = withPageZoom(event.clientY);
439
+ drag = {
440
+ entry: entry,
441
+ source: self,
442
+ pointerId: event.pointerId,
443
+ startY: y,
444
+ lastY: y,
445
+ lastClientY: event.clientY,
446
+ startScroll: getScrollTop(),
447
+ active: 0,
448
+ };
449
+ try {
450
+ self.elem.setPointerCapture(event.pointerId);
451
+ } catch (error) {
452
+ // WHY: It throws when the pointer is already gone. The drag still works without the capture.
453
+ }
454
+ };
455
+
456
+ const onPointerMove = function (self, event) {
457
+ if (!drag || event.pointerId !== drag.pointerId) return;
458
+ if (event.pointerType === "mouse" && event.buttons === 0) {
459
+ onPointerUp(self, event); // WHY: Recover if the mouse was released outside the browser.
460
+ return;
461
+ }
462
+ drag.lastY = withPageZoom(event.clientY);
463
+ drag.lastClientY = event.clientY;
464
+ if (!drag.active) {
465
+ if (Math.abs(drag.lastY - drag.startY) < DRAG_THRESHOLD) return; // WHY: A small move is still a click.
466
+ beginDrag();
467
+ }
468
+ updateDrag();
469
+ updateAutoScroll();
470
+ };
471
+
472
+ const onPointerUp = function (self, event) {
473
+ if (!drag || event.pointerId !== drag.pointerId) return;
474
+ if (drag.active) {
475
+ suppressClick = 1;
476
+ // WHY: a touch drag makes no click, so the flag swallowed the next real tap. The click of a mouse
477
+ // drag comes in this same input turn, before the timer.
478
+ box.after(0, function () { suppressClick = 0; });
479
+ finishDrag(1);
480
+ } else {
481
+ drag = null;
482
+ }
483
+ };
484
+
485
+ const onPointerCancel = function (self, event) {
486
+ if (!drag || event.pointerId !== drag.pointerId) return;
487
+ if (drag.active) {
488
+ finishDrag(0);
489
+ } else {
490
+ drag = null;
491
+ }
492
+ };
493
+
494
+ const beginDrag = function () {
495
+ const index = indexOf(drag.entry);
496
+ drag.active = 1;
497
+ drag.index = index;
498
+ drag.target = index;
499
+ drag.tops = entries.map(function (e) { return e.row.elem.offsetTop; });
500
+ drag.heights = entries.map(function (e) { return e.row.elem.offsetHeight; });
501
+ // The locked rows are walls: the dragged row stays between them.
502
+ drag.minIndex = 0;
503
+ drag.maxIndex = entries.length - 1;
504
+ for (let i = index - 1; i >= 0; i--) {
505
+ if (!entries[i].locked) continue;
506
+ drag.minIndex = i + 1;
507
+ break;
508
+ }
509
+ for (let i = index + 1; i < entries.length; i++) {
510
+ if (!entries[i].locked) continue;
511
+ drag.maxIndex = i - 1;
512
+ break;
513
+ }
514
+ entries.forEach(function (e, i) {
515
+ e.row.css.transition = (i === index) ? ROW_TRANSITION : SHIFT_TRANSITION;
516
+ });
517
+ drag.source.cursor = "grabbing";
518
+ paintRow(drag.entry);
519
+ // WHY: A scroll container clips even a row with a higher z-index. Draw a non-interactive
520
+ // copy above the page; the real row keeps its place, focus and pointer capture.
521
+ dragPreview = drag.entry.row.elem.cloneNode(true);
522
+ dragPreview.removeAttribute("id");
523
+ dragPreview.querySelectorAll("[id]").forEach(function (elem) { elem.removeAttribute("id"); });
524
+ dragPreview.setAttribute("aria-hidden", "true");
525
+ dragPreview.inert = true;
526
+ Object.assign(dragPreview.style, {
527
+ position: "fixed", margin: "0", pointerEvents: "none", zIndex: "2147483647",
528
+ transition: "none", transformOrigin: "center",
529
+ });
530
+ document.documentElement.appendChild(dragPreview);
531
+ drag.entry.row.css.opacity = "0";
532
+ box.onDragStart(box, drag.entry.item, index);
533
+ };
534
+
535
+ // How far the dragged row moves (dy) to sit at the index: the rows between move out of its way.
536
+ const getDropDy = function (state, index) {
537
+ const from = state.index;
538
+ const tops = state.tops;
539
+ const heights = state.heights;
540
+ if (index < from) return tops[index] - tops[from];
541
+ if (index > from) return (tops[index] + heights[index]) - (tops[from] + heights[from]);
542
+ return 0;
543
+ };
544
+
545
+ // Target: the dragged row takes a place when it is past the middle of the way to it.
546
+ // WHY: The dragged row stops at the edge of the list. A rule like "its center passes the center of a row"
547
+ // can not reach the first and the last place there (its center stops on the center of that row).
548
+ const findDragTarget = function (dy) {
549
+ const index = drag.index;
550
+ for (let k = drag.minIndex; k < index; k++) {
551
+ const middle = (getDropDy(drag, k) + getDropDy(drag, k + 1)) / 2;
552
+ if (dy < middle) return k;
553
+ }
554
+ for (let k = drag.maxIndex; k > index; k--) {
555
+ const middle = (getDropDy(drag, k - 1) + getDropDy(drag, k)) / 2;
556
+ if (dy > middle) return k;
557
+ }
558
+ return index;
559
+ };
560
+
561
+ // A row moved up shows the number above it, a row moved down the number below it.
562
+ const getNumberChange = function (shift) {
563
+ if (shift < 0) return -1;
564
+ if (shift > 0) return 1;
565
+ return 0;
566
+ };
567
+
568
+ const updateDrag = function () {
569
+ if (!drag || !drag.active) return;
570
+ const index = drag.index;
571
+ let dy = (drag.lastY - drag.startY) + (getScrollTop() - drag.startScroll);
572
+ const minDy = getDropDy(drag, drag.minIndex); // The dragged row stays between its first and its last place.
573
+ const maxDy = getDropDy(drag, drag.maxIndex);
574
+ dy = Math.max(minDy, Math.min(dy, maxDy));
575
+ drag.dy = dy;
576
+
577
+ const target = findDragTarget(dy);
578
+ drag.target = target;
579
+
580
+ const slot = drag.heights[index] + box.rowGap;
581
+ entries.forEach(function (e, k) {
582
+ if (k === index) return;
583
+ let shift = 0;
584
+ if (index < target && k > index && k <= target) shift = -slot;
585
+ else if (target < index && k >= target && k < index) shift = slot;
586
+ e.row.css.transform = shift ? "translateY(" + shift + "px)" : "";
587
+ if (e.row.lblNumber) e.row.lblNumber.plainText = String(k + 1 + getNumberChange(shift));
588
+ });
589
+ const draggedRow = entries[index].row;
590
+ const rect = draggedRow.elem.getBoundingClientRect();
591
+ const width = draggedRow.elem.offsetWidth;
592
+ const height = draggedRow.elem.offsetHeight;
593
+ const zoom = rect.width / width;
594
+ // WHY: Scaled from its center (like the row at the drop): the drop does not jump sideways.
595
+ // The unscaled preview is placed so that its center is the center of the row (zoom: a zoomed page).
596
+ Object.assign(dragPreview.style, {
597
+ left: (rect.left + (rect.width - width) / 2) + "px",
598
+ top: (rect.top + dy * zoom + (rect.height - height) / 2) + "px",
599
+ width: width + "px",
600
+ height: height + "px",
601
+ transform: "scale(" + (zoom * _s.rowDragging.scale) + ")",
602
+ });
603
+ if (draggedRow.lblNumber) draggedRow.lblNumber.plainText = String(target + 1); // The numbers show the new order while dragging.
604
+ if (draggedRow.lblNumber) {
605
+ // The preview is visual only; keep its position number in sync with the real row.
606
+ const numberIndex = Array.from(draggedRow.elem.children).indexOf(draggedRow.lblNumber.elem);
607
+ if (numberIndex >= 0) dragPreview.children[numberIndex].textContent = String(target + 1);
608
+ }
609
+ };
610
+
611
+ // Scrolls the list while the pointer is near its top or bottom edge.
612
+ const updateAutoScroll = function () {
613
+ if (isAutoHeight || !drag || !drag.active) return;
614
+ const rect = box.scrollBox.elem.getBoundingClientRect();
615
+ const edge = Math.min(AUTO_SCROLL_EDGE, rect.height / 4);
616
+ const y = drag.lastClientY;
617
+ let speed = 0;
618
+ if (y < rect.top + edge) {
619
+ speed = -(rect.top + edge - y) / AUTO_SCROLL_SLOWDOWN;
620
+ } else if (y > rect.bottom - edge) {
621
+ speed = (y - (rect.bottom - edge)) / AUTO_SCROLL_SLOWDOWN;
622
+ }
623
+ drag.scrollSpeed = Math.max(-AUTO_SCROLL_MAX_SPEED, Math.min(AUTO_SCROLL_MAX_SPEED, Math.round(speed)));
624
+ if (drag.scrollSpeed && !autoScrollFrame) autoScrollFrame = requestAnimationFrame(autoScrollTick);
625
+ };
626
+
627
+ const autoScrollTick = function () {
628
+ autoScrollFrame = null;
629
+ if (!box || !drag || !drag.active || !drag.scrollSpeed) return;
630
+ const scrollElem = box.scrollBox.elem;
631
+ const before = scrollElem.scrollTop;
632
+ scrollElem.scrollTop = before + drag.scrollSpeed;
633
+ if (scrollElem.scrollTop === before) return; // At the end: stop the loop.
634
+ updateDrag();
635
+ autoScrollFrame = requestAnimationFrame(autoScrollTick);
636
+ };
637
+
638
+ const stopAutoScroll = function () {
639
+ if (autoScrollFrame) cancelAnimationFrame(autoScrollFrame);
640
+ autoScrollFrame = null;
641
+ };
642
+
643
+ // apply: 1 -> Drop at the target. 0 -> Back to the start.
644
+ const finishDrag = function (apply) {
645
+ stopAutoScroll();
646
+ const finished = drag;
647
+ const from = finished.index;
648
+ const target = apply ? finished.target : from;
649
+ const finalDy = getDropDy(finished, target);
650
+ if (!apply) {
651
+ entries.forEach(function (e, k) {
652
+ if (k !== from) e.row.css.transform = "";
653
+ });
654
+ }
655
+
656
+ // The dropped row slides to its place, then the order changes.
657
+ const droppedRow = finished.entry.row;
658
+ droppedRow.css = {
659
+ transition: "none",
660
+ transform: "translateY(" + finished.dy + "px) scale(" + _s.rowDragging.scale + ")",
661
+ };
662
+ if (dragPreview) dragPreview.remove();
663
+ dragPreview = null;
664
+ droppedRow.css.opacity = "";
665
+ void droppedRow.elem.offsetHeight; // WHY: Start the settling animation at the last dragged position.
666
+ droppedRow.css = { transition: SETTLE_TRANSITION, transform: "translateY(" + finalDy + "px)" };
667
+ finished.source.cursor = "";
668
+ drag = null;
669
+ isSettling = 1;
670
+ paintRow(finished.entry);
671
+
672
+ cancelSettle = box.after(SETTLE_DURATION, function () {
673
+ cancelSettle = null;
674
+ isSettling = 0;
675
+ placeDroppedRow(finished.entry, from, target);
676
+ });
677
+ };
678
+
679
+ // After the slide: the order of the data and the DOM changes, without an animation.
680
+ const placeDroppedRow = function (entry, from, target) {
681
+ entries.forEach(function (e) {
682
+ e.row.css = { transition: "none", transform: "" };
683
+ });
684
+ if (!moveEntry(from, target)) updateRows(); // WHY: Puts back the numbers changed while dragging.
685
+ void box.wrapper.elem.offsetHeight; // WHY: Applies the "none" transition before it is set back.
686
+ entries.forEach(function (e) { e.row.css.transition = ROW_TRANSITION; });
687
+ const finalIndex = indexOf(entry);
688
+ box.onDragEnd(box, entry.item, finalIndex);
689
+ if (target === from) return;
690
+ announce(box.texts.dropped, entry);
691
+ box.onChange(box, entry.item, from, finalIndex);
692
+ };
693
+
694
+ // *** VIEW:
695
+
696
+ const createIconButton = function (svg, ariaLabel) {
697
+ const btn = Label({
698
+ text: svg,
699
+ width: _s.button.size,
700
+ height: _s.button.size,
701
+ round: _s.button.round,
702
+ color: "transparent",
703
+ clickable: 1,
704
+ });
705
+ btn.elem.style.display = "flex";
706
+ btn.css = { alignItems: "center", justifyContent: "center", transition: "background-color 0.15s" };
707
+ btn.lineHeight = "0";
708
+ btn.shrink = 0;
709
+ btn.cursor = "pointer";
710
+ btn.role = "button";
711
+ btn.aria = { label: ariaLabel };
712
+ btn.elem.setAttribute("title", ariaLabel);
713
+ btn.on("mouseenter", function () {
714
+ if (btn.aria.disabled === "true") return;
715
+ btn.color = _s.button.hoverColor;
716
+ });
717
+ btn.on("mouseleave", function () { btn.color = "transparent"; });
718
+ // WHY: A press on a button must not start a drag of the row (dragBy: "row").
719
+ btn.on("pointerdown", function (self, event) {
720
+ event.stopPropagation();
721
+ event.preventDefault();
722
+ });
723
+ return btn;
724
+ };
725
+
726
+ // "Sales, Monthly totals, Locked"
727
+ const getRowLabel = function (entry) {
728
+ let label = entry.text;
729
+ if (entry.description) label += ", " + entry.description;
730
+ if (entry.locked) label += ", " + box.texts.locked;
731
+ return label;
732
+ };
733
+
734
+ const getHandleSvg = function (entry, color) {
735
+ if (entry.locked) return SortableList.getLockSvg(color, _s.handle.size - 2);
736
+ return SortableList.getGripSvg(color, _s.handle.size);
737
+ };
738
+
739
+ // LABEL: Handle (grip, or a lock for a locked row)
740
+ const createHandle = function (entry) {
741
+ const handle = Label({
742
+ text: getHandleSvg(entry, _s.handle.iconColor),
743
+ width: _s.handle.size + 6,
744
+ height: _s.handle.size + 10,
745
+ clickable: 1,
746
+ });
747
+ handle.elem.style.display = "flex";
748
+ handle.css = { alignItems: "center", justifyContent: "center", marginLeft: "-4px" };
749
+ handle.lineHeight = "0";
750
+ handle.shrink = 0;
751
+ handle.aria = { hidden: "true" };
752
+ handle.elem.setAttribute("title", entry.locked ? box.texts.locked : box.texts.handle);
753
+ if (entry.locked) return handle;
754
+ handle.cursor = "grab";
755
+ handle.css.touchAction = "none";
756
+ handle.on("mouseenter", function () {
757
+ if (box.enabled == 1) handle.text = getHandleSvg(entry, _s.handle.hoverColor);
758
+ });
759
+ handle.on("mouseleave", function () {
760
+ handle.text = getHandleSvg(entry, _s.handle.iconColor);
761
+ });
762
+ return handle;
763
+ };
764
+
765
+ // LABEL: Position number
766
+ const createNumberLabel = function () {
767
+ const lblNumber = Label({
768
+ plainText: "",
769
+ width: _s.number.width,
770
+ fontSize: _s.number.fontSize,
771
+ textColor: _s.number.textColor,
772
+ textAlign: "center",
773
+ });
774
+ lblNumber.shrink = 0;
775
+ lblNumber.css.fontVariantNumeric = "tabular-nums";
776
+ if (_s.number.fontFamily) lblNumber.css.fontFamily = _s.number.fontFamily;
777
+ return lblNumber;
778
+ };
779
+
780
+ // The up / down buttons: move the row one step and keep the focus on it.
781
+ const moveByButton = function (entry, direction, event) {
782
+ event.stopPropagation();
783
+ if (!moveBy(entry, direction, 1)) return;
784
+ announce(box.texts.moved, entry);
785
+ entry.row.elem.focus({ preventScroll: true });
786
+ };
787
+
788
+ const bindRowEvents = function (row, entry, isRowDrag) {
789
+ const dragSource = (isRowDrag || !row.handle) ? row : row.handle;
790
+ if (!entry.locked && (isRowDrag || row.handle)) {
791
+ dragSource.on("pointerdown", function (self, event) { onPointerDown(entry, self, event); });
792
+ dragSource.listenTo(dragSource.elem, "lostpointercapture", onPointerCancel);
793
+ }
794
+ row.on("keydown", function (self, event) { onRowKeyDown(entry, event); });
795
+ row.on("focus", function () { paintRow(entry); });
796
+ row.on("blur", function () {
797
+ if (isReordering) return;
798
+ if (grab && grab.entry === entry) endGrab(1);
799
+ paintRow(entry);
800
+ });
801
+ row.on("mouseenter", function () {
802
+ entry.isHover = 1;
803
+ if (!drag) paintRow(entry);
804
+ });
805
+ row.on("mouseleave", function () {
806
+ entry.isHover = 0;
807
+ if (!drag) paintRow(entry);
808
+ });
809
+ row.on("click", function () {
810
+ if (suppressClick) {
811
+ suppressClick = 0;
812
+ return;
813
+ }
814
+ if (box.enabled == 1) box.onItemClick(box, entry.item, indexOf(entry));
815
+ });
816
+ };
817
+
818
+ const createRow = function (entry) {
819
+
820
+ const isRowDrag = (box.dragBy === "row");
821
+
822
+ // GROUP: Row
823
+ const row = HGroup({
824
+ width: "100%",
825
+ height: "auto",
826
+ align: "left center",
827
+ gap: _s.row.gap,
828
+ padding: _s.row.padding,
829
+ color: entry.locked ? _s.rowLocked.color : _s.row.color,
830
+ border: _s.row.border,
831
+ borderColor: _s.row.borderColor,
832
+ round: _s.row.round,
833
+ });
834
+ entry.row = row;
835
+ row.css = {
836
+ minHeight: _s.row.minHeight + "px",
837
+ boxSizing: "border-box",
838
+ outline: "none",
839
+ transition: ROW_TRANSITION,
840
+ transformOrigin: "center center",
841
+ };
842
+ row.shrink = 0;
843
+ row.role = "option";
844
+ row.aria = { roledescription: "sortable item", label: getRowLabel(entry) };
845
+ row.clickable = 1;
846
+ row.tabIndex = (box.enabled == 1) ? 0 : -1;
847
+ if (isRowDrag && !entry.locked) {
848
+ row.cursor = "grab";
849
+ row.css.touchAction = "none";
850
+ }
851
+
852
+ if (box.showHandle == 1) row.handle = createHandle(entry);
853
+ if (box.showNumbers == 1) row.lblNumber = createNumberLabel();
854
+
855
+ // GROUP: Content (text, description or your own view)
856
+ row.content = VGroup({ width: "auto", height: "auto", align: "left center", gap: 2 });
857
+ row.content.css = { flex: "1 1 0", minWidth: "0" };
858
+
859
+ if (typeof box.createItemView === "function") {
860
+ box.createItemView(entry.item, indexOf(entry), box);
861
+ } else {
862
+ row.lblText = Label({
863
+ plainText: entry.text,
864
+ width: "100%",
865
+ fontSize: _s.text.fontSize,
866
+ textColor: _s.text.textColor,
867
+ ellipsis: 1,
868
+ });
869
+ if (_s.text.fontFamily) row.lblText.css.fontFamily = _s.text.fontFamily;
870
+ if (entry.description) {
871
+ row.lblDesc = Label({
872
+ plainText: entry.description,
873
+ width: "100%",
874
+ fontSize: _s.desc.fontSize,
875
+ textColor: _s.desc.textColor,
876
+ ellipsis: 1,
877
+ });
878
+ }
879
+ }
880
+
881
+ endGroup();
882
+
883
+ // LABEL: Up / down buttons
884
+ if (box.showButtons == 1) {
885
+ const upSvg = SortableList.getChevronSvg("up", _s.button.iconColor, _s.button.iconSize);
886
+ const downSvg = SortableList.getChevronSvg("down", _s.button.iconColor, _s.button.iconSize);
887
+ row.btnUp = createIconButton(upSvg, box.texts.moveUp);
888
+ row.btnUp.on("click", function (self, event) { moveByButton(entry, -1, event); });
889
+ row.btnDown = createIconButton(downSvg, box.texts.moveDown);
890
+ row.btnDown.on("click", function (self, event) { moveByButton(entry, 1, event); });
891
+ }
892
+
893
+ // LABEL: Remove button
894
+ if (box.removable == 1) {
895
+ const closeSvg = SortableList.getCloseSvg(_s.button.iconColor, _s.button.iconSize);
896
+ row.btnRemove = createIconButton(closeSvg, box.texts.remove);
897
+ row.btnRemove.on("click", function (self, event) {
898
+ event.stopPropagation();
899
+ if (box.enabled == 1) removeEntry(entry, 0, 1);
900
+ });
901
+ }
902
+
903
+ endGroup();
904
+
905
+ bindRowEvents(row, entry, isRowDrag);
906
+
907
+ return row;
908
+ };
909
+
910
+ // Draws all the rows again.
911
+ const render = function () {
912
+ if (grab) grab = null;
913
+ if (box.wrapper) box.wrapper.remove();
914
+ const host = isAutoHeight ? box : box.scrollBox;
915
+ createIn(host, function () {
916
+ // GROUP: Rows
917
+ box.wrapper = VGroup({
918
+ width: "100%",
919
+ height: "auto",
920
+ align: "left top",
921
+ gap: box.rowGap,
922
+ padding: _s.box.padding,
923
+ });
924
+ box.wrapper.css = { alignItems: "stretch", boxSizing: "border-box" };
925
+ box.wrapper.position = "relative"; // WHY: The scroll box (or the auto height box) grows with it.
926
+ box.wrapper.role = "listbox";
927
+ box.wrapper.aria = { label: box.ariaLabel };
928
+ entries.forEach(createRow);
929
+ endGroup();
930
+ });
931
+ updateRows();
932
+ };
933
+
934
+ const removeEntry = function (entry, silent, moveFocus) {
935
+ const index = indexOf(entry);
936
+ if (index < 0) return null;
937
+ if (grab && grab.entry === entry) grab = null;
938
+ const hadFocus = (document.activeElement === entry.row.elem);
939
+ entries.splice(index, 1);
940
+ isReordering = 1;
941
+ entry.row.remove();
942
+ isReordering = 0;
943
+ entry.row = null;
944
+ updateRows();
945
+ if (!silent) announce(box.texts.removed, entry);
946
+ if (hadFocus && moveFocus && entries.length) focusRow(Math.min(index, entries.length - 1));
947
+ if (!silent) box.onRemove(box, entry.item, index);
948
+ return entry.item;
949
+ };
950
+
951
+ const isBusy = function () {
952
+ return (drag && drag.active) || isSettling;
953
+ };
954
+
955
+ // *** PUBLIC FUNCTIONS:
956
+
957
+ box.setItems = function (items) {
958
+ if (isBusy()) return;
959
+ entries = (Array.isArray(items) ? items : []).map(toEntry);
960
+ render();
961
+ };
962
+ // USAGE: list.setItems(["A", "B", { key: "c", text: "C", description: "Third", locked: 1 }]) // onChange is not called.
963
+
964
+ box.getItems = function () {
965
+ return entries.map(function (e) { return e.item; });
966
+ };
967
+ // USAGE: const order = list.getItems(); // The same strings / objects in the new order.
968
+
969
+ box.getKeys = function () {
970
+ return entries.map(function (e) { return e.key; });
971
+ };
972
+ // USAGE: list.getKeys() // ["sales", "stock", ...] (a string item is its own key)
973
+
974
+ box.getIndexByKey = function (key) {
975
+ return entries.findIndex(function (e) { return e.key === String(key); });
976
+ };
977
+
978
+ // silent: 1 -> onChange is not called. A locked row does not move. Returns 1 when it moved.
979
+ box.moveItem = function (fromIndex, toIndex, silent = 0) {
980
+ if (isBusy()) return 0;
981
+ const from = Number(fromIndex);
982
+ const to = Math.max(0, Math.min(Number(toIndex), entries.length - 1));
983
+ const entry = entries[from];
984
+ if (!entry || entry.locked || from === to) return 0;
985
+ moveEntry(from, to);
986
+ if (!silent) box.onChange(box, entry.item, from, to);
987
+ return 1;
988
+ };
989
+ // USAGE: list.moveItem(0, 3)
990
+
991
+ box.moveUp = function (index) {
992
+ const entry = entries[index];
993
+ return (entry && !isBusy()) ? moveBy(entry, -1, 1) : 0;
994
+ };
995
+
996
+ box.moveDown = function (index) {
997
+ const entry = entries[index];
998
+ return (entry && !isBusy()) ? moveBy(entry, 1, 1) : 0;
999
+ };
1000
+
1001
+ // index: -1 or empty -> at the end.
1002
+ box.addItem = function (item, index = -1) {
1003
+ if (isBusy()) return;
1004
+ const entry = toEntry(item);
1005
+ const at = (index < 0 || index > entries.length) ? entries.length : index;
1006
+ entries.splice(at, 0, entry);
1007
+ createIn(box.wrapper, function () { createRow(entry); });
1008
+ const next = entries[at + 1];
1009
+ if (next) box.wrapper.elem.insertBefore(entry.row.elem, next.row.elem);
1010
+ updateRows();
1011
+ };
1012
+ // USAGE: list.addItem("New item") or list.addItem({ key: "x", text: "X" }, 0)
1013
+
1014
+ // silent: 1 -> onRemove is not called. Returns the removed item.
1015
+ box.removeItem = function (index, silent = 0) {
1016
+ if (isBusy()) return null;
1017
+ const entry = entries[index];
1018
+ return entry ? removeEntry(entry, silent, 0) : null;
1019
+ };
1020
+ // USAGE: list.removeItem(2)
1021
+
1022
+ box.setEnabled = function (enabled) {
1023
+ box.enabled = basic.isOn(enabled) ? 1 : 0;
1024
+ if (box.enabled != 1 && grab) endGrab(0);
1025
+ if (box.enabled != 1 && drag) {
1026
+ if (drag.active) finishDrag(0);
1027
+ else drag = null;
1028
+ }
1029
+ box.css.opacity = (box.enabled == 1) ? "1" : String(_s.disabled.opacity);
1030
+ box.aria = { disabled: (box.enabled == 1) ? "false" : "true" };
1031
+ entries.forEach(function (e) {
1032
+ if (e.row) e.row.tabIndex = (box.enabled == 1) ? 0 : -1;
1033
+ });
1034
+ updateRows();
1035
+ };
1036
+ // USAGE: get: list.enabled, set: list.setEnabled(0)
1037
+
1038
+ box.focusItem = function (index = 0) {
1039
+ focusRow(index);
1040
+ };
1041
+
1042
+ box.refresh = function () {
1043
+ updateRows();
1044
+ };
1045
+
1046
+
1047
+ // *** OBJECT VIEW:
1048
+
1049
+ box.css.boxSizing = "border-box";
1050
+
1051
+ if (!isAutoHeight) {
1052
+ // BOX: Scrolling area
1053
+ box.scrollBox = Box(0, 0, "100%", "100%", { color: "transparent", scrollY: 1 });
1054
+ }
1055
+
1056
+ // LABEL: Empty text
1057
+ box.lblEmpty = Label({
1058
+ plainText: box.emptyText,
1059
+ width: "100%",
1060
+ fontSize: _s.empty.fontSize,
1061
+ textColor: _s.empty.textColor,
1062
+ textAlign: "center",
1063
+ padding: [0, 20],
1064
+ visible: 0,
1065
+ });
1066
+ if (isAutoHeight) box.lblEmpty.position = "relative";
1067
+ else box.lblEmpty.top = 24;
1068
+
1069
+ // LABEL: Screen reader messages (not visible)
1070
+ box.lblLive = Label({ left: 0, top: 0, width: 1, height: 1, opacity: 0, plainText: "" });
1071
+ box.lblLive.aria = { live: "polite" };
1072
+ box.lblLive.css.overflow = "hidden";
1073
+
1074
+ if (!isAutoHeight && box.useScrollBar == 1 && typeof ScrollBar !== "undefined") {
1075
+ box.scrollBar = ScrollBar(Object.assign({ scrollableBox: box.scrollBox, neverHide: 0, showDots: 0 }, _s.scrollBar));
1076
+ }
1077
+
1078
+ // *** OBJECT INIT CODE:
1079
+
1080
+ // WHY: On the document (capture), not on the row: the move and the release are also caught outside the list
1081
+ // and when the pointer capture failed (a row stayed grabbed after a release outside of it).
1082
+ box.listenTo(document, "pointermove", onPointerMove, true);
1083
+ box.listenTo(document, "pointerup", onPointerUp, true);
1084
+ box.listenTo(document, "pointercancel", onPointerCancel, true);
1085
+ box.listenTo(window, "blur", function () {
1086
+ if (!drag) return;
1087
+ if (drag.active) finishDrag(0);
1088
+ else drag = null;
1089
+ });
1090
+
1091
+ // The drag preview is a raw element on <html> and the auto scroll a rAF loop: basic.js can not clean them.
1092
+ const superRemove = box.remove;
1093
+ box.superRemove = superRemove;
1094
+ box.remove = function () {
1095
+ if (!box) return; // WHY: remove() can be called twice (also by the parent's remove()).
1096
+ stopAutoScroll();
1097
+ if (dragPreview) dragPreview.remove();
1098
+ dragPreview = null;
1099
+ drag = null;
1100
+ superRemove.call(box);
1101
+ box = null;
1102
+ };
1103
+
1104
+ if (!isAutoHeight) {
1105
+ // WHY: A wheel scroll during a drag moves the list under the dragged row.
1106
+ box.scrollBox.on("scroll", function () {
1107
+ if (drag && drag.active) updateDrag();
1108
+ });
1109
+ }
1110
+
1111
+ entries = (Array.isArray(startItems) ? startItems : []).map(toEntry);
1112
+ render();
1113
+ box.setEnabled(box.enabled);
1114
+
1115
+ return endObject(box);
1116
+
1117
+ };
1118
+
1119
+ // *** STATIC FUNCTIONS:
1120
+
1121
+ SortableList.getGripSvg = function (color, size) {
1122
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="' + color + '"><circle cx="9" cy="6" r="1.7"/><circle cx="15" cy="6" r="1.7"/><circle cx="9" cy="12" r="1.7"/><circle cx="15" cy="12" r="1.7"/><circle cx="9" cy="18" r="1.7"/><circle cx="15" cy="18" r="1.7"/></svg>';
1123
+ };
1124
+
1125
+ SortableList.getLockSvg = function (color, size) {
1126
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="none" stroke="' + color + '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="11" width="14" height="9" rx="2"/><path d="M8 11V8a4 4 0 0 1 8 0v3"/></svg>';
1127
+ };
1128
+
1129
+ SortableList.getChevronSvg = function (direction, color, size) {
1130
+ const path = (direction === "up") ? "M6 15l6-6 6 6" : "M6 9l6 6 6-6";
1131
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="none" stroke="' + color + '" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="' + path + '"/></svg>';
1132
+ };
1133
+
1134
+ SortableList.getCloseSvg = function (color, size) {
1135
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="none" stroke="' + color + '" stroke-width="2.4" stroke-linecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>';
1136
+ };
1137
+
1138
+ // *** STYLE PACKAGES:
1139
+ // USAGE: SortableList({ styleName: "modern" })
1140
+ // USAGE: SortableList({ styleName: "dark", style: { row: { round: 4 } } }) // Change only some keys.
1141
+ // USAGE: SortableList({ style: SortableList.styles.dark }) // The same as styleName: "dark". (The package is copied, never changed.)
1142
+ // NOTE: A new package needs only the keys that differ from the default (classic) style.
1143
+ // USAGE: SortableList.styles.myStyle = { row: { color: "ivory" } };
1144
+ SortableList.styles = {
1145
+
1146
+ // White rows with a thin border.
1147
+ light: SortableListDefaults.style,
1148
+
1149
+ // Soft cadetblue rows, no border. Same colors as the modern Stepper, CheckBox, ProgressBar and Tabs.
1150
+ modern: {
1151
+ row: { color: "#EEF5F5", hoverColor: "#E4EFEF", border: 1, borderColor: "transparent", round: 12, padding: [14, 10], gap: 10, minHeight: 50 },
1152
+ rowFocus: { borderColor: "cadetblue" },
1153
+ rowDragging: { color: White(1), borderColor: "cadetblue", boxShadow: "0px 12px 28px rgba(47, 95, 97, 0.25)", scale: 1.02 },
1154
+ rowLocked: { color: "#F5F8F8" },
1155
+ handle: { iconColor: "#8FB5B6", hoverColor: "#2F5F61", size: 18 },
1156
+ number: { width: 22, fontSize: 13, textColor: "cadetblue", fontFamily: "opensans-bold" },
1157
+ text: { fontSize: 15, textColor: "#2F5F61", fontFamily: "" },
1158
+ desc: { fontSize: 12, textColor: "#6E9A9B" },
1159
+ button: { size: 28, iconSize: 14, iconColor: "#2F5F61", hoverColor: "#CFE2E2", round: 100, disabledOpacity: 0.25 },
1160
+ empty: { fontSize: 14, textColor: "#6E9A9B" },
1161
+ scrollBar: { bar_color: "#CFE2E2", bar_mouseOverColor: "cadetblue", bar_width: 4, bar_round: 3, bar_opacity: 0.6, bar_mouseOverOpacity: 1, bar_padding: 2 },
1162
+ },
1163
+
1164
+ // Dark theme, same colors as SelectBox, TagInput, Tabs and Stepper "dark".
1165
+ dark: {
1166
+ row: { color: "#232322", hoverColor: "#2A2A29", border: 1, borderColor: "rgba(255, 255, 255, 0.10)", round: 8, padding: [12, 10], gap: 10, minHeight: 48 },
1167
+ rowFocus: { borderColor: "#65A293" },
1168
+ rowDragging: { color: "#2C2C2B", borderColor: "#65A293", boxShadow: "0px 12px 28px rgba(0, 0, 0, 0.5)", scale: 1.02 },
1169
+ rowLocked: { color: "#1C1C1B" },
1170
+ handle: { iconColor: "rgba(255, 255, 255, 0.3)", hoverColor: "rgba(255, 255, 255, 0.8)", size: 18 },
1171
+ number: { width: 22, fontSize: 13, textColor: "#65A293", fontFamily: "opensans-bold" },
1172
+ text: { fontSize: 15, textColor: "rgba(255, 255, 255, 0.90)", fontFamily: "" },
1173
+ desc: { fontSize: 12, textColor: "rgba(255, 255, 255, 0.45)" },
1174
+ button: { size: 28, iconSize: 14, iconColor: "rgba(255, 255, 255, 0.75)", hoverColor: "rgba(255, 255, 255, 0.10)", round: 6, disabledOpacity: 0.25 },
1175
+ empty: { fontSize: 14, textColor: "rgba(255, 255, 255, 0.45)" },
1176
+ disabled: { opacity: 0.4 },
1177
+ scrollBar: { bar_color: "#8A8A88", bar_mouseOverColor: "#BFBFBD", bar_width: 4, bar_round: 3, bar_opacity: 0.4, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
1178
+ },
1179
+
1180
+ };
1181
+ SortableList.styles.classic = SortableList.styles.light; // the old name of light: the pages made with it keep their look