-
Notifications
You must be signed in to change notification settings - Fork 13
/
wMousetrap.js
47 lines (40 loc) · 1.42 KB
/
wMousetrap.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
/**
* Mousetrap wrapper for AngularJS
* @version v0.0.1 - 2013-12-30
* @link https://github.com/mgonto/mgo-mousetrap
* @author Martin Gontovnikas <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
angular.module('mgo-mousetrap', []).directive('wMousetrap', function () {
return {
restrict: 'A',
controller: ['$scope', '$element', '$attrs',
function ($scope, $element, $attrs) {
var mousetrap;
$scope.$watch($attrs.wMousetrap, function(_mousetrap) {
mousetrap = _mousetrap;
for (var key in mousetrap) {
if (mousetrap.hasOwnProperty(key)) {
Mousetrap.unbind(key);
Mousetrap.bind(key, applyWrapper(mousetrap[key]));
}
}
}, true);
function applyWrapper(func) {
return function(e) {
$scope.$apply(function() {
func(e);
});
};
}
$element.bind('$destroy', function() {
if (!mousetrap) return;
for (var key in mousetrap) {
if (mousetrap.hasOwnProperty(key)) {
Mousetrap.unbind(key);
}
}
});
}]
}
});