Skip to content

Commit 8a03de4

Browse files
committed
Handle exact value on tick click
1 parent ab41d83 commit 8a03de4

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

demo/demo.js

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

229+
//Slider with ticks at intermediate positions
230+
$scope.slider_ticks_at = {
231+
value: 500,
232+
options: {
233+
ceil: 1000,
234+
floor: 0,
235+
showTicks: 100
236+
}
237+
};
238+
229239
//Slider with draggable range
230240
$scope.slider_draggable_range = {
231241
minValue: 1,

demo/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ <h2>Range slider with ticks and values</h2>
173173
></rzslider>
174174
</article>
175175

176+
<article>
177+
<h2>Slider with ticks at intermediate positions</h2>
178+
<rzslider
179+
rz-slider-model="slider_ticks_at.value"
180+
rz-slider-options="slider_ticks_at.options"
181+
></rzslider>
182+
</article>
183+
176184
<article>
177185
<h2>Slider with draggable range</h2>
178186
<rzslider

dist/rzslider.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.8.0 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-02-13 */
4+
2016-03-11 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -1062,6 +1062,7 @@
10621062
/**
10631063
* Return the translated value if a translate function is provided else the original value
10641064
* @param value
1065+
* @param which if it's min or max handle
10651066
* @returns {*}
10661067
*/
10671068
getDisplayValue: function(value, which) {
@@ -1072,11 +1073,13 @@
10721073
* Round value to step and precision based on minValue
10731074
*
10741075
* @param {number} value
1076+
* @param {number} customStep a custom step to override the defined step
10751077
* @returns {number}
10761078
*/
1077-
roundStep: function(value) {
1078-
var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
1079-
steppedDifference = Math.round(+steppedDifference) * this.step;
1079+
roundStep: function(value, customStep) {
1080+
var step = customStep ? customStep : this.step,
1081+
steppedDifference = parseFloat((value - this.minValue) / step).toPrecision(12);
1082+
steppedDifference = Math.round(+steppedDifference) * step;
10801083
var newValue = (this.minValue + (+steppedDifference)).toFixed(this.precision);
10811084
return +newValue;
10821085
},
@@ -1309,7 +1312,7 @@
13091312
this.fullBar.on('mousedown', angular.bind(this, this.onStart, null, null));
13101313
this.fullBar.on('mousedown', angular.bind(this, this.onMove, this.fullBar));
13111314
this.ticks.on('mousedown', angular.bind(this, this.onStart, null, null));
1312-
this.ticks.on('mousedown', angular.bind(this, this.onMove, this.ticks));
1315+
this.ticks.on('mousedown', angular.bind(this, this.onTickClick, this.ticks));
13131316
}
13141317
}
13151318

@@ -1329,7 +1332,7 @@
13291332
this.fullBar.on('touchstart', angular.bind(this, this.onStart, null, null));
13301333
this.fullBar.on('touchstart', angular.bind(this, this.onMove, this.fullBar));
13311334
this.ticks.on('touchstart', angular.bind(this, this.onStart, null, null));
1332-
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
1335+
this.ticks.on('touchstart', angular.bind(this, this.onTickClick, this.ticks));
13331336
}
13341337
}
13351338

@@ -1398,9 +1401,10 @@
13981401
*
13991402
* @param {jqLite} pointer
14001403
* @param {Event} event The event
1404+
* @param {boolean} fromTick if the event occured on a tick or not
14011405
* @returns {undefined}
14021406
*/
1403-
onMove: function(pointer, event) {
1407+
onMove: function(pointer, event, fromTick) {
14041408
var newOffset = this.getEventPosition(event),
14051409
newValue;
14061410

@@ -1410,7 +1414,10 @@
14101414
newValue = this.maxValue;
14111415
} else {
14121416
newValue = this.offsetToValue(newOffset);
1413-
newValue = this.roundStep(newValue);
1417+
if(fromTick && angular.isNumber(this.options.showTicks))
1418+
newValue = this.roundStep(newValue, this.options.showTicks);
1419+
else
1420+
newValue = this.roundStep(newValue);
14141421
}
14151422
this.positionTrackingHandle(newValue);
14161423
},
@@ -1437,6 +1444,10 @@
14371444
this.callOnEnd();
14381445
},
14391446

