-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag-tracker.js
162 lines (135 loc) · 4.85 KB
/
drag-tracker.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
function dragTracker(options) {
"use strict";
//Element.closest polyfill:
//https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
const ep = Element.prototype;
if (!ep.matches)
ep.matches = ep.msMatchesSelector || ep.webkitMatchesSelector;
if (!ep.closest)
ep.closest = function(s) {
var node = this;
do {
if(node.matches(s)) return node;
//https://github.com/Financial-Times/polyfill-service/issues/1279
node = (node.tagName === 'svg') ? node.parentNode : node.parentElement;
} while(node);
return null;
};
options = options || {};
const container = options.container || document.documentElement,
callback = options.callback || console.log,
callbackStart = options.callbackDragStart,
callbackEnd = options.callbackDragEnd,
selector = options.selector,
//handleOffset: "center", true (default), false
handleOffset = options.handleOffset || (options.handleOffset !== false),
roundCoords = (options.roundCoords !== false),
dragOutside = (options.dragOutside !== false)
;
//Whether callback coordinates should be the dragged element's center instead of the top-left corner
let offsetToCenter = null;
switch(handleOffset) {
case 'center':
offsetToCenter = true; break;
case 'topleft':
case 'top-left':
offsetToCenter = false; break;
}
let dragged, mouseOffset, dragStart;
function getMousePos(e, elm, offset, stayWithin) {
let x = e.clientX,
y = e.clientY;
function respectBounds(value, min, max) {
return Math.max(min, Math.min(value, max));
}
if(elm) {
const bounds = elm.getBoundingClientRect();
x -= bounds.left;
y -= bounds.top;
if(offset) {
x -= offset[0];
y -= offset[1];
}
if(stayWithin) {
x = respectBounds(x, 0, bounds.width);
y = respectBounds(y, 0, bounds.height);
}
//Adjust the mouseOffset on the dragged element
//if the element is positioned by its center:
if(elm !== container) {
const center = (offsetToCenter !== null)
? offsetToCenter
//SVG circles and ellipses are positioned by their center (cx/cy), not the top-left corner:
: (elm.nodeName === 'circle') || (elm.nodeName === 'ellipse');
if(center) {
x -= bounds.width/2;
y -= bounds.height/2;
}
}
}
return (roundCoords ? [Math.round(x), Math.round(y)] : [x, y]);
}
function onDown(e) {
dragged = selector ? e.target.closest(selector) : {};
if(dragged) {
e.preventDefault();
mouseOffset = (selector && handleOffset) ? getMousePos(e, dragged) : [0, 0];
dragStart = getMousePos(e, container, mouseOffset);
if(roundCoords) { dragStart = dragStart.map(Math.round); }
if(callbackStart) {
callbackStart(dragged, dragStart);
}
}
}
function onMove(e) {
if(!dragged) { return; }
e.preventDefault();
const pos = getMousePos(e, container, mouseOffset, !dragOutside);
callback(dragged, pos, dragStart);
}
function onEnd(e) {
if(!dragged) { return; }
if(callbackEnd) {
const pos = getMousePos(e, container, mouseOffset, !dragOutside);
callbackEnd(dragged, pos, dragStart);
}
dragged = null;
}
/* Mouse/touch input */
container.addEventListener('mousedown', function(e) {
if(isLeftButton(e)) { onDown(e); }
});
container.addEventListener('touchstart', function(e) {
onDown(tweakTouch(e));
});
window.addEventListener('mousemove', function(e) {
if(!dragged) { return; }
if(isLeftButton(e)) { onMove(e); }
//"mouseup" outside of window
else { onEnd(e); }
});
window.addEventListener('touchmove', function(e) {
onMove(tweakTouch(e));
});
container.addEventListener('mouseup', function(e) {
//Here we check that the left button is *no longer* pressed:
if(!isLeftButton(e)) { onEnd(e); }
});
function onTouchEnd(e) { onEnd(tweakTouch(e)); }
container.addEventListener('touchend', onTouchEnd);
container.addEventListener('touchcancel', onTouchEnd);
function isLeftButton(e) {
return (e.buttons !== undefined)
? (e.buttons === 1)
//Safari (not tested):
//https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#Browser_compatibility
: (e.which === 1);
}
function tweakTouch(e) {
let touch = e.targetTouches[0];
//touchend:
if(!touch) { touch = e.changedTouches[0]; }
touch.preventDefault = e.preventDefault.bind(e);
return touch;
}
}