forked from openstreetmap/iD
/
drag.js
218 lines (166 loc) · 5.97 KB
/
drag.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { dispatch as d3_dispatch } from 'd3-dispatch';
import {
customEvent as d3_customEvent,
event as d3_event,
mouse as d3_mouse,
select as d3_select,
selection as d3_selection
} from 'd3-selection';
import { osmNote } from '../osm';
import { utilRebind } from '../util/rebind';
import { utilPrefixCSSProperty, utilPrefixDOMProperty } from '../util';
/*
`behaviorDrag` is like `d3_behavior.drag`, with the following differences:
* The `origin` function is expected to return an [x, y] tuple rather than an
{x, y} object.
* The events are `start`, `move`, and `end`.
(https://github.com/mbostock/d3/issues/563)
* The `start` event is not dispatched until the first cursor movement occurs.
(https://github.com/mbostock/d3/pull/368)
* The `move` event has a `point` and `delta` [x, y] tuple properties rather
than `x`, `y`, `dx`, and `dy` properties.
* The `end` event is not dispatched if no movement occurs.
* An `off` function is available that unbinds the drag's internal event handlers.
*/
export function behaviorDrag() {
var dispatch = d3_dispatch('start', 'move', 'end');
var _origin = null;
var _selector = '';
var _event;
var _target;
var _surface;
// use pointer events on supported platforms; fallback to mouse events
var _pointerPrefix = 'PointerEvent' in window ? 'pointer' : 'mouse';
var d3_event_userSelectProperty = utilPrefixCSSProperty('UserSelect');
var d3_event_userSelectSuppress = function() {
var selection = d3_selection();
var select = selection.style(d3_event_userSelectProperty);
selection.style(d3_event_userSelectProperty, 'none');
return function() {
selection.style(d3_event_userSelectProperty, select);
};
};
function d3_eventCancel() {
d3_event.stopPropagation();
d3_event.preventDefault();
}
function eventOf(thiz, argumentz) {
return function(e1) {
e1.target = behavior;
d3_customEvent(e1, dispatch.apply, dispatch, [e1.type, thiz, argumentz]);
};
}
function dragstart() {
_target = this;
_event = eventOf(_target, arguments);
var eventTarget = d3_event.target;
var offset;
var startOrigin = point();
var started = false;
var selectEnable = d3_event_userSelectSuppress();
d3_select(window)
.on(_pointerPrefix + 'move.drag', dragmove)
.on(_pointerPrefix + 'up.drag', dragend, true);
if (_origin) {
offset = _origin.apply(_target, arguments);
offset = [offset[0] - startOrigin[0], offset[1] - startOrigin[1]];
} else {
offset = [0, 0];
}
d3_event.stopPropagation();
function point() {
var p = _surface || _target.parentNode;
return d3_mouse(p);
}
function dragmove() {
var p = point();
var dx = p[0] - startOrigin[0];
var dy = p[1] - startOrigin[1];
if (dx === 0 && dy === 0)
return;
startOrigin = p;
d3_eventCancel();
if (!started) {
started = true;
_event({ type: 'start' });
} else {
_event({
type: 'move',
point: [p[0] + offset[0], p[1] + offset[1]],
delta: [dx, dy]
});
}
}
function dragend() {
if (started) {
_event({ type: 'end' });
d3_eventCancel();
if (d3_event.target === eventTarget) {
d3_select(window)
.on('click.drag', click, true);
}
}
d3_select(window)
.on(_pointerPrefix + 'move.drag', null)
.on(_pointerPrefix + 'up.drag', null);
selectEnable();
}
function click() {
d3_eventCancel();
d3_select(window)
.on('click.drag', null);
}
}
function behavior(selection) {
var matchesSelector = utilPrefixDOMProperty('matchesSelector');
var delegate = dragstart;
if (_selector) {
delegate = function() {
var root = this;
var target = d3_event.target;
for (; target && target !== root; target = target.parentNode) {
var datum = target.__data__;
var entity = datum instanceof osmNote ? datum
: datum && datum.properties && datum.properties.entity;
if (entity && target[matchesSelector](_selector)) {
return dragstart.call(target, entity);
}
}
};
}
selection
.on(_pointerPrefix + 'down.drag' + _selector, delegate);
}
behavior.off = function(selection) {
selection
.on(_pointerPrefix + 'down.drag' + _selector, null);
};
behavior.selector = function(_) {
if (!arguments.length) return _selector;
_selector = _;
return behavior;
};
behavior.origin = function(_) {
if (!arguments.length) return _origin;
_origin = _;
return behavior;
};
behavior.cancel = function() {
d3_select(window)
.on(_pointerPrefix + 'move.drag', null)
.on(_pointerPrefix + 'up.drag', null);
return behavior;
};
behavior.target = function() {
if (!arguments.length) return _target;
_target = arguments[0];
_event = eventOf(_target, Array.prototype.slice.call(arguments, 1));
return behavior;
};
behavior.surface = function() {
if (!arguments.length) return _surface;
_surface = arguments[0];
return behavior;
};
return utilRebind(behavior, dispatch, 'on');
}