1447+
onTickClick: function(pointer, event) {
1448+
this.onMove(pointer, event, true);
1449+
},
1450+
14401451
onPointerFocus: function(pointer, ref) {
14411452
this.tracking = ref;
14421453
pointer.one('blur', angular.bind(this, this.onPointerBlur, pointer));

dist/rzslider.min.js

Lines changed: 2 additions & 2 deletions
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
@@ -1066,6 +1066,7 @@
10661066
/**
10671067
* Return the translated value if a translate function is provided else the original value
10681068
* @param value
1069+
* @param which if it's min or max handle
10691070
* @returns {*}
10701071
*/
10711072
getDisplayValue: function(value, which) {
@@ -1076,11 +1077,13 @@
10761077
* Round value to step and precision based on minValue
10771078
*
10781079
* @param {number} value
1080+
* @param {number} customStep a custom step to override the defined step
10791081
* @returns {number}
10801082
*/
1081-
roundStep: function(value) {
1082-
var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
1083-
steppedDifference = Math.round(+steppedDifference) * this.step;
1083+
roundStep: function(value, customStep) {
1084+
var step = customStep ? customStep : this.step,
1085+
steppedDifference = parseFloat((value - this.minValue) / step).toPrecision(12);
1086+
steppedDifference = Math.round(+steppedDifference) * step;
10841087
var newValue = (this.minValue + (+steppedDifference)).toFixed(this.precision);
10851088
return +newValue;
10861089
},
@@ -1313,7 +1316,7 @@
13131316
this.fullBar.on('mousedown', angular.bind(this, this.onStart, null, null));
13141317
this.fullBar.on('mousedown', angular.bind(this, this.onMove, this.fullBar));
13151318
this.ticks.on('mousedown', angular.bind(this, this.onStart, null, null));
1316-
this.ticks.on('mousedown', angular.bind(this, this.onMove, this.ticks));
1319+
this.ticks.on('mousedown', angular.bind(this, this.onTickClick, this.ticks));
13171320
}
13181321
}
13191322

@@ -1333,7 +1336,7 @@
13331336
this.fullBar.on('touchstart', angular.bind(this, this.onStart, null, null));
13341337
this.fullBar.on('touchstart', angular.bind(this, this.onMove, this.fullBar));
13351338
this.ticks.on('touchstart', angular.bind(this, this.onStart, null, null));
1336-
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
1339+
this.ticks.on('touchstart', angular.bind(this, this.onTickClick, this.ticks));
13371340
}
13381341
}
13391342

@@ -1402,9 +1405,10 @@
14021405
*
14031406
* @param {jqLite} pointer
14041407
* @param {Event} event The event
1408+
* @param {boolean} fromTick if the event occured on a tick or not
14051409
* @returns {undefined}
14061410
*/
1407-
onMove: function(pointer, event) {
1411+
onMove: function(pointer, event, fromTick) {
14081412
var newOffset = this.getEventPosition(event),
14091413
newValue;
14101414

@@ -1414,7 +1418,10 @@
14141418
newValue = this.maxValue;
14151419
} else {
14161420
newValue = this.offsetToValue(newOffset);
1417-
newValue = this.roundStep(newValue);
1421+
if(fromTick && angular.isNumber(this.options.showTicks))
1422+
newValue = this.roundStep(newValue, this.options.showTicks);
1423+
else
1424+
newValue = this.roundStep(newValue);
14181425
}
14191426
this.positionTrackingHandle(newValue);
14201427
},
@@ -1441,6 +1448,10 @@
14411448
this.callOnEnd();
14421449
},
14431450

1451+
onTickClick: function(pointer, event) {
1452+
this.onMove(pointer, event, true);
1453+
},
1454+
14441455
onPointerFocus: function(pointer, ref) {
14451456
this.tracking = ref;
14461457
pointer.one('blur', angular.bind(this, this.onPointerBlur, pointer));

0 commit comments

Comments
 (0)