-
Notifications
You must be signed in to change notification settings - Fork 24
/
addSortBy.ts
235 lines (220 loc) · 6.68 KB
/
addSortBy.ts
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
import type { DataBodyCell } from '../bodyCells';
import type { BodyRow } from '../bodyRows';
import type { TablePlugin, NewTablePropSet, DeriveRowsFn } from '../types/TablePlugin';
import { compare } from '../utils/compare';
import { isShiftClick } from '../utils/event';
import { derived, writable, type Readable, type Writable } from 'svelte/store';
export interface SortByConfig {
initialSortKeys?: SortKey[];
disableMultiSort?: boolean;
isMultiSortEvent?: (event: Event) => boolean;
toggleOrder?: ('asc' | 'desc' | undefined)[];
serverSide?: boolean;
}
const DEFAULT_TOGGLE_ORDER: ('asc' | 'desc' | undefined)[] = ['asc', 'desc', undefined];
export interface SortByState<Item> {
sortKeys: WritableSortKeys;
preSortedRows: Readable<BodyRow<Item>[]>;
}
export interface SortByColumnOptions {
disable?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getSortValue?: (value: any) => string | number | (string | number)[];
invert?: boolean;
}
export type SortByPropSet = NewTablePropSet<{
'thead.tr.th': {
order: 'asc' | 'desc' | undefined;
toggle: (event: Event) => void;
clear: () => void;
disabled: boolean;
};
'tbody.tr.td': {
order: 'asc' | 'desc' | undefined;
};
}>;
export interface SortKey {
id: string;
order: 'asc' | 'desc';
}
export const createSortKeysStore = (initKeys: SortKey[]): WritableSortKeys => {
const { subscribe, update, set } = writable(initKeys);
const toggleId = (
id: string,
{ multiSort = true, toggleOrder = DEFAULT_TOGGLE_ORDER }: ToggleOptions = {}
) => {
update(($sortKeys) => {
const keyIdx = $sortKeys.findIndex((key) => key.id === id);
const key = $sortKeys[keyIdx];
const order = key?.order;
const orderIdx = toggleOrder.findIndex((o) => o === order);
const nextOrderIdx = (orderIdx + 1) % toggleOrder.length;
const nextOrder = toggleOrder[nextOrderIdx];
if (!multiSort) {
if (nextOrder === undefined) {
return [];
}
return [{ id, order: nextOrder }];
}
if (keyIdx === -1 && nextOrder !== undefined) {
return [...$sortKeys, { id, order: nextOrder }];
}
if (nextOrder === undefined) {
return [...$sortKeys.slice(0, keyIdx), ...$sortKeys.slice(keyIdx + 1)];
}
return [
...$sortKeys.slice(0, keyIdx),
{ id, order: nextOrder },
...$sortKeys.slice(keyIdx + 1),
];
});
};
const clearId = (id: string) => {
update(($sortKeys) => {
const keyIdx = $sortKeys.findIndex((key) => key.id === id);
if (keyIdx === -1) {
return $sortKeys;
}
return [...$sortKeys.slice(0, keyIdx), ...$sortKeys.slice(keyIdx + 1)];
});
};
return {
subscribe,
update,
set,
toggleId,
clearId,
};
};
interface ToggleOptions {
multiSort?: boolean;
toggleOrder?: ('asc' | 'desc' | undefined)[];
}
export type WritableSortKeys = Writable<SortKey[]> & {
toggleId: (id: string, options: ToggleOptions) => void;
clearId: (id: string) => void;
};
const getSortedRows = <Item, Row extends BodyRow<Item>>(
rows: Row[],
sortKeys: SortKey[],
columnOptions: Record<string, SortByColumnOptions>
): Row[] => {
// Shallow clone to prevent sort affecting `preSortedRows`.
const $sortedRows = [...rows] as typeof rows;
$sortedRows.sort((a, b) => {
for (const key of sortKeys) {
const invert = columnOptions[key.id]?.invert ?? false;
// TODO check why cellForId returns `undefined`.
const cellA = a.cellForId[key.id];
const cellB = b.cellForId[key.id];
let order = 0;
// Only need to check properties of `cellA` as both should have the same
// properties.
const getSortValue = columnOptions[key.id]?.getSortValue;
if (!cellA.isData()) {
return 0;
}
const valueA = cellA.value;
const valueB = (cellB as DataBodyCell<Item>).value;
if (getSortValue !== undefined) {
const sortValueA = getSortValue(valueA);
const sortValueB = getSortValue(valueB);
order = compare(sortValueA, sortValueB);
} else if (typeof valueA === 'string' || typeof valueA === 'number') {
// typeof `cellB.value` is logically equal to `cellA.value`.
order = compare(valueA, valueB as string | number);
}
if (order !== 0) {
let orderFactor = 1;
// If the current key order is `'desc'`, reverse the order.
if (key.order === 'desc') {
orderFactor *= -1;
}
// If `invert` is `true`, we want to invert the sort without
// affecting the view model's indication.
if (invert) {
orderFactor *= -1;
}
return order * orderFactor;
}
}
return 0;
});
for (let i = 0; i < $sortedRows.length; i++) {
const { subRows } = $sortedRows[i];
if (subRows === undefined) {
continue;
}
const sortedSubRows = getSortedRows<Item, Row>(subRows as Row[], sortKeys, columnOptions);
const clonedRow = $sortedRows[i].clone() as Row;
clonedRow.subRows = sortedSubRows;
$sortedRows[i] = clonedRow;
}
return $sortedRows;
};
export const addSortBy =
<Item>({
initialSortKeys = [],
disableMultiSort = false,
isMultiSortEvent = isShiftClick,
toggleOrder,
serverSide = false,
}: SortByConfig = {}): TablePlugin<Item, SortByState<Item>, SortByColumnOptions, SortByPropSet> =>
({ columnOptions }) => {
const disabledSortIds = Object.entries(columnOptions)
.filter(([, option]) => option.disable === true)
.map(([columnId]) => columnId);
const sortKeys = createSortKeysStore(initialSortKeys);
const preSortedRows = writable<BodyRow<Item>[]>([]);
const deriveRows: DeriveRowsFn<Item> = (rows) => {
return derived([rows, sortKeys], ([$rows, $sortKeys]) => {
preSortedRows.set($rows);
if (serverSide) {
return $rows;
}
return getSortedRows<Item, typeof $rows[number]>($rows, $sortKeys, columnOptions);
});
};
const pluginState: SortByState<Item> = { sortKeys, preSortedRows };
return {
pluginState,
deriveRows,
hooks: {
'thead.tr.th': (cell) => {
const disabled = disabledSortIds.includes(cell.id);
const props = derived(sortKeys, ($sortKeys) => {
const key = $sortKeys.find((k) => k.id === cell.id);
const toggle = (event: Event) => {
if (!cell.isData()) return;
if (disabled) return;
sortKeys.toggleId(cell.id, {
multiSort: disableMultiSort ? false : isMultiSortEvent(event),
toggleOrder,
});
};
const clear = () => {
if (!cell.isData()) return;
if (disabledSortIds.includes(cell.id)) return;
sortKeys.clearId(cell.id);
};
return {
order: key?.order,
toggle,
clear,
disabled,
};
});
return { props };
},
'tbody.tr.td': (cell) => {
const props = derived(sortKeys, ($sortKeys) => {
const key = $sortKeys.find((k) => k.id === cell.id);
return {
order: key?.order,
};
});
return { props };
},
},
};
};