Skip to content

Commit

Permalink
Fixed wrong IE event processing
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmat1k committed Jun 1, 2015
1 parent 766d749 commit 360492e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 29 deletions.
2 changes: 0 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
},
preventMouse: false
});

indicator.
</script>
</body>
</html>
21 changes: 12 additions & 9 deletions lib/wheel-indicator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Generates event when user makes new movement (like a swipe on a touchscreen).
* @version 1.0.3
* @version 1.0.4
* @link https://github.com/Promo/wheel-indicator
* @license MIT
*/
Expand Down Expand Up @@ -92,15 +92,16 @@ var WheelIndicator = (function(win, doc) {
}

var getDeltaY = function(event){
if(event.wheelDelta) {
if (event.wheelDelta) {
getDeltaY = function(event) {
return event.wheelDelta;
}
return event.wheelDelta * -1;
};
} else {
getDeltaY = function(event) {
return event.deltaY;
}
};
}

return getDeltaY(event);
};

Expand All @@ -117,8 +118,8 @@ var WheelIndicator = (function(win, doc) {
function processDelta(event) {
var
self = this,
delta = event.deltaY,
direction = event.deltaY > 0 ? 'down' : 'up',
delta = getDeltaY(event),
direction = delta > 0 ? 'down' : 'up',
arrayLength = self._deltaArray.length,
changedDirection = false,
repeatDirection = 0,
Expand Down Expand Up @@ -152,10 +153,11 @@ var WheelIndicator = (function(win, doc) {
}
}

//если колесо в движение и данное событие дельты не первое в массиве
//если колесо в движении и данное событие дельты не первое в массиве
if(!self._isStopped){
if(changedDirection) {
self._isAcceleration = true;

triggerEvent.call(this, event);
} else {
//делаем проверку если только направление движение стабильно в одну сторону
Expand All @@ -174,6 +176,7 @@ var WheelIndicator = (function(win, doc) {
self._isStopped = false;
self._isAcceleration = true;
self._direction = direction;

triggerEvent.call(this, event);
}

Expand All @@ -186,7 +189,7 @@ var WheelIndicator = (function(win, doc) {
deltaArray0Abs = Math.abs(this._deltaArray[0]),
deltaArray1Abs = Math.abs(this._deltaArray[1]),
deltaArray2Abs = Math.abs(this._deltaArray[2]),
deltaAbs = Math.abs(event.deltaY);
deltaAbs = Math.abs(getDeltaY(event));

if((deltaAbs > deltaArray2Abs) &&
(deltaArray2Abs > deltaArray1Abs) &&
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wheel-indicator",
"version": "1.0.3",
"version": "1.0.4",
"description": "normalizes an inertial mousewheel",
"main": "lib/wheel-indicator",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions test/algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Algorithm', function() {
}
};

delete require.cache[require.resolve('../lib/wheel-indicator')];
var WheelIndicator = require('../lib/wheel-indicator');

Object.keys(testData).forEach(function(key){
Expand Down
34 changes: 17 additions & 17 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
require('should');

describe('Methods', function(){
before(function(){
delete require.cache[require.resolve('../lib/wheel-indicator')];
global.document = {
addEventListener: function(type, handler){
setTimeout(function(){
handler({
deltaY: testData.down.delta
});
}, 0);
}
};
var WheelIndicator = require('../lib/wheel-indicator')
});

var testData = {
down: {
moves: [ 'down' ],
delta: [ 0.025,0.225,0.675,1.75,2.2,3.4,3.475,4.275,3.65,7.3,6.625,6.075,5.6,5.225,4.875,4.6,4.325,4.191666666666666,4.066666666666666,3.95,3.775,3.65,3.575,3.4,3.3,3.15,3.025,2.925,5.475,2.55,2.45,2.325,2.225,2.091666666666667,1.975,1.85,1.75,1.625,1.525,1.425,1.35,1.25,1.15,1.0416666666666667,0.975,0.9,0.825,0.775,0.675,0.625,0.575,0.5166666666666667,0.475,0.45,0.4,0.375,0.325,0.3,0.275,0.25,0.225,0.2,0.175,0.175,0.15,0.125,0.125,0.1,0.1,0.1,0.075,0.075,0.075,0.05,0.05,0.05,0.05,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025 ],
device: 'Mac OSX notebook trackpad'
},
twiceDown: {
moves: [ 'down', 'down' ],
delta: [0.05,0.425,0.7,1.225,1.5,2.35,1.8,1.675,1.825,2.075,1.85,4.425,4.15,3.9,3.7,3.45,3.25,3.075,2.9,2.775,2.7,2.575,2.425,2.325,2.225,2.091666666666667,1.975,1.85,1.775,1.625,0.025,0.175,0.3,0.425,0.6,1.45,2.05,2.225,2.9,3.025,5.8,5.375,4.9,4.575,4.275,4.025,3.8,3.575,3.45,3.325,3.2,3.075,2.95,2.85,2.725,2.625,2.5,2.4,2.25,2.125,2.025,1.925,1.8,1.675,1.575,1.475,1.375,1.3,1.2,1.1,1,0.925,0.875,0.775,0.725,0.675,0.575,0.5166666666666667,0.5,0.45,0.4,0.375,0.325,0.3,0.275,0.25,0.225,0.2,0.175,0.175,0.15,0.15,0.125,0.125,0.1,0.1,0.1,0.075,0.075,0.075,0.05,0.05,0.05,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025,0.025],
device: 'Mac OSX notebook trackpad'
},
up: {
moves: [ 'up' ],
delta: [-0.025,-0.1,-0.2,-0.475,-0.725,-1.4,-1.575,-2.025,-3.225,-3.825,-3.375,-6.55,-5.975,-5.475,-5.1,-4.725,-4.425,-4.15,-3.925,-3.8,-3.675,-3.55,-3.425,-3.325,-3.175,-3.05,-2.95,-2.8,-2.675,-2.6,-2.45,-2.375,-2.225,-2.091666666666667,-1.975,-1.9,-1.775,-1.675,-1.55,-1.45,-1.35,-1.25,-1.15,-1.0416666666666667,-1,-0.9,-0.825,-0.775,-0.7,-0.625,-0.575,-0.5166666666666667,-0.475,-0.45,-0.4,-0.375,-0.325,-0.3,-0.275,-0.25,-0.225,-0.2,-0.175,-0.175,-0.15,-0.125,-0.125,-0.1,-0.1,-0.1,-0.075,-0.075,-0.075,-0.05,-0.05,-0.05,-0.05,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025],
device: 'Mac OSX notebook trackpad'
},
twiceUp: {
moves: [ 'up', 'up' ],
delta: [-0.025,-0.15,-0.25,-0.475,-0.75,-0.95,-1.475,-1.575,-2.15,-2.325,-1.975,-1.975,-1.7,-1.6,-0.975,-3.8,-3.6,-3.425,-3.25,-3.075,-2.875,-2.725,-2.6,-2.45,-2.35,-2.225,-2.091666666666667,-2.025,-1.9,-0.05,-0.4,-0.7,-1,-2.05,-2.6,-2.925,-2.575,-3.225,-6.05,-5.575,-5.125,-4.725,-4.45,-4.175,-3.925,-3.675,-3.6,-3.45,-3.325,-3.225,-3.075,-2.975,-2.825,-2.7,-2.625,-2.5,-2.35,-2.25,-2.15,-2.025,-1.9,-1.8,-1.675,-1.575,-1.475,-1.35,-1.25,-1.2,-1.1,-1,-0.925,-0.85,-0.775,-0.725,-0.675,-0.575,-0.5166666666666667,-0.5,-0.45,-0.4,-0.375,-0.325,-0.3,-0.275,-0.25,-0.225,-0.2,-0.175,-0.175,-0.15,-0.15,-0.125,-0.125,-0.1,-0.1,-0.1,-0.075,-0.075,-0.075,-0.05,-0.05,-0.05,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025,-0.025],
device: 'Mac OSX notebook trackpad'
}
},
currentDeltaArr;
};

global.window = {
addEventListener: function(){}
Expand All @@ -43,6 +41,8 @@ describe('Methods', function(){
}
};

delete require.cache[require.resolve('../lib/wheel-indicator')];

var WheelIndicator = require('../lib/wheel-indicator'),
result = [];

Expand Down
1 change: 1 addition & 0 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('Options', function(){
}
};

delete require.cache[require.resolve('../lib/wheel-indicator')];
var WheelIndicator = require('../lib/wheel-indicator');

new WheelIndicator({
Expand Down
66 changes: 66 additions & 0 deletions test/wheelDelta-event-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* global describe, it, global */

'use strict';

require('should');

describe('wheelDelta event object', function() {
var testData = {
down: {
moves: [ 'down' ],
delta: [ -120 ],
device: 'Mac OSX opera 30 | notebook trackpad'
}
},
currentDeltaArr;

global.window = {
addEventListener: function(){}
};

global.document = {
addEventListener: function(type, handler){
currentDeltaArr.forEach(function(delta){
handler({
//ie contain wheelDelta instead of deltaY
wheelDelta: delta
});
});
}
};

delete require.cache[require.resolve('../lib/wheel-indicator')];
var WheelIndicator = require('../lib/wheel-indicator');

Object.keys(testData).forEach(function(key){
var test = testData[key],
result = [];

currentDeltaArr = test['delta'];

new WheelIndicator({
elem: document,
callback: function(e){
result.push(e.direction);
}
});

it('Test: ' + key + ', Env: ' + test.device + ', moves: ' + test['moves'], function () {
result.should.be.eql(test['moves']);
});
});

global.document = {
addEventListener: function(type, handler){
currentDeltaArr.forEach(function(delta){
handler({
//ie contain wheelDelta instead of deltaY
deltaY: delta
});
});
}
};
console.log('afff');
delete require.cache[require.resolve('../lib/wheel-indicator')];
WheelIndicator = require('../lib/wheel-indicator');
});

0 comments on commit 360492e

Please sign in to comment.