Skip to content

Commit d524fe5

Browse files
committed
Merge branch 'drag-suppress' into 3.2.2
2 parents 35f65d8 + aa75127 commit d524fe5

9 files changed

Lines changed: 270 additions & 288 deletions

File tree

d3.js

Lines changed: 222 additions & 234 deletions
Large diffs are not rendered by default.

d3.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/behavior/drag.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import "../core/document";
22
import "../core/rebind";
3+
import "../event/drag";
34
import "../event/event";
45
import "../event/mouse";
56
import "../event/touches";
6-
import "../event/user-select";
77
import "behavior";
88

99
d3.behavior.drag = function() {
@@ -23,7 +23,7 @@ d3.behavior.drag = function() {
2323
offset,
2424
origin_ = point(),
2525
moved = 0,
26-
selectEnable = d3_event_userSelectSuppress(touchId != null ? "drag-" + touchId : "drag");
26+
dragRestore = d3_event_dragSuppress(touchId != null ? "drag-" + touchId : "drag");
2727

2828
var w = d3.select(d3_window)
2929
.on(touchId != null ? "touchmove.drag-" + touchId : "mousemove.drag", dragmove)
@@ -54,23 +54,16 @@ d3.behavior.drag = function() {
5454

5555
moved |= dx | dy;
5656
origin_ = p;
57-
d3_eventCancel();
5857

5958
event_({type: "drag", x: p[0] + offset[0], y: p[1] + offset[1], dx: dx, dy: dy});
6059
}
6160

6261
function dragend() {
63-
event_({type: "dragend"});
64-
65-
// if moved, prevent the mouseup (and possibly click) from propagating
66-
if (moved) {
67-
d3_eventCancel();
68-
if (d3.event.target === eventTarget) d3_eventSuppress(w, "click");
69-
}
70-
7162
w .on(touchId != null ? "touchmove.drag-" + touchId : "mousemove.drag", null)
7263
.on(touchId != null ? "touchend.drag-" + touchId : "mouseup.drag", null);
73-
selectEnable();
64+
65+
dragRestore(moved && d3.event.target === eventTarget);
66+
event_({type: "dragend"});
7467
}
7568
}
7669

src/behavior/zoom.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import "../core/document";
22
import "../core/rebind";
3+
import "../event/drag";
34
import "../event/event";
45
import "../event/mouse";
56
import "../event/touches";
6-
import "../event/user-select";
77
import "../selection/selection";
88
import "behavior";
99

@@ -105,7 +105,7 @@ d3.behavior.zoom = function() {
105105
moved = 0,
106106
w = d3.select(d3_window).on("mousemove.zoom", mousemove).on("mouseup.zoom", mouseup),
107107
l = location(d3.mouse(target)),
108-
selectEnable = d3_event_userSelectSuppress("zoom");
108+
dragRestore = d3_event_dragSuppress("zoom");
109109

110110
function mousemove() {
111111
moved = 1;
@@ -114,10 +114,8 @@ d3.behavior.zoom = function() {
114114
}
115115

116116
function mouseup() {
117-
if (moved) d3_eventCancel();
118117
w.on("mousemove.zoom", null).on("mouseup.zoom", null);
119-
selectEnable();
120-
if (moved && d3.event.target === eventTarget) d3_eventSuppress(w, "click.zoom");
118+
dragRestore(moved && d3.event.target === eventTarget);
121119
}
122120
}
123121

src/event/drag.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import "../core/document";
2+
import "../core/vendor";
3+
import "../selection/on";
4+
5+
var d3_event_dragSelect = d3_vendorSymbol(d3_documentElement.style, "userSelect");
6+
7+
function d3_event_dragSuppress(type) {
8+
var selectstart = "selectstart." + type,
9+
dragstart = "dragstart." + type,
10+
click = "click." + type,
11+
w = d3.select(d3_window).on(selectstart, d3_eventPreventDefault).on(dragstart, d3_eventPreventDefault),
12+
style = d3_documentElement.style,
13+
select = style[d3_event_dragSelect];
14+
style[d3_event_dragSelect] = "none";
15+
return function(suppressClick) {
16+
w.on(selectstart, null).on(dragstart, null);
17+
style[d3_event_dragSelect] = select;
18+
if (suppressClick) { // suppress the next click, but only if it’s immediate
19+
function off() { w.on(click, null); }
20+
w.on(click, function() { d3_eventPreventDefault(); off(); }, true);
21+
setTimeout(off, 0);
22+
}
23+
};
24+
}

src/event/event.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import "dispatch";
22

33
d3.event = null;
44

5-
function d3_eventCancel() {
6-
d3.event.stopPropagation();
5+
function d3_eventPreventDefault() {
76
d3.event.preventDefault();
87
}
98

@@ -13,15 +12,6 @@ function d3_eventSource() {
1312
return e;
1413
}
1514

16-
// Registers an event listener for the specified target that cancels the next
17-
// event for the specified type, but only if it occurs immediately. This is
18-
// useful to disambiguate dragging from clicking.
19-
function d3_eventSuppress(target, type) {
20-
function off() { target.on(type, null); }
21-
target.on(type, function() { d3_eventCancel(); off(); }, true);
22-
setTimeout(off, 0); // clear the handler if it doesn't fire
23-
}
24-
2515
// Like d3.dispatch, but for custom events abstracting native UI events. These
2616
// events have a target component (such as a brush), a target element (such as
2717
// the svg:g element containing the brush) and the standard arguments `d` (the

src/event/user-select.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/svg/brush.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "../core/document";
22
import "../core/rebind";
33
import "../event/dispatch";
4+
import "../event/drag";
45
import "../event/event";
56
import "../event/mouse";
67
import "../event/touches";
@@ -100,6 +101,7 @@ d3.svg.brush = function() {
100101
resizingX = !/^(n|s)$/.test(resizing) && x,
101102
resizingY = !/^(e|w)$/.test(resizing) && y,
102103
dragging = eventTarget.classed("extent"),
104+
dragRestore = d3_event_dragSuppress("brush"),
103105
center,
104106
origin = mouse(),
105107
offset;
@@ -139,7 +141,6 @@ d3.svg.brush = function() {
139141
// Notify listeners.
140142
event_({type: "brushstart"});
141143
brushmove();
142-
d3_eventCancel();
143144

144145
function mouse() {
145146
var touches = d3.event.changedTouches;
@@ -154,7 +155,7 @@ d3.svg.brush = function() {
154155
origin[1] -= extent[1][1];
155156
dragging = 2;
156157
}
157-
d3_eventCancel();
158+
d3_eventPreventDefault();
158159
}
159160
}
160161

@@ -163,7 +164,7 @@ d3.svg.brush = function() {
163164
origin[0] += extent[1][0];
164165
origin[1] += extent[1][1];
165166
dragging = 0;
166-
d3_eventCancel();
167+
d3_eventPreventDefault();
167168
}
168169
}
169170

@@ -267,8 +268,8 @@ d3.svg.brush = function() {
267268
.on("keydown.brush", null)
268269
.on("keyup.brush", null);
269270

271+
dragRestore();
270272
event_({type: "brushend"});
271-
d3_eventCancel();
272273
}
273274
}
274275

test/load.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ module.exports = function() {
1414
files.push("src/end");
1515

1616
function topic() {
17-
smash.load(files, expression, sandbox, this.callback);
17+
var callback = this.callback;
18+
smash.load(files, expression, sandbox, function(error, result) {
19+
if (error) console.trace(error.stack);
20+
callback(error, result);
21+
});
1822
}
1923

2024
topic.expression = function(_) {

0 commit comments

Comments
 (0)