|
1 | | -(function(){d3 = {version: "0.30.6"}; // semver |
| 1 | +(function(){d3 = {version: "0.30.8"}; // semver |
2 | 2 | if (!Date.now) Date.now = function() { |
3 | 3 | return +new Date(); |
4 | 4 | }; |
@@ -1448,22 +1448,30 @@ function d3_selection(groups) { |
1448 | 1448 | return groups; |
1449 | 1449 | }; |
1450 | 1450 |
|
1451 | | - // TODO namespaced event listeners to allow multiples |
| 1451 | + // type can be namespaced, e.g., "click.foo" |
| 1452 | + // listener can be null for removal |
1452 | 1453 | 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 |
1453 | 1461 | 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) { |
1455 | 1467 | var o = d3.event; // Events can be reentrant (e.g., focus). |
1456 | 1468 | d3.event = e; |
1457 | 1469 | try { |
1458 | 1470 | listener.call(this, d, i); |
1459 | 1471 | } finally { |
1460 | 1472 | d3.event = o; |
1461 | 1473 | } |
1462 | | - }; |
1463 | | - if (this.addEventListener) |
1464 | | - this.addEventListener(type, l, false); |
1465 | | - else |
1466 | | - this["on" + type] = l; |
| 1474 | + } |
1467 | 1475 | }); |
1468 | 1476 | }; |
1469 | 1477 |
|
|
0 commit comments