Permalink
Browse files

initial commit for es6 transpilers support

  • Loading branch information...
1 parent 9c66466 commit 5fd5d95f90991a8ad02fa6760c3986762e3282d5 @chemerisuk committed Mar 23, 2014
Showing with 85 additions and 81 deletions.
  1. +1 −0 .jshintrc
  2. +1 −0 Gruntfile.js
  3. +2 −1 package.json
  4. +2 −4 src/element.get.js
  5. +75 −74 src/eventhandler.js
  6. +4 −2 src/node.events.js
View
@@ -1,4 +1,5 @@
{
+ "esnext": true,
"browser": true,
"boss": true,
View
@@ -162,6 +162,7 @@ module.exports = function(grunt) {
"build/better-dom.js": ["src/*.js"]
},
options: {
+ transform: ["browserify-es6-modules", "es6-browserify"],
postBundleCB: function(err, src, next) {
// append copyrights header
next(err, grunt.template.process(
View
@@ -32,7 +32,8 @@
"karma-firefox-launcher": "^0.1",
"karma-safari-launcher": "^0.1",
"karma-opera-launcher": "^0.1",
- "watchify": "^0.6.3"
+ "es6-browserify": "0.0.7",
+ "browserify-es6-modules": "^0.1.1"
},
"scripts": {
"test": "grunt test",
View
@@ -61,10 +61,8 @@ hooks.undefined = function(node) {
return node[name];
};
-hooks.type = function(node) {
- // some browsers don't recognize input[type=email] etc.
- return node.getAttribute("type") || node.type;
-};
+// some browsers don't recognize input[type=email] etc.
+hooks.type = (node) => node.getAttribute("type") || node.type;
if (!_.DOM2_EVENTS) {
hooks.textContent = function(node) { return node.innerText };
View
@@ -6,86 +6,85 @@ var _ = require("./utils"),
SelectorMatcher = require("./selectormatcher"),
defaultArgs = ["target", "currentTarget", "defaultPrevented"],
CUSTOM_EVENT_TYPE = "dataavailable",
- hooks = {};
-
-module.exports = function(type, selector, callback, props, el, once) {
- var hook = hooks[type],
- node = el._node,
- matcher = SelectorMatcher(selector, node),
- handler = function(e) {
- e = e || window.event;
- // early stop in case of default action
- if (module.exports.skip === type) return;
- // handle custom events in legacy IE
- if (handler._type === CUSTOM_EVENT_TYPE && e.srcUrn !== type) return;
- // srcElement can be null in legacy IE when target is document
- var target = e.target || e.srcElement || document,
- currentTarget = matcher ? matcher(target) : node,
- fn = typeof callback === "string" ? el[callback] : callback,
- args = props || defaultArgs;
-
- // early stop for late binding or when target doesn't match selector
- if (typeof fn !== "function" || !currentTarget) return;
-
- // off callback even if it throws an exception later
- if (once) el.off(type, callback);
-
- args = args.map(function(name) {
- if (!_.DOM2_EVENTS) {
- switch (name) {
- case "which":
- return e.keyCode;
- case "button":
- var button = e.button;
- // click: 1 === left; 2 === middle; 3 === right
- return button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) );
- case "pageX":
- return e.pageX || e.clientX + _.docEl.scrollLeft - _.docEl.clientLeft;
- case "pageY":
- return e.clientY + _.docEl.scrollTop - _.docEl.clientTop;
+ hooks = {},
+ EventHandler = function(type, selector, callback, props, el, once) {
+ var hook = hooks[type],
+ node = el._node,
+ matcher = SelectorMatcher(selector, node),
+ handler = function(e) {
+ e = e || window.event;
+ // early stop in case of default action
+ if (EventHandler.skip === type) return;
+ // handle custom events in legacy IE
+ if (handler._type === CUSTOM_EVENT_TYPE && e.srcUrn !== type) return;
+ // srcElement can be null in legacy IE when target is document
+ var target = e.target || e.srcElement || document,
+ currentTarget = matcher ? matcher(target) : node,
+ fn = typeof callback === "string" ? el[callback] : callback,
+ args = props || defaultArgs;
+
+ // early stop for late binding or when target doesn't match selector
+ if (typeof fn !== "function" || !currentTarget) return;
+
+ // off callback even if it throws an exception later
+ if (once) el.off(type, callback);
+
+ args = args.map(function(name) {
+ if (!_.DOM2_EVENTS) {
+ switch (name) {
+ case "which":
+ return e.keyCode;
+ case "button":
+ var button = e.button;
+ // click: 1 === left; 2 === middle; 3 === right
+ return button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) );
+ case "pageX":
+ return e.pageX || e.clientX + _.docEl.scrollLeft - _.docEl.clientLeft;
+ case "pageY":
+ return e.clientY + _.docEl.scrollTop - _.docEl.clientTop;
+ }
}
- }
- switch (name) {
- case "type":
- return type;
- case "defaultPrevented":
- // IE8 and Android 2.3 use returnValue instead of defaultPrevented
- return "defaultPrevented" in e ? e.defaultPrevented : e.returnValue === false;
- case "target":
- return $Element(target);
- case "currentTarget":
- return $Element(currentTarget);
- case "relatedTarget":
- return $Element(e.relatedTarget || e[(e.toElement === node ? "from" : "to") + "Element"]);
- }
+ switch (name) {
+ case "type":
+ return type;
+ case "defaultPrevented":
+ // IE8 and Android 2.3 use returnValue instead of defaultPrevented
+ return "defaultPrevented" in e ? e.defaultPrevented : e.returnValue === false;
+ case "target":
+ return $Element(target);
+ case "currentTarget":
+ return $Element(currentTarget);
+ case "relatedTarget":
+ return $Element(e.relatedTarget || e[(e.toElement === node ? "from" : "to") + "Element"]);
+ }
- return e[name];
- });
- // if props is not specified then prepend extra arguments if they exist
- if (e._args) args = e._args.concat(args);
-
- if (fn.apply(el, args) === false) {
- // prevent default if handler returns false
- if (_.DOM2_EVENTS) {
- e.preventDefault();
- } else {
- e.returnValue = false;
+ return e[name];
+ });
+ // if props is not specified then prepend extra arguments if they exist
+ if (e._args) args = e._args.concat(args);
+
+ if (fn.apply(el, args) === false) {
+ // prevent default if handler returns false
+ if (_.DOM2_EVENTS) {
+ e.preventDefault();
+ } else {
+ e.returnValue = false;
+ }
}
- }
- };
+ };
- if (hook) handler = hook(handler, type) || handler;
- // handle custom events for IE8
- if (!_.DOM2_EVENTS && !("on" + (handler._type || type) in node)) {
- handler._type = CUSTOM_EVENT_TYPE;
- }
+ if (hook) handler = hook(handler, type) || handler;
+ // handle custom events for IE8
+ if (!_.DOM2_EVENTS && !("on" + (handler._type || type) in node)) {
+ handler._type = CUSTOM_EVENT_TYPE;
+ }
- handler.type = selector ? type + " " + selector : type;
- handler.callback = callback;
+ handler.type = selector ? type + " " + selector : type;
+ handler.callback = callback;
- return handler;
-};
+ return handler;
+ };
// EventHandler hooks
@@ -120,4 +119,6 @@ if (!_.DOM2_EVENTS) {
});
}
-module.exports.hooks = hooks;
+EventHandler.hooks = hooks;
+
+export default EventHandler;
View
@@ -4,8 +4,10 @@
* @see https://github.com/chemerisuk/better-dom/wiki/Event-handling
*/
var _ = require("./utils"),
- $Node = require("./node"),
- EventHandler = require("./eventhandler");
+ $Node = require("./node")/*,
+ EventHandler = require("./eventhandler")*/;
+
+import EventHandler from "./eventhandler";
/**
* Bind a DOM event

0 comments on commit 5fd5d95

Please sign in to comment.