Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial thing that works

  • Loading branch information...
commit ffeb893c48ec556f2a9576515ca907c91031981c 0 parents
@substack authored
16 example/index.html
@@ -0,0 +1,16 @@
+<html>
+ <head>
+ <style>
+ #box {
+ width: 200px;
+ height: 200px;
+ margin: auto;
+ background-color: red;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="box"></div>
+ <script src="bundle.js"></script>
+ </body>
+</html>
13 example/main.js
@@ -0,0 +1,13 @@
+var ever = require('../');
+var box = document.querySelector('#box');
+
+var state = false;
+ever(box).on('click', function (ev) {
+ ev.stopPropagation();
+ state = !state;
+ box.style['background-color'] = state ? 'red' : 'rgb(127,127,127)';
+});
+
+setInterval(function () {
+ ever(box).emit('click');
+}, 3000);
69 index.js
@@ -0,0 +1,69 @@
+module.exports = function (elem) {
+ return new Ever(elem);
+};
+
+function Ever (elem) {
+ this.element = elem;
+}
+
+Ever.prototype.on = function (name, cb) {
+ this.element.addEventListener(name, cb);
+};
+
+var initSignatures = require('./init.json');
+
+Ever.prototype.emit = function (name, ev) {
+ if (typeof name === 'object') {
+ ev = name;
+ name = ev.type;
+ }
+
+ if (!isEvent(ev)) {
+ var type = Ever.typeOf(name);
+
+ var opts = ev || {};
+ if (opts.type === undefined) opts.type = name;
+
+ ev = document.createEvent(type + 's');
+ var init = typeof ev['init' + type] === 'function'
+ ? 'init' + type : 'initEvent'
+ ;
+
+ var sig = initSignatures[init];
+ var used = {};
+ var args = [];
+
+ for (var i = 0; i < sig.length; i++) {
+ var key = sig[i];
+ args.push(opts[key]);
+ used[key] = true;
+ }
+ ev[init].apply(ev, args);
+
+ // attach remaining unused options to the object
+ for (var key in opts) {
+ if (!used[key]) ev[key] = opts[key];
+ }
+ }
+ return this.element.dispatchEvent(ev);
+};
+
+function isEvent (ev) {
+ var s = Object.prototype.toString.call(ev);
+ return /\[object \S+Event\]/.test(s);
+}
+
+Ever.types = require('./types.json');
+Ever.typeOf = (function () {
+ var types = {};
+ for (var key in Ever.types) {
+ var ts = Ever.types[key];
+ for (var i = 0; i < ts.length; i++) {
+ types[ts[i]] = key;
+ }
+ }
+
+ return function (name) {
+ return types[name] || 'Event';
+ };
+})();;
41 init.json
@@ -0,0 +1,41 @@
+{
+ "initEvent" : [
+ "type",
+ "canBubble",
+ "cancelable"
+ ],
+ "initUIEvent" : [
+ "type",
+ "canBubble",
+ "cancelable",
+ "view",
+ "detail"
+ ],
+ "initMouseEvent" : [
+ "type",
+ "canBubble",
+ "cancelable",
+ "view",
+ "detail",
+ "screenX",
+ "screenY",
+ "clientX",
+ "clientY",
+ "ctrlKey",
+ "altKey",
+ "shiftKey",
+ "metaKey",
+ "button",
+ "relatedTarget"
+ ],
+ "initMutationEvent" : [
+ "type",
+ "canBubble",
+ "cancelable",
+ "relatedNode",
+ "prevValue",
+ "newValue",
+ "attrName",
+ "attrChange"
+ ]
+}
19 readme.markdown
@@ -0,0 +1,19 @@
+# dome
+
+[dom events](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Registration-interfaces) with a node-style api
+
+# example
+
+# methods
+
+# install
+
+With [npm](http://npmjs.org) do:
+
+```
+npm install eventual
+```
+
+# license
+
+MIT
43 types.json
@@ -0,0 +1,43 @@
+{
+ "MouseEvent" : [
+ "click",
+ "mousedown",
+ "mouseup",
+ "mouseover",
+ "mousemove",
+ "mouseout"
+ ],
+ "KeyEvent" : [
+ "keydown",
+ "keyup",
+ "keypress"
+ ],
+ "MutationEvent" : [
+ "DOMSubtreeModified",
+ "DOMNodeInserted",
+ "DOMNodeRemoved",
+ "DOMNodeRemovedFromDocument",
+ "DOMNodeInsertedIntoDocument",
+ "DOMAttrModified",
+ "DOMCharacterDataModified"
+ ],
+ "HTMLEvent" : [
+ "load",
+ "unload",
+ "abort",
+ "error",
+ "select",
+ "change",
+ "submit",
+ "reset",
+ "focus",
+ "blur",
+ "resize",
+ "scroll"
+ ],
+ "UIEvent" : [
+ "DOMFocusIn",
+ "DOMFocusOut",
+ "DOMActivate"
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.