Created
May 11, 2009 19:41
-
-
Save edvakf/110135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventGroup = function(target){ | |
this.target = target || window; | |
this.handler = {}; | |
this.doPreventDefault = true; | |
this.doStopPropagation = true; | |
var self = this; | |
this.delegate = function(e){ | |
if(e){ | |
if(self.doStopPropagation) e.stopPropagation(); | |
if(self.doPreventDefault) e.preventDefault(); | |
}else{ | |
e = window.event; | |
if(self.doStopPropagation) e.cancelBubble = true; | |
if(self.doPreventDefault) e.returnValue = false; | |
e.target = e.srcElement; | |
} | |
self.handler[e.type].call(self, e); | |
} | |
return this; | |
} | |
EventGroup.prototype.listen = function(type,func){ | |
var target = this.target; | |
this.handler[type] = func; | |
if(target.addEventListener){ | |
target.addEventListener(type,this.delegate,false); | |
}else if(target.attachEvent){ | |
target.attachEvent('on'+type,this.delegate); | |
}else{ | |
target['on'+type] = this.delegate; | |
} | |
return this; | |
}; | |
EventGroup.prototype.unlisten = function(type){ | |
var target = this.target; | |
if(target.removeEventListener){ | |
target.removeEventListener(type,this.delegate,false); | |
}else if(target.detachEvent){ | |
target.detachEvent('on'+type,this.delegate); | |
}else{ | |
target['on'+type] = null; | |
} | |
this.handler[type] = null; | |
return this; | |
}; | |
EventGroup.prototype.destroy = function(type){ | |
for (var type in this.handler) if(this.handler.hasOwnProperty(type)) { | |
this.unlisten(type); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment