-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrag.js
More file actions
85 lines (68 loc) · 2.09 KB
/
Copy pathdrag.js
File metadata and controls
85 lines (68 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// TODO Track touch points by identifier.
d3.behavior.drag = function() {
var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"),
origin = null;
function drag() {
this.on("mousedown.drag", mousedown)
.on("touchstart.drag", mousedown);
}
function mousedown() {
var target = this,
event_ = event.of(target, arguments),
eventTarget = d3.event.target,
offset,
origin_ = point(),
moved = 0;
var w = d3.select(window)
.on("mousemove.drag", dragmove)
.on("touchmove.drag", dragmove)
.on("mouseup.drag", dragend, true)
.on("touchend.drag", dragend, true);
if (origin) {
offset = origin.apply(target, arguments);
offset = [offset.x - origin_[0], offset.y - origin_[1]];
} else {
offset = [0, 0];
}
d3_eventCancel();
event_({type: "dragstart"});
function point() {
var p = target.parentNode,
t = d3.event.changedTouches;
return t ? d3.touches(p, t)[0] : d3.mouse(p);
}
function dragmove() {
if (!target.parentNode) return dragend(); // target removed from DOM
var p = point(),
dx = p[0] - origin_[0],
dy = p[1] - origin_[1];
moved |= dx | dy;
origin_ = p;
d3_eventCancel();
event_({type: "drag", x: p[0] + offset[0], y: p[1] + offset[1], dx: dx, dy: dy});
}
function dragend() {
event_({type: "dragend"});
// if moved, prevent the mouseup (and possibly click) from propagating
if (moved) {
d3_eventCancel();
if (d3.event.target === eventTarget) w.on("click.drag", click, true);
}
w .on("mousemove.drag", null)
.on("touchmove.drag", null)
.on("mouseup.drag", null)
.on("touchend.drag", null);
}
// prevent the subsequent click from propagating (e.g., for anchors)
function click() {
d3_eventCancel();
w.on("click.drag", null);
}
}
drag.origin = function(x) {
if (!arguments.length) return origin;
origin = x;
return drag;
};
return d3.rebind(drag, event, "on");
};