-
Notifications
You must be signed in to change notification settings - Fork 41
/
wheel-indicator.js
262 lines (209 loc) · 7.64 KB
/
wheel-indicator.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
257
258
259
260
261
262
/**
* Generates event when user makes new movement (like a swipe on a touchscreen).
* @version 1.2.2
* @link https://github.com/Promo/wheel-indicator
* @license MIT
*/
/* global module, window, document */
var WheelIndicator = (function() {
function Module(options) {
var DEFAULTS = {
callback: function(){},
elem: document,
preventMouse: true
};
this.eventWheel = 'onwheel' in document ? 'wheel' : 'mousewheel';
this._options = extend(DEFAULTS, options);
this._deltaArray = [ 0, 0, 0 ];
this._isAcceleration = false;
this._isStopped = true;
this._direction = '';
this._timer = '';
this._isWorking = true;
var self = this;
this._wheelHandler = function(event) {
if (self._isWorking) {
processDelta.call(self, event);
if (self._options.preventMouse) {
preventDefault(event);
}
}
};
addEvent(this._options.elem, this.eventWheel, this._wheelHandler);
}
Module.prototype = {
constructor: Module,
turnOn: function(){
this._isWorking = true;
return this;
},
turnOff: function(){
this._isWorking = false;
return this;
},
setOptions: function(options){
this._options = extend(this._options, options);
return this;
},
getOption: function(option){
var neededOption = this._options[option];
if (neededOption !== undefined) {
return neededOption;
}
throw new Error('Unknown option');
},
destroy: function(){
removeEvent(this._options.elem, this.eventWheel, this._wheelHandler);
return this;
}
};
function triggerEvent(event){
event.direction = this._direction;
this._options.callback(event);
}
var getDeltaY = function(event){
if (event.wheelDelta && !event.deltaY) {
getDeltaY = function(event) {
return event.wheelDelta * -1;
};
} else {
getDeltaY = function(event) {
return event.deltaY;
};
}
return getDeltaY(event);
};
function preventDefault(event){
event = event || window.event;
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
function processDelta(event) {
var
self = this,
delta = getDeltaY(event);
if (delta === 0) return;
var direction = delta > 0 ? 'down' : 'up',
arrayLength = self._deltaArray.length,
changedDirection = false,
repeatDirection = 0,
sustainableDirection, i;
clearTimeout(self._timer);
self._timer = setTimeout(function() {
self._deltaArray = [ 0, 0, 0 ];
self._isStopped = true;
self._direction = direction;
}, 150);
//check how many of last three deltas correspond to certain direction
for(i = 0; i < arrayLength; i++) {
if(self._deltaArray[i] !== 0) {
self._deltaArray[i] > 0 ? ++repeatDirection : --repeatDirection;
}
}
//if all of last three deltas is greater than 0 or lesser than 0 then direction is switched
if (Math.abs(repeatDirection) === arrayLength) {
//determine type of sustainable direction
//(three positive or negative deltas in a row)
sustainableDirection = repeatDirection > 0 ? 'down' : 'up';
if(sustainableDirection !== self._direction) {
//direction is switched
changedDirection = true;
self._direction = direction;
}
}
//if wheel`s moving and current event is not the first in array
if (!self._isStopped){
if(changedDirection) {
self._isAcceleration = true;
triggerEvent.call(this, event);
} else {
//check only if movement direction is sustainable
if(Math.abs(repeatDirection) === arrayLength) {
//must take deltas to don`t get a bug
//[-116, -109, -103]
//[-109, -103, 1] - new impulse
analyzeArray.call(this, event);
}
}
}
//if wheel is stopped and current delta value is the first in array
if (self._isStopped) {
self._isStopped = false;
self._isAcceleration = true;
self._direction = direction;
triggerEvent.call(this, event);
}
self._deltaArray.shift();
self._deltaArray.push(delta);
}
function analyzeArray(event) {
var
deltaArray0Abs = Math.abs(this._deltaArray[0]),
deltaArray1Abs = Math.abs(this._deltaArray[1]),
deltaArray2Abs = Math.abs(this._deltaArray[2]),
deltaAbs = Math.abs(getDeltaY(event));
if((deltaAbs > deltaArray2Abs) &&
(deltaArray2Abs > deltaArray1Abs) &&
(deltaArray1Abs > deltaArray0Abs)) {
if(!this._isAcceleration) {
triggerEvent.call(this, event);
this._isAcceleration = true;
}
}
if((deltaAbs < deltaArray2Abs) &&
(deltaArray2Abs <= deltaArray1Abs)) {
this._isAcceleration = false;
}
}
function addEvent(elem, type, handler){
if(elem.addEventListener) {
elem.addEventListener(type, handler, isPassiveSupported() ? { passive: false } : false);
} else if (elem.attachEvent) {
elem.attachEvent('on' + type, handler);
}
}
function removeEvent(elem, type, handler) {
if (elem.removeEventListener) {
elem.removeEventListener(type, handler, isPassiveSupported() ? { passive: false } : false);
} else if (elem.detachEvent) {
elem.detachEvent('on'+ type, handler);
}
}
function extend(defaults, options) {
var extended = {},
prop;
for (prop in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = defaults[prop];
}
}
for (prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop)) {
extended[prop] = options[prop];
}
}
return extended;
}
return Module;
}());
if (typeof exports === 'object') {
module.exports = WheelIndicator;
}
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Improving_scrolling_performance_with_passive_listeners
function isPassiveSupported() {
var passiveSupported = false;
try {
var options = Object.defineProperty({}, "passive", {
get: function() {
passiveSupported = true;
}
});
window.addEventListener("test", null, options);
window.removeEventListener("test", null, options);
} catch(err) {}
return passiveSupported;
}