Repository navigation
Expand file tree
/
Copy pathDataTableController.js
More file actions
308 lines (265 loc) · 7.05 KB
/
Copy pathDataTableController.js
File metadata and controls
308 lines (265 loc) · 7.05 KB
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
import angular from 'angular';
import { TableDefaults, ColumnDefaults } from '../defaults';
import { AdjustColumnWidths, ForceFillColumnWidths } from '../utils/math';
import { ColumnsByPin, ColumnGroupWidths, CamelCase, ObjectId, ScrollbarWidth } from '../utils/utils';
export class DataTableController {
/**
* Creates an instance of the DataTable Controller
* @param {scope}
* @param {filter}
*/
/*@ngInject*/
constructor($scope, $filter, $log, $transclude){
Object.assign(this, {
$scope: $scope,
$filter: $filter,
$log: $log
});
this.defaults();
// set scope to the parent
this.options.$outer = $scope.$parent;
$scope.$watch('dt.options.columns', (newVal, oldVal) => {
this.transposeColumnDefaults();
if(newVal.length !== oldVal.length){
this.adjustColumns();
}
this.calculateColumns();
}, true);
// default sort
var watch = $scope.$watch('dt.rows', (newVal) => {
if(newVal){
watch();
this.onSorted();
}
});
}
/**
* Creates and extends default options for the grid control
*/
defaults(){
this.expanded = this.expanded || {};
this.options = angular.extend(angular.
copy(TableDefaults), this.options);
angular.forEach(TableDefaults.paging, (v,k) => {
if(!this.options.paging[k]){
this.options.paging[k] = v;
}
});
if(this.options.selectable && this.options.multiSelect){
this.selected = this.selected || [];
}
}
/**
* On init or when a column is added, we need to
* make sure all the columns added have the correct
* defaults applied.
*/
transposeColumnDefaults(){
for(var i=0, len = this.options.columns.length; i < len; i++) {
var column = this.options.columns[i];
column.$id = ObjectId();
angular.forEach(ColumnDefaults, (v,k) => {
if(!column.hasOwnProperty(k)){
column[k] = v;
}
});
if(column.name && !column.prop){
column.prop = CamelCase(column.name);
}
this.options.columns[i] = column;
}
}
/**
* Calculate column groups and widths
*/
calculateColumns(){
var columns = this.options.columns;
this.columnsByPin = ColumnsByPin(columns);
this.columnWidths = ColumnGroupWidths(this.columnsByPin, columns);
}
/**
* Returns the css classes for the data table.
* @return {style object}
*/
tableCss(){
return {
'fixed': this.options.scrollbarV,
'selectable': this.options.selectable,
'checkboxable': this.options.checkboxSelection
};
}
/**
* Adjusts the column widths to handle greed/etc.
* @param {int} forceIdx
*/
adjustColumns(forceIdx){
var width = this.options.internal.innerWidth - this.options.internal.scrollBarWidth;
if(this.options.columnMode === 'force'){
ForceFillColumnWidths(this.options.columns, width, forceIdx);
} else if(this.options.columnMode === 'flex') {
AdjustColumnWidths(this.options.columns, width);
}
}
/**
* Calculates the page size given the height * row height.
* @return {[type]}
*/
calculatePageSize(){
this.options.paging.size = Math.ceil(
this.options.internal.bodyHeight / this.options.rowHeight) + 1;
}
/**
* Sorts the values of the grid for client side sorting.
*/
onSorted(){
if(!this.rows) return;
// return all sorted column, in the same order in which they were sorted
var sorts = this.options.columns
.filter((c) => {
return c.sort;
})
.sort((a, b) => {
// sort the columns with lower sortPriority order first
if (a.sortPriority && b.sortPriority){
if (a.sortPriority > b.sortPriority) return 1;
if (a.sortPriority < b.sortPriority) return -1;
} else if (a.sortPriority){
return -1;
} else if (b.sortPriority){
return 1;
}
return 0;
})
.map((c, i) => {
// update sortPriority
c.sortPriority = i + 1;
return c;
});
if(sorts.length){
this.onSort({sorts: sorts});
if (this.options.onSort){
this.options.onSort(sorts);
}
var clientSorts = [];
for(var i=0, len=sorts.length; i < len; i++) {
var c = sorts[i];
if(c.comparator !== false){
var dir = c.sort === 'asc' ? '' : '-';
if (c.sortBy !== undefined) {
clientSorts.push(dir + c.sortBy);
} else {
clientSorts.push(dir + c.prop);
}
}
}
if(clientSorts.length){
// todo: more ideal to just resort vs splice and repush
// but wasn't responding to this change ...
var sortedValues = this.$filter('orderBy')(this.rows, clientSorts);
this.rows.splice(0, this.rows.length);
this.rows.push(...sortedValues);
}
}
this.options.internal.setYOffset(0);
}
/**
* Invoked when a tree is collasped/expanded
* @param {row model}
* @param {cell model}
*/
onTreeToggled(row, cell){
this.onTreeToggle({
row: row,
cell: cell
});
}
/**
* Invoked when the body triggers a page change.
* @param {offset}
* @param {size}
*/
onBodyPage(offset, size){
this.onPage({
offset: offset,
size: size
});
}
/**
* Invoked when the footer triggers a page change.
* @param {offset}
* @param {size}
*/
onFooterPage(offset, size){
var pageBlockSize = this.options.rowHeight * size,
offsetY = pageBlockSize * offset;
this.options.internal.setYOffset(offsetY);
}
/**
* Invoked when the header checkbox directive has changed.
*/
onHeaderCheckboxChange(){
if(this.rows){
var matches = this.selected.length === this.rows.length;
this.selected.splice(0, this.selected.length);
if(!matches){
this.selected.push(...this.rows);
}
}
}
/**
* Returns if all the rows are selected
* @return {Boolean} if all selected
*/
isAllRowsSelected(){
if(this.rows) return false;
return this.selected.length === this.rows.length;
}
/**
* Occurs when a header directive triggered a resize event
* @param {object} column
* @param {int} width
*/
onResized(column, width){
var idx =this.options.columns.indexOf(column);
if(idx > -1){
var column = this.options.columns[idx];
column.width = width;
column.canAutoResize = false;
this.adjustColumns(idx);
this.calculateColumns();
}
if (this.onColumnResize){
this.onColumnResize({
column: column,
width: width
});
}
}
/**
* Occurs when a row was selected
* @param {object} rows
*/
onSelected(rows){
this.onSelect({
rows: rows
});
}
/**
* Occurs when a row was click but may not be selected.
* @param {object} row
*/
onRowClicked(row){
this.onRowClick({
row: row
});
}
/**
* Occurs when a row was double click but may not be selected.
* @param {object} row
*/
onRowDblClicked(row){
this.onRowDblClick({
row: row
});
}
}