Skip to content

Commit 0ad91f8

Browse files
committed
Handle exact value on tick click
1 parent f52b146 commit 0ad91f8

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

demo/demo.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
237237
}
238238
};
239239

240+
//Slider with ticks at intermediate positions
241+
$scope.slider_ticks_at = {
242+
value: 500,
243+
options: {
244+
ceil: 1000,
245+
floor: 0,
246+
showTicks: 100
247+
}
248+
};
249+
240250
//Slider with draggable range
241251
$scope.slider_draggable_range = {
242252
minValue: 1,

demo/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ <h2>Range slider with ticks and values</h2>
181181
></rzslider>
182182
</article>
183183

184+
<article>
185+
<h2>Slider with ticks at intermediate positions</h2>
186+
<rzslider
187+
rz-slider-model="slider_ticks_at.value"
188+
rz-slider-options="slider_ticks_at.options"
189+
></rzslider>
190+
</article>
191+
184192
<article>
185193
<h2>Slider with draggable range</h2>
186194
<rzslider

dist/rzslider.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@
10781078
/**
10791079
* Return the translated value if a translate function is provided else the original value
10801080
* @param value
1081+
* @param which if it's min or max handle
10811082
* @returns {*}
10821083
*/
10831084
getDisplayValue: function(value, which) {
@@ -1088,11 +1089,13 @@
10881089
* Round value to step and precision based on minValue
10891090
*
10901091
* @param {number} value
1092+
* @param {number} customStep a custom step to override the defined step
10911093
* @returns {number}
10921094
*/
1093-
roundStep: function(value) {
1094-
var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
1095-
steppedDifference = Math.round(steppedDifference) * this.step;
1095+
roundStep: function(value, customStep) {
1096+
var step = customStep ? customStep : this.step,
1097+
steppedDifference = parseFloat((value - this.minValue) / step).toPrecision(12);
1098+
steppedDifference = Math.round(+steppedDifference) * step;
10961099
var newValue = (this.minValue + steppedDifference).toFixed(this.precision);
10971100
return +newValue;
10981101
},
@@ -1335,7 +1338,7 @@
13351338
this.fullBar.on('mousedown', angular.bind(this, this.onStart, null, null));
13361339
this.fullBar.on('mousedown', angular.bind(this, this.onMove, this.fullBar));
13371340
this.ticks.on('mousedown', angular.bind(this, this.onStart, null, null));
1338-
this.ticks.on('mousedown', angular.bind(this, this.onMove, this.ticks));
1341+
this.ticks.on('mousedown', angular.bind(this, this.onTickClick, this.ticks));
13391342
}
13401343
}
13411344

@@ -1355,7 +1358,7 @@
13551358
this.fullBar.on('touchstart', angular.bind(this, this.onStart, null, null));
13561359
this.fullBar.on('touchstart', angular.bind(this, this.onMove, this.fullBar));
13571360
this.ticks.on('touchstart', angular.bind(this, this.onStart, null, null));
1358-
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
1361+
this.ticks.on('touchstart', angular.bind(this, this.onTickClick, this.ticks));
13591362
}
13601363
}
13611364

@@ -1424,9 +1427,10 @@
14241427
*
14251428
* @param {jqLite} pointer
14261429
* @param {Event} event The event
1430+
* @param {boolean} fromTick if the event occured on a tick or not
14271431
* @returns {undefined}
14281432
*/
1429-
onMove: function(pointer, event) {
1433+
onMove: function(pointer, event, fromTick) {
14301434
var newOffset = this.getEventPosition(event),
14311435
newValue,
14321436
ceilValue = this.options.rightToLeft ? this.minValue : this.maxValue,
@@ -1438,7 +1442,10 @@
14381442
newValue = ceilValue;
14391443
} else {
14401444
newValue = this.offsetToValue(newOffset);
1441-
newValue = this.roundStep(newValue);
1445+
if(fromTick && angular.isNumber(this.options.showTicks))
1446+
newValue = this.roundStep(newValue, this.options.showTicks);
1447+
else
1448+
newValue = this.roundStep(newValue);
14421449
}
14431450
this.positionTrackingHandle(newValue);
14441451
},
@@ -1465,6 +1472,10 @@
14651472
this.callOnEnd();
14661473
},
14671474

