var ZPIN = ZPIN || {}; /** * Event handeling are put in this object */ ZPIN.Events = { /** * This will add an event handler to the node * * @param {HTMLElement} node The node to set an eventhandler * @param {String} type The type of the event * @param {function} f The event handler function */ addEventHandler: function(node, type, f) { if (node.addEventListener) { // w3c node.addEventListener(type, f, false); // false -> event bubbles up } else if (node.attachEvent) { // ie node.attachEvent("on"+type, f); } else { node["on"+type] = f; } }, /** * Removes an event handler from a node * * @param {HTMLElement} node The node to remove an eventhandler * @param {String} type The type of the event * @param {function} f The event handler function to be removed */ removeEventHandler: function (node, type, f) { if ( node.removeEventListener ) { //w3c node.removeEventListener(type, f, false); } else if ( node.detachEvent ) { // ie node.detachEvent("on"+type, f); } else { node["on"+type] = function() {}; } }, /** * This function is used by ImageEditor and is a copy from kort.js */ registerEvent: function(object, methodName) { return function () { object[methodName].apply(object, arguments); }; }, /** * This functions fires events from target of given type. * !! This cannot be used on mouse events etc. !! * * @param {HTMLElement} target The element the events will be fired from * @param {String} type The type of the event */ fireEvent: function(target, type) { var e; // Event we want to fire // Create the event if ( document.createEvent ) { // DOM // Make event of correct type if ( type === "mousedown" || type === "mouseup" || type === "click" || type === "mouseover" || type === "mousemove" || type === "mouseout" ) { e = document.createEvent("MouseEvent"); } else if ( type === "keydown" || type === "keyup" ) { e = document.createEvent("KeyboardEvent"); } else if ( type === "load" || type === "unload" || type === "abort" || type === "error" || type === "select" || type === "change" || type === "submit" || type === "reset" || type === "focus" || type === "blur" || type === "resize" || type === "scroll" ) { e = document.createEvent("Event"); } else { throw type+" is an event I dont know how to make!"; } e.initEvent(type, true, false); } else if ( document.createEventObject ) { // IE e = document.createEventObject(); } else { // Browser cant handle this - exit return false; } // Fires it if ( target.dispatchEvent ) { // DOM target.dispatchEvent(e); } else if ( target.fireEvent ) { // IE target.fireEvent("on"+type, e); } }, /** * Makes the bubbeling stop, telling the browser * "it's ok we have handeled this now, dont tell our parents". */ stopPropagation: function(e) { e.cancelBubble = true; // IE if ( e.stopPropagation ) { // w3c e.stopPropagation(); } }, /** * Makes the browser cancel its default action */ preventDefault: function(e) { e.returnValue = false; // IE if ( e.preventDefault ) { // w3c e.preventDefault(); } }, /** * Fetch the event properties * * @param {Event} e The event * @return {Object} {e: The event, t: target} */ getEventProperties: function(e) { e = e || window.event; var t = e.target || e.srcElement; if ( t && t.nodeType == 3 ) { // Fixing safari bug t = t.parentNode; } return {e: e, t: t}; }, getMousecoordinates: function(e) { var posx = 0; var posy = 0; if (e.pageX || e.pageY) { posx = e.pageX; posy = e.pageY; } else if (e.clientX || e.clientY) { posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; } return {x: posx, y: posy}; } };