/
manualColumnFreeze.js
201 lines (168 loc) · 5.95 KB
/
manualColumnFreeze.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
import { BasePlugin } from '../base';
import Hooks from '../../pluginHooks';
import freezeColumnItem from './contextMenuItem/freezeColumn';
import unfreezeColumnItem from './contextMenuItem/unfreezeColumn';
import './manualColumnFreeze.css';
Hooks.getSingleton().register('beforeColumnFreeze');
Hooks.getSingleton().register('afterColumnFreeze');
Hooks.getSingleton().register('beforeColumnUnfreeze');
Hooks.getSingleton().register('afterColumnUnfreeze');
export const PLUGIN_KEY = 'manualColumnFreeze';
export const PLUGIN_PRIORITY = 110;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @plugin ManualColumnFreeze
* @class ManualColumnFreeze
*
* @description
* This plugin allows to manually "freeze" and "unfreeze" a column using an entry in the Context Menu or using API.
* You can turn it on by setting a {@link Options#manualColumnFreeze} property to `true`.
*
* @example
* ```js
* // Enables the plugin
* manualColumnFreeze: true,
* ```
*/
export class ManualColumnFreeze extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
/**
* Determines when the moving operation is allowed.
*
* @type {boolean}
*/
#afterFirstUse = 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 ManualColumnFreeze#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.addHook('afterContextMenuDefaultOptions', options => this.#addContextMenuEntry(options));
this.addHook('beforeColumnMove', (columns, finalIndex) => this.#onBeforeColumnMove(columns, finalIndex));
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.#afterFirstUse = false;
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:
* - [`manualColumnFreeze`](@/api/options.md#manualcolumnfreeze)
*/
updatePlugin() {
this.disablePlugin();
this.enablePlugin();
super.updatePlugin();
}
/**
* Freezes the specified column (adds it to fixed columns).
*
* `freezeColumn()` doesn't re-render the table,
* so you need to call the `render()` method afterward.
*
* @param {number} column Visual column index.
*/
freezeColumn(column) {
const settings = this.hot.getSettings();
// columns are already fixed (frozen)
const freezePerformed = settings.fixedColumnsStart < this.hot.countCols()
&& column > settings.fixedColumnsStart - 1;
if (!this.#afterFirstUse) {
this.#afterFirstUse = true;
}
const beforeColumnFreezeHook = this.hot.runHooks('beforeColumnFreeze', column, freezePerformed);
if (beforeColumnFreezeHook === false) {
return;
}
if (freezePerformed) {
this.hot.columnIndexMapper.moveIndexes(column, settings.fixedColumnsStart);
// Since 12.0.0, the "fixedColumnsLeft" is replaced with the "fixedColumnsStart" option.
// However, keeping the old name still in effect. When both option names are used together,
// the error is thrown. To prevent that, the plugin needs to modify the original option key
// to bypass the validation.
settings._fixedColumnsStart += 1;
}
this.hot.runHooks('afterColumnFreeze', column, freezePerformed);
}
/**
* Unfreezes the given column (remove it from fixed columns and bring to it's previous position).
*
* @param {number} column Visual column index.
*/
unfreezeColumn(column) {
const settings = this.hot.getSettings();
// columns are not fixed (not frozen)
const unfreezePerformed = settings.fixedColumnsStart > 0 && (column <= settings.fixedColumnsStart - 1);
if (!this.#afterFirstUse) {
this.#afterFirstUse = true;
}
const beforeColumnUnfreezeHook = this.hot.runHooks('beforeColumnUnfreeze', column, unfreezePerformed);
if (beforeColumnUnfreezeHook === false) {
return;
}
if (unfreezePerformed) {
// Since 12.0.0, the "fixedColumnsLeft" is replaced with the "fixedColumnsStart" option.
// However, keeping the old name still in effect. When both option names are used together,
// the error is thrown. To prevent that, the plugin needs to modify the original option key
// to bypass the validation.
settings._fixedColumnsStart -= 1;
this.hot.columnIndexMapper.moveIndexes(column, settings.fixedColumnsStart);
}
this.hot.runHooks('afterColumnUnfreeze', column, unfreezePerformed);
}
/**
* Adds the manualColumnFreeze context menu entries.
*
* @private
* @param {object} options Context menu options.
*/
#addContextMenuEntry(options) {
options.items.push(
{ name: '---------' },
freezeColumnItem(this),
unfreezeColumnItem(this)
);
}
/**
* Prevents moving the columns from/to fixed area.
*
* @private
* @param {Array} columns Array of visual column indexes to be moved.
* @param {number} finalIndex Visual column index, being a start index for the moved columns. Points to where the elements will be placed after the moving action.
* @returns {boolean|undefined}
*/
#onBeforeColumnMove(columns, finalIndex) {
if (this.#afterFirstUse) {
const freezeLine = this.hot.getSettings().fixedColumnsStart;
// Moving any column before the "freeze line" isn't possible.
if (finalIndex < freezeLine) {
return false;
}
// Moving frozen column isn't possible.
if (columns.some(column => column < freezeLine)) {
return false;
}
}
}
}