Skip to content

Commit 9bfa6e3

Browse files
committed
fix(cellNav): Don't setup when directive not there
The cellNav feature was attaching logic to render containers and grid cells despite the uiGridCellnav directive not being on the parent grid component. This was causing exception to be thrown during scroll events. This change looks for the cellNav controller (which is simply empty) and bails before attaching logic if the controller is not present. Also added tests to cover the exceptions that were occuring. Fixes #2128
1 parent b0e36aa commit 9bfa6e3

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/features/cellnav/js/cellnav.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@
622622
priority: -150,
623623
require: '^uiGrid',
624624
scope: false,
625+
controller: function () {},
625626
compile: function () {
626627
return {
627628
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
@@ -696,15 +697,17 @@
696697
return {
697698
replace: true,
698699
priority: -99999, //this needs to run very last
699-
require: ['^uiGrid', 'uiGridRenderContainer'],
700+
require: ['^uiGrid', 'uiGridRenderContainer', '?^uiGridCellnav'],
700701
scope: false,
701702
compile: function () {
702703
return {
703-
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
704-
},
705704
post: function ($scope, $elm, $attrs, controllers) {
706705
var uiGridCtrl = controllers[0],
707-
renderContainerCtrl = controllers[1];
706+
renderContainerCtrl = controllers[1],
707+
cellNavController = controllers[2];
708+
709+
// Skip attaching cell-nav specific logic if the directive is not attached above us
710+
if (!cellNavController) { return; }
708711

709712
var containerId = renderContainerCtrl.containerId;
710713

@@ -765,9 +768,15 @@
765768
return {
766769
priority: -150, // run after default uiGridCell directive and ui.grid.edit uiGridCell
767770
restrict: 'A',
768-
require: '^uiGrid',
771+
require: ['^uiGrid', '?^uiGridCellnav'],
769772
scope: false,
770-
link: function ($scope, $elm, $attrs, uiGridCtrl) {
773+
link: function ($scope, $elm, $attrs, controllers) {
774+
var uiGridCtrl = controllers[0],
775+
cellNavController = controllers[1];
776+
777+
// Skip attaching cell-nav specific logic if the directive is not attached above us
778+
if (!cellNavController) { return; }
779+
771780
if (!$scope.col.colDef.allowCellFocus) {
772781
return;
773782
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
describe('ui.grid.cellNav directive', function () {
2+
var $scope, $compile, elm, uiGridConstants;
3+
4+
beforeEach(module('ui.grid.cellNav'));
5+
6+
beforeEach(inject(function (_$rootScope_, _$compile_, _uiGridConstants_) {
7+
$scope = _$rootScope_;
8+
$compile = _$compile_;
9+
uiGridConstants = _uiGridConstants_;
10+
11+
$scope.gridOpts = {
12+
data: [{ name: 'Bob' }]
13+
};
14+
}));
15+
16+
it('should not throw exceptions when scrolling when a grid does NOT have the ui-grid-cellNav directive', function () {
17+
elm = angular.element('<div ui-grid="gridOpts"></div>');
18+
19+
$compile(elm)($scope);
20+
$scope.$digest();
21+
22+
expect(function () {
23+
$scope.$broadcast(uiGridConstants.events.GRID_SCROLL, {});
24+
}).not.toThrow();
25+
});
26+
});

0 commit comments

Comments
 (0)