forked from chartjs/chartjs-plugin-zoom
/
handlers.js
209 lines (179 loc) · 6.3 KB
/
handlers.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
import {directionEnabled, debounce, keyNotPressed, getModifierKey, keyPressed} from './utils';
import {zoom, zoomRect} from './core';
import {callback as call} from 'chart.js/helpers';
import {getState} from './state';
function removeHandler(chart, type) {
const {handlers} = getState(chart);
const handler = handlers[type];
if (handler && handler.target) {
handler.target.removeEventListener(type, handler);
delete handlers[type];
}
}
function addHandler(chart, target, type, handler) {
const {handlers, options} = getState(chart);
const oldHandler = handlers[type];
if (oldHandler && oldHandler.target === target) {
// already attached
return;
}
removeHandler(chart, type);
handlers[type] = (event) => handler(chart, event, options);
handlers[type].target = target;
target.addEventListener(type, handlers[type]);
}
export function mouseMove(chart, event) {
const state = getState(chart);
if (state.dragStart) {
state.dragging = true;
state.dragEnd = event;
chart.update('none');
}
}
function zoomStart(chart, event, zoomOptions) {
const {onZoomStart, onZoomRejected} = zoomOptions;
if (onZoomStart) {
const {left: offsetX, top: offsetY} = event.target.getBoundingClientRect();
const point = {
x: event.clientX - offsetX,
y: event.clientY - offsetY
};
if (call(onZoomStart, [{chart, event, point}]) === false) {
call(onZoomRejected, [{chart, event}]);
return false;
}
}
}
export function mouseDown(chart, event) {
const state = getState(chart);
const {pan: panOptions, zoom: zoomOptions = {}} = state.options;
if (keyPressed(getModifierKey(panOptions), event) || keyNotPressed(getModifierKey(zoomOptions.drag), event)) {
return call(zoomOptions.onZoomRejected, [{chart, event}]);
}
if (zoomStart(chart, event, zoomOptions) === false) {
return;
}
state.dragStart = event;
addHandler(chart, chart.canvas, 'mousemove', mouseMove);
}
export function computeDragRect(chart, mode, beginPoint, endPoint) {
const {left: offsetX, top: offsetY} = beginPoint.target.getBoundingClientRect();
const xEnabled = directionEnabled(mode, 'x', chart);
const yEnabled = directionEnabled(mode, 'y', chart);
let {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
if (xEnabled) {
left = Math.min(beginPoint.clientX, endPoint.clientX) - offsetX;
right = Math.max(beginPoint.clientX, endPoint.clientX) - offsetX;
}
if (yEnabled) {
top = Math.min(beginPoint.clientY, endPoint.clientY) - offsetY;
bottom = Math.max(beginPoint.clientY, endPoint.clientY) - offsetY;
}
const width = right - left;
const height = bottom - top;
return {
left,
top,
right,
bottom,
width,
height,
zoomX: xEnabled && width ? 1 + ((chartWidth - width) / chartWidth) : 1,
zoomY: yEnabled && height ? 1 + ((chartHeight - height) / chartHeight) : 1
};
}
export function mouseUp(chart, event) {
const state = getState(chart);
if (!state.dragStart) {
return;
}
removeHandler(chart, 'mousemove');
const {mode, onZoomComplete, drag: {threshold = 0}} = state.options.zoom;
const rect = computeDragRect(chart, mode, state.dragStart, event);
const distanceX = directionEnabled(mode, 'x', chart) ? rect.width : 0;
const distanceY = directionEnabled(mode, 'y', chart) ? rect.height : 0;
const distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
// Remove drag start and end before chart update to stop drawing selected area
state.dragStart = state.dragEnd = null;
if (distance <= threshold) {
state.dragging = false;
chart.update('none');
return;
}
zoomRect(chart, {x: rect.left, y: rect.top}, {x: rect.right, y: rect.bottom}, 'zoom');
setTimeout(() => (state.dragging = false), 500);
call(onZoomComplete, [{chart}]);
}
function wheelPreconditions(chart, event, zoomOptions) {
// Before preventDefault, check if the modifier key required and pressed
if (keyNotPressed(getModifierKey(zoomOptions.wheel), event)) {
call(zoomOptions.onZoomRejected, [{chart, event}]);
return;
}
if (zoomStart(chart, event, zoomOptions) === false) {
return;
}
// Prevent the event from triggering the default behavior (e.g. content scrolling).
if (event.cancelable) {
event.preventDefault();
}
// Firefox always fires the wheel event twice:
// First without the delta and right after that once with the delta properties.
if (event.deltaY === undefined) {
return;
}
return true;
}
export function wheel(chart, event) {
const {handlers: {onZoomComplete}, options: {zoom: zoomOptions}} = getState(chart);
if (!wheelPreconditions(chart, event, zoomOptions)) {
return;
}
const rect = event.target.getBoundingClientRect();
const speed = 1 + (event.deltaY >= 0 ? -zoomOptions.wheel.speed : zoomOptions.wheel.speed);
const amount = {
x: speed,
y: speed,
focalPoint: {
x: event.clientX - rect.left,
y: event.clientY - rect.top
}
};
zoom(chart, amount);
if (onZoomComplete) {
onZoomComplete();
}
}
function addDebouncedHandler(chart, name, handler, delay) {
if (handler) {
getState(chart).handlers[name] = debounce(() => call(handler, [{chart}]), delay);
}
}
export function addListeners(chart, options) {
const canvas = chart.canvas;
const {wheel: wheelOptions, drag: dragOptions, onZoomComplete} = options.zoom;
// Install listeners. Do this dynamically based on options so that we can turn zoom on and off
// We also want to make sure listeners aren't always on. E.g. if you're scrolling down a page
// and the mouse goes over a chart you don't want it intercepted unless the plugin is enabled
if (wheelOptions.enabled) {
addHandler(chart, canvas, 'wheel', wheel);
addDebouncedHandler(chart, 'onZoomComplete', onZoomComplete, 250);
} else {
removeHandler(chart, 'wheel');
}
if (dragOptions.enabled) {
addHandler(chart, canvas, 'mousedown', mouseDown);
addHandler(chart, canvas.ownerDocument, 'mouseup', mouseUp);
} else {
removeHandler(chart, 'mousedown');
removeHandler(chart, 'mousemove');
removeHandler(chart, 'mouseup');
}
}
export function removeListeners(chart) {
removeHandler(chart, 'mousedown');
removeHandler(chart, 'mousemove');
removeHandler(chart, 'mouseup');
removeHandler(chart, 'wheel');
removeHandler(chart, 'click');
}