-
Notifications
You must be signed in to change notification settings - Fork 3k
/
trimRows.js
executable file
·325 lines (285 loc) · 8.09 KB
/
trimRows.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
import { BasePlugin } from '../base';
import { TrimmingMap } from '../../translations';
import { arrayEach, arrayReduce } from '../../helpers/array';
export const PLUGIN_KEY = 'trimRows';
export const PLUGIN_PRIORITY = 330;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @plugin TrimRows
* @class TrimRows
*
* @description
* The plugin allows to trim certain rows. The trimming is achieved by applying the transformation algorithm to the data
* transformation. In this case, when the row is trimmed it is not accessible using `getData*` methods thus the trimmed
* data is not visible to other plugins.
*
* @example
* ::: only-for javascript
* ```js
* const container = document.getElementById('example');
* const hot = new Handsontable(container, {
* data: getData(),
* // hide selected rows on table initialization
* trimRows: [1, 2, 5]
* });
*
* // access the trimRows plugin instance
* const trimRowsPlugin = hot.getPlugin('trimRows');
*
* // hide single row
* trimRowsPlugin.trimRow(1);
*
* // hide multiple rows
* trimRowsPlugin.trimRow(1, 2, 9);
*
* // or as an array
* trimRowsPlugin.trimRows([1, 2, 9]);
*
* // show single row
* trimRowsPlugin.untrimRow(1);
*
* // show multiple rows
* trimRowsPlugin.untrimRow(1, 2, 9);
*
* // or as an array
* trimRowsPlugin.untrimRows([1, 2, 9]);
*
* // rerender table to see the changes
* hot.render();
* ```
* :::
*
* ::: only-for react
* ```jsx
* const hotRef = useRef(null);
*
* ...
*
* <HotTable
* ref={hotRef}
* data={getData()}
* // hide selected rows on table initialization
* trimRows={[1, 2, 5]}
* />
*
* const hot = hotRef.current.hotInstance;
* // access the trimRows plugin instance
* const trimRowsPlugin = hot.getPlugin('trimRows');
*
* // hide single row
* trimRowsPlugin.trimRow(1);
*
* // hide multiple rows
* trimRowsPlugin.trimRow(1, 2, 9);
*
* // or as an array
* trimRowsPlugin.trimRows([1, 2, 9]);
*
* // show single row
* trimRowsPlugin.untrimRow(1);
*
* // show multiple rows
* trimRowsPlugin.untrimRow(1, 2, 9);
*
* // or as an array
* trimRowsPlugin.untrimRows([1, 2, 9]);
*
* // rerender table to see the changes
* hot.render();
* ```
* :::
*/
export class TrimRows extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
/**
* Map of skipped rows by the plugin.
*
* @private
* @type {null|TrimmingMap}
*/
trimmedRowsMap = null;
/**
* 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 AutoRowSize#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.trimmedRowsMap = this.hot.rowIndexMapper.registerMap('trimRows', new TrimmingMap());
this.trimmedRowsMap.addLocalHook('init', () => this.#onMapInit());
super.enablePlugin();
}
/**
* Updates the plugin's state.
*
* This method is executed when [`updateSettings()`](@/api/core.md#updatesettings) is invoked with any of the following configuration options:
* - [`trimRows`](@/api/options.md#trimrows)
*/
updatePlugin() {
const trimmedRows = this.hot.getSettings()[PLUGIN_KEY];
if (Array.isArray(trimmedRows)) {
this.hot.batchExecution(() => {
this.trimmedRowsMap.clear();
arrayEach(trimmedRows, (physicalRow) => {
this.trimmedRowsMap.setValueAtIndex(physicalRow, true);
});
}, true);
}
super.updatePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.hot.rowIndexMapper.unregisterMap('trimRows');
super.disablePlugin();
}
/**
* Get list of trimmed rows.
*
* @returns {Array} Physical rows.
*/
getTrimmedRows() {
return this.trimmedRowsMap.getTrimmedIndexes();
}
/**
* Trims the rows provided in the array.
*
* @param {number[]} rows Array of physical row indexes.
* @fires Hooks#beforeTrimRow
* @fires Hooks#afterTrimRow
*/
trimRows(rows) {
const currentTrimConfig = this.getTrimmedRows();
const isValidConfig = this.isValidConfig(rows);
let destinationTrimConfig = currentTrimConfig;
if (isValidConfig) {
destinationTrimConfig = Array.from(new Set(currentTrimConfig.concat(rows)));
}
const allowTrimRow = this.hot.runHooks('beforeTrimRow', currentTrimConfig, destinationTrimConfig, isValidConfig);
if (allowTrimRow === false) {
return;
}
if (isValidConfig) {
this.hot.batchExecution(() => {
arrayEach(rows, (physicalRow) => {
this.trimmedRowsMap.setValueAtIndex(physicalRow, true);
});
}, true);
}
this.hot.runHooks('afterTrimRow', currentTrimConfig, destinationTrimConfig, isValidConfig,
isValidConfig && destinationTrimConfig.length > currentTrimConfig.length);
}
/**
* Trims the row provided as a physical row index (counting from 0).
*
* @param {...number} row Physical row index.
*/
trimRow(...row) {
this.trimRows(row);
}
/**
* Untrims the rows provided in the array.
*
* @param {number[]} rows Array of physical row indexes.
* @fires Hooks#beforeUntrimRow
* @fires Hooks#afterUntrimRow
*/
untrimRows(rows) {
const currentTrimConfig = this.getTrimmedRows();
const isValidConfig = this.isValidConfig(rows);
let destinationTrimConfig = currentTrimConfig;
const trimmingMapValues = this.trimmedRowsMap.getValues().slice();
const isAnyRowUntrimmed = rows.length > 0;
if (isValidConfig && isAnyRowUntrimmed) {
// Preparing new values for trimming map.
arrayEach(rows, (physicalRow) => {
trimmingMapValues[physicalRow] = false;
});
// Preparing new trimming config.
destinationTrimConfig = arrayReduce(trimmingMapValues, (trimmedIndexes, isTrimmed, physicalIndex) => {
if (isTrimmed) {
trimmedIndexes.push(physicalIndex);
}
return trimmedIndexes;
}, []);
}
const allowUntrimRow = this.hot
.runHooks('beforeUntrimRow', currentTrimConfig, destinationTrimConfig, isValidConfig && isAnyRowUntrimmed);
if (allowUntrimRow === false) {
return;
}
if (isValidConfig && isAnyRowUntrimmed) {
this.trimmedRowsMap.setValues(trimmingMapValues);
}
this.hot.runHooks('afterUntrimRow', currentTrimConfig, destinationTrimConfig, isValidConfig && isAnyRowUntrimmed,
isValidConfig && destinationTrimConfig.length < currentTrimConfig.length);
}
/**
* Untrims the row provided as a physical row index (counting from 0).
*
* @param {...number} row Physical row index.
*/
untrimRow(...row) {
this.untrimRows(row);
}
/**
* Checks if given row is hidden.
*
* @param {number} physicalRow Physical row index.
* @returns {boolean}
*/
isTrimmed(physicalRow) {
return this.trimmedRowsMap.getValueAtIndex(physicalRow) || false;
}
/**
* Untrims all trimmed rows.
*/
untrimAll() {
this.untrimRows(this.getTrimmedRows());
}
/**
* Get if trim config is valid. Check whether all of the provided physical row indexes are within source data.
*
* @param {Array} trimmedRows List of physical row indexes.
* @returns {boolean}
*/
isValidConfig(trimmedRows) {
const sourceRows = this.hot.countSourceRows();
return trimmedRows
.every(trimmedRow => (Number.isInteger(trimmedRow) && trimmedRow >= 0 && trimmedRow < sourceRows));
}
/**
* On map initialized hook callback.
*/
#onMapInit() {
const trimmedRows = this.hot.getSettings()[PLUGIN_KEY];
if (Array.isArray(trimmedRows)) {
this.hot.batchExecution(() => {
arrayEach(trimmedRows, (physicalRow) => {
this.trimmedRowsMap.setValueAtIndex(physicalRow, true);
});
}, true);
}
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}