Skip to content

Commit

Permalink
fix(keyboardSupport): Prevent losing focus when slider is rerendered (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinH authored Sep 22, 2016
1 parent 8af6174 commit 8cbdc46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
30 changes: 25 additions & 5 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v5.5.0 -
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
https://github.com/angular-slider/angularjs-slider -
2016-09-06 */
2016-09-22 */
/*jslint unparam: true */
/*global angular: false, console: false, define, module */
(function(root, factory) {
Expand Down Expand Up @@ -304,6 +304,11 @@
*/
this.cmbLabelShown = false;

/**
* Internal variable to keep track of the focus element
*/
this.currentFocusElement = null;

// Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles
Expand Down Expand Up @@ -413,6 +418,7 @@
this.scope.$on('$destroy', function() {
self.unbindEvents();
angular.element($window).off('resize', calcDimFn);
self.currentFocusElement = null;
});
},

Expand Down Expand Up @@ -577,7 +583,7 @@
else {
this.customTrFn = function(modelValue) {
if (this.options.bindIndexForStepsArray)
return this.getStepValue(modelValue)
return this.getStepValue(modelValue);
return modelValue;
};
}
Expand Down Expand Up @@ -605,6 +611,14 @@
this.manageEventsBindings();
this.setDisabledState();
this.calcViewDimensions();
this.refocusPointerIfNeeded();
},

refocusPointerIfNeeded: function() {
if (this.currentFocusElement) {
this.onPointerFocus(this.currentFocusElement.pointer, this.currentFocusElement.ref);
this.focusElement(this.currentFocusElement.pointer)
}
},

/**
Expand Down Expand Up @@ -1070,7 +1084,7 @@
};
}

if(this.options.autoHideLimitLabels){
if (this.options.autoHideLimitLabels) {
this.shFloorCeil();
}
},
Expand All @@ -1092,7 +1106,7 @@
backgroundColor: pointercolor
};
}
if(this.options.autoHideLimitLabels){
if (this.options.autoHideLimitLabels) {
this.shFloorCeil();
}

Expand Down Expand Up @@ -1695,6 +1709,11 @@
pointer.on('keyup', angular.bind(this, this.onKeyUp));
this.firstKeyDown = true;
pointer.addClass('rz-active');

this.currentFocusElement = {
pointer: pointer,
ref: ref
};
},

onKeyUp: function() {
Expand All @@ -1707,6 +1726,7 @@
pointer.off('keyup');
this.tracking = '';
pointer.removeClass('rz-active');
this.currentFocusElement = null
},

/**
Expand Down Expand Up @@ -1925,7 +1945,7 @@
newMinValue = this.options.minLimit;
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;
newMinValue = newMaxValue - this.dragging.difference;
}
Expand Down
4 changes: 2 additions & 2 deletions dist/rzslider.min.js

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@
*/
this.cmbLabelShown = false;

/**
* Internal variable to keep track of the focus element
*/
this.currentFocusElement = null;

// Slider DOM elements wrapped in jqLite
this.fullBar = null; // The whole slider bar
this.selBar = null; // Highlight between two handles
Expand Down Expand Up @@ -417,6 +422,7 @@
this.scope.$on('$destroy', function() {
self.unbindEvents();
angular.element($window).off('resize', calcDimFn);
self.currentFocusElement = null;
});
},

Expand Down Expand Up @@ -581,7 +587,7 @@
else {
this.customTrFn = function(modelValue) {
if (this.options.bindIndexForStepsArray)
return this.getStepValue(modelValue)
return this.getStepValue(modelValue);
return modelValue;
};
}
Expand Down Expand Up @@ -609,6 +615,14 @@
this.manageEventsBindings();
this.setDisabledState();
this.calcViewDimensions();
this.refocusPointerIfNeeded();
},

refocusPointerIfNeeded: function() {
if (this.currentFocusElement) {
this.onPointerFocus(this.currentFocusElement.pointer, this.currentFocusElement.ref);
this.focusElement(this.currentFocusElement.pointer)
}
},

/**
Expand Down Expand Up @@ -1074,7 +1088,7 @@
};
}

if(this.options.autoHideLimitLabels){
if (this.options.autoHideLimitLabels) {
this.shFloorCeil();
}
},
Expand All @@ -1096,7 +1110,7 @@
backgroundColor: pointercolor
};
}
if(this.options.autoHideLimitLabels){
if (this.options.autoHideLimitLabels) {
this.shFloorCeil();
}

Expand Down Expand Up @@ -1699,6 +1713,11 @@
pointer.on('keyup', angular.bind(this, this.onKeyUp));
this.firstKeyDown = true;
pointer.addClass('rz-active');

this.currentFocusElement = {
pointer: pointer,
ref: ref
};
},

onKeyUp: function() {
Expand All @@ -1711,6 +1730,7 @@
pointer.off('keyup');
this.tracking = '';
pointer.removeClass('rz-active');
this.currentFocusElement = null
},

/**
Expand Down Expand Up @@ -1929,7 +1949,7 @@
newMinValue = this.options.minLimit;
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;
newMinValue = newMaxValue - this.dragging.difference;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/specs/keyboard-controls/specific-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@
helper.pressKeydown(helper.slider.minH, 'RIGHT');
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() {
Expand Down

0 comments on commit 8cbdc46

Please sign in to comment.