This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
datatable.js
executable file
·347 lines (274 loc) · 8.15 KB
/
datatable.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
import {inject} from 'aurelia-dependency-injection';
import {bindingMode, computedFrom} from 'aurelia-binding';
import {bindable, customElement} from 'aurelia-templating';
import {resolvedView} from 'aurelia-view-manager';
import {EntityManager} from 'aurelia-orm';
import {Router} from 'aurelia-router';
import {Homefront} from 'homefront';
@customElement('datatable')
@resolvedView('spoonx/datatable', 'datatable')
@inject(Router, Element, EntityManager)
export class DataTable {
@bindable({defaultBindingMode: bindingMode.twoWay})
criteria = {};
@bindable({defaultBindingMode: bindingMode.twoWay})
where = {};
@bindable limit = 30;
@bindable columns = '';
@bindable searchColumn = 'name';
@bindable actions = [];
@bindable searchable = null; // Show the search field? (Optional attribute).
@bindable sortable = null; // Columns can be sorted? (Optional attribute).
@bindable edit = null; // Rows are editable? (Optional attribute).
@bindable destroy = null; // Rows are removable? (Optional attribute).
@bindable page = 1; // Current page.
@bindable loadingIndicator = '<center>Loading...</center>';
@bindable populate = false; // Which columns to populate. True for all, string for specific.
@bindable detailView = false; // Detail viewmodel
@bindable select; // User provided callback, called upon clicking on a row.
@bindable repository;
@bindable resource;
@bindable data;
@bindable route;
@bindable pages;
@bindable footer;
loading = false;
hasVisibleActions = false;
constructor(router, element, entityManager) {
this.router = router;
this.element = element;
this.entityManager = entityManager;
}
attached() {
if (!this.repository && this.resource) {
this.repository = this.entityManager.getRepository(this.resource);
}
this.ready = true;
this.criteria.where = this.where || {};
this.criteria.sort = this.criteria.sort || {};
this.load();
}
detached() {
this.ready = false;
}
pageChanged() {
if (!this.ready) {
return;
}
this.load();
}
limitChanged() {
if (!this.ready) {
return;
}
this.load();
}
load() {
this.loading = true;
this.criteria.skip = (this.page * this.limit) - this.limit;
this.criteria.limit = this.limit;
if (!this.populate) {
this.criteria.populate = null;
} else if (typeof this.populate === 'string') {
this.criteria.populate = this.populate;
} else if(Array.isArray(this.populate)) {
this.criteria.populate = this.populate.join(',');
}
this.repository.find(this.criteria, true)
.then(result => {
this.loading = false;
this.data = result;
})
.catch(error => {
this.loading = false;
this.triggerEvent('exception', {on: 'load', error: error});
});
}
gatherData(criteria = {}) {
return this.repository.find(criteria, true).catch(error => {
this.triggerEvent('exception', {on: 'load', error: error});
});
}
populateEntity(row) {
return this.repository.getPopulatedEntity(row);
}
doDestroy(row) {
if (typeof this.destroy === 'function') {
return this.destroy(row);
}
this.populateEntity(row).destroy()
.then(() => {
this.load();
this.triggerEvent('destroyed', row);
})
.catch(error => {
this.triggerEvent('exception', {on: 'destroy', error: error});
});
}
doEdit(row) {
if (typeof this.edit === 'function') {
return this.edit(row);
}
}
doCustomAction(action, row) {
if (!action) {
return false;
}
if (typeof action.action === 'function') {
return action.action(row);
}
}
checkDisabled(action, row) {
if (!action) {
return true;
}
if (typeof action.disabled === 'function') {
return action.disabled(row);
}
return false;
}
checkVisibility(action, row) {
if (!action) {
return false;
}
if (typeof action.visible !== 'function') {
this.hasVisibleActions = true;
return true;
}
let isVisible = action.visible(row);
if (isVisible) {
this.hasVisibleActions = true;
}
return isVisible;
}
showActions() {
return this.destroy !== null || this.edit !== null || this.actions.length > 0;
}
doSort(columnLabel) {
let column = columnLabel.column;
if (this.sortable === null || !this.isSortable(column)) {
return;
}
this.criteria.sort = {
[column]: this.criteria.sort[column] === 'asc' ? 'desc' : 'asc'
};
this.load();
}
searchColumnChanged(newValue, oldValue) {
if (!this.ready) {
return;
}
delete this.criteria.where[oldValue];
return this.doSearch();
}
doSearch() {
if (!this.ready) {
return;
}
if (typeof this.criteria.where[this.searchColumn] === 'object') {
this.criteria.where[this.searchColumn].contains = this.search;
} else {
this.criteria.where[this.searchColumn] = {contains: this.search};
}
if (!this.ready) {
return;
}
this.pager.reloadCount();
this.load();
}
reload() {
this.pager.reloadCount(); // reload the amount of results
if (this.page === 1) {
this.load(); // this.pageChanged() won't trigger if the current page is already page 1.
}
this.page = 1;
}
@computedFrom('columnLabels', 'hasVisibleActions', 'detailView')
get colspan() {
return this.columnLabels.length + (this.hasVisibleActions ? 1 : 0) + (this.detailView ? 1 : 0);
}
@computedFrom('columns')
get columnLabels() {
function clean(str) {
return str.replace(/^'?\s*|\s*'$/g, '');
}
function ucfirst(str) {
return str[0].toUpperCase() + str.substr(1);
}
if (Array.isArray(this.columns)) {
return this.columns.map(column => {
return {
nested : !this.isSortable(column.property),
column : column.property,
label : ucfirst(clean(column.label || column.property)),
route : column.route || false,
converter: column.valueConverters || false
};
});
}
let labelsRaw = this.columns.split(',');
let columnsArray = [];
let labels = [];
labelsRaw.forEach(label => {
if (!label) {
return;
}
let converter = label.split(' | ');
let aliased = converter[0].split(' as ');
let cleanedColumn = clean(aliased[0]);
if (columnsArray.indexOf(cleanedColumn) === -1) {
columnsArray.push(cleanedColumn);
}
labels.push({
nested : !this.isSortable(cleanedColumn),
column : cleanedColumn,
label : ucfirst(clean(aliased[1] || aliased[0])),
converter: (converter.length > 1) ? converter.slice(1).join(' | ') : false
});
});
return labels;
}
triggerEvent(event, payload = {}) {
payload.bubbles = true;
return this.element.dispatchEvent(new CustomEvent(event, payload));
}
selected(row, columnOptions) {
if (columnOptions.route) {
let params = {};
if (columnOptions.route.params) {
Object.keys(columnOptions.route.params).forEach(param => {
let property = columnOptions.route.params[param];
params[param] = this.displayValue(row, property);
});
}
return this.router.navigateToRoute(columnOptions.route.name, params);
}
if (this.route) {
return this.router.navigateToRoute(this.route, {id: row.id});
}
if (this.select) {
return this.select(row);
}
}
isSortable(column) {
if (column.indexOf('.') > 0) {
return false;
}
if (!this.populate) {
return true;
}
if (typeof this.populate !== 'string') {
return this.populate.indexOf(column) === -1;
}
return this.populate
.replace(' ', '')
.split(',')
.indexOf(column) === -1;
}
displayValue(row, propertyName) {
return new Homefront(row, Homefront.MODE_NESTED).fetch(propertyName);
}
collapseRow(row) {
row._collapsed = (row._collapsed) ? false : true;
}
}