Commit 8cbdc467 authored by Valentin Hervieu's avatar Valentin Hervieu Committed by GitHub

fix(keyboardSupport): Prevent losing focus when slider is rerendered (#415)

Fix #411
parent 8af61742
/*! angularjs-slider - v5.5.0 - /*! angularjs-slider - v5.5.0 -
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> - (c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
https://github.com/angular-slider/angularjs-slider - https://github.com/angular-slider/angularjs-slider -
2016-09-06 */ 2016-09-22 */
/*jslint unparam: true */ /*jslint unparam: true */
/*global angular: false, console: false, define, module */ /*global angular: false, console: false, define, module */
(function(root, factory) { (function(root, factory) {
...@@ -304,6 +304,11 @@ ...@@ -304,6 +304,11 @@
*/ */
this.cmbLabelShown = false; this.cmbLabelShown = false;
/**
* Internal variable to keep track of the focus element
*/
this.currentFocusElement = null;
// Slider DOM elements wrapped in jqLite // Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles this.selBar = null; // Highlight between two handles
...@@ -413,6 +418,7 @@ ...@@ -413,6 +418,7 @@
this.scope.$on('$destroy', function() { this.scope.$on('$destroy', function() {
self.unbindEvents(); self.unbindEvents();
angular.element($window).off('resize', calcDimFn); angular.element($window).off('resize', calcDimFn);
self.currentFocusElement = null;
}); });
}, },
...@@ -577,7 +583,7 @@ ...@@ -577,7 +583,7 @@
else { else {
this.customTrFn = function(modelValue) { this.customTrFn = function(modelValue) {
if (this.options.bindIndexForStepsArray) if (this.options.bindIndexForStepsArray)
return this.getStepValue(modelValue) return this.getStepValue(modelValue);
return modelValue; return modelValue;
}; };
} }
...@@ -605,6 +611,14 @@ ...@@ -605,6 +611,14 @@
this.manageEventsBindings(); this.manageEventsBindings();
this.setDisabledState(); this.setDisabledState();
this.calcViewDimensions(); this.calcViewDimensions();
this.refocusPointerIfNeeded();
},
refocusPointerIfNeeded: function() {
if (this.currentFocusElement) {
this.onPointerFocus(this.currentFocusElement.pointer, this.currentFocusElement.ref);
this.focusElement(this.currentFocusElement.pointer)
}
}, },
/** /**
...@@ -1070,7 +1084,7 @@ ...@@ -1070,7 +1084,7 @@
}; };
} }
if(this.options.autoHideLimitLabels){ if (this.options.autoHideLimitLabels) {
this.shFloorCeil(); this.shFloorCeil();
} }
}, },
...@@ -1092,7 +1106,7 @@ ...@@ -1092,7 +1106,7 @@
backgroundColor: pointercolor backgroundColor: pointercolor
}; };
} }
if(this.options.autoHideLimitLabels){ if (this.options.autoHideLimitLabels) {
this.shFloorCeil(); this.shFloorCeil();
} }
...@@ -1695,6 +1709,11 @@ ...@@ -1695,6 +1709,11 @@
pointer.on('keyup', angular.bind(this, this.onKeyUp)); pointer.on('keyup', angular.bind(this, this.onKeyUp));
this.firstKeyDown = true; this.firstKeyDown = true;
pointer.addClass('rz-active'); pointer.addClass('rz-active');
this.currentFocusElement = {
pointer: pointer,
ref: ref
};
}, },
onKeyUp: function() { onKeyUp: function() {
...@@ -1707,6 +1726,7 @@ ...@@ -1707,6 +1726,7 @@
pointer.off('keyup'); pointer.off('keyup');
this.tracking = ''; this.tracking = '';
pointer.removeClass('rz-active'); pointer.removeClass('rz-active');
this.currentFocusElement = null
}, },
/** /**
...@@ -1925,7 +1945,7 @@ ...@@ -1925,7 +1945,7 @@
newMinValue = this.options.minLimit; newMinValue = this.options.minLimit;
newMaxValue = newMinValue + this.dragging.difference; newMaxValue = newMinValue + this.dragging.difference;
} }
if (this.options.maxLimit != null && newMaxValue > this.options.maxLimit){ if (this.options.maxLimit != null && newMaxValue > this.options.maxLimit) {
newMaxValue = this.options.maxLimit; newMaxValue = this.options.maxLimit;
newMinValue = newMaxValue - this.dragging.difference; newMinValue = newMaxValue - this.dragging.difference;
} }
......
This diff is collapsed.
...@@ -308,6 +308,11 @@ ...@@ -308,6 +308,11 @@
*/ */
this.cmbLabelShown = false; this.cmbLabelShown = false;
/**
* Internal variable to keep track of the focus element
*/
this.currentFocusElement = null;
// Slider DOM elements wrapped in jqLite // Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles this.selBar = null; // Highlight between two handles
...@@ -417,6 +422,7 @@ ...@@ -417,6 +422,7 @@
this.scope.$on('$destroy', function() { this.scope.$on('$destroy', function() {
self.unbindEvents(); self.unbindEvents();
angular.element($window).off('resize', calcDimFn); angular.element($window).off('resize', calcDimFn);
self.currentFocusElement = null;
}); });
}, },
...@@ -581,7 +587,7 @@ ...@@ -581,7 +587,7 @@
else { else {
this.customTrFn = function(modelValue) { this.customTrFn = function(modelValue) {
if (this.options.bindIndexForStepsArray) if (this.options.bindIndexForStepsArray)
return this.getStepValue(modelValue) return this.getStepValue(modelValue);
return modelValue; return modelValue;
}; };
} }
...@@ -609,6 +615,14 @@ ...@@ -609,6 +615,14 @@
this.manageEventsBindings(); this.manageEventsBindings();
this.setDisabledState(); this.setDisabledState();
this.calcViewDimensions(); this.calcViewDimensions();
this.refocusPointerIfNeeded();
},
refocusPointerIfNeeded: function() {
if (this.currentFocusElement) {
this.onPointerFocus(this.currentFocusElement.pointer, this.currentFocusElement.ref);
this.focusElement(this.currentFocusElement.pointer)
}
}, },
/** /**
...@@ -1074,7 +1088,7 @@ ...@@ -1074,7 +1088,7 @@
}; };
} }
if(this.options.autoHideLimitLabels){ if (this.options.autoHideLimitLabels) {
this.shFloorCeil(); this.shFloorCeil();
} }
}, },
...@@ -1096,7 +1110,7 @@ ...@@ -1096,7 +1110,7 @@
backgroundColor: pointercolor backgroundColor: pointercolor
}; };
} }
if(this.options.autoHideLimitLabels){ if (this.options.autoHideLimitLabels) {
this.shFloorCeil(); this.shFloorCeil();
} }
...@@ -1699,6 +1713,11 @@ ...@@ -1699,6 +1713,11 @@
pointer.on('keyup', angular.bind(this, this.onKeyUp)); pointer.on('keyup', angular.bind(this, this.onKeyUp));
this.firstKeyDown = true; this.firstKeyDown = true;
pointer.addClass('rz-active'); pointer.addClass('rz-active');
this.currentFocusElement = {
pointer: pointer,
ref: ref
};
}, },
onKeyUp: function() { onKeyUp: function() {
...@@ -1711,6 +1730,7 @@ ...@@ -1711,6 +1730,7 @@
pointer.off('keyup'); pointer.off('keyup');
this.tracking = ''; this.tracking = '';
pointer.removeClass('rz-active'); pointer.removeClass('rz-active');
this.currentFocusElement = null
}, },
/** /**
...@@ -1929,7 +1949,7 @@ ...@@ -1929,7 +1949,7 @@
newMinValue = this.options.minLimit; newMinValue = this.options.minLimit;
newMaxValue = newMinValue + this.dragging.difference; newMaxValue = newMinValue + this.dragging.difference;
} }
if (this.options.maxLimit != null && newMaxValue > this.options.maxLimit){ if (this.options.maxLimit != null && newMaxValue > this.options.maxLimit) {
newMaxValue = this.options.maxLimit; newMaxValue = this.options.maxLimit;
newMinValue = newMaxValue - this.dragging.difference; newMinValue = newMaxValue - this.dragging.difference;
} }
......
...@@ -241,6 +241,26 @@ ...@@ -241,6 +241,26 @@
helper.pressKeydown(helper.slider.minH, 'RIGHT'); helper.pressKeydown(helper.slider.minH, 'RIGHT');
expect(helper.scope.slider.value).to.equal(90); expect(helper.scope.slider.value).to.equal(90);
}); });
it('should refocus the slider after a reset if needed and still handle keyboard', function() {
var sliderConf = {
value: 90,
options: {
floor: 0,
ceil: 100,
step: 1
}
};
helper.createSlider(sliderConf);
//try to move minH right
helper.slider.minH.triggerHandler('focus');
helper.slider.resetSlider();
helper.pressKeydown(helper.slider.minH, 'RIGHT');
expect(document.activeElement).to.equal(helper.slider.minH[0]);
expect(helper.scope.slider.value).to.equal(91);
});
}); });
describe('Right to left Keyboard controls - specific tests', function() { describe('Right to left Keyboard controls - specific tests', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment