Skip to content

Commit cc8144c

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(gridEdit): Fixing scrollToFocus issues.
Also, improving code coverage and fixing build issue.
1 parent 55635d1 commit cc8144c

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed

src/features/edit/js/gridEdit.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@
535535
cellNavNavigateDereg = uiGridCtrl.grid.api.cellNav.on.navigate($scope, function (newRowCol, oldRowCol, evt) {
536536
if ($scope.col.colDef.enableCellEditOnFocus) {
537537
// Don't begin edit if the cell hasn't changed
538-
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col && evt && (evt.type === 'click' || evt.type === 'keydown')) {
538+
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col &&
539+
(!evt || (evt && (evt.type === 'click' || evt.type === 'keydown')))) {
539540
$timeout(function() {
540541
beginEdit(evt);
541542
});
@@ -545,7 +546,6 @@
545546
}
546547

547548
$scope.beginEditEventsWired = true;
548-
549549
}
550550

551551
function touchStart(event) {
@@ -896,7 +896,6 @@
896896
}
897897
return object;
898898
}
899-
900899
}
901900
};
902901
}]);

src/features/edit/test/uiGridCell.spec.js

+96-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
describe('ui.grid.edit GridCellDirective', function() {
66
var $compile, $rootScope, $templateCache, $timeout, element, grid, gridClassFactory, gridUtil,
7-
recompile, scope, uiGridConstants, uiGridCtrl, uiGridEditService;
7+
recompile, scope, uiGridConstants, uiGridCtrl, uiGridEditService, onNavigateCb;
88

99
beforeEach(function() {
10+
module('ui.grid');
1011
module('ui.grid.edit');
1112

1213
inject(function(_$compile_, _$rootScope_, _$templateCache_, _$timeout_, _gridClassFactory_,
@@ -37,6 +38,27 @@
3738
uiGridEditService.initializeGrid(grid);
3839
grid.buildColumns();
3940
grid.modifyRows(grid.options.data);
41+
grid.options.onRegisterApi = function(gridApi) {
42+
uiGridCtrl = {
43+
grid: {
44+
api: gridApi
45+
}
46+
};
47+
gridApi.cellNav = {
48+
on: {
49+
navigate: function(scope, callback) {
50+
uiGridCtrl.gridScope = scope;
51+
onNavigateCb = callback;
52+
53+
return angular.noop;
54+
},
55+
viewPortKeyDown: function() {
56+
return angular.noop;
57+
}
58+
}
59+
};
60+
};
61+
element = angular.element('<div ui-grid-cell/>');
4062

4163
scope.grid = grid;
4264
scope.col = grid.columns[0];
@@ -89,6 +111,79 @@
89111
});
90112
});
91113

114+
describe('on navigate', function() {
115+
beforeEach(function() {
116+
element = angular.element('<div ui-grid="grid.options" ui-grid-edit/>');
117+
});
118+
describe('if enableCellEditOnFocus is set to true', function() {
119+
beforeEach(function() {
120+
recompile();
121+
uiGridCtrl.gridScope.col.colDef.enableCellEditOnFocus = true;
122+
uiGridCtrl.gridScope.col.editableCellTemplate = '';
123+
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary = jasmine.createSpy('scrollToIfNecessary').and.returnValue({then: angular.noop});
124+
scope.$apply();
125+
});
126+
afterEach(function() {
127+
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary.calls.reset();
128+
});
129+
it('should trigger scrolling if necessary when the navigating to the current row and column via click', function(done) {
130+
var event = new jQuery.Event('click');
131+
132+
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, event);
133+
$timeout.flush();
134+
setTimeout(function() {
135+
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
136+
done();
137+
}, 1);
138+
});
139+
it('should trigger scrolling if necessary when the navigating to the current row and column via keydown', function(done) {
140+
var event = new jQuery.Event('keydown');
141+
142+
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, event);
143+
$timeout.flush();
144+
setTimeout(function() {
145+
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
146+
done();
147+
}, 1);
148+
});
149+
it('should trigger scrolling if necessary when the navigating to the current row and column without an event', function(done) {
150+
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, null);
151+
$timeout.flush();
152+
setTimeout(function() {
153+
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
154+
done();
155+
}, 1);
156+
});
157+
it('should not trigger scrolling if necessary when the navigating to a different row and column', function(done) {
158+
onNavigateCb({row: 'DifferentRow', col: 'DifferentCol'}, {}, null);
159+
$timeout.flush();
160+
setTimeout(function() {
161+
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).not.toHaveBeenCalled();
162+
done();
163+
}, 1);
164+
});
165+
});
166+
describe('if enableCellEditOnFocus is set to false', function() {
167+
beforeEach(function() {
168+
recompile();
169+
uiGridCtrl.gridScope.col.colDef.enableCellEditOnFocus = false;
170+
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary = jasmine.createSpy('scrollToIfNecessary').and.returnValue({then: angular.noop});
171+
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, null);
172+
$timeout.flush();
173+
scope.$apply();
174+
});
175+
afterEach(function() {
176+
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary.calls.reset();
177+
});
178+
it('should not trigger scrolling', function(done) {
179+
setTimeout(function() {
180+
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).not.toHaveBeenCalled();
181+
done();
182+
}, 1);
183+
});
184+
});
185+
});
186+
92187
describe('touchStart', function() {
93188
function testTouchStart(withOriginalEvent) {
94189
beforeEach(function() {

src/features/edit/test/uiGridEditFileChooserDirective.spec.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,27 @@ describe('ui.grid.edit uiGridEditFileChooser', function() {
4343
}
4444
describe('when no target exists', function() {
4545
beforeEach(function() {
46-
var event = new Event('change', {target: null});
46+
var event = document.createEvent('HTMLEvents');
4747

48+
event.initEvent('change', false, true);
4849
fileChooser[0].dispatchEvent(event);
4950
});
5051
testNegativeScenario();
5152
});
5253
describe('when no target files exists', function() {
5354
beforeEach(function() {
54-
var event = new Event('change', {target: {}});
55+
var event = document.createEvent('HTMLEvents');
5556

57+
event.initEvent('change', false, true);
5658
fileChooser[0].dispatchEvent(event);
5759
});
5860
testNegativeScenario();
5961
});
6062
describe('when there are 0 target files', function() {
6163
beforeEach(function() {
62-
var event = new Event('change', {target: {files: []}});
64+
var event = document.createEvent('HTMLEvents');
6365

66+
event.initEvent('change', false, true);
6467
fileChooser[0].dispatchEvent(event);
6568
});
6669
testNegativeScenario();
@@ -83,8 +86,9 @@ describe('ui.grid.edit uiGridEditFileChooser', function() {
8386
expect(fileChooser[0].select).toHaveBeenCalled();
8487
});
8588
it('should emit an end cell edit event on blur', function() {
86-
var event = new Event('blur');
89+
var event = document.createEvent('HTMLEvents');
8790

91+
event.initEvent('blur', false, true);
8892
spyOn($scope, '$emit').and.callFake(angular.noop);
8993
fileChooser.triggerHandler(event);
9094

0 commit comments

Comments
 (0)