Skip to content

Commit 7681b14

Browse files
committed
Added optional modifier keys
1 parent 17b29b9 commit 7681b14

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,21 @@ plugins: {
5757
// Minimal pan distance required before actually applying pan
5858
threshold: 10,
5959

60+
// Required modifier key for panning. Default null (no key required)
61+
// Can be one of 'ctrl', 'shift', 'alt', 'meta'
62+
modifierKey: null,
63+
64+
// Usually non mouse input implie no keyboard,
65+
// so by default modifier key is only required on mouse.
66+
requireModifierNonMouse: false,
67+
6068
// Function called while the user is panning
6169
onPan: function({chart}) { console.log(`I'm panning!!!`); },
6270
// Function called once panning is completed
63-
onPanComplete: function({chart}) { console.log(`I was panned!!!`); }
71+
onPanComplete: function({chart}) { console.log(`I was panned!!!`); },
72+
// Function called when pan fails because modifier key was not detected.
73+
// event is the a hammer event that failed - see https://hammerjs.github.io/api#event-object
74+
onModifierKeyFailed: function({chart, event}) { console.log(`I didn't start panning!`); }
6475
},
6576

6677
// Container for zoom options
@@ -109,10 +120,17 @@ plugins: {
109120
// On category scale, minimal zoom level before actually applying zoom
110121
sensitivity: 3,
111122

123+
// Prevent accidental capture of scroll events.
124+
// Can be one of 'ctrl', 'alt', 'shift', or 'meta'
125+
// Default null (no modifier key needed)
126+
wheelModifierKey: 'ctrl',
127+
112128
// Function called while the user is zooming
113129
onZoom: function({chart}) { console.log(`I'm zooming!!!`); },
114130
// Function called once zooming is completed
115-
onZoomComplete: function({chart}) { console.log(`I was zoomed!!!`); }
131+
onZoomComplete: function({chart}) { console.log(`I was zoomed!!!`); },
132+
// Function called when wheel input occurs without modifier key
133+
onWheelModifierFailed: function({chart, event}) { console.log(`I didn't start zooming!`); }
116134
}
117135
}
118136
}

src/plugin.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ Chart.Zoom.defaults = Chart.defaults.global.plugins.zoom = {
1717
enabled: false,
1818
mode: 'xy',
1919
speed: 20,
20-
threshold: 10
20+
threshold: 10,
21+
modifierKey: null,
22+
requireModifierNonMouse: false,
2123
},
2224
zoom: {
2325
enabled: false,
2426
mode: 'xy',
2527
sensitivity: 3,
26-
speed: 0.1
28+
speed: 0.1,
29+
wheelModifierKey: null
2730
}
2831
};
2932

@@ -510,6 +513,20 @@ var zoomPlugin = {
510513

511514
var _scrollTimeout = null;
512515
chartInstance.$zoom._wheelHandler = function(event) {
516+
var zoomOptions = chartInstance.$zoom._options.zoom;
517+
518+
if (zoomOptions
519+
&& zoomOptions.wheelModifierKey
520+
&& !event[zoomOptions.wheelModifierKey + 'Key']) {
521+
if (typeof zoomOptions.onWheelModifierKeyFailed === 'function') {
522+
zoomOptions.onWheelModifierKeyFailed({
523+
chart: chartInstance,
524+
event: event
525+
});
526+
}
527+
return;
528+
}
529+
513530
// Prevent the event from triggering the default behavior (eg. Content scrolling).
514531
if (event.cancelable) {
515532
event.preventDefault();
@@ -530,7 +547,6 @@ var zoomPlugin = {
530547
y: offsetY
531548
};
532549

533-
var zoomOptions = chartInstance.$zoom._options.zoom;
534550
var speedPercent = zoomOptions.speed;
535551

536552
if (event.deltaY >= 0) {
@@ -547,10 +563,33 @@ var zoomPlugin = {
547563
};
548564

549565
if (Hammer) {
566+
var panEnabler = function(recognizer, event) {
567+
const panOptions = chartInstance.$zoom._options.pan;
568+
if (!panOptions || !panOptions.enabled) {
569+
return false;
570+
}
571+
if (!event || !event.srcEvent) { // Sometimes Hammer queries this with a null event.
572+
return true;
573+
}
574+
const requireModifier = panOptions.modifierKey
575+
&& (panOptions.requireModifierNonMouse || event.pointerType === 'mouse');
576+
if (requireModifier && !event.srcEvent[panOptions.modifierKey + 'Key']) {
577+
if (typeof panOptions.onModifierKeyFailed === 'function') {
578+
panOptions.onModifierKeyFailed({
579+
chart: chartInstance,
580+
event: event
581+
});
582+
}
583+
return false;
584+
}
585+
return true;
586+
};
587+
550588
var mc = new Hammer.Manager(node);
551589
mc.add(new Hammer.Pinch());
552590
mc.add(new Hammer.Pan({
553-
threshold: panThreshold
591+
threshold: panThreshold,
592+
enable: panEnabler
554593
}));
555594

556595
// Hammer reports the total scaling. We need the incremental amount

test/specs/defaults.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ describe('defaults', function() {
66
enabled: false,
77
mode: 'xy',
88
speed: 20,
9-
threshold: 10
9+
threshold: 10,
10+
modifierKey: null,
11+
requireModifierNonMouse: false
1012
},
1113
zoom: {
1214
enabled: false,
1315
mode: 'xy',
1416
sensitivity: 3,
15-
speed: 0.1
17+
speed: 0.1,
18+
wheelModifierKey: null
1619
}
1720
};
1821

0 commit comments

Comments
 (0)