Skip to content

Commit 664fd56

Browse files
author
Valentin Hervieu
committed
feat(range): Add a noSwitching value to prevent switching min/max
1 parent b5960c2 commit 664fd56

File tree

6 files changed

+91
-124
lines changed

6 files changed

+91
-124
lines changed

dist/rzslider.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ rzslider .rz-pointer:hover:after {
9898
background-color: #ffffff;
9999
}
100100

101+
rzslider .rz-pointer.rz-active {
102+
z-index: 4;
103+
}
104+
101105
rzslider .rz-pointer.rz-active:after {
102106
background-color: #451aff;
103107
}

dist/rzslider.js

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
keyboardSupport: true,
5353
scale: 1,
5454
enforceRange: false,
55+
noSwitching: false,
5556
onlyBindHandles: false,
5657
onStart: null,
5758
onChange: null,
@@ -1322,21 +1323,14 @@
13221323
newValue;
13231324

13241325
if (newOffset <= 0) {
1325-
if (pointer.rzsp === 0)
1326-
return;
13271326
newValue = this.minValue;
1328-
newOffset = 0;
13291327
} else if (newOffset >= this.maxPos) {
1330-
if (pointer.rzsp === this.maxPos)
1331-
return;
13321328
newValue = this.maxValue;
1333-
newOffset = this.maxPos;
13341329
} else {
13351330
newValue = this.offsetToValue(newOffset);
13361331
newValue = this.roundStep(newValue);
1337-
newOffset = this.valueToOffset(newValue);
13381332
}
1339-
this.positionTrackingHandle(newValue, newOffset);
1333+
this.positionTrackingHandle(newValue);
13401334
},
13411335

13421336
/**
@@ -1402,36 +1396,28 @@
14021396
if (action == null || this.tracking === '') return;
14031397
event.preventDefault();
14041398

1405-
var newValue = this.roundStep(this.sanitizeValue(action)),
1406-
newOffset = this.valueToOffset(newValue);
1399+
var newValue = this.roundStep(this.sanitizeValue(action));
14071400
if (!this.options.draggableRangeOnly) {
1408-
this.positionTrackingHandle(newValue, newOffset);
1401+
this.positionTrackingHandle(newValue);
14091402
} else {
14101403
var difference = this.scope.rzSliderHigh - this.scope.rzSliderModel,
1411-
newMinOffset, newMaxOffset,
14121404
newMinValue, newMaxValue;
14131405
if (this.tracking === 'rzSliderModel') {
14141406
newMinValue = newValue;
1415-
newMinOffset = newOffset;
14161407
newMaxValue = newValue + difference;
14171408
if (newMaxValue > this.maxValue) {
14181409
newMaxValue = this.maxValue;
14191410
newMinValue = newMaxValue - difference;
1420-
newMinOffset = this.valueToOffset(newMinValue);
14211411
}
1422-
newMaxOffset = this.valueToOffset(newMaxValue);
14231412
} else {
14241413
newMaxValue = newValue;
1425-
newMaxOffset = newOffset;
14261414
newMinValue = newValue - difference;
14271415
if (newMinValue < this.minValue) {
14281416
newMinValue = this.minValue;
14291417
newMaxValue = newMinValue + difference;
1430-
newMaxOffset = this.valueToOffset(newMaxValue);
14311418
}
1432-
newMinOffset = this.valueToOffset(newMinValue);
14331419
}
1434-
this.positionTrackingBar(newMinValue, newMaxValue, newMinOffset, newMaxOffset);
1420+
this.positionTrackingBar(newMinValue, newMaxValue);
14351421
}
14361422
},
14371423

@@ -1469,100 +1455,94 @@
14691455
*/
14701456
onDragMove: function(pointer, event) {
14711457
var newOffset = this.getEventPosition(event),
1472-
newMinOffset, newMaxOffset,
14731458
newMinValue, newMaxValue;
14741459

14751460
if (newOffset <= this.dragging.lowLimit) {
14761461
if (this.minH.rzsp === 0)
14771462
return;
14781463
newMinValue = this.minValue;
1479-
newMinOffset = 0;
14801464
newMaxValue = this.minValue + this.dragging.difference;
1481-
newMaxOffset = this.valueToOffset(newMaxValue);
14821465
} else if (newOffset >= this.maxPos - this.dragging.highLimit) {
14831466
if (this.maxH.rzsp === this.maxPos)
14841467
return;
14851468
newMaxValue = this.maxValue;
1486-
newMaxOffset = this.maxPos;
14871469
newMinValue = this.maxValue - this.dragging.difference;
1488-
newMinOffset = this.valueToOffset(newMinValue);
14891470
} else {
14901471
newMinValue = this.offsetToValue(newOffset - this.dragging.lowLimit);
14911472
newMinValue = this.roundStep(newMinValue);
1492-
newMinOffset = this.valueToOffset(newMinValue);
14931473
newMaxValue = newMinValue + this.dragging.difference;
1494-
newMaxOffset = this.valueToOffset(newMaxValue);
14951474
}
14961475

1497-
this.positionTrackingBar(newMinValue, newMaxValue, newMinOffset, newMaxOffset);
1476+
this.positionTrackingBar(newMinValue, newMaxValue);
14981477
},
14991478

