From aac5517f134f8740b63946120b8fcfc977e4dcd3 Mon Sep 17 00:00:00 2001 From: Thomas BrĂ¼derli Date: Wed, 20 Jan 2010 11:31:53 +0000 Subject: New utility functions to add and remove event handlers --- itjs/it.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/itjs/it.js b/itjs/it.js index 090d28f..a48d829 100644 --- a/itjs/it.js +++ b/itjs/it.js @@ -103,6 +103,71 @@ function it_event(p) p.element["on" + p.event].oldhandler = oldhandler; } +function it_add_event(p) +{ + if (!p.object || !p.method) // not enough arguments + return; + if (!p.element) + p.element = document; + + if (!p.object._it_events) + p.object._it_events = []; + + var evt = p.event; + var key = p.event + '*' + p.method; + var p_closure = p; // Needed for Konqueror 3.4.2 as p is (wrongly) shadowed by global input element named p + if (!p.object._it_events[key]) + p.object._it_events[key] = function(e){ return p_closure.object[p_closure.method](e, p_closure); }; + + if (p.element.addEventListener) + p.element.addEventListener(evt, p.object._it_events[key], false); + else if (p.element.attachEvent) + p.element.attachEvent('on'+evt, p.object._it_events[key]); + else + { + p.element['on'+evt] = function(e) + { + var ret = true; + for (var k in p_closure.object._it_events) + if (p_closure.object._it_events[k] && k.indexOf(evt) == 0) + ret = p_closure.object._it_events[k](e); + return ret; + }; + } +} + +function it_remove_event(p) +{ + if (!p.element) + p.element = document; + + var key = p.event + '*' + p.method; + if (p.object && p.object._it_events && p.object._it_events[key]) { + if (p.element.removeEventListener) + p.element.removeEventListener(p.event, p.object._it_events[key], false); + else if (p.element.detachEvent) + p.element.detachEvent('on'+p.event, p.object._it_events[key]); + + p.object._it_events[key] = null; + } +} + +/** + * Prevent event propagation and bubbeling + */ +function it_event_void(evt) +{ + var e = evt ? evt : window.event; + if (e.preventDefault) + e.preventDefault(); + if (e.stopPropagation) + e.stopPropagation(); + + e.cancelBubble = true; + e.returnValue = false; + return false; +} + /* Get object pixel position. Based on quirksmode.org's code */ function it_get_obj_x(obj) { -- cgit v1.2.3