-
Notifications
You must be signed in to change notification settings - Fork 3k
/
bindRowsWithHeaders.js
126 lines (108 loc) · 2.84 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
import { BasePlugin } from '../base';
import LooseBindsMap from './maps/looseBindsMap';
import StrictBindsMap from './maps/strictBindsMap';
export const PLUGIN_KEY = 'bindRowsWithHeaders';
export const PLUGIN_PRIORITY = 210;
const DEFAULT_BIND = 'loose';
const bindTypeToMapStrategy = new Map([
['loose', LooseBindsMap],
['strict', StrictBindsMap]
]);
/**
* @plugin BindRowsWithHeaders
* @class BindRowsWithHeaders
*
* @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
* ::: only-for javascript
* ```js
* const container = document.getElementById('example');
* const hot = new Handsontable(container, {
* data: getData(),
* // enable plugin
* bindRowsWithHeaders: true
* });
* ```
* :::
*
* ::: only-for react
* ```jsx
* <HotTable
* data={getData()}
* // enable plugin
* bindRowsWithHeaders={true}
* />
* ```
* :::
*/
export class BindRowsWithHeaders extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
constructor(hotInstance) {
super(hotInstance);
/**
* Plugin indexes cache.
*
* @private
* @type {null|IndexMap}
*/
this.headerIndexes = 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 BindRowsWithHeaders#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;
}
let bindType = this.hot.getSettings()[PLUGIN_KEY];
if (typeof bindType !== 'string') {
bindType = DEFAULT_BIND;
}
const MapStrategy = bindTypeToMapStrategy.get(bindType);
this.headerIndexes = this.hot.rowIndexMapper.registerMap('bindRowsWithHeaders', new MapStrategy());
this.addHook('modifyRowHeader', row => this.onModifyRowHeader(row));
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.hot.rowIndexMapper.unregisterMap('bindRowsWithHeaders');
super.disablePlugin();
}
/**
* On modify row header listener.
*
* @private
* @param {number} row Row index.
* @returns {number}
*/
onModifyRowHeader(row) {
return this.headerIndexes.getValueAtIndex(this.hot.toPhysicalRow(row));
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}