-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhotkeys.js
75 lines (62 loc) · 2 KB
/
hotkeys.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
// Object to save the replaced hotkeys
var comboMap = {};
Hotkeys = function(options) {
this.hotkeys = [];
if (!options || options.autoLoad === undefined) {
this.autoLoad = true;
} else {
this.autoLoad = options.autoLoad;
}
}
_.extend(Hotkeys.prototype, {
add: function(obj) {
var combo = obj.combo;
var description = obj.description;
var evenType = obj.evenType;
var callback = obj.callback;
// add all hotkeys to local collection
this.hotkeys.push(obj);
// Small Error check
if(!_.isArray(combo)){
check(combo, String);
}
if(!_.isString(evenType)){
evenType = '';
}
if (!_.isFunction(callback)) {
throw new Meteor.Error(001, 'Error 001: Callback is not a Function');
}
var comboMapEntry = comboMap[combo];
if (!_.isArray(comboMapEntry)) {
comboMap[combo] = []
}
if (this.autoLoad === true) {
comboMap[combo].push(callback);
Mousetrap.bind(combo, callback, evenType);
}
},
load: function() {
var allHotkeys = this.hotkeys;
_.each(allHotkeys, function(hotkey) {
comboMap[hotkey.combo].push(hotkey.callback);
Mousetrap.bind(hotkey.combo, hotkey.callback, hotkey.evenType);
})
},
unload: function() {
var allHotkeys = this.hotkeys;
_.each(allHotkeys, function(hotkey) {
var comboMapEntry = comboMap[hotkey.combo];
_.each(comboMapEntry, function(func, index){
if(func == hotkey.callback){
comboMap[hotkey.combo].splice(index,1);
}
});
// bind it back to the original one
if(comboMapEntry.length > 0){
Mousetrap.bind(hotkey.combo, comboMap[hotkey.combo][comboMapEntry.length -1 ], hotkey.evenType);
}else{
Mousetrap.unbind(hotkey.combo);
}
})
}
})