Skip to content

Commit 24bdbc4

Browse files
committed
allow dragging outside the window, by listening to document instead of coverslip
1 parent 5ee8db9 commit 24bdbc4

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/components/dragelement/index.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ dragElement.unhoverRaw = unhover.raw;
5858
* e is the original event
5959
*/
6060
dragElement.init = function init(options) {
61-
var gd = options.gd,
62-
numClicks = 1,
63-
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
64-
startX,
61+
var gd = options.gd;
62+
var numClicks = 1;
63+
var DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;
64+
var element = options.element;
65+
66+
var startX,
6567
startY,
6668
newMouseDownTime,
6769
cursor,
@@ -70,10 +72,10 @@ dragElement.init = function init(options) {
7072

7173
if(!gd._mouseDownTime) gd._mouseDownTime = 0;
7274

73-
options.element.style.pointerEvents = 'all';
75+
element.style.pointerEvents = 'all';
7476

75-
options.element.onmousedown = onStart;
76-
options.element.ontouchstart = onStart;
77+
element.onmousedown = onStart;
78+
element.ontouchstart = onStart;
7779

7880
function onStart(e) {
7981
// make dragging and dragged into properties of gd
@@ -100,29 +102,28 @@ dragElement.init = function init(options) {
100102

101103
if(hasHover) {
102104
dragCover = coverSlip();
103-
dragCover.style.cursor = window.getComputedStyle(options.element).cursor;
105+
dragCover.style.cursor = window.getComputedStyle(element).cursor;
104106
}
105107
else {
106108
// document acts as a dragcover for mobile, bc we can't create dragcover dynamically
107109
dragCover = document;
108110
cursor = window.getComputedStyle(document.documentElement).cursor;
109-
document.documentElement.style.cursor = window.getComputedStyle(options.element).cursor;
111+
document.documentElement.style.cursor = window.getComputedStyle(element).cursor;
110112
}
111113

112-
dragCover.addEventListener('mousemove', onMove);
113-
dragCover.addEventListener('mouseup', onDone);
114-
dragCover.addEventListener('mouseout', onDone);
115-
dragCover.addEventListener('touchmove', onMove);
116-
dragCover.addEventListener('touchend', onDone);
114+
document.addEventListener('mousemove', onMove);
115+
document.addEventListener('mouseup', onDone);
116+
document.addEventListener('touchmove', onMove);
117+
document.addEventListener('touchend', onDone);
117118

118119
return Lib.pauseEvent(e);
119120
}
120121

121122
function onMove(e) {
122-
var offset = pointerOffset(e),
123-
dx = offset[0] - startX,
124-
dy = offset[1] - startY,
125-
minDrag = options.minDrag || constants.MINDRAG;
123+
var offset = pointerOffset(e);
124+
var dx = offset[0] - startX;
125+
var dy = offset[1] - startY;
126+
var minDrag = options.minDrag || constants.MINDRAG;
126127

127128
if(Math.abs(dx) < minDrag) dx = 0;
128129
if(Math.abs(dy) < minDrag) dy = 0;
@@ -137,11 +138,10 @@ dragElement.init = function init(options) {
137138
}
138139

139140
function onDone(e) {
140-
dragCover.removeEventListener('mousemove', onMove);
141-
dragCover.removeEventListener('mouseup', onDone);
142-
dragCover.removeEventListener('mouseout', onDone);
143-
dragCover.removeEventListener('touchmove', onMove);
144-
dragCover.removeEventListener('touchend', onDone);
141+
document.removeEventListener('mousemove', onMove);
142+
document.removeEventListener('mouseup', onDone);
143+
document.removeEventListener('touchmove', onMove);
144+
document.removeEventListener('touchend', onDone);
145145

146146
if(hasHover) {
147147
Lib.removeElement(dragCover);

test/jasmine/tests/dragelement_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('dragElement', function() {
7777
expect(args[2]).toBe(true);
7878
});
7979

80-
it('should pass dragged and numClicks to doneFn on mouseup & mouseout', function() {
80+
it('should pass dragged and numClicks to doneFn on mouseup', function() {
8181
var args = [];
8282
var options = {
8383
element: this.element,
@@ -96,7 +96,7 @@ describe('dragElement', function() {
9696

9797
mouseEvent('mousedown', this.x, this.y);
9898
mouseEvent('mousemove', this.x + 10, this.y + 10);
99-
mouseEvent('mouseout', this.x, this.y);
99+
mouseEvent('mouseup', this.x, this.y);
100100

101101
expect(args[0]).toBe(true);
102102
expect(args[1]).toEqual(2);
@@ -118,7 +118,7 @@ describe('dragElement', function() {
118118
mouseEvent('mousemove', this.x + 10, this.y + 10);
119119
expect(countCoverSlip()).toEqual(1);
120120

121-
mouseEvent('mouseout', this.x, this.y);
121+
mouseEvent('mouseup', this.x, this.y);
122122
expect(countCoverSlip()).toEqual(0);
123123
});
124124

0 commit comments

Comments
 (0)