Permalink
Browse files

Merge pull request #2633 from SergioCrisostomo/fix-2598

Fix 2598 - mousewheel dom3 and wheel property
  • Loading branch information...
ibolmo committed Jul 23, 2014
2 parents 1d38828 + 0a27d9f commit 20ed69135451d9c3c80784ea040d67375c943010
Showing with 110 additions and 5 deletions.
  1. +1 −1 Source/Element/Element.Event.js
  2. +13 −4 Source/Types/DOMEvent.js
  3. +96 −0 Specs/Element/Element.Event.js
@@ -130,7 +130,7 @@ Element.Properties.events = {set: function(events){
Element.NativeEvents = {
click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons
mousewheel: 2, DOMMouseScroll: 2, //mouse wheel
wheel: 2, mousewheel: 2, DOMMouseScroll: 2, //mouse wheel
mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement
keydown: 2, keypress: 2, keyup: 2, //keyboard
orientationchange: 2, // mobile
View
@@ -17,6 +17,16 @@ provides: Event
(function(){
var _keys = {};
var normalizeWheelSpeed = function(event){
var normalized;
if (event.wheelDelta){
normalized = event.wheelDelta % 120 == 0 ? event.wheelDelta / 120 : event.wheelDelta / 12;
} else {
var rawAmount = event.deltaY || event.detail || 0;
normalized = -(rawAmount % 3 == 0 ? rawAmount / 3 : rawAmount * 10);
}
return normalized;
}
var DOMEvent = this.DOMEvent = new Type('DOMEvent', function(event, win){
if (!win) win = window;
@@ -41,7 +51,7 @@ var DOMEvent = this.DOMEvent = new Type('DOMEvent', function(event, win){
else if (code > 95 && code < 106) this.key = code - 96;
}
if (this.key == null) this.key = String.fromCharCode(code).toLowerCase();
} else if (type == 'click' || type == 'dblclick' || type == 'contextmenu' || type == 'DOMMouseScroll' || type.indexOf('mouse') == 0){
} else if (type == 'click' || type == 'dblclick' || type == 'contextmenu' || type == 'wheel' || type == 'DOMMouseScroll' || type.indexOf('mouse') == 0){
var doc = win.document;
doc = (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body;
this.page = {
@@ -52,9 +62,8 @@ var DOMEvent = this.DOMEvent = new Type('DOMEvent', function(event, win){
x: (event.pageX != null) ? event.pageX - win.pageXOffset : event.clientX,
y: (event.pageY != null) ? event.pageY - win.pageYOffset : event.clientY
};
if (type == 'DOMMouseScroll' || type == 'mousewheel')
this.wheel = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
if (type == 'DOMMouseScroll' || type == 'wheel' || type == 'mousewheel')
this.wheel = normalizeWheelSpeed(event);
this.rightClick = (event.which == 3 || event.button == 2);
if (type == 'mouseover' || type == 'mouseout'){
var related = event.relatedTarget || event[(type == 'mouseover' ? 'from' : 'to') + 'Element'];
@@ -376,4 +376,100 @@ describe('Element.removeEvent', function(){
});
describe('Mouse wheel', function(){
function attachProperties(e, direction){
e.detail = 1 * direction;
e.wheelDelta = 1 * direction;
e.deltaY = -1 * direction;
}
function dispatchFakeWheel(type, wheelDirection){
var event;
try {
// Firefox
event = document.createEvent("MouseEvents");
event.initMouseEvent(type, true, true, window, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, null);
attachProperties(event, wheelDirection);
window.dispatchEvent(event);
} catch(e){}
try {
// Chrome, PhantomJS, Safari
event = document.createEvent("WheelEvent");
event.initMouseEvent(type, 0, 100, window, 0, 0, 0, 0, null, null, null, null);
attachProperties(event, wheelDirection);
window.dispatchEvent(event);
} catch(e){}
try {
// IE9
event = document.createEvent("HTMLEvents");
event.initEvent(type, true, false);
attachProperties(event, wheelDirection);
window.dispatchEvent(event);
} catch(e){}
try {
// IE10+, Safari
var event = document.createEvent("MouseEvents");
event.initEvent(type, true, true);
attachProperties(event, wheelDirection);
window.dispatchEvent(event);
} catch(e){}
try {
// IE8
var event = document.createEventObject();
document.documentElement.fireEvent(type, event);
} catch(e){}
}
var triggered = false;
var wheel = false;
var testWheel = !!window.addEventListener;
var callback = function(e){
if (e.wheel) wheel = e.wheel > 0 ? 'wheel moved up' : 'wheel moved down';
triggered = 'triggered';
};
beforeEach(function(){
wheel = triggered = false;
window.addEvent('mousewheel', callback);
document.documentElement.addEvent('mousewheel', callback);
});
afterEach(function(){
window.removeEvent('mousewheel', callback);
document.documentElement.removeEvent('mousewheel', callback);
});
it('should trigger/listen to mousewheel event', function(){
// http://jsfiddle.net/W6QrS/3
['mousewheel', 'wheel' ,'DOMMouseScroll' ].each(dispatchFakeWheel);
expect(triggered).toBeTruthy();
});
it('should listen to mouse wheel direction', function(){
// http://jsfiddle.net/58yCr/
if (!testWheel) return;
// fire event with wheel going up
['mousewheel', 'wheel' ,'DOMMouseScroll' ].each(function(type){
dispatchFakeWheel(type, 120);
});
expect(wheel).toEqual('wheel moved up');
wheel = false;
// fire event with wheel going down
['mousewheel', 'wheel' ,'DOMMouseScroll' ].each(function(type){
dispatchFakeWheel(type, -120);
});
expect(wheel).toEqual('wheel moved down');
});
});
})();

0 comments on commit 20ed691

Please sign in to comment.