-
Notifications
You must be signed in to change notification settings - Fork 14
/
fixtable-grid.js
399 lines (331 loc) · 12.7 KB
/
fixtable-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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import Ember from 'ember';
import layout from '../templates/components/fixtable-grid';
const checkboxColumnWidth = 40; // in pixels
const possiblePageSizes = [ 25, 50, 100, 250, 500 ];
const defaultPage = 1;
const defaultPageSize = 25;
const toCompareString = x => x === null || typeof x === 'undefined' ? '' : x + '';
export default Ember.Component.extend({
layout: layout,
fixtable: null,
columnsByKey: null,
// paging
clientPaging: false,
serverPaging: false,
totalRowsOnServer: 0, // only used for server paging
// filters
filters: null,
filterToApply: null,
filterDebounce: 500,
realtimeFiltering: true,
filtersAreActive: false,
filtersAreDirty: false,
// sorting
sortBy: null,
sortAscending: true,
// row selection
rowSelection: false,
selectedRows: null, // maps row indices to selected state
suppressSelectToggle: false,
nullMessage: "No data available",
showManualFilterButtons: Ember.computed('realtimeFiltering', 'filters',
function fixtableGrid$showManualFilterButtons() {
return !this.get('realtimeFiltering') && Object.keys(this.get('filters')).length;
}),
onFilterChanged: function fixtableGrid$onFilterChanged(/*filters, columnKey*/) {
this.set('filtersAreDirty', true);
if (this.get('realtimeFiltering')) {
Ember.run.debounce(this, this.applyFilter, this.get('filterDebounce'));
}
},
applyFilter: function fixtableGrid$applyFilter() {
// update the filterToApply property to trigger a change in sortedFilteredContent
var filters = this.get('filters');
this.set('filterToApply', JSON.parse(JSON.stringify(filters)));
this.set('currentPage', 1);
this.set('filtersAreDirty', false);
var filtersAreActive = Object.keys(filters).some(key => !!filters[key]);
this.set('filtersAreActive', filtersAreActive);
Ember.run.once(this, this.notifyReloadContent);
},
clearFilter: function fixtableGrid$clearFilter() {
// clear all the bound filter values and re-apply
var filters = this.get('filters');
Object.keys(filters).forEach(key => filters.set(key, ''));
this.applyFilter();
},
currentPage: defaultPage,
afterCurrentPageChanged: Ember.observer('currentPage', function fixtableGrid$afterCurrentPageChanged() {
Ember.run.once(this, this.notifyReloadContent);
}),
pageSize: defaultPageSize,
afterPageSizeChanged: Ember.observer('pageSize', function fixtableGrid$afterPageSizeChanged() {
Ember.run.once(this, this.notifyReloadContent);
this.set('currentPage', defaultPage);
}),
notifyReloadContent: function fixtableGrid$notifyReloadContent() {
var handler = this.get('onReloadContent');
if (typeof handler !== 'function') { return; }
var sortBy = this.get('sortBy');
var sortInfo = sortBy ? { key: sortBy, ascending: this.get('sortAscending') } : null;
handler(
this.get('currentPage'),
this.get('pageSize'),
this.get('filterToApply') || {},
sortInfo);
},
pageSizeOptions: Ember.computed('totalRows', function fixtableGrid$pageSizeOptions() {
// limit the page size options based on content size
for (var i = 0; i < possiblePageSizes.length; i++) {
if (possiblePageSizes[i] >= this.get('totalRows')) {
break;
}
}
return possiblePageSizes.slice(0, i + 1);
}),
showPaging: Ember.computed('clientPaging', 'serverPaging', function fixtableGrid$showPaging() {
return this.get('clientPaging') || this.get('serverPaging');
}),
afterSortChanged: Ember.observer('sortBy', 'sortAscending', function fixtableGrid$afterSortChanged() {
this.set('currentPage', defaultPage);
Ember.run.once(this, this.notifyReloadContent);
}),
sortedContent: Ember.computed('content.[]', 'serverPaging', 'sortBy', 'sortAscending',
function fixtableGrid$sortedContent() {
var content = this.get('content') || [];
var sortBy = this.get('sortBy');
// don't sort on the client if server pagination is on, or if no sort specified
if (this.get('serverPaging') || !sortBy) {
return content;
}
var customSort = this.get('columnsByKey')[sortBy].sortFunction;
var sortFunction;
if (typeof customSort === 'function') {
sortFunction = (leftRow, rightRow) => {
var leftVal = leftRow[sortBy];
var rightVal = rightRow[sortBy];
return this.get('sortAscending') ? customSort(leftVal, rightVal) : customSort(rightVal, leftVal);
};
}
else {
sortFunction = (leftRow, rightRow) => {
var leftVal = toCompareString(leftRow[sortBy]);
var rightVal = toCompareString(rightRow[sortBy]);
return this.get('sortAscending') ? leftVal.localeCompare(rightVal) : rightVal.localeCompare(leftVal);
};
}
var sortedContent = content.slice(); // don't mutate the original collection
sortedContent.sort(sortFunction);
return sortedContent;
}),
sortedFilteredContent: Ember.computed('sortedContent', 'filterToApply', 'serverPaging',
function fixtableGrid$sortedFilteredContent() {
var sortedContent = this.get('sortedContent') || [];
if (this.get('serverPaging')) {
return sortedContent; // don't filter on the client if server pagination is on
}
var columnsByKey = this.get('columnsByKey');
// client filtering
var filters = this.get('filterToApply') || {};
var sortedFilteredContent = sortedContent.filter(function(row) {
return Object.keys(filters).every(function(columnKey) {
var filterDef = columnsByKey[columnKey].filter;
var filterFunction = filterDef.filterFunction;
var filterValue = filters[columnKey];
if (typeof filterFunction === 'function') {
return filterFunction(row, filterValue);
}
else {
if (!filters[columnKey]) { return true; } // no filter
var cellData = (((row.get) ? row.get(columnKey) : row[columnKey]) || '').toLowerCase();
filterValue = filterValue.toLowerCase();
if (columnsByKey[columnKey].filter.type === 'select') {
return cellData === filterValue;
}
else {
return cellData.includes(filterValue);
}
}
});
});
return sortedFilteredContent;
}),
visibleContent: Ember.computed('sortedFilteredContent', 'currentPage', 'pageSize', 'clientPaging',
function fixtableGrid$visibleContent() {
var sortedFilteredContent = this.get('sortedFilteredContent') || [];
if (!this.get('clientPaging')) {
return sortedFilteredContent; // don't paginate on the client unless client pagination is on
}
var currentPage = this.get('currentPage');
var pageSize = this.get('pageSize');
return sortedFilteredContent.slice((currentPage - 1) * pageSize, currentPage * pageSize);
}),
totalRows: Ember.computed('sortedFilteredContent.[]', 'serverPaging', 'totalRowsOnServer',
function fixtableGrid$totalRows() {
if (this.get('serverPaging'))
{
return this.get('totalRowsOnServer');
}
else {
var sortedFilteredContent = this.get('sortedFilteredContent');
return sortedFilteredContent ? sortedFilteredContent.length : 0;
}
}),
totalPages: Ember.computed('totalRows', 'pageSize', function fixtableGrid$totalPages() {
return Math.ceil(this.get('totalRows') / this.get('pageSize'));
}),
validatePageNumberRange(unvalidated) {
return Math.min(Math.max(1, unvalidated), this.get('totalPages'));
},
resetSelection: Ember.observer('visibleContent.[]', function fixtableGrid$resetSelection() {
if (!this.get('rowSelection')) { return; }
var selectedRows = Ember.Object.create();
this.set('selectedRows', selectedRows);
this.get('visibleContent').forEach((row, rowIndex) => {
selectedRows.set(rowIndex.toString(), false);
});
var selectionKeys = Object.keys(selectedRows);
var self = this;
selectionKeys.forEach(function(key) {
if (!selectedRows.hasObserverFor(key)) {
selectedRows.addObserver(key, self, 'onRowSelectedOrDeselected');
}
});
this.setSelectAllToggleChecked(false);
this.setSelectAllToggleIndeterminate(false);
}),
setSelectAllToggleChecked(value) {
this.set('suppressSelectToggle', true);
this.set('selectAllToggle', value);
this.set('suppressSelectToggle', false);
},
setSelectAllToggleIndeterminate(indeterminate) {
if (!this.get('rowSelection')) { return; }
var selector = '.fixtable-column-headers th .fixtable-checkbox input[type=checkbox]';
var element = this.get('element');
if (element) {
var checkbox = element.querySelector(selector);
checkbox.indeterminate = indeterminate;
}
},
onRowSelectedOrDeselected(selectedRows, rowIndex) {
if (!this.get('rowSelection')) { return; }
var selectedRowKeys = Object.keys(selectedRows);
var numSelected = selectedRowKeys.filter(key => selectedRows[key]).length;
var allRowsAreSelected = numSelected === this.get('visibleContent').length;
var someRowsAreSelected = numSelected > 0;
if (allRowsAreSelected) {
this.setSelectAllToggleChecked(true);
this.setSelectAllToggleIndeterminate(false);
}
else if (someRowsAreSelected) {
this.setSelectAllToggleChecked(false);
this.setSelectAllToggleIndeterminate(true);
}
else {
this.setSelectAllToggleChecked(false);
this.setSelectAllToggleIndeterminate(false);
}
var handler = this.get('onSelectionChanged');
if (typeof handler === 'function') {
handler(selectedRows, rowIndex);
}
},
toggleSelectAll: Ember.observer('selectAllToggle', function fixtableGrid$toggleSelectAll() {
if (this.get('suppressSelectToggle') || !this.get('rowSelection')) {
return; // quit if we're programmatically setting the property, not responding to user input
}
var selectedRows = this.get('selectedRows');
var selectedRowKeys = Object.keys(selectedRows);
var numSelected = selectedRowKeys.filter(rowIndex => selectedRows[rowIndex]).length;
var allRowsAreSelected = numSelected === this.get('visibleContent').length;
if (allRowsAreSelected) {
selectedRowKeys.forEach(rowIndex => selectedRows.set(rowIndex, false));
}
else {
selectedRowKeys.forEach(rowIndex => selectedRows.set(rowIndex, true));
}
this.setSelectAllToggleIndeterminate(false); // if the user clicked, it's not indeterminate
}),
actions: {
goToPreviousPage() {
this.set('currentPage', this.validatePageNumberRange(this.get('currentPage') - 1));
},
goToNextPage() {
this.set('currentPage', this.validatePageNumberRange(this.get('currentPage') + 1));
},
applyManualFilter() {
this.applyFilter();
},
clearManualFilter() {
this.clearFilter();
},
sortColumn(columnKey) {
if (this.get('sortBy') === columnKey) {
this.set('sortAscending', !this.get('sortAscending'));
}
else {
this.set('sortBy', columnKey);
this.set('sortAscending', true);
}
}
},
init() {
this._super(...arguments);
var columnsByKey = {};
this.get('columns').forEach(column => {
columnsByKey[column.key] = column;
});
this.set('columnsByKey', columnsByKey);
this.updateFilterObservers();
this.resetSelection();
},
updateFilterObservers() {
var filters = Ember.Object.create();
this.set('filters', filters);
this.get('columns').forEach(colDef => {
if (colDef.filter && typeof filters[colDef.key] === 'undefined') {
filters[colDef.key] = "";
}
});
var filterKeys = Object.keys(filters);
var self = this;
filterKeys.forEach(function(key) {
if (!filters.hasObserverFor(key)) {
filters.addObserver(key, self, 'onFilterChanged');
}
});
},
didInsertElement() {
this._super(...arguments);
Ember.run.later(this, this.initializeFixtable, 0);
},
initializeFixtable() {
// initialize the Fixtable script
var fixtable = new Fixtable(this.$('.fixtable')[0]);
// account for the row selection checkbox column, if present
var indexOffset = 1;
if (this.get('rowSelection')) {
indexOffset = 2;
fixtable.setColumnWidth(1, checkboxColumnWidth);
}
// set fixtable column widths
var columns = this.get('columns');
columns.forEach((col, index) => {
if (col.width) {
fixtable.setColumnWidth(index + indexOffset, col.width);
}
});
fixtable.setDimensions();
this.set('fixtable', fixtable);
this.notifyReloadContent();
},
didRender() {
// force the Fixtable to resize itself when rendered
this._super(...arguments);
let fixtable = this.get('fixtable');
if (fixtable) {
fixtable.setDimensions();
}
}
});