Skip to content

Commit c619b5e

Browse files
committed
Add smooth-steps behaviour flag (#627)
1 parent 1544357 commit c619b5e

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

documentation/behaviour-option.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
</section>
2727

2828

29+
<section>
30+
<ul>
31+
<li><a href="#section-tap">Tap</a></li>
32+
<li><a href="#section-tap">Drag</a></li>
33+
<li><a href="#section-fixed">Fixed dragging</a></li>
34+
<li><a href="#section-drag-all">Drag all handles</a></li>
35+
<li><a href="#section-snap">Snap</a></li>
36+
<li><a href="#section-hover">Hover</a></li>
37+
<li><a href="#section-unconstrained">Unconstrained</a></li>
38+
<li><a href="#section-smooth-steps">Smooth steps</a></li>
39+
</ul>
40+
</section>
41+
2942
<?php sect('examples'); ?>
3043
<h2>Example configurations</h2>
3144

@@ -212,13 +225,33 @@
212225
</section>
213226

214227

228+
<?php sect('smooth-steps'); ?>
229+
<h2>Smooth steps</h2>
230+
231+
<section>
232+
233+
<div class="view">
234+
<p>With this option set, handles will ignore <code>step</code> values while dragging. Steps are applied when a handle is released. The <code>"update"</code> event fires when a handle is released.</p>
235+
<div class="example">
236+
<div id="smooth-steps"></div>
237+
<span class="example-val" id="smooth-steps-values"></span>
238+
<?php run('smooth-steps'); ?>
239+
</div>
240+
</div>
241+
242+
<div class="side">
243+
<?php code('smooth-steps'); ?>
244+
</div>
245+
</section>
246+
247+
215248
<?php sect('combined'); ?>
216249
<h2>Combined options</h2>
217250

218251
<section>
219252

220253
<div class="view">
221-
<p>Most <code>'behaviour'</code> flags can be combined.</p>
254+
<p>Most <code>'behaviour'</code> flags can be combined. This example combines <code>'tap'</code>, <code>'drag'</code> and <code>'smooth-steps'</code>.</p>
222255
<div class="example">
223256
<div id="combined"></div>
224257
<?php run('combined'); ?>

documentation/behaviour-option/combined.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var dragTapSlider = document.getElementById('combined');
22

33
noUiSlider.create(dragTapSlider, {
44
start: [40, 60],
5-
behaviour: 'drag-tap',
5+
behaviour: 'drag-smooth-steps-tap',
6+
step: 10,
67
connect: true,
78
range: {
89
'min': 20,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var smoothStepsSlider = document.getElementById('smooth-steps');
2+
var smoothStepsValues = document.getElementById('smooth-steps-values');
3+
4+
noUiSlider.create(smoothStepsSlider, {
5+
start: [0, 100],
6+
behaviour: 'smooth-steps',
7+
step: 15,
8+
connect: true,
9+
range: {
10+
'min': 0,
11+
'max': 100
12+
}
13+
});
14+
15+
smoothStepsSlider.noUiSlider.on('update', function (values) {
16+
smoothStepsValues.innerHTML = values.join(' :: ');
17+
});

src/nouislider.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ interface Behaviour {
152152
tap: boolean;
153153
drag: boolean;
154154
dragAll: boolean;
155+
smoothSteps: boolean;
155156
fixed: boolean;
156157
snap: boolean;
157158
hover: boolean;
@@ -1126,6 +1127,7 @@ function testBehaviour(parsed: ParsedOptions, entry: unknown): void {
11261127
const hover = entry.indexOf("hover") >= 0;
11271128
const unconstrained = entry.indexOf("unconstrained") >= 0;
11281129
const dragAll = entry.indexOf("drag-all") >= 0;
1130+
const smoothSteps = entry.indexOf("smooth-steps") >= 0;
11291131

11301132
if (fixed) {
11311133
if (parsed.handles !== 2) {
@@ -1144,6 +1146,7 @@ function testBehaviour(parsed: ParsedOptions, entry: unknown): void {
11441146
tap: tap || snap,
11451147
drag: drag,
11461148
dragAll: dragAll,
1149+
smoothSteps: smoothSteps,
11471150
fixed: fixed,
11481151
snap: snap,
11491152
hover: hover,
@@ -2081,6 +2084,16 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
20812084
}
20822085
}
20832086

2087+
if (options.events.smoothSteps) {
2088+
data.handleNumbers.forEach(function (handleNumber) {
2089+
setHandle(handleNumber, scope_Locations[handleNumber], true, true, false, false);
2090+
});
2091+
2092+
data.handleNumbers.forEach(function (handleNumber: number) {
2093+
fireEvent("update", handleNumber);
2094+
});
2095+
}
2096+
20842097
data.handleNumbers.forEach(function (handleNumber: number) {
20852098
fireEvent("change", handleNumber);
20862099
fireEvent("set", handleNumber);
@@ -2448,7 +2461,8 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
24482461
to: number,
24492462
lookBackward: boolean,
24502463
lookForward: boolean,
2451-
getValue: boolean
2464+
getValue: boolean,
2465+
smoothSteps?: boolean
24522466
): number | false {
24532467
let distance;
24542468

@@ -2495,7 +2509,9 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
24952509
}
24962510
}
24972511

2498-
to = scope_Spectrum.getStep(to);
2512+
if (!smoothSteps) {
2513+
to = scope_Spectrum.getStep(to);
2514+
}
24992515

25002516
// Limit percentage to the 0 - 100 range
25012517
to = limit(to);
@@ -2528,6 +2544,8 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
25282544
// Store first handle now, so we still have it in case handleNumbers is reversed
25292545
const firstHandle = handleNumbers[0];
25302546

2547+
const smoothSteps = options.events.smoothSteps;
2548+
25312549
let b = [!upward, upward];
25322550
let f = [upward, !upward];
25332551

@@ -2549,7 +2567,8 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
25492567
proposals[handleNumber] + proposal,
25502568
b[o],
25512569
f[o],
2552-
false
2570+
false,
2571+
smoothSteps
25532572
);
25542573

25552574
// Stop if one of the handles can't move.
@@ -2571,7 +2590,8 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
25712590

25722591
// Step 2: Try to set the handles with the found percentage
25732592
handleNumbers.forEach(function (handleNumber, o) {
2574-
state = setHandle(handleNumber, locations[handleNumber] + proposal, b[o], f[o]) || state;
2593+
state =
2594+
setHandle(handleNumber, locations[handleNumber] + proposal, b[o], f[o], false, smoothSteps) || state;
25752595
});
25762596

25772597
// Step 3: If a handle moved, fire events
@@ -2631,10 +2651,19 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O
26312651
to: number | false,
26322652
lookBackward: boolean,
26332653
lookForward: boolean,
2634-
exactInput?: boolean
2654+
exactInput?: boolean,
2655+
smoothSteps?: boolean
26352656
): boolean {
26362657
if (!exactInput) {
2637-
to = checkHandlePosition(scope_Locations, handleNumber, <number>to, lookBackward, lookForward, false);
2658+
to = checkHandlePosition(
2659+
scope_Locations,
2660+
handleNumber,
2661+
<number>to,
2662+
lookBackward,
2663+
lookForward,
2664+
false,
2665+
smoothSteps
2666+
);
26382667
}
26392668

26402669
if (to === false) {

0 commit comments

Comments
 (0)