This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (75 loc) · 1.53 KB
/
index.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
/**
* dependencies
*/
var dispatcher = require('k');
/**
* Export `Shortcuts`
*/
module.exports = Shortcuts;
/**
* Initialize `Shortcuts`.
*
* @param {Element} el
* @param {Object} obj
* @api public
*/
function Shortcuts(el, obj){
if (!(this instanceof Shortcuts)) return new Shortcuts(el, obj);
this.k = dispatcher(el);
this.bindings = {};
this.obj = obj;
this.el = el;
}
/**
* Bind `keys`, `method`.
*
* @param {String} keys
* @param {String} method
* @return {Shortcuts}
* @api public
*/
Shortcuts.prototype.bind = function(keys, method){
if (2 != arguments.length) throw new Error('expected 2 arguments');
var bindings = this.bindings;
var m = bindings[keys] = bindings[keys] || {};
var callback = this.callback(method);
m[method] = callback;
this.k(keys, callback);
return this;
};
/**
* Unbind `keys`, `method`.
*
* @param {String} keys
* @param {String} method
* @return {Shortcuts}
* @api public
*/
Shortcuts.prototype.unbind = function(keys, method){
var methods = this.bindings[keys];
if (2 == arguments.length) {
this.k.unbind(keys, methods[method]);
return this;
}
if (1 == arguments.length) {
this.bindings[keys] = {};
this.k.unbind(keys);
return this;
}
this.bindings = {};
this.k.unbind();
return this;
};
/**
* Wrap the given `method`.
*
* @param {String} method
* @return {Function}
* @api private
*/
Shortcuts.prototype.callback = function(method){
var obj = this.obj;
return function callback(){
obj[method].apply(obj, arguments);
}
};