cleanup event handling
This commit is contained in:
parent
66a7c35d80
commit
9805336c0a
1 changed files with 11 additions and 59 deletions
|
@ -1,12 +1,3 @@
|
|||
/**
|
||||
* eventHandler
|
||||
*
|
||||
* @author Icewind <icewind (at) derideal (dot) com>
|
||||
* @copyright 2009
|
||||
* @license http://www.gnu.org/licenses/gpl.html GNU Public License
|
||||
* @version 0.1
|
||||
*/
|
||||
|
||||
/*event handling
|
||||
usage: document.events.add(node,type,function,arguments);
|
||||
or: node.addEvent(type,function,arguments);
|
||||
|
@ -17,40 +8,25 @@ document.events.functions=Array();
|
|||
document.events.args=Array();
|
||||
|
||||
document.events.add=function(element,type,func,args){
|
||||
if(!element.eventCallBacks){
|
||||
element.eventCallBacks=Array();
|
||||
}
|
||||
if(!element.eventCallBacks[type]){
|
||||
element.eventCallBacks[type]=Array();
|
||||
}
|
||||
if(args){
|
||||
if(!args.push){
|
||||
args=[args];
|
||||
}
|
||||
}
|
||||
if(!args){
|
||||
args=Array();
|
||||
}
|
||||
args.push('eventHolder');
|
||||
args.push('argHolder');
|
||||
args=args||[];
|
||||
if (type && element){
|
||||
|
||||
//wrap the function in a function, otherwise it won't work if func is actually a callBack
|
||||
var funcId=document.events.functions.length;
|
||||
document.events.functions[funcId]=func;
|
||||
document.events.args[funcId]=args;
|
||||
eval('var callback=function(event,arg){document.events.callback.call(this,'+funcId+',event,arg)};');
|
||||
element.eventCallBacks[type].push(callback);
|
||||
args.foreach(function(argument){
|
||||
func.bind(argument);
|
||||
})
|
||||
if(element.addEventListener){
|
||||
var eventType=type;
|
||||
if(eventType.substr(0,2)=='on'){
|
||||
eventType=eventType.substr(2);
|
||||
if(type.substr(0,2)=='on'){
|
||||
type=type.substr(2);
|
||||
}
|
||||
element.addEventListener(eventType,callback,false);
|
||||
element.addEventListener(type,func,false);
|
||||
}else if(element.attachEvent){
|
||||
element.attachEvent(type,callback);
|
||||
element.attachEvent(type,func);
|
||||
}
|
||||
return callback;
|
||||
return func;
|
||||
}
|
||||
}
|
||||
document.events.remove=function(element,type,func){
|
||||
|
@ -64,33 +40,9 @@ document.events.remove=function(element,type,func){
|
|||
}
|
||||
}
|
||||
|
||||
document.events.callback=function(funcId,event,arg){
|
||||
if(!event)var event=window.event;
|
||||
var args=document.events.args[funcId];
|
||||
args[args.length-2]=event;
|
||||
args[args.length-1]=arg;
|
||||
result=document.events.functions[funcId].apply(this,args);
|
||||
if(result===false){
|
||||
if(event.preventDefault){
|
||||
event.preventDefault();
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
document.events.trigger=function(element,type,event,args){
|
||||
var callbacks=element.eventCallBacks[type];
|
||||
for(var i=0;i<callbacks.length;i++){
|
||||
callbacks[i].call(element,event,args);
|
||||
}
|
||||
}
|
||||
|
||||
Node.prototype.addEvent=function(type,func,arguments){
|
||||
return document.events.add(this,type,func,arguments);
|
||||
Node.prototype.addEvent=function(type,func,args){
|
||||
return document.events.add(this,type,func,args);
|
||||
}
|
||||
Node.prototype.removeEvent=function(type,func){
|
||||
document.events.remove(this,type,func);
|
||||
}
|
||||
Node.prototype.triggerEvent=function(type,event,arg){
|
||||
return document.events.trigger(this,type,event,arg);
|
||||
}
|
Loading…
Reference in a new issue