From 40ec65c0501d29d94f3d369d8e8f24b4d575cd0d Mon Sep 17 00:00:00 2001 From: Dana Greenberg Date: Wed, 10 Feb 2016 15:09:45 -0500 Subject: [PATCH] fix: Introduce gridDimensionChanged event Closes #5090 --- .../auto-resize-grid/js/auto-resize.js | 1 + src/js/core/directives/ui-grid-menu.js | 16 +++++--- src/js/core/factories/GridApi.js | 40 ++++++++++++------- .../unit/core/directives/ui-grid-menu.spec.js | 5 ++- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/features/auto-resize-grid/js/auto-resize.js b/src/features/auto-resize-grid/js/auto-resize.js index f1666626d4..164122f18c 100644 --- a/src/features/auto-resize-grid/js/auto-resize.js +++ b/src/features/auto-resize-grid/js/auto-resize.js @@ -41,6 +41,7 @@ if (newGridHeight !== prevGridHeight || newGridWidth !== prevGridWidth) { uiGridCtrl.grid.gridHeight = newGridHeight; uiGridCtrl.grid.gridWidth = newGridWidth; + uiGridCtrl.grid.api.core.raise.gridDimensionChanged(prevGridHeight, prevGridWidth, newGridHeight, newGridWidth); $scope.$apply(function () { uiGridCtrl.grid.refresh() diff --git a/src/js/core/directives/ui-grid-menu.js b/src/js/core/directives/ui-grid-menu.js index 7b88a5f508..e0be2ef0aa 100644 --- a/src/js/core/directives/ui-grid-menu.js +++ b/src/js/core/directives/ui-grid-menu.js @@ -43,19 +43,25 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18 templateUrl: 'ui-grid/uiGridMenu', replace: false, link: function ($scope, $elm, $attrs, uiGridCtrl) { - var gridMenuMaxHeight; $scope.dynamicStyles = ''; - if (uiGridCtrl) { + var setupHeightStyle = function(gridHeight) { // magic number of 30 because the grid menu displays somewhat below // the top of the grid. It is approximately 30px. - gridMenuMaxHeight = uiGridCtrl.grid.gridHeight - 30; - $scope.dynamicStyles = [ + var gridMenuMaxHeight = gridHeight - 30; + $scope.dynamicStyles = [ '.grid' + uiGridCtrl.grid.id + ' .ui-grid-menu-mid {', - 'max-height: ' + gridMenuMaxHeight + 'px;', + 'max-height: ' + gridMenuMaxHeight + 'px;', '}' ].join(' '); + }; + + if (uiGridCtrl) { + setupHeightStyle(uiGridCtrl.grid.gridHeight); + uiGridCtrl.grid.api.core.on.gridDimensionChanged($scope, function(oldGridHeight, oldGridWidth, newGridHeight, newGridWidth) { + setupHeightStyle(newGridHeight); + }); } $scope.i18n = { diff --git a/src/js/core/factories/GridApi.js b/src/js/core/factories/GridApi.js index 0fb0ac4fbd..594bae0a32 100644 --- a/src/js/core/factories/GridApi.js +++ b/src/js/core/factories/GridApi.js @@ -22,7 +22,7 @@ var GridApi = function GridApi(grid) { this.grid = grid; this.listeners = []; - + /** * @ngdoc function * @name renderingComplete @@ -31,14 +31,14 @@ * time as `onRegisterApi`, but provides a way to obtain * that same event within features without stopping end * users from getting at the onRegisterApi method. - * + * * Included in gridApi so that it's always there - otherwise * there is still a timing problem with when a feature can - * call this. - * - * @param {GridApi} gridApi the grid api, as normally + * call this. + * + * @param {GridApi} gridApi the grid api, as normally * returned in the onRegisterApi method - * + * * @example *
            *      gridApi.core.on.renderingComplete( grid );
@@ -52,9 +52,9 @@
            * @eventOf  ui.grid.core.api:PublicApi
            * @description  is raised after the filter is changed.  The nature
            * of the watch expression doesn't allow notification of what changed,
-           * so the receiver of this event will need to re-extract the filter 
+           * so the receiver of this event will need to re-extract the filter
            * conditions from the columns.
-           * 
+           *
            */
           this.registerEvent( 'core', 'filterChanged' );
 
@@ -63,26 +63,26 @@
            * @name setRowInvisible
            * @methodOf  ui.grid.core.api:PublicApi
            * @description Sets an override on the row to make it always invisible,
-           * which will override any filtering or other visibility calculations.  
+           * which will override any filtering or other visibility calculations.
            * If the row is currently visible then sets it to invisible and calls
            * both grid refresh and emits the rowsVisibleChanged event
            * @param {object} rowEntity gridOptions.data[] array instance
            */
           this.registerMethod( 'core', 'setRowInvisible', GridRow.prototype.setRowInvisible );
-      
+
           /**
            * @ngdoc function
            * @name clearRowInvisible
            * @methodOf  ui.grid.core.api:PublicApi
-           * @description Clears any override on visibility for the row so that it returns to 
-           * using normal filtering and other visibility calculations.  
+           * @description Clears any override on visibility for the row so that it returns to
+           * using normal filtering and other visibility calculations.
            * If the row is currently invisible then sets it to visible and calls
            * both grid refresh and emits the rowsVisibleChanged event
            * TODO: if a filter is active then we can't just set it to visible?
            * @param {object} rowEntity gridOptions.data[] array instance
            */
           this.registerMethod( 'core', 'clearRowInvisible', GridRow.prototype.clearRowInvisible );
-      
+
           /**
            * @ngdoc function
            * @name getVisibleRows
@@ -92,7 +92,7 @@
            * @returns {array} an array of gridRow
            */
           this.registerMethod( 'core', 'getVisibleRows', this.grid.getVisibleRows );
-          
+
           /**
            * @ngdoc event
            * @name rowsVisibleChanged
@@ -142,6 +142,16 @@
            * arguments: oldHeight, newHeight
            */
           this.registerEvent( 'core', 'canvasHeightChanged');
+
+          /**
+           * @ngdoc event
+           * @name gridDimensionChanged
+           * @eventOf  ui.grid.core.api:PublicApi
+           * @description  is raised when the grid dimensions have changed (when autoResize is on)
+           * 
+ * arguments: oldGridHeight, oldGridWidth, newGridHeight, newGridWidth + */ + this.registerEvent( 'core', 'gridDimensionChanged'); }; /** @@ -365,7 +375,7 @@ }); }; - + return GridApi; }]); diff --git a/test/unit/core/directives/ui-grid-menu.spec.js b/test/unit/core/directives/ui-grid-menu.spec.js index 2416b6603d..9fb1ed2dcc 100644 --- a/test/unit/core/directives/ui-grid-menu.spec.js +++ b/test/unit/core/directives/ui-grid-menu.spec.js @@ -92,7 +92,10 @@ describe('ui-grid-menu', function() { api: { core: { on: { - scrollBegin: angular.noop + scrollBegin: angular.noop, + gridDimensionChanged: function($scope, fxn) { + fxn(100, 100, 400, 200); + } } } }