Skip to content

@subtleGradient /README.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Event Delegation by Stedia and the MooTools Dev Team
// basic usage:
$('myParentElement').relayEvents({
'a.menuitem':{
'click': function(e){}
},
'input[name=phone]':{
'keydown': function(e){},
'keypress': function(e){}
}
});
/*
Event Delegation - really powerfull, if you can understant why :-)
License: MIT-style license.
Copyright: Copyright (c) 2008 Daniel Steigerwald, daniel.steigerwald.cz
Example: http://daniel.steigerwald.cz/demos/delegated/default.htm
*/
(function(){
function check(e, selector, fn){
var target = e.target,
els = this.getElements(selector),
isOverOut = /^(mouseover|mouseout)$/.test(e.type);
for (var i = els.length; i--; ){
var el = els[i];
if (el == target || el.hasChild(target)){
if (isOverOut && !checkOverOut.call(el, e)) return;
fn.call(el, e, el);
return;
}
}
}
function checkOverOut(e){
var related = e.relatedTarget;
return related == undefined
|| related !== false
&& $type(this) != 'document'
&& related != this
&& related.prefix != 'xul'
&& !this.hasChild(related);
}
Element.Properties.relayedEvents = {
set: function(obj){
this.relayEvents(obj);
}
};
Element.implement({
relayEvent: function(selector, type, fn){
type = { mouseenter: 'mouseover', mouseleave: 'mouseout'}[type] || type;
var revents = this.retrieve('$moo:revents');
if (!revents) this.store('$moo:revents', revents = {});
var sel = revents[selector] = revents[selector] || {};
sel[type] = sel[type] || [];
var fn = check.bindWithEvent(this, [selector, fn]);
sel[type].push(fn);
return this.addEvent(type, fn);
},
relayEvents: function(selector, events){
if (/^(hash|object)$/.test($type(selector)))
for (var sel in selector) this.relayEvents(sel, selector[sel]);
else
for (var type in events) this.relayEvent(selector, type, events[type]);
return this;
},
unrelayEvents: function(selector){
var events = this.retrieve('$moo:revents');
if (events && events[selector]){
for (var type in events[selector]){
events[selector][type].each(function(fn){
this.removeEvent(type, fn);
}, this);
}
}
return this;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.