diff --git a/src/ui-layout.js b/src/ui-layout.js index 223b591..ee93765 100644 --- a/src/ui-layout.js +++ b/src/ui-layout.js @@ -171,14 +171,20 @@ angular.module('ui.layout', []) return event; }; + ctrl.getMousePosition = function(mouseEvent){ + return ctrl.sizeProperties.mouseProperty in mouseEvent ? mouseEvent[ctrl.sizeProperties.mouseProperty] + : mouseEvent.originalEvent && ctrl.sizeProperties.mouseProperty in mouseEvent.originalEvent ? mouseEvent.originalEvent[ctrl.sizeProperties.mouseProperty] + : mouseEvent.targetTouches ? mouseEvent.targetTouches[0][ctrl.sizeProperties.mouseProperty] + : mouseEvent.originalEvent && mouseEvent.originalEvent.targetTouches ? mouseEvent.originalEvent.targetTouches[0][ctrl.sizeProperties.mouseProperty] + : null; + }; + ctrl.mouseMoveHandler = function(mouseEvent) { - var mousePos = mouseEvent[ctrl.sizeProperties.mouseProperty] || - (mouseEvent.originalEvent && mouseEvent.originalEvent[ctrl.sizeProperties.mouseProperty]) || - // jQuery does touches weird, see #82 - ($window.jQuery ? - (mouseEvent.originalEvent ? mouseEvent.originalEvent.targetTouches[0][ctrl.sizeProperties.mouseProperty] : 0) : - (mouseEvent.targetTouches ? mouseEvent.targetTouches[0][ctrl.sizeProperties.mouseProperty] : 0)); + var mousePos = ctrl.getMousePosition(mouseEvent); + + if(mousePos === null) return; + lastPos = mousePos - offset($element)[ctrl.sizeProperties.offsetPos]; //Cancel previous rAF call diff --git a/test/uiLayoutCtrl.spec.js b/test/uiLayoutCtrl.spec.js index deb9f38..78d25d8 100644 --- a/test/uiLayoutCtrl.spec.js +++ b/test/uiLayoutCtrl.spec.js @@ -36,5 +36,86 @@ describe('Controller: uiLayoutCtrl', function () { expect(uic.isLayoutElement(tagContainer)).toEqual(true); expect(uic.isLayoutElement(notUiEl)).toEqual(false); }); + + describe('getMousePosition', function(){ + + var controller; + + beforeEach(function(){ + controller = $controller('uiLayoutCtrl', { + $scope: scope, + $attrs: {}, + $element: angular.element('
')}); + }); + + it('should handle standard mouse event', function(){ + var mockMouseEvent = {}; + mockMouseEvent[controller.sizeProperties.mouseProperty] = 0; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(0); + }); + + it('should handle jQuery mouse event', function(){ + + var mockMouseEvent = { + originalEvent: {} + }; + mockMouseEvent.originalEvent[controller.sizeProperties.mouseProperty] = 0; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(0); + }); + + it('should handle standard touch event', function(){ + + var mockMouseEvent = { + targetTouches: [] + }; + mockMouseEvent.targetTouches[0] = {}; + mockMouseEvent.targetTouches[0][controller.sizeProperties.mouseProperty] = 0; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(0); + }); + + it('should handle unrecognised standard event', function(){ + var mockMouseEvent = {}; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(null); + }); + + it('should handle jQuery touch event', function(){ + + var mockMouseEvent = { + originalEvent: { + targetTouches: [] + } + }; + + mockMouseEvent.originalEvent.targetTouches[0] = {}; + mockMouseEvent.originalEvent.targetTouches[0][controller.sizeProperties.mouseProperty] = 0; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(0); + }); + + it('should handle unrecognised jQuery event', function(){ + + var mockMouseEvent = { + originalEvent: {} + }; + + var result = controller.getMousePosition(mockMouseEvent); + + expect(result).toEqual(null); + }); + }); }); }); \ No newline at end of file