This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
bindRowsWithHeaders.js
171 lines (154 loc) · 4.31 KB
/
bindRowsWithHeaders.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
import BasePlugin from 'handsontable/plugins/_base';
import { rangeEach } from 'handsontable/helpers/number';
import { registerPlugin } from 'handsontable/plugins';
import BindStrategy from './bindStrategy';
/**
* @plugin BindRowsWithHeaders
* @pro
*
* @description
* Plugin allows binding the table rows with their headers.
*
* If the plugin is enabled, the table row headers will "stick" to the rows, when they are hidden/moved. Basically, if
* at the initialization row 0 has a header titled "A", it will have it no matter what you do with the table.
*
* @example
* ```js
* const container = document.getElementById('example');
* const hot = new Handsontable(container, {
* date: getData(),
* // enable plugin
* bindRowsWithHeaders: true
* });
* ```
*/
class BindRowsWithHeaders extends BasePlugin {
constructor(hotInstance) {
super(hotInstance);
/**
* Strategy object for binding rows with headers.
*
* @private
* @type {BindStrategy}
*/
this.bindStrategy = new BindStrategy();
/**
* List of last removed row indexes.
*
* @private
* @type {Array}
*/
this.removedRows = [];
}
/**
* 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 BindRowsWithHeaders#enablePlugin} method is called.
*
* @returns {Boolean}
*/
isEnabled() {
return !!this.hot.getSettings().bindRowsWithHeaders;
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
let bindStrategy = this.hot.getSettings().bindRowsWithHeaders;
if (typeof bindStrategy !== 'string') {
bindStrategy = BindStrategy.DEFAULT_STRATEGY;
}
this.bindStrategy.setStrategy(bindStrategy);
this.bindStrategy.createMap(this.hot.countSourceRows());
this.addHook('modifyRowHeader', row => this.onModifyRowHeader(row));
this.addHook('afterCreateRow', (index, amount) => this.onAfterCreateRow(index, amount));
this.addHook('beforeRemoveRow', (index, amount) => this.onBeforeRemoveRow(index, amount));
this.addHook('afterRemoveRow', () => this.onAfterRemoveRow());
this.addHook('afterLoadData', firstRun => this.onAfterLoadData(firstRun));
super.enablePlugin();
}
/**
* Updates the plugin state. This method is executed when {@link Core#updateSettings} is invoked.
*/
updatePlugin() {
super.updatePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.removedRows.length = 0;
this.bindStrategy.clearMap();
super.disablePlugin();
}
/**
* On modify row header listener.
*
* @private
* @param {Number} row Row index.
* @returns {Number}
*
* @fires Hooks#modifyRow
*/
onModifyRowHeader(row) {
return this.bindStrategy.translate(this.hot.runHooks('modifyRow', row));
}
/**
* On after create row listener.
*
* @private
* @param {Number} index Row index.
* @param {Number} amount Defines how many rows removed.
*/
onAfterCreateRow(index, amount) {
this.bindStrategy.createRow(index, amount);
}
/**
* On before remove row listener.
*
* @private
* @param {Number} index Row index.
* @param {Number} amount Defines how many rows removed.
*
* @fires Hooks#modifyRow
*/
onBeforeRemoveRow(index, amount) {
this.removedRows.length = 0;
if (index !== false) {
// Collect physical row index.
rangeEach(index, index + amount - 1, (removedIndex) => {
this.removedRows.push(this.hot.runHooks('modifyRow', removedIndex));
});
}
}
/**
* On after remove row listener.
*
* @private
*/
onAfterRemoveRow() {
this.bindStrategy.removeRow(this.removedRows);
}
/**
* On after load data listener.
*
* @private
* @param {Boolean} firstRun Indicates if hook was fired while Handsontable initialization.
*/
onAfterLoadData(firstRun) {
if (!firstRun) {
this.bindStrategy.createMap(this.hot.countSourceRows());
}
}
/**
* Destroys the plugin instance.
*/
destroy() {
this.bindStrategy.destroy();
super.destroy();
}
}
registerPlugin('bindRowsWithHeaders', BindRowsWithHeaders);
export default BindRowsWithHeaders;