init
This commit is contained in:
commit
6b2d64855d
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
node_modules
|
202
LICENSE
Normal file
202
LICENSE
Normal file
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2013 Adobe Systems Incorporated
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
146
README.md
Normal file
146
README.md
Normal file
@ -0,0 +1,146 @@
|
||||
# Eve
|
||||
|
||||
Tiny event helping JavaScript library.
|
||||
|
||||
## eve(name, scope, varargs)
|
||||
Fires event with given `name`, given scope and other parameters.
|
||||
|
||||
### Parameters
|
||||
- _name_ **string**
|
||||
name of the _event_, dot (`.`) or slash (`/`) separated
|
||||
- _scope_ **object**
|
||||
context for the event handlers
|
||||
- _varargs_ **...**
|
||||
the rest of arguments will be sent to event handlers
|
||||
|
||||
**Returns:** **object** array of returned values from the listeners. Array has two methods `.firstDefined()` and `.lastDefined()` to get first or last not `undefined` value.
|
||||
|
||||
## eve.listeners(name)
|
||||
Internal method which gives you array of all event handlers that will be triggered by the given `name`.
|
||||
|
||||
### Parameters
|
||||
- _name_ **string**
|
||||
name of the event, dot (`.`) or slash (`/`) separated
|
||||
|
||||
**Returns:** **array** array of event handlers
|
||||
|
||||
## eve.separator(separator)
|
||||
If for some reasons you don’t like default separators (`.` or `/`) you can specify yours
|
||||
here. Be aware that if you pass a string longer than one character it will be treated as
|
||||
a list of characters.
|
||||
|
||||
### Parameters
|
||||
- _separator_ **string**
|
||||
new separator. Empty string resets to default: `.` or `/`.
|
||||
|
||||
|
||||
## eve.on(name, f, name, f)
|
||||
Binds given event handler with a given name. You can use wildcards “`*`” for the names:
|
||||
|
||||
```js
|
||||
eve.on("*.under.*", f);
|
||||
eve("mouse.under.floor"); // triggers f
|
||||
```
|
||||
Use <a href="#eve" class="dr-link">eve</a> to trigger the listener.
|
||||
|
||||
### Parameters
|
||||
- _name_ **string**
|
||||
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- _f_ **function**
|
||||
event handler function
|
||||
- _name_ **array**
|
||||
if you don’t want to use separators, you can use array of strings
|
||||
- _f_ **function**
|
||||
event handler function
|
||||
|
||||
**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.
|
||||
|
||||
### Example:
|
||||
```js
|
||||
eve.on("mouse", eatIt)(2);
|
||||
eve.on("mouse", scream);
|
||||
eve.on("mouse", catchIt)(1);
|
||||
```
|
||||
This will ensure that `catchIt` function will be called before `eatIt`.
|
||||
|
||||
If you want to put your handler before non-indexed handlers, specify a negative value.
|
||||
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”.
|
||||
|
||||
|
||||
## eve.f(event, varargs)
|
||||
Returns function that will fire given event with optional arguments.
|
||||
Arguments that will be passed to the result function will be also
|
||||
concated to the list of final arguments.
|
||||
|
||||
```js
|
||||
el.onclick = eve.f("click", 1, 2);
|
||||
eve.on("click", function (a, b, c) {
|
||||
console.log(a, b, c); // 1, 2, [event object]
|
||||
});
|
||||
```
|
||||
### Parameters
|
||||
- _event_ **string**
|
||||
event name
|
||||
- _varargs_ **…**
|
||||
and any other arguments
|
||||
|
||||
**Returns:** **function** possible event handler function
|
||||
|
||||
## eve.stop()
|
||||
Is used inside an event handler to stop the event, preventing any subsequent listeners from firing.
|
||||
|
||||
|
||||
## eve.nt([subname])
|
||||
Could be used inside event handler to figure out actual name of the event.
|
||||
|
||||
### Parameters
|
||||
- _subname_ **string**
|
||||
subname of the event
|
||||
|
||||
**Returns:** **string** name of the event, if `subname` is not specified
|
||||
or
|
||||
|
||||
**Returns:** **boolean** `true`, if current event’s name contains `subname`
|
||||
|
||||
## eve.nts()
|
||||
Could be used inside event handler to figure out actual name of the event.
|
||||
|
||||
**Returns:** **array** names of the event
|
||||
|
||||
## eve.off(name, f)
|
||||
Removes given function from the list of event listeners assigned to given name.
|
||||
If no arguments specified all the events will be cleared.
|
||||
|
||||
### Parameters
|
||||
- _name_ **string**
|
||||
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- _f_ **function**
|
||||
event handler function
|
||||
|
||||
|
||||
## eve.unbind()
|
||||
See <a href="#eve.off" class="dr-link">eve.off</a>
|
||||
|
||||
|
||||
## eve.once(name, f)
|
||||
Binds given event handler with a given name to only run once then unbind itself.
|
||||
|
||||
```js
|
||||
eve.once("login", f);
|
||||
eve("login"); // triggers f
|
||||
eve("login"); // no listeners
|
||||
```
|
||||
Use <a href="#eve" class="dr-link">eve</a> to trigger the listener.
|
||||
|
||||
### Parameters
|
||||
- _name_ **string**
|
||||
name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- _f_ **function**
|
||||
event handler function
|
||||
|
||||
**Returns:** **function** same return function as <a href="#eve.on" class="dr-link">eve.on</a>
|
||||
|
||||
## eve.version()
|
||||
Current version of the library.
|
||||
|
||||
|
13
component.json
Normal file
13
component.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "eve",
|
||||
"repo": "adobe-webplatform/eve",
|
||||
"description": "Custom Events",
|
||||
"version": "0.5.4",
|
||||
"keywords": ["events"],
|
||||
"dependencies": {},
|
||||
"development": {},
|
||||
"main": "eve.js",
|
||||
"scripts": [
|
||||
"eve.js"
|
||||
]
|
||||
}
|
10
dr.json
Normal file
10
dr.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"title": "Eve",
|
||||
"output": "README.md",
|
||||
"template": "template.dot",
|
||||
"css": "custom",
|
||||
"files": [{
|
||||
"url": "eve.js",
|
||||
"link": "https://github.com/adobe-webplatform/eve/blob/master/eve.js"
|
||||
}]
|
||||
}
|
66
e.html
Normal file
66
e.html
Normal file
@ -0,0 +1,66 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<meta charset="utf-8">
|
||||
<title>Eve Use Case</title>
|
||||
<script src="eve.js"></script>
|
||||
<script>
|
||||
var log = function (text) {
|
||||
log.log = log.log || [];
|
||||
log.log.push(text);
|
||||
};
|
||||
window.onload = function () {
|
||||
document.getElementById("res").innerHTML = log.log.join("<br>");
|
||||
};
|
||||
</script>
|
||||
<script>
|
||||
var f1, f2, f3, f4;
|
||||
|
||||
// setting up listeners
|
||||
eve.on("hit", f1 = function () {
|
||||
log(" I’m hit!");
|
||||
});
|
||||
eve.on("hit/face", f2 = function () {
|
||||
log(" Oh, my face!");
|
||||
});
|
||||
eve.on("hit/chest", f3 = function () {
|
||||
log(" Oh, my chest!");
|
||||
});
|
||||
eve.on("hit/*/leg", f4 = function () {
|
||||
log(" Ouch!");
|
||||
});
|
||||
eve.once("hit", function () {
|
||||
log(" You scoundrel!");
|
||||
})(-1);
|
||||
|
||||
// fire events
|
||||
log("In your face!");
|
||||
eve("hit/face");
|
||||
// I’m hit!
|
||||
// Oh, my face!
|
||||
log("Take that!");
|
||||
// You can use “.” or “/” as delimiter
|
||||
eve("hit.chest.leg");
|
||||
// I’m hit!
|
||||
// Oh, my chest!
|
||||
// Ouch!
|
||||
|
||||
// Unbinding
|
||||
log("");
|
||||
eve.unbind("hit", f3);
|
||||
log("Take that!");
|
||||
eve("hit.chest.leg");
|
||||
// I’m hit!
|
||||
// Ouch!
|
||||
|
||||
// Unbinding by wildcard
|
||||
log("");
|
||||
eve.unbind("hit/*");
|
||||
log("In your face!");
|
||||
eve("hit.face");
|
||||
// I’m hit!
|
||||
log("Take that!");
|
||||
eve("hit.chest.leg");
|
||||
// I’m hit!
|
||||
</script>
|
||||
<pre id="res"></pre>
|
||||
</html>
|
435
eve.js
Normal file
435
eve.js
Normal file
@ -0,0 +1,435 @@
|
||||
// Copyright (c) 2017 Adobe Systems Incorporated. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ┌────────────────────────────────────────────────────────────┐ \\
|
||||
// │ Eve 0.5.4 - JavaScript Events Library │ \\
|
||||
// ├────────────────────────────────────────────────────────────┤ \\
|
||||
// │ Author Dmitry Baranovskiy (http://dmitry.baranovskiy.com/) │ \\
|
||||
// └────────────────────────────────────────────────────────────┘ \\
|
||||
|
||||
(function (glob) {
|
||||
var version = "0.5.4",
|
||||
has = "hasOwnProperty",
|
||||
separator = /[\.\/]/,
|
||||
comaseparator = /\s*,\s*/,
|
||||
wildcard = "*",
|
||||
numsort = function (a, b) {
|
||||
return a - b;
|
||||
},
|
||||
current_event,
|
||||
stop,
|
||||
events = {n: {}},
|
||||
firstDefined = function () {
|
||||
for (var i = 0, ii = this.length; i < ii; i++) {
|
||||
if (typeof this[i] != "undefined") {
|
||||
return this[i];
|
||||
}
|
||||
}
|
||||
},
|
||||
lastDefined = function () {
|
||||
var i = this.length;
|
||||
while (--i) {
|
||||
if (typeof this[i] != "undefined") {
|
||||
return this[i];
|
||||
}
|
||||
}
|
||||
},
|
||||
objtos = Object.prototype.toString,
|
||||
Str = String,
|
||||
isArray = Array.isArray || function (ar) {
|
||||
return ar instanceof Array || objtos.call(ar) == "[object Array]";
|
||||
},
|
||||
/*\
|
||||
* eve
|
||||
[ method ]
|
||||
|
||||
* Fires event with given `name`, given scope and other parameters.
|
||||
|
||||
- name (string) name of the *event*, dot (`.`) or slash (`/`) separated
|
||||
- scope (object) context for the event handlers
|
||||
- varargs (...) the rest of arguments will be sent to event handlers
|
||||
|
||||
= (object) array of returned values from the listeners. Array has two methods `.firstDefined()` and `.lastDefined()` to get first or last not `undefined` value.
|
||||
\*/
|
||||
eve = function (name, scope) {
|
||||
var oldstop = stop,
|
||||
args = Array.prototype.slice.call(arguments, 2),
|
||||
listeners = eve.listeners(name),
|
||||
z = 0,
|
||||
l,
|
||||
indexed = [],
|
||||
queue = {},
|
||||
out = [],
|
||||
ce = current_event;
|
||||
out.firstDefined = firstDefined;
|
||||
out.lastDefined = lastDefined;
|
||||
current_event = name;
|
||||
stop = 0;
|
||||
for (var i = 0, ii = listeners.length; i < ii; i++) if ("zIndex" in listeners[i]) {
|
||||
indexed.push(listeners[i].zIndex);
|
||||
if (listeners[i].zIndex < 0) {
|
||||
queue[listeners[i].zIndex] = listeners[i];
|
||||
}
|
||||
}
|
||||
indexed.sort(numsort);
|
||||
while (indexed[z] < 0) {
|
||||
l = queue[indexed[z++]];
|
||||
out.push(l.apply(scope, args));
|
||||
if (stop) {
|
||||
stop = oldstop;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < ii; i++) {
|
||||
l = listeners[i];
|
||||
if ("zIndex" in l) {
|
||||
if (l.zIndex == indexed[z]) {
|
||||
out.push(l.apply(scope, args));
|
||||
if (stop) {
|
||||
break;
|
||||
}
|
||||
do {
|
||||
z++;
|
||||
l = queue[indexed[z]];
|
||||
l && out.push(l.apply(scope, args));
|
||||
if (stop) {
|
||||
break;
|
||||
}
|
||||
} while (l)
|
||||
} else {
|
||||
queue[l.zIndex] = l;
|
||||
}
|
||||
} else {
|
||||
out.push(l.apply(scope, args));
|
||||
if (stop) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
stop = oldstop;
|
||||
current_event = ce;
|
||||
return out;
|
||||
};
|
||||
// Undocumented. Debug only.
|
||||
eve._events = events;
|
||||
/*\
|
||||
* eve.listeners
|
||||
[ method ]
|
||||
|
||||
* Internal method which gives you array of all event handlers that will be triggered by the given `name`.
|
||||
|
||||
- name (string) name of the event, dot (`.`) or slash (`/`) separated
|
||||
|
||||
= (array) array of event handlers
|
||||
\*/
|
||||
eve.listeners = function (name) {
|
||||
var names = isArray(name) ? name : name.split(separator),
|
||||
e = events,
|
||||
item,
|
||||
items,
|
||||
k,
|
||||
i,
|
||||
ii,
|
||||
j,
|
||||
jj,
|
||||
nes,
|
||||
es = [e],
|
||||
out = [];
|
||||
for (i = 0, ii = names.length; i < ii; i++) {
|
||||
nes = [];
|
||||
for (j = 0, jj = es.length; j < jj; j++) {
|
||||
e = es[j].n;
|
||||
items = [e[names[i]], e[wildcard]];
|
||||
k = 2;
|
||||
while (k--) {
|
||||
item = items[k];
|
||||
if (item) {
|
||||
nes.push(item);
|
||||
out = out.concat(item.f || []);
|
||||
}
|
||||
}
|
||||
}
|
||||
es = nes;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
/*\
|
||||
* eve.separator
|
||||
[ method ]
|
||||
|
||||
* If for some reasons you don’t like default separators (`.` or `/`) you can specify yours
|
||||
* here. Be aware that if you pass a string longer than one character it will be treated as
|
||||
* a list of characters.
|
||||
|
||||
- separator (string) new separator. Empty string resets to default: `.` or `/`.
|
||||
\*/
|
||||
eve.separator = function (sep) {
|
||||
if (sep) {
|
||||
sep = Str(sep).replace(/(?=[\.\^\]\[\-])/g, "\\");
|
||||
sep = "[" + sep + "]";
|
||||
separator = new RegExp(sep);
|
||||
} else {
|
||||
separator = /[\.\/]/;
|
||||
}
|
||||
};
|
||||
/*\
|
||||
* eve.on
|
||||
[ method ]
|
||||
**
|
||||
* Binds given event handler with a given name. You can use wildcards “`*`” for the names:
|
||||
| eve.on("*.under.*", f);
|
||||
| eve("mouse.under.floor"); // triggers f
|
||||
* Use @eve to trigger the listener.
|
||||
**
|
||||
- name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- f (function) event handler function
|
||||
**
|
||||
- name (array) if you don’t want to use separators, you can use array of strings
|
||||
- f (function) event handler function
|
||||
**
|
||||
= (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.
|
||||
> Example:
|
||||
| eve.on("mouse", eatIt)(2);
|
||||
| eve.on("mouse", scream);
|
||||
| eve.on("mouse", catchIt)(1);
|
||||
* This will ensure that `catchIt` function will be called before `eatIt`.
|
||||
*
|
||||
* If you want to put your handler before non-indexed handlers, specify a negative value.
|
||||
* 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”.
|
||||
\*/
|
||||
eve.on = function (name, f) {
|
||||
if (typeof f != "function") {
|
||||
return function () {};
|
||||
}
|
||||
var names = isArray(name) ? isArray(name[0]) ? name : [name] : Str(name).split(comaseparator);
|
||||
for (var i = 0, ii = names.length; i < ii; i++) {
|
||||
(function (name) {
|
||||
var names = isArray(name) ? name : Str(name).split(separator),
|
||||
e = events,
|
||||
exist;
|
||||
for (var i = 0, ii = names.length; i < ii; i++) {
|
||||
e = e.n;
|
||||
e = e.hasOwnProperty(names[i]) && e[names[i]] || (e[names[i]] = {n: {}});
|
||||
}
|
||||
e.f = e.f || [];
|
||||
for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
!exist && e.f.push(f);
|
||||
}(names[i]));
|
||||
}
|
||||
return function (zIndex) {
|
||||
if (+zIndex == +zIndex) {
|
||||
f.zIndex = +zIndex;
|
||||
}
|
||||
};
|
||||
};
|
||||
/*\
|
||||
* eve.f
|
||||
[ method ]
|
||||
**
|
||||
* Returns function that will fire given event with optional arguments.
|
||||
* Arguments that will be passed to the result function will be also
|
||||
* concated to the list of final arguments.
|
||||
| el.onclick = eve.f("click", 1, 2);
|
||||
| eve.on("click", function (a, b, c) {
|
||||
| console.log(a, b, c); // 1, 2, [event object]
|
||||
| });
|
||||
- event (string) event name
|
||||
- varargs (…) and any other arguments
|
||||
= (function) possible event handler function
|
||||
\*/
|
||||
eve.f = function (event) {
|
||||
var attrs = [].slice.call(arguments, 1);
|
||||
return function () {
|
||||
eve.apply(null, [event, null].concat(attrs).concat([].slice.call(arguments, 0)));
|
||||
};
|
||||
};
|
||||
/*\
|
||||
* eve.stop
|
||||
[ method ]
|
||||
**
|
||||
* Is used inside an event handler to stop the event, preventing any subsequent listeners from firing.
|
||||
\*/
|
||||
eve.stop = function () {
|
||||
stop = 1;
|
||||
};
|
||||
/*\
|
||||
* eve.nt
|
||||
[ method ]
|
||||
**
|
||||
* Could be used inside event handler to figure out actual name of the event.
|
||||
**
|
||||
- subname (string) #optional subname of the event
|
||||
**
|
||||
= (string) name of the event, if `subname` is not specified
|
||||
* or
|
||||
= (boolean) `true`, if current event’s name contains `subname`
|
||||
\*/
|
||||
eve.nt = function (subname) {
|
||||
var cur = isArray(current_event) ? current_event.join(".") : current_event;
|
||||
if (subname) {
|
||||
return new RegExp("(?:\\.|\\/|^)" + subname + "(?:\\.|\\/|$)").test(cur);
|
||||
}
|
||||
return cur;
|
||||
};
|
||||
/*\
|
||||
* eve.nts
|
||||
[ method ]
|
||||
**
|
||||
* Could be used inside event handler to figure out actual name of the event.
|
||||
**
|
||||
**
|
||||
= (array) names of the event
|
||||
\*/
|
||||
eve.nts = function () {
|
||||
return isArray(current_event) ? current_event : current_event.split(separator);
|
||||
};
|
||||
/*\
|
||||
* eve.off
|
||||
[ method ]
|
||||
**
|
||||
* Removes given function from the list of event listeners assigned to given name.
|
||||
* If no arguments specified all the events will be cleared.
|
||||
**
|
||||
- name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- f (function) event handler function
|
||||
\*/
|
||||
/*\
|
||||
* eve.unbind
|
||||
[ method ]
|
||||
**
|
||||
* See @eve.off
|
||||
\*/
|
||||
eve.off = eve.unbind = function (name, f) {
|
||||
if (!name) {
|
||||
eve._events = events = {n: {}};
|
||||
return;
|
||||
}
|
||||
var names = isArray(name) ? isArray(name[0]) ? name : [name] : Str(name).split(comaseparator);
|
||||
if (names.length > 1) {
|
||||
for (var i = 0, ii = names.length; i < ii; i++) {
|
||||
eve.off(names[i], f);
|
||||
}
|
||||
return;
|
||||
}
|
||||
names = isArray(name) ? name : Str(name).split(separator);
|
||||
var e,
|
||||
key,
|
||||
splice,
|
||||
i, ii, j, jj,
|
||||
cur = [events],
|
||||
inodes = [];
|
||||
for (i = 0, ii = names.length; i < ii; i++) {
|
||||
for (j = 0; j < cur.length; j += splice.length - 2) {
|
||||
splice = [j, 1];
|
||||
e = cur[j].n;
|
||||
if (names[i] != wildcard) {
|
||||
if (e[names[i]]) {
|
||||
splice.push(e[names[i]]);
|
||||
inodes.unshift({
|
||||
n: e,
|
||||
name: names[i]
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for (key in e) if (e[has](key)) {
|
||||
splice.push(e[key]);
|
||||
inodes.unshift({
|
||||
n: e,
|
||||
name: key
|
||||
});
|
||||
}
|
||||
}
|
||||
cur.splice.apply(cur, splice);
|
||||
}
|
||||
}
|
||||
for (i = 0, ii = cur.length; i < ii; i++) {
|
||||
e = cur[i];
|
||||
while (e.n) {
|
||||
if (f) {
|
||||
if (e.f) {
|
||||
for (j = 0, jj = e.f.length; j < jj; j++) if (e.f[j] == f) {
|
||||
e.f.splice(j, 1);
|
||||
break;
|
||||
}
|
||||
!e.f.length && delete e.f;
|
||||
}
|
||||
for (key in e.n) if (e.n[has](key) && e.n[key].f) {
|
||||
var funcs = e.n[key].f;
|
||||
for (j = 0, jj = funcs.length; j < jj; j++) if (funcs[j] == f) {
|
||||
funcs.splice(j, 1);
|
||||
break;
|
||||
}
|
||||
!funcs.length && delete e.n[key].f;
|
||||
}
|
||||
} else {
|
||||
delete e.f;
|
||||
for (key in e.n) if (e.n[has](key) && e.n[key].f) {
|
||||
delete e.n[key].f;
|
||||
}
|
||||
}
|
||||
e = e.n;
|
||||
}
|
||||
}
|
||||
// prune inner nodes in path
|
||||
prune: for (i = 0, ii = inodes.length; i < ii; i++) {
|
||||
e = inodes[i];
|
||||
for (key in e.n[e.name].f) {
|
||||
// not empty (has listeners)
|
||||
continue prune;
|
||||
}
|
||||
for (key in e.n[e.name].n) {
|
||||
// not empty (has children)
|
||||
continue prune;
|
||||
}
|
||||
// is empty
|
||||
delete e.n[e.name];
|
||||
}
|
||||
};
|
||||
/*\
|
||||
* eve.once
|
||||
[ method ]
|
||||
**
|
||||
* Binds given event handler with a given name to only run once then unbind itself.
|
||||
| eve.once("login", f);
|
||||
| eve("login"); // triggers f
|
||||
| eve("login"); // no listeners
|
||||
* Use @eve to trigger the listener.
|
||||
**
|
||||
- name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
|
||||
- f (function) event handler function
|
||||
**
|
||||
= (function) same return function as @eve.on
|
||||
\*/
|
||||
eve.once = function (name, f) {
|
||||
var f2 = function () {
|
||||
eve.off(name, f2);
|
||||
return f.apply(this, arguments);
|
||||
};
|
||||
return eve.on(name, f2);
|
||||
};
|
||||
/*\
|
||||
* eve.version
|
||||
[ property (string) ]
|
||||
**
|
||||
* Current version of the library.
|
||||
\*/
|
||||
eve.version = version;
|
||||
eve.toString = function () {
|
||||
return "You are running Eve " + version;
|
||||
};
|
||||
glob.eve = eve;
|
||||
typeof module != "undefined" && module.exports ? module.exports = eve : typeof define === "function" && define.amd ? define("eve", [], function () { return eve; }) : glob.eve = eve;
|
||||
})(typeof window != "undefined" ? window : this);
|
73
package.json
Normal file
73
package.json
Normal file
@ -0,0 +1,73 @@
|
||||
{
|
||||
"name" : "eve",
|
||||
|
||||
"author" : {
|
||||
"name" : "Dmitry Baranovskiy",
|
||||
"email" : "dmitry@baranovskiy.com",
|
||||
"url" : "http://dmitry.baranovskiy.com"
|
||||
},
|
||||
"description" : "Simple custom events",
|
||||
"license": "Apache-2.0",
|
||||
"version" : "0.5.4",
|
||||
|
||||
"main" : "./eve.js",
|
||||
|
||||
"devDependencies": {
|
||||
"mocha": "*",
|
||||
"expect.js": "*",
|
||||
"eslint": "*",
|
||||
"dr.js": "~0.1.0"
|
||||
},
|
||||
|
||||
"scripts": {
|
||||
"test": "eslint eve.js",
|
||||
"build": "node node_modules/dr.js/dr dr.json"
|
||||
},
|
||||
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:adobe-webplatform/eve.git"
|
||||
},
|
||||
|
||||
"eslintConfig": {
|
||||
"globals": {
|
||||
"window": true,
|
||||
"console": true
|
||||
},
|
||||
"rules": {
|
||||
"block-scoped-var": 0,
|
||||
"comma-dangle": 0,
|
||||
"no-extra-parens": 1,
|
||||
"no-shadow": 0,
|
||||
"consistent-return": 0,
|
||||
"eqeqeq": 0,
|
||||
"no-new-wrappers": 0,
|
||||
"no-sequences": 1,
|
||||
"radix": 2,
|
||||
"new-parens": 0,
|
||||
"no-underscore-dangle": 0,
|
||||
"no-path-concat": 0,
|
||||
"strict": 0,
|
||||
"camelcase": 0,
|
||||
"no-extend-native": 0,
|
||||
"no-loop-func": 0,
|
||||
"new-cap": 0,
|
||||
"no-unused-expressions": 0,
|
||||
"no-mixed-requires": 0,
|
||||
"indent": ["error", 4, { "SwitchCase": 1, "VariableDeclarator": 1 }],
|
||||
"no-multi-spaces": [0, {
|
||||
"exceptions": {
|
||||
"VariableDeclarator": true
|
||||
}
|
||||
}],
|
||||
"no-trailing-spaces": [2, { "skipBlankLines": false }],
|
||||
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
|
||||
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
|
||||
"quotes": [
|
||||
1,
|
||||
"double",
|
||||
"avoid-escape"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
13
template.dot
Normal file
13
template.dot
Normal file
@ -0,0 +1,13 @@
|
||||
# Eve
|
||||
|
||||
Tiny event helping JavaScript library.
|
||||
{{~it.out :item:index}}
|
||||
## {{=item[0].name}}
|
||||
{{~item :line:i}}{{ if (i > 0) { }}{{ if (line.text) { }}{{=line.text.join("\n").replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}{{ if (line.attr) { }}### Parameters
|
||||
{{~line.attr :attr:j}}- _{{=attr.name}}_ {{~attr.type :type:k}} **{{=type}}**{{~}}
|
||||
{{=attr.desc.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}
|
||||
{{~}}{{ } }}{{ if (line.html) { }}{{=line.html.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}{{ if (line.head) { }}
|
||||
### {{=line.head}}{{ } }}{{ if (line.code) { }}```js
|
||||
{{=line.code.join("\n")}}
|
||||
```{{ } }}{{ if (line.rtrn) { }}**Returns:** {{~line.rtrn.type :type:k}} **{{=type}}**{{~}} {{=line.rtrn.desc.replace(/<\/?code>/g, "`").replace(/<\/?em>/g, "_")}}{{ } }}
|
||||
{{ } }}{{~}}{{~}}
|
Loading…
Reference in New Issue
Block a user