/
collapsibleColumns.js
executable file
·549 lines (469 loc) · 15 KB
/
collapsibleColumns.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import { objectEach } from '../../helpers/object';
import { arrayEach } from '../../helpers/array';
import { rangeEach } from '../../helpers/number';
import { warn } from '../../helpers/console';
import {
addClass,
hasClass,
fastInnerText
} from '../../helpers/dom/element';
import EventManager from '../../eventManager';
import { registerPlugin } from '../../plugins';
import { stopImmediatePropagation } from '../../helpers/dom/event';
import BasePlugin from '../_base';
/**
* @plugin CollapsibleColumns
* @dependencies NestedHeaders HiddenColumns
*
* @description
* The {@link CollapsibleColumns} plugin allows collapsing of columns, covered by a header with the `colspan` property defined.
*
* Clicking the "collapse/expand" button collapses (or expands) all "child" headers except the first one.
*
* Setting the {@link Options#collapsibleColumns} property to `true` will display a "collapse/expand" button in every header
* with a defined `colspan` property.
*
* To limit this functionality to a smaller group of headers, define the `collapsibleColumns` property as an array
* of objects, as in the example below.
*
* @example
* ```js
* const container = document.getElementById('example');
* const hot = new Handsontable(container, {
* data: generateDataObj(),
* colHeaders: true,
* rowHeaders: true,
* // enable plugin
* collapsibleColumns: true,
* });
*
* // or
* const hot = new Handsontable(container, {
* data: generateDataObj(),
* colHeaders: true,
* rowHeaders: true,
* // enable and configure which columns can be collapsed
* collapsibleColumns: [
* {row: -4, col: 1, collapsible: true},
* {row: -3, col: 5, collapsible: true}
* ],
* });
* ```
*/
class CollapsibleColumns extends BasePlugin {
constructor(hotInstance) {
super(hotInstance);
/**
* Cached plugin settings.
*
* @private
* @type {Boolean|Array}
*/
this.settings = null;
/**
* Object listing headers with buttons enabled.
*
* @private
* @type {Object}
*/
this.buttonEnabledList = {};
/**
* Cached reference to the HiddenColumns plugin.
*
* @private
* @type {Object}
*/
this.hiddenColumnsPlugin = null;
/**
* Cached reference to the NestedHeaders plugin.
*
* @private
* @type {Object}
*/
this.nestedHeadersPlugin = null;
/**
* Object listing the currently collapsed sections.
*
* @private
* @type {Object}
*/
this.collapsedSections = {};
/**
* Number of column header levels.
*
* @private
* @type {Number}
*/
this.columnHeaderLevelCount = null;
/**
* Event manager instance reference.
*
* @private
* @type {EventManager}
*/
this.eventManager = 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` than the {@link CollapsibleColumns#enablePlugin} method is called.
*
* @returns {Boolean}
*/
isEnabled() {
return !!this.hot.getSettings().collapsibleColumns;
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
this.settings = this.hot.getSettings().collapsibleColumns;
if (typeof this.settings !== 'boolean') {
this.parseSettings();
}
this.hiddenColumnsPlugin = this.hot.getPlugin('hiddenColumns');
this.nestedHeadersPlugin = this.hot.getPlugin('nestedHeaders');
this.checkDependencies();
this.addHook('afterRender', () => this.onAfterRender());
this.addHook('afterInit', () => this.onAfterInit());
this.addHook('afterGetColHeader', (col, TH) => this.onAfterGetColHeader(col, TH));
this.addHook('beforeOnCellMouseDown', (event, coords, TD) => this.onBeforeOnCellMouseDown(event, coords, TD));
this.eventManager = new EventManager(this.hot);
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.settings = null;
this.buttonEnabledList = {};
this.hiddenColumnsPlugin = null;
this.collapsedSections = {};
this.clearButtons();
super.disablePlugin();
}
/**
* Clears the expand/collapse buttons.
*
* @private
*/
clearButtons() {
if (!this.hot.view) {
return;
}
const headerLevels = this.hot.view.wt.getSetting('columnHeaders').length;
const mainHeaders = this.hot.view.wt.wtTable.THEAD;
const topHeaders = this.hot.view.wt.wtOverlays.topOverlay.clone.wtTable.THEAD;
const topLeftCornerHeaders = this.hot.view.wt.wtOverlays.topLeftCornerOverlay ?
this.hot.view.wt.wtOverlays.topLeftCornerOverlay.clone.wtTable.THEAD : null;
const removeButton = function(button) {
if (button) {
button.parentNode.removeChild(button);
}
};
rangeEach(0, headerLevels - 1, (i) => {
const masterLevel = mainHeaders.childNodes[i];
const topLevel = topHeaders.childNodes[i];
const topLeftCornerLevel = topLeftCornerHeaders ? topLeftCornerHeaders.childNodes[i] : null;
rangeEach(0, masterLevel.childNodes.length - 1, (j) => {
let button = masterLevel.childNodes[j].querySelector('.collapsibleIndicator');
removeButton(button);
if (topLevel && topLevel.childNodes[j]) {
button = topLevel.childNodes[j].querySelector('.collapsibleIndicator');
removeButton(button);
}
if (topLeftCornerHeaders && topLeftCornerLevel && topLeftCornerLevel.childNodes[j]) {
button = topLeftCornerLevel.childNodes[j].querySelector('.collapsibleIndicator');
removeButton(button);
}
});
}, true);
}
/**
* Parses the plugin settings and create a button configuration array.
*
* @private
*/
parseSettings() {
objectEach(this.settings, (val) => {
if (!this.buttonEnabledList[val.row]) {
this.buttonEnabledList[val.row] = {};
}
this.buttonEnabledList[val.row][val.col] = val.collapsible;
});
}
/**
* Checks if plugin dependencies are met.
*
* @private
* @returns {Boolean}
*/
meetsDependencies() {
const settings = this.hot.getSettings();
return settings.nestedHeaders && settings.hiddenColumns;
}
/**
* Checks if all the required dependencies are enabled.
*
* @private
*/
checkDependencies() {
const settings = this.hot.getSettings();
if (this.meetsDependencies()) {
return;
}
if (!settings.nestedHeaders) {
warn('You need to configure the Nested Headers plugin in order to use collapsible headers.');
}
if (!settings.hiddenColumns) {
warn('You need to configure the Hidden Columns plugin in order to use collapsible headers.');
}
}
/**
* Generates the indicator element.
*
* @private
* @param {Number} column Column index.
* @param {HTMLElement} TH TH Element.
* @returns {HTMLElement}
*/
generateIndicator(column, TH) {
const TR = TH.parentNode;
const THEAD = TR.parentNode;
const row = ((-1) * THEAD.childNodes.length) + Array.prototype.indexOf.call(THEAD.childNodes, TR);
if (Object.keys(this.buttonEnabledList).length > 0 && (!this.buttonEnabledList[row] || !this.buttonEnabledList[row][column])) {
return null;
}
const divEl = this.hot.rootDocument.createElement('DIV');
addClass(divEl, 'collapsibleIndicator');
if (this.collapsedSections[row] && this.collapsedSections[row][column] === true) {
addClass(divEl, 'collapsed');
fastInnerText(divEl, '+');
} else {
addClass(divEl, 'expanded');
fastInnerText(divEl, '-');
}
return divEl;
}
/**
* Marks (internally) a section as 'collapsed' or 'expanded' (optionally, also mark the 'child' headers).
*
* @private
* @param {String} state State ('collapsed' or 'expanded').
* @param {Number} row Row index.
* @param {Number} column Column index.
* @param {Boolean} recursive If `true`, it will also attempt to mark the child sections.
*/
markSectionAs(state, row, column, recursive) {
if (!this.collapsedSections[row]) {
this.collapsedSections[row] = {};
}
switch (state) {
case 'collapsed':
this.collapsedSections[row][column] = true;
break;
case 'expanded':
this.collapsedSections[row][column] = void 0;
break;
default:
break;
}
if (recursive) {
const nestedHeadersColspans = this.nestedHeadersPlugin.colspanArray;
const level = this.nestedHeadersPlugin.rowCoordsToLevel(row);
const childHeaders = this.nestedHeadersPlugin.getChildHeaders(row, column);
const childColspanLevel = nestedHeadersColspans[level + 1];
for (let i = 1; i < childHeaders.length; i++) {
if (childColspanLevel && childColspanLevel[childHeaders[i]].colspan > 1) {
this.markSectionAs(state, row + 1, childHeaders[i], true);
}
}
}
}
/**
* Expands section at the provided coords.
*
* @param {Object} coords Contains coordinates information. (`coords.row`, `coords.col`)
*/
expandSection(coords) {
this.markSectionAs('expanded', coords.row, coords.col, true);
this.toggleCollapsibleSection(coords, 'expand');
}
/**
* Collapses section at the provided coords.
*
* @param {Object} coords Contains coordinates information. (`coords.row`, `coords.col`)
*/
collapseSection(coords) {
this.markSectionAs('collapsed', coords.row, coords.col, true);
this.toggleCollapsibleSection(coords, 'collapse');
}
/**
* Collapses or expand all collapsible sections, depending on the action parameter.
*
* @param {String} action 'collapse' or 'expand'.
*/
toggleAllCollapsibleSections(action) {
const nestedHeadersColspanArray = this.nestedHeadersPlugin.colspanArray;
if (this.settings === true) {
arrayEach(nestedHeadersColspanArray, (headerLevel, i) => {
arrayEach(headerLevel, (header, j) => {
if (header.colspan > 1) {
const row = this.nestedHeadersPlugin.levelToRowCoords(parseInt(i, 10));
const col = parseInt(j, 10);
this.markSectionAs(action === 'collapse' ? 'collapsed' : 'expanded', row, col, true);
this.toggleCollapsibleSection({
row,
col
}, action);
}
});
});
} else {
objectEach(this.buttonEnabledList, (headerRow, i) => {
objectEach(headerRow, (header, j) => {
const rowIndex = parseInt(i, 10);
const columnIndex = parseInt(j, 10);
this.markSectionAs(action === 'collapse' ? 'collapsed' : 'expanded', rowIndex, columnIndex, true);
this.toggleCollapsibleSection({
row: rowIndex,
col: columnIndex
}, action);
});
});
}
}
/**
* Collapses all collapsible sections.
*/
collapseAll() {
this.toggleAllCollapsibleSections('collapse');
}
/**
* Expands all collapsible sections.
*/
expandAll() {
this.toggleAllCollapsibleSections('expand');
}
/**
* Collapses/Expands a section.
*
* @param {Object} coords Section coordinates.
* @param {String} action Action definition ('collapse' or 'expand').
*/
toggleCollapsibleSection(coords, action) {
if (coords.row) {
coords.row = parseInt(coords.row, 10);
}
if (coords.col) {
coords.col = parseInt(coords.col, 10);
}
const hiddenColumns = this.hiddenColumnsPlugin.hiddenColumns;
const colspanArray = this.nestedHeadersPlugin.colspanArray;
const level = this.nestedHeadersPlugin.rowCoordsToLevel(coords.row);
const currentHeaderColspan = colspanArray[level][coords.col].colspan;
const childHeaders = this.nestedHeadersPlugin.getChildHeaders(coords.row, coords.col);
let nextLevel = level + 1;
let childColspanLevel = colspanArray[nextLevel];
let firstChildColspan = childColspanLevel ? childColspanLevel[childHeaders[0]].colspan || 1 : 1;
while (firstChildColspan === currentHeaderColspan && nextLevel < this.columnHeaderLevelCount) {
nextLevel += 1;
childColspanLevel = colspanArray[nextLevel];
firstChildColspan = childColspanLevel ? childColspanLevel[childHeaders[0]].colspan || 1 : 1;
}
rangeEach(firstChildColspan, currentHeaderColspan - 1, (i) => {
const colToHide = coords.col + i;
switch (action) {
case 'collapse':
if (!this.hiddenColumnsPlugin.isHidden(colToHide)) {
hiddenColumns.push(colToHide);
}
break;
case 'expand':
if (this.hiddenColumnsPlugin.isHidden(colToHide)) {
hiddenColumns.splice(hiddenColumns.indexOf(colToHide), 1);
}
break;
default:
break;
}
});
this.hot.render();
this.hot.view.wt.wtOverlays.adjustElementsSize(true);
}
/**
* Adds the indicator to the headers.
*
* @private
* @param {Number} column Column index.
* @param {HTMLElement} TH TH element.
*/
onAfterGetColHeader(column, TH) {
if (TH.hasAttribute('colspan') && TH.getAttribute('colspan') > 1 && column >= this.hot.getSettings().fixedColumnsLeft) {
const button = this.generateIndicator(column, TH);
if (button !== null) {
TH.querySelector('div:first-child').appendChild(button);
}
}
}
/**
* Indicator mouse event callback.
*
* @private
* @param {Object} event Mouse event.
* @param {Object} coords Event coordinates.
*/
onBeforeOnCellMouseDown(event, coords) {
if (hasClass(event.target, 'collapsibleIndicator')) {
if (hasClass(event.target, 'expanded')) {
// mark section as collapsed
if (!this.collapsedSections[coords.row]) {
this.collapsedSections[coords.row] = [];
}
this.markSectionAs('collapsed', coords.row, coords.col, true);
this.eventManager.fireEvent(event.target, 'mouseup');
this.toggleCollapsibleSection(coords, 'collapse');
} else if (hasClass(event.target, 'collapsed')) {
this.markSectionAs('expanded', coords.row, coords.col, true);
this.eventManager.fireEvent(event.target, 'mouseup');
this.toggleCollapsibleSection(coords, 'expand');
}
stopImmediatePropagation(event);
return false;
}
}
/**
* AfterInit hook callback.
*
* @private
*/
onAfterInit() {
this.columnHeaderLevelCount = this.hot.view.wt.getSetting('columnHeaders').length;
}
/**
* AfterRender hook callback.
*
* @private
*/
onAfterRender() {
if (!this.nestedHeadersPlugin.enabled || !this.hiddenColumnsPlugin.enabled) {
this.disablePlugin();
}
}
/**
* Destroys the plugin instance.
*/
destroy() {
this.settings = null;
this.buttonEnabledList = null;
this.hiddenColumnsPlugin = null;
this.nestedHeadersPlugin = null;
this.collapsedSections = null;
this.columnHeaderLevelCount = null;
super.destroy();
}
}
registerPlugin('collapsibleColumns', CollapsibleColumns);
export default CollapsibleColumns;