Skip to content

Commit 1d2354e

Browse files
author
Valentin Hervieu
committed
Start to implement the keyboard support
1 parent 06c0822 commit 1d2354e

File tree

4 files changed

+113
-17
lines changed

4 files changed

+113
-17
lines changed

dist/rzslider.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,16 +1014,16 @@
10141014
* @returns {number}
10151015
*/
10161016
valueToOffset: function(val) {
1017-
return (this.sanitizeOffsetValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
1017+
return (this.sanitizeValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
10181018
},
10191019

10201020
/**
1021-
* Ensure that the position rendered is within the slider bounds, even if the value is not
1021+
* Returns a value that is within slider range
10221022
*
10231023
* @param {number} val
10241024
* @returns {number}
10251025
*/
1026-
sanitizeOffsetValue: function(val) {
1026+
sanitizeValue: function(val) {
10271027
return Math.min(Math.max(val, this.minValue), this.maxValue);
10281028
},
10291029

@@ -1126,6 +1126,11 @@
11261126
this.selBar.on('touchstart', angular.bind(this, barMove, this.selBar));
11271127
this.ticks.on('touchstart', angular.bind(this, this.onStart, null, null));
11281128
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
1129+
1130+
this.minH.on('focus', angular.bind(this, this.onPointerFocus, this.minH, 'rzSliderModel'))
1131+
if (this.range) {
1132+
this.maxH.on('focus', angular.bind(this, this.onPointerFocus, this.maxH, 'rzSliderHigh'));
1133+
}
11291134
},
11301135

11311136
/**
@@ -1156,10 +1161,6 @@
11561161
event.stopPropagation();
11571162
event.preventDefault();
11581163

1159-
if (this.tracking !== '') {
1160-
return;
1161-
}
1162-
11631164
// We have to do this in case the HTML where the sliders are on
11641165
// have been animated into view.
11651166
this.calcViewDimensions();
@@ -1210,6 +1211,49 @@
12101211
this.positionTrackingHandle(newValue, newOffset);
12111212
},
12121213

1214+
onPointerFocus: function(pointer, ref, event) {
1215+
this.tracking = ref;
1216+
pointer.one('blur', angular.bind(this, this.onPointerBlur, pointer));
1217+
pointer.on('keydown', angular.bind(this, this.onKeyboardEvent));
1218+
pointer.addClass('rz-active');
1219+
},
1220+
1221+
onPointerBlur: function(pointer, event) {
1222+
this.tracking = '';
1223+
pointer.off('keydown');
1224+
pointer.removeClass('rz-active');
1225+
},
1226+
1227+
onKeyboardEvent: function(event) {
1228+
var keyCode = event.keyCode || event.which,
1229+
keys = {
1230+
38: 'UP',
1231+
40: 'DOWN',
1232+
37: 'LEFT',
1233+
39: 'RIGHT'
1234+
},
1235+
actions = {
1236+
UP: 5,
1237+
DOWN: -5,
1238+
LEFT: -1,
1239+
RIGHT: 1
1240+
},
1241+
key = keys[keyCode],
1242+
action = actions[key];
1243+
1244+
if (!key || !this.tracking) return;
1245+
event.preventDefault();
1246+
1247+
var value = this.scope[this.tracking],
1248+
newValue = this.roundStep(this.sanitizeValue(value + action)),
1249+
newOffset = this.valueToOffset(newValue);
1250+
var switched = this.positionTrackingHandle(newValue, newOffset);
1251+
if (switched) {
1252+
var pointer = this.tracking === 'rzSliderModel' ? this.minH : this.maxH;
1253+
pointer[0].focus(); //to focus the correct pointer
1254+
}
1255+
},
1256+
12131257
/**
12141258
* onDragStart event handler
12151259
*
@@ -1302,17 +1346,20 @@
13021346
*/
13031347
positionTrackingHandle: function(newValue, newOffset) {
13041348
var valueChanged = false;
1349+
var switched = false;
13051350

13061351
if (this.range) {
13071352
/* This is to check if we need to switch the min and max handles*/
13081353
if (this.tracking === 'rzSliderModel' && newValue >= this.scope.rzSliderHigh) {
1354+
switched = true;
13091355
this.scope[this.tracking] = this.scope.rzSliderHigh;
13101356
this.updateHandles(this.tracking, this.maxH.rzsp);
13111357
this.tracking = 'rzSliderHigh';
13121358
this.minH.removeClass('rz-active');
13131359
this.maxH.addClass('rz-active');
13141360
valueChanged = true;
13151361
} else if (this.tracking === 'rzSliderHigh' && newValue <= this.scope.rzSliderModel) {
1362+
switched = true;
13161363
this.scope[this.tracking] = this.scope.rzSliderModel;
13171364
this.updateHandles(this.tracking, this.minH.rzsp);
13181365
this.tracking = 'rzSliderModel';
@@ -1332,6 +1379,7 @@
13321379
this.scope.$apply();
13331380
this.callOnChange();
13341381
}
1382+
return switched;
13351383
},
13361384

13371385
/**

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.

0 commit comments

Comments
 (0)