Skip to content
Browse files

copied Custom Event support from MooTools

However, I quite hate this. I'd rather this get solved with the ideas from #4, and in the event-emitters branch.
  • Loading branch information...
1 parent dfe3235 commit f7d681de5fe1a475ba46776b646b6b51dab95ab2 @seanmonstar committed Nov 14, 2011
Showing with 44 additions and 5 deletions.
  1. +44 −5 lib/shipyard/dom/Node.js
View
49 lib/shipyard/dom/Node.js
@@ -45,13 +45,13 @@ var Node = new Class({
},
addEvent: function addEvent(name, fn) {
- fn = wrapHandler(this, fn);
+ fn = wrapHandler(this, name, fn);
_addEvent.call(this, name, fn);
return Events.prototype.addEvent.call(this, name, fn);
},
removeEvent: function removeEvent(name, fn) {
- fn = unwrapHandler(this, fn);
+ fn = unwrapHandler(this, name, fn);
_removeEvent.call(this, name, fn);
return Events.prototype.removeEvent.call(this, name, fn);
}
@@ -91,19 +91,58 @@ Node.wrap = function(node) {
// Event Listeners
-function wrapHandler(node, fn) {
+
+function relay(e){
+ var related = e.relatedTarget;
+ if (related == null) {
+ return true;
+ }
+ if (!related) {
+ return false;
+ }
+ return (related !== this.getNode() && related.prefix !== 'xul' && !this.contains(related));
+}
+
+var CustomEvents = {
+ mouseenter: {
+ base: 'mouseover',
+ condition: relay
+ },
+ mouseleave: {
+ base: 'mouseout',
+ condition: relay
+ }
+};
+
+function wrapHandler(node, type, fn) {
+ var realType = type,
+ condition = fn;
var events = node.$wrappedEvents || (node.$wrappedEvents = {
wraps: [],
origs: []
});
var wrapped = events.wraps[events.origs.indexOf(fn)];
if (!wrapped) {
+ var custom = CustomeEvents[type];
+ if (custom) {
+ if (custom.condition) {
+ condition = function(e) {
+ if (custom.condition.call(this, e, type)) {
+ return fn.call(this, e);
+ }
+ return true;
+ };
+ }
+ if (custom.base) {
+ realType = Function.from(custom.base).call(this, type);
+ }
+ }
wrapped = function(e) {
if (e) {
e = new DOMEvent(e, node.getWindow());
}
- fn.call(this, e);
+ condition.call(this, e);
};
events.origs.push(fn);
events.wraps.push(wrapped);
@@ -112,7 +151,7 @@ function wrapHandler(node, fn) {
return wrapped;
}
-function unwrapHandler(node, fn) {
+function unwrapHandler(node, type, fn) {
var events = node.$wrappedEvents || (node.$wrappedEvents = {
wraps: [],
origs: []

0 comments on commit f7d681d

Please sign in to comment.
Something went wrong with that request. Please try again.