-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (183 loc) · 4.17 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
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
/*
* delegate-manager
* Delegate manager
*
* @copyright 2013 Enrico Marino and Federico Spini
* @license MIT
*/
/*
* Expose `Manager`
*/
module.exports = DelegateManager;
/*
* Module dependencies
*/
var delegate = require('delegate');
/*
* Utilities
*/
var object = {};
var toString = object.toString;
/*
* DelegateManager
* Create an event manager.
*
* @param {Element} `target` object which events will be bound to
* @param {Object} `obj` which will receive method calls
* @return {Manager} the event manager
*/
function DelegateManager(target, obj) {
if (!(this instanceof DelegateManager)) {
return new DelegateManager(target, obj);
}
this.target = target;
this.obj = obj;
this._bindings = {};
}
/**
* bind
* Bind to `event` with optional `method` name.
* When `method` is undefined it becomes `event`
* with the "on" prefix.
*
* @example
* events.bind('login') // implies "onlogin"
* events.bind('login', 'onLogin')
*
* @param {String} event `event` name
* @param {String} [method] `method` name
* @return {DelegateManager} the event manager, for chaining
* @api public
*/
DelegateManager.prototype.bind = function(str, method) {
if (toString.call(str) === '[object Object]') {
return this.bind_all(str);
}
var target = this.target;
var obj = this.obj;
var bindings = this._bindings;
var event = parse(str);
var name = event.name;
var selector = event.selector;
var method = method || 'on' + name;
var fn = obj[method].bind(obj);
var callback;
if (selector !== '') {
callback = delegate.bind(target, selector, name, fn, false);
} else {
target.addEventListener(name, fn, false);
callback = fn;
}
bindings[name] = bindings[name] || {};
bindings[name][method] = callback;
return this;
};
/**
* bind_all
* Bind to `event` with optional `method` name
* for each pair `event`/`method` in `obj`
* When `method` is undefined it becomes `event`
* with the "on" prefix.
*
* @example
* events.bind('login') // implies "onlogin"
* events.bind('login', 'onLogin')
*
* @param {Object} obj pairs `event`/`method` to bind
* @return {DelegateManager} the event manager, for chaining
* @api public
*/
DelegateManager.prototype.bind_all = function(obj) {
var event;
for (event in obj) {
this.bind(event, obj[event]);
}
return this;
};
/**
* unbind
* Unbind a single binding, all bindings for `event`,
* or all bindings within the manager.
*
* @example
* events.unbind('login', 'onLogin')
* events.unbind('login')
* events.unbind()
*
* @param {String} [event]
* @param {String} [method]
* @return {DelegateManager} the event manager, for chaining
* @api public
*/
DelegateManager.prototype.unbind = function(str, method) {
if (0 == arguments.length) {
return this.unbind_all();
}
if (1 == arguments.length) {
return this.unbind_all_of(event);
}
var target = this.target;
var bindings = this._bindings;
var event = parse(str);
var name = event.name;
var selector = event.selector;
var method = method || 'on' + name;
var functions = bindings[name];
if (!functions) {
return this;
}
var fn = functions[method];
if (!fn) {
return this;
}
delegate.unbind(target, name, fn, false);
delete bindings[name][method];
return this;
};
/**
* unbind_all
* Unbind all events.
*
* @api private
*/
DelegateManager.prototype.unbind_all = function() {
var bindings = this._bindings;
var event;
for (event in bindings) {
this.unbind_all_of(event);
}
return this;
};
/**
* unbind_all_of
* Unbind all events for `event`.
*
* @param {String} event
* @api private
*/
DelegateManager.prototype.unbind_all_of = function(event) {
var bindings = this._bindings[event];
var method;
if (!bindings) {
return this;
}
for (method in bindings) {
this.unbind(event, method);
}
delete this._bindings[event];
return this;
};
/*
* parse
* Parse event / selector string.
*
* @param {String} string
* @return {Object}
* @api private
*/
function parse(str) {
var parts = str.split(' ');
var event = parts.shift();
var selector = parts.join(' ');
return { name: event, selector: selector };
}