/** * Class: JSL.event * All the event related functions are in this class. */ (function() { function _event_init(e) { this.event = e || window.event; return this; } _event_init.prototype = { /** * The famous addEvent function. But calling it in this format is not the preffered method. Use the DOM interface to make the call. * Arguments: ele(DOM Node) - The Element to which the event should be attached. This must be a DOM Node. * type(String) - The event type - "load", 'mouseover', 'click', etc. * func(Function) - The function that must be called on the event * capture(Boolean) - true if you want to enable capture. * Example: * JSL.dom("#ele").on("mouseover", function(e){ alert("Hello World"); }); * JSL.event().add(document.getElementById("ele"), "mouseover", function(e){ alert("Hello World"); }); */ "add" :function(ele,type,func,capture) { function _makeCallback(e){ var ele = JSL.event(e).getTarget() || document; func.call(ele,e); } capture = capture||true; if(ele.attachEvent) { return ele.attachEvent('on' + type, _makeCallback); } else if(ele.addEventListener) { ele.addEventListener(type, _makeCallback, capture); return true; } else { ele['on' + type] = _makeCallback; } }, /** * Stop an event from further propogation. * Taken from http://www.openjs.com/articles/prevent_default_action/ * Example: * JSL.event(e).stop(); */ "stop": function(){ var e = this.event; e.cancelBubble = true; e.returnValue = false; if(e.stopPropagation) e.stopPropagation(); if(e.preventDefault) e.preventDefault(); return false; }, /** * Get the target of the current event * Example: * var ele = JSL.event(e).getTarget(); */ "getTarget": function() { var element; var e = this.event; if(e.target) element=e.target; else if(e.srcElement) element=e.srcElement; if(element && element.nodeType==3) element=element.parentNode; //Safari Bug fix return element; } } window.JSL["event"] = function(e) { return new _event_init(e); } })(); /* :TODO: * remove() ? * */