Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Friday, July 18, 2008

Some java script snippets - Event Handler - part 1

This is the first part of a sample event handler. We will see the declaration here

Note: Copy the code into notepad or someother custom editor for more legibility



var HANDLER = new EventHandler();
/**************************************************************************
Event Handler definition begins
**************************************************************************/
/* Cross browser event handler, that allows multiple handlers to handle the event */
function EventHandler () {
/* This is an array of eventHandler arrays, essentially a hashMap
defined using associative arrays.
Example content at run time would be
events["onload"] = ["adjustDiv", "centerAlignText"...];
where righthand side is a array of event Handling functions
*/
var eHandler = this;
eHandler.events = [];

function getHandlersForEventType(aEventType) {
/* if this is the first time we are listening the event,
create handlers array
*/
if(eHandler.events[aEventType]==undefined) {
eHandler.events[aEventType] = [];
}
return eHandler.events[aEventType];
}

function detectEvent(event) {
// grab the event object (IE uses a global event object)
event = event || ((this.ownerDocument || this.document || this).parentWindow || window).event;
return event;
}

function registerEventHandlerWithType(aEventType, handlerFnc){
var handlers = getHandlersForEventType(aEventType)
handlers[handlerFnc] = handlerFnc;
}

function unregisterEventHandlerWithType(aEventType, handlerFnc){
var handlers = getHandlersForEventType(aEventType);
/* if this handler is not registered*/
if(handlers[handlerFnc]==undefined) {
return;
}
delete handlers[handlerFnc];
}

function handleEvent(aEvent){
aEvent = detectEvent(aEvent);
var handlers = getHandlersForEventType(aEvent.type);
var diagMsg = "";
for(var handle in handlers){
// construct the method call from the function name and pass parameter
eval(handle)(aEvent);
}
/* stop the event bubbling now, this ensures browser compatibility */
if(typeof aEvent.cancelBubble != undefined){
// IE event model
aEvent.cancelBubble = true;
}
if (aEvent.stopPropagation){
// Gecko event model
aEvent.stopPropagation();
}
}

return {
/* public methods that are being exposed */
registerEventHandlerWithType: registerEventHandlerWithType,
unregisterEventHandlerWithType: unregisterEventHandlerWithType,
handleEvent: handleEvent
}
}

/**************************************************************************
Event Handler definition ends
**************************************************************************/

function observeEvent(aEvent){
HANDLER.handleEvent(aEvent);
}

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.