Skip to content

Commit a32a21a

Browse files
committedJan 18, 2018
Add passive events detection
1 parent 26e6df1 commit a32a21a

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed
 

‎src/components/dragelement/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var unhover = require('./unhover');
2727
dragElement.unhover = unhover.wrapped;
2828
dragElement.unhoverRaw = unhover.raw;
2929

30+
var supportsPassive = Lib.eventListenerOptionsSupported();
31+
3032
/**
3133
* Abstracts click & drag interactions
3234
*
@@ -102,11 +104,16 @@ dragElement.init = function init(options) {
102104

103105
element.onmousedown = onStart;
104106

105-
if(element._ontouchstart) {
106-
element.removeEventListener('touchstart', element._ontouchstart);
107+
if(!supportsPassive) {
108+
element.ontouchstart = onStart;
109+
}
110+
else {
111+
if(element._ontouchstart) {
112+
element.removeEventListener('touchstart', element._ontouchstart);
113+
}
114+
element._ontouchstart = onStart;
115+
element.addEventListener('touchstart', onStart, {passive: false});
107116
}
108-
element._ontouchstart = onStart;
109-
element.addEventListener('touchstart', onStart, {passive: false});
110117

111118
function _clampFn(dx, dy, minDrag) {
112119
if(Math.abs(dx) < minDrag) dx = 0;

‎src/lib/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,25 @@ lib.subplotSort = function(a, b) {
890890
}
891891
return numB - numA;
892892
};
893+
894+
/*
895+
* test if event listener options supported
896+
*/
897+
lib.eventListenerOptionsSupported = function() {
898+
var supported = false;
899+
900+
try {
901+
var opts = Object.defineProperty({}, 'passive', {
902+
get: function() {
903+
supported = true;
904+
}
905+
});
906+
907+
window.addEventListener('test', null, opts);
908+
window.removeEventListener('test', null, opts);
909+
} catch(e) {
910+
supported = false;
911+
}
912+
913+
return supported;
914+
};

‎src/plots/cartesian/dragbox.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var constants = require('./constants');
3434
var MINDRAG = constants.MINDRAG;
3535
var MINZOOM = constants.MINZOOM;
3636

37+
var supportsPassive = Lib.eventListenerOptionsSupported();
38+
3739
// flag for showing "doubleclick to zoom out" only at the beginning
3840
var SHOWZOOMOUTTIP = true;
3941

@@ -1041,14 +1043,20 @@ function calcLinks(constraintGroups, xIDs, yIDs) {
10411043

10421044
// still seems to be some confusion about onwheel vs onmousewheel...
10431045
function attachWheelEventHandler(element, handler) {
1044-
var wheelEventName = element.onwheel !== undefined ? 'wheel' : 'mousewheel';
1045-
1046-
if(element._onwheel) {
1047-
element.removeEventListener(wheelEventName, element._onwheel);
1046+
if(!supportsPassive) {
1047+
if(element.onwheel !== undefined) element.onwheel = handler;
1048+
else if(element.onmousewheel !== undefined) element.onmousewheel = handler;
10481049
}
1049-
element._onwheel = handler;
1050+
else {
1051+
var wheelEventName = element.onwheel !== undefined ? 'wheel' : 'mousewheel';
10501052

1051-
element.addEventListener(wheelEventName, handler, {passive: false});
1053+
if(element._onwheel) {
1054+
element.removeEventListener(wheelEventName, element._onwheel);
1055+
}
1056+
element._onwheel = handler;
1057+
1058+
element.addEventListener(wheelEventName, handler, {passive: false});
1059+
}
10521060
}
10531061

10541062
module.exports = {

0 commit comments

Comments
 (0)
Please sign in to comment.