summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas BrĂ¼derli2010-01-20 11:31:53 +0000
committerThomas BrĂ¼derli2010-01-20 11:31:53 +0000
commitaac5517f134f8740b63946120b8fcfc977e4dcd3 (patch)
tree41f65561ceddf0571400c59972be8b8de5bc22e0
parentcddebeb0fd99a4ad4e276682e75b9bd8c31c79e9 (diff)
downloaditools-aac5517f134f8740b63946120b8fcfc977e4dcd3.tar.gz
itools-aac5517f134f8740b63946120b8fcfc977e4dcd3.tar.bz2
itools-aac5517f134f8740b63946120b8fcfc977e4dcd3.zip
New utility functions to add and remove event handlers
-rw-r--r--itjs/it.js65
1 files changed, 65 insertions, 0 deletions
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)
{