1475+
onTickClick: function(pointer, event) {
1476+
this.onMove(pointer, event, true);
1477+
},
1478+
14681479
onPointerFocus: function(pointer, ref) {
14691480
this.tracking = ref;
14701481
pointer.one('blur', angular.bind(this, this.onPointerBlur, pointer));

dist/rzslider.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rzslider.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@
10821082
/**
10831083
* Return the translated value if a translate function is provided else the original value
10841084
* @param value
1085+
* @param which if it's min or max handle
10851086
* @returns {*}
10861087
*/
10871088
getDisplayValue: function(value, which) {
@@ -1092,11 +1093,13 @@
10921093
* Round value to step and precision based on minValue
10931094
*
10941095
* @param {number} value
1096+
* @param {number} customStep a custom step to override the defined step
10951097
* @returns {number}
10961098
*/
1097-
roundStep: function(value) {
1098-
var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
1099-
steppedDifference = Math.round(steppedDifference) * this.step;
1099+
roundStep: function(value, customStep) {
1100+
var step = customStep ? customStep : this.step,
1101+
steppedDifference = parseFloat((value - this.minValue) / step).toPrecision(12);
1102+
steppedDifference = Math.round(+steppedDifference) * step;
11001103
var newValue = (this.minValue + steppedDifference).toFixed(this.precision);
11011104
return +newValue;
11021105
},
@@ -1339,7 +1342,7 @@
13391342
this.fullBar.on('mousedown', angular.bind(this, this.onStart, null, null));
13401343
this.fullBar.on('mousedown', angular.bind(this, this.onMove, this.fullBar));
13411344
this.ticks.on('mousedown', angular.bind(this, this.onStart, null, null));
1342-
this.ticks.on('mousedown', angular.bind(this, this.onMove, this.ticks));
1345+
this.ticks.on('mousedown', angular.bind(this, this.onTickClick, this.ticks));
13431346
}
13441347
}
13451348

@@ -1359,7 +1362,7 @@
13591362
this.fullBar.on('touchstart', angular.bind(this, this.onStart, null, null));
13601363
this.fullBar.on('touchstart', angular.bind(this, this.onMove, this.fullBar));
13611364
this.ticks.on('touchstart', angular.bind(this, this.onStart, null, null));
1362-
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
1365+
this.ticks.on('touchstart', angular.bind(this, this.onTickClick, this.ticks));
13631366
}
13641367
}
13651368

@@ -1428,9 +1431,10 @@
14281431
*
14291432
* @param {jqLite} pointer
14301433
* @param {Event} event The event
1434+
* @param {boolean} fromTick if the event occured on a tick or not
14311435
* @returns {undefined}
14321436
*/
1433-
onMove: function(pointer, event) {
1437+
onMove: function(pointer, event, fromTick) {
14341438
var newOffset = this.getEventPosition(event),
14351439
newValue,
14361440
ceilValue = this.options.rightToLeft ? this.minValue : this.maxValue,
@@ -1442,7 +1446,10 @@
14421446
newValue = ceilValue;
14431447
} else {
14441448
newValue = this.offsetToValue(newOffset);
1445-
newValue = this.roundStep(newValue);
1449+
if(fromTick && angular.isNumber(this.options.showTicks))
1450+
newValue = this.roundStep(newValue, this.options.showTicks);
1451+
else
1452+
newValue = this.roundStep(newValue);
14461453
}
14471454
this.positionTrackingHandle(newValue);
14481455
},
@@ -1469,6 +1476,10 @@
14691476
this.callOnEnd();
14701477
},
14711478

1479+
onTickClick: function(pointer, event) {
1480+
this.onMove(pointer, event, true);
1481+
},
1482+
14721483
onPointerFocus: function(pointer, ref) {
14731484
this.tracking = ref;
14741485
pointer.one('blur', angular.bind(this, this.onPointerBlur, pointer));

0 commit comments

Comments
 (0)