@pretable/core 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +513 -62
- package/dist/index.d.cts +100 -11
- package/dist/index.d.ts +100 -11
- package/dist/index.mjs +513 -62
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -33,6 +33,197 @@ function autosizeColumns(input) {
|
|
|
33
33
|
return { widths };
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// ../grid-core/dist/evaluate-filter.js
|
|
37
|
+
var NO_OPERAND = /* @__PURE__ */ new Set([
|
|
38
|
+
"isEmpty",
|
|
39
|
+
"isNotEmpty"
|
|
40
|
+
]);
|
|
41
|
+
function isFilterActive(filter) {
|
|
42
|
+
const { operator, value } = filter;
|
|
43
|
+
if (NO_OPERAND.has(operator))
|
|
44
|
+
return true;
|
|
45
|
+
if (value === null || value === void 0)
|
|
46
|
+
return false;
|
|
47
|
+
if (typeof value === "string")
|
|
48
|
+
return value.trim() !== "";
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.length > 0;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function isEmptyCell(cell) {
|
|
54
|
+
if (cell === null || cell === void 0)
|
|
55
|
+
return true;
|
|
56
|
+
if (typeof cell === "number")
|
|
57
|
+
return Number.isNaN(cell);
|
|
58
|
+
return String(cell).trim() === "";
|
|
59
|
+
}
|
|
60
|
+
function toBooleanCell(value) {
|
|
61
|
+
if (typeof value === "boolean")
|
|
62
|
+
return value;
|
|
63
|
+
if (value === "true" || value === 1 || value === "1")
|
|
64
|
+
return true;
|
|
65
|
+
if (value === "false" || value === 0 || value === "0")
|
|
66
|
+
return false;
|
|
67
|
+
return Boolean(value);
|
|
68
|
+
}
|
|
69
|
+
var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
70
|
+
var ISO_DATETIME_RE = /^(\d{4}-\d{2}-\d{2})[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/i;
|
|
71
|
+
var MAX_ISO_YEAR = 9999;
|
|
72
|
+
var GREGORIAN_400Y_MS = 146097 * 864e5;
|
|
73
|
+
function utcMs(year, month, day) {
|
|
74
|
+
return year >= 0 && year < 100 ? Date.UTC(year + 400, month, day) - GREGORIAN_400Y_MS : Date.UTC(year, month, day);
|
|
75
|
+
}
|
|
76
|
+
function utcDayOf(ms) {
|
|
77
|
+
const d = new Date(ms);
|
|
78
|
+
if (Number.isNaN(d.getTime()))
|
|
79
|
+
return Number.NaN;
|
|
80
|
+
const year = d.getUTCFullYear();
|
|
81
|
+
if (year < 0 || year > MAX_ISO_YEAR)
|
|
82
|
+
return Number.NaN;
|
|
83
|
+
d.setUTCHours(0, 0, 0, 0);
|
|
84
|
+
return d.getTime();
|
|
85
|
+
}
|
|
86
|
+
function isoDayMs(iso) {
|
|
87
|
+
if (!ISO_DATE_RE.test(iso))
|
|
88
|
+
return Number.NaN;
|
|
89
|
+
const [y, m, d] = iso.split("-").map(Number);
|
|
90
|
+
const ms = utcMs(y, m - 1, d);
|
|
91
|
+
const back = new Date(ms);
|
|
92
|
+
return back.getUTCFullYear() === y && back.getUTCMonth() === m - 1 && back.getUTCDate() === d ? ms : Number.NaN;
|
|
93
|
+
}
|
|
94
|
+
function toDayMs(input) {
|
|
95
|
+
if (input instanceof Date)
|
|
96
|
+
return utcDayOf(input.getTime());
|
|
97
|
+
if (typeof input === "number")
|
|
98
|
+
return utcDayOf(input);
|
|
99
|
+
if (typeof input !== "string")
|
|
100
|
+
return Number.NaN;
|
|
101
|
+
const trimmed = input.trim();
|
|
102
|
+
const dateOnly = isoDayMs(trimmed);
|
|
103
|
+
if (!Number.isNaN(dateOnly))
|
|
104
|
+
return dateOnly;
|
|
105
|
+
const parts = ISO_DATETIME_RE.exec(trimmed);
|
|
106
|
+
if (!parts)
|
|
107
|
+
return Number.NaN;
|
|
108
|
+
const day = isoDayMs(parts[1]);
|
|
109
|
+
if (Number.isNaN(day))
|
|
110
|
+
return Number.NaN;
|
|
111
|
+
return parts[2] ? utcDayOf(Date.parse(trimmed.replace(" ", "T"))) : day;
|
|
112
|
+
}
|
|
113
|
+
function evaluateFilter(cell, type, operator, value) {
|
|
114
|
+
if (operator === "isEmpty")
|
|
115
|
+
return isEmptyCell(cell);
|
|
116
|
+
if (operator === "isNotEmpty")
|
|
117
|
+
return !isEmptyCell(cell);
|
|
118
|
+
switch (type) {
|
|
119
|
+
case "number": {
|
|
120
|
+
const n = typeof cell === "number" ? cell : Number(cell);
|
|
121
|
+
if (Number.isNaN(n))
|
|
122
|
+
return false;
|
|
123
|
+
switch (operator) {
|
|
124
|
+
case "equals":
|
|
125
|
+
return n === Number(value);
|
|
126
|
+
case "notEquals":
|
|
127
|
+
return n !== Number(value);
|
|
128
|
+
case "gt":
|
|
129
|
+
return n > Number(value);
|
|
130
|
+
case "gte":
|
|
131
|
+
return n >= Number(value);
|
|
132
|
+
case "lt":
|
|
133
|
+
return n < Number(value);
|
|
134
|
+
case "lte":
|
|
135
|
+
return n <= Number(value);
|
|
136
|
+
case "between": {
|
|
137
|
+
if (!Array.isArray(value))
|
|
138
|
+
return false;
|
|
139
|
+
const a = Number(value[0]);
|
|
140
|
+
const b = Number(value[1]);
|
|
141
|
+
const lo = Math.min(a, b);
|
|
142
|
+
const hi = Math.max(a, b);
|
|
143
|
+
return n >= lo && n <= hi;
|
|
144
|
+
}
|
|
145
|
+
default:
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
case "date": {
|
|
150
|
+
const c = toDayMs(cell);
|
|
151
|
+
if (Number.isNaN(c))
|
|
152
|
+
return false;
|
|
153
|
+
switch (operator) {
|
|
154
|
+
case "on":
|
|
155
|
+
return c === toDayMs(value);
|
|
156
|
+
case "before":
|
|
157
|
+
return c < toDayMs(value);
|
|
158
|
+
case "after":
|
|
159
|
+
return c > toDayMs(value);
|
|
160
|
+
case "dateBetween": {
|
|
161
|
+
if (!Array.isArray(value))
|
|
162
|
+
return false;
|
|
163
|
+
const a = toDayMs(value[0]);
|
|
164
|
+
const b = toDayMs(value[1]);
|
|
165
|
+
if (Number.isNaN(a) || Number.isNaN(b))
|
|
166
|
+
return false;
|
|
167
|
+
const lo = Math.min(a, b);
|
|
168
|
+
const hi = Math.max(a, b);
|
|
169
|
+
return c >= lo && c <= hi;
|
|
170
|
+
}
|
|
171
|
+
default:
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
case "boolean": {
|
|
176
|
+
const c = String(toBooleanCell(cell));
|
|
177
|
+
const set = Array.isArray(value) ? value.map(String) : [];
|
|
178
|
+
if (set.length === 0)
|
|
179
|
+
return true;
|
|
180
|
+
switch (operator) {
|
|
181
|
+
case "isAnyOf":
|
|
182
|
+
return set.includes(c);
|
|
183
|
+
case "isNoneOf":
|
|
184
|
+
return !set.includes(c);
|
|
185
|
+
default:
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
case "enum": {
|
|
190
|
+
const c = String(cell);
|
|
191
|
+
const set = Array.isArray(value) ? value.map(String) : [];
|
|
192
|
+
if (set.length === 0)
|
|
193
|
+
return true;
|
|
194
|
+
switch (operator) {
|
|
195
|
+
case "isAnyOf":
|
|
196
|
+
return set.includes(c);
|
|
197
|
+
case "isNoneOf":
|
|
198
|
+
return !set.includes(c);
|
|
199
|
+
default:
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
case "text":
|
|
204
|
+
default: {
|
|
205
|
+
const hay = String(cell ?? "").toLowerCase();
|
|
206
|
+
const needle = String(value ?? "").toLowerCase();
|
|
207
|
+
switch (operator) {
|
|
208
|
+
case "contains":
|
|
209
|
+
return hay.includes(needle);
|
|
210
|
+
case "notContains":
|
|
211
|
+
return !hay.includes(needle);
|
|
212
|
+
case "equals":
|
|
213
|
+
return hay === needle;
|
|
214
|
+
case "notEquals":
|
|
215
|
+
return hay !== needle;
|
|
216
|
+
case "startsWith":
|
|
217
|
+
return hay.startsWith(needle);
|
|
218
|
+
case "endsWith":
|
|
219
|
+
return hay.endsWith(needle);
|
|
220
|
+
default:
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
36
227
|
// ../grid-core/dist/derived-rows.js
|
|
37
228
|
function createSourceRows(options) {
|
|
38
229
|
return options.rows.map((row, index) => ({
|
|
@@ -54,22 +245,20 @@ function deriveVisibleRows(input) {
|
|
|
54
245
|
function resolveFilters(columns, filters) {
|
|
55
246
|
const columnMap = new Map(columns.map((c) => [c.id, c]));
|
|
56
247
|
const resolved = [];
|
|
57
|
-
for (const [columnId,
|
|
58
|
-
if (!
|
|
248
|
+
for (const [columnId, filter] of Object.entries(filters)) {
|
|
249
|
+
if (!filter || !isFilterActive(filter))
|
|
59
250
|
continue;
|
|
60
|
-
}
|
|
61
251
|
const column = columnMap.get(columnId);
|
|
62
|
-
if (!column)
|
|
252
|
+
if (!column || column.filterable === false)
|
|
63
253
|
continue;
|
|
64
|
-
}
|
|
65
|
-
resolved.push({ column, needle: rawNeedle.toLowerCase() });
|
|
254
|
+
resolved.push({ column, filter });
|
|
66
255
|
}
|
|
67
256
|
return resolved;
|
|
68
257
|
}
|
|
69
258
|
function matchesFilters(row, resolvedFilters) {
|
|
70
|
-
for (const { column,
|
|
71
|
-
const
|
|
72
|
-
if (!
|
|
259
|
+
for (const { column, filter } of resolvedFilters) {
|
|
260
|
+
const cell = readCellValue(row, column);
|
|
261
|
+
if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
|
|
73
262
|
return false;
|
|
74
263
|
}
|
|
75
264
|
}
|
|
@@ -80,30 +269,31 @@ var collator = new Intl.Collator(void 0, {
|
|
|
80
269
|
sensitivity: "base"
|
|
81
270
|
});
|
|
82
271
|
function sortRows(rows, columns, sort) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
272
|
+
const keys = sort.map((entry) => {
|
|
273
|
+
const column = columns.find((c) => c.id === entry.columnId);
|
|
274
|
+
if (!column)
|
|
275
|
+
return null;
|
|
276
|
+
const rawKeys = rows.map((r) => readCellValue(r.row, column));
|
|
277
|
+
const allNumeric = rawKeys.every((v) => typeof v === "number");
|
|
278
|
+
const multiplier = entry.direction === "asc" ? 1 : -1;
|
|
279
|
+
return allNumeric ? { kind: "num", keys: rawKeys, multiplier } : {
|
|
280
|
+
kind: "str",
|
|
281
|
+
keys: rawKeys.map((v) => String(v ?? "")),
|
|
282
|
+
multiplier
|
|
283
|
+
};
|
|
284
|
+
}).filter((k) => k !== null);
|
|
285
|
+
if (keys.length === 0) {
|
|
88
286
|
return [...rows];
|
|
89
287
|
}
|
|
90
|
-
const multiplier = sort.direction === "asc" ? 1 : -1;
|
|
91
|
-
const rawKeys = rows.map((entry) => readCellValue(entry.row, column));
|
|
92
|
-
const allNumeric = rawKeys.every((v) => typeof v === "number");
|
|
93
|
-
if (allNumeric) {
|
|
94
|
-
const numKeys = rawKeys;
|
|
95
|
-
const indexed2 = rows.map((_, i) => i);
|
|
96
|
-
indexed2.sort((a, b) => {
|
|
97
|
-
const diff = numKeys[a] - numKeys[b];
|
|
98
|
-
return diff !== 0 ? diff * multiplier : rows[a].sourceIndex - rows[b].sourceIndex;
|
|
99
|
-
});
|
|
100
|
-
return indexed2.map((i) => rows[i]);
|
|
101
|
-
}
|
|
102
|
-
const strKeys = rawKeys.map((v) => String(v ?? ""));
|
|
103
288
|
const indexed = rows.map((_, i) => i);
|
|
104
289
|
indexed.sort((a, b) => {
|
|
105
|
-
const
|
|
106
|
-
|
|
290
|
+
for (const key of keys) {
|
|
291
|
+
const cmp = key.kind === "num" ? key.keys[a] - key.keys[b] : collator.compare(key.keys[a], key.keys[b]);
|
|
292
|
+
if (cmp !== 0) {
|
|
293
|
+
return cmp * key.multiplier;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return rows[a].sourceIndex - rows[b].sourceIndex;
|
|
107
297
|
});
|
|
108
298
|
return indexed.map((i) => rows[i]);
|
|
109
299
|
}
|
|
@@ -136,20 +326,40 @@ function applyAutosize(options, autosizeOptions) {
|
|
|
136
326
|
});
|
|
137
327
|
return { ...options, columns: nextColumns };
|
|
138
328
|
}
|
|
329
|
+
function groupColumnsByPin(columns) {
|
|
330
|
+
const left = [];
|
|
331
|
+
const unpinned = [];
|
|
332
|
+
const right = [];
|
|
333
|
+
for (const column of columns) {
|
|
334
|
+
const region = column.pinned === "left" ? left : column.pinned === "right" ? right : unpinned;
|
|
335
|
+
if (column.id === ROW_SELECT_COLUMN_ID) {
|
|
336
|
+
region.unshift(column);
|
|
337
|
+
} else {
|
|
338
|
+
region.push(column);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return [...left, ...unpinned, ...right];
|
|
342
|
+
}
|
|
139
343
|
function createGridCore(inputOptions) {
|
|
140
344
|
const listeners = /* @__PURE__ */ new Set();
|
|
141
|
-
|
|
142
|
-
|
|
345
|
+
const groupedInput = {
|
|
346
|
+
...inputOptions,
|
|
347
|
+
columns: groupColumnsByPin(inputOptions.columns)
|
|
348
|
+
};
|
|
349
|
+
let options = groupedInput.autosize ? applyAutosize(groupedInput, typeof groupedInput.autosize === "object" ? groupedInput.autosize : void 0) : groupedInput;
|
|
350
|
+
let originalColumns = groupColumnsByPin(inputOptions.columns).map((c) => ({ ...c }));
|
|
143
351
|
let sourceRows = createSourceRows(options);
|
|
144
352
|
const sourceRowIndex = new Map(sourceRows.map((entry) => [entry.id, entry]));
|
|
353
|
+
const pinnedWidthColumnIds = /* @__PURE__ */ new Set();
|
|
145
354
|
let cachedSnapshot = null;
|
|
146
355
|
let cachedVisibleRows = null;
|
|
147
356
|
let cachedDerivedSort = null;
|
|
148
357
|
let cachedDerivedFilters = null;
|
|
149
|
-
let sort =
|
|
358
|
+
let sort = [];
|
|
150
359
|
let filters = {};
|
|
151
360
|
let selection = { ranges: [], anchor: null };
|
|
152
361
|
let focus = { rowId: null, columnId: null };
|
|
362
|
+
let editing = null;
|
|
153
363
|
let viewport = {
|
|
154
364
|
scrollTop: 0,
|
|
155
365
|
scrollLeft: 0,
|
|
@@ -168,22 +378,31 @@ function createGridCore(inputOptions) {
|
|
|
168
378
|
},
|
|
169
379
|
getSnapshot,
|
|
170
380
|
setSort(columnId, direction) {
|
|
171
|
-
|
|
381
|
+
const candidate = columnId && direction ? [{ columnId, direction }] : [];
|
|
382
|
+
const next = sanitizeSortEntries(candidate, options.columns);
|
|
383
|
+
if (sortsEqual(sort, next)) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
sort = next;
|
|
387
|
+
emit();
|
|
388
|
+
},
|
|
389
|
+
replaceSort(entries) {
|
|
390
|
+
const next = sanitizeSortEntries(entries, options.columns);
|
|
391
|
+
if (sortsEqual(sort, next)) {
|
|
172
392
|
return;
|
|
173
393
|
}
|
|
174
|
-
sort =
|
|
394
|
+
sort = next;
|
|
175
395
|
emit();
|
|
176
396
|
},
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (currentValue === trimmed) {
|
|
397
|
+
setColumnFilter(columnId, filter) {
|
|
398
|
+
const current = filters[columnId];
|
|
399
|
+
if (filter && isFilterActive(filter)) {
|
|
400
|
+
if (current && columnFilterEqual(current, filter)) {
|
|
182
401
|
return;
|
|
183
402
|
}
|
|
184
|
-
filters = { ...filters, [columnId]:
|
|
403
|
+
filters = { ...filters, [columnId]: filter };
|
|
185
404
|
} else {
|
|
186
|
-
if (
|
|
405
|
+
if (current === void 0) {
|
|
187
406
|
return;
|
|
188
407
|
}
|
|
189
408
|
const next = { ...filters };
|
|
@@ -201,10 +420,9 @@ function createGridCore(inputOptions) {
|
|
|
201
420
|
},
|
|
202
421
|
replaceFilters(nextFilters) {
|
|
203
422
|
const normalized = {};
|
|
204
|
-
for (const [columnId,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
normalized[columnId] = trimmed;
|
|
423
|
+
for (const [columnId, filter] of Object.entries(nextFilters)) {
|
|
424
|
+
if (filter && isFilterActive(filter)) {
|
|
425
|
+
normalized[columnId] = filter;
|
|
208
426
|
}
|
|
209
427
|
}
|
|
210
428
|
if (filtersEqual(filters, normalized)) {
|
|
@@ -213,6 +431,25 @@ function createGridCore(inputOptions) {
|
|
|
213
431
|
filters = normalized;
|
|
214
432
|
emit();
|
|
215
433
|
},
|
|
434
|
+
distinctColumnValues(columnId) {
|
|
435
|
+
const column = options.columns.find((c) => c.id === columnId);
|
|
436
|
+
if (!column) {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
const seen = /* @__PURE__ */ new Set();
|
|
440
|
+
for (const entry of sourceRows) {
|
|
441
|
+
const raw = column.value ? column.value(entry.row) : entry.row[columnId];
|
|
442
|
+
if (raw === null || raw === void 0) {
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
const s = String(raw);
|
|
446
|
+
if (s.trim() === "") {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
seen.add(s);
|
|
450
|
+
}
|
|
451
|
+
return [...seen].sort((a, b) => a.localeCompare(b));
|
|
452
|
+
},
|
|
216
453
|
setSelection(next) {
|
|
217
454
|
if (selectionsEqual(selection, next)) {
|
|
218
455
|
return;
|
|
@@ -491,6 +728,7 @@ function createGridCore(inputOptions) {
|
|
|
491
728
|
const nextColumns = options.columns.slice();
|
|
492
729
|
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
493
730
|
options = { ...options, columns: nextColumns };
|
|
731
|
+
pinnedWidthColumnIds.add(columnId);
|
|
494
732
|
emit();
|
|
495
733
|
},
|
|
496
734
|
moveColumn(columnId, toIndex) {
|
|
@@ -514,26 +752,73 @@ function createGridCore(inputOptions) {
|
|
|
514
752
|
return;
|
|
515
753
|
}
|
|
516
754
|
nextColumns.splice(clampedTo, 0, moved);
|
|
517
|
-
let
|
|
518
|
-
for (let i =
|
|
755
|
+
let leftBoundary = minIndex;
|
|
756
|
+
for (let i = minIndex; i < nextColumns.length; i += 1) {
|
|
519
757
|
if (i === clampedTo) {
|
|
520
758
|
continue;
|
|
521
759
|
}
|
|
522
760
|
if (nextColumns[i]?.pinned === "left") {
|
|
523
|
-
|
|
761
|
+
leftBoundary = i + 1;
|
|
524
762
|
} else {
|
|
525
763
|
break;
|
|
526
764
|
}
|
|
527
765
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
766
|
+
let rightBoundary = nextColumns.length;
|
|
767
|
+
for (let i = nextColumns.length - 1; i >= minIndex; i -= 1) {
|
|
768
|
+
if (i === clampedTo) {
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (nextColumns[i]?.pinned === "right") {
|
|
772
|
+
rightBoundary = i;
|
|
773
|
+
} else {
|
|
774
|
+
break;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
const nextPinned = clampedTo < leftBoundary ? "left" : clampedTo >= rightBoundary ? "right" : void 0;
|
|
778
|
+
if (nextPinned !== moved.pinned) {
|
|
532
779
|
nextColumns[clampedTo] = { ...moved, pinned: nextPinned };
|
|
533
780
|
}
|
|
534
781
|
options = { ...options, columns: nextColumns };
|
|
535
782
|
emit();
|
|
536
783
|
},
|
|
784
|
+
setColumnOrder(ids) {
|
|
785
|
+
const byId = new Map(options.columns.map((c) => [c.id, c]));
|
|
786
|
+
const taken = /* @__PURE__ */ new Set();
|
|
787
|
+
const ordered = [];
|
|
788
|
+
for (const id of ids) {
|
|
789
|
+
if (id === ROW_SELECT_COLUMN_ID || taken.has(id)) {
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
const column = byId.get(id);
|
|
793
|
+
if (!column) {
|
|
794
|
+
continue;
|
|
795
|
+
}
|
|
796
|
+
taken.add(id);
|
|
797
|
+
ordered.push(column);
|
|
798
|
+
}
|
|
799
|
+
for (const column of options.columns) {
|
|
800
|
+
if (taken.has(column.id)) {
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
taken.add(column.id);
|
|
804
|
+
ordered.push(column);
|
|
805
|
+
}
|
|
806
|
+
const nextColumns = groupColumnsByPin(ordered);
|
|
807
|
+
let changed = nextColumns.length !== options.columns.length;
|
|
808
|
+
if (!changed) {
|
|
809
|
+
for (let i = 0; i < nextColumns.length; i += 1) {
|
|
810
|
+
if (nextColumns[i].id !== options.columns[i].id) {
|
|
811
|
+
changed = true;
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
if (!changed) {
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
options = { ...options, columns: nextColumns };
|
|
820
|
+
emit();
|
|
821
|
+
},
|
|
537
822
|
setColumnPinned(columnId, pinned) {
|
|
538
823
|
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
539
824
|
return;
|
|
@@ -543,7 +828,7 @@ function createGridCore(inputOptions) {
|
|
|
543
828
|
return;
|
|
544
829
|
}
|
|
545
830
|
const column = options.columns[idx];
|
|
546
|
-
const nextPinnedValue = pinned === "left"
|
|
831
|
+
const nextPinnedValue = pinned === "left" || pinned === "right" ? pinned : void 0;
|
|
547
832
|
if (column.pinned === nextPinnedValue) {
|
|
548
833
|
return;
|
|
549
834
|
}
|
|
@@ -555,7 +840,12 @@ function createGridCore(inputOptions) {
|
|
|
555
840
|
while (boundary < nextColumns.length && nextColumns[boundary]?.pinned === "left") {
|
|
556
841
|
boundary += 1;
|
|
557
842
|
}
|
|
558
|
-
|
|
843
|
+
let rightBoundary = nextColumns.length;
|
|
844
|
+
while (rightBoundary > boundary && nextColumns[rightBoundary - 1]?.pinned === "right") {
|
|
845
|
+
rightBoundary -= 1;
|
|
846
|
+
}
|
|
847
|
+
const staysAtRightBoundary = nextPinnedValue === "right" || nextPinnedValue === void 0 && column.pinned === "right";
|
|
848
|
+
const insertAt = staysAtRightBoundary ? rightBoundary : boundary;
|
|
559
849
|
const nextColumn = {
|
|
560
850
|
...column,
|
|
561
851
|
pinned: nextPinnedValue
|
|
@@ -627,9 +917,13 @@ function createGridCore(inputOptions) {
|
|
|
627
917
|
}
|
|
628
918
|
return { ...newCol };
|
|
629
919
|
});
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
920
|
+
const grouped = groupColumnsByPin(merged);
|
|
921
|
+
const changed = !sameColumnLayout(options.columns, grouped);
|
|
922
|
+
originalColumns = groupColumnsByPin(nextColumns).map((c) => ({ ...c }));
|
|
923
|
+
options = { ...options, columns: grouped };
|
|
924
|
+
if (changed) {
|
|
925
|
+
emit();
|
|
926
|
+
}
|
|
633
927
|
},
|
|
634
928
|
applyTransaction(transaction) {
|
|
635
929
|
if (!options.getRowId) {
|
|
@@ -680,6 +974,108 @@ function createGridCore(inputOptions) {
|
|
|
680
974
|
}
|
|
681
975
|
cachedVisibleRows = null;
|
|
682
976
|
emit();
|
|
977
|
+
},
|
|
978
|
+
setRows(nextRows) {
|
|
979
|
+
options = { ...options, rows: nextRows };
|
|
980
|
+
sourceRows = createSourceRows(options);
|
|
981
|
+
sourceRowIndex.clear();
|
|
982
|
+
for (const entry of sourceRows) {
|
|
983
|
+
sourceRowIndex.set(entry.id, entry);
|
|
984
|
+
}
|
|
985
|
+
const hasRow = (id) => id != null && sourceRowIndex.has(id);
|
|
986
|
+
const keptRanges = selection.ranges.filter((range) => hasRow(range.startRowId) && hasRow(range.endRowId));
|
|
987
|
+
const anchorValid = !selection.anchor || hasRow(selection.anchor.rowId);
|
|
988
|
+
if (keptRanges.length !== selection.ranges.length || !anchorValid) {
|
|
989
|
+
selection = {
|
|
990
|
+
ranges: keptRanges,
|
|
991
|
+
anchor: anchorValid ? selection.anchor : null
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
if (focus.rowId !== null && !hasRow(focus.rowId)) {
|
|
995
|
+
focus = { rowId: null, columnId: null };
|
|
996
|
+
}
|
|
997
|
+
if (editing && !hasRow(editing.rowId)) {
|
|
998
|
+
editing = null;
|
|
999
|
+
}
|
|
1000
|
+
if (options.autosize) {
|
|
1001
|
+
const measured = applyAutosize({ ...options, columns: originalColumns }, typeof options.autosize === "object" ? options.autosize : void 0);
|
|
1002
|
+
const measuredById = new Map(measured.columns.map((column) => [column.id, column.widthPx]));
|
|
1003
|
+
options = {
|
|
1004
|
+
...options,
|
|
1005
|
+
// Keyed by id, not index — `moveColumn` can leave the live order out
|
|
1006
|
+
// of step with `originalColumns`, and the live set may carry the
|
|
1007
|
+
// synthetic row-select column that never appears in props.
|
|
1008
|
+
columns: options.columns.map((column) => {
|
|
1009
|
+
if (pinnedWidthColumnIds.has(column.id)) {
|
|
1010
|
+
return column;
|
|
1011
|
+
}
|
|
1012
|
+
const width = measuredById.get(column.id);
|
|
1013
|
+
if (width === void 0 || width === column.widthPx) {
|
|
1014
|
+
return column;
|
|
1015
|
+
}
|
|
1016
|
+
return { ...column, widthPx: width };
|
|
1017
|
+
})
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
cachedVisibleRows = null;
|
|
1021
|
+
emit();
|
|
1022
|
+
},
|
|
1023
|
+
beginEdit(addr, opts) {
|
|
1024
|
+
editing = {
|
|
1025
|
+
rowId: addr.rowId,
|
|
1026
|
+
columnId: addr.columnId,
|
|
1027
|
+
draft: opts?.draft,
|
|
1028
|
+
status: opts?.status ?? "editing"
|
|
1029
|
+
};
|
|
1030
|
+
emit();
|
|
1031
|
+
},
|
|
1032
|
+
setEditDraft(value) {
|
|
1033
|
+
if (!editing)
|
|
1034
|
+
return;
|
|
1035
|
+
editing = { ...editing, draft: value };
|
|
1036
|
+
emit();
|
|
1037
|
+
},
|
|
1038
|
+
markEditing() {
|
|
1039
|
+
if (!editing || editing.status !== "checking")
|
|
1040
|
+
return;
|
|
1041
|
+
editing = { ...editing, status: "editing", error: void 0 };
|
|
1042
|
+
emit();
|
|
1043
|
+
},
|
|
1044
|
+
markEditValidating() {
|
|
1045
|
+
if (!editing)
|
|
1046
|
+
return;
|
|
1047
|
+
editing = { ...editing, status: "validating", error: void 0 };
|
|
1048
|
+
emit();
|
|
1049
|
+
},
|
|
1050
|
+
markEditSaving() {
|
|
1051
|
+
if (!editing)
|
|
1052
|
+
return;
|
|
1053
|
+
editing = { ...editing, status: "saving", error: void 0 };
|
|
1054
|
+
emit();
|
|
1055
|
+
},
|
|
1056
|
+
markEditInvalid(message) {
|
|
1057
|
+
if (!editing)
|
|
1058
|
+
return;
|
|
1059
|
+
editing = { ...editing, status: "editing", error: message };
|
|
1060
|
+
emit();
|
|
1061
|
+
},
|
|
1062
|
+
markEditError(message) {
|
|
1063
|
+
if (!editing)
|
|
1064
|
+
return;
|
|
1065
|
+
editing = { ...editing, status: "error", error: message };
|
|
1066
|
+
emit();
|
|
1067
|
+
},
|
|
1068
|
+
commitEditSucceeded() {
|
|
1069
|
+
if (!editing)
|
|
1070
|
+
return;
|
|
1071
|
+
editing = null;
|
|
1072
|
+
emit();
|
|
1073
|
+
},
|
|
1074
|
+
cancelEdit() {
|
|
1075
|
+
if (!editing)
|
|
1076
|
+
return;
|
|
1077
|
+
editing = null;
|
|
1078
|
+
emit();
|
|
683
1079
|
}
|
|
684
1080
|
};
|
|
685
1081
|
return store;
|
|
@@ -698,7 +1094,7 @@ function createGridCore(inputOptions) {
|
|
|
698
1094
|
cachedDerivedFilters = filters;
|
|
699
1095
|
cachedSnapshot = {
|
|
700
1096
|
viewport,
|
|
701
|
-
sort,
|
|
1097
|
+
sort: sort.map((entry) => ({ ...entry })),
|
|
702
1098
|
filters: { ...filters },
|
|
703
1099
|
selection: {
|
|
704
1100
|
ranges: selection.ranges.map((r) => ({ ...r })),
|
|
@@ -710,7 +1106,8 @@ function createGridCore(inputOptions) {
|
|
|
710
1106
|
visibleRange: {
|
|
711
1107
|
start: 0,
|
|
712
1108
|
end: visibleRows.length
|
|
713
|
-
}
|
|
1109
|
+
},
|
|
1110
|
+
editing: editing ? { ...editing } : null
|
|
714
1111
|
};
|
|
715
1112
|
return cachedSnapshot;
|
|
716
1113
|
}
|
|
@@ -724,6 +1121,15 @@ function createGridCore(inputOptions) {
|
|
|
724
1121
|
function clamp(value, min, max) {
|
|
725
1122
|
return Math.max(min, Math.min(max, value));
|
|
726
1123
|
}
|
|
1124
|
+
function sameColumnLayout(a, b) {
|
|
1125
|
+
if (a.length !== b.length) {
|
|
1126
|
+
return false;
|
|
1127
|
+
}
|
|
1128
|
+
return a.every((left, index) => {
|
|
1129
|
+
const right = b[index];
|
|
1130
|
+
return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
727
1133
|
function computePageStep(viewport, visibleRows) {
|
|
728
1134
|
if (viewport.height <= 0 || visibleRows.length === 0) {
|
|
729
1135
|
return 1;
|
|
@@ -753,6 +1159,36 @@ function selectionsEqual(a, b) {
|
|
|
753
1159
|
}
|
|
754
1160
|
return a.anchor.rowId === b.anchor.rowId && a.anchor.columnId === b.anchor.columnId;
|
|
755
1161
|
}
|
|
1162
|
+
function sanitizeSortEntries(entries, columns) {
|
|
1163
|
+
return entries.filter((entry) => {
|
|
1164
|
+
const column = columns.find((c) => c.id === entry.columnId);
|
|
1165
|
+
return column !== void 0 && column.sortable !== false;
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
function sortsEqual(a, b) {
|
|
1169
|
+
if (a.length !== b.length) {
|
|
1170
|
+
return false;
|
|
1171
|
+
}
|
|
1172
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
1173
|
+
const ae = a[i];
|
|
1174
|
+
const be = b[i];
|
|
1175
|
+
if (ae.columnId !== be.columnId || ae.direction !== be.direction) {
|
|
1176
|
+
return false;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return true;
|
|
1180
|
+
}
|
|
1181
|
+
function columnFilterEqual(a, b) {
|
|
1182
|
+
if (a.operator !== b.operator) {
|
|
1183
|
+
return false;
|
|
1184
|
+
}
|
|
1185
|
+
const av = a.value;
|
|
1186
|
+
const bv = b.value;
|
|
1187
|
+
if (Array.isArray(av) && Array.isArray(bv)) {
|
|
1188
|
+
return av.length === bv.length && av.every((v, i) => v === bv[i]);
|
|
1189
|
+
}
|
|
1190
|
+
return av === bv;
|
|
1191
|
+
}
|
|
756
1192
|
function filtersEqual(a, b) {
|
|
757
1193
|
const aKeys = Object.keys(a);
|
|
758
1194
|
const bKeys = Object.keys(b);
|
|
@@ -760,7 +1196,9 @@ function filtersEqual(a, b) {
|
|
|
760
1196
|
return false;
|
|
761
1197
|
}
|
|
762
1198
|
for (const key of aKeys) {
|
|
763
|
-
|
|
1199
|
+
const av = a[key];
|
|
1200
|
+
const bv = b[key];
|
|
1201
|
+
if (!av || !bv || !columnFilterEqual(av, bv)) {
|
|
764
1202
|
return false;
|
|
765
1203
|
}
|
|
766
1204
|
}
|
|
@@ -778,9 +1216,11 @@ function createGrid(options) {
|
|
|
778
1216
|
subscribe: engine.subscribe,
|
|
779
1217
|
getSnapshot: engine.getSnapshot,
|
|
780
1218
|
setSort: engine.setSort,
|
|
781
|
-
|
|
1219
|
+
replaceSort: engine.replaceSort,
|
|
1220
|
+
setColumnFilter: engine.setColumnFilter,
|
|
782
1221
|
clearFilters: engine.clearFilters,
|
|
783
1222
|
replaceFilters: engine.replaceFilters,
|
|
1223
|
+
distinctColumnValues: engine.distinctColumnValues,
|
|
784
1224
|
setSelection: engine.setSelection,
|
|
785
1225
|
selectAll: engine.selectAll,
|
|
786
1226
|
clearSelection: engine.clearSelection,
|
|
@@ -794,11 +1234,22 @@ function createGrid(options) {
|
|
|
794
1234
|
autosizeColumns: engine.autosizeColumns,
|
|
795
1235
|
setColumnWidth: engine.setColumnWidth,
|
|
796
1236
|
moveColumn: engine.moveColumn,
|
|
1237
|
+
setColumnOrder: engine.setColumnOrder,
|
|
797
1238
|
setColumnPinned: engine.setColumnPinned,
|
|
798
1239
|
autosizeColumn: engine.autosizeColumn,
|
|
799
1240
|
resetColumnLayout: engine.resetColumnLayout,
|
|
800
1241
|
mergeColumnsFromProps: engine.mergeColumnsFromProps,
|
|
801
|
-
applyTransaction: engine.applyTransaction
|
|
1242
|
+
applyTransaction: engine.applyTransaction,
|
|
1243
|
+
setRows: engine.setRows,
|
|
1244
|
+
beginEdit: engine.beginEdit,
|
|
1245
|
+
setEditDraft: engine.setEditDraft,
|
|
1246
|
+
markEditing: engine.markEditing,
|
|
1247
|
+
markEditValidating: engine.markEditValidating,
|
|
1248
|
+
markEditSaving: engine.markEditSaving,
|
|
1249
|
+
markEditInvalid: engine.markEditInvalid,
|
|
1250
|
+
markEditError: engine.markEditError,
|
|
1251
|
+
commitEditSucceeded: engine.commitEditSucceeded,
|
|
1252
|
+
cancelEdit: engine.cancelEdit
|
|
802
1253
|
};
|
|
803
1254
|
}
|
|
804
1255
|
|