-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathcomponent.js
368 lines (295 loc) · 10.3 KB
/
component.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
import Component from '@ember/component';
import { scheduleOnce } from '@ember/runloop';
import { computed } from '@ember/object';
import { observer } from '../../-private/utils/observer';
import { bool, readOnly, or } from '@ember/object/computed';
import CollapseTree, { SELECT_MODE } from '../../-private/collapse-tree';
import defaultTo from '../../-private/utils/default-to';
import layout from './template';
import { assert } from '@ember/debug';
let setupRowCountForTest = false;
export function setSetupRowCountForTest(bool) {
setupRowCountForTest = bool;
}
/**
The table body component. This component manages the main bulk of the rows of
the table, provided occlusion for them and managing their behavior. It yields
a template for each row, and an API that contains a row component, the row
value, and the meta object for the row.
```hbs
<EmberTable as |t|>
<t.head @columns={{this.columns}} />
<t.body @rows={{this.rows}} as |b|>
<b.row>
</t.body>
</EmberTable>
```
@yield {object} body - the API object yielded by the table body
@yield {Component} body.row - The table row component
@yield {object} body.rowValue - The value for the currently yielded row
@yield {object} body.rowMeta - The meta for the currently yielded row
@class <EmberTbody />
@public
*/
export default Component.extend({
layout,
tagName: 'tbody',
/**
The API object passed in by the table
@argument api
@required
@type object
*/
api: null,
unwrappedApi: or('api.api', 'api'),
columns: readOnly('unwrappedApi.columnTree.leaves'),
columnMetaCache: readOnly('unwrappedApi.columnTree.columnMetaCache'),
/**
Sets which row selection behavior to follow. Possible values are 'none'
(clicking on a row does nothing), 'single' (clicking on a row selects it and
deselects other rows), and 'multiple' (multiple rows can be selected through
ctrl/cmd-click or shift-click).
@argument checkboxSelectionMode
@type string? ('multiple')
*/
checkboxSelectionMode: defaultTo(SELECT_MODE.MULTIPLE),
/**
Sets which checkbox selection behavior to follow. Possible values are 'none'
(clicking on a row does nothing), 'single' (clicking on a row selects it and
deselects other rows), and 'multiple' (multiple rows can be selected through
ctrl/cmd-click or shift-click).
@argument rowSelectionMode
@type string? ('multiple')
*/
rowSelectionMode: defaultTo(SELECT_MODE.MULTIPLE),
/**
When true, this option enables the toggling of rows without using the ctrlKey or metaKey.
@argument rowToggleMode
@type boolean
*/
rowToggleMode: defaultTo(false),
/**
When true, this option causes selecting all of a node's children to also
select the node itself.
@argument selectingChildrenSelectsParent
@type boolean
*/
selectingChildrenSelectsParent: defaultTo(true),
/**
The currently selected rows. Can either be an array or an individual row.
@argument selection
@type array|object|null
*/
selection: null,
/**
A function that will override how selection is compared to row value.
@argument selectionMatchFunction
@type function?
*/
selectionMatchFunction: null,
/**
An action that is called when the row selection of the table changes.
Will be called with either an array or individual row, depending on the
checkboxSelectionMode.
@argument onSelect
@type Action?
@param {object} selection - The new selection
*/
onSelect: null,
/**
Estimated height for each row. This number is used to decide how many rows
will be rendered at initial rendering.
@argument estimateRowHeight
@type number? (30)
*/
estimateRowHeight: defaultTo(30),
/**
A flag that controls if all rows have same static height or not. By default
it is set to false and row height is dependent on its internal content. If
it is set to true, all rows have the same height equivalent to
estimateRowHeight.
@argument staticHeight
@type boolean? (false)
*/
staticHeight: defaultTo(false),
/**
The number of extra rows to render on either side of the table's viewport
@argument bufferSize
@type number? (1)
*/
bufferSize: defaultTo(1),
/**
A flag that tells the table to render all of its rows at once.
@argument renderAll
@type boolean? (false)
*/
renderAll: defaultTo(false),
/**
An action that is triggered when the table reaches the first row.
@argument firstReached
@type Action?
*/
firstReached: null,
/**
An action that is triggered when the table reaches the last row.
@argument lastReached
@type Action?
*/
lastReached: null,
/**
An action that is triggered when the first visible row of the table changes.
@argument firstVisibleChanged
@type Action?
*/
firstVisibleChanged: null,
/**
An action that is triggered when the last visible row of the table changes.
@argument lastVisibleChanged
@type Action?
*/
lastVisibleChanged: null,
/**
Boolean flag that enables tree behavior if items have a `children` property
@argument enableTree
@type boolean? (true)
*/
enableTree: defaultTo(true),
/**
Boolean flag that enables collapsing tree nodes
@argument enableCollapse
@type boolean? (true)
*/
enableCollapse: defaultTo(true),
/**
The row items that the table should display
@argument rows
@type array? ([])
*/
rows: defaultTo(() => []),
/**
This key is the property used by the collection to determine whether an
array mutation is an append, prepend, or complete replacement. It is also
the key that is passed to the actions, and can be used to restore scroll
position with `idForFirstItem`. This is passed through to the
vertical-collection.
@argument key
@type string? ('@identity')
*/
key: defaultTo('@identity'),
/**
The property is passed through to the vertical-collection. If set, upon initialization
the scroll position will be set such that the item
with the provided id is at the top left on screen.
If the item with id cannot be found, scrollTop is set to 0.
@argument idForFirstItem
@type string?
*/
idForFirstItem: null,
/**
A selector string that will select the element from
which to calculate the viewable height.
@argument containerSelector
@type string? (<tableId>)
*/
containerSelector: defaultTo(''),
/**
Whether or not the table can select, is true if an `onSelect` action was
passed to the table.
*/
canSelect: bool('onSelect'),
dataTestRowCount: null,
attributeBindings: ['dataTestRowCount:data-test-row-count'],
init() {
this._super(...arguments);
/**
The map that contains row meta information for this table.
*/
this.rowMetaCache = new Map();
/**
A data structure that the table uses wrapping the `rows` object. It flattens
the tree structure of the nodes and allows us to treat it as a flat list of
items. This is much more convenient for most table operations in general.
*/
this.collapseTree = CollapseTree.create({
onSelect: this.onSelect?.bind(this),
});
this._updateCollapseTree();
/*
* Ember test selectors will remove data-test-row-count from the bindings,
* so if it is missing there is no need to all the count.
*
* Even when ember-table is testing a production build, the you may want to
* run tests which make assertions about row count. To implement that capability
* reference a boolean variable controlled by the test helpers.
*/
if (setupRowCountForTest) {
this._isObservingDebugRowCount = true;
let scheduleUpdate = (this._scheduleUpdate = () => {
scheduleOnce('actions', this, this._updateDataTestRowCount);
});
this.collapseTree.addObserver('rows', scheduleUpdate);
this.collapseTree.addObserver('[]', scheduleUpdate);
}
assert(
'You must create an <EmberThead /> with columns before creating an <EmberTbody />',
!!this.get('unwrappedApi.columnTree')
);
},
_updateDataTestRowCount() {
this.set('dataTestRowCount', this.get('collapseTree.length'));
},
// eslint-disable-next-line
_updateCollapseTree: observer(
'unwrappedApi.{sorts,sortFunction,compareFunction,sortEmptyLast}',
'enableCollapse',
'enableTree',
'selection',
'selectionMatchFunction',
'selectingChildrenSelectsParent',
'onSelect',
function() {
this.collapseTree.set('sorts', this.get('unwrappedApi.sorts'));
this.collapseTree.set('sortFunction', this.get('unwrappedApi.sortFunction'));
this.collapseTree.set('compareFunction', this.get('unwrappedApi.compareFunction'));
this.collapseTree.set('sortEmptyLast', this.get('unwrappedApi.sortEmptyLast'));
this.collapseTree.set('enableCollapse', this.get('enableCollapse'));
this.collapseTree.set('enableTree', this.get('enableTree'));
this.collapseTree.set('selection', this.get('selection'));
this.collapseTree.set('selectionMatchFunction', this.get('selectionMatchFunction'));
this.collapseTree.set(
'selectingChildrenSelectsParent',
this.get('selectingChildrenSelectsParent')
);
}
),
willDestroy() {
for (let [row, meta] of this.rowMetaCache.entries()) {
meta.destroy();
this.rowMetaCache.delete(row);
}
if (this._isObservingDebugRowCount) {
this.collapseTree.removeObserver('rows', this._scheduleUpdate);
this.collapseTree.removeObserver('[]', this._scheduleUpdate);
}
this.collapseTree.destroy();
},
/**
Computed property which updates the CollapseTree and erases caches. This is
a computed for 1.12 compatibility, otherwise it would make sense to use
lifecycle hooks instead.
*/
wrappedRows: computed('rows', function() {
let rows = this.get('rows');
this.collapseTree.set('rowMetaCache', this.rowMetaCache);
this.collapseTree.set('rows', rows);
return this.collapseTree;
}),
/**
Computed property which calculates container selector for vertical collection.
It can be a custom selector provided directly to <EmberTbody />.
If not, it will be equal to parent <EmberTable /> `id`.
*/
_containerSelector: computed('containerSelector', 'unwrappedApi.tableId', function() {
return this.get('containerSelector') || `#${this.get('unwrappedApi.tableId')}`;
}),
});