Repository navigation
Expand file tree
/
Copy pathhotkeys.js
More file actions
275 lines (226 loc) · 7.88 KB
/
Copy pathhotkeys.js
File metadata and controls
275 lines (226 loc) · 7.88 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* angular-hotkeys
*
* Automatic keyboard shortcuts for your angular apps
*
* (c) 2014 Wes Cruver
* License: MIT
*/
// TODOS:
// - check to make sure key is not already bound, if it is, unbind it
//
(function() {
'use strict';
angular.module('cfp.hotkeys', []).provider('hotkeys', function() {
function symbolize (combo) {
var map = {
command : '⌘',
shift : '⇧',
left : '←',
right : '→',
up : '↑',
down : '↓',
'return' : '↩',
backspace : '⌫'
};
combo = combo.split('+');
for (var i = 0; i < combo.length; i++) {
// try to resolve command / ctrl based on OS:
if (combo[i] == 'mod') {
if (window.navigator && window.navigator.platform.indexOf('Mac') >=0 ) {
console.log(window.navigator.platform);
combo[i] = 'command';
} else {
combo[i] = 'ctrl';
}
}
combo[i] = map[combo[i]] || combo[i];
}
return combo.join(' + ');
}
function Hotkey (key, description, callback, persistent) {
// TODO: Check that the values are sane because we could
// be trying to instantiate a new Hotkey with outside dev's
// supplied values
this.key = key;
this.description = description;
this.callback = callback;
this.persistent = persistent;
}
// TODO: this gets called a lot. We should cache the result
Hotkey.prototype.format = function() {
// format the hotkey for display:
var sequence = this.key.split(/[\s]/);
for (var i = 0; i < sequence.length; i++) {
sequence[i] = symbolize(sequence[i]);
}
return sequence;
};
this.$get = ['$rootElement', '$rootScope', '$compile', function ($rootElement, $rootScope, $compile) {
var scope = $rootScope.$new();
/**
* Holds an array of Hotkey objects currently bound
* @type {Array}
*/
scope.hotkeys = [];
/**
* Contains the state of the help's visibility
* @type {Boolean}
*/
scope.helpVisible = false;
/**
* Holds the title string for the help menu
* @type {String}
*/
scope.title = 'Keyboard Shortcuts:';
$rootScope.$on('$routeChangeSuccess', function (event, route) {
purgeHotkeys();
if (route.hotkeys) {
angular.forEach(route.hotkeys, function (hotkey) {
// a string was given, which implies this is a function that is to be
// $eval()'d within that controller's scope
// TODO: hotkey here is super confusing. sometimes a function (that gets turned into an array), sometimes a string
if (typeof(hotkey[2]) === 'string' || hotkey[2] instanceof String) {
hotkey[2]= [hotkey[2], route];
}
// todo: perform check to make sure not already defined:
// this came from a route, so it's likely not meant to be persistent:
hotkey[3] = false;
_add.apply(this, hotkey);
});
}
console.log(scope.hotkeys);
});
// TODO: Make this configurable:
var helpMenu = angular.element('<div class="cfp-hotkeys-container" ng-class="{fade: helpVisible}"><div class="cfp-hotkeys">' +
'<h4>{{ title }}</h4>' +
'<table><tbody>' +
'<tr ng-repeat="hotkey in hotkeys | filter:{ description: \'!$$undefined$$\' }">' +
'<td class="cfp-hotkeys-keys">' +
'<span ng-repeat="derp in hotkey.format()" class="cfp-hotkeys-key">{{ derp }}</span>' +
'</td>' +
'<td class="cfp-hotkeys-text">{{ hotkey.description }}</td>' +
'</tr>' +
'</tbody></table>' +
'</div></div>');
// Auto-create a help menu:
// TODO: Make this configurable
_add('?', 'Show this help menu', toggleHelp);
angular.element($rootElement).append($compile(helpMenu)(scope));
/**
* Purges all non-persistent hotkeys (such as those defined in routes)
*
* Without this, the same hotkey would get recreated everytime
* the route is accessed.
*/
function purgeHotkeys() {
// TODO: hotkey is used as an argument everywhere, but the object type
// is always different. perhaps I sohuld create a hotkey object so it
// is consistent all the time.
angular.forEach(scope.hotkeys, function (hotkey) {
if (!hotkey.persistent) {
_del(hotkey);
}
});
}
/**
* Toggles the help menu element's visiblity
*/
function toggleHelp() {
scope.helpVisible = !scope.helpVisible;
}
/**
* Creates a new Hotkey and creates the Mousetrap binding
*
* @param {string} key mousetrap key binding
* @param {string} description description for the help menu
* @param {Function} callback method to call when key is pressed
* @param {boolean} persistent if true, the binding is preserved upon route changes
*/
function _add (key, description, callback, persistent) {
// a config object was passed instead, so unwrap it:
if (key instanceof Object) {
description = key.description;
callback = key.callback;
persistent = key.persistent;
key = key.hotkey;
}
// description is optional:
if (description instanceof Function) {
callback = description;
description = '$$undefined$$';
}
// any items added through the public API are for controllers
// that persist through navigation, and thus undefined should mean
// true in this case.
if (persistent === undefined) {
persistent = true;
}
Mousetrap.bind(key, wrapApply(callback));
scope.hotkeys.push(new Hotkey(key, description, callback, persistent));
}
/**
* delete and unbind a Hotkey
*
* @param {mixed} hotkey Either the bound key or an instance of Hotkey
* @return {boolean} true if successful
*/
function _del (hotkey) {
var key = (hotkey instanceof Hotkey) ? hotkey.key : hotkey;
Mousetrap.unbind(key);
for (var i = 0; i < scope.hotkeys.length; i++) {
if (scope.hotkeys[i].key === key) {
scope.hotkeys.splice(i, 1);
}
}
}
/**
* Get a Hotkey object by key binding
*
* @param {[string]} key they key the Hotkey is bound to
* @return {Hotkey} The Hotkey object
*/
function _get (key) {
for (var i = 0; i < scope.hotkeys.length; i++) {
if (scope.hotkeys[i].key === key) {
return scope.hotkeys[i];
}
}
return false;
}
/**
* All callbacks sent to Mousetrap are wrapped using this function
* so that we can force a $scope.$apply()
*
* @param {Function} callback [description]
* @return {[type]} [description]
*/
function wrapApply (callback) {
// return mousetrap a function to call
return function (event) {
// if this is an array, it means we provided a route object
// because the scope wasn't available yet, so rewrap the callback
// now that the scope is available:
if (callback instanceof Array) {
var funcString = callback[0];
var route = callback[1];
callback = function (event) {
route.scope.$eval(funcString);
};
}
// this takes place outside angular, so we'll have to call
// $apply() to make sure angular's digest happens
$rootScope.$apply(function() {
// call the original hotkey callback with the keyboard event
callback(event);
});
};
}
return {
add: _add,
del: _del,
get: _get
};
}];
});
})();