-
Notifications
You must be signed in to change notification settings - Fork 26
/
scroll_on_wheel_handler.js
112 lines (99 loc) · 2.68 KB
/
scroll_on_wheel_handler.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
/**
* Visual Blocks Editor
*
* Copyright 2011 Google Inc.
* http://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Scroll-BlockSpace-on-mousewheel interaction handler.
*/
'use strict';
goog.provide('Blockly.ScrollOnWheelHandler');
/**
* @param {!Blockly.BlockSpace} blockSpace
* @constructor
*/
Blockly.ScrollOnWheelHandler = function(blockSpace) {
/**
* @private {Blockly.BlockSpace}
*/
this.blockSpace_ = blockSpace;
/**
* @private {BindData}
*/
this.wheelEventBindData_ = null;
/**
* (for Safari)
* @private {BindData}
*/
this.mousewheelEventBindData_ = null;
};
/**
* Establish a mousewheel handler on the given wheelTarget that will
* @param {!EventTarget} wheelTarget
*/
Blockly.ScrollOnWheelHandler.prototype.bindTo = function(wheelTarget) {
this.unbindWheelScrollHandler();
this.wheelEventBindData_ = Blockly.bindEvent_(
wheelTarget,
'wheel',
this,
this.onWheel_
);
// Safari uses 'mousewheel'
this.mousewheelEventBindData_ = Blockly.bindEvent_(
wheelTarget,
'mousewheel',
this,
this.onWheel_
);
};
/**
* Unbinds previously bound handler to begin pan-drag. Safe to call if no
* such handler is bound.
*/
Blockly.ScrollOnWheelHandler.prototype.unbindWheelScrollHandler = function() {
if (this.wheelEventBindData_) {
Blockly.unbindEvent_(this.wheelEventBindData_);
this.wheelEventBindData_ = null;
}
if (this.mousewheelEventBindData_) {
Blockly.unbindEvent_(this.mousewheelEventBindData_);
this.mousewheelEventBindData_ = null;
}
};
/**
* Scroll the BlockSpace up or down based on wheel scrolling.
* @param {!Event} e Mouse wheel scroll event.
* @private
*/
Blockly.ScrollOnWheelHandler.prototype.onWheel_ = function(e) {
if (!this.blockSpace_.scrollbarPair) {
return;
}
// + is down
var wheelDelta = Blockly.getNormalizedWheelDeltaY(e);
if (wheelDelta) {
this.blockSpace_.scrollWithAnySelectedBlock(
this.blockSpace_.getScrollOffsetX(),
this.blockSpace_.getScrollOffsetY() + wheelDelta,
e.clientX,
e.clientY
);
// Don't scroll the page.
e.stopPropagation();
e.preventDefault();
}
};