-
Notifications
You must be signed in to change notification settings - Fork 3k
/
nestedRows.js
486 lines (423 loc) · 14.3 KB
/
nestedRows.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
import { BasePlugin } from '../base';
import DataManager from './data/dataManager';
import CollapsingUI from './ui/collapsing';
import HeadersUI from './ui/headers';
import ContextMenuUI from './ui/contextMenu';
import { error } from '../../helpers/console';
import { isArrayOfObjects } from '../../helpers/data';
import { TrimmingMap } from '../../translations';
import RowMoveController from './utils/rowMoveController';
import './nestedRows.scss';
export const PLUGIN_KEY = 'nestedRows';
export const PLUGIN_PRIORITY = 300;
const SHORTCUTS_GROUP = PLUGIN_KEY;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* Error message for the wrong data type error.
*/
const WRONG_DATA_TYPE_ERROR = 'The Nested Rows plugin requires an Array of Objects as a dataset to be' +
' provided. The plugin has been disabled.';
/**
* @plugin NestedRows
* @class NestedRows
*
* @description
* Plugin responsible for displaying and operating on data sources with nested structures.
*/
export class NestedRows extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
/**
* Reference to the DataManager instance.
*
* @private
* @type {object}
*/
dataManager = null;
/**
* Reference to the HeadersUI instance.
*
* @private
* @type {object}
*/
headersUI = null;
/**
* Map of skipped rows by plugin.
*
* @private
* @type {null|TrimmingMap}
*/
collapsedRowsMap = null;
/**
* Allows skipping the render cycle if set as `true`.
*
* @type {boolean}
*/
#skipRender = false;
/**
* Allows skipping the internal Core methods call if set as `true`.
*
* @type {boolean}
*/
#skipCoreAPIModifiers = false;
/**
* Checks if the plugin is enabled in the handsontable settings. This method is executed in {@link Hooks#beforeInit}
* hook and if it returns `true` then the {@link NestedRows#enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return !!this.hot.getSettings()[PLUGIN_KEY];
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
this.collapsedRowsMap = this.hot.rowIndexMapper.registerMap('nestedRows', new TrimmingMap());
this.dataManager = new DataManager(this, this.hot);
this.collapsingUI = new CollapsingUI(this, this.hot);
this.headersUI = new HeadersUI(this, this.hot);
this.contextMenuUI = new ContextMenuUI(this, this.hot);
this.rowMoveController = new RowMoveController(this);
this.addHook('afterInit', (...args) => this.#onAfterInit(...args));
this.addHook('beforeViewRender', (...args) => this.#onBeforeViewRender(...args));
this.addHook('modifyRowData', (...args) => this.onModifyRowData(...args));
this.addHook('modifySourceLength', (...args) => this.onModifySourceLength(...args));
this.addHook('beforeDataSplice', (...args) => this.onBeforeDataSplice(...args));
this.addHook('filterData', (...args) => this.#onFilterData(...args));
this.addHook('afterContextMenuDefaultOptions', (...args) => this.#onAfterContextMenuDefaultOptions(...args));
this.addHook('afterGetRowHeader', (...args) => this.#onAfterGetRowHeader(...args));
this.addHook('beforeOnCellMouseDown', (...args) => this.#onBeforeOnCellMouseDown(...args));
this.addHook('beforeRemoveRow', (...args) => this.#onBeforeRemoveRow(...args));
this.addHook('afterRemoveRow', (...args) => this.#onAfterRemoveRow(...args));
this.addHook('beforeAddChild', (...args) => this.#onBeforeAddChild(...args));
this.addHook('afterAddChild', (...args) => this.#onAfterAddChild(...args));
this.addHook('beforeDetachChild', (...args) => this.#onBeforeDetachChild(...args));
this.addHook('afterDetachChild', (...args) => this.#onAfterDetachChild(...args));
this.addHook('modifyRowHeaderWidth', (...args) => this.#onModifyRowHeaderWidth(...args));
this.addHook('afterCreateRow', (...args) => this.#onAfterCreateRow(...args));
this.addHook('beforeRowMove', (...args) => this.#onBeforeRowMove(...args));
this.addHook('beforeLoadData', data => this.#onBeforeLoadData(data));
this.addHook('beforeUpdateData', data => this.#onBeforeLoadData(data));
this.registerShortcuts();
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.hot.rowIndexMapper.unregisterMap('nestedRows');
this.unregisterShortcuts();
super.disablePlugin();
}
/**
* Updates the plugin's state.
*
* This method is executed when [`updateSettings()`](@/api/core.md#updatesettings) is invoked with any of the following configuration options:
* - [`nestedRows`](@/api/options.md#nestedrows)
*/
updatePlugin() {
this.disablePlugin();
// We store a state of the data manager.
const currentSourceData = this.dataManager.getData();
this.enablePlugin();
// After enabling plugin previously stored data is restored.
this.dataManager.updateWithData(currentSourceData);
super.updatePlugin();
}
/**
* Register shortcuts responsible for toggling collapsible columns.
*
* @private
*/
registerShortcuts() {
this.hot.getShortcutManager()
.getContext('grid')
.addShortcut({
keys: [['Enter']],
callback: () => {
const { highlight } = this.hot.getSelectedRangeLast();
if (highlight.col === -1 && highlight.row >= 0) {
const row = this.collapsingUI.translateTrimmedRow(highlight.row);
if (this.collapsingUI.areChildrenCollapsed(row)) {
this.collapsingUI.expandChildren(row);
} else {
this.collapsingUI.collapseChildren(row);
}
}
},
runOnlyIf: () => {
const highlight = this.hot.getSelectedRangeLast()?.highlight;
return highlight && this.hot.selection.isCellVisible(highlight) && highlight.isHeader();
},
group: SHORTCUTS_GROUP,
});
}
/**
* Unregister shortcuts responsible for toggling collapsible columns.
*
* @private
*/
unregisterShortcuts() {
this.hot.getShortcutManager()
.getContext('grid')
.removeShortcutsByGroup(SHORTCUTS_GROUP);
}
/**
* `beforeRowMove` hook callback.
*
* @param {Array} rows Array of visual row indexes to be moved.
* @param {number} finalIndex Visual row index, being a start index for the moved rows. Points to where the elements
* will be placed after the moving action. To check the visualization of the final index, please take a look at
* [documentation](@/guides/rows/row-summary.md).
* @param {undefined|number} dropIndex Visual row index, being a drop index for the moved rows. Points to where we
* are going to drop the moved elements. To check visualization of drop index please take a look at
* [documentation](@/guides/rows/row-summary.md).
* @param {boolean} movePossible Indicates if it's possible to move rows to the desired position.
* @fires Hooks#afterRowMove
* @returns {boolean}
*/
#onBeforeRowMove(rows, finalIndex, dropIndex, movePossible) {
return this.rowMoveController.onBeforeRowMove(rows, finalIndex, dropIndex, movePossible);
}
/**
* Enable the modify hook skipping flag - allows retrieving the data from Handsontable without this plugin's
* modifications.
*
* @private
*/
disableCoreAPIModifiers() {
this.#skipCoreAPIModifiers = true;
}
/**
* Disable the modify hook skipping flag.
*
* @private
*/
enableCoreAPIModifiers() {
this.#skipCoreAPIModifiers = false;
}
/**
* `beforeOnCellMousedown` hook callback.
*
* @param {MouseEvent} event Mousedown event.
* @param {object} coords Cell coords.
* @param {HTMLElement} TD Clicked cell.
*/
#onBeforeOnCellMouseDown(event, coords, TD) {
this.collapsingUI.toggleState(event, coords, TD);
}
/**
* The modifyRowData hook callback.
*
* @private
* @param {number} row Visual row index.
* @returns {boolean}
*/
onModifyRowData(row) {
if (this.#skipCoreAPIModifiers) {
return;
}
return this.dataManager.getDataObject(row);
}
/**
* Modify the source data length to match the length of the nested structure.
*
* @private
* @returns {number}
*/
onModifySourceLength() {
if (this.#skipCoreAPIModifiers) {
return;
}
return this.dataManager.countAllRows();
}
/**
* @private
* @param {number} index The index where the data was spliced.
* @param {number} amount An amount of items to remove.
* @param {object} element An element to add.
* @returns {boolean}
*/
onBeforeDataSplice(index, amount, element) {
if (this.#skipCoreAPIModifiers || this.dataManager.isRowHighestLevel(index)) {
return true;
}
this.dataManager.spliceData(index, amount, element);
return false;
}
/**
* Provide custom source data filtering. It's handled by core method and replaces the native filtering.
*
* @param {number} index The index where the data filtering starts.
* @param {number} amount An amount of rows which filtering applies to.
* @param {number} physicalRows Physical row indexes.
* @returns {Array}
*/
#onFilterData(index, amount, physicalRows) {
this.collapsingUI.collapsedRowsStash.stash();
this.collapsingUI.collapsedRowsStash.trimStash(physicalRows[0], amount);
this.collapsingUI.collapsedRowsStash.shiftStash(physicalRows[0], null, (-1) * amount);
this.dataManager.filterData(index, amount, physicalRows);
this.#skipRender = true;
return this.dataManager.getData().slice(); // Data contains reference sometimes.
}
/**
* `afterContextMenuDefaultOptions` hook callback.
*
* @param {object} defaultOptions The default context menu items order.
* @returns {boolean}
*/
#onAfterContextMenuDefaultOptions(defaultOptions) {
return this.contextMenuUI.appendOptions(defaultOptions);
}
/**
* `afterGetRowHeader` hook callback.
*
* @param {number} row Row index.
* @param {HTMLElement} TH Row header element.
*/
#onAfterGetRowHeader(row, TH) {
this.headersUI.appendLevelIndicators(row, TH);
}
/**
* `modifyRowHeaderWidth` hook callback.
*
* @param {number} rowHeaderWidth The initial row header width(s).
* @returns {number}
*/
#onModifyRowHeaderWidth(rowHeaderWidth) {
return Math.max(this.headersUI.rowHeaderWidthCache, rowHeaderWidth);
}
/**
* `onAfterRemoveRow` hook callback.
*
* @param {number} index Removed row.
* @param {number} amount Amount of removed rows.
* @param {Array} logicRows An array of the removed physical rows.
* @param {string} source Source of action.
*/
#onAfterRemoveRow(index, amount, logicRows, source) {
if (source === this.pluginName) {
return;
}
this.hot._registerTimeout(() => {
this.#skipRender = false;
this.headersUI.updateRowHeaderWidth();
this.collapsingUI.collapsedRowsStash.applyStash();
});
}
/**
* Callback for the `beforeRemoveRow` change list of removed physical indexes by reference. Removing parent node
* has effect in removing children nodes.
*
* @param {number} index Visual index of starter row.
* @param {number} amount Amount of rows to be removed.
* @param {Array} physicalRows List of physical indexes.
*/
#onBeforeRemoveRow(index, amount, physicalRows) {
const modifiedPhysicalRows = Array.from(physicalRows.reduce((removedRows, physicalIndex) => {
if (this.dataManager.isParent(physicalIndex)) {
const children = this.dataManager.getDataObject(physicalIndex).__children;
// Preserve a parent in the list of removed rows.
removedRows.add(physicalIndex);
if (Array.isArray(children)) {
// Add a children to the list of removed rows.
children.forEach(child => removedRows.add(this.dataManager.getRowIndex(child)));
}
return removedRows;
}
// Don't modify list of removed rows when already checked element isn't a parent.
return removedRows.add(physicalIndex);
}, new Set()));
// Modifying hook's argument by the reference.
physicalRows.length = 0;
physicalRows.push(...modifiedPhysicalRows);
}
/**
* `beforeAddChild` hook callback.
*/
#onBeforeAddChild() {
this.collapsingUI.collapsedRowsStash.stash();
}
/**
* `afterAddChild` hook callback.
*
* @param {object} parent Parent element.
* @param {object} element New child element.
*/
#onAfterAddChild(parent, element) {
this.collapsingUI.collapsedRowsStash.shiftStash(this.dataManager.getRowIndex(element));
this.collapsingUI.collapsedRowsStash.applyStash();
this.headersUI.updateRowHeaderWidth();
}
/**
* `beforeDetachChild` hook callback.
*/
#onBeforeDetachChild() {
this.collapsingUI.collapsedRowsStash.stash();
}
/**
* `afterDetachChild` hook callback.
*
* @param {object} parent Parent element.
* @param {object} element New child element.
* @param {number} finalElementRowIndex The final row index of the detached element.
*/
#onAfterDetachChild(parent, element, finalElementRowIndex) {
this.collapsingUI.collapsedRowsStash.shiftStash(finalElementRowIndex, null, -1);
this.collapsingUI.collapsedRowsStash.applyStash();
this.headersUI.updateRowHeaderWidth();
}
/**
* `afterCreateRow` hook callback.
*/
#onAfterCreateRow() {
this.dataManager.rewriteCache();
}
/**
* `afterInit` hook callback.
*/
#onAfterInit() {
this.headersUI.updateRowHeaderWidth();
}
/**
* `beforeViewRender` hook callback.
*
* @param {boolean} force Indicates if the render call was triggered by a change of settings or data.
* @param {object} skipRender An object, holder for skipRender functionality.
*/
#onBeforeViewRender(force, skipRender) {
if (this.#skipRender) {
skipRender.skipRender = true;
}
}
/**
* `beforeLoadData` hook callback.
*
* @param {Array} data The source data.
*/
#onBeforeLoadData(data) {
if (!isArrayOfObjects(data)) {
error(WRONG_DATA_TYPE_ERROR);
this.hot.getSettings()[PLUGIN_KEY] = false;
this.disablePlugin();
return;
}
this.dataManager.setData(data);
this.dataManager.rewriteCache();
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}