15001479
/**
15011480
* Set the new value and offset for the entire bar
15021481
*
15031482
* @param {number} newMinValue the new minimum value
15041483
* @param {number} newMaxValue the new maximum value
1505-
* @param {number} newMinOffset the new minimum offset
1506-
* @param {number} newMaxOffset the new maximum offset
15071484
*/
1508-
positionTrackingBar: function(newMinValue, newMaxValue, newMinOffset, newMaxOffset) {
1485+
positionTrackingBar: function(newMinValue, newMaxValue) {
15091486
this.scope.rzSliderModel = newMinValue;
15101487
this.scope.rzSliderHigh = newMaxValue;
1511-
this.updateHandles('rzSliderModel', newMinOffset);
1512-
this.updateHandles('rzSliderHigh', newMaxOffset);
1488+
this.updateHandles('rzSliderModel', this.valueToOffset(newMinValue));
1489+
this.updateHandles('rzSliderHigh', this.valueToOffset(newMaxValue));
15131490
this.applyModel();
15141491
},
15151492

15161493
/**
15171494
* Set the new value and offset to the current tracking handle
15181495
*
15191496
* @param {number} newValue new model value
1520-
* @param {number} newOffset new offset value
15211497
*/
1522-
positionTrackingHandle: function(newValue, newOffset) {
1498+
positionTrackingHandle: function(newValue) {
15231499
var valueChanged = false;
1524-
var switched = false;
15251500

15261501
if (this.range) {
15271502
newValue = this.applyMinRange(newValue);
1528-
newOffset = this.valueToOffset(newValue);
15291503
/* This is to check if we need to switch the min and max handles */
1530-
if (this.tracking === 'rzSliderModel' && newValue >= this.scope.rzSliderHigh) {
1531-
switched = true;
1532-
this.scope[this.tracking] = this.scope.rzSliderHigh;
1533-
this.updateHandles(this.tracking, this.maxH.rzsp);
1534-
this.updateAriaAttributes();
1535-
this.tracking = 'rzSliderHigh';
1536-
this.minH.removeClass('rz-active');
1537-
this.maxH.addClass('rz-active');
1538-
if (this.options.keyboardSupport)
1539-
this.focusElement(this.maxH);
1504+
if (this.tracking === 'rzSliderModel' && newValue > this.scope.rzSliderHigh) {
1505+
if(this.options.noSwitching && this.scope.rzSliderHigh !== this.minValue) {
1506+
newValue = this.scope.rzSliderHigh;
1507+
}
1508+
else {
1509+
this.scope[this.tracking] = this.scope.rzSliderHigh;
1510+
this.updateHandles(this.tracking, this.maxH.rzsp);
1511+
this.updateAriaAttributes();
1512+
this.tracking = 'rzSliderHigh';
1513+
this.minH.removeClass('rz-active');
1514+
this.maxH.addClass('rz-active');
1515+
if (this.options.keyboardSupport)
1516+
this.focusElement(this.maxH);
1517+
}
15401518
valueChanged = true;
1541-
} else if (this.tracking === 'rzSliderHigh' && newValue <= this.scope.rzSliderModel) {
1542-
switched = true;
1543-
this.scope[this.tracking] = this.scope.rzSliderModel;
1544-
this.updateHandles(this.tracking, this.minH.rzsp);
1545-
this.updateAriaAttributes();
1546-
this.tracking = 'rzSliderModel';
1547-
this.maxH.removeClass('rz-active');
1548-
this.minH.addClass('rz-active');
1549-
if (this.options.keyboardSupport)
1550-
this.focusElement(this.minH);
1519+
} else if (this.tracking === 'rzSliderHigh' && newValue < this.scope.rzSliderModel) {
1520+
if(this.options.noSwitching && this.scope.rzSliderModel !== this.maxValue) {
1521+
newValue = this.scope.rzSliderModel;
1522+
}
1523+
else {
1524+
this.scope[this.tracking] = this.scope.rzSliderModel;
1525+
this.updateHandles(this.tracking, this.minH.rzsp);
1526+
this.updateAriaAttributes();
1527+
this.tracking = 'rzSliderModel';
1528+
this.maxH.removeClass('rz-active');
1529+
this.minH.addClass('rz-active');
1530+
if (this.options.keyboardSupport)
1531+
this.focusElement(this.minH);
1532+
}
15511533
valueChanged = true;
15521534
}
15531535
}
15541536

15551537
if (this.scope[this.tracking] !== newValue) {
15561538
this.scope[this.tracking] = newValue;
1557-
this.updateHandles(this.tracking, newOffset);
1539+
this.updateHandles(this.tracking, this.valueToOffset(newValue));
15581540
this.updateAriaAttributes();
15591541
valueChanged = true;
15601542
}
15611543

1562-
if (valueChanged) {
1544+
if (valueChanged)
15631545
this.applyModel();
1564-
}
1565-
return switched;
15661546
},
15671547

15681548
applyMinRange: function(newValue) {

dist/rzslider.min.css

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

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.

0 commit comments

Comments
 (0)