Skip to content

Commit 995d3c4

Browse files
committed
fix(Mobile): Bind only to touch or mouse events
Binding to both for the same event causes issues for the grid menu as touch-enabled browsers will fire touch events as well as mouse events subsequently. NOTE: PhantomJS provides touch events though you wouldn't expect it. I've updated the header-cell test specs to allow choosing which sort of event types to trigger.
1 parent ac18398 commit 995d3c4

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/js/core/directives/ui-grid-header-cell.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@
102102
$scope.colMenu = false;
103103
}
104104

105-
function handleClick(evt) {
105+
function handleClick(event) {
106106
// If the shift key is being held down, add this column to the sort
107107
var add = false;
108-
if (evt.shiftKey) {
108+
if (event.shiftKey) {
109109
add = true;
110110
}
111111

@@ -140,7 +140,11 @@
140140
// Long-click (for mobile)
141141
var cancelMousedownTimeout;
142142
var mousedownStartTime = 0;
143-
$contentsElm.on('mousedown touchstart', function(event) {
143+
144+
var downEvent = gridUtil.isTouchEnabled() ? 'touchstart' : 'mousedown';
145+
$contentsElm.on(downEvent, function(event) {
146+
event.stopPropagation();
147+
144148
if (typeof(event.originalEvent) !== 'undefined' && event.originalEvent !== undefined) {
145149
event = event.originalEvent;
146150
}
@@ -160,19 +164,20 @@
160164
}
161165
});
162166
});
163-
164-
$contentsElm.on('mouseup touchend', function () {
167+
168+
var upEvent = gridUtil.isTouchEnabled() ? 'touchend' : 'mouseup';
169+
$contentsElm.on(upEvent, function () {
165170
$timeout.cancel(cancelMousedownTimeout);
166171
});
167172

168173
$scope.$on('$destroy', function () {
169174
$contentsElm.off('mousedown touchstart');
170-
});
175+
});
171176
}
172177

173178

174-
$scope.toggleMenu = function($event) {
175-
$event.stopPropagation();
179+
$scope.toggleMenu = function(event) {
180+
event.stopPropagation();
176181

177182
// If the menu is already showing...
178183
if (uiGridCtrl.columnMenuScope.menuShown) {
@@ -196,8 +201,9 @@
196201

197202
// If this column is sortable, add a click event handler
198203
if ($scope.sortable) {
199-
$contentsElm.on('click touchend', function(evt) {
200-
evt.stopPropagation();
204+
var clickEvent = gridUtil.isTouchEnabled() ? 'touchend' : 'click';
205+
$contentsElm.on(clickEvent, function(event) {
206+
event.stopPropagation();
201207

202208
$timeout.cancel(cancelMousedownTimeout);
203209

@@ -209,7 +215,7 @@
209215
}
210216
else {
211217
// short click
212-
handleClick(evt);
218+
handleClick(event);
213219
}
214220
});
215221

test/unit/core/directives/ui-grid-header-cell.spec.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
describe('uiGridHeaderCell', function () {
2-
var grid, $scope, $compile, $document, $timeout, $window, recompile, $animate, uiGridConstants, columnDefs;
2+
var grid, $scope, $compile, $document, $timeout, $window, recompile, $animate, uiGridConstants, gridUtil, columnDefs;
3+
4+
var downEvent, upEvent, clickEvent;
35

46
var data = [
57
{ "name": "Ethel Price", "gender": "female", "company": "Enersol" },
@@ -23,14 +25,27 @@ describe('uiGridHeaderCell', function () {
2325

2426
beforeEach(module('ui.grid'));
2527

26-
beforeEach(inject(function (_$compile_, $rootScope, _$document_, _$timeout_, _$window_, _$animate_, _uiGridConstants_) {
28+
beforeEach(inject(function (_$compile_, $rootScope, _$document_, _$timeout_, _$window_, _$animate_, _uiGridConstants_, _gridUtil_) {
2729
$scope = $rootScope;
2830
$compile = _$compile_;
2931
$document = _$document_;
3032
$timeout = _$timeout_;
3133
$window = _$window_;
3234
$animate = _$animate_;
3335
uiGridConstants = _uiGridConstants_;
36+
gridUtil = _gridUtil_;
37+
38+
// Decide whether to use mouse or touch events based on which capabilities the browser has
39+
if (gridUtil.isTouchEnabled()) {
40+
downEvent = 'touchstart';
41+
upEvent = 'touchend';
42+
clickEvent = 'touchstart';
43+
}
44+
else {
45+
downEvent = 'mousedown';
46+
upEvent = 'mouseup';
47+
clickEvent = 'click';
48+
}
3449

3550
$scope.gridOpts = {
3651
enableSorting: true,
@@ -70,7 +85,7 @@ describe('uiGridHeaderCell', function () {
7085
});
7186

7287
function openMenu() {
73-
headerCell1.trigger('mousedown');
88+
headerCell1.trigger(downEvent);
7489
$scope.$digest();
7590
$timeout.flush();
7691
$scope.$digest();
@@ -87,7 +102,7 @@ describe('uiGridHeaderCell', function () {
87102
it('should do nothing', inject(function() {
88103
expect(menu.find('.ui-grid-menu-inner').length).toEqual(0, 'column menu is not initially visible');
89104

90-
headerCell1.trigger({ type: 'mousedown', button: 3 });
105+
headerCell1.trigger({ type: downEvent, button: 3 });
91106

92107
$timeout.flush();
93108
$scope.$digest();
@@ -101,7 +116,7 @@ describe('uiGridHeaderCell', function () {
101116
openMenu();
102117
expect(menu.find('.ui-grid-menu-inner').length).toEqual(1, 'column menu is visible');
103118

104-
$document.trigger('click');
119+
$document.trigger(clickEvent);
105120

106121
$timeout.flush();
107122
$scope.$digest();

0 commit comments

Comments
 (0)