-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathui-grid.js
352 lines (288 loc) · 12.6 KB
/
ui-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
(function () {
'use strict';
angular.module('ui.grid').controller('uiGridController', ['$scope', '$element', '$attrs', 'gridUtil', '$q', 'uiGridConstants',
'$templateCache', 'gridClassFactory', '$timeout', '$parse', '$compile',
function ($scope, $elm, $attrs, gridUtil, $q, uiGridConstants,
$templateCache, gridClassFactory, $timeout, $parse, $compile) {
// gridUtil.logDebug('ui-grid controller');
var self = this;
self.grid = gridClassFactory.createGrid($scope.uiGrid);
//assign $scope.$parent if appScope not already assigned
self.grid.appScope = self.grid.appScope || $scope.$parent;
$elm.addClass('grid' + self.grid.id);
self.grid.rtl = gridUtil.getStyles($elm[0])['direction'] === 'rtl';
// angular.extend(self.grid.options, );
//all properties of grid are available on scope
$scope.grid = self.grid;
if ($attrs.uiGridColumns) {
$attrs.$observe('uiGridColumns', function(value) {
self.grid.options.columnDefs = value;
self.grid.buildColumns()
.then(function(){
self.grid.preCompileCellTemplates();
self.grid.refreshCanvas(true);
});
});
}
// if fastWatch is set we watch only the length and the reference, not every individual object
var deregFunctions = [];
if (self.grid.options.fastWatch) {
self.uiGrid = $scope.uiGrid;
if (angular.isString($scope.uiGrid.data)) {
deregFunctions.push( $scope.$parent.$watch($scope.uiGrid.data, dataWatchFunction) );
deregFunctions.push( $scope.$parent.$watch(function() {
if ( self.grid.appScope[$scope.uiGrid.data] ){
return self.grid.appScope[$scope.uiGrid.data].length;
} else {
return undefined;
}
}, dataWatchFunction) );
} else {
deregFunctions.push( $scope.$parent.$watch(function() { return $scope.uiGrid.data; }, dataWatchFunction) );
deregFunctions.push( $scope.$parent.$watch(function() { return $scope.uiGrid.data.length; }, dataWatchFunction) );
}
deregFunctions.push( $scope.$parent.$watch(function() { return $scope.uiGrid.columnDefs; }, columnDefsWatchFunction) );
deregFunctions.push( $scope.$parent.$watch(function() { return $scope.uiGrid.columnDefs.length; }, columnDefsWatchFunction) );
} else {
if (angular.isString($scope.uiGrid.data)) {
deregFunctions.push( $scope.$parent.$watchCollection($scope.uiGrid.data, dataWatchFunction) );
} else {
deregFunctions.push( $scope.$parent.$watchCollection(function() { return $scope.uiGrid.data; }, dataWatchFunction) );
}
deregFunctions.push( $scope.$parent.$watchCollection(function() { return $scope.uiGrid.columnDefs; }, columnDefsWatchFunction) );
}
function columnDefsWatchFunction(n, o) {
if (n && n !== o) {
self.grid.options.columnDefs = n;
self.grid.buildColumns({ orderByColumnDefs: true })
.then(function(){
self.grid.preCompileCellTemplates();
self.grid.callDataChangeCallbacks(uiGridConstants.dataChange.COLUMN);
});
}
}
var mostRecentData;
function dataWatchFunction(newData) {
// gridUtil.logDebug('dataWatch fired');
var promises = [];
if ( self.grid.options.fastWatch ){
if (angular.isString($scope.uiGrid.data)) {
newData = self.grid.appScope[$scope.uiGrid.data];
} else {
newData = $scope.uiGrid.data;
}
}
mostRecentData = newData;
if (newData) {
// columns length is greater than the number of row header columns, which don't count because they're created automatically
var hasColumns = self.grid.columns.length > (self.grid.rowHeaderColumns ? self.grid.rowHeaderColumns.length : 0);
if (
// If we have no columns
!hasColumns &&
// ... and we don't have a ui-grid-columns attribute, which would define columns for us
!$attrs.uiGridColumns &&
// ... and we have no pre-defined columns
self.grid.options.columnDefs.length === 0 &&
// ... but we DO have data
newData.length > 0
) {
// ... then build the column definitions from the data that we have
self.grid.buildColumnDefsFromData(newData);
}
// If we haven't built columns before and either have some columns defined or some data defined
if (!hasColumns && (self.grid.options.columnDefs.length > 0 || newData.length > 0)) {
// Build the column set, then pre-compile the column cell templates
promises.push(self.grid.buildColumns()
.then(function() {
self.grid.preCompileCellTemplates();
}));
}
$q.all(promises).then(function() {
// use most recent data, rather than the potentially outdated data passed into watcher handler
self.grid.modifyRows(mostRecentData)
.then(function () {
// if (self.viewport) {
self.grid.redrawInPlace(true);
// }
$scope.$evalAsync(function() {
self.grid.refreshCanvas(true);
self.grid.callDataChangeCallbacks(uiGridConstants.dataChange.ROW);
});
});
});
}
}
var styleWatchDereg = $scope.$watch(function () { return self.grid.styleComputations; }, function() {
self.grid.refreshCanvas(true);
});
$scope.$on('$destroy', function() {
deregFunctions.forEach( function( deregFn ){ deregFn(); });
styleWatchDereg();
});
self.fireEvent = function(eventName, args) {
// Add the grid to the event arguments if it's not there
if (typeof(args) === 'undefined' || args === undefined) {
args = {};
}
if (typeof(args.grid) === 'undefined' || args.grid === undefined) {
args.grid = self.grid;
}
$scope.$broadcast(eventName, args);
};
self.innerCompile = function innerCompile(elm) {
$compile(elm)($scope);
};
}]);
/**
* @ngdoc directive
* @name ui.grid.directive:uiGrid
* @element div
* @restrict EA
* @param {Object} uiGrid Options for the grid to use
*
* @description Create a very basic grid.
*
* @example
<example module="app">
<file name="app.js">
var app = angular.module('app', ['ui.grid']);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.data = [
{ name: 'Bob', title: 'CEO' },
{ name: 'Frank', title: 'Lowly Developer' }
];
}]);
</file>
<file name="index.html">
<div ng-controller="MainCtrl">
<div ui-grid="{ data: data }"></div>
</div>
</file>
</example>
*/
angular.module('ui.grid').directive('uiGrid', uiGridDirective);
uiGridDirective.$inject = ['$compile', '$templateCache', '$timeout', '$window', 'gridUtil', 'uiGridConstants'];
function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil, uiGridConstants) {
return {
templateUrl: 'ui-grid/ui-grid',
scope: {
uiGrid: '='
},
replace: true,
transclude: true,
controller: 'uiGridController',
compile: function () {
return {
post: function ($scope, $elm, $attrs, uiGridCtrl) {
var grid = uiGridCtrl.grid;
// Initialize scrollbars (TODO: move to controller??)
uiGridCtrl.scrollbars = [];
grid.element = $elm;
// See if the grid has a rendered width, if not, wait a bit and try again
var sizeCheckInterval = 100; // ms
var maxSizeChecks = 20; // 2 seconds total
var sizeChecks = 0;
// Setup (event listeners) the grid
setup();
// And initialize it
init();
// Mark rendering complete so API events can happen
grid.renderingComplete();
// If the grid doesn't have size currently, wait for a bit to see if it gets size
checkSize();
/*-- Methods --*/
function checkSize() {
// If the grid has no width and we haven't checked more than <maxSizeChecks> times, check again in <sizeCheckInterval> milliseconds
if ($elm[0].offsetWidth <= 0 && sizeChecks < maxSizeChecks) {
setTimeout(checkSize, sizeCheckInterval);
sizeChecks++;
}
else {
$timeout(init);
}
}
// Setup event listeners and watchers
function setup() {
// Bind to window resize events
angular.element($window).on('resize', gridResize);
// Unbind from window resize events when the grid is destroyed
$elm.on('$destroy', function () {
angular.element($window).off('resize', gridResize);
});
// If we add a left container after render, we need to watch and react
$scope.$watch(function () { return grid.hasLeftContainer();}, function (newValue, oldValue) {
if (newValue === oldValue) {
return;
}
grid.refreshCanvas(true);
});
// If we add a right container after render, we need to watch and react
$scope.$watch(function () { return grid.hasRightContainer();}, function (newValue, oldValue) {
if (newValue === oldValue) {
return;
}
grid.refreshCanvas(true);
});
}
// Initialize the directive
function init() {
grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm);
// Default canvasWidth to the grid width, in case we don't get any column definitions to calculate it from
grid.canvasWidth = uiGridCtrl.grid.gridWidth;
grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm);
// 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
if (grid.gridHeight <= grid.options.rowHeight && grid.options.enableMinHeightCheck) {
autoAdjustHeight();
}
// Run initial canvas refresh
grid.refreshCanvas(true);
}
// Set the grid's height ourselves in the case that its height would be unusably small
function autoAdjustHeight() {
// Figure out the new height
var contentHeight = grid.options.minRowsToShow * grid.options.rowHeight;
var headerHeight = grid.options.showHeader ? grid.options.headerRowHeight : 0;
var footerHeight = grid.calcFooterHeight();
var scrollbarHeight = 0;
if (grid.options.enableHorizontalScrollbar === uiGridConstants.scrollbars.ALWAYS) {
scrollbarHeight = gridUtil.getScrollbarWidth();
}
var maxNumberOfFilters = 0;
// Calculates the maximum number of filters in the columns
angular.forEach(grid.options.columnDefs, function(col) {
if (col.hasOwnProperty('filter')) {
if (maxNumberOfFilters < 1) {
maxNumberOfFilters = 1;
}
}
else if (col.hasOwnProperty('filters')) {
if (maxNumberOfFilters < col.filters.length) {
maxNumberOfFilters = col.filters.length;
}
}
});
if (grid.options.enableFiltering) {
var allColumnsHaveFilteringTurnedOff = grid.options.columnDefs.every(function(col) {
return col.enableFiltering === false;
});
if (!allColumnsHaveFilteringTurnedOff) {
maxNumberOfFilters++;
}
}
var filterHeight = maxNumberOfFilters * headerHeight;
var newHeight = headerHeight + contentHeight + footerHeight + scrollbarHeight + filterHeight;
$elm.css('height', newHeight + 'px');
grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm);
}
// Resize the grid on window resize events
function gridResize($event) {
grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm);
grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm);
grid.refreshCanvas(true);
}
}
};
}
};
}
})();