eve 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -2
- package/component.json +1 -1
- package/eve.js +75 -43
- package/package.json +1 -1
package/README.md
CHANGED
package/component.json
CHANGED
package/eve.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
var version = "0.4.2",
|
|
22
22
|
has = "hasOwnProperty",
|
|
23
23
|
separator = /[\.\/]/,
|
|
24
|
+
comaseparator = /\s*,\s*/,
|
|
24
25
|
wildcard = "*",
|
|
25
26
|
fun = function () {},
|
|
26
27
|
numsort = function (a, b) {
|
|
@@ -29,6 +30,21 @@
|
|
|
29
30
|
current_event,
|
|
30
31
|
stop,
|
|
31
32
|
events = {n: {}},
|
|
33
|
+
firstDefined = function () {
|
|
34
|
+
for (var i = 0, ii = this.length; i < ii; i++) {
|
|
35
|
+
if (typeof this[i] != "undefined") {
|
|
36
|
+
return this[i];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
lastDefined = function () {
|
|
41
|
+
var i = this.length;
|
|
42
|
+
while (--i) {
|
|
43
|
+
if (typeof this[i] != "undefined") {
|
|
44
|
+
return this[i];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
32
48
|
/*\
|
|
33
49
|
* eve
|
|
34
50
|
[ method ]
|
|
@@ -41,10 +57,10 @@
|
|
|
41
57
|
- scope (object) context for the event handlers
|
|
42
58
|
- varargs (...) the rest of arguments will be sent to event handlers
|
|
43
59
|
|
|
44
|
-
= (object) array of returned values from the listeners
|
|
60
|
+
= (object) array of returned values from the listeners. Array has two methods `.firstDefined()` and `.lastDefined()` to get first or last not `undefined` value.
|
|
45
61
|
\*/
|
|
46
62
|
eve = function (name, scope) {
|
|
47
|
-
|
|
63
|
+
name = String(name);
|
|
48
64
|
var e = events,
|
|
49
65
|
oldstop = stop,
|
|
50
66
|
args = Array.prototype.slice.call(arguments, 2),
|
|
@@ -57,6 +73,8 @@
|
|
|
57
73
|
out = [],
|
|
58
74
|
ce = current_event,
|
|
59
75
|
errors = [];
|
|
76
|
+
out.firstDefined = firstDefined;
|
|
77
|
+
out.lastDefined = lastDefined;
|
|
60
78
|
current_event = name;
|
|
61
79
|
stop = 0;
|
|
62
80
|
for (var i = 0, ii = listeners.length; i < ii; i++) if ("zIndex" in listeners[i]) {
|
|
@@ -102,10 +120,10 @@
|
|
|
102
120
|
}
|
|
103
121
|
stop = oldstop;
|
|
104
122
|
current_event = ce;
|
|
105
|
-
return out
|
|
123
|
+
return out;
|
|
106
124
|
};
|
|
107
|
-
|
|
108
|
-
|
|
125
|
+
// Undocumented. Debug only.
|
|
126
|
+
eve._events = events;
|
|
109
127
|
/*\
|
|
110
128
|
* eve.listeners
|
|
111
129
|
[ method ]
|
|
@@ -169,27 +187,34 @@
|
|
|
169
187
|
| eve.on("mouse", eatIt)(2);
|
|
170
188
|
| eve.on("mouse", scream);
|
|
171
189
|
| eve.on("mouse", catchIt)(1);
|
|
172
|
-
* This will ensure that `catchIt
|
|
173
|
-
|
|
190
|
+
* This will ensure that `catchIt` function will be called before `eatIt`.
|
|
191
|
+
*
|
|
174
192
|
* If you want to put your handler before non-indexed handlers, specify a negative value.
|
|
175
193
|
* Note: I assume most of the time you don’t need to worry about z-index, but it’s nice to have this feature “just in case”.
|
|
176
194
|
\*/
|
|
177
195
|
eve.on = function (name, f) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
var names = name.split(separator),
|
|
183
|
-
e = events;
|
|
184
|
-
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
185
|
-
e = e.n;
|
|
186
|
-
e = e.hasOwnProperty(names[i]) && e[names[i]] || (e[names[i]] = {n: {}});
|
|
196
|
+
name = String(name);
|
|
197
|
+
if (typeof f != "function") {
|
|
198
|
+
return function () {};
|
|
187
199
|
}
|
|
188
|
-
|
|
189
|
-
for (i = 0, ii =
|
|
190
|
-
|
|
200
|
+
var names = name.split(comaseparator);
|
|
201
|
+
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
202
|
+
(function (name) {
|
|
203
|
+
var names = name.split(separator),
|
|
204
|
+
e = events,
|
|
205
|
+
exist;
|
|
206
|
+
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
207
|
+
e = e.n;
|
|
208
|
+
e = e.hasOwnProperty(names[i]) && e[names[i]] || (e[names[i]] = {n: {}});
|
|
209
|
+
}
|
|
210
|
+
e.f = e.f || [];
|
|
211
|
+
for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
|
|
212
|
+
exist = true;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
!exist && e.f.push(f);
|
|
216
|
+
}(names[i]));
|
|
191
217
|
}
|
|
192
|
-
e.f.push(f);
|
|
193
218
|
return function (zIndex) {
|
|
194
219
|
if (+zIndex == +zIndex) {
|
|
195
220
|
f.zIndex = +zIndex;
|
|
@@ -201,23 +226,23 @@
|
|
|
201
226
|
[ method ]
|
|
202
227
|
**
|
|
203
228
|
* Returns function that will fire given event with optional arguments.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
229
|
+
* Arguments that will be passed to the result function will be also
|
|
230
|
+
* concated to the list of final arguments.
|
|
231
|
+
| el.onclick = eve.f("click", 1, 2);
|
|
232
|
+
| eve.on("click", function (a, b, c) {
|
|
233
|
+
| console.log(a, b, c); // 1, 2, [event object]
|
|
234
|
+
| });
|
|
210
235
|
> Arguments
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
236
|
+
- event (string) event name
|
|
237
|
+
- varargs (…) and any other arguments
|
|
238
|
+
= (function) possible event handler function
|
|
214
239
|
\*/
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
240
|
+
eve.f = function (event) {
|
|
241
|
+
var attrs = [].slice.call(arguments, 1);
|
|
242
|
+
return function () {
|
|
243
|
+
eve.apply(null, [event, null].concat(attrs).concat([].slice.call(arguments, 0)));
|
|
244
|
+
};
|
|
245
|
+
};
|
|
221
246
|
/*\
|
|
222
247
|
* eve.stop
|
|
223
248
|
[ method ]
|
|
@@ -264,7 +289,7 @@
|
|
|
264
289
|
[ method ]
|
|
265
290
|
**
|
|
266
291
|
* Removes given function from the list of event listeners assigned to given name.
|
|
267
|
-
|
|
292
|
+
* If no arguments specified all the events will be cleared.
|
|
268
293
|
**
|
|
269
294
|
> Arguments
|
|
270
295
|
**
|
|
@@ -278,12 +303,19 @@
|
|
|
278
303
|
* See @eve.off
|
|
279
304
|
\*/
|
|
280
305
|
eve.off = eve.unbind = function (name, f) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
var names = name.split(
|
|
286
|
-
|
|
306
|
+
if (!name) {
|
|
307
|
+
eve._events = events = {n: {}};
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
var names = name.split(comaseparator);
|
|
311
|
+
if (names.length > 1) {
|
|
312
|
+
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
313
|
+
eve.off(names[i], f);
|
|
314
|
+
}
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
names = name.split(separator);
|
|
318
|
+
var e,
|
|
287
319
|
key,
|
|
288
320
|
splice,
|
|
289
321
|
i, ii, j, jj,
|
|
@@ -367,5 +399,5 @@
|
|
|
367
399
|
eve.toString = function () {
|
|
368
400
|
return "You are running Eve " + version;
|
|
369
401
|
};
|
|
370
|
-
(typeof module != "undefined" && module.exports) ? (module.exports = eve) : (typeof define
|
|
402
|
+
(typeof module != "undefined" && module.exports) ? (module.exports = eve) : (typeof define === "function" && define.amd ? (define("eve", [], function() { return eve; })) : (glob.eve = eve));
|
|
371
403
|
})(this);
|