equal
deleted
inserted
replaced
|
1 // type can be namespaced, e.g., "click.foo" |
|
2 // listener can be null for removal |
|
3 d3_selectionPrototype.on = function(type, listener, capture) { |
|
4 if (arguments.length < 3) capture = false; |
|
5 |
|
6 // parse the type specifier |
|
7 var name = "__on" + type, i = type.indexOf("."); |
|
8 if (i > 0) type = type.substring(0, i); |
|
9 |
|
10 // if called with only one argument, return the current listener |
|
11 if (arguments.length < 2) return (i = this.node()[name]) && i._; |
|
12 |
|
13 // remove the old event listener, and add the new event listener |
|
14 return this.each(function(d, i) { |
|
15 var node = this; |
|
16 |
|
17 if (node[name]) node.removeEventListener(type, node[name], capture); |
|
18 if (listener) node.addEventListener(type, node[name] = l, capture); |
|
19 |
|
20 // wrapped event listener that preserves i |
|
21 function l(e) { |
|
22 var o = d3.event; // Events can be reentrant (e.g., focus). |
|
23 d3.event = e; |
|
24 try { |
|
25 listener.call(node, node.__data__, i); |
|
26 } finally { |
|
27 d3.event = o; |
|
28 } |
|
29 } |
|
30 |
|
31 // stash the unwrapped listener for retrieval |
|
32 l._ = listener; |
|
33 }); |
|
34 }; |