ozdenui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,838 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
Slider - v26.11
|
|
6
|
+
|
|
7
|
+
UI COMPONENT TEMPLATE
|
|
8
|
+
- NEW PROJECTS: a page or a project that needs a slider uses this component with styleName
|
|
9
|
+
"round" (02-comp-examples/slider.htm), unless the user asks for another look. "split" stays the default
|
|
10
|
+
of the file, for the pages made before. On a dark page: "roundDark". SliderField (comp/slider-field.js, a
|
|
11
|
+
slider in the shape of a field) is a parallel design: only when a page asks for that shape.
|
|
12
|
+
- A value from minValue to maxValue (0 - 100 by default) on a step (1), chosen by dragging the thumb. The value
|
|
13
|
+
bubble over the thumb shows the value while it is dragged (showValueOnDrag) and for a while after setValue()
|
|
14
|
+
(showValueOnSet). (minValue, maxValue, step, setMinValue(), setMaxValue(), setStep().)
|
|
15
|
+
- A press on the track: the mouse moves the thumb there and drags from there. A finger: a move to the
|
|
16
|
+
side drags from there, a tap moves the thumb there, a move up or down scrolls the page (touch-action: pan-y).
|
|
17
|
+
- The keyboard: ArrowRight / ArrowUp one step, ArrowLeft / ArrowDown one step back, PageUp / PageDown
|
|
18
|
+
ten steps, Home / End the ends. role "slider" with aria-valuenow / valuemin / valuemax (ariaLabel: its name);
|
|
19
|
+
a ring around the thumb only for the keyboard (style.focus).
|
|
20
|
+
- A key, a tap and a click on the track are short drags for the page: onChangeStart(self), onChange(self),
|
|
21
|
+
onChangeEnd(self), so a page that saves in onChangeEnd saves them too.
|
|
22
|
+
- Style packages: "split" (the default: inspired by Google Material 3: a thick track split in two at the
|
|
23
|
+
thumb, a thin line for the thumb, a dot at the end), "splitDark" (split for a dark page), "round" (a thin track and
|
|
24
|
+
a round white knob: the look of most web and phone sliders; a ring in the track color while it is dragged) and
|
|
25
|
+
"roundDark" (round for a dark page).
|
|
26
|
+
Select with styleName. (Slider.styles)
|
|
27
|
+
The colors and the height of the track stay params (trackColor, trackHeight...): a package fills the ones the
|
|
28
|
+
page does not give, so Slider({ styleName: "round", trackColor: "tomato" }) is a tomato round slider.
|
|
29
|
+
- Needs basic.js v26.10.27 (a package of params).
|
|
30
|
+
|
|
31
|
+
USAGE:
|
|
32
|
+
const volume = Slider({ width: 280, value: 70, onChange: function (self) { println(self.value); } });
|
|
33
|
+
volume.setValue(40);
|
|
34
|
+
Slider({ styleName: "round", width: "100%", value: 30 });
|
|
35
|
+
Slider({ styleName: "round", minValue: 12, maxValue: 24, step: 0.5, value: 16 }); // 12, 12.5 ... 24
|
|
36
|
+
|
|
37
|
+
Started Date: June 2024
|
|
38
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
39
|
+
|
|
40
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
41
|
+
Licensed under the Apache License, Version 2.0
|
|
42
|
+
SPDX-License-Identifier: Apache-2.0
|
|
43
|
+
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
"use strict";
|
|
47
|
+
|
|
48
|
+
// Default values
|
|
49
|
+
const SliderDefaults = {
|
|
50
|
+
key: 0,
|
|
51
|
+
width: 250,
|
|
52
|
+
|
|
53
|
+
value: 0, // Current selected value
|
|
54
|
+
minValue: 0, // The value at the left end (setMinValue)
|
|
55
|
+
maxValue: 100, // The value at the right end (setMaxValue)
|
|
56
|
+
step: 1, // The values are min + n * step (Ex: 0.5). setStep()
|
|
57
|
+
|
|
58
|
+
minThumbHeight: 44,
|
|
59
|
+
thumbColor: "rgba(0,0,0,0.7)", // Color of the draggable button
|
|
60
|
+
|
|
61
|
+
trackHeight: 16, // Ex: 16, 50
|
|
62
|
+
trackColor: "cadetblue", // Color of the filled part
|
|
63
|
+
trackBackgroundColor: "rgba(0,0,0,0.05)", // Color of the unfilled part
|
|
64
|
+
|
|
65
|
+
onChangeStart: function(self) {}, // A drag starts (a tap and a key are short drags)
|
|
66
|
+
onChange: function(self) {}, // self.value changed: while dragging, a tap, a key. Not at create time, not by setValue()
|
|
67
|
+
onChangeEnd: function(self) {}, // The drag ends: a page that saves, saves here
|
|
68
|
+
|
|
69
|
+
showValueAtStart: 0,
|
|
70
|
+
showValueOnSet: 1,
|
|
71
|
+
showValueOnDrag: 1,
|
|
72
|
+
|
|
73
|
+
ariaLabel: "", // The name a screen reader says (Ex: "Volume"). "": none
|
|
74
|
+
|
|
75
|
+
styleName: "split", // "split", "splitDark", "round", "roundDark", "minimal", "minimalDark" or a name added to Slider.styles
|
|
76
|
+
style: { // The split style package (default): inspired by Material 3
|
|
77
|
+
thumb: { // The part that is dragged: a line (split) or a knob (round)
|
|
78
|
+
areaWidth: 20, // px: the part that takes the mouse and the finger (the thumb is in its middle)
|
|
79
|
+
width: 4,
|
|
80
|
+
height: 0, // 0: as high as the slider
|
|
81
|
+
round: 2,
|
|
82
|
+
boxShadow: "", // "": none
|
|
83
|
+
},
|
|
84
|
+
dragging: { // The thumb while it is dragged ("": as it is)
|
|
85
|
+
width: 2, // the line gets thinner
|
|
86
|
+
trackSpace: 3, // the tracks come closer
|
|
87
|
+
ringSize: 0, // px: a ring in the track color around the thumb (0: none)
|
|
88
|
+
ringOpacity: 0.2,
|
|
89
|
+
},
|
|
90
|
+
track: {
|
|
91
|
+
space: 6, // px between the thumb and the tracks (negative: the tracks go under the thumb)
|
|
92
|
+
outerRound: 16, // the ends of the slider
|
|
93
|
+
innerRound: 4, // the ends at the thumb
|
|
94
|
+
},
|
|
95
|
+
trackDot: { // The dot at the end of the empty track (size 0: none)
|
|
96
|
+
size: 4,
|
|
97
|
+
right: 4,
|
|
98
|
+
},
|
|
99
|
+
value: { // The bubble of the value over the thumb
|
|
100
|
+
color: "rgba(0,0,0,0.9)",
|
|
101
|
+
textColor: "white",
|
|
102
|
+
fontSize: 14,
|
|
103
|
+
padding: [11, 9],
|
|
104
|
+
round: 16,
|
|
105
|
+
space: 6, // px between the bubble and the thumb
|
|
106
|
+
},
|
|
107
|
+
focus: { // The ring around the thumb when the slider has the focus of the keyboard
|
|
108
|
+
ringColor: "", // "": the track color
|
|
109
|
+
ringWidth: 2,
|
|
110
|
+
ringSpace: 2, // px between the thumb and the ring
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const Slider = function(params = {}) {
|
|
117
|
+
|
|
118
|
+
// Merge style package: params.style > Slider.styles[styleName] > SliderDefaults.style (split)
|
|
119
|
+
basic.applyStylePackage(params, Slider.styles, SliderDefaults);
|
|
120
|
+
|
|
121
|
+
// Merge params:
|
|
122
|
+
mergeIntoIfMissing(params, SliderDefaults);
|
|
123
|
+
|
|
124
|
+
// BOX: Component container
|
|
125
|
+
const box = startObject(params);
|
|
126
|
+
|
|
127
|
+
const _s = box.style;
|
|
128
|
+
// WHY: A value from a page can be a text ("12"): "12" + 1 * 0.5 would be "120.5".
|
|
129
|
+
box.minValue = Number(box.minValue);
|
|
130
|
+
box.maxValue = Number(box.maxValue);
|
|
131
|
+
box.step = Number(box.step);
|
|
132
|
+
const thumbHeight = (box.minThumbHeight > box.trackHeight) ? box.minThumbHeight : box.trackHeight;
|
|
133
|
+
box.height = thumbHeight;
|
|
134
|
+
box.clipContent = 0;
|
|
135
|
+
|
|
136
|
+
// *** PRIVATE VARIABLES:
|
|
137
|
+
let isDragging = false;
|
|
138
|
+
let isKeyboardFocus = 0; // The ring of the focus shows only for the keyboard
|
|
139
|
+
let isTrackClick = false; // A mouse press on the track that has not moved yet (its value stays shown after it)
|
|
140
|
+
let pendingTouch = null; // { x, y } of a finger on the track, until its direction is known
|
|
141
|
+
|
|
142
|
+
let startX = 0;
|
|
143
|
+
let startLeft = 0;
|
|
144
|
+
let dragScale = 1; // Screen px of one px of the slider, measured when a drag starts (getScreenScale)
|
|
145
|
+
let cancelHideValue = null; // the timer of showValue() (box.after)
|
|
146
|
+
|
|
147
|
+
const VALUE_SHOW_DURATION = 1200; // ms: the value bubble is hidden after it (showValue)
|
|
148
|
+
const TOUCH_SLOP = 6; // px: a smaller move of a finger is still a tap
|
|
149
|
+
const PAGE_STEPS = 10; // PageUp / PageDown
|
|
150
|
+
const FOCUS_RING_OPACITY = 0.8; // of the track color (style.focus.ringColor "")
|
|
151
|
+
|
|
152
|
+
let trackSpace = _s.track.space;
|
|
153
|
+
let isFirstRun = 1;
|
|
154
|
+
|
|
155
|
+
// *** PRIVATE FUNCTIONS:
|
|
156
|
+
|
|
157
|
+
// Maximum left position of the thumb (px).
|
|
158
|
+
// WHY: Read every time: with width: "100%" the real width comes after the layout and changes with the page.
|
|
159
|
+
// (A value read at create time let the thumb be dragged out of the slider.)
|
|
160
|
+
const getMaxLeft = function() {
|
|
161
|
+
const max = box.width - thumbBox.width;
|
|
162
|
+
return (max > 0) ? max : 0;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// WHY: Read every time: setMinValue() / setMaxValue() change the range. (A range of 0 would divide by 0.)
|
|
166
|
+
const getValueRange = function() {
|
|
167
|
+
const range = box.maxValue - box.minValue;
|
|
168
|
+
return (range > 0) ? range : 1;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// The number of decimals of the step (0.5 -> 1), so 0.1 + 0.2 is written 0.3, not 0.30000000000000004
|
|
172
|
+
const getStepDecimals = function() {
|
|
173
|
+
const text = String(box.step);
|
|
174
|
+
const dot = text.indexOf(".");
|
|
175
|
+
return (dot < 0) ? 0 : text.length - dot - 1;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const getStep = function() {
|
|
179
|
+
return (box.step > 0) ? box.step : 1;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// A value inside the range, on a step.
|
|
183
|
+
const toStepValue = function(value) {
|
|
184
|
+
value = Number(value);
|
|
185
|
+
if (!isFinite(value)) value = box.minValue;
|
|
186
|
+
const step = getStep();
|
|
187
|
+
const steps = Math.round((value - box.minValue) / step);
|
|
188
|
+
let stepValue = Number((box.minValue + steps * step).toFixed(getStepDecimals()));
|
|
189
|
+
if (stepValue < box.minValue) stepValue = box.minValue;
|
|
190
|
+
if (stepValue > box.maxValue) stepValue = box.maxValue;
|
|
191
|
+
return stepValue;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const getLeftOfValue = function(value) {
|
|
195
|
+
return (value - box.minValue) * (getMaxLeft() / getValueRange());
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// How many screen px is one px of the slider? (page.fit() zooms the page, a parent can have a scale)
|
|
199
|
+
// WHY: The mouse moves in screen px: on a page zoomed to 2 the thumb went twice as far as the mouse.
|
|
200
|
+
const getScreenScale = function() {
|
|
201
|
+
if (box.elem.offsetWidth <= 0) return 1;
|
|
202
|
+
return (box.elem.getBoundingClientRect().width / box.elem.offsetWidth) || 1;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// The value under a point of the screen: the middle of the thumb goes there.
|
|
206
|
+
const getValueAtClientX = function(clientX) {
|
|
207
|
+
const maxLeft = getMaxLeft();
|
|
208
|
+
if (maxLeft <= 0) return box.value;
|
|
209
|
+
const pointLeft = (clientX - box.elem.getBoundingClientRect().left) / getScreenScale();
|
|
210
|
+
const thumbLeft = Math.min(Math.max(pointLeft - thumbBox.width / 2, 0), maxLeft);
|
|
211
|
+
return toStepValue((thumbLeft / maxLeft) * getValueRange() + box.minValue);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// The value, its text in the bubble and for a screen reader. (Only this function writes them.)
|
|
215
|
+
const writeValue = function(value) {
|
|
216
|
+
box.value = value;
|
|
217
|
+
valueBox.text = value;
|
|
218
|
+
box.aria = {
|
|
219
|
+
valuenow: String(value),
|
|
220
|
+
valuemin: String(box.minValue),
|
|
221
|
+
valuemax: String(box.maxValue),
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const updateTrack = function(animated = 0) {
|
|
226
|
+
if (animated == 0) trackBox.dontMotion();
|
|
227
|
+
trackBox.width = thumbBox.left + thumbLineBox.left - trackSpace;
|
|
228
|
+
if (animated == 0) emptyTrackBox.dontMotion();
|
|
229
|
+
emptyTrackBox.width = box.width - (thumbBox.left + thumbBox.width) + thumbLineBox.left - trackSpace;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// The value bubble over the thumb: it grows out of the thumb and shrinks back into it.
|
|
233
|
+
const showValueBubble = function() {
|
|
234
|
+
valueBox.css.transform = "translateY(0px) scale(1)";
|
|
235
|
+
valueBox.opacity = 1;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const hideValueBubble = function() {
|
|
239
|
+
valueBox.css.transform = "translateY(25px) scale(0.1)";
|
|
240
|
+
valueBox.opacity = 0;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// WHY "- thumbLineBox.top": the space is over the thumb you see (a knob is lower than the top of its area).
|
|
244
|
+
const placeValueBubble = function() {
|
|
245
|
+
valueBox.aline(thumbBox, "top", _s.value.space - thumbLineBox.top, "center");
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const updateThumbPosition = function(newLeft, animated = 0) {
|
|
249
|
+
const maxLeft = getMaxLeft();
|
|
250
|
+
if (newLeft < 0 || !isFinite(newLeft)) newLeft = 0;
|
|
251
|
+
if (newLeft > maxLeft) newLeft = maxLeft;
|
|
252
|
+
// WHY round: parseInt cut the fraction of the position, so setValue(50) gave 49 (46 of 101 values were one less)
|
|
253
|
+
// WHY only with a width: a slider not drawn yet (a closed tab, a hidden group: width 0) has no position to read;
|
|
254
|
+
// its value became the minimum, and a settings page saved 0
|
|
255
|
+
let value = box.value;
|
|
256
|
+
if (maxLeft > 0) {
|
|
257
|
+
value = toStepValue(((newLeft / maxLeft) * getValueRange()) + box.minValue);
|
|
258
|
+
newLeft = getLeftOfValue(value); // WHY: the thumb stands on its step (a step of 1 on 0 - 10 is 30 px)
|
|
259
|
+
}
|
|
260
|
+
if (animated == 0) thumbBox.dontMotion();
|
|
261
|
+
thumbBox.left = newLeft;
|
|
262
|
+
updateTrack(animated);
|
|
263
|
+
if (animated == 0) valueBox.dontMotion();
|
|
264
|
+
writeValue(value);
|
|
265
|
+
placeValueBubble();
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// The thumb on a value (setValue, a key, a press on the track, a resize, a new range).
|
|
269
|
+
// WHY writeValue after: updateThumbPosition() reads the value back from the position (0 while it is hidden).
|
|
270
|
+
const moveThumbToValue = function(value, animated) {
|
|
271
|
+
showHideTracks(value, animated);
|
|
272
|
+
updateThumbPosition(getLeftOfValue(value), animated);
|
|
273
|
+
writeValue(value);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// The look of the thumb and of the tracks: style.thumb and style.track, or style.dragging while it is dragged.
|
|
277
|
+
const getThumbWidth = function($isDragging) {
|
|
278
|
+
if ($isDragging && _s.dragging.width !== "") return _s.dragging.width;
|
|
279
|
+
return _s.thumb.width;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const getTrackSpace = function($isDragging) {
|
|
283
|
+
if ($isDragging && _s.dragging.trackSpace !== "") return _s.dragging.trackSpace;
|
|
284
|
+
return _s.track.space;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// Its own shadow, and the ring in the track color while it is dragged ("": no shadow)
|
|
288
|
+
const getThumbShadow = function($isDragging) {
|
|
289
|
+
const shadows = [];
|
|
290
|
+
if (_s.thumb.boxShadow) shadows.push(_s.thumb.boxShadow);
|
|
291
|
+
if ($isDragging && _s.dragging.ringSize) {
|
|
292
|
+
const ringColor = Alpha(box.trackColor, _s.dragging.ringOpacity);
|
|
293
|
+
shadows.push("0 0 0 " + _s.dragging.ringSize + "px " + ringColor);
|
|
294
|
+
}
|
|
295
|
+
return shadows.join(", ");
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// While dragging: the look of style.dragging (split: a thinner thumb line, closer tracks) and the value bubble (showValueOnDrag).
|
|
299
|
+
// $isValueKept: at the end, the bubble stays for a while (a click on the track: it was there only for a moment).
|
|
300
|
+
const setIsDragging = function($isDragging, $isValueKept = false) {
|
|
301
|
+
isDragging = $isDragging;
|
|
302
|
+
trackSpace = getTrackSpace(isDragging);
|
|
303
|
+
// NOTE: withMotion() runs later: the callback reads the values of this call ($isDragging, $isValueKept).
|
|
304
|
+
thumbLineBox.withMotion(function(){
|
|
305
|
+
thumbLineBox.width = getThumbWidth($isDragging);
|
|
306
|
+
thumbLineBox.boxShadow = getThumbShadow($isDragging);
|
|
307
|
+
thumbLineBox.center("left");
|
|
308
|
+
updateTrack(1);
|
|
309
|
+
box.showValueClean();
|
|
310
|
+
if (!$isDragging && $isValueKept) {
|
|
311
|
+
box.showValue();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!$isDragging) {
|
|
315
|
+
hideValueBubble();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (box.showValueOnDrag) showValueBubble();
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// The ring around the thumb, only for the keyboard (style.focus).
|
|
323
|
+
const paintFocus = function() {
|
|
324
|
+
const ringColor = _s.focus.ringColor || Alpha(box.trackColor, FOCUS_RING_OPACITY);
|
|
325
|
+
thumbLineBox.css.outline = (isKeyboardFocus) ? _s.focus.ringWidth + "px solid " + ringColor : "";
|
|
326
|
+
thumbLineBox.css.outlineOffset = _s.focus.ringSpace + "px";
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// WHY try: matches() throws in an old browser that does not know :focus-visible.
|
|
330
|
+
const isFocusOfKeyboard = function() {
|
|
331
|
+
try {
|
|
332
|
+
return box.elem.matches(":focus-visible") ? 1 : 0;
|
|
333
|
+
} catch (error) {
|
|
334
|
+
return 0;
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const showHideTracks = function($currentLeft, animated) {
|
|
339
|
+
if (animated == 0) {
|
|
340
|
+
trackBox.dontMotion();
|
|
341
|
+
emptyTrackBox.dontMotion();
|
|
342
|
+
}
|
|
343
|
+
trackBox.opacity = ($currentLeft <= box.minValue) ? 0 : 1;
|
|
344
|
+
emptyTrackBox.opacity = ($currentLeft >= box.maxValue) ? 0 : 1;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// *** PUBLIC FUNCTIONS:
|
|
348
|
+
box.setValue = function(val) {
|
|
349
|
+
|
|
350
|
+
moveThumbToValue(toStepValue(val), 1); // WHY: the value is the truth, not the position (width 0 while hidden)
|
|
351
|
+
|
|
352
|
+
if (isFirstRun) {
|
|
353
|
+
isFirstRun = 0;
|
|
354
|
+
if (box.showValueAtStart && box.showValueOnSet) box.showValue();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (box.showValueOnSet) box.showValue();
|
|
358
|
+
|
|
359
|
+
};
|
|
360
|
+
// Sets the current slider value and updates UI
|
|
361
|
+
|
|
362
|
+
box.getValue = function() {
|
|
363
|
+
return box.value;
|
|
364
|
+
};
|
|
365
|
+
// Returns the current slider value
|
|
366
|
+
|
|
367
|
+
// Shows the value bubble for a while.
|
|
368
|
+
box.showValue = function() {
|
|
369
|
+
box.showValueClean();
|
|
370
|
+
showValueBubble();
|
|
371
|
+
cancelHideValue = box.after(VALUE_SHOW_DURATION, hideValueBubble);
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// Stops the timer of showValue() (the bubble is not hidden by it).
|
|
375
|
+
box.showValueClean = function() {
|
|
376
|
+
if (cancelHideValue) cancelHideValue();
|
|
377
|
+
cancelHideValue = null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// The value is put inside the new range and on a step; the thumb moves to it without the bubble.
|
|
381
|
+
const applyRange = function() {
|
|
382
|
+
moveThumbToValue(toStepValue(box.value), 0);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// The value at the left end. USAGE: slider.setMinValue(12)
|
|
386
|
+
box.setMinValue = function(val) {
|
|
387
|
+
box.minValue = Number(val);
|
|
388
|
+
applyRange();
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// The value at the right end. USAGE: slider.setMaxValue(24)
|
|
392
|
+
box.setMaxValue = function(val) {
|
|
393
|
+
box.maxValue = Number(val);
|
|
394
|
+
applyRange();
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// The values are min + n * step. USAGE: slider.setStep(0.5)
|
|
398
|
+
box.setStep = function(val) {
|
|
399
|
+
box.step = Number(val);
|
|
400
|
+
applyRange();
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// Sets the color of the filled portion of the slider
|
|
404
|
+
box.setTrackColor = function(color) {
|
|
405
|
+
box.trackColor = color;
|
|
406
|
+
trackBox.color = color;
|
|
407
|
+
trackDot.color = color;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
// Sets the color of the unfilled portion of the slider
|
|
411
|
+
box.setTrackBackgroundColor = function(color) {
|
|
412
|
+
box.trackBackgroundColor = color;
|
|
413
|
+
emptyTrackBox.color = color;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
// Sets the color of the draggable thumb
|
|
417
|
+
box.setThumbColor = function(color) {
|
|
418
|
+
box.thumbColor = color;
|
|
419
|
+
thumbLineBox.color = color;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// *** OBJECT VIEW:
|
|
423
|
+
|
|
424
|
+
// BOX: Track
|
|
425
|
+
const trackBox = Box({
|
|
426
|
+
height: box.trackHeight,
|
|
427
|
+
width: 0,
|
|
428
|
+
left: 0,
|
|
429
|
+
top: 0,
|
|
430
|
+
color: box.trackColor,
|
|
431
|
+
borderColor: "rgba(0, 0, 0, 0.4)",
|
|
432
|
+
});
|
|
433
|
+
that.css = {
|
|
434
|
+
borderTopRightRadius: _s.track.innerRound + "px",
|
|
435
|
+
borderTopLeftRadius: _s.track.outerRound + "px",
|
|
436
|
+
borderBottomRightRadius: _s.track.innerRound + "px",
|
|
437
|
+
borderBottomLeftRadius: _s.track.outerRound + "px",
|
|
438
|
+
};
|
|
439
|
+
that.setMotion("width 0.2s, opacity 0.2s");
|
|
440
|
+
that.center("top");
|
|
441
|
+
|
|
442
|
+
// BOX: Empty track
|
|
443
|
+
const emptyTrackBox = Box({
|
|
444
|
+
height: box.trackHeight,
|
|
445
|
+
width: 0,
|
|
446
|
+
right: 0,
|
|
447
|
+
top: 0,
|
|
448
|
+
color: box.trackBackgroundColor,
|
|
449
|
+
});
|
|
450
|
+
that.css = {
|
|
451
|
+
borderTopRightRadius: _s.track.outerRound + "px",
|
|
452
|
+
borderTopLeftRadius: _s.track.innerRound + "px",
|
|
453
|
+
borderBottomRightRadius: _s.track.outerRound + "px",
|
|
454
|
+
borderBottomLeftRadius: _s.track.innerRound + "px",
|
|
455
|
+
};
|
|
456
|
+
that.setMotion("width 0.2s, opacity 0.2s");
|
|
457
|
+
that.center("top");
|
|
458
|
+
|
|
459
|
+
// BOX: Track dot
|
|
460
|
+
const trackDot = Box({
|
|
461
|
+
color: box.trackColor,
|
|
462
|
+
width: _s.trackDot.size,
|
|
463
|
+
height: _s.trackDot.size,
|
|
464
|
+
round: _s.trackDot.size,
|
|
465
|
+
});
|
|
466
|
+
emptyTrackBox.add(that);
|
|
467
|
+
that.center("top");
|
|
468
|
+
that.right = _s.trackDot.right;
|
|
469
|
+
|
|
470
|
+
// BOX: Thumb (the draggable part)
|
|
471
|
+
const thumbBox = startBox({
|
|
472
|
+
width: _s.thumb.areaWidth,
|
|
473
|
+
height: thumbHeight,
|
|
474
|
+
top: 0,
|
|
475
|
+
left: 0,
|
|
476
|
+
color: "transparent",
|
|
477
|
+
clickable: 1,
|
|
478
|
+
clipContent: 0, // WHY: the ring of a dragged knob (style.dragging.ringSize) is wider than the thumb
|
|
479
|
+
});
|
|
480
|
+
that.cursor = "pointer";
|
|
481
|
+
that.setMotion("left 0.2s");
|
|
482
|
+
|
|
483
|
+
// BOX: Thumb line (split) or knob (round)
|
|
484
|
+
const thumbLineBox = Box({
|
|
485
|
+
top: 0,
|
|
486
|
+
width: _s.thumb.width,
|
|
487
|
+
height: _s.thumb.height || thumbHeight,
|
|
488
|
+
color: box.thumbColor,
|
|
489
|
+
round: _s.thumb.round,
|
|
490
|
+
borderColor: "rgba(0, 0, 0, 0.4)",
|
|
491
|
+
boxShadow: getThumbShadow(false),
|
|
492
|
+
});
|
|
493
|
+
that.setMotion("width 0.2s, left 0.2s, box-shadow 0.2s");
|
|
494
|
+
that.center("left");
|
|
495
|
+
that.center("top");
|
|
496
|
+
|
|
497
|
+
endBox();
|
|
498
|
+
|
|
499
|
+
// LABEL: Value
|
|
500
|
+
const valueBox = Label({
|
|
501
|
+
text: "67",
|
|
502
|
+
color: _s.value.color,
|
|
503
|
+
textColor: _s.value.textColor,
|
|
504
|
+
fontSize: _s.value.fontSize,
|
|
505
|
+
padding: _s.value.padding,
|
|
506
|
+
round: _s.value.round,
|
|
507
|
+
});
|
|
508
|
+
placeValueBubble();
|
|
509
|
+
hideValueBubble();
|
|
510
|
+
valueBox.setMotion("transform 0.2s, opacity 0.2s, left 0.2s");
|
|
511
|
+
|
|
512
|
+
// *** OBJECT INIT CODE:
|
|
513
|
+
|
|
514
|
+
// Mouse Events
|
|
515
|
+
// WHY: "e.clientX || e.touches[0]" threw at the left edge of the screen: clientX 0 is falsy, a mouse event has no touches.
|
|
516
|
+
const getClientX = (e) => (e.touches && e.touches.length) ? e.touches[0].clientX : e.clientX;
|
|
517
|
+
const getClientY = (e) => (e.touches && e.touches.length) ? e.touches[0].clientY : e.clientY;
|
|
518
|
+
|
|
519
|
+
// The removers of the document events of a drag (box.listenTo: remove() cleans them too)
|
|
520
|
+
let dragEventRemovers = [];
|
|
521
|
+
|
|
522
|
+
const removeDragEvents = () => {
|
|
523
|
+
dragEventRemovers.forEach((removeEvent) => removeEvent());
|
|
524
|
+
dragEventRemovers = [];
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
const startDrag = (clientX) => {
|
|
528
|
+
setIsDragging(true);
|
|
529
|
+
startX = clientX;
|
|
530
|
+
startLeft = parseFloat(thumbBox.left) || 0; // (not parseInt: a click on the thumb moved it a part of a pixel)
|
|
531
|
+
dragScale = getScreenScale();
|
|
532
|
+
|
|
533
|
+
removeDragEvents(); // WHY: a second touch before the end of a drag must not add the events twice
|
|
534
|
+
dragEventRemovers = [
|
|
535
|
+
box.listenTo(document, "mousemove", onMouseMove),
|
|
536
|
+
box.listenTo(document, "mouseup", onMouseUp),
|
|
537
|
+
box.listenTo(document, "touchmove", onMouseMove, { passive: false }),
|
|
538
|
+
box.listenTo(document, "touchend", onMouseUp),
|
|
539
|
+
// WHY: The system can take a touch (a call, a gesture of the screen): its touchend never comes, and the
|
|
540
|
+
// next touch anywhere on the page moved the thumb and could not scroll the page.
|
|
541
|
+
box.listenTo(document, "touchcancel", onMouseUp),
|
|
542
|
+
];
|
|
543
|
+
box.onChangeStart(box);
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// The thumb goes to a point of the track; the page hears it when the value changes (onChange).
|
|
547
|
+
const moveThumbToPoint = (clientX, animated) => {
|
|
548
|
+
const value = getValueAtClientX(clientX);
|
|
549
|
+
if (value === box.value) return;
|
|
550
|
+
moveThumbToValue(value, animated);
|
|
551
|
+
box.onChange(box);
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
// A press on the thumb starts a drag. A press of the mouse on the track moves the thumb there and drags from there.
|
|
555
|
+
const onPress = (self, e) => {
|
|
556
|
+
// WHY: Only the main button: the right one (its menu) started a drag too.
|
|
557
|
+
const isOtherButton = (e.type === "mousedown" && e.button !== 0);
|
|
558
|
+
if (isOtherButton) return;
|
|
559
|
+
isKeyboardFocus = 0;
|
|
560
|
+
paintFocus();
|
|
561
|
+
const isOnThumb = thumbBox.elem.contains(e.target);
|
|
562
|
+
if (!isOnThumb && e.type === "touchstart") {
|
|
563
|
+
waitForTouchDirection(e);
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
// WHY: After a touch the browser sends mousedown and mouseup too: a tap gave two onChangeStart and two onChangeEnd.
|
|
567
|
+
if (e.type === "touchstart") e.preventDefault();
|
|
568
|
+
if (isOnThumb) {
|
|
569
|
+
startDrag(getClientX(e));
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
isTrackClick = true;
|
|
573
|
+
startDrag(getClientX(e));
|
|
574
|
+
moveThumbToPoint(getClientX(e), 1);
|
|
575
|
+
startLeft = parseFloat(thumbBox.left) || 0; // the drag goes on from the new place
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
// A finger on the track: a move to the side drags from there, a lift is a tap there, a move up or down is the
|
|
579
|
+
// scroll of the page. WHY: a finger that scrolls a page of sliders must not change their values.
|
|
580
|
+
const waitForTouchDirection = (e) => {
|
|
581
|
+
pendingTouch = { x: getClientX(e), y: getClientY(e) };
|
|
582
|
+
removeDragEvents();
|
|
583
|
+
dragEventRemovers = [
|
|
584
|
+
box.listenTo(document, "touchmove", onPendingTouchMove, { passive: false }),
|
|
585
|
+
box.listenTo(document, "touchend", onPendingTouchEnd),
|
|
586
|
+
box.listenTo(document, "touchcancel", cancelPendingTouch),
|
|
587
|
+
];
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const cancelPendingTouch = () => {
|
|
591
|
+
pendingTouch = null;
|
|
592
|
+
removeDragEvents();
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
const onPendingTouchMove = (self, e) => {
|
|
596
|
+
const dx = getClientX(e) - pendingTouch.x;
|
|
597
|
+
const dy = getClientY(e) - pendingTouch.y;
|
|
598
|
+
const isSmallMove = (Math.abs(dx) < TOUCH_SLOP && Math.abs(dy) < TOUCH_SLOP);
|
|
599
|
+
if (isSmallMove) return;
|
|
600
|
+
if (Math.abs(dy) >= Math.abs(dx)) {
|
|
601
|
+
cancelPendingTouch(); // the page scrolls
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
e.preventDefault();
|
|
605
|
+
pendingTouch = null;
|
|
606
|
+
startDrag(getClientX(e));
|
|
607
|
+
moveThumbToPoint(getClientX(e), 0);
|
|
608
|
+
startLeft = parseFloat(thumbBox.left) || 0;
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
// A tap on the track: a short drag for the page (onChangeStart, onChange, onChangeEnd).
|
|
612
|
+
const onPendingTouchEnd = (self, e) => {
|
|
613
|
+
e.preventDefault(); // WHY: the mousedown and mouseup of the browser after a tap would be a second press
|
|
614
|
+
const tapX = pendingTouch.x;
|
|
615
|
+
cancelPendingTouch();
|
|
616
|
+
box.onChangeStart(box);
|
|
617
|
+
moveThumbToPoint(tapX, 1);
|
|
618
|
+
box.onChangeEnd(box);
|
|
619
|
+
if (box.showValueOnDrag) box.showValue();
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
const onMouseMove = (self, e) => {
|
|
623
|
+
if (!isDragging) return;
|
|
624
|
+
e.preventDefault();
|
|
625
|
+
isTrackClick = false;
|
|
626
|
+
const clientX = getClientX(e);
|
|
627
|
+
const maxLeft = getMaxLeft();
|
|
628
|
+
let dx = (clientX - startX) / dragScale;
|
|
629
|
+
let newLeft = startLeft + dx;
|
|
630
|
+
|
|
631
|
+
if (newLeft < 0) newLeft = 0;
|
|
632
|
+
if (newLeft > maxLeft) newLeft = maxLeft;
|
|
633
|
+
|
|
634
|
+
updateThumbPosition(newLeft);
|
|
635
|
+
showHideTracks(box.value, 0);
|
|
636
|
+
box.onChange(box);
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const onMouseUp = () => {
|
|
640
|
+
removeDragEvents();
|
|
641
|
+
setIsDragging(false, isTrackClick && box.showValueOnDrag);
|
|
642
|
+
isTrackClick = false;
|
|
643
|
+
box.onChangeEnd(box);
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
// The keys that change the value (a key is a short drag for the page: onChangeStart, onChange, onChangeEnd).
|
|
647
|
+
const keyValues = {
|
|
648
|
+
ArrowRight: () => box.value + getStep(),
|
|
649
|
+
ArrowUp: () => box.value + getStep(),
|
|
650
|
+
ArrowLeft: () => box.value - getStep(),
|
|
651
|
+
ArrowDown: () => box.value - getStep(),
|
|
652
|
+
PageUp: () => box.value + getStep() * PAGE_STEPS,
|
|
653
|
+
PageDown: () => box.value - getStep() * PAGE_STEPS,
|
|
654
|
+
Home: () => box.minValue,
|
|
655
|
+
End: () => box.maxValue,
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
const onKeyDown = (self, e) => {
|
|
659
|
+
if (!Object.prototype.hasOwnProperty.call(keyValues, e.key)) return;
|
|
660
|
+
e.preventDefault(); // WHY: the arrows and PageUp / PageDown scrolled the page too
|
|
661
|
+
isKeyboardFocus = 1;
|
|
662
|
+
paintFocus();
|
|
663
|
+
const value = toStepValue(keyValues[e.key]());
|
|
664
|
+
if (value === box.value) return;
|
|
665
|
+
box.onChangeStart(box);
|
|
666
|
+
moveThumbToValue(value, 1);
|
|
667
|
+
if (box.showValueOnDrag) box.showValue();
|
|
668
|
+
box.onChange(box);
|
|
669
|
+
box.onChangeEnd(box);
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
const onFocus = () => {
|
|
673
|
+
isKeyboardFocus = isFocusOfKeyboard();
|
|
674
|
+
paintFocus();
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
const onBlur = () => {
|
|
678
|
+
isKeyboardFocus = 0;
|
|
679
|
+
paintFocus();
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
box.role = "slider";
|
|
683
|
+
box.tabIndex = 0;
|
|
684
|
+
box.aria = { label: box.ariaLabel };
|
|
685
|
+
box.cursor = "pointer";
|
|
686
|
+
// WHY pan-y: a finger that moves up or down on the slider scrolls the page; a move to the side is ours.
|
|
687
|
+
// WHY outline none: the ring of the focus is around the thumb (paintFocus), not around the whole slider.
|
|
688
|
+
box.css = { touchAction: "pan-y", outline: "none" };
|
|
689
|
+
|
|
690
|
+
box.on("mousedown", onPress);
|
|
691
|
+
box.on("touchstart", onPress, { passive: false });
|
|
692
|
+
box.on("keydown", onKeyDown);
|
|
693
|
+
box.on("focus", onFocus);
|
|
694
|
+
box.on("blur", onBlur);
|
|
695
|
+
|
|
696
|
+
// WHY: With width: "100%" the slider gets its real width after the layout, and it changes with
|
|
697
|
+
// the page. The thumb must stay on its value. (No animation and no value bubble here.)
|
|
698
|
+
box.onResize(function() {
|
|
699
|
+
if (isDragging) return;
|
|
700
|
+
moveThumbToValue(box.value, 0);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
box.setValue(box.value);
|
|
704
|
+
|
|
705
|
+
return endObject(box);
|
|
706
|
+
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// *** STYLE PACKAGES:
|
|
710
|
+
// USAGE: Slider({ styleName: "round" })
|
|
711
|
+
// USAGE: Slider({ styleName: "round", trackColor: "tomato" }) // The colors and the height of the track are params.
|
|
712
|
+
// USAGE: Slider({ styleName: "round", style: { value: { color: "tomato" } } }) // Change only some keys.
|
|
713
|
+
// NOTE: A new package needs only the keys that differ from the default (split) style.
|
|
714
|
+
// USAGE: Slider.styles.myStyle = { trackColor: "tomato", style: { trackDot: { size: 0 } } };
|
|
715
|
+
Slider.styles = {
|
|
716
|
+
|
|
717
|
+
// Inspired by Google Material 3: a thick track in two parts with a space at the thumb, a thin line for the
|
|
718
|
+
// thumb (thinner while it is dragged), a dot at the end of the empty track.
|
|
719
|
+
split: SliderDefaults.style,
|
|
720
|
+
|
|
721
|
+
// The split look for a dark page: a light empty track, the main cyan of the kit, a light thumb line and a light
|
|
722
|
+
// value bubble with a dark text (a dark bubble was lost on the page).
|
|
723
|
+
splitDark: {
|
|
724
|
+
trackColor: "#5BBAC1",
|
|
725
|
+
trackBackgroundColor: White(0.12),
|
|
726
|
+
thumbColor: White(0.85),
|
|
727
|
+
style: {
|
|
728
|
+
value: {
|
|
729
|
+
color: "#F2F2F2",
|
|
730
|
+
textColor: "#232323",
|
|
731
|
+
},
|
|
732
|
+
},
|
|
733
|
+
},
|
|
734
|
+
|
|
735
|
+
// The look of most web and phone sliders: a thin round track, a round white knob with a soft
|
|
736
|
+
// shadow, a ring in the track color while it is dragged, a small value bubble. A package of params: the colors
|
|
737
|
+
// and the height of the track are params, a page can give its own.
|
|
738
|
+
round: {
|
|
739
|
+
trackHeight: 6,
|
|
740
|
+
trackBackgroundColor: "rgba(0, 0, 0, 0.12)",
|
|
741
|
+
thumbColor: "white",
|
|
742
|
+
style: {
|
|
743
|
+
thumb: {
|
|
744
|
+
areaWidth: 24, // as wide as the knob: the knob goes to the ends of the track
|
|
745
|
+
width: 24,
|
|
746
|
+
height: 24,
|
|
747
|
+
round: 12,
|
|
748
|
+
boxShadow: "0 1px 3px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(0, 0, 0, 0.04)",
|
|
749
|
+
},
|
|
750
|
+
dragging: {
|
|
751
|
+
width: "",
|
|
752
|
+
trackSpace: "",
|
|
753
|
+
ringSize: 6,
|
|
754
|
+
ringOpacity: 0.2,
|
|
755
|
+
},
|
|
756
|
+
track: {
|
|
757
|
+
space: -12, // the tracks meet under the middle of the knob
|
|
758
|
+
outerRound: 3,
|
|
759
|
+
innerRound: 3,
|
|
760
|
+
},
|
|
761
|
+
trackDot: {
|
|
762
|
+
size: 0,
|
|
763
|
+
},
|
|
764
|
+
value: {
|
|
765
|
+
color: "#232323",
|
|
766
|
+
fontSize: 13,
|
|
767
|
+
padding: [9, 5],
|
|
768
|
+
round: 8,
|
|
769
|
+
space: 10, // (over the ring of the drag)
|
|
770
|
+
},
|
|
771
|
+
},
|
|
772
|
+
},
|
|
773
|
+
|
|
774
|
+
// The round look for a dark page: a light empty track, the main cyan of the kit, a stronger ring
|
|
775
|
+
// and a light value bubble with a dark text (a dark bubble was lost on the page).
|
|
776
|
+
roundDark: {
|
|
777
|
+
trackHeight: 6,
|
|
778
|
+
trackColor: "#5BBAC1",
|
|
779
|
+
trackBackgroundColor: White(0.16),
|
|
780
|
+
thumbColor: "white",
|
|
781
|
+
style: {
|
|
782
|
+
thumb: {
|
|
783
|
+
areaWidth: 24,
|
|
784
|
+
width: 24,
|
|
785
|
+
height: 24,
|
|
786
|
+
round: 12,
|
|
787
|
+
boxShadow: "0 1px 3px rgba(0, 0, 0, 0.5)",
|
|
788
|
+
},
|
|
789
|
+
dragging: {
|
|
790
|
+
width: "",
|
|
791
|
+
trackSpace: "",
|
|
792
|
+
ringSize: 6,
|
|
793
|
+
ringOpacity: 0.3,
|
|
794
|
+
},
|
|
795
|
+
track: {
|
|
796
|
+
space: -12,
|
|
797
|
+
outerRound: 3,
|
|
798
|
+
innerRound: 3,
|
|
799
|
+
},
|
|
800
|
+
trackDot: {
|
|
801
|
+
size: 0,
|
|
802
|
+
},
|
|
803
|
+
value: {
|
|
804
|
+
color: "#F2F2F2",
|
|
805
|
+
textColor: "#232323",
|
|
806
|
+
fontSize: 13,
|
|
807
|
+
padding: [9, 5],
|
|
808
|
+
round: 8,
|
|
809
|
+
space: 10,
|
|
810
|
+
},
|
|
811
|
+
},
|
|
812
|
+
},
|
|
813
|
+
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
// The minimal theme (_designs/minimal-theme.htm): a 2 px track, the chosen part in ink, a 12 px knob in the ground of
|
|
817
|
+
// the window with a thin ink line around it (a line, not a shadow), a small ink value bubble, a blue focus ring.
|
|
818
|
+
Slider.createMinimalStyle = function (colors) {
|
|
819
|
+
return {
|
|
820
|
+
minThumbHeight: 20,
|
|
821
|
+
trackHeight: 2,
|
|
822
|
+
trackColor: colors.ink,
|
|
823
|
+
trackBackgroundColor: colors.track,
|
|
824
|
+
thumbColor: colors.ground,
|
|
825
|
+
style: {
|
|
826
|
+
thumb: { areaWidth: 12, width: 12, height: 12, round: 6, boxShadow: "0 0 0 1px " + colors.ink },
|
|
827
|
+
dragging: { width: "", trackSpace: "", ringSize: 0, ringOpacity: 0 },
|
|
828
|
+
track: { space: -6, outerRound: 1, innerRound: 1 },
|
|
829
|
+
trackDot: { size: 0 },
|
|
830
|
+
value: { color: colors.ink, textColor: colors.onInk, fontSize: 11, padding: [6, 2], round: 3, space: 6 },
|
|
831
|
+
focus: { ringColor: colors.focus, ringWidth: 2, ringSpace: 2 },
|
|
832
|
+
},
|
|
833
|
+
};
|
|
834
|
+
};
|
|
835
|
+
Slider.MINIMAL_COLORS = { ground: "#FFFFFF", ink: "#1F1F1F", onInk: "#FFFFFF", track: Black(0.12), focus: "#3D8BD9" };
|
|
836
|
+
Slider.styles.minimal = Slider.createMinimalStyle(Slider.MINIMAL_COLORS);
|
|
837
|
+
Slider.MINIMAL_DARK_COLORS = { ground: "#1A1A19", ink: "#EDEDEB", onInk: "#1A1A19", track: White(0.14), focus: "#7FB6DE" };
|
|
838
|
+
Slider.styles.minimalDark = Slider.createMinimalStyle(Slider.MINIMAL_DARK_COLORS);
|