-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathangular-hotkeys.js
More file actions
181 lines (157 loc) · 4.23 KB
/
angular-hotkeys.js
File metadata and controls
181 lines (157 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function() {
'use strict';
var hotKeys = angular.module('drahak.hotkeys', []);
hotKeys.directive('hotkey', ['$parse', '$hotkey', 'HotKeysElement', function($parse, $hotkey, HotKeysElement) {
return {
restrict: 'AE',
link: function(scope, element, attrs) {
var hotkeys = scope.$eval(attrs.hotkey || attrs.bind);
if (angular.isUndefined(hotkeys)) {
var invoker = $parse(attrs.invoke);
hotkeys = {};
hotkeys[attrs.hotkey || attrs.bind] = function(event) {
invoker(scope, { $event: event });
}
}
var isUsedAsAttribute = element[0].nodeName.toLowerCase() !== 'hotkey';
var entityManager = isUsedAsAttribute ?
HotKeysElement(element) :
$hotkey;
angular.forEach(hotkeys, function(handler, hotkey) {
entityManager.bind(hotkey, handler);
});
}
}
}]);
hotKeys.factory('HotKeysElement', ['$window', 'HotKeys', function($window, HotKeys) {
/**
* @params {HTMLElement} element
* @returns {HotKeys}
*/
return function(element) {
var keys = [];
var elem = angular.element(element);
var root = angular.element($window);
var scope = elem.scope();
var hotKeys = HotKeys();
/** @type {HotKeys} */
if (scope) scope.$hotKeys = hotKeys;
root.bind('blur', function() { keys = []; });
elem.bind('keydown', function(e) {
if (keys.indexOf(e.keyCode) === -1) keys.push(e.keyCode);
hotKeys.trigger(keys, [e]);
});
elem.bind('keyup', function(e) {
keys.splice(keys.indexOf(e.keyCode), 1);
});
return hotKeys;
};
}]);
hotKeys.factory('HotKeys', ['ParseKey', '$rootScope', function(ParseKey, $rootScope) {
/**
* @constructor
*/
var HotKeys = function() {
this._hotKeys = {};
};
/**
* Get hot key index
* @param {String|Array.<Number>} hotKeyExpr
* @returns {String}
* @private
*/
HotKeys.prototype._getHotKeyIndex = function(hotKeyExpr) {
var hotKey;
if (angular.isString(hotKeyExpr)) {
hotKey = ParseKey(hotKeyExpr);
} else if (angular.isArray(hotKeyExpr)) {
hotKey = hotKeyExpr;
} else {
throw new Error('HotKey expects hot key to be string expression or key codes array, ' + typeof(hotKeyExpr) + ' given.');
}
return hotKey.sort().join('+');
};
/**
* Register hot key handler
* @param {String|Array.<Number>} hotKey
* @param {Function} callback
* @returns this
*/
HotKeys.prototype.bind = function(hotKey, callback) {
hotKey = this._getHotKeyIndex(hotKey);
if (!this._hotKeys[hotKey]) {
this._hotKeys[hotKey] = [];
}
this._hotKeys[hotKey].push(callback);
return this;
};
/**
* Remove registered hot key handlers
* @param {String|Array.<Number>} hotKey
* @returns this
*/
HotKeys.prototype.unbind = function(hotKey) {
hotKey = this._getHotKeyIndex(hotKey);
this._hotKeys[hotKey] = [];
return this;
};
/**
* Trigger hot key handlers
* @param {String|Array.<Number>} hotKey
* @param {Array} [args]
*/
HotKeys.prototype.trigger = function(hotKey, args) {
args = args || [];
hotKey = this._getHotKeyIndex(hotKey);
angular.forEach(this._hotKeys[hotKey], function(callback) {
callback.apply(callback, args);
});
if (!$rootScope.$$phase) {
$rootScope.$apply();
}
};
return function() {
return new HotKeys();
}
}]);
hotKeys.factory('$hotkey', ['$window', 'HotKeysElement', function($window, HotKeysElement) {
return HotKeysElement($window);
}]);
hotKeys.service('ParseKey', function() {
var lexer = {
'backspace': 8,
'return': 8,
'tab': 9,
'tabulator': 9,
'enter': 13,
'shift': 16,
'ctrl': 17,
'control': 17,
'alt': 18,
'esc': 27,
'escape': 27,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'insert': 45,
'del': 46,
'delete': 46
};
return function(expression) {
var keys = [];
var expressions = expression.split('+');
angular.forEach(expressions, function(expr) {
expr = expr.trim().toLowerCase();
if (lexer[expr]) {
keys.push(lexer[expr]);
} else if (expr.length === 1) {
keys.push(expr.toUpperCase().charCodeAt(0));
} else {
throw new Error('ParseKey expects one character or special expression like "Tab" or "Control", "' + expr + '" given');
}
});
return keys;
};
});
})();