UNPKG

d3

Version:

A small, free JavaScript library for manipulating documents based on data.

35 lines (28 loc) 1.07 kB
// type can be namespaced, e.g., "click.foo" // listener can be null for removal d3_selectionPrototype.on = function(type, listener, capture) { if (arguments.length < 3) capture = false; // parse the type specifier var name = "__on" + type, i = type.indexOf("."); if (i > 0) type = type.substring(0, i); // if called with only one argument, return the current listener if (arguments.length < 2) return (i = this.node()[name]) && i._; // remove the old event listener, and add the new event listener return this.each(function(d, i) { var node = this; if (node[name]) node.removeEventListener(type, node[name], capture); if (listener) node.addEventListener(type, node[name] = l, capture); // wrapped event listener that preserves i function l(e) { var o = d3.event; // Events can be reentrant (e.g., focus). d3.event = e; try { listener.call(node, node.__data__, i); } finally { d3.event = o; } } // stash the unwrapped listener for retrieval l._ = listener; }); };