Skip to content

Commit

Permalink
Add d3.customEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 6, 2016
1 parent 23dc352 commit 2e6967d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ If *parameters* is a function, it is evaluated for each selected element, in ord

The current [event](https://developer.mozilla.org/en-US/docs/DOM/event), if any. This is set during the invocation of an event listener, and is reset after the listener terminates. Use this to access standard event fields such as [*event*.timeStamp](https://www.w3.org/TR/dom/#dom-event-timestamp) and methods such as [*event*.preventDefault](https://www.w3.org/TR/dom/#dom-event-preventdefault). While you can use the native [*event*.pageX](https://developer.mozilla.org/en/DOM/event.pageX) and [*event*.pageY](https://developer.mozilla.org/en/DOM/event.pageY), it is often more convenient to transform the event position to the local coordinate system of the container that received the event using [d3.mouse](#mouse), [d3.touch](#touch) or [d3.touches](#touches).

<a name="customEvent" href="#customEvent">#</a> d3.<b>customEvent</b>(<i>event</i>, <i>listener</i>[, <i>that</i>[, <i>arguments</i>]])

Invokes the specified *listener*, using the specified *that* `this` context and passing the specified *arguments*, if any. During the invocation, [d3.event](#event) is set to the specified *event*; after the listener returns (or throws an error), d3.event is restored to its previous value. In addition, sets *event*.sourceEvent to the prior value of d3.event, allowing custom events to retain a reference to the originating native event. Returns the value returned by the *listener*.

<a name="mouse" href="#mouse">#</a> d3.<b>mouse</b>(<i>container</i>)

Returns the *x* and *y* coordinates of the [current event](#event) relative to the specified *container*. The container may be an HTML or SVG container element, such as a [G element](http://www.w3.org/TR/SVG/struct.html#Groups) or an [SVG element](http://www.w3.org/TR/SVG/struct.html#SVGElement). The coordinates are returned as a two-element array of numbers [*x*, *y*].
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export {default as selectorAll} from "./src/selectorAll";
export {default as touch} from "./src/touch";
export {default as touches} from "./src/touches";
export {default as window} from "./src/window";
export {event} from "./src/selection/on";
export {event, customEvent} from "./src/selection/on";
11 changes: 11 additions & 0 deletions src/selection/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,14 @@ export default function(typename, value, capture) {
for (i = 0; i < n; ++i) this.each(on(typenames[i], value, capture));
return this;
}

export function customEvent(event1, callback, that, args) {
var event0 = event;
event1.sourceEvent = event;
event = event1;
try {
return callback.apply(that, args);
} finally {
event = event0;
}
}

0 comments on commit 2e6967d

Please sign in to comment.