Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created May 11, 2009 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvakf/110135 to your computer and use it in GitHub Desktop.
Save edvakf/110135 to your computer and use it in GitHub Desktop.
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