/
multiColumnSorting.js
277 lines (251 loc) · 8.85 KB
/
multiColumnSorting.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
import { ColumnSorting } from '../columnSorting';
import { registerRootComparator } from '../columnSorting/sortService';
import { wasHeaderClickedProperly } from '../columnSorting/utils';
import { isPressedCtrlKey } from '../../utils/keyStateObserver';
import { addClass, removeClass } from '../../helpers/dom/element';
import { rootComparator } from './rootComparator';
import { warnAboutPluginsConflict } from './utils';
import { getClassesToAdd, getClassesToRemove } from './domHelpers';
import './multiColumnSorting.css';
export const PLUGIN_KEY = 'multiColumnSorting';
export const PLUGIN_PRIORITY = 170;
const APPEND_COLUMN_CONFIG_STRATEGY = 'append';
const CONFLICTED_PLUGIN_KEY = 'columnSorting';
registerRootComparator(PLUGIN_KEY, rootComparator);
/**
* @plugin MultiColumnSorting
* @class MultiColumnSorting
*
* @description
* This plugin sorts the view by columns (but does not sort the data source!). To enable the plugin, set the
* {@link Options#multiColumnSorting} property to the correct value (see the examples below).
*
* @example
* ```js
* // as boolean
* multiColumnSorting: true
*
* // as an object with initial sort config (sort ascending for column at index 1 and then sort descending for column at index 0)
* multiColumnSorting: {
* initialConfig: [{
* column: 1,
* sortOrder: 'asc'
* }, {
* column: 0,
* sortOrder: 'desc'
* }]
* }
*
* // as an object which define specific sorting options for all columns
* multiColumnSorting: {
* sortEmptyCells: true, // true = the table sorts empty cells, false = the table moves all empty cells to the end of the table (by default)
* indicator: true, // true = shows indicator for all columns (by default), false = don't show indicator for columns
* headerAction: true, // true = allow to click on the headers to sort (by default), false = turn off possibility to click on the headers to sort
* compareFunctionFactory: function(sortOrder, columnMeta) {
* return function(value, nextValue) {
* // Some value comparisons which will return -1, 0 or 1...
* }
* }
* }
*
* // as an object passed to the `column` property, allows specifying a custom options for the desired column.
* // please take a look at documentation of `column` property: {@link Options#columns}
* columns: [{
* multiColumnSorting: {
* indicator: false, // disable indicator for the first column,
* sortEmptyCells: true,
* headerAction: false, // clicks on the first column won't sort
* compareFunctionFactory: function(sortOrder, columnMeta) {
* return function(value, nextValue) {
* return 0; // Custom compare function for the first column (don't sort)
* }
* }
* }
* }]
* ```
*/
export class MultiColumnSorting extends ColumnSorting {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
constructor(hotInstance) {
super(hotInstance);
/**
* Main settings key designed for the plugin.
*
* @private
* @type {string}
*/
this.pluginKey = PLUGIN_KEY;
}
/**
* 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 MultiColumnSorting#enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return super.isEnabled();
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (!this.enabled && this.hot.getSettings()[this.pluginKey] && this.hot.getSettings()[CONFLICTED_PLUGIN_KEY]) {
warnAboutPluginsConflict();
}
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
super.disablePlugin();
}
/**
* Sorts the table by chosen columns and orders.
*
* @param {undefined|object|Array} sortConfig Single column sort configuration or full sort configuration (for all sorted columns).
* The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
* sort order (`asc` for ascending, `desc` for descending).
*
* **Note**: Please keep in mind that every call of `sort` function set an entirely new sort order. Previous sort configs aren't preserved.
*
* @example
* ```js
* // sort ascending first visual column
* hot.getPlugin('multiColumnSorting').sort({ column: 0, sortOrder: 'asc' });
*
* // sort first two visual column in the defined sequence
* hot.getPlugin('multiColumnSorting').sort([{
* column: 1, sortOrder: 'asc'
* }, {
* column: 0, sortOrder: 'desc'
* }]);
* ```
*
* @fires Hooks#beforeColumnSort
* @fires Hooks#afterColumnSort
*/
sort(sortConfig) {
super.sort(sortConfig);
}
/**
* Clear the sort performed on the table.
*/
clearSort() {
super.clearSort();
}
/**
* Checks if the table is sorted (any column have to be sorted).
*
* @returns {boolean}
*/
isSorted() {
return super.isSorted();
}
/**
* Get sort configuration for particular column or for all sorted columns. Objects contain `column` and `sortOrder` properties.
*
* **Note**: Please keep in mind that returned objects expose **visual** column index under the `column` key. They are handled by the `sort` function.
*
* @param {number} [column] Visual column index.
* @returns {undefined|object|Array}
*/
getSortConfig(column) {
return super.getSortConfig(column);
}
/**
* @description
* Warn: Useful mainly for providing server side sort implementation (see in the example below). It doesn't sort the data set. It just sets sort configuration for all sorted columns.
* Note: Please keep in mind that this method doesn't re-render the table.
*
* @example
* ```js
* beforeColumnSort: function(currentSortConfig, destinationSortConfigs) {
* const columnSortPlugin = this.getPlugin('multiColumnSorting');
*
* columnSortPlugin.setSortConfig(destinationSortConfigs);
*
* // const newData = ... // Calculated data set, ie. from an AJAX call.
*
* this.loadData(newData); // Load new data set and re-render the table.
*
* return false; // The blockade for the default sort action.
* }
* ```
*
* @param {undefined|object|Array} sortConfig Single column sort configuration or full sort configuration (for all sorted columns).
* The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
* sort order (`asc` for ascending, `desc` for descending).
*/
setSortConfig(sortConfig) {
super.setSortConfig(sortConfig);
}
/**
* Get normalized sort configs.
*
* @private
* @param {object|Array} [sortConfig=[]] Single column sort configuration or full sort configuration (for all sorted columns).
* The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
* sort order (`asc` for ascending, `desc` for descending).
* @returns {Array}
*/
getNormalizedSortConfigs(sortConfig = []) {
if (Array.isArray(sortConfig)) {
return sortConfig;
}
return [sortConfig];
}
/**
* Update header classes.
*
* @private
* @param {HTMLElement} headerSpanElement Header span element.
* @param {...*} args Extra arguments for helpers.
*/
updateHeaderClasses(headerSpanElement, ...args) {
super.updateHeaderClasses(headerSpanElement, ...args);
removeClass(headerSpanElement, getClassesToRemove(headerSpanElement));
if (this.enabled !== false) {
addClass(headerSpanElement, getClassesToAdd(...args));
}
}
/**
* Overwriting base plugin's `onUpdateSettings` method. Please keep in mind that `onAfterUpdateSettings` isn't called
* for `updateSettings` in specific situations.
*
* @private
* @param {object} newSettings New settings object.
*/
onUpdateSettings(newSettings) {
if (this.hot.getSettings()[this.pluginKey] && this.hot.getSettings()[CONFLICTED_PLUGIN_KEY]) {
warnAboutPluginsConflict();
}
super.onUpdateSettings(newSettings);
}
/**
* Callback for the `onAfterOnCellMouseDown` hook.
*
* @private
* @param {Event} event Event which are provided by hook.
* @param {CellCoords} coords Visual coords of the selected cell.
*/
onAfterOnCellMouseDown(event, coords) {
if (wasHeaderClickedProperly(coords.row, coords.col, event) === false) {
return;
}
if (this.wasClickableHeaderClicked(event, coords.col)) {
if (isPressedCtrlKey()) {
this.hot.deselectCell();
this.hot.selectColumns(coords.col);
this.sort(this.getNextSortConfig(coords.col, APPEND_COLUMN_CONFIG_STRATEGY));
} else {
this.sort(this.getColumnNextConfig(coords.col));
}
}
}
}