/
dragToScroll.js
256 lines (217 loc) · 5.79 KB
/
dragToScroll.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { BasePlugin } from '../base';
import { isRightClick } from '../../helpers/dom/event';
import { getParentWindow } from '../../helpers/dom/element';
export const PLUGIN_KEY = 'dragToScroll';
export const PLUGIN_PRIORITY = 100;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @description
* Plugin used to scroll Handsontable by selecting a cell and dragging outside of the visible viewport.
*
*
* @class DragToScroll
* @plugin DragToScroll
*/
export class DragToScroll extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
/**
* Size of an element and its position relative to the viewport,
* e.g. {bottom: 449, height: 441, left: 8, right: 814, top: 8, width: 806, x: 8, y:8}.
*
* @type {DOMRect}
*/
boundaries = null;
/**
* Callback function.
*
* @private
* @type {Function}
*/
callback = null;
/**
* Flag indicates mouseDown/mouseUp.
*
* @private
* @type {boolean}
*/
listening = false;
/**
* Checks if the plugin is enabled in the handsontable settings. This method is executed in {@link Hooks#beforeInit}
* hook and if it returns `true` then the {@link DragToScroll#enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return !!this.hot.getSettings()[PLUGIN_KEY];
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
this.addHook('afterOnCellMouseDown', event => this.#setupListening(event));
this.addHook('afterOnCellCornerMouseDown', event => this.#setupListening(event));
this.registerEvents();
super.enablePlugin();
}
/**
* Updates the plugin's state.
*
* This method is executed when [`updateSettings()`](@/api/core.md#updatesettings) is invoked with any of the following configuration options:
* - [`dragToScroll`](@/api/options.md#dragtoscroll)
*/
updatePlugin() {
this.disablePlugin();
this.enablePlugin();
super.updatePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.unregisterEvents();
super.disablePlugin();
}
/**
* Sets the value of the visible element.
*
* @param {DOMRect|{left: number, right: number, top: number, bottom: number}} [boundaries] An object with
* coordinates. Contains the window boundaries by default. The object is compatible with DOMRect.
*/
setBoundaries(boundaries = {
left: 0,
right: this.hot.rootWindow.innerWidth,
top: 0,
bottom: this.hot.rootWindow.innerHeight,
}) {
this.boundaries = boundaries;
}
/**
* Changes callback function.
*
* @param {Function} callback The callback function.
*/
setCallback(callback) {
this.callback = callback;
}
/**
* Checks if the mouse position (X, Y) is outside the viewport and fires a callback with calculated X an Y diffs
* between passed boundaries.
*
* @param {number} x Mouse X coordinate to check.
* @param {number} y Mouse Y coordinate to check.
*/
check(x, y) {
let diffX = 0;
let diffY = 0;
if (y < this.boundaries.top) {
// y is less than top
diffY = y - this.boundaries.top;
} else if (y > this.boundaries.bottom) {
// y is more than bottom
diffY = y - this.boundaries.bottom;
}
if (x < this.boundaries.left) {
// x is less than left
diffX = x - this.boundaries.left;
} else if (x > this.boundaries.right) {
// x is more than right
diffX = x - this.boundaries.right;
}
this.callback(diffX, diffY);
}
/**
* Enables listening on `mousemove` event.
*
* @private
*/
listen() {
this.listening = true;
}
/**
* Disables listening on `mousemove` event.
*
* @private
*/
unlisten() {
this.listening = false;
}
/**
* Returns current state of listening.
*
* @private
* @returns {boolean}
*/
isListening() {
return this.listening;
}
/**
* Registers dom listeners.
*
* @private
*/
registerEvents() {
const { rootWindow } = this.hot;
let frame = rootWindow;
while (frame) {
this.eventManager.addEventListener(frame.document, 'contextmenu', () => this.unlisten());
this.eventManager.addEventListener(frame.document, 'mouseup', () => this.unlisten());
this.eventManager.addEventListener(frame.document, 'mousemove', event => this.onMouseMove(event));
frame = getParentWindow(frame);
}
}
/**
* Unbinds the events used by the plugin.
*
* @private
*/
unregisterEvents() {
this.eventManager.clear();
}
/**
* On after on cell/cellCorner mouse down listener.
*
* @param {MouseEvent} event The mouse event object.
*/
#setupListening(event) {
if (isRightClick(event)) {
return;
}
const scrollHandler = this.hot.view._wt.wtOverlays.topOverlay.mainTableScrollableElement;
this.setBoundaries(scrollHandler !== this.hot.rootWindow ? scrollHandler.getBoundingClientRect() : undefined);
this.setCallback((scrollX, scrollY) => {
const horizontalScrollValue = scrollHandler.scrollLeft ?? scrollHandler.scrollX;
const verticalScrollValue = scrollHandler.scrollTop ?? scrollHandler.scrollY;
scrollHandler.scroll(
horizontalScrollValue + (Math.sign(scrollX) * 50),
verticalScrollValue + (Math.sign(scrollY) * 20)
);
});
this.listen();
}
/**
* 'mouseMove' event callback.
*
* @private
* @param {MouseEvent} event `mousemove` event properties.
*/
onMouseMove(event) {
if (!this.isListening()) {
return;
}
this.check(event.clientX, event.clientY);
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}