|
160 | 160 | </file>
|
161 | 161 | </example>
|
162 | 162 | */
|
163 |
| -angular.module('ui.grid').directive('uiGrid', |
164 |
| - [ |
165 |
| - '$compile', |
166 |
| - '$templateCache', |
167 |
| - 'gridUtil', |
168 |
| - '$window', |
169 |
| - 'uiGridConstants', |
170 |
| - function( |
171 |
| - $compile, |
172 |
| - $templateCache, |
173 |
| - gridUtil, |
174 |
| - $window, |
175 |
| - uiGridConstants |
176 |
| - ) { |
| 163 | +angular.module('ui.grid').directive('uiGrid', uiGridDirective); |
| 164 | + |
| 165 | +uiGridDirective.$inject = ['$compile', '$templateCache', '$timeout', '$window', 'gridUtil', 'uiGridConstants']; |
| 166 | +function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil, uiGridConstants) { |
| 167 | + return { |
| 168 | + templateUrl: 'ui-grid/ui-grid', |
| 169 | + scope: { |
| 170 | + uiGrid: '=' |
| 171 | + }, |
| 172 | + replace: true, |
| 173 | + transclude: true, |
| 174 | + controller: 'uiGridController', |
| 175 | + compile: function () { |
177 | 176 | return {
|
178 |
| - templateUrl: 'ui-grid/ui-grid', |
179 |
| - scope: { |
180 |
| - uiGrid: '=' |
181 |
| - }, |
182 |
| - replace: true, |
183 |
| - transclude: true, |
184 |
| - controller: 'uiGridController', |
185 |
| - compile: function () { |
186 |
| - return { |
187 |
| - post: function ($scope, $elm, $attrs, uiGridCtrl) { |
188 |
| - // gridUtil.logDebug('ui-grid postlink'); |
189 |
| - |
190 |
| - var grid = uiGridCtrl.grid; |
191 |
| - |
192 |
| - // Initialize scrollbars (TODO: move to controller??) |
193 |
| - uiGridCtrl.scrollbars = []; |
194 |
| - |
195 |
| - //todo: assume it is ok to communicate that rendering is complete?? |
196 |
| - grid.renderingComplete(); |
197 |
| - |
198 |
| - grid.element = $elm; |
199 |
| - |
200 |
| - grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm); |
201 |
| - |
202 |
| - // Default canvasWidth to the grid width, in case we don't get any column definitions to calculate it from |
203 |
| - grid.canvasWidth = uiGridCtrl.grid.gridWidth; |
204 |
| - |
205 |
| - grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
206 |
| - |
207 |
| - // If the grid isn't tall enough to fit a single row, it's kind of useless. Resize it to fit a minimum number of rows |
208 |
| - if (grid.gridHeight < grid.options.rowHeight && grid.options.enableMinHeightCheck) { |
209 |
| - // Figure out the new height |
210 |
| - var contentHeight = grid.options.minRowsToShow * grid.options.rowHeight; |
211 |
| - var headerHeight = grid.options.showHeader ? grid.options.headerRowHeight : 0; |
212 |
| - var footerHeight = grid.calcFooterHeight(); |
213 |
| - |
214 |
| - var scrollbarHeight = 0; |
215 |
| - if (grid.options.enableHorizontalScrollbar === uiGridConstants.scrollbars.ALWAYS) { |
216 |
| - scrollbarHeight = gridUtil.getScrollbarWidth(); |
217 |
| - } |
| 177 | + post: function ($scope, $elm, $attrs, uiGridCtrl) { |
| 178 | + var grid = uiGridCtrl.grid; |
| 179 | + // Initialize scrollbars (TODO: move to controller??) |
| 180 | + uiGridCtrl.scrollbars = []; |
| 181 | + grid.element = $elm; |
218 | 182 |
|
219 |
| - var maxNumberOfFilters = 0; |
220 |
| - // Calculates the maximum number of filters in the columns |
221 |
| - angular.forEach(grid.options.columnDefs, function(col) { |
222 |
| - if (col.hasOwnProperty('filter')) { |
223 |
| - if (maxNumberOfFilters < 1) { |
224 |
| - maxNumberOfFilters = 1; |
225 |
| - } |
226 |
| - } |
227 |
| - else if (col.hasOwnProperty('filters')) { |
228 |
| - if (maxNumberOfFilters < col.filters.length) { |
229 |
| - maxNumberOfFilters = col.filters.length; |
230 |
| - } |
231 |
| - } |
232 |
| - }); |
233 |
| - var filterHeight = maxNumberOfFilters * headerHeight; |
234 | 183 |
|
235 |
| - var newHeight = headerHeight + contentHeight + footerHeight + scrollbarHeight + filterHeight; |
| 184 | + // See if the grid has a rendered width, if not, wait a bit and try again |
| 185 | + var sizeCheckInterval = 100; // ms |
| 186 | + var maxSizeChecks = 20; // 2 seconds total |
| 187 | + var sizeChecks = 0; |
236 | 188 |
|
237 |
| - $elm.css('height', newHeight + 'px'); |
| 189 | + // Setup (event listeners) the grid |
| 190 | + setup(); |
238 | 191 |
|
239 |
| - grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
240 |
| - } |
| 192 | + // And initialize it |
| 193 | + init(); |
241 | 194 |
|
242 |
| - // Run initial canvas refresh |
243 |
| - grid.refreshCanvas(); |
| 195 | + // Mark rendering complete so API events can happen |
| 196 | + grid.renderingComplete(); |
244 | 197 |
|
245 |
| - //if we add a left container after render, we need to watch and react |
246 |
| - $scope.$watch(function () { return grid.hasLeftContainer();}, function (newValue, oldValue) { |
247 |
| - if (newValue === oldValue) { |
248 |
| - return; |
249 |
| - } |
250 |
| - grid.refreshCanvas(true); |
251 |
| - }); |
| 198 | + // If the grid doesn't have size currently, wait for a bit to see if it gets size |
| 199 | + checkSize(); |
252 | 200 |
|
253 |
| - //if we add a right container after render, we need to watch and react |
254 |
| - $scope.$watch(function () { return grid.hasRightContainer();}, function (newValue, oldValue) { |
255 |
| - if (newValue === oldValue) { |
256 |
| - return; |
257 |
| - } |
258 |
| - grid.refreshCanvas(true); |
259 |
| - }); |
| 201 | + /*-- Methods --*/ |
260 | 202 |
|
| 203 | + function checkSize() { |
| 204 | + // If the grid has no width and we haven't checked more than <maxSizeChecks> times, check again in <sizeCheckInterval> milliseconds |
| 205 | + if ($elm[0].offsetWidth <= 0 && sizeChecks < maxSizeChecks) { |
| 206 | + setTimeout(checkSize, sizeCheckInterval); |
| 207 | + sizeChecks++; |
| 208 | + } |
| 209 | + else { |
| 210 | + $timeout(init); |
| 211 | + } |
| 212 | + } |
261 | 213 |
|
262 |
| - // Resize the grid on window resize events |
263 |
| - function gridResize($event) { |
264 |
| - grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm); |
265 |
| - grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
| 214 | + // Setup event listeners and watchers |
| 215 | + function setup() { |
| 216 | + // Bind to window resize events |
| 217 | + angular.element($window).on('resize', gridResize); |
266 | 218 |
|
267 |
| - grid.refreshCanvas(true); |
| 219 | + // Unbind from window resize events when the grid is destroyed |
| 220 | + $elm.on('$destroy', function () { |
| 221 | + angular.element($window).off('resize', gridResize); |
| 222 | + }); |
| 223 | + |
| 224 | + // If we add a left container after render, we need to watch and react |
| 225 | + $scope.$watch(function () { return grid.hasLeftContainer();}, function (newValue, oldValue) { |
| 226 | + if (newValue === oldValue) { |
| 227 | + return; |
268 | 228 | }
|
| 229 | + grid.refreshCanvas(true); |
| 230 | + }); |
269 | 231 |
|
270 |
| - angular.element($window).on('resize', gridResize); |
| 232 | + // If we add a right container after render, we need to watch and react |
| 233 | + $scope.$watch(function () { return grid.hasRightContainer();}, function (newValue, oldValue) { |
| 234 | + if (newValue === oldValue) { |
| 235 | + return; |
| 236 | + } |
| 237 | + grid.refreshCanvas(true); |
| 238 | + }); |
| 239 | + } |
271 | 240 |
|
272 |
| - // Unbind from window resize events when the grid is destroyed |
273 |
| - $elm.on('$destroy', function () { |
274 |
| - angular.element($window).off('resize', gridResize); |
275 |
| - }); |
| 241 | + // Initialize the directive |
| 242 | + function init() { |
| 243 | + grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm); |
| 244 | + |
| 245 | + // Default canvasWidth to the grid width, in case we don't get any column definitions to calculate it from |
| 246 | + grid.canvasWidth = uiGridCtrl.grid.gridWidth; |
| 247 | + |
| 248 | + grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
| 249 | + |
| 250 | + // If the grid isn't tall enough to fit a single row, it's kind of useless. Resize it to fit a minimum number of rows |
| 251 | + if (grid.gridHeight < grid.options.rowHeight && grid.options.enableMinHeightCheck) { |
| 252 | + autoAdjustHeight(); |
| 253 | + } |
| 254 | + |
| 255 | + // Run initial canvas refresh |
| 256 | + grid.refreshCanvas(true); |
| 257 | + } |
| 258 | + |
| 259 | + // Set the grid's height ourselves in the case that its height would be unusably small |
| 260 | + function autoAdjustHeight() { |
| 261 | + // Figure out the new height |
| 262 | + var contentHeight = grid.options.minRowsToShow * grid.options.rowHeight; |
| 263 | + var headerHeight = grid.options.showHeader ? grid.options.headerRowHeight : 0; |
| 264 | + var footerHeight = grid.calcFooterHeight(); |
| 265 | + |
| 266 | + var scrollbarHeight = 0; |
| 267 | + if (grid.options.enableHorizontalScrollbar === uiGridConstants.scrollbars.ALWAYS) { |
| 268 | + scrollbarHeight = gridUtil.getScrollbarWidth(); |
276 | 269 | }
|
277 |
| - }; |
| 270 | + |
| 271 | + var maxNumberOfFilters = 0; |
| 272 | + // Calculates the maximum number of filters in the columns |
| 273 | + angular.forEach(grid.options.columnDefs, function(col) { |
| 274 | + if (col.hasOwnProperty('filter')) { |
| 275 | + if (maxNumberOfFilters < 1) { |
| 276 | + maxNumberOfFilters = 1; |
| 277 | + } |
| 278 | + } |
| 279 | + else if (col.hasOwnProperty('filters')) { |
| 280 | + if (maxNumberOfFilters < col.filters.length) { |
| 281 | + maxNumberOfFilters = col.filters.length; |
| 282 | + } |
| 283 | + } |
| 284 | + }); |
| 285 | + var filterHeight = maxNumberOfFilters * headerHeight; |
| 286 | + |
| 287 | + var newHeight = headerHeight + contentHeight + footerHeight + scrollbarHeight + filterHeight; |
| 288 | + |
| 289 | + $elm.css('height', newHeight + 'px'); |
| 290 | + |
| 291 | + grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
| 292 | + } |
| 293 | + |
| 294 | + // Resize the grid on window resize events |
| 295 | + function gridResize($event) { |
| 296 | + grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm); |
| 297 | + grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm); |
| 298 | + |
| 299 | + grid.refreshCanvas(true); |
| 300 | + } |
278 | 301 | }
|
279 | 302 | };
|
280 | 303 | }
|
281 |
| - ]); |
| 304 | + }; |
| 305 | +} |
282 | 306 |
|
283 | 307 | })();
|
0 commit comments