From 9bfa6e3f638548f3f78c565f142771ca65e580bd Mon Sep 17 00:00:00 2001 From: Brian Hann Date: Tue, 2 Dec 2014 11:16:06 -0600 Subject: [PATCH] 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 --- src/features/cellnav/js/cellnav.js | 21 ++++++++++----- .../test/uiGridCellNavDirective.spec.js | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/features/cellnav/test/uiGridCellNavDirective.spec.js diff --git a/src/features/cellnav/js/cellnav.js b/src/features/cellnav/js/cellnav.js index c954ffe189..3a6caa871c 100644 --- a/src/features/cellnav/js/cellnav.js +++ b/src/features/cellnav/js/cellnav.js @@ -622,6 +622,7 @@ priority: -150, require: '^uiGrid', scope: false, + controller: function () {}, compile: function () { return { pre: function ($scope, $elm, $attrs, uiGridCtrl) { @@ -696,15 +697,17 @@ return { replace: true, priority: -99999, //this needs to run very last - require: ['^uiGrid', 'uiGridRenderContainer'], + require: ['^uiGrid', 'uiGridRenderContainer', '?^uiGridCellnav'], scope: false, compile: function () { return { - pre: function ($scope, $elm, $attrs, uiGridCtrl) { - }, post: function ($scope, $elm, $attrs, controllers) { var uiGridCtrl = controllers[0], - renderContainerCtrl = controllers[1]; + renderContainerCtrl = controllers[1], + cellNavController = controllers[2]; + + // Skip attaching cell-nav specific logic if the directive is not attached above us + if (!cellNavController) { return; } var containerId = renderContainerCtrl.containerId; @@ -765,9 +768,15 @@ return { priority: -150, // run after default uiGridCell directive and ui.grid.edit uiGridCell restrict: 'A', - require: '^uiGrid', + require: ['^uiGrid', '?^uiGridCellnav'], scope: false, - link: function ($scope, $elm, $attrs, uiGridCtrl) { + link: function ($scope, $elm, $attrs, controllers) { + var uiGridCtrl = controllers[0], + cellNavController = controllers[1]; + + // Skip attaching cell-nav specific logic if the directive is not attached above us + if (!cellNavController) { return; } + if (!$scope.col.colDef.allowCellFocus) { return; } diff --git a/src/features/cellnav/test/uiGridCellNavDirective.spec.js b/src/features/cellnav/test/uiGridCellNavDirective.spec.js new file mode 100644 index 0000000000..f7183a1968 --- /dev/null +++ b/src/features/cellnav/test/uiGridCellNavDirective.spec.js @@ -0,0 +1,26 @@ +describe('ui.grid.cellNav directive', function () { + var $scope, $compile, elm, uiGridConstants; + + beforeEach(module('ui.grid.cellNav')); + + beforeEach(inject(function (_$rootScope_, _$compile_, _uiGridConstants_) { + $scope = _$rootScope_; + $compile = _$compile_; + uiGridConstants = _uiGridConstants_; + + $scope.gridOpts = { + data: [{ name: 'Bob' }] + }; + })); + + it('should not throw exceptions when scrolling when a grid does NOT have the ui-grid-cellNav directive', function () { + elm = angular.element('
'); + + $compile(elm)($scope); + $scope.$digest(); + + expect(function () { + $scope.$broadcast(uiGridConstants.events.GRID_SCROLL, {}); + }).not.toThrow(); + }); +}); \ No newline at end of file