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,762 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ PageControl - v26.09
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - A box that holds pages side by side and shows one of them. The pages slide (or fade) when another one is opened.
9
+ (Wizards, onboarding screens, carousels, mobile app screens...)
10
+ - Pages are created with addPage(key) and filled with startPage(key) ... endPage(), or with getPage(key).
11
+ - Transitions: "slide" (horizontal, default), "slide-vertical", "fade", "none". The move is done with CSS transforms.
12
+ - The pages always have the size of the box: width: "100%" and window resizes need no code. (No setSizes())
13
+ - Swipe with a finger or a mouse drag (swipe: 1), keyboard arrows when the box has the focus (keyboard: 1).
14
+ - Dots (dots: 1) and arrow buttons (arrows: 1) drawn with code, loop: 1 (after the last page comes the first),
15
+ autoPlay: seconds (carousel; it pauses while the mouse is over the box).
16
+ - Inactive pages are hidden from the keyboard and screen readers (inert, aria-hidden).
17
+ - Events: onBeforeChange(self, fromKey, toKey) can return false to stop the change (Ex: form validation),
18
+ onChange(self) when the new page is opened, onClose(self, key) when the old page is out of the view (after the motion).
19
+ - onChange is NOT called when the component is created or when a page is opened with silent: 1.
20
+
21
+ USAGE:
22
+ const pages = PageControl({ width: "100%", height: 400, dots: 1, arrows: 1 });
23
+ pages.addPage("welcome");
24
+ pages.startPage("welcome");
25
+ Label({ text: "Welcome" });
26
+ pages.endPage();
27
+ pages.addPage("details");
28
+ pages.open("details"); // Slides to the page. onChange is called.
29
+ pages.next(); pages.previous();
30
+ PageControl({ transition: "fade", autoPlay: 4, loop: 1, pages: ["a", "b", "c"] });
31
+
32
+ Started Date: September 2026
33
+ Webpage: https://bug7a.github.io/js-components/
34
+
35
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
36
+ Licensed under the Apache License, Version 2.0
37
+ SPDX-License-Identifier: Apache-2.0
38
+
39
+ */
40
+
41
+ "use strict";
42
+
43
+ // Default values:
44
+ const PageControlDefaults = {
45
+ key: "0",
46
+ width: 600,
47
+ height: 400,
48
+ pages: [], // Keys of the pages to create at start. Ex: ["welcome", "details"]
49
+ value: null, // Key of the open page. null: the first page
50
+ transition: "slide", // "slide", "slide-vertical", "fade", "none" (Only at create time)
51
+ motion: 0.3, // Seconds. 0: No animation
52
+ easing: "cubic-bezier(0.2, 0, 0, 1)",
53
+ loop: 0, // 1: next() after the last page opens the first page.
54
+ swipe: 1, // 1: Drag with a finger or the mouse to change the page. (slide transitions)
55
+ swipeThreshold: 0.2, // Part of the box size (0.2: 20%) to change the page. A fast flick also changes it.
56
+ keyboard: 1, // 1: Arrow keys (when the box has the focus), Home / End
57
+ dots: 0, // 1: Page dots at the bottom
58
+ arrows: 0, // 1: Previous / next buttons at the sides
59
+ autoPlay: 0, // Seconds. 0: Off. Ex: 4 -> next page every 4 seconds (loop is used)
60
+ pageScroll: 0, // 1: Every page scrolls its own content (scrollY)
61
+ enabled: 1,
62
+ ariaLabel: "Pages",
63
+ onBeforeChange: function (self, fromKey, toKey) { }, // Return false to stop the change.
64
+ onChange: function (self) { }, // self.value, self.previousValue
65
+ onClose: function (self, key) { }, // The old page is out of the view (after the motion)
66
+ style: {
67
+ box: {
68
+ color: "transparent",
69
+ border: 0,
70
+ borderColor: "transparent",
71
+ round: 0,
72
+ },
73
+ page: {
74
+ color: "transparent",
75
+ },
76
+ dots: {
77
+ size: 8,
78
+ gap: 8,
79
+ bottom: 12,
80
+ color: Black(0.25),
81
+ activeColor: "#141414",
82
+ activeWidth: 20, // The active dot is a short bar. Same as size: a circle.
83
+ motion: 0.25,
84
+ },
85
+ arrows: {
86
+ size: 36,
87
+ side: 10, // Space from the sides
88
+ color: White(0.9),
89
+ hoverColor: White(1),
90
+ iconColor: "#141414",
91
+ iconSize: 18,
92
+ round: 100,
93
+ shadow: "0 2px 8px rgba(0, 0, 0, 0.15)",
94
+ disabledOpacity: 0.3,
95
+ },
96
+ focus: {
97
+ outlineColor: Black(0),
98
+ },
99
+ disabled: {
100
+ opacity: 0.5,
101
+ },
102
+ }
103
+ };
104
+
105
+ const PageControl = function (params = {}) {
106
+
107
+ // Merge params:
108
+ mergeIntoIfMissing(params, PageControlDefaults);
109
+
110
+ // Edit params, if needed:
111
+ const _bs = params.style.box;
112
+ params.color = _bs.color;
113
+ params.border = _bs.border;
114
+ params.borderColor = _bs.borderColor;
115
+ params.round = _bs.round;
116
+ const startPages = params.pages;
117
+ const startValue = params.value;
118
+ params.pages = [];
119
+ params.value = null;
120
+
121
+ // BOX: Component container
122
+ let box = startObject(params);
123
+
124
+ // *** PRIVATE VARIABLES:
125
+ const _s = box.style;
126
+ const isVertical = (box.transition === "slide-vertical");
127
+ const isFade = (box.transition === "fade");
128
+ const isSlide = (box.transition === "slide" || isVertical);
129
+ let items = []; // [{ key, page }]
130
+ let dotList = [];
131
+ let panelStack = [];
132
+ let cancelClose = null; // (box.after / box.every: stopped by remove())
133
+ let stopAutoPlayTimer = null;
134
+ let isHover = 0;
135
+ let drag = null; // { startX, startY, x, y, time, active, id }
136
+ const CLOSE_WAIT_EXTRA = 30; // ms after the motion: then onClose is called
137
+ const SWIPE_START_DISTANCE = 6; // px: a smaller move is still a click
138
+ const EDGE_RESISTANCE = 3; // At the first and the last page (no loop) the track moves 3 times less than the pointer.
139
+ const FLICK_SPEED = 0.5; // px per ms: a faster swipe changes the page, also when it is short
140
+ const SWIPE_CLICK_WAIT = 50; // ms: the click after a swipe is stopped in this time
141
+ const CONTROLS_Z_INDEX = 5; // The dots and the arrows are over the pages.
142
+ const BUTTON_FADE_TIME = "0.15s";
143
+
144
+ // *** PUBLIC VARIABLES:
145
+ box.previousValue = null;
146
+ // NOTE: Default values are also public variables. (box.value, box.loop, box.autoPlay)
147
+
148
+ // *** PRIVATE FUNCTIONS:
149
+
150
+ const findItem = function (key) {
151
+ return items.find(function (item) { return item.key === key; }) || null;
152
+ };
153
+
154
+ const getIndex = function (key) {
155
+ return items.findIndex(function (item) { return item.key === key; });
156
+ };
157
+
158
+ const getMotion = function () {
159
+ return (box.motion > 0) ? box.motion : 0;
160
+ };
161
+
162
+ const getTrackTransform = function (index, offsetPx = 0) {
163
+ // NOTE: 2D translate. (translate3d + will-change made a separate layer that some headless screenshots did not paint.)
164
+ if (isVertical) return "translate(0, calc(" + (-index * 100) + "% + " + offsetPx + "px))";
165
+ return "translate(calc(" + (-index * 100) + "% + " + offsetPx + "px), 0)";
166
+ };
167
+
168
+ const setTrackMotion = function (enabled) {
169
+ const seconds = (enabled) ? getMotion() : 0;
170
+ box.track.css.transition = (seconds > 0) ? "transform " + seconds + "s " + box.easing : "none";
171
+ };
172
+
173
+ // Slide: the track moves. Fade: the opacity of the pages.
174
+ const placePages = function (index, animated) {
175
+
176
+ if (isSlide) {
177
+ setTrackMotion(animated);
178
+ box.track.css.transform = getTrackTransform(index);
179
+ return;
180
+ }
181
+
182
+ const isFadeMotion = (isFade && animated && getMotion() > 0);
183
+ const fadeTransition = (isFadeMotion) ? "opacity " + getMotion() + "s " + box.easing : "none";
184
+ items.forEach(function (item, i) {
185
+ const isActive = (i === index);
186
+ item.page.css.transition = fadeTransition;
187
+ item.page.css.opacity = (isActive) ? "1" : "0";
188
+ item.page.zIndex = (isActive) ? 1 : 0;
189
+ });
190
+
191
+ };
192
+
193
+ // Inactive pages: no focus, no screen reader
194
+ const hideInactivePages = function (index) {
195
+ items.forEach(function (item, i) {
196
+ const isActive = (i === index);
197
+ item.page.aria = { hidden: (isActive) ? "false" : "true" };
198
+ if (isActive) {
199
+ item.page.elem.removeAttribute("inert");
200
+ } else {
201
+ item.page.elem.setAttribute("inert", "");
202
+ }
203
+ item.page.css.pointerEvents = (isActive) ? "" : "none";
204
+ });
205
+ };
206
+
207
+ // Shows the open page: its place, the other pages, the dots, the arrows.
208
+ const showActivePage = function (animated = 1) {
209
+ const index = Math.max(0, getIndex(box.value));
210
+ placePages(index, animated);
211
+ hideInactivePages(index);
212
+ updateDots();
213
+ updateArrows();
214
+ };
215
+
216
+ const updateDots = function () {
217
+ if (box.dots != 1) return;
218
+ const index = getIndex(box.value);
219
+ dotList.forEach(function (dot, i) {
220
+ const active = (i === index);
221
+ dot.color = (active) ? _s.dots.activeColor : _s.dots.color;
222
+ dot.width = (active) ? _s.dots.activeWidth : _s.dots.size;
223
+ dot.aria = { current: (active) ? "true" : "false" };
224
+ });
225
+ };
226
+
227
+ const paintArrow = function (btn, canGo) {
228
+ const isUsable = (canGo && box.enabled == 1);
229
+ btn.css.opacity = (isUsable) ? "1" : String(_s.arrows.disabledOpacity);
230
+ btn.css.pointerEvents = (isUsable) ? "auto" : "none";
231
+ btn.aria = { disabled: (canGo) ? "false" : "true" };
232
+ };
233
+
234
+ const updateArrows = function () {
235
+ if (box.arrows != 1) return;
236
+ const index = getIndex(box.value);
237
+ const canLoop = (box.loop == 1 && items.length > 1);
238
+ paintArrow(box.btnPrev, canLoop || index > 0);
239
+ paintArrow(box.btnNext, canLoop || index < items.length - 1);
240
+ box.btnPrev.visible = (items.length > 1) ? 1 : 0;
241
+ box.btnNext.visible = (items.length > 1) ? 1 : 0;
242
+ };
243
+
244
+ // Creates the dots again (page count changed)
245
+ const createDots = function () {
246
+ if (box.dots != 1) return;
247
+ dotList.forEach(function (dot) { dot.remove(); });
248
+ dotList = [];
249
+ items.forEach(function (item) {
250
+ let dot = null;
251
+ // WHY: createIn() restores the container. (setDefaultContainerBox() left it on the dots, so the next
252
+ // object of the page, created after addPage(), went into the dots group.)
253
+ createIn(box.dotsBox, function () {
254
+ dot = Box({ width: _s.dots.size, height: _s.dots.size, color: _s.dots.color, round: 100, clickable: 1 });
255
+ });
256
+ dot.shrink = 0;
257
+ dot.cursor = "pointer";
258
+ dot.css.transition = "width " + _s.dots.motion + "s, background-color " + _s.dots.motion + "s";
259
+ dot.role = "button";
260
+ dot.aria = { label: "Page " + String(item.key) };
261
+ dot.on("click", function (self, event) {
262
+ event.stopPropagation();
263
+ if (box.enabled != 1) return; // WHY: setEnabled(0) stopped the arrows, keys and swipe, not the dots
264
+ box.open(item.key);
265
+ });
266
+ dotList.push(dot);
267
+ });
268
+ box.dotsBox.visible = (items.length > 1) ? 1 : 0;
269
+ updateDots();
270
+ };
271
+
272
+ // onClose of the old page after the motion
273
+ const scheduleClose = function (key) {
274
+ if (cancelClose) cancelClose();
275
+ cancelClose = null;
276
+ if (key === null || key === undefined) return;
277
+ const delay = (box.transition === "none") ? 0 : getMotion() * 1000 + CLOSE_WAIT_EXTRA;
278
+ cancelClose = box.after(delay, function () {
279
+ cancelClose = null;
280
+ box.onClose(box, key);
281
+ });
282
+ };
283
+
284
+ // *** AUTO PLAY:
285
+
286
+ const startAutoPlay = function () {
287
+ stopAutoPlay();
288
+ if (!(box.autoPlay > 0) || items.length < 2) return;
289
+ stopAutoPlayTimer = box.every(box.autoPlay * 1000, function () {
290
+ if (isHover || (drag && drag.active) || box.enabled != 1) return;
291
+ const index = getIndex(box.value);
292
+ box.open(items[(index + 1) % items.length].key);
293
+ });
294
+ };
295
+
296
+ const stopAutoPlay = function () {
297
+ if (stopAutoPlayTimer) stopAutoPlayTimer();
298
+ stopAutoPlayTimer = null;
299
+ };
300
+
301
+ // *** SWIPE:
302
+
303
+ const onPointerDown = function (self, event) {
304
+ if (box.swipe != 1 || !isSlide || box.enabled != 1 || items.length < 2) return;
305
+ if (event.pointerType === "mouse" && event.button !== 0) return;
306
+ drag = {
307
+ startX: event.clientX,
308
+ startY: event.clientY,
309
+ x: event.clientX,
310
+ y: event.clientY,
311
+ time: Date.now(),
312
+ active: 0,
313
+ id: event.pointerId,
314
+ };
315
+ };
316
+
317
+ const capturePointer = function (pointerId) {
318
+ if (!box.elem.setPointerCapture) return;
319
+ try {
320
+ box.elem.setPointerCapture(pointerId);
321
+ } catch (error) { }
322
+ };
323
+
324
+ // The mouse button was released outside the box before the drag took the pointer: no pointerup came here.
325
+ // WHY: the next moves with no button pressed swiped the pages, and a click left them half way
326
+ const endLostDrag = function () {
327
+ const wasActive = drag.active;
328
+ drag = null;
329
+ box.track.cursor = "";
330
+ if (wasActive) showActivePage(1); // Back to the place
331
+ };
332
+
333
+ const onPointerMove = function (self, event) {
334
+ if (!drag || event.pointerId !== drag.id) return;
335
+ if (event.pointerType === "mouse" && event.buttons === 0) {
336
+ endLostDrag();
337
+ return;
338
+ }
339
+ drag.x = event.clientX;
340
+ drag.y = event.clientY;
341
+ const dx = drag.x - drag.startX;
342
+ const dy = drag.y - drag.startY;
343
+ if (!drag.active) {
344
+ // WHY: A small move is a click, a vertical move is the scroll of the page content.
345
+ const main = (isVertical) ? Math.abs(dy) : Math.abs(dx);
346
+ const other = (isVertical) ? Math.abs(dx) : Math.abs(dy);
347
+ if (main < SWIPE_START_DISTANCE || main < other) return;
348
+ drag.active = 1;
349
+ setTrackMotion(0);
350
+ box.track.cursor = "grabbing";
351
+ capturePointer(event.pointerId);
352
+ }
353
+ event.preventDefault();
354
+ const index = getIndex(box.value);
355
+ let offset = (isVertical) ? dy : dx;
356
+ // Resistance at the ends (no loop)
357
+ const atStart = (index === 0 && offset > 0);
358
+ const atEnd = (index === items.length - 1 && offset < 0);
359
+ if ((atStart || atEnd) && box.loop != 1) offset = offset / EDGE_RESISTANCE;
360
+ box.track.css.transform = getTrackTransform(index, offset);
361
+ };
362
+
363
+ // The index of the page that a swipe opens. (The same index: back to the place)
364
+ const getSwipeTarget = function (index, offset, elapsed) {
365
+ const size = (isVertical) ? box.elem.clientHeight : box.elem.clientWidth;
366
+ const velocity = Math.abs(offset) / elapsed; // px per ms
367
+ let target = index;
368
+ if (Math.abs(offset) > size * box.swipeThreshold || velocity > FLICK_SPEED) {
369
+ target = (offset < 0) ? index + 1 : index - 1;
370
+ }
371
+ if (box.loop == 1) target = (target + items.length) % items.length;
372
+ return Math.max(0, Math.min(items.length - 1, target));
373
+ };
374
+
375
+ const onPointerUp = function (self, event) {
376
+ if (!drag || event.pointerId !== drag.id) return;
377
+ const wasActive = drag.active;
378
+ const dx = drag.x - drag.startX;
379
+ const dy = drag.y - drag.startY;
380
+ const elapsed = Math.max(1, Date.now() - drag.time);
381
+ drag = null;
382
+ box.track.cursor = "";
383
+ if (!wasActive) return;
384
+ event.preventDefault();
385
+ box._swiped = 1; // WHY: The "click" after this pointer up is stopped. (onClickCapture)
386
+ box.after(SWIPE_CLICK_WAIT, function () {
387
+ box._swiped = 0;
388
+ });
389
+
390
+ const index = getIndex(box.value);
391
+ const target = getSwipeTarget(index, (isVertical) ? dy : dx, elapsed);
392
+
393
+ if (target !== index) {
394
+ box.open(items[target].key);
395
+ } else {
396
+ showActivePage(1); // Back to the place
397
+ }
398
+ };
399
+
400
+ // WHY: A drag must not end as a click on the page content.
401
+ const onClickCapture = function (self, event) {
402
+ if (!box._swiped) return;
403
+ event.stopPropagation();
404
+ event.preventDefault();
405
+ box._swiped = 0;
406
+ };
407
+
408
+ const onKeyDown = function (self, event) {
409
+ if (box.keyboard != 1 || box.enabled != 1) return;
410
+ if (event.target !== box.elem) return; // WHY: Inputs in the pages keep their arrow keys.
411
+ const prevKey = (isVertical) ? "ArrowUp" : "ArrowLeft";
412
+ const nextKey = (isVertical) ? "ArrowDown" : "ArrowRight";
413
+ if (event.key === nextKey) {
414
+ event.preventDefault();
415
+ box.next();
416
+ return;
417
+ }
418
+ if (event.key === prevKey) {
419
+ event.preventDefault();
420
+ box.previous();
421
+ return;
422
+ }
423
+ if (event.key === "Home") {
424
+ event.preventDefault();
425
+ if (items[0]) box.open(items[0].key);
426
+ return;
427
+ }
428
+ if (event.key === "End") {
429
+ event.preventDefault();
430
+ if (items.length) box.open(items[items.length - 1].key);
431
+ }
432
+ };
433
+
434
+ // *** PUBLIC FUNCTIONS:
435
+
436
+ // Creates a page. Returns its Box. index: Position (default: the end)
437
+ box.addPage = function (key, index = -1) {
438
+ const oldItem = findItem(key);
439
+ if (oldItem) {
440
+ console.warn("PageControl: A page with this key exists: " + key);
441
+ return oldItem.page;
442
+ }
443
+
444
+ // BOX: Page
445
+ // WHY: createIn() restores the container, so objects created after addPage() stay where they were.
446
+ let page = null;
447
+ createIn(box.track, function () {
448
+ page = Box({ width: "100%", height: "100%", color: _s.page.color });
449
+ });
450
+ page.css = {
451
+ position: (isSlide) ? "relative" : "absolute",
452
+ left: "0px",
453
+ top: "0px",
454
+ flex: "0 0 100%",
455
+ boxSizing: "border-box",
456
+ };
457
+ page.role = "group";
458
+ page.aria = { label: String(key) };
459
+ page.pageKey = key;
460
+ if (box.pageScroll == 1) page.scrollY = 1;
461
+ if (!isSlide) page.css.opacity = "0";
462
+
463
+ const item = { key: key, page: page };
464
+ if (index >= 0 && index < items.length) {
465
+ box.track.elem.insertBefore(page.elem, items[index].page.elem);
466
+ items.splice(index, 0, item);
467
+ } else {
468
+ items.push(item);
469
+ }
470
+ box.pages = box.getKeys();
471
+
472
+ if (box.value === null) box.value = key; // The first page is open.
473
+ createDots();
474
+ showActivePage(0);
475
+ startAutoPlay();
476
+ return page;
477
+ };
478
+ // USAGE: const page = pages.addPage("welcome") // A Box. Fill it with startPage() / endPage() or setDefaultContainerBox(page).
479
+
480
+ // Objects created between startPage(key) and endPage() go into the page.
481
+ box.startPage = function (key) {
482
+ const page = box.getPage(key) || box.addPage(key);
483
+ panelStack.push(getDefaultContainerBox());
484
+ if (page.wrapper) page.wrapper.remove();
485
+ setDefaultContainerBox(page);
486
+ // WHY: pageScroll: height "auto" + min-height 100%, so a long content can scroll. Otherwise 100%, so groups with height "100%" fill the page.
487
+ page.wrapper = VGroup({
488
+ width: "100%",
489
+ height: (box.pageScroll == 1) ? "auto" : "100%",
490
+ align: "left top",
491
+ gap: 0,
492
+ position: "relative",
493
+ });
494
+ page.wrapper.css = { alignItems: "stretch", minHeight: "100%" };
495
+ return page.wrapper;
496
+ };
497
+ // USAGE: pages.startPage("welcome"); Label({ text: "Hi" }); pages.endPage();
498
+ // NOTE: The wrapper is a VGroup (100% x 100%, grows with a long content). Start your own group in it for another layout.
499
+
500
+ box.endPage = function () {
501
+ endGroup();
502
+ const previous = panelStack.pop();
503
+ if (previous) setDefaultContainerBox(previous);
504
+ };
505
+
506
+ box.getPage = function (key = box.value) {
507
+ const item = findItem(key);
508
+ return (item) ? item.page : null;
509
+ };
510
+
511
+ box.getKeys = function () {
512
+ return items.map(function (item) { return item.key; });
513
+ };
514
+
515
+ box.getIndex = function (key = box.value) {
516
+ return getIndex(key);
517
+ };
518
+
519
+ box.getCount = function () {
520
+ return items.length;
521
+ };
522
+
523
+ // Opens a page. silent: 1 -> onChange is not called. Returns 1 when the page is opened.
524
+ box.open = function (key, silent = 0) {
525
+ const item = findItem(key);
526
+ if (!item) {
527
+ console.warn("PageControl: No page with this key: " + key);
528
+ return 0;
529
+ }
530
+ if (box.value === key) {
531
+ showActivePage(1);
532
+ return 1;
533
+ }
534
+ if (box.onBeforeChange(box, box.value, key) === false) {
535
+ showActivePage(1);
536
+ return 0;
537
+ }
538
+ const oldKey = box.value;
539
+ box.previousValue = oldKey;
540
+ box.value = key;
541
+ showActivePage(1);
542
+ scheduleClose(oldKey);
543
+ if (box.autoPlay > 0) startAutoPlay(); // Restart the timer after a manual change
544
+ if (!silent) box.onChange(box);
545
+ return 1;
546
+ };
547
+ // USAGE: pages.open("details")
548
+
549
+ box.openIndex = function (index, silent = 0) {
550
+ const item = items[index];
551
+ return (item) ? box.open(item.key, silent) : 0;
552
+ };
553
+
554
+ box.next = function (silent = 0) {
555
+ const index = getIndex(box.value);
556
+ if (index < 0) return 0;
557
+ let target = index + 1;
558
+ if (target >= items.length) {
559
+ if (box.loop != 1) return 0;
560
+ target = 0;
561
+ }
562
+ return box.open(items[target].key, silent);
563
+ };
564
+
565
+ box.previous = function (silent = 0) {
566
+ const index = getIndex(box.value);
567
+ if (index < 0) return 0;
568
+ let target = index - 1;
569
+ if (target < 0) {
570
+ if (box.loop != 1) return 0;
571
+ target = items.length - 1;
572
+ }
573
+ return box.open(items[target].key, silent);
574
+ };
575
+
576
+ box.isFirst = function () { return getIndex(box.value) === 0; };
577
+ box.isLast = function () { return getIndex(box.value) === items.length - 1; };
578
+
579
+ // Removes a page. When it is the open page, the next (or the previous) page is opened without motion.
580
+ box.removePage = function (key, silent = 0) {
581
+ const index = getIndex(key);
582
+ if (index < 0) return 0;
583
+ const wasOpen = (box.value === key);
584
+ items[index].page.remove();
585
+ items.splice(index, 1);
586
+ box.pages = box.getKeys();
587
+ if (wasOpen) {
588
+ const next = items[index] || items[index - 1] || null;
589
+ box.previousValue = key;
590
+ box.value = (next) ? next.key : null;
591
+ }
592
+ createDots();
593
+ showActivePage(0);
594
+ startAutoPlay();
595
+ if (wasOpen && !silent) box.onChange(box);
596
+ return 1;
597
+ };
598
+
599
+ box.clear = function () {
600
+ items.forEach(function (item) { item.page.remove(); });
601
+ items = [];
602
+ box.pages = [];
603
+ box.value = null;
604
+ box.previousValue = null;
605
+ createDots();
606
+ showActivePage(0);
607
+ stopAutoPlay();
608
+ };
609
+
610
+ box.setLoop = function (loop) {
611
+ box.loop = basic.isOn(loop) ? 1 : 0;
612
+ updateArrows();
613
+ };
614
+
615
+ box.setAutoPlay = function (seconds) {
616
+ box.autoPlay = Number(seconds) || 0;
617
+ startAutoPlay();
618
+ };
619
+ // USAGE: pages.setAutoPlay(4) or pages.setAutoPlay(0) to stop
620
+
621
+ box.setEnabled = function (enabled) {
622
+ box.enabled = basic.isOn(enabled) ? 1 : 0;
623
+ box.css.opacity = (box.enabled == 1) ? "1" : String(_s.disabled.opacity);
624
+ box.tabIndex = (box.enabled == 1 && box.keyboard == 1) ? 0 : -1;
625
+ updateArrows();
626
+ };
627
+
628
+ box.refresh = function () {
629
+ showActivePage(0);
630
+ };
631
+
632
+ box.focus = function () {
633
+ box.elem.focus({ preventScroll: true });
634
+ };
635
+
636
+
637
+ // *** OBJECT VIEW:
638
+
639
+ box.clipContent = 1;
640
+ box.clickable = 1;
641
+ // WHY: The browser keeps the scroll of the other direction, the swipe gets this one.
642
+ const getTouchAction = function () {
643
+ if (!isSlide || box.swipe != 1) return "";
644
+ return (isVertical) ? "pan-x" : "pan-y";
645
+ };
646
+ box.css = { outline: "none", touchAction: getTouchAction() };
647
+ box.role = "region";
648
+ box.aria = { label: box.ariaLabel };
649
+
650
+ // BOX: Track (slide: a flex row / column of the pages that moves; fade: stacked pages)
651
+ box.track = Box(0, 0, "100%", "100%", { color: "transparent", clickable: 1 });
652
+ if (isSlide) {
653
+ box.track.elem.style.display = "flex";
654
+ box.track.css.flexDirection = (isVertical) ? "column" : "row";
655
+ }
656
+ box.track.css.userSelect = "none";
657
+ box.track.css.overflow = "visible"; // WHY: basic.js boxes clip their content. The pages after the first one are outside the track; the component box clips instead.
658
+
659
+ // GROUP: Dots
660
+ if (box.dots == 1) {
661
+ box.dotsBox = HGroup({ width: "auto", height: "auto", align: "center center", gap: _s.dots.gap, padding: [6, 4] });
662
+ box.dotsBox.css = {
663
+ position: "absolute",
664
+ left: "50%",
665
+ top: "auto",
666
+ bottom: _s.dots.bottom + "px",
667
+ transform: "translateX(-50%)",
668
+ pointerEvents: "auto",
669
+ };
670
+ box.dotsBox.zIndex = CONTROLS_Z_INDEX;
671
+ box.dotsBox.role = "group";
672
+ box.dotsBox.aria = { label: "Page dots" };
673
+ endGroup();
674
+ }
675
+
676
+ // LABEL: Arrow buttons
677
+ if (box.arrows == 1) {
678
+ const createArrow = function (direction) {
679
+ const isLeft = (direction === "left");
680
+ const btn = Label({
681
+ text: PageControl.getArrowSvg(direction, _s.arrows.iconColor, _s.arrows.iconSize),
682
+ width: _s.arrows.size,
683
+ height: _s.arrows.size,
684
+ color: _s.arrows.color,
685
+ round: _s.arrows.round,
686
+ clickable: 1,
687
+ });
688
+ btn.css = { position: "absolute", top: "50%", transform: "translateY(-50%)" };
689
+ btn.elem.style.display = "flex";
690
+ btn.css = {
691
+ alignItems: "center",
692
+ justifyContent: "center",
693
+ transition: "background-color " + BUTTON_FADE_TIME + ", opacity " + BUTTON_FADE_TIME,
694
+ };
695
+ btn.lineHeight = "0";
696
+ btn.cursor = "pointer";
697
+ btn.zIndex = CONTROLS_Z_INDEX;
698
+ btn.boxShadow = _s.arrows.shadow;
699
+ btn.role = "button";
700
+ btn.aria = { label: (isLeft) ? "Previous page" : "Next page" };
701
+ if (isLeft) {
702
+ btn.css.left = _s.arrows.side + "px";
703
+ } else {
704
+ btn.css = { left: "auto", right: _s.arrows.side + "px" };
705
+ }
706
+ btn.on("mouseenter", function () {
707
+ btn.color = _s.arrows.hoverColor;
708
+ });
709
+ btn.on("mouseleave", function () {
710
+ btn.color = _s.arrows.color;
711
+ });
712
+ btn.on("pointerdown", function (self, event) {
713
+ event.stopPropagation(); // WHY: No swipe from the buttons.
714
+ });
715
+ return btn;
716
+ };
717
+ box.btnPrev = createArrow("left");
718
+ box.btnNext = createArrow("right");
719
+ box.btnPrev.on("click", function (self, event) {
720
+ event.stopPropagation();
721
+ box.previous();
722
+ });
723
+ box.btnNext.on("click", function (self, event) {
724
+ event.stopPropagation();
725
+ box.next();
726
+ });
727
+ }
728
+
729
+ // *** OBJECT INIT CODE:
730
+
731
+ box.on("pointerdown", onPointerDown);
732
+ box.on("pointermove", onPointerMove);
733
+ box.on("pointerup", onPointerUp);
734
+ box.on("pointercancel", onPointerUp);
735
+ box.on("keydown", onKeyDown);
736
+ box.on("mouseenter", function () { isHover = 1; });
737
+ box.on("mouseleave", function () { isHover = 0; });
738
+ // WHY :focus-visible: the ring is for the keyboard (a click focused the box and showed it too)
739
+ box.on("focus", function () {
740
+ box.boxShadow = box.elem.matches(":focus-visible") ? "inset 0 0 0 2px " + _s.focus.outlineColor : "none";
741
+ });
742
+ box.on("blur", function () {
743
+ box.boxShadow = "none";
744
+ });
745
+ box.listenTo(box.elem, "click", onClickCapture, true); // WHY: After a swipe, the click must not reach the page content.
746
+
747
+ (startPages || []).forEach(function (key) { box.addPage(key); });
748
+ if (startValue !== null && startValue !== undefined && findItem(startValue)) box.value = startValue;
749
+ box.setEnabled(box.enabled);
750
+ showActivePage(0);
751
+ startAutoPlay();
752
+
753
+ return endObject(box);
754
+
755
+ };
756
+
757
+ // *** STATIC FUNCTIONS:
758
+
759
+ PageControl.getArrowSvg = function (direction, color, size) {
760
+ const path = (direction === "left") ? "m15 6-6 6 6 6" : "m9 6 6 6-6 6";
761
+ 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>';
762
+ };