-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-shortcuts.js
196 lines (167 loc) · 4.34 KB
/
angular-shortcuts.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function() {
'use strict';
angular
.module('shortCuts', [])
.provider('$shortCuts', $shortCuts);
function $shortCuts(){
var provider = {
$get : $get,
shortCut : shortCut,
showLog : showLog
}
, _keyCodes = {
'ctrl' : 17,
'a' : 65,
'b' : 66,
'c' : 67,
'd' : 68,
'e' : 69,
'f' : 70,
'g' : 71,
'h' : 72,
'i' : 73,
'j' : 74,
'k' : 75,
'l' : 76,
'm' : 77,
'n' : 78,
'o' : 79,
'p' : 80,
'q' : 81,
'r' : 82,
's' : 83,
't' : 84,
'u' : 85,
'v' : 86,
'w' : 87,
'x' : 88,
'y' : 89,
'z' : 90,
}
, _shortCuts = []
, _showLog = false;
return provider;
function shortCut(keys, config) {
_shortCuts.push(new ShortCut(keys, config));
return this;
};
function showLog(option){
_showLog = option;
};
function ShortCut(keys, config){
var keys = buildKeys(keys);
validateKeysAmount(keys);
validateConfig(config);
var shortCut = {
keyOne : getKeyCode(keys[0])
, keyTwo : getKeyCode(keys[1])
, config : config
};
return shortCut;
};
function getKeyCode(key){
var keyCode = _keyCodes[key];
if (!keyCode)
throw new KeyNotAvailable(key);
return _keyCodes[key]
};
function buildKeys(keys){
return keys.split('+');
};
function log(mgs){
if (_showLog) {
console.warn(mgs);
};
};
function validateConfig(config){
if (!config.actionType){
throw new ActionTypeNotDefined();
};
if (config.actionType === 'EVENT' && !config.eventName){
throw new EventNameNotDefined();
};
if (config.actionType === 'CALLBACK' && !angular.isFunction(config.callBack)){
throw new CallBackNotDefined();
};
};
function validateKeysAmount(keys){
if (keys.length > 2)
throw new InvalidKeysAmountException();
};
function InvalidKeysAmountException() {
return Error('More than two keys defined.');
};
function KeyNotAvailable(key) {
return Error('The key "' + key + '" is not available.');
};
function ActionTypeNotDefined(){
return Error('Action type not defined in config.');
};
function EventNameNotDefined(){
return Error('Event name not defined in config.');
};
function CallBackNotDefined(){
return Error('CallBack not defined or not is a function.')
};
// the $get function retuns the service
$get.$inject = ['$log', '$rootScope'];
function $get($log, $rootScope){
var service = {
getShortCuts : getShortCuts
}
, fisrtKeyActive = false
, firstKeyCode;
document.onkeydown = onKeyDown;
return service;
function onKeyDown(event){
if (event.target.nodeName === 'BODY') {
handle(event);
};
};
function handle(event){
if (isFirstKey(event.keyCode)) {
fisrtKeyActive = true;
firstKeyCode = event.keyCode;
};
if (fisrtKeyActive == true) {
var shortCut = geShortCutByKeys(firstKeyCode, event.keyCode);
if (shortCut) {
executeConfigAction(shortCut.config);
fisrtKeyActive = false;
};
};
};
function executeConfigAction(config){
if (config.actionType === 'EVENT'){
dispachEvent(config.eventName);
return;
};
log('Running callBack {} ' + config.callBack);
config.callBack();
};
function dispachEvent(eventName){
log('Broadcasting event {} "' + eventName + '".')
$rootScope.$broadcast(eventName);
};
function isFirstKey(key){
var result = false;
angular.forEach(_shortCuts, function(shortCut) {
if (shortCut.keyOne === key)
result = true;
});
return result;
};
function geShortCutByKeys(keyOne, keyTwo){
var result;
angular.forEach(_shortCuts, function(shortCut) {
if (shortCut.keyOne === keyOne && shortCut.keyTwo === keyTwo)
result = shortCut;
});
return result;
};
function getShortCuts(){
return angular.copy(_shortCuts);
};
};
};
})();