Skip to content

Instantly share code, notes, and snippets.

@edvakf edvakf/EventGroup.js
Created May 11, 2009

Embed
What would you like to do?
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
You can’t perform that action at this time.