aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/event.js50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/event.js b/src/event.js
index 10585bb..74f22eb 100755
--- a/src/event.js
+++ b/src/event.js
@@ -39,48 +39,64 @@ SVG.on = function(node, event, listener) {
// create listener, get object-index
var l = listener.bind(node.instance || node)
, index = (SVG.handlerMap.indexOf(node) + 1 || SVG.handlerMap.push(node)) - 1
+ , ev = event.split('.')[0]
+ , ns = event.split('.')[1] || '*'
+
// ensure valid object
- SVG.listeners[index] = SVG.listeners[index] || {}
- SVG.listeners[index][event] = SVG.listeners[index][event] || {}
+ SVG.listeners[index] = SVG.listeners[index] || {}
+ SVG.listeners[index][ev] = SVG.listeners[index][ev] || {}
+ SVG.listeners[index][ev][ns] = SVG.listeners[index][ev][ns] || {}
// reference listener
- SVG.listeners[index][event][listener] = l
+ SVG.listeners[index][ev][ns][listener] = l
// add listener
- node.addEventListener(event, l, false)
+ node.addEventListener(ev, l, false)
}
// Add event unbinder in the SVG namespace
SVG.off = function(node, event, listener) {
var index = SVG.handlerMap.indexOf(node)
+ , ev = event && event.split('.')[0]
+ , ns = event && event.split('.')[1]
+
+ if(index == -1) return
if (listener) {
// remove listener reference
- if (index != -1 && SVG.listeners[index][event]) {
+ if (SVG.listeners[index][ev] && SVG.listeners[index][ev][ns || '*']) {
// remove listener
- node.removeEventListener(event, SVG.listeners[index][event][listener], false)
+ node.removeEventListener(ev, SVG.listeners[index][ev][ns || '*'][listener], false)
- delete SVG.listeners[index][event][listener]
+ delete SVG.listeners[index][ev][ns || '*'][listener]
}
- } else if (event) {
+ } else if (ns) {
+ // remove all listeners for the namespaced event
+ if (SVG.listeners[index][ev] && SVG.listeners[index][ev][ns]) {
+ for (listener in SVG.listeners[index][ev][ns])
+ SVG.off(node, [ev, ns].join('.'), listener)
+
+ delete SVG.listeners[index][ev][ns]
+ }
+
+ } else if (ev) {
// remove all listeners for the event
- if (SVG.listeners[index][event]) {
- for (listener in SVG.listeners[index][event])
- SVG.off(node, event, listener)
+ if (SVG.listeners[index][ev]) {
+ for (namespace in SVG.listeners[index][ev])
+ SVG.off(node, [ev, namespace].join('.'))
- delete SVG.listeners[index][event]
+ delete SVG.listeners[index][ev]
}
} else {
// remove all listeners on a given node
- if (index != -1) {
- for (event in SVG.listeners[index])
- SVG.off(node, event)
+ for (event in SVG.listeners[index])
+ SVG.off(node, event)
+
+ delete SVG.listeners[index]
- delete SVG.listeners[index]
- }
}
}