-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhandsontable.ts
More file actions
218 lines (205 loc) · 6.41 KB
/
handsontable.ts
File metadata and controls
218 lines (205 loc) · 6.41 KB
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
import Handsontable from 'handsontable';
import type { Plugins } from 'handsontable/plugins';
import type { CellProperties } from 'handsontable/settings';
import { getCache } from '../../utils/cache';
import { writable } from '../../utils/types';
import { schema } from '../DataModel/schema';
import { userPreferences } from '../Preferences/userPreferences';
import type { Dataset } from '../WbPlanView/Wrapped';
import {
formatAttachmentsFromCell,
getAttachmentsColumn,
} from '../WorkBench/attachmentHelpers';
import type { BatchEditPack } from './batchEditHelpers';
import { BATCH_EDIT_KEY, isBatchEditNullRecord } from './batchEditHelpers';
import { getPhysicalColToMappingCol } from './hotHelpers';
import type { WbMapping } from './mapping';
import type { WbPickLists } from './pickLists';
export function configureHandsontable(
hot: Handsontable,
mappings: WbMapping | undefined,
dataset: Dataset,
pickLists: WbPickLists
): void {
identifyDefaultValues(hot, mappings);
curryCells(hot, mappings, dataset, pickLists);
setSort(hot, dataset);
}
export function identifyDefaultValues(
hot: Handsontable,
mappings: WbMapping | undefined
): void {
if (mappings === undefined) return;
const existingColumns = hot.getSettings()
.columns as readonly Handsontable.ColumnSettings[];
hot.updateSettings({
columns: (index) => ({
...existingColumns?.[index],
placeholder: mappings.defaultValues[index],
}),
});
}
type GetProperty = (
physicalRow: number,
physicalCol: number,
_property: number | string
) => Partial<CellProperties>;
function curryCells(
hot: Handsontable,
mappings: WbMapping | undefined,
dataset: Dataset,
pickLists: WbPickLists
): void {
const identifyPickLists = getPickListsIdentifier(pickLists);
const identifyNullRecords = getIdentifyNullRecords(hot, mappings, dataset);
const identifyAttachments = getAttachmentsIdentifier(dataset);
hot.updateSettings({
cells: (physicalRow, physicalColumn, property) => {
const pickListsResults =
identifyPickLists?.(physicalRow, physicalColumn, property) ?? {};
const attachmentsResults =
identifyAttachments?.(physicalRow, physicalColumn, property) ?? {};
const nullRecordsResults =
dataset.uploadresult?.success === true
? {}
: (identifyNullRecords?.(physicalRow, physicalColumn, property) ??
{});
return {
...pickListsResults,
...attachmentsResults,
...nullRecordsResults,
};
},
});
}
function getPickListsIdentifier(
pickLists: WbPickLists
): GetProperty | undefined {
const callback: GetProperty = (_physicalRow, physicalCol, _property) =>
physicalCol in pickLists
? {
type: 'autocomplete',
source: writable(pickLists[physicalCol].items),
strict: pickLists[physicalCol].readOnly,
allowInvalid: true,
filter:
userPreferences.get('workBench', 'editor', 'filterPickLists') ===
'none',
filteringCaseSensitive:
userPreferences.get('workBench', 'editor', 'filterPickLists') ===
'case-sensitive',
sortByRelevance: false,
trimDropdown: false,
}
: { type: 'text' };
return callback;
}
function getIdentifyNullRecords(
hot: Handsontable,
mappings: WbMapping | undefined,
dataset: Dataset
): GetProperty | undefined {
if (!dataset.isupdate || mappings === undefined) return undefined;
const makeNullRecordsReadOnly: GetProperty = (
physicalRow,
physicalCol,
_property
) => {
const physicalColToMappingCol = getPhysicalColToMappingCol(
mappings,
dataset
);
const mappingCol = physicalColToMappingCol(physicalCol);
if (mappingCol === -1 || mappingCol === undefined) {
// Definitely don't need to anything, not even mapped
return { readOnly: true };
}
const batchEditRaw: string | undefined =
hot.getDataAtRow(hot.toVisualRow(physicalRow)).at(-1) ?? undefined;
if (
batchEditRaw === undefined ||
// Will happen for new rows + rows auto-added at the bottom.
batchEditRaw.trim() === ''
) {
return { readOnly: false };
}
const batchEditPack: BatchEditPack | undefined =
JSON.parse(batchEditRaw)[BATCH_EDIT_KEY];
return {
readOnly: isBatchEditNullRecord(
batchEditPack,
mappings.lines[mappingCol].mappingPath
),
};
};
return makeNullRecordsReadOnly;
}
function getAttachmentsIdentifier(dataset: Dataset): GetProperty | undefined {
const attachmentsColumnIndex = getAttachmentsColumn(dataset);
const callback: GetProperty = (_physicalRow, physicalCol, _property) =>
physicalCol === attachmentsColumnIndex
? {
renderer: (
instance,
td,
row,
col,
property,
value,
...rest
): void => {
const formattedValue = formatAttachmentsFromCell(value);
const cellMeta = instance.getCellMeta(row, col);
cellMeta.formattedValue = formattedValue;
Handsontable.renderers.TextRenderer(
instance,
td,
row,
col,
property,
formattedValue,
...rest
);
},
readOnly: true,
}
: {};
return callback;
}
function setSort(hot: Handsontable, dataset: Dataset): void {
const sortConfig = getCache(
'workBenchSortConfig',
`${schema.domainLevelIds.collection}_${dataset.id}`
);
if (!Array.isArray(sortConfig)) return;
const visualSortConfig = sortConfig.map(({ physicalCol, ...rest }) => ({
...rest,
column: hot.toVisualColumn(physicalCol),
}));
getHotPlugin(hot, 'multiColumnSorting').sort(visualSortConfig);
}
/**
* Cached retrieved HOT plugins
* This improved performance in earlier versions
* REFACTOR: check if this is still necessary
*/
const hotPlugins = new WeakMap<
Handsontable,
// eslint-disable-next-line functional/prefer-readonly-type
{
[key in keyof Plugins]?: Plugins[key] | undefined;
}
>();
export function getHotPlugin<NAME extends keyof Plugins>(
hot: Handsontable,
pluginName: NAME
): Plugins[NAME] {
let plugins = hotPlugins.get(hot);
if (plugins === undefined) {
plugins = {};
hotPlugins.set(hot, plugins);
}
if (plugins[pluginName] === undefined)
plugins[pluginName] = hot.getPlugin(pluginName);
return plugins[pluginName]!;
}