|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /** |
| 5 | + * @ngdoc overview |
| 6 | + * @name ui.grid.emptyBaseLayer |
| 7 | + * @description |
| 8 | + * |
| 9 | + * # ui.grid.emptyBaseLayer |
| 10 | + * |
| 11 | + * <div class="alert alert-warning" role="alert"><strong>Alpha</strong> This feature is in development. There will almost certainly be breaking api changes, or there are major outstanding bugs.</div> |
| 12 | + * |
| 13 | + * This module provides the ability to have the background of the ui-grid be empty rows, this would be displayed in the case were |
| 14 | + * the grid height is greater then the amount of rows displayed. |
| 15 | + * |
| 16 | + * <div doc-module-components="ui.grid.emptyBaseLayer"></div> |
| 17 | + */ |
| 18 | + var module = angular.module('ui.grid.emptyBaseLayer', ['ui.grid']); |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * @ngdoc service |
| 23 | + * @name ui.grid.emptyBaseLayer.service:uiGridBaseLayerService |
| 24 | + * |
| 25 | + * @description Services for the empty base layer grid |
| 26 | + */ |
| 27 | + module.service('uiGridBaseLayerService', ['gridUtil', '$compile', function (gridUtil, $compile) { |
| 28 | + var service = { |
| 29 | + initializeGrid: function (grid, disableEmptyBaseLayer) { |
| 30 | + |
| 31 | + /** |
| 32 | + * @ngdoc object |
| 33 | + * @name ui.grid.emptyBaseLayer.api:GridOptions |
| 34 | + * |
| 35 | + * @description GridOptions for emptyBaseLayer feature, these are available to be |
| 36 | + * set using the ui-grid {@link ui.grid.class:GridOptions gridOptions} |
| 37 | + */ |
| 38 | + grid.baseLayer = { |
| 39 | + emptyRows: [] |
| 40 | + }; |
| 41 | + |
| 42 | + /** |
| 43 | + * @ngdoc object |
| 44 | + * @name enableEmptyGridBaseLayer |
| 45 | + * @propertyOf ui.grid.emptyBaseLayer.api:GridOptions |
| 46 | + * @description Enable empty base layer, which shows empty rows as background on the entire grid |
| 47 | + * <br/>Defaults to true, if the directive is used. |
| 48 | + * <br/>Set to false either by setting this attribute or passing false to the directive. |
| 49 | + */ |
| 50 | + //default option to true unless it was explicitly set to false |
| 51 | + if (grid.options.enableEmptyGridBaseLayer !== false) { |
| 52 | + grid.options.enableEmptyGridBaseLayer = !disableEmptyBaseLayer; |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + setNumberOfEmptyRows: function(viewportHeight, grid) { |
| 57 | + var rowHeight = grid.options.rowHeight, |
| 58 | + rows = Math.ceil(viewportHeight / rowHeight); |
| 59 | + if (rows > 0) { |
| 60 | + grid.baseLayer.emptyRows = []; |
| 61 | + for (var i = 0; i < rows; i++) { |
| 62 | + grid.baseLayer.emptyRows.push({}); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + return service; |
| 68 | + }]); |
| 69 | + |
| 70 | + /** |
| 71 | + * @ngdoc object |
| 72 | + * @name ui.grid.emptyBaseLayer.directive:uiGridEmptyBaseLayer |
| 73 | + * @description Shows empty rows in the background of the ui-grid, these span |
| 74 | + * the full height of the ui-grid, so that there won't be blank space below the shown rows. |
| 75 | + * @example |
| 76 | + * <pre> |
| 77 | + * <div ui-grid="gridOptions" class="grid" ui-grid-empty-base-layer></div> |
| 78 | + * </pre> |
| 79 | + * Or you can enable/disable it dynamically by passing in true or false. It doesn't |
| 80 | + * the value, so it would only be set on initial render. |
| 81 | + * <pre> |
| 82 | + * <div ui-grid="gridOptions" class="grid" ui-grid-empty-base-layer="false"></div> |
| 83 | + * </pre> |
| 84 | + */ |
| 85 | + module.directive('uiGridEmptyBaseLayer', ['gridUtil', 'uiGridBaseLayerService', |
| 86 | + '$parse', |
| 87 | + function (gridUtil, uiGridBaseLayerService, $parse) { |
| 88 | + return { |
| 89 | + require: '^uiGrid', |
| 90 | + scope: false, |
| 91 | + compile: function ($elm, $attrs) { |
| 92 | + return { |
| 93 | + pre: function ($scope, $elm, $attrs, uiGridCtrl) { |
| 94 | + var disableEmptyBaseLayer = $parse($attrs.uiGridEmptyBaseLayer)($scope) === false; |
| 95 | + uiGridBaseLayerService.initializeGrid(uiGridCtrl.grid, disableEmptyBaseLayer); |
| 96 | + }, |
| 97 | + post: function ($scope, $elm, $attrs, uiGridCtrl) { |
| 98 | + if (!uiGridCtrl.grid.options.enableEmptyGridBaseLayer) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + var renderBodyContainer = uiGridCtrl.grid.renderContainers.body, |
| 103 | + viewportHeight = renderBodyContainer.getViewportHeight(); |
| 104 | + |
| 105 | + function heightHasChanged() { |
| 106 | + var newViewPortHeight = renderBodyContainer.getViewportHeight(); |
| 107 | + |
| 108 | + if (newViewPortHeight !== viewportHeight) { |
| 109 | + viewportHeight = newViewPortHeight; |
| 110 | + return true; |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + function getEmptyBaseLayerCss(viewportHeight) { |
| 116 | + var ret = ''; |
| 117 | + // Set ui-grid-empty-base-layer height |
| 118 | + ret += '\n .grid' + uiGridCtrl.grid.id + |
| 119 | + ' .ui-grid-render-container ' + |
| 120 | + '.ui-grid-empty-base-layer-container.ui-grid-canvas ' + |
| 121 | + '{ height: ' + viewportHeight + 'px; }'; |
| 122 | + return ret; |
| 123 | + } |
| 124 | + |
| 125 | + uiGridCtrl.grid.registerStyleComputation({ |
| 126 | + func: function() { |
| 127 | + if (heightHasChanged()) { |
| 128 | + uiGridBaseLayerService.setNumberOfEmptyRows(viewportHeight, uiGridCtrl.grid); |
| 129 | + } |
| 130 | + return getEmptyBaseLayerCss(viewportHeight); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + }; |
| 135 | + } |
| 136 | + }; |
| 137 | + }]); |
| 138 | + |
| 139 | + /** |
| 140 | + * @ngdoc directive |
| 141 | + * @name ui.grid.emptyBaseLayer.directive:uiGridViewport |
| 142 | + * @description stacks on the uiGridViewport directive to append the empty grid base layer html elements to the |
| 143 | + * default gridRow template |
| 144 | + */ |
| 145 | + module.directive('uiGridViewport', |
| 146 | + ['$compile', 'gridUtil', '$templateCache', |
| 147 | + function ($compile, gridUtil, $templateCache) { |
| 148 | + return { |
| 149 | + priority: -200, |
| 150 | + scope: false, |
| 151 | + compile: function ($elm, $attrs) { |
| 152 | + var emptyBaseLayerContainer = $templateCache.get('ui-grid/emptyBaseLayerContainer'); |
| 153 | + $elm.prepend(emptyBaseLayerContainer); |
| 154 | + return { |
| 155 | + pre: function ($scope, $elm, $attrs, controllers) { |
| 156 | + }, |
| 157 | + post: function ($scope, $elm, $attrs, controllers) { |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + }; |
| 162 | + }]); |
| 163 | + |
| 164 | +})(); |
0 commit comments