eve 0.5.0 → 0.5.4
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/.npmignore +2 -0
- package/README.md +142 -1
- package/component.json +1 -1
- package/dr.json +10 -0
- package/eve.js +41 -32
- package/package.json +56 -1
- package/template.dot +13 -0
package/.npmignore
ADDED
package/README.md
CHANGED
|
@@ -2,4 +2,145 @@
|
|
|
2
2
|
|
|
3
3
|
Tiny event helping JavaScript library.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## eve(name, scope, varargs)
|
|
6
|
+
Fires event with given `name`, given scope and other parameters.
|
|
7
|
+
|
|
8
|
+
### Parameters
|
|
9
|
+
- _name_ **string**
|
|
10
|
+
name of the _event_, dot (`.`) or slash (`/`) separated
|
|
11
|
+
- _scope_ **object**
|
|
12
|
+
context for the event handlers
|
|
13
|
+
- _varargs_ **...**
|
|
14
|
+
the rest of arguments will be sent to event handlers
|
|
15
|
+
|
|
16
|
+
**Returns:** **object** array of returned values from the listeners. Array has two methods `.firstDefined()` and `.lastDefined()` to get first or last not `undefined` value.
|
|
17
|
+
|
|
18
|
+
## eve.listeners(name)
|
|
19
|
+
Internal method which gives you array of all event handlers that will be triggered by the given `name`.
|
|
20
|
+
|
|
21
|
+
### Parameters
|
|
22
|
+
- _name_ **string**
|
|
23
|
+
name of the event, dot (`.`) or slash (`/`) separated
|
|
24
|
+
|
|
25
|
+
**Returns:** **array** array of event handlers
|
|
26
|
+
|
|
27
|
+
## eve.separator(separator)
|
|
28
|
+
If for some reasons you don’t like default separators (`.` or `/`) you can specify yours
|
|
29
|
+
here. Be aware that if you pass a string longer than one character it will be treated as
|
|
30
|
+
a list of characters.
|
|
31
|
+
|
|
32
|
+
### Parameters
|
|
33
|
+
- _separator_ **string**
|
|
34
|
+
new separator. Empty string resets to default: `.` or `/`.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## eve.on(name, f, name, f)
|
|
38
|
+
Binds given event handler with a given name. You can use wildcards “`*`” for the names:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
eve.on("*.under.*", f);
|
|
42
|
+
eve("mouse.under.floor"); // triggers f
|
|
43
|
+
```
|
|
44
|
+
Use <a href="#eve" class="dr-link">eve</a> to trigger the listener.
|
|
45
|
+
|
|
46
|
+
### Parameters
|
|
47
|
+
- _name_ **string**
|
|
48
|
+
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
|
49
|
+
- _f_ **function**
|
|
50
|
+
event handler function
|
|
51
|
+
- _name_ **array**
|
|
52
|
+
if you don’t want to use separators, you can use array of strings
|
|
53
|
+
- _f_ **function**
|
|
54
|
+
event handler function
|
|
55
|
+
|
|
56
|
+
**Returns:** **function** returned function accepts a single numeric parameter that represents z-index of the handler. It is an optional feature and only used when you need to ensure that some subset of handlers will be invoked in a given order, despite of the order of assignment.
|
|
57
|
+
|
|
58
|
+
### Example:
|
|
59
|
+
```js
|
|
60
|
+
eve.on("mouse", eatIt)(2);
|
|
61
|
+
eve.on("mouse", scream);
|
|
62
|
+
eve.on("mouse", catchIt)(1);
|
|
63
|
+
```
|
|
64
|
+
This will ensure that `catchIt` function will be called before `eatIt`.
|
|
65
|
+
|
|
66
|
+
If you want to put your handler before non-indexed handlers, specify a negative value.
|
|
67
|
+
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”.
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
## eve.f(event, varargs)
|
|
71
|
+
Returns function that will fire given event with optional arguments.
|
|
72
|
+
Arguments that will be passed to the result function will be also
|
|
73
|
+
concated to the list of final arguments.
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
el.onclick = eve.f("click", 1, 2);
|
|
77
|
+
eve.on("click", function (a, b, c) {
|
|
78
|
+
console.log(a, b, c); // 1, 2, [event object]
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
### Parameters
|
|
82
|
+
- _event_ **string**
|
|
83
|
+
event name
|
|
84
|
+
- _varargs_ **…**
|
|
85
|
+
and any other arguments
|
|
86
|
+
|
|
87
|
+
**Returns:** **function** possible event handler function
|
|
88
|
+
|
|
89
|
+
## eve.stop()
|
|
90
|
+
Is used inside an event handler to stop the event, preventing any subsequent listeners from firing.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
## eve.nt([subname])
|
|
94
|
+
Could be used inside event handler to figure out actual name of the event.
|
|
95
|
+
|
|
96
|
+
### Parameters
|
|
97
|
+
- _subname_ **string**
|
|
98
|
+
subname of the event
|
|
99
|
+
|
|
100
|
+
**Returns:** **string** name of the event, if `subname` is not specified
|
|
101
|
+
or
|
|
102
|
+
|
|
103
|
+
**Returns:** **boolean** `true`, if current event’s name contains `subname`
|
|
104
|
+
|
|
105
|
+
## eve.nts()
|
|
106
|
+
Could be used inside event handler to figure out actual name of the event.
|
|
107
|
+
|
|
108
|
+
**Returns:** **array** names of the event
|
|
109
|
+
|
|
110
|
+
## eve.off(name, f)
|
|
111
|
+
Removes given function from the list of event listeners assigned to given name.
|
|
112
|
+
If no arguments specified all the events will be cleared.
|
|
113
|
+
|
|
114
|
+
### Parameters
|
|
115
|
+
- _name_ **string**
|
|
116
|
+
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
|
117
|
+
- _f_ **function**
|
|
118
|
+
event handler function
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## eve.unbind()
|
|
122
|
+
See <a href="#eve.off" class="dr-link">eve.off</a>
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## eve.once(name, f)
|
|
126
|
+
Binds given event handler with a given name to only run once then unbind itself.
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
eve.once("login", f);
|
|
130
|
+
eve("login"); // triggers f
|
|
131
|
+
eve("login"); // no listeners
|
|
132
|
+
```
|
|
133
|
+
Use <a href="#eve" class="dr-link">eve</a> to trigger the listener.
|
|
134
|
+
|
|
135
|
+
### Parameters
|
|
136
|
+
- _name_ **string**
|
|
137
|
+
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
|
138
|
+
- _f_ **function**
|
|
139
|
+
event handler function
|
|
140
|
+
|
|
141
|
+
**Returns:** **function** same return function as <a href="#eve.on" class="dr-link">eve.on</a>
|
|
142
|
+
|
|
143
|
+
## eve.version()
|
|
144
|
+
Current version of the library.
|
|
145
|
+
|
|
146
|
+
|
package/component.json
CHANGED
package/dr.json
ADDED
package/eve.js
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
|
-
// Copyright (c)
|
|
2
|
-
//
|
|
1
|
+
// Copyright (c) 2017 Adobe Systems Incorporated. All rights reserved.
|
|
2
|
+
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
5
5
|
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
6
|
+
//
|
|
7
7
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
8
|
+
//
|
|
9
9
|
// Unless required by applicable law or agreed to in writing, software
|
|
10
10
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
// ┌────────────────────────────────────────────────────────────┐ \\
|
|
15
|
-
// │ Eve 0.5.
|
|
15
|
+
// │ Eve 0.5.4 - JavaScript Events Library │ \\
|
|
16
16
|
// ├────────────────────────────────────────────────────────────┤ \\
|
|
17
17
|
// │ Author Dmitry Baranovskiy (http://dmitry.baranovskiy.com/) │ \\
|
|
18
18
|
// └────────────────────────────────────────────────────────────┘ \\
|
|
19
19
|
|
|
20
20
|
(function (glob) {
|
|
21
|
-
var version = "0.5.
|
|
21
|
+
var version = "0.5.4",
|
|
22
22
|
has = "hasOwnProperty",
|
|
23
23
|
separator = /[\.\/]/,
|
|
24
24
|
comaseparator = /\s*,\s*/,
|
|
25
25
|
wildcard = "*",
|
|
26
|
-
fun = function () {},
|
|
27
26
|
numsort = function (a, b) {
|
|
28
27
|
return a - b;
|
|
29
28
|
},
|
|
@@ -49,15 +48,13 @@
|
|
|
49
48
|
Str = String,
|
|
50
49
|
isArray = Array.isArray || function (ar) {
|
|
51
50
|
return ar instanceof Array || objtos.call(ar) == "[object Array]";
|
|
52
|
-
}
|
|
51
|
+
},
|
|
53
52
|
/*\
|
|
54
53
|
* eve
|
|
55
54
|
[ method ]
|
|
56
55
|
|
|
57
56
|
* Fires event with given `name`, given scope and other parameters.
|
|
58
57
|
|
|
59
|
-
> Arguments
|
|
60
|
-
|
|
61
58
|
- name (string) name of the *event*, dot (`.`) or slash (`/`) separated
|
|
62
59
|
- scope (object) context for the event handlers
|
|
63
60
|
- varargs (...) the rest of arguments will be sent to event handlers
|
|
@@ -65,18 +62,15 @@
|
|
|
65
62
|
= (object) array of returned values from the listeners. Array has two methods `.firstDefined()` and `.lastDefined()` to get first or last not `undefined` value.
|
|
66
63
|
\*/
|
|
67
64
|
eve = function (name, scope) {
|
|
68
|
-
var
|
|
69
|
-
oldstop = stop,
|
|
65
|
+
var oldstop = stop,
|
|
70
66
|
args = Array.prototype.slice.call(arguments, 2),
|
|
71
67
|
listeners = eve.listeners(name),
|
|
72
68
|
z = 0,
|
|
73
|
-
f = false,
|
|
74
69
|
l,
|
|
75
70
|
indexed = [],
|
|
76
71
|
queue = {},
|
|
77
72
|
out = [],
|
|
78
|
-
ce = current_event
|
|
79
|
-
errors = [];
|
|
73
|
+
ce = current_event;
|
|
80
74
|
out.firstDefined = firstDefined;
|
|
81
75
|
out.lastDefined = lastDefined;
|
|
82
76
|
current_event = name;
|
|
@@ -126,16 +120,14 @@
|
|
|
126
120
|
current_event = ce;
|
|
127
121
|
return out;
|
|
128
122
|
};
|
|
129
|
-
|
|
130
|
-
|
|
123
|
+
// Undocumented. Debug only.
|
|
124
|
+
eve._events = events;
|
|
131
125
|
/*\
|
|
132
126
|
* eve.listeners
|
|
133
127
|
[ method ]
|
|
134
128
|
|
|
135
129
|
* Internal method which gives you array of all event handlers that will be triggered by the given `name`.
|
|
136
130
|
|
|
137
|
-
> Arguments
|
|
138
|
-
|
|
139
131
|
- name (string) name of the event, dot (`.`) or slash (`/`) separated
|
|
140
132
|
|
|
141
133
|
= (array) array of event handlers
|
|
@@ -205,7 +197,7 @@
|
|
|
205
197
|
- name (array) if you don’t want to use separators, you can use array of strings
|
|
206
198
|
- f (function) event handler function
|
|
207
199
|
**
|
|
208
|
-
= (function) returned function accepts a single numeric parameter that represents z-index of the handler. It is an optional feature and only used when you need to ensure that some subset of handlers will be invoked in a given order, despite of the order of assignment.
|
|
200
|
+
= (function) returned function accepts a single numeric parameter that represents z-index of the handler. It is an optional feature and only used when you need to ensure that some subset of handlers will be invoked in a given order, despite of the order of assignment.
|
|
209
201
|
> Example:
|
|
210
202
|
| eve.on("mouse", eatIt)(2);
|
|
211
203
|
| eve.on("mouse", scream);
|
|
@@ -219,7 +211,7 @@
|
|
|
219
211
|
if (typeof f != "function") {
|
|
220
212
|
return function () {};
|
|
221
213
|
}
|
|
222
|
-
var names = isArray(name) ?
|
|
214
|
+
var names = isArray(name) ? isArray(name[0]) ? name : [name] : Str(name).split(comaseparator);
|
|
223
215
|
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
224
216
|
(function (name) {
|
|
225
217
|
var names = isArray(name) ? name : Str(name).split(separator),
|
|
@@ -254,7 +246,6 @@
|
|
|
254
246
|
| eve.on("click", function (a, b, c) {
|
|
255
247
|
| console.log(a, b, c); // 1, 2, [event object]
|
|
256
248
|
| });
|
|
257
|
-
> Arguments
|
|
258
249
|
- event (string) event name
|
|
259
250
|
- varargs (…) and any other arguments
|
|
260
251
|
= (function) possible event handler function
|
|
@@ -280,8 +271,6 @@
|
|
|
280
271
|
**
|
|
281
272
|
* Could be used inside event handler to figure out actual name of the event.
|
|
282
273
|
**
|
|
283
|
-
> Arguments
|
|
284
|
-
**
|
|
285
274
|
- subname (string) #optional subname of the event
|
|
286
275
|
**
|
|
287
276
|
= (string) name of the event, if `subname` is not specified
|
|
@@ -314,8 +303,6 @@
|
|
|
314
303
|
* Removes given function from the list of event listeners assigned to given name.
|
|
315
304
|
* If no arguments specified all the events will be cleared.
|
|
316
305
|
**
|
|
317
|
-
> Arguments
|
|
318
|
-
**
|
|
319
306
|
- name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
|
320
307
|
- f (function) event handler function
|
|
321
308
|
\*/
|
|
@@ -330,7 +317,7 @@
|
|
|
330
317
|
eve._events = events = {n: {}};
|
|
331
318
|
return;
|
|
332
319
|
}
|
|
333
|
-
var names = isArray(name) ?
|
|
320
|
+
var names = isArray(name) ? isArray(name[0]) ? name : [name] : Str(name).split(comaseparator);
|
|
334
321
|
if (names.length > 1) {
|
|
335
322
|
for (var i = 0, ii = names.length; i < ii; i++) {
|
|
336
323
|
eve.off(names[i], f);
|
|
@@ -342,7 +329,8 @@
|
|
|
342
329
|
key,
|
|
343
330
|
splice,
|
|
344
331
|
i, ii, j, jj,
|
|
345
|
-
cur = [events]
|
|
332
|
+
cur = [events],
|
|
333
|
+
inodes = [];
|
|
346
334
|
for (i = 0, ii = names.length; i < ii; i++) {
|
|
347
335
|
for (j = 0; j < cur.length; j += splice.length - 2) {
|
|
348
336
|
splice = [j, 1];
|
|
@@ -350,10 +338,18 @@
|
|
|
350
338
|
if (names[i] != wildcard) {
|
|
351
339
|
if (e[names[i]]) {
|
|
352
340
|
splice.push(e[names[i]]);
|
|
341
|
+
inodes.unshift({
|
|
342
|
+
n: e,
|
|
343
|
+
name: names[i]
|
|
344
|
+
});
|
|
353
345
|
}
|
|
354
346
|
} else {
|
|
355
347
|
for (key in e) if (e[has](key)) {
|
|
356
348
|
splice.push(e[key]);
|
|
349
|
+
inodes.unshift({
|
|
350
|
+
n: e,
|
|
351
|
+
name: key
|
|
352
|
+
});
|
|
357
353
|
}
|
|
358
354
|
}
|
|
359
355
|
cur.splice.apply(cur, splice);
|
|
@@ -387,6 +383,20 @@
|
|
|
387
383
|
e = e.n;
|
|
388
384
|
}
|
|
389
385
|
}
|
|
386
|
+
// prune inner nodes in path
|
|
387
|
+
prune: for (i = 0, ii = inodes.length; i < ii; i++) {
|
|
388
|
+
e = inodes[i];
|
|
389
|
+
for (key in e.n[e.name].f) {
|
|
390
|
+
// not empty (has listeners)
|
|
391
|
+
continue prune;
|
|
392
|
+
}
|
|
393
|
+
for (key in e.n[e.name].n) {
|
|
394
|
+
// not empty (has children)
|
|
395
|
+
continue prune;
|
|
396
|
+
}
|
|
397
|
+
// is empty
|
|
398
|
+
delete e.n[e.name];
|
|
399
|
+
}
|
|
390
400
|
};
|
|
391
401
|
/*\
|
|
392
402
|
* eve.once
|
|
@@ -398,8 +408,6 @@
|
|
|
398
408
|
| eve("login"); // no listeners
|
|
399
409
|
* Use @eve to trigger the listener.
|
|
400
410
|
**
|
|
401
|
-
> Arguments
|
|
402
|
-
**
|
|
403
411
|
- name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
|
404
412
|
- f (function) event handler function
|
|
405
413
|
**
|
|
@@ -422,5 +430,6 @@
|
|
|
422
430
|
eve.toString = function () {
|
|
423
431
|
return "You are running Eve " + version;
|
|
424
432
|
};
|
|
425
|
-
|
|
426
|
-
|
|
433
|
+
glob.eve = eve;
|
|
434
|
+
typeof module != "undefined" && module.exports ? module.exports = eve : typeof define === "function" && define.amd ? define("eve", [], function () { return eve; }) : glob.eve = eve;
|
|
435
|
+
})(typeof window != "undefined" ? window : this);
|
package/package.json
CHANGED
|
@@ -7,12 +7,67 @@
|
|
|
7
7
|
"url" : "http://dmitry.baranovskiy.com"
|
|
8
8
|
},
|
|
9
9
|
"description" : "Simple custom events",
|
|
10
|
-
"
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"version" : "0.5.4",
|
|
11
12
|
|
|
12
13
|
"main" : "./eve.js",
|
|
13
14
|
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"mocha": "*",
|
|
17
|
+
"expect.js": "*",
|
|
18
|
+
"eslint": "*",
|
|
19
|
+
"dr.js": "~0.1.0"
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "eslint eve.js",
|
|
24
|
+
"build": "node node_modules/dr.js/dr dr.json"
|
|
25
|
+
},
|
|
26
|
+
|
|
14
27
|
"repository": {
|
|
15
28
|
"type": "git",
|
|
16
29
|
"url": "git@github.com:adobe-webplatform/eve.git"
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
"eslintConfig": {
|
|
33
|
+
"globals": {
|
|
34
|
+
"window": true,
|
|
35
|
+
"console": true
|
|
36
|
+
},
|
|
37
|
+
"rules": {
|
|
38
|
+
"block-scoped-var": 0,
|
|
39
|
+
"comma-dangle": 0,
|
|
40
|
+
"no-extra-parens": 1,
|
|
41
|
+
"no-shadow": 0,
|
|
42
|
+
"consistent-return": 0,
|
|
43
|
+
"eqeqeq": 0,
|
|
44
|
+
"no-new-wrappers": 0,
|
|
45
|
+
"no-sequences": 1,
|
|
46
|
+
"radix": 2,
|
|
47
|
+
"new-parens": 0,
|
|
48
|
+
"no-underscore-dangle": 0,
|
|
49
|
+
"no-path-concat": 0,
|
|
50
|
+
"strict": 0,
|
|
51
|
+
"camelcase": 0,
|
|
52
|
+
"no-extend-native": 0,
|
|
53
|
+
"no-loop-func": 0,
|
|
54
|
+
"new-cap": 0,
|
|
55
|
+
"no-unused-expressions": 0,
|
|
56
|
+
"no-mixed-requires": 0,
|
|
57
|
+
"indent": ["error", 4, { "SwitchCase": 1, "VariableDeclarator": 1 }],
|
|
58
|
+
"no-multi-spaces": [0, {
|
|
59
|
+
"exceptions": {
|
|
60
|
+
"VariableDeclarator": true
|
|
61
|
+
}
|
|
62
|
+
}],
|
|
63
|
+
"no-trailing-spaces": [2, { "skipBlankLines": false }],
|
|
64
|
+
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
|
|
65
|
+
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
|
|
66
|
+
"quotes": [
|
|
67
|
+
1,
|
|
68
|
+
"double",
|
|
69
|
+
"avoid-escape"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
17
72
|
}
|
|
18
73
|
}
|
package/template.dot
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Eve
|
|
2
|
+
|
|
3
|
+
Tiny event helping JavaScript library.
|
|
4
|
+
{{~it.out :item:index}}
|
|
5
|
+
## {{=item[0].name}}
|
|
6
|
+
{{~item :line:i}}{{ if (i > 0) { }}{{ if (line.text) { }}{{=line.text.join("\n").replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}{{ if (line.attr) { }}### Parameters
|
|
7
|
+
{{~line.attr :attr:j}}- _{{=attr.name}}_ {{~attr.type :type:k}} **{{=type}}**{{~}}
|
|
8
|
+
{{=attr.desc.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}
|
|
9
|
+
{{~}}{{ } }}{{ if (line.html) { }}{{=line.html.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}{{ if (line.head) { }}
|
|
10
|
+
### {{=line.head}}{{ } }}{{ if (line.code) { }}```js
|
|
11
|
+
{{=line.code.join("\n")}}
|
|
12
|
+
```{{ } }}{{ if (line.rtrn) { }}**Returns:** {{~line.rtrn.type :type:k}} **{{=type}}**{{~}} {{=line.rtrn.desc.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}
|
|
13
|
+
{{ } }}{{~}}{{~}}
|