Permalink
Browse files

Corrections to uiKeypress

- Forgot to pass $parse to utility fn
- Syntax changed, '13-ctrl-shift 14-ctrl-shift' allows multiple alternatives separated by spaces
  • Loading branch information...
1 parent f5562b3 commit 987492046c088a5495b19d7ae3e1258e72e970c5 Dean Sofer committed Aug 26, 2012
Showing with 36 additions and 67 deletions.
  1. +10 −10 modules/directives/keypress/keypress.js
  2. +26 −57 modules/directives/keypress/test/keypressSpec.js
@@ -1,4 +1,4 @@
-function keypress(mode, scope, elm, attrs){
+function keypress(mode, scope, elm, attrs, $parse){
var params, paramsParsed, expression, keys, combinations = [], keysByCode = {
8: 'backspace',
9: 'tab',
@@ -22,14 +22,14 @@ function keypress(mode, scope, elm, attrs){
angular.forEach(params, function (v, k) {
var combination = {};
combination.expression = $parse(v);
- combination.keys = k;
- keys = {};
- angular.forEach(combination.keys.split(' '), function (value) {
- keys[value] = true;
+ angular.forEach(k.split(' '), function(variation) {
+ combination.keys = {};
+ angular.forEach(variation.split('-'), function (value) {
+ combination.keys[value] = true;
+ });
+ combinations.push(combination);
});
- combination.keys = keys;
- combinations.push(combination);
});
// Check only mathcing of pressed keys one of the conditions
@@ -77,23 +77,23 @@ function keypress(mode, scope, elm, attrs){
angular.module('ui.directives').directive('uiKeydown', ['$parse', function ($parse) {
return {
link: function (scope, elm, attrs) {
- keypress('keydown', scope, elm, attrs);
+ keypress('keydown', scope, elm, attrs, $parse);
}
};
}]);
angular.module('ui.directives').directive('uiKeypress', ['$parse', function ($parse) {
return {
link: function (scope, elm, attrs) {
- keypress('keydown', scope, elm, attrs);
+ keypress('keydown', scope, elm, attrs, $parse);
}
};
}]);
angular.module('ui.directives').directive('uiKeyup', ['$parse', function ($parse) {
return {
link: function (scope, elm, attrs) {
- keypress('keydown', scope, elm, attrs);
+ keypress('keydown', scope, elm, attrs, $parse);
}
};
}]);
@@ -28,71 +28,40 @@ describe('uiKeypress', function () {
};
}));
- describe('keypress with Object syntax', function () {
-
- it('should support single key press', function () {
- createElement({'13': 'event=true'}).trigger(createKeyEvent(13));
- expect($scope.event).toBe(true);
- });
-
- it('should support combined key press', function () {
- createElement({'ctrl-shift-13': 'event=true'}).trigger(createKeyEvent(13, false, true, true));
- expect($scope.event).toBe(true);
- });
-
- it('should support multiple key press definitions', function () {
- var elm = createElement({'13': 'event1=true', 'ctrl-shift-13': 'event2=true'});
-
- elm.trigger(createKeyEvent(13));
- expect($scope.event1).toBe(true);
-
- elm.trigger(createKeyEvent(13, false, true, true));
- expect($scope.event2).toBe(true);
- });
-
- it('should support $event in expressions', function () {
-
- var element = createElement({'esc': 'cb($event)', '13': 'event2=$event'});
-
- element.trigger(createKeyEvent(27));
- expect($scope.event1.keyCode).toBe(27);
-
- element.trigger(createKeyEvent(13));
- expect($scope.event2.keyCode).toBe(13);
- });
+ it('should support single key press', function () {
+ createElement({'13': 'event=true'}).trigger(createKeyEvent(13));
+ expect($scope.event).toBe(true);
});
- describe('keypress with String syntax', function () {
-
- it('should support single key press', function () {
- createElement('event=true on 13').trigger(createKeyEvent(13));
- expect($scope.event).toBe(true);
- });
-
- it('should support combined key press', function () {
- createElement('event=true on ctrl-shift-13').trigger(createKeyEvent(13, false, true, true));
- expect($scope.event).toBe(true);
- });
+ it('should support combined key press', function () {
+ createElement({'ctrl-shift-13': 'event=true'}).trigger(createKeyEvent(13, false, true, true));
+ expect($scope.event).toBe(true);
+ });
+
+ it('should support alternative combinations', function () {
+ $scope.event = 0;
+ createElement({'ctrl-shift-14 ctrl-shift-13': 'event=event+1'}).trigger(createKeyEvent(13, false, true, true)).trigger(createKeyEvent(14, false, true, true));
+ expect($scope.event).toBe(2);
+ });
- it('should support multiple key press definitions', function () {
- var elm = createElement('event1=true on 13 and event2=true on ctrl-shift-13');
+ it('should support multiple key press definitions', function () {
+ var elm = createElement({'13': 'event1=true', 'ctrl-shift-13': 'event2=true'});
- elm.trigger(createKeyEvent(13));
- expect($scope.event1).toBe(true);
+ elm.trigger(createKeyEvent(13));
+ expect($scope.event1).toBe(true);
- elm.trigger(createKeyEvent(13, false, true, true));
- expect($scope.event2).toBe(true);
- });
+ elm.trigger(createKeyEvent(13, false, true, true));
+ expect($scope.event2).toBe(true);
+ });
- it('should support $event in expressions', function () {
+ it('should support $event in expressions', function () {
- var element = createElement('cb($event) on esc and event2=$event on enter');
+ var element = createElement({'esc': 'cb($event)', '13': 'event2=$event'});
- element.trigger(createKeyEvent(27));
- expect($scope.event1.keyCode).toBe(27);
+ element.trigger(createKeyEvent(27));
+ expect($scope.event1.keyCode).toBe(27);
- element.trigger(createKeyEvent(13));
- expect($scope.event2.keyCode).toBe(13);
- });
+ element.trigger(createKeyEvent(13));
+ expect($scope.event2.keyCode).toBe(13);
});
});

0 comments on commit 9874920

Please sign in to comment.