Skip to content

Commit

Permalink
Redesign d3_eventSuppress slightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 7, 2013
1 parent 45c3b4b commit 80a1dd6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
17 changes: 8 additions & 9 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,15 @@ d3 = function() {
while (s = e.sourceEvent) e = s;
return e;
}
function d3_eventSuppress(name) {
var w = d3.select(d3_window);
function unbind() {
w.on(name + ".suppress", null);
function d3_eventSuppress(target, type) {
function off() {
target.on(type, null);
}
w.on(name + ".suppress", function() {
target.on(type, function() {
d3_eventCancel();
unbind();
off();
}, true);
setTimeout(unbind, 0);
setTimeout(off, 0);
}
function d3_eventDispatch(target) {
var dispatch = new d3_dispatch(), i = 0, n = arguments.length;
Expand Down Expand Up @@ -535,7 +534,7 @@ d3 = function() {
});
if (moved) {
d3_eventCancel();
if (d3.event.target === eventTarget) d3_eventSuppress("click");
if (d3.event.target === eventTarget) d3_eventSuppress(w, "click");
}
w.on(touchId != null ? "touchmove.drag-" + touchId : "mousemove.drag", null).on(touchId != null ? "touchend.drag-" + touchId : "mouseup.drag", null);
}
Expand Down Expand Up @@ -1178,7 +1177,7 @@ d3 = function() {
function mouseup() {
if (moved) d3_eventCancel();
w.on("mousemove.zoom", null).on("mouseup.zoom", null);
if (moved && d3.event.target === eventTarget) d3_eventSuppress("click");
if (moved && d3.event.target === eventTarget) d3_eventSuppress(w, "click.zoom");
}
}
function mousewheel() {
Expand Down
10 changes: 5 additions & 5 deletions d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/behavior/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ d3.behavior.drag = function() {
// if moved, prevent the mouseup (and possibly click) from propagating
if (moved) {
d3_eventCancel();
if (d3.event.target === eventTarget) d3_eventSuppress('click');
if (d3.event.target === eventTarget) d3_eventSuppress(w, "click");
}

w .on(touchId != null ? "touchmove.drag-" + touchId : "mousemove.drag", null)
Expand Down
2 changes: 1 addition & 1 deletion src/behavior/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ d3.behavior.zoom = function() {
function mouseup() {
if (moved) d3_eventCancel();
w.on("mousemove.zoom", null).on("mouseup.zoom", null);
if (moved && d3.event.target === eventTarget) d3_eventSuppress('click');
if (moved && d3.event.target === eventTarget) d3_eventSuppress(w, "click.zoom");
}
}

Expand Down
21 changes: 7 additions & 14 deletions src/event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ function d3_eventSource() {
return e;
}

// In some cases, e.g. on dragend of behavior.zoom and behavior.drag, we need
// to suppress an event that fires immediately.
function d3_eventSuppress(name) {
var w = d3.select(d3_window);
function unbind() {
w.on(name + ".suppress", null);
}
w.on(name + ".suppress", function() {
// prevent the subsequent event from propagating (e.g., for anchors)
d3_eventCancel();
unbind();
}, true);
// clear the handler after a 0ms timeout in case it doesn't fire after all
setTimeout(unbind, 0);
// Registers an event listener for the specified target that cancels the next
// event for the specified type, but only if it occurs immediately. This is
// useful to disambiguate dragging from clicking.
function d3_eventSuppress(target, type) {
function off() { target.on(type, null); }
target.on(type, function() { d3_eventCancel(); off(); }, true);
setTimeout(off, 0); // clear the handler if it doesn't fire
}

// Like d3.dispatch, but for custom events abstracting native UI events. These
Expand Down

0 comments on commit 80a1dd6

Please sign in to comment.