Skip to content

Commit c31590e

Browse files
committed
Add support for namespaced listeners.
You can now also remove listeners by specifying a null listener.
1 parent 67f122b commit c31590e

4 files changed

Lines changed: 83 additions & 67 deletions

File tree

d3.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function(){d3 = {version: "0.30.6"}; // semver
1+
(function(){d3 = {version: "0.30.8"}; // semver
22
if (!Date.now) Date.now = function() {
33
return +new Date();
44
};
@@ -1448,22 +1448,30 @@ function d3_selection(groups) {
14481448
return groups;
14491449
};
14501450

1451-
// TODO namespaced event listeners to allow multiples
1451+
// type can be namespaced, e.g., "click.foo"
1452+
// listener can be null for removal
14521453
groups.on = function(type, listener) {
1454+
1455+
// parse the type specifier
1456+
var i = type.indexOf("."),
1457+
typo = i == -1 ? type : type.substring(0, i),
1458+
name = "__on" + type;
1459+
1460+
// remove the old event listener, and add the new event listener
14531461
return groups.each(function(d, i) {
1454-
var l = function(e) {
1462+
if (this[name]) this.removeEventListener(typo, this[name], false);
1463+
if (listener) this.addEventListener(typo, this[name] = l, false);
1464+
1465+
// wrapped event listener that preserves d, i
1466+
function l(e) {
14551467
var o = d3.event; // Events can be reentrant (e.g., focus).
14561468
d3.event = e;
14571469
try {
14581470
listener.call(this, d, i);
14591471
} finally {
14601472
d3.event = o;
14611473
}
1462-
};
1463-
if (this.addEventListener)
1464-
this.addEventListener(type, l, false);
1465-
else
1466-
this["on" + type] = l;
1474+
}
14671475
});
14681476
};
14691477

0 commit comments

Comments
 (0)