-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
lt-body.js
508 lines (447 loc) · 12.9 KB
/
lt-body.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import Component from '@ember/component';
import { computed, observer } from '@ember/object';
import layout from 'ember-light-table/templates/components/lt-body';
import { run } from '@ember/runloop';
import Row from 'ember-light-table/classes/Row';
/**
* @module Light Table
*/
/**
* ```hbs
* {{#light-table table as |t|}}
* {{#t.body multiSelect=true onRowClick=(action 'rowClicked') as |body|}}
* {{#body.expanded-row as |row|}}
* Hello <b>{{row.firstName}}</b>
* {{/body.expanded-row}}
*
* {{#if isLoading}}
* {{#body.loader}}
* Loading...
* {{/body.loader}}
* {{/if}}
*
* {{#if table.isEmpty}}
* {{#body.no-data}}
* No users found.
* {{/body.no-data}}
* {{/if}}
* {{/t.body}}
* {{/light-table}}
* ```
*
* @class t.body
*/
export default Component.extend({
layout,
classNames: ['lt-body-wrap'],
classNameBindings: ['canSelect', 'multiSelect', 'canExpand'],
/**
* @property table
* @type {Table}
* @private
*/
table: null,
/**
* @property sharedOptions
* @type {Object}
* @private
*/
sharedOptions: null,
/**
* @property tableActions
* @type {Object}
*/
tableActions: null,
/**
* @property extra
* @type {Object}
*/
extra: null,
/**
* Allows a user to select a row on click. All this will do is apply the necessary
* CSS classes and add the row to `table.selectedRows`. If `multiSelect` is disabled
* only one row will be selected at a time.
*
* @property canSelect
* @type {Boolean}
* @default true
*/
canSelect: true,
/**
* Select a row on click. If this is set to `false` and multiSelect is
* enabled, using click + `shift`, `cmd`, or `ctrl` will still work as
* intended, while clicking on the row will not set the row as selected.
*
* @property selectOnClick
* @type {Boolean}
* @default true
*/
selectOnClick: true,
/**
* Allows for expanding row. This will create a new row under the row that was
* clicked with the template provided by `body.expanded-row`.
*
* ```hbs
* {{#body.expanded-row as |row|}}
* This is the content of the expanded row for {{row.firstName}}
* {{/body.expanded-row}}
* ```
*
* @property canExpand
* @type {Boolean}
* @default false
*/
canExpand: false,
/**
* Allows a user to select multiple rows with the `ctrl`, `cmd`, and `shift` keys.
* These rows can be easily accessed via `table.get('selectedRows')`
*
* @property multiSelect
* @type {Boolean}
* @default false
*/
multiSelect: false,
/**
* When multiSelect is true, this property determines whether or not `ctrl`
* (or `cmd`) is required to select additional rows, one by one. When false,
* simply clicking on subsequent rows will select or deselect them.
*
* `shift` to select many consecutive rows is unaffected by this property.
*
* @property multiSelectRequiresKeyboard
* @type {Boolean}
* @default true
*/
multiSelectRequiresKeyboard: true,
/**
* Hide scrollbar when not scrolling
*
* @property autoHideScrollbar
* @type {Boolean}
* @default true
*/
autoHideScrollbar: true,
/**
* Allows multiple rows to be expanded at once
*
* @property multiRowExpansion
* @type {Boolean}
* @default true
*/
multiRowExpansion: true,
/**
* Expand a row on click
*
* @property expandOnClick
* @type {Boolean}
* @default true
*/
expandOnClick: true,
/**
* If true, the body block will yield columns and rows, allowing you
* to define your own table body
*
* @property overwrite
* @type {Boolean}
* @default false
*/
overwrite: false,
/**
* If true, the body will prepend an invisible `<tr>` that scaffolds the
* widths of the table cells.
*
* ember-light-table uses [`table-layout: fixed`](https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout).
* This means, that the widths of the columns are defined by the first row
* only. By prepending this scaffolding row, widths of columns only need to
* be specified once.
*
* @property enableScaffolding
* @type {Boolean}
* @default false
*/
enableScaffolding: false,
/**
* ID of main table component. Used to generate divs for ember-wormhole
*
* @property tableId
* @type {String}
* @private
*/
tableId: null,
/**
* @property scrollBuffer
* @type {Number}
* @default 500
*/
scrollBuffer: 500,
/**
* @property scrollBufferRows
* @type {Number}
* @default 500 / estimatedRowHeight
*/
scrollBufferRows: computed('scrollBuffer', 'sharedOptions.estimatedRowHeight', function() {
return Math.ceil(
this.get('scrollBuffer') / (this.get('sharedOptions.estimatedRowHeight') || 1)
);
}),
/**
* @property useVirtualScrollbar
* @type {Boolean}
* @default false
* @private
*/
useVirtualScrollbar: false,
/**
* Set this property to scroll to a specific px offset.
*
* This only works when `useVirtualScrollbar` is `true`, i.e. when you are
* using fixed headers / footers.
*
* @property scrollTo
* @type {Number}
* @default null
*/
scrollTo: null,
_scrollTo: null,
/**
* Set this property to a `Row` to scroll that `Row` into view.
*
* This only works when `useVirtualScrollbar` is `true`, i.e. when you are
* using fixed headers / footers.
*
* @property scrollToRow
* @type {Row}
* @default null
*/
scrollToRow: null,
_scrollToRow: null,
/**
* @property targetScrollOffset
* @type {Number}
* @default 0
* @private
*/
targetScrollOffset: 0,
/**
* @property currentScrollOffset
* @type {Number}
* @default 0
* @private
*/
currentScrollOffset: 0,
/**
* @property hasReachedTargetScrollOffset
* @type {Boolean}
* @default true
* @private
*/
hasReachedTargetScrollOffset: true,
/**
* Allows to customize the component used to render rows
*
* ```hbs
* {{#light-table table as |t|}}
* {{t.body rowComponent=(component 'my-row')}}
* {{/light-table}}
* ```
* @property rowComponent
* @type {Ember.Component}
* @default null
*/
rowComponent: null,
/**
* Allows to customize the component used to render spanned rows
*
* ```hbs
* {{#light-table table as |t|}}
* {{t.body spannedRowComponent=(component 'my-spanned-row')}}
* {{/light-table}}
* ```
* @property spannedRowComponent
* @type {Ember.Component}
* @default null
*/
spannedRowComponent: null,
/**
* Allows to customize the component used to render infinite loader
*
* ```hbs
* {{#light-table table as |t|}}
* {{t.body infinityComponent=(component 'my-infinity')}}
* {{/light-table}}
* ```
* @property infinityComponent
* @type {Ember.Component}
* @default null
*/
infinityComponent: null,
rows: computed.readOnly('table.visibleRows'),
columns: computed.readOnly('table.visibleColumns'),
colspan: computed.readOnly('columns.length'),
_prevSelectedIndex: -1,
init() {
this._super(...arguments);
/*
We can only set `useVirtualScrollbar` once all contextual components have
been initialized since fixedHeader and fixedFooter are set on t.head and t.foot
initialization.
*/
run.once(this, this._setupVirtualScrollbar);
},
didReceiveAttrs() {
this._super(...arguments);
this.setupScrollOffset();
},
destroy() {
this._super(...arguments);
run.cancel(this._checkTargetOffsetTimer);
run.cancel(this._setTargetOffsetTimer);
},
_setupVirtualScrollbar() {
let { fixedHeader, fixedFooter } = this.get('sharedOptions');
this.set('useVirtualScrollbar', fixedHeader || fixedFooter);
},
onRowsChange: observer('rows.[]', function() {
this._checkTargetOffsetTimer = run.scheduleOnce('afterRender', this, this.checkTargetScrollOffset);
}),
setupScrollOffset() {
let {
scrollTo,
_scrollTo,
scrollToRow,
_scrollToRow
} = this.getProperties(['scrollTo', '_scrollTo', 'scrollToRow', '_scrollToRow']);
let targetScrollOffset = null;
this.setProperties({ _scrollTo: scrollTo, _scrollToRow: scrollToRow });
if (scrollTo !== _scrollTo) {
targetScrollOffset = Number.parseInt(scrollTo, 10);
if (Number.isNaN(targetScrollOffset)) {
targetScrollOffset = null;
}
this.setProperties({
targetScrollOffset,
hasReachedTargetScrollOffset: targetScrollOffset <= 0
});
} else if (scrollToRow !== _scrollToRow) {
if (scrollToRow instanceof Row) {
let rowElement = this.element.querySelector(`[data-row-id=${scrollToRow.get('rowId')}]`);
if (rowElement instanceof Element) {
targetScrollOffset = rowElement.offsetTop;
}
}
this.setProperties({ targetScrollOffset, hasReachedTargetScrollOffset: true });
}
},
checkTargetScrollOffset() {
if (!this.get('hasReachedTargetScrollOffset')) {
let targetScrollOffset = this.get('targetScrollOffset');
let currentScrollOffset = this.get('currentScrollOffset');
if (targetScrollOffset > currentScrollOffset) {
this.set('targetScrollOffset', null);
this._setTargetOffsetTimer = run.schedule('render', null, () => {
this.set('targetScrollOffset', targetScrollOffset);
});
} else {
this.set('hasReachedTargetScrollOffset', true);
}
}
},
toggleExpandedRow(row) {
let multiRowExpansion = this.get('multiRowExpansion');
let shouldExpand = !row.expanded;
if (multiRowExpansion) {
row.toggleProperty('expanded');
} else {
this.get('table.expandedRows').setEach('expanded', false);
row.set('expanded', shouldExpand);
}
},
actions: {
/**
* onRowClick action. Handles selection, and row expansion.
* @event onRowClick
* @param {Row} row The row that was clicked
* @param {Event} event The click event
*/
onRowClick(row, e) {
let rows = this.get('table.rows');
let multiSelect = this.get('multiSelect');
let multiSelectRequiresKeyboard = this.get('multiSelectRequiresKeyboard');
let canSelect = this.get('canSelect');
let selectOnClick = this.get('selectOnClick');
let canExpand = this.get('canExpand');
let expandOnClick = this.get('expandOnClick');
let isSelected = row.get('selected');
let currIndex = rows.indexOf(row);
let prevIndex = this._prevSelectedIndex === -1 ? currIndex : this._prevSelectedIndex;
this._prevSelectedIndex = currIndex;
let toggleExpandedRow = () => {
if (canExpand && expandOnClick) {
this.toggleExpandedRow(row);
}
};
if (canSelect) {
if (e.shiftKey && multiSelect) {
rows.slice(Math.min(currIndex, prevIndex), Math.max(currIndex, prevIndex) + 1).forEach((r) => r.set('selected', !isSelected));
} else if ((!multiSelectRequiresKeyboard || (e.ctrlKey || e.metaKey)) && multiSelect) {
row.toggleProperty('selected');
} else {
if (selectOnClick) {
this.get('table.selectedRows').setEach('selected', false);
row.set('selected', !isSelected);
}
toggleExpandedRow();
}
} else {
toggleExpandedRow();
}
this.sendAction('onRowClick', ...arguments);
},
/**
* onRowDoubleClick action.
* @event onRowDoubleClick
* @param {Row} row The row that was clicked
* @param {Event} event The click event
*/
onRowDoubleClick(/* row */) {
this.sendAction('onRowDoubleClick', ...arguments);
},
/**
* onScroll action - sent when user scrolls in the Y direction
*
* This only works when `useVirtualScrollbar` is `true`, i.e. when you are
* using fixed headers / footers.
*
* @event onScroll
* @param {Number} scrollOffset The scroll offset in px
* @param {Event} event The scroll event
*/
onScroll(scrollOffset /* , event */) {
this.set('currentScrollOffset', scrollOffset);
this.sendAction('onScroll', ...arguments);
},
/**
* onScrolledToBottom action - sent when user scrolls to the bottom
*
* @event onScrolledToBottom
*/
onScrolledToBottom() {
this.sendAction('onScrolledToBottom');
},
firstVisibleChanged(item, index /* , key */) {
this.sendAction('firstVisibleChanged', ...arguments);
const estimateScrollOffset = index * this.get('sharedOptions.estimatedRowHeight');
this.sendAction('onScroll', estimateScrollOffset, null);
},
lastVisibleChanged(/* item, index, key */) {
this.sendAction('lastVisibleChanged', ...arguments);
},
firstReached(/* item, index, key */) {
this.sendAction('firstReached', ...arguments);
},
lastReached(/* item, index, key */) {
this.sendAction('lastReached', ...arguments);
this.sendAction('onScrolledToBottom');
}
}
});