Skip to content

Commit

Permalink
Fix for angular-ui#205
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenzieajudd committed Apr 21, 2016
1 parent 5e68417 commit 346116e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/ui-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions test/uiLayoutCtrl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div></div>')});
});

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);
});
});
});
});

1 comment on commit 346116e

@itzik182
Copy link

@itzik182 itzik182 commented on 346116e Oct 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mackenzieajudd Hi, which version entered above changes?

Please sign in to comment.