var Events = new EventsManager();
function EventsManager()
{
var registry = null;
var last_lable = 0;
}
/**
* Initiate the registry and add unload event to window
*/
EventsManager.prototype.init = function()
{
if(this.registry != null)
return;
this.registry = new Array();
this.add(window, 'unload', this.unloadEvents);
}
/**
* Add an event listener to an object
*
* @param object obj The object to attach to
* @param string eventType The type of event to attach to
* @param string lable (Opt) Provide a virtual lable so removing listeners is easier
*
* @return The virtual lable used to register this event (so you may remove it later with ease), or false on failure
*/
EventsManager.prototype.add = function(obj, eventType, func, lable)
{
this.init();
if(typeof obj != 'object' || typeof func != 'function')
return false;
if(!lable)
lable = 'EVENT_LABLE_' + (this.last_lable++);
var success = true;
var add_reg = false;
var closure = null;
//------------------------------
// Add listeners
//------------------------------
// Moz
if(obj.addEventListener)
obj.addEventListener(eventType, func, false);
// IE
else if(obj.attachEvent)
{
closure = function() { func.call(obj); };
success = obj.attachEvent('on' + eventType, closure);
}
// Attempt at others
else
{
if(obj['on' + eventType])
{
var oldListener = obj['on' + eventType];
closure = function() { func.call(obj); oldListener.call(obj); }
}
obj['on' + eventType] = closure;
}
//------------------------------
// Add to reg
//------------------------------
if(success)
{
if(typeof closure == 'function')
func = closure;
this.registry[lable] = {obj: obj, eventType: eventType, func: func};
return lable;
}
else
return false;
}
/**
* Remove an event listener with the lable it was added to the registry with
*
* @param string lable The label the event was added to the registry with
* @param bool forceDel (Opt) If the browser doesn't support listener functions, we tried
* to solve it by explicitly setting the event listener. In this case, we
* cannot choose to remove a single listener, we have to remove all of them. Defaults to true.
*
* @return True on success, false on failure
*/
EventsManager.prototype.remove = function(lable, forceDel)
{
if(!this.registry[lable])
return false;
reg = this.registry[lable];
//------------------------------
// Remove listeners
//------------------------------
// Moz
if(reg.obj.removeEventListener)
{
reg.obj.removeEventListener(reg.eventType, reg.func, false);
done = true;
}
// IE
else if(reg.obj.detatchEvent)
done = reg.obj.detatchEvent('on' + reg.eventType, reg.func);
// Attempt at others
else
{
if(forceDel)
{
reg.obj['on' + reg.eventType] = null;
done = true;
}
else
done = false;
}
//------------------------------
// Remove from reg
//------------------------------
if(done)
this.registry[lable] = null;
return done;
}
/**
* Unload all events defined in the registry
*/
EventsManager.prototype.unloadEvents = function()
{
for(var lable in Events.registry)
{
if(Events.registry[lable] == null)
continue;
Events.remove.call(Events, lable, true);
}
Events.registry = null;
}