diff options
author | Thomas BrĂ¼derli | 2010-01-20 11:31:53 +0000 |
---|---|---|
committer | Thomas BrĂ¼derli | 2010-01-20 11:31:53 +0000 |
commit | aac5517f134f8740b63946120b8fcfc977e4dcd3 (patch) | |
tree | 41f65561ceddf0571400c59972be8b8de5bc22e0 | |
parent | cddebeb0fd99a4ad4e276682e75b9bd8c31c79e9 (diff) | |
download | itools-aac5517f134f8740b63946120b8fcfc977e4dcd3.tar.gz itools-aac5517f134f8740b63946120b8fcfc977e4dcd3.tar.bz2 itools-aac5517f134f8740b63946120b8fcfc977e4dcd3.zip |
New utility functions to add and remove event handlers
-rw-r--r-- | itjs/it.js | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -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) { |