@ys-knife-crud/vue 0.0.1
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/README.md +234 -0
- package/dist/index.cjs +1318 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +663 -0
- package/dist/index.d.ts +663 -0
- package/dist/index.js +1279 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1279 @@
|
|
|
1
|
+
// src/table/default.ts
|
|
2
|
+
import { computed, ref, watch } from "vue";
|
|
3
|
+
function useDefault(props) {
|
|
4
|
+
const meta = ref(null);
|
|
5
|
+
const paged = ref(null);
|
|
6
|
+
const metaLoading = ref(false);
|
|
7
|
+
const dataLoading = ref(false);
|
|
8
|
+
const currentPage = ref(1);
|
|
9
|
+
const rows = computed(
|
|
10
|
+
() => paged.value?.items ?? []
|
|
11
|
+
);
|
|
12
|
+
const totalKnown = computed(() => paged.value?.totalCount != null);
|
|
13
|
+
const total = computed(() => {
|
|
14
|
+
const p = paged.value;
|
|
15
|
+
if (!p) return 0;
|
|
16
|
+
if (p.totalCount != null) return p.totalCount;
|
|
17
|
+
return p.offset + p.items.length + (p.hasNext ? 1 : 0);
|
|
18
|
+
});
|
|
19
|
+
async function loadMeta(signal) {
|
|
20
|
+
metaLoading.value = true;
|
|
21
|
+
try {
|
|
22
|
+
meta.value = await props.metaFun(signal);
|
|
23
|
+
} finally {
|
|
24
|
+
metaLoading.value = false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
watch(() => props.metaFun, () => loadMeta());
|
|
28
|
+
return {
|
|
29
|
+
meta,
|
|
30
|
+
paged,
|
|
31
|
+
rows,
|
|
32
|
+
metaLoading,
|
|
33
|
+
dataLoading,
|
|
34
|
+
currentPage,
|
|
35
|
+
total,
|
|
36
|
+
totalKnown,
|
|
37
|
+
loadMeta
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/table/rowActions.ts
|
|
42
|
+
import { ref as ref2, watch as watch2 } from "vue";
|
|
43
|
+
function visibleActions(actions, row) {
|
|
44
|
+
return actions.filter((a) => a.show?.(row) ?? true);
|
|
45
|
+
}
|
|
46
|
+
function isEnabled(action, row) {
|
|
47
|
+
return action.enable?.(row) ?? true;
|
|
48
|
+
}
|
|
49
|
+
function useRowActions(props) {
|
|
50
|
+
const actions = ref2([]);
|
|
51
|
+
const actionsLoading = ref2(false);
|
|
52
|
+
async function loadActions(signal) {
|
|
53
|
+
if (!props.rowActionsFunc) {
|
|
54
|
+
actions.value = [];
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
actionsLoading.value = true;
|
|
58
|
+
try {
|
|
59
|
+
actions.value = await props.rowActionsFunc(signal);
|
|
60
|
+
} finally {
|
|
61
|
+
actionsLoading.value = false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
watch2(() => props.rowActionsFunc, () => loadActions());
|
|
65
|
+
return {
|
|
66
|
+
actions,
|
|
67
|
+
actionsLoading,
|
|
68
|
+
loadActions
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/table/selection.ts
|
|
73
|
+
import { computed as computed2, nextTick, ref as ref3, watch as watch3 } from "vue";
|
|
74
|
+
function useSelection({ tableEl, rows, rowKey }) {
|
|
75
|
+
const selectedMap = ref3(/* @__PURE__ */ new Map());
|
|
76
|
+
const selectedRows = computed2(() => [...selectedMap.value.values()]);
|
|
77
|
+
let restoring = false;
|
|
78
|
+
function keyOf(row) {
|
|
79
|
+
return row[rowKey.value];
|
|
80
|
+
}
|
|
81
|
+
function onSelectionChange(selection) {
|
|
82
|
+
if (restoring) return;
|
|
83
|
+
const next = new Map(selectedMap.value);
|
|
84
|
+
for (const row of rows.value) next.delete(keyOf(row));
|
|
85
|
+
for (const row of selection) next.set(keyOf(row), row);
|
|
86
|
+
selectedMap.value = next;
|
|
87
|
+
}
|
|
88
|
+
function isRowSelected(row) {
|
|
89
|
+
return selectedMap.value.has(keyOf(row));
|
|
90
|
+
}
|
|
91
|
+
function toggleRowSelection(row, selected) {
|
|
92
|
+
const next = new Map(selectedMap.value);
|
|
93
|
+
const shouldSelect = selected ?? !next.has(keyOf(row));
|
|
94
|
+
if (shouldSelect) next.set(keyOf(row), row);
|
|
95
|
+
else next.delete(keyOf(row));
|
|
96
|
+
selectedMap.value = next;
|
|
97
|
+
tableEl.value?.toggleRowSelection?.(row, shouldSelect);
|
|
98
|
+
}
|
|
99
|
+
async function restoreSelection() {
|
|
100
|
+
await nextTick();
|
|
101
|
+
const table = tableEl.value;
|
|
102
|
+
if (!table?.toggleRowSelection) {
|
|
103
|
+
restoring = false;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
restoring = true;
|
|
107
|
+
for (const row of rows.value) {
|
|
108
|
+
table.toggleRowSelection(row, selectedMap.value.has(keyOf(row)));
|
|
109
|
+
}
|
|
110
|
+
await nextTick();
|
|
111
|
+
restoring = false;
|
|
112
|
+
}
|
|
113
|
+
function clearSelection() {
|
|
114
|
+
selectedMap.value = /* @__PURE__ */ new Map();
|
|
115
|
+
tableEl.value?.clearSelection?.();
|
|
116
|
+
}
|
|
117
|
+
function selectAllOnPage() {
|
|
118
|
+
const next = new Map(selectedMap.value);
|
|
119
|
+
for (const row of rows.value) next.set(keyOf(row), row);
|
|
120
|
+
selectedMap.value = next;
|
|
121
|
+
void restoreSelection();
|
|
122
|
+
}
|
|
123
|
+
function invertSelectionOnPage() {
|
|
124
|
+
const next = new Map(selectedMap.value);
|
|
125
|
+
for (const row of rows.value) {
|
|
126
|
+
const key = keyOf(row);
|
|
127
|
+
if (next.has(key)) next.delete(key);
|
|
128
|
+
else next.set(key, row);
|
|
129
|
+
}
|
|
130
|
+
selectedMap.value = next;
|
|
131
|
+
void restoreSelection();
|
|
132
|
+
}
|
|
133
|
+
watch3(rows, () => {
|
|
134
|
+
restoring = true;
|
|
135
|
+
void restoreSelection();
|
|
136
|
+
}, { flush: "pre" });
|
|
137
|
+
return {
|
|
138
|
+
selectedRows,
|
|
139
|
+
onSelectionChange,
|
|
140
|
+
isRowSelected,
|
|
141
|
+
toggleRowSelection,
|
|
142
|
+
restoreSelection,
|
|
143
|
+
clearSelection,
|
|
144
|
+
selectAllOnPage,
|
|
145
|
+
invertSelectionOnPage
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/table/customConfig.ts
|
|
150
|
+
import { computed as computed3, ref as ref4, watch as watch4 } from "vue";
|
|
151
|
+
function displayableColumns(columns) {
|
|
152
|
+
return columns.map((col, idx) => ({ col, idx })).filter(({ col }) => col.showForDisplay ?? true).sort((a, b) => (a.col.displayOrder ?? a.idx) - (b.col.displayOrder ?? b.idx)).map(({ col }) => col);
|
|
153
|
+
}
|
|
154
|
+
function useCustomConfig(props, meta) {
|
|
155
|
+
const customConfigs = ref4({ columns: {} });
|
|
156
|
+
const customConfigLoading = ref4(false);
|
|
157
|
+
const defaultPageSize = ref4(void 0);
|
|
158
|
+
const configDialogVisible = ref4(false);
|
|
159
|
+
const draftColumns = ref4([]);
|
|
160
|
+
const columns = computed3(() => {
|
|
161
|
+
const sorted = displayableColumns(meta.value?.columns ?? []);
|
|
162
|
+
const cfg = customConfigs.value.columns;
|
|
163
|
+
const merged = sorted.map((col, idx) => ({ col, cfg: cfg[col.propertyPath], idx }));
|
|
164
|
+
const visibleCols = merged.filter((x) => x.cfg?.visible ?? true);
|
|
165
|
+
visibleCols.sort((a, b) => (a.cfg?.order ?? a.idx) - (b.cfg?.order ?? b.idx));
|
|
166
|
+
return visibleCols.map(({ col, cfg: c }) => ({
|
|
167
|
+
...col,
|
|
168
|
+
// displayName 缺省回落到 propertyPath(Column 允许只填 propertyPath)
|
|
169
|
+
displayName: col.displayName ?? col.propertyPath,
|
|
170
|
+
// 空字符串视为未设置(与原 columnWidth() 行为一致:返回 undefined)
|
|
171
|
+
width: c?.width || void 0
|
|
172
|
+
}));
|
|
173
|
+
});
|
|
174
|
+
async function loadCustomConfigs(signal) {
|
|
175
|
+
customConfigLoading.value = true;
|
|
176
|
+
try {
|
|
177
|
+
if (!props.loadCustomConfigFun) {
|
|
178
|
+
customConfigs.value = { columns: {} };
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const loaded = await props.loadCustomConfigFun(signal) ?? { columns: {} };
|
|
182
|
+
customConfigs.value = loaded;
|
|
183
|
+
if (loaded.pageSize !== void 0) {
|
|
184
|
+
defaultPageSize.value = loaded.pageSize;
|
|
185
|
+
}
|
|
186
|
+
} finally {
|
|
187
|
+
customConfigLoading.value = false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function updateDefaultPageSize(size) {
|
|
191
|
+
defaultPageSize.value = size;
|
|
192
|
+
const configs = { pageSize: size, columns: customConfigs.value.columns };
|
|
193
|
+
props.saveCustomConfigFun?.(configs);
|
|
194
|
+
customConfigs.value = configs;
|
|
195
|
+
}
|
|
196
|
+
const WIDTH_SAVE_DEBOUNCE_MS = 500;
|
|
197
|
+
let widthSaveTimer;
|
|
198
|
+
function scheduleWidthSave() {
|
|
199
|
+
if (widthSaveTimer !== void 0) clearTimeout(widthSaveTimer);
|
|
200
|
+
widthSaveTimer = setTimeout(() => {
|
|
201
|
+
widthSaveTimer = void 0;
|
|
202
|
+
void props.saveCustomConfigFun?.(customConfigs.value);
|
|
203
|
+
}, WIDTH_SAVE_DEBOUNCE_MS);
|
|
204
|
+
}
|
|
205
|
+
function updateColumnWidth(propertyPath, width) {
|
|
206
|
+
const prev = customConfigs.value.columns[propertyPath];
|
|
207
|
+
customConfigs.value = {
|
|
208
|
+
...customConfigs.value,
|
|
209
|
+
columns: {
|
|
210
|
+
...customConfigs.value.columns,
|
|
211
|
+
[propertyPath]: { ...prev, propertyPath, width: String(Math.round(width)) }
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
scheduleWidthSave();
|
|
215
|
+
}
|
|
216
|
+
function generateDraftColumns() {
|
|
217
|
+
const sorted = displayableColumns(meta.value?.columns ?? []);
|
|
218
|
+
const cfg = customConfigs.value.columns;
|
|
219
|
+
const merged = sorted.map((col, idx) => ({ col, cfg: cfg[col.propertyPath], idx }));
|
|
220
|
+
merged.sort((a, b) => (a.cfg?.order ?? a.idx) - (b.cfg?.order ?? b.idx));
|
|
221
|
+
return merged.map(({ col, cfg: c }) => ({
|
|
222
|
+
propertyPath: col.propertyPath,
|
|
223
|
+
displayName: col.displayName ?? col.propertyPath,
|
|
224
|
+
visible: c?.visible ?? true,
|
|
225
|
+
width: c?.width ?? ""
|
|
226
|
+
}));
|
|
227
|
+
}
|
|
228
|
+
function openConfigDialog() {
|
|
229
|
+
draftColumns.value = generateDraftColumns();
|
|
230
|
+
configDialogVisible.value = true;
|
|
231
|
+
}
|
|
232
|
+
function resetDraft() {
|
|
233
|
+
const sorted = displayableColumns(meta.value?.columns ?? []);
|
|
234
|
+
draftColumns.value = sorted.map((col) => ({
|
|
235
|
+
propertyPath: col.propertyPath,
|
|
236
|
+
displayName: col.displayName ?? col.propertyPath,
|
|
237
|
+
visible: true,
|
|
238
|
+
width: ""
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
function moveDraft(index, delta) {
|
|
242
|
+
const target = index + delta;
|
|
243
|
+
if (target < 0 || target >= draftColumns.value.length) return;
|
|
244
|
+
const arr = [...draftColumns.value];
|
|
245
|
+
const [item] = arr.splice(index, 1);
|
|
246
|
+
arr.splice(target, 0, item);
|
|
247
|
+
draftColumns.value = arr;
|
|
248
|
+
}
|
|
249
|
+
async function saveConfigDialog() {
|
|
250
|
+
const columns2 = {};
|
|
251
|
+
draftColumns.value.forEach((d, i) => {
|
|
252
|
+
columns2[d.propertyPath] = {
|
|
253
|
+
propertyPath: d.propertyPath,
|
|
254
|
+
visible: d.visible,
|
|
255
|
+
order: i,
|
|
256
|
+
width: d.width
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
const configs = { pageSize: defaultPageSize.value, columns: columns2 };
|
|
260
|
+
await props.saveCustomConfigFun?.(configs);
|
|
261
|
+
customConfigs.value = configs;
|
|
262
|
+
configDialogVisible.value = false;
|
|
263
|
+
}
|
|
264
|
+
watch4(() => props.loadCustomConfigFun, () => loadCustomConfigs());
|
|
265
|
+
return {
|
|
266
|
+
// 字段
|
|
267
|
+
customConfigs,
|
|
268
|
+
customConfigLoading,
|
|
269
|
+
defaultPageSize,
|
|
270
|
+
configDialogVisible,
|
|
271
|
+
draftColumns,
|
|
272
|
+
columns,
|
|
273
|
+
// 函数
|
|
274
|
+
loadCustomConfigs,
|
|
275
|
+
updateDefaultPageSize,
|
|
276
|
+
updateColumnWidth,
|
|
277
|
+
openConfigDialog,
|
|
278
|
+
moveDraft,
|
|
279
|
+
resetDraft,
|
|
280
|
+
saveConfigDialog
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/table/exportExcel.ts
|
|
285
|
+
import { computed as computed4, onScopeDispose, ref as ref5 } from "vue";
|
|
286
|
+
import {
|
|
287
|
+
createConsoleExportApiFunc
|
|
288
|
+
} from "@ys-knife-crud/core";
|
|
289
|
+
function useExportExcel({
|
|
290
|
+
props,
|
|
291
|
+
tableName,
|
|
292
|
+
columns,
|
|
293
|
+
currentRows,
|
|
294
|
+
selectedRows,
|
|
295
|
+
total,
|
|
296
|
+
exportPageSize,
|
|
297
|
+
hasMorePage
|
|
298
|
+
}) {
|
|
299
|
+
const exportOptions = computed4(() => {
|
|
300
|
+
const opts = [];
|
|
301
|
+
if (props.exportSelected) {
|
|
302
|
+
opts.push({
|
|
303
|
+
value: "selected",
|
|
304
|
+
label: `\u5BFC\u51FA\u9009\u4E2D\u7684\u6570\u636E\uFF08\u5DF2\u9009 ${selectedRows.value.length} \u9879\uFF09`,
|
|
305
|
+
disabled: selectedRows.value.length === 0
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
opts.push({ value: "page", label: `\u5BFC\u51FA\u5F53\u524D\u9875\u6570\u636E\uFF08${currentRows.value.length} \u6761\uFF09`, disabled: false });
|
|
309
|
+
if (hasMorePage.value) {
|
|
310
|
+
opts.push({ value: "all", label: "\u5BFC\u51FA\u6240\u6709\u6570\u636E", disabled: false });
|
|
311
|
+
}
|
|
312
|
+
return opts;
|
|
313
|
+
});
|
|
314
|
+
const exportDialogVisible = ref5(false);
|
|
315
|
+
const exporting = ref5(false);
|
|
316
|
+
const exportFetched = ref5(0);
|
|
317
|
+
const exportTotal = ref5(0);
|
|
318
|
+
const exportTotalKnown = ref5(false);
|
|
319
|
+
let exportCancelled = false;
|
|
320
|
+
let exportAbort = null;
|
|
321
|
+
let pendingExportApi = null;
|
|
322
|
+
const exportCancelledVisible = ref5(false);
|
|
323
|
+
const exportCancelledRows = ref5(0);
|
|
324
|
+
let exportStartedAt = 0;
|
|
325
|
+
let etaTimer = null;
|
|
326
|
+
const etaNow = ref5(0);
|
|
327
|
+
onScopeDispose(stopEtaTimer);
|
|
328
|
+
function stopEtaTimer() {
|
|
329
|
+
if (etaTimer != null) {
|
|
330
|
+
clearInterval(etaTimer);
|
|
331
|
+
etaTimer = null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const exportEtaSeconds = computed4(() => {
|
|
335
|
+
if (!exportTotalKnown.value || exportFetched.value === 0) return null;
|
|
336
|
+
const elapsed = (etaNow.value - exportStartedAt) / 1e3;
|
|
337
|
+
if (elapsed < 0.5) return null;
|
|
338
|
+
const remaining = exportTotal.value - exportFetched.value;
|
|
339
|
+
if (remaining <= 0) return null;
|
|
340
|
+
return Math.max(1, Math.ceil(remaining / (exportFetched.value / elapsed)));
|
|
341
|
+
});
|
|
342
|
+
const exportPercent = computed4(() => {
|
|
343
|
+
if (exportTotalKnown.value && exportTotal.value > 0)
|
|
344
|
+
return Math.min(100, Math.round(exportFetched.value / exportTotal.value * 100));
|
|
345
|
+
return exportFetched.value > 0 ? 99 : 0;
|
|
346
|
+
});
|
|
347
|
+
function onExportClick() {
|
|
348
|
+
const enabled = exportOptions.value.filter((o) => !o.disabled);
|
|
349
|
+
if (enabled.length === 1) {
|
|
350
|
+
doExport(enabled[0].value);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
exportDialogVisible.value = true;
|
|
354
|
+
}
|
|
355
|
+
function exportSheetName() {
|
|
356
|
+
return tableName.value ?? "\u6570\u636E";
|
|
357
|
+
}
|
|
358
|
+
function newExportApi() {
|
|
359
|
+
if (props.exportorFunc) return props.exportorFunc();
|
|
360
|
+
return createConsoleExportApiFunc()();
|
|
361
|
+
}
|
|
362
|
+
function exportRowValues(row) {
|
|
363
|
+
return columns.value.map((c) => row[c.propertyPath] ?? null);
|
|
364
|
+
}
|
|
365
|
+
async function doExport(scope) {
|
|
366
|
+
exportDialogVisible.value = false;
|
|
367
|
+
if (scope === "all") {
|
|
368
|
+
await exportAllStreaming();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const data = scope === "selected" ? selectedRows.value : currentRows.value;
|
|
372
|
+
const api = newExportApi();
|
|
373
|
+
const sheet = exportSheetName();
|
|
374
|
+
await api.renderHeader({ [sheet]: columns.value });
|
|
375
|
+
await api.renderRows(sheet, data.map(exportRowValues));
|
|
376
|
+
await api.download(exportFileName());
|
|
377
|
+
}
|
|
378
|
+
async function exportAllStreaming() {
|
|
379
|
+
exporting.value = true;
|
|
380
|
+
exportCancelled = false;
|
|
381
|
+
exportFetched.value = 0;
|
|
382
|
+
exportTotal.value = total.value;
|
|
383
|
+
exportTotalKnown.value = false;
|
|
384
|
+
exportStartedAt = Date.now();
|
|
385
|
+
etaNow.value = exportStartedAt;
|
|
386
|
+
stopEtaTimer();
|
|
387
|
+
etaTimer = setInterval(() => {
|
|
388
|
+
etaNow.value = Date.now();
|
|
389
|
+
}, 500);
|
|
390
|
+
exportAbort = new AbortController();
|
|
391
|
+
const api = newExportApi();
|
|
392
|
+
const sheet = exportSheetName();
|
|
393
|
+
await api.renderHeader({ [sheet]: columns.value });
|
|
394
|
+
try {
|
|
395
|
+
const limit = exportPageSize.value;
|
|
396
|
+
let offset = 0;
|
|
397
|
+
for (; ; ) {
|
|
398
|
+
const res = await props.dataFun({ limit, offset }, exportAbort.signal);
|
|
399
|
+
await api.renderRows(sheet, res.items.map(exportRowValues));
|
|
400
|
+
exportFetched.value += res.items.length;
|
|
401
|
+
if (res.totalCount != null) {
|
|
402
|
+
exportTotal.value = res.totalCount;
|
|
403
|
+
exportTotalKnown.value = true;
|
|
404
|
+
}
|
|
405
|
+
if (exportCancelled || !res.hasNext || res.items.length === 0) break;
|
|
406
|
+
offset += limit;
|
|
407
|
+
}
|
|
408
|
+
} catch (e) {
|
|
409
|
+
if (!exportCancelled) throw e;
|
|
410
|
+
} finally {
|
|
411
|
+
exporting.value = false;
|
|
412
|
+
stopEtaTimer();
|
|
413
|
+
exportAbort = null;
|
|
414
|
+
}
|
|
415
|
+
if (!exportCancelled) {
|
|
416
|
+
await api.download(exportFileName());
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
exportCancelledRows.value = exportFetched.value;
|
|
420
|
+
pendingExportApi = api;
|
|
421
|
+
exportCancelledVisible.value = true;
|
|
422
|
+
}
|
|
423
|
+
function cancelExport() {
|
|
424
|
+
exportCancelled = true;
|
|
425
|
+
exportAbort?.abort();
|
|
426
|
+
}
|
|
427
|
+
async function keepPartialExport() {
|
|
428
|
+
exportCancelledVisible.value = false;
|
|
429
|
+
const api = pendingExportApi;
|
|
430
|
+
pendingExportApi = null;
|
|
431
|
+
if (api) await api.download(exportFileName(true));
|
|
432
|
+
}
|
|
433
|
+
async function discardPartialExport() {
|
|
434
|
+
exportCancelledVisible.value = false;
|
|
435
|
+
const api = pendingExportApi;
|
|
436
|
+
pendingExportApi = null;
|
|
437
|
+
if (api) await api.cancel();
|
|
438
|
+
}
|
|
439
|
+
function exportFileName(partial = false) {
|
|
440
|
+
const now = /* @__PURE__ */ new Date();
|
|
441
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
442
|
+
const stamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
443
|
+
return `${tableName.value ?? "\u5BFC\u51FA\u6570\u636E"}_${stamp}${partial ? "_\u90E8\u5206" : ""}.xlsx`;
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
exportOptions,
|
|
447
|
+
exportDialogVisible,
|
|
448
|
+
exporting,
|
|
449
|
+
exportFetched,
|
|
450
|
+
exportTotal,
|
|
451
|
+
exportTotalKnown,
|
|
452
|
+
exportEtaSeconds,
|
|
453
|
+
exportPercent,
|
|
454
|
+
exportCancelledVisible,
|
|
455
|
+
exportCancelledRows,
|
|
456
|
+
onExportClick,
|
|
457
|
+
doExport,
|
|
458
|
+
cancelExport,
|
|
459
|
+
keepPartialExport,
|
|
460
|
+
discardPartialExport
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/importExcel/importExcel.ts
|
|
465
|
+
import { computed as computed5, nextTick as nextTick2, ref as ref6 } from "vue";
|
|
466
|
+
function normalizeHeader(value) {
|
|
467
|
+
return String(value ?? "").trim().toLowerCase();
|
|
468
|
+
}
|
|
469
|
+
function isEmptyValue(value) {
|
|
470
|
+
return value === void 0 || value === null || typeof value === "string" && value.trim() === "";
|
|
471
|
+
}
|
|
472
|
+
function resolveColumns(columns, header) {
|
|
473
|
+
const headerIndex = /* @__PURE__ */ new Map();
|
|
474
|
+
header.forEach((cell, i) => {
|
|
475
|
+
const key = normalizeHeader(cell);
|
|
476
|
+
if (key !== "" && !headerIndex.has(key)) {
|
|
477
|
+
headerIndex.set(key, i);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
const missing = [];
|
|
481
|
+
const resolved = columns.map((column) => {
|
|
482
|
+
if (typeof column.position === "number") {
|
|
483
|
+
return { column, index: column.position };
|
|
484
|
+
}
|
|
485
|
+
const byName = headerIndex.get(normalizeHeader(column.name));
|
|
486
|
+
if (byName !== void 0) {
|
|
487
|
+
return { column, index: byName };
|
|
488
|
+
}
|
|
489
|
+
if (column.alias) {
|
|
490
|
+
for (const alias of column.alias) {
|
|
491
|
+
const byAlias = headerIndex.get(normalizeHeader(alias));
|
|
492
|
+
if (byAlias !== void 0) {
|
|
493
|
+
return { column, index: byAlias };
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (column.optional) {
|
|
498
|
+
return { column, index: -1 };
|
|
499
|
+
}
|
|
500
|
+
missing.push(column);
|
|
501
|
+
return { column, index: -1 };
|
|
502
|
+
});
|
|
503
|
+
return { resolved, missing };
|
|
504
|
+
}
|
|
505
|
+
function useImportExcel({ columns, processor, parser }) {
|
|
506
|
+
const rows = ref6([]);
|
|
507
|
+
let resolvedColumns = [];
|
|
508
|
+
const fileName = ref6("");
|
|
509
|
+
const sheetName = ref6("");
|
|
510
|
+
const loading = ref6(false);
|
|
511
|
+
const processing = ref6(false);
|
|
512
|
+
const stopping = ref6(false);
|
|
513
|
+
let processController = null;
|
|
514
|
+
const processingTotal = ref6(0);
|
|
515
|
+
const progressDone = ref6(0);
|
|
516
|
+
const progressSuccess = ref6(0);
|
|
517
|
+
const progressFailed = ref6(0);
|
|
518
|
+
const hasData = computed5(() => rows.value.length > 0);
|
|
519
|
+
const invalidCount = computed5(() => rows.value.filter((r) => r.status === "invalid").length);
|
|
520
|
+
const successCount = computed5(() => rows.value.filter((r) => r.status === "success").length);
|
|
521
|
+
const failedCount = computed5(() => rows.value.filter((r) => r.status === "failed").length);
|
|
522
|
+
const selectedCount = computed5(() => rows.value.filter((r) => r.selected).length);
|
|
523
|
+
const pendingCount = computed5(
|
|
524
|
+
() => rows.value.filter(
|
|
525
|
+
(r) => r.selected && (r.status === "valid" || r.status === "failed")
|
|
526
|
+
).length
|
|
527
|
+
);
|
|
528
|
+
const processedCount = computed5(() => successCount.value + failedCount.value);
|
|
529
|
+
function mapInputs(inputs) {
|
|
530
|
+
const data = {};
|
|
531
|
+
const errors = [];
|
|
532
|
+
for (const { column } of resolvedColumns) {
|
|
533
|
+
const raw = inputs[column.name];
|
|
534
|
+
if (isEmptyValue(raw)) {
|
|
535
|
+
data[column.name] = void 0;
|
|
536
|
+
if (!column.optional) {
|
|
537
|
+
errors.push(`\u300C${column.name}\u300D\u4E0D\u80FD\u4E3A\u7A7A`);
|
|
538
|
+
}
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
const value = column.valueMapper ? column.valueMapper(raw) : raw;
|
|
542
|
+
data[column.name] = value;
|
|
543
|
+
const validatorErrors = column.validator?.(value);
|
|
544
|
+
if (validatorErrors && validatorErrors.length > 0) {
|
|
545
|
+
errors.push(...validatorErrors);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return { data, errors };
|
|
549
|
+
}
|
|
550
|
+
async function loadFile(file) {
|
|
551
|
+
const parse = parser.value;
|
|
552
|
+
if (!parse) {
|
|
553
|
+
throw new Error("\u672A\u63D0\u4F9B parser\uFF08Excel \u89E3\u6790\u5668\uFF09\uFF0C\u65E0\u6CD5\u52A0\u8F7D\u6587\u4EF6");
|
|
554
|
+
}
|
|
555
|
+
loading.value = true;
|
|
556
|
+
try {
|
|
557
|
+
const sheets = await parse(file);
|
|
558
|
+
const sheet = sheets[0];
|
|
559
|
+
if (!sheet || sheet.rows.length === 0) {
|
|
560
|
+
throw new Error("\u5DE5\u4F5C\u8868\u4E3A\u7A7A\uFF1A\u9996\u884C\u9700\u4E3A\u8868\u5934\uFF0C\u7B2C\u4E8C\u884C\u8D77\u4E3A\u6570\u636E");
|
|
561
|
+
}
|
|
562
|
+
const headerRow = sheet.rows[0];
|
|
563
|
+
const { resolved, missing } = resolveColumns(columns.value, headerRow.values);
|
|
564
|
+
if (missing.length > 0) {
|
|
565
|
+
throw new Error(
|
|
566
|
+
`\u8868\u5934\u7F3A\u5C11\u5FC5\u9700\u5217\uFF1A${missing.map((c) => `\u300C${c.name}\u300D`).join("\u3001")}`
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
resolvedColumns = resolved;
|
|
570
|
+
const mapped = [];
|
|
571
|
+
for (const sheetRow of sheet.rows.slice(1)) {
|
|
572
|
+
const inputs = {};
|
|
573
|
+
for (const { column, index } of resolvedColumns) {
|
|
574
|
+
inputs[column.name] = index >= 0 ? sheetRow.values[index] : void 0;
|
|
575
|
+
}
|
|
576
|
+
const { data, errors } = mapInputs(inputs);
|
|
577
|
+
const valid = errors.length === 0;
|
|
578
|
+
mapped.push({
|
|
579
|
+
rowNumber: sheetRow.rowNumber,
|
|
580
|
+
raw: [...sheetRow.values],
|
|
581
|
+
data,
|
|
582
|
+
selected: valid,
|
|
583
|
+
status: valid ? "valid" : "invalid",
|
|
584
|
+
errors
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
rows.value = mapped;
|
|
588
|
+
fileName.value = file.name;
|
|
589
|
+
sheetName.value = sheet.name;
|
|
590
|
+
processingTotal.value = 0;
|
|
591
|
+
progressDone.value = 0;
|
|
592
|
+
progressSuccess.value = 0;
|
|
593
|
+
progressFailed.value = 0;
|
|
594
|
+
return mapped;
|
|
595
|
+
} finally {
|
|
596
|
+
loading.value = false;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function syncSelection(selectedRows) {
|
|
600
|
+
const selectedSet = new Set(selectedRows);
|
|
601
|
+
for (const row of rows.value) {
|
|
602
|
+
row.selected = selectedSet.has(row);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
async function startProcessing() {
|
|
606
|
+
if (processing.value) return null;
|
|
607
|
+
const importer = processor.value;
|
|
608
|
+
if (!importer) {
|
|
609
|
+
throw new Error("\u672A\u63D0\u4F9B processor\uFF08\u5BFC\u5165\u5904\u7406\u5668\uFF09");
|
|
610
|
+
}
|
|
611
|
+
const targets = rows.value.filter(
|
|
612
|
+
(r) => r.selected && (r.status === "valid" || r.status === "failed")
|
|
613
|
+
);
|
|
614
|
+
if (targets.length === 0) return null;
|
|
615
|
+
const batch = Math.max(1, Math.floor(importer.batchSize ?? 1));
|
|
616
|
+
processController = new AbortController();
|
|
617
|
+
const signal = processController.signal;
|
|
618
|
+
processing.value = true;
|
|
619
|
+
stopping.value = false;
|
|
620
|
+
processingTotal.value = targets.length;
|
|
621
|
+
progressDone.value = 0;
|
|
622
|
+
progressSuccess.value = 0;
|
|
623
|
+
progressFailed.value = 0;
|
|
624
|
+
const summary = {
|
|
625
|
+
total: targets.length,
|
|
626
|
+
success: 0,
|
|
627
|
+
failed: 0,
|
|
628
|
+
stopped: false
|
|
629
|
+
};
|
|
630
|
+
try {
|
|
631
|
+
for (let i = 0; i < targets.length; i += batch) {
|
|
632
|
+
if (stopping.value) {
|
|
633
|
+
summary.stopped = true;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
const chunk = targets.slice(i, i + batch);
|
|
637
|
+
for (const row of chunk) {
|
|
638
|
+
row.status = "processing";
|
|
639
|
+
row.message = void 0;
|
|
640
|
+
}
|
|
641
|
+
await nextTick2();
|
|
642
|
+
try {
|
|
643
|
+
const results = await importer.process(chunk, signal);
|
|
644
|
+
if (!Array.isArray(results) || results.length !== chunk.length) {
|
|
645
|
+
throw new Error(
|
|
646
|
+
`processor \u8FD4\u56DE\u7ED3\u679C\u6570\u91CF\uFF08${Array.isArray(results) ? results.length : "\u975E\u6570\u7EC4"}\uFF09\u4E0E\u8F93\u5165\u884C\u6570\uFF08${chunk.length}\uFF09\u4E0D\u4E00\u81F4`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
chunk.forEach((row, j) => {
|
|
650
|
+
const result = results[j];
|
|
651
|
+
row.status = result.status === "failed" ? "failed" : "success";
|
|
652
|
+
row.message = result.status === "failed" ? result.message : void 0;
|
|
653
|
+
});
|
|
654
|
+
} catch (e) {
|
|
655
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
656
|
+
for (const row of chunk) {
|
|
657
|
+
row.status = "failed";
|
|
658
|
+
row.message = msg;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
for (const row of chunk) {
|
|
662
|
+
if (row.status === "failed") {
|
|
663
|
+
summary.failed += 1;
|
|
664
|
+
progressFailed.value += 1;
|
|
665
|
+
} else {
|
|
666
|
+
summary.success += 1;
|
|
667
|
+
progressSuccess.value += 1;
|
|
668
|
+
}
|
|
669
|
+
progressDone.value += 1;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return summary;
|
|
673
|
+
} finally {
|
|
674
|
+
processing.value = false;
|
|
675
|
+
stopping.value = false;
|
|
676
|
+
processController = null;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
function stopProcessing() {
|
|
680
|
+
if (!processing.value || stopping.value) return;
|
|
681
|
+
stopping.value = true;
|
|
682
|
+
processController?.abort();
|
|
683
|
+
}
|
|
684
|
+
function saveRowEdit(row, inputs) {
|
|
685
|
+
const { data, errors } = mapInputs(inputs);
|
|
686
|
+
row.data = data;
|
|
687
|
+
for (const { column, index } of resolvedColumns) {
|
|
688
|
+
if (index >= 0) {
|
|
689
|
+
const v = inputs[column.name];
|
|
690
|
+
row.raw[index] = v === void 0 || v === null ? "" : v;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
row.errors = errors;
|
|
694
|
+
row.message = void 0;
|
|
695
|
+
if (errors.length > 0) {
|
|
696
|
+
row.status = "invalid";
|
|
697
|
+
row.selected = false;
|
|
698
|
+
} else {
|
|
699
|
+
row.status = "valid";
|
|
700
|
+
row.selected = true;
|
|
701
|
+
row.errors = [];
|
|
702
|
+
}
|
|
703
|
+
return { row, errors };
|
|
704
|
+
}
|
|
705
|
+
function removeRow(row) {
|
|
706
|
+
const index = rows.value.indexOf(row);
|
|
707
|
+
if (index >= 0) {
|
|
708
|
+
rows.value.splice(index, 1);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
function clear() {
|
|
712
|
+
rows.value = [];
|
|
713
|
+
resolvedColumns = [];
|
|
714
|
+
fileName.value = "";
|
|
715
|
+
sheetName.value = "";
|
|
716
|
+
processingTotal.value = 0;
|
|
717
|
+
progressDone.value = 0;
|
|
718
|
+
progressSuccess.value = 0;
|
|
719
|
+
progressFailed.value = 0;
|
|
720
|
+
}
|
|
721
|
+
return {
|
|
722
|
+
rows,
|
|
723
|
+
fileName,
|
|
724
|
+
sheetName,
|
|
725
|
+
loading,
|
|
726
|
+
processing,
|
|
727
|
+
stopping,
|
|
728
|
+
processingTotal,
|
|
729
|
+
progressDone,
|
|
730
|
+
progressSuccess,
|
|
731
|
+
progressFailed,
|
|
732
|
+
hasData,
|
|
733
|
+
invalidCount,
|
|
734
|
+
successCount,
|
|
735
|
+
failedCount,
|
|
736
|
+
selectedCount,
|
|
737
|
+
pendingCount,
|
|
738
|
+
processedCount,
|
|
739
|
+
loadFile,
|
|
740
|
+
syncSelection,
|
|
741
|
+
saveRowEdit,
|
|
742
|
+
removeRow,
|
|
743
|
+
startProcessing,
|
|
744
|
+
stopProcessing,
|
|
745
|
+
clear
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// src/mainPanel/nodes.ts
|
|
750
|
+
import { ref as ref7, watch as watch5 } from "vue";
|
|
751
|
+
function isGroupNode(node) {
|
|
752
|
+
return !!(node.children && node.children.length > 0);
|
|
753
|
+
}
|
|
754
|
+
function findNode(list, key) {
|
|
755
|
+
for (const node of list) {
|
|
756
|
+
if (node.key === key) return node;
|
|
757
|
+
const found = node.children ? findNode(node.children, key) : void 0;
|
|
758
|
+
if (found) return found;
|
|
759
|
+
}
|
|
760
|
+
return void 0;
|
|
761
|
+
}
|
|
762
|
+
function collectGroupKeys(list) {
|
|
763
|
+
const keys = [];
|
|
764
|
+
for (const node of list) {
|
|
765
|
+
if (isGroupNode(node)) {
|
|
766
|
+
keys.push(node.key);
|
|
767
|
+
keys.push(...collectGroupKeys(node.children));
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return keys;
|
|
771
|
+
}
|
|
772
|
+
function filterNodesByKeyword(list, keyword) {
|
|
773
|
+
const tokens = keyword.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
774
|
+
if (tokens.length === 0) return list;
|
|
775
|
+
const matches = (label) => {
|
|
776
|
+
const text = label.toLowerCase();
|
|
777
|
+
return tokens.every((t) => text.includes(t));
|
|
778
|
+
};
|
|
779
|
+
const walk = (nodes) => {
|
|
780
|
+
const result = [];
|
|
781
|
+
for (const node of nodes) {
|
|
782
|
+
if (isGroupNode(node)) {
|
|
783
|
+
if (matches(node.label)) {
|
|
784
|
+
result.push(node);
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
const children = walk(node.children);
|
|
788
|
+
if (children.length > 0) result.push({ ...node, children });
|
|
789
|
+
} else if (matches(node.label)) {
|
|
790
|
+
result.push(node);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return result;
|
|
794
|
+
};
|
|
795
|
+
return walk(list);
|
|
796
|
+
}
|
|
797
|
+
function useMainPanelNodes(props) {
|
|
798
|
+
const nodes = ref7([]);
|
|
799
|
+
const menuLoading = ref7(false);
|
|
800
|
+
async function loadNodes() {
|
|
801
|
+
menuLoading.value = true;
|
|
802
|
+
try {
|
|
803
|
+
nodes.value = await props.nodesFunc();
|
|
804
|
+
} finally {
|
|
805
|
+
menuLoading.value = false;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
watch5(() => props.nodesFunc, () => {
|
|
809
|
+
void loadNodes();
|
|
810
|
+
});
|
|
811
|
+
return {
|
|
812
|
+
nodes,
|
|
813
|
+
menuLoading,
|
|
814
|
+
loadNodes
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// src/mainPanel/tabs.ts
|
|
819
|
+
import { defineAsyncComponent, ref as ref8 } from "vue";
|
|
820
|
+
function useMainPanelTabs(props) {
|
|
821
|
+
const openTabs = ref8([]);
|
|
822
|
+
const activeKey = ref8("");
|
|
823
|
+
function openTab(node) {
|
|
824
|
+
const label = (node.icon ? `${node.icon} ` : "") + node.label;
|
|
825
|
+
if (!openTabs.value.some((tab) => tab.key === node.key)) {
|
|
826
|
+
openTabs.value.push({
|
|
827
|
+
key: node.key,
|
|
828
|
+
label,
|
|
829
|
+
componentPath: node.component ?? "",
|
|
830
|
+
props: node.props
|
|
831
|
+
});
|
|
832
|
+
if (node.component) getComponent(node.component);
|
|
833
|
+
}
|
|
834
|
+
activeKey.value = node.key;
|
|
835
|
+
}
|
|
836
|
+
function removeTab(key) {
|
|
837
|
+
const target = String(key);
|
|
838
|
+
const index = openTabs.value.findIndex((tab) => tab.key === target);
|
|
839
|
+
if (index < 0) return;
|
|
840
|
+
openTabs.value.splice(index, 1);
|
|
841
|
+
if (activeKey.value === target) {
|
|
842
|
+
const next = openTabs.value[Math.min(index, openTabs.value.length - 1)];
|
|
843
|
+
activeKey.value = next?.key ?? "";
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
const componentCache = /* @__PURE__ */ new Map();
|
|
847
|
+
function getComponent(path) {
|
|
848
|
+
if (!path) return null;
|
|
849
|
+
if (!componentCache.has(path)) {
|
|
850
|
+
const loader = props.componentMap?.[path];
|
|
851
|
+
componentCache.set(
|
|
852
|
+
path,
|
|
853
|
+
loader ? defineAsyncComponent(() => loader().then((m) => m.default)) : (
|
|
854
|
+
// 未提供 componentMap 时的兜底:跳过打包器静态分析,按运行时动态 import 解析
|
|
855
|
+
defineAsyncComponent(() => import(
|
|
856
|
+
/* @vite-ignore */
|
|
857
|
+
path
|
|
858
|
+
).then((m) => m.default))
|
|
859
|
+
)
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
return componentCache.get(path) ?? null;
|
|
863
|
+
}
|
|
864
|
+
return {
|
|
865
|
+
openTabs,
|
|
866
|
+
activeKey,
|
|
867
|
+
openTab,
|
|
868
|
+
removeTab,
|
|
869
|
+
getComponent
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// src/filter/filterPanel.ts
|
|
874
|
+
import { computed as computed6, ref as ref9 } from "vue";
|
|
875
|
+
import { FilterInfo, emptyFilter } from "ys.knife.query.js";
|
|
876
|
+
var FilterPanelKey = /* @__PURE__ */ Symbol("YsFilterPanel");
|
|
877
|
+
function useFilterPanel() {
|
|
878
|
+
const items = ref9(/* @__PURE__ */ new Map());
|
|
879
|
+
let counter = 0;
|
|
880
|
+
const register = (api) => {
|
|
881
|
+
const key = String(++counter);
|
|
882
|
+
items.value.set(key, api);
|
|
883
|
+
items.value = new Map(items.value);
|
|
884
|
+
return () => {
|
|
885
|
+
items.value.delete(key);
|
|
886
|
+
items.value = new Map(items.value);
|
|
887
|
+
};
|
|
888
|
+
};
|
|
889
|
+
const filter6 = computed6(() => {
|
|
890
|
+
const nonNull = [...items.value.values()].map((a) => a.filter).filter((f) => f !== null);
|
|
891
|
+
if (nonNull.length === 0) return emptyFilter();
|
|
892
|
+
return FilterInfo.createAnd(...nonNull);
|
|
893
|
+
});
|
|
894
|
+
function reset() {
|
|
895
|
+
for (const api of items.value.values()) api.reset();
|
|
896
|
+
}
|
|
897
|
+
return { filter: filter6, register, reset };
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// src/filter/textFilterItem.ts
|
|
901
|
+
import { computed as computed7, ref as ref10 } from "vue";
|
|
902
|
+
import { filter, con } from "ys.knife.query.js";
|
|
903
|
+
function useTextFilterItem(props) {
|
|
904
|
+
const value = ref10(
|
|
905
|
+
typeof props.defaultValue === "string" ? props.defaultValue : ""
|
|
906
|
+
);
|
|
907
|
+
function isEmpty(v) {
|
|
908
|
+
return v == null || v.trim() === "";
|
|
909
|
+
}
|
|
910
|
+
const filterInfo = computed7(() => {
|
|
911
|
+
if (isEmpty(value.value)) return null;
|
|
912
|
+
return filter(props.propertyPath, props.op, con(value.value));
|
|
913
|
+
});
|
|
914
|
+
function reset() {
|
|
915
|
+
value.value = typeof props.defaultValue === "string" ? props.defaultValue : "";
|
|
916
|
+
}
|
|
917
|
+
return { value, filter: filterInfo, reset };
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// src/filter/dateFilterItem.ts
|
|
921
|
+
import { computed as computed8, ref as ref11 } from "vue";
|
|
922
|
+
import { filter as filter2, con as con2 } from "ys.knife.query.js";
|
|
923
|
+
function useDateFilterItem(props) {
|
|
924
|
+
const value = ref11(
|
|
925
|
+
typeof props.defaultValue === "string" ? props.defaultValue : ""
|
|
926
|
+
);
|
|
927
|
+
function isEmpty(v) {
|
|
928
|
+
return v == null || v.trim() === "";
|
|
929
|
+
}
|
|
930
|
+
const filterInfo = computed8(() => {
|
|
931
|
+
if (isEmpty(value.value)) return null;
|
|
932
|
+
return filter2(props.propertyPath, props.op, con2(value.value));
|
|
933
|
+
});
|
|
934
|
+
function reset() {
|
|
935
|
+
value.value = typeof props.defaultValue === "string" ? props.defaultValue : "";
|
|
936
|
+
}
|
|
937
|
+
return { value, filter: filterInfo, reset };
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// src/filter/dateRangeFilterItem.ts
|
|
941
|
+
import { computed as computed9, ref as ref12 } from "vue";
|
|
942
|
+
import { filter as filter3, con as con3, Operator } from "ys.knife.query.js";
|
|
943
|
+
function useDateRangeFilterItem(props) {
|
|
944
|
+
function toRange(v) {
|
|
945
|
+
if (v && v.length === 2 && typeof v[0] === "string" && typeof v[1] === "string") {
|
|
946
|
+
return [v[0], v[1]];
|
|
947
|
+
}
|
|
948
|
+
return null;
|
|
949
|
+
}
|
|
950
|
+
const initial = toRange(props.defaultValue);
|
|
951
|
+
const value = ref12(initial);
|
|
952
|
+
function isEmpty(v) {
|
|
953
|
+
return v == null || v.length !== 2 || !v[0] || !v[1];
|
|
954
|
+
}
|
|
955
|
+
const filterInfo = computed9(() => {
|
|
956
|
+
if (isEmpty(value.value)) return null;
|
|
957
|
+
const [start, end] = value.value;
|
|
958
|
+
return filter3(props.propertyPath, Operator.Between, con3([start, end]));
|
|
959
|
+
});
|
|
960
|
+
function reset() {
|
|
961
|
+
value.value = initial;
|
|
962
|
+
}
|
|
963
|
+
return { value, filter: filterInfo, reset };
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// src/filter/enumFilterItem.ts
|
|
967
|
+
import { computed as computed10, ref as ref13 } from "vue";
|
|
968
|
+
import { filter as filter4, con as con4, Operator as Operator2 } from "ys.knife.query.js";
|
|
969
|
+
function toSingleValue(v) {
|
|
970
|
+
if (typeof v === "string" || typeof v === "number") return v;
|
|
971
|
+
return null;
|
|
972
|
+
}
|
|
973
|
+
function toMultiValue(v) {
|
|
974
|
+
if (Array.isArray(v)) {
|
|
975
|
+
return v.filter(
|
|
976
|
+
(item) => typeof item === "string" || typeof item === "number"
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
return [];
|
|
980
|
+
}
|
|
981
|
+
function useEnumFilterItem(props) {
|
|
982
|
+
const isMultiple = props.multiple === true;
|
|
983
|
+
const value = ref13(
|
|
984
|
+
isMultiple ? toMultiValue(props.defaultValue) : toSingleValue(props.defaultValue)
|
|
985
|
+
);
|
|
986
|
+
function isEmpty() {
|
|
987
|
+
const v = value.value;
|
|
988
|
+
if (isMultiple) {
|
|
989
|
+
return !Array.isArray(v) || v.length === 0;
|
|
990
|
+
}
|
|
991
|
+
if (v == null) return true;
|
|
992
|
+
if (typeof v === "string" && v.trim() === "") return true;
|
|
993
|
+
return false;
|
|
994
|
+
}
|
|
995
|
+
const filterInfo = computed10(() => {
|
|
996
|
+
if (isEmpty()) return null;
|
|
997
|
+
if (isMultiple) {
|
|
998
|
+
const arr = value.value;
|
|
999
|
+
if (arr.length === 1) {
|
|
1000
|
+
const v2 = arr[0];
|
|
1001
|
+
if (v2 == null) return null;
|
|
1002
|
+
return filter4(props.propertyPath, Operator2.Equals, con4(v2));
|
|
1003
|
+
}
|
|
1004
|
+
return filter4(
|
|
1005
|
+
props.propertyPath,
|
|
1006
|
+
Operator2.In,
|
|
1007
|
+
con4(arr)
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
1010
|
+
const v = value.value;
|
|
1011
|
+
if (v == null) return null;
|
|
1012
|
+
return filter4(props.propertyPath, props.op, con4(v));
|
|
1013
|
+
});
|
|
1014
|
+
function reset() {
|
|
1015
|
+
value.value = isMultiple ? toMultiValue(props.defaultValue) : toSingleValue(props.defaultValue);
|
|
1016
|
+
}
|
|
1017
|
+
return { value, filter: filterInfo, reset };
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// src/advancedFilter/advancedFilter.ts
|
|
1021
|
+
import { computed as computed11, ref as ref14 } from "vue";
|
|
1022
|
+
import { FilterInfo as FilterInfo2, Operator as Operator3, con as con5, emptyFilter as emptyFilter2, filter as filter5 } from "ys.knife.query.js";
|
|
1023
|
+
import { isConditionGroup, isNullOperator } from "@ys-knife-crud/core";
|
|
1024
|
+
import {
|
|
1025
|
+
FIELD_TYPE_OPERATORS,
|
|
1026
|
+
defaultOperatorForType,
|
|
1027
|
+
inferColumnFieldType,
|
|
1028
|
+
isMultiValueOperator,
|
|
1029
|
+
isRangeOperator
|
|
1030
|
+
} from "@ys-knife-crud/core";
|
|
1031
|
+
function useAdvancedFilter(props) {
|
|
1032
|
+
let seq = 0;
|
|
1033
|
+
function nextId(prefix) {
|
|
1034
|
+
return `adv-${prefix}-${++seq}`;
|
|
1035
|
+
}
|
|
1036
|
+
const fieldTypeMap = computed11(() => {
|
|
1037
|
+
const map = /* @__PURE__ */ new Map();
|
|
1038
|
+
for (const col of props.columns) {
|
|
1039
|
+
map.set(
|
|
1040
|
+
col.propertyPath,
|
|
1041
|
+
inferColumnFieldType(col, props.optionSources?.[col.propertyPath] !== void 0)
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
return map;
|
|
1045
|
+
});
|
|
1046
|
+
function fieldTypeOf(propertyPath) {
|
|
1047
|
+
return fieldTypeMap.value.get(propertyPath) ?? "string";
|
|
1048
|
+
}
|
|
1049
|
+
function operatorsOf(propertyPath) {
|
|
1050
|
+
return FIELD_TYPE_OPERATORS[fieldTypeOf(propertyPath)];
|
|
1051
|
+
}
|
|
1052
|
+
function emptyValue(type, op) {
|
|
1053
|
+
if (isNullOperator(op)) return null;
|
|
1054
|
+
if (isRangeOperator(op)) return [null, null];
|
|
1055
|
+
if (isMultiValueOperator(op)) return [];
|
|
1056
|
+
void type;
|
|
1057
|
+
return null;
|
|
1058
|
+
}
|
|
1059
|
+
function newLeaf() {
|
|
1060
|
+
const first = props.columns[0];
|
|
1061
|
+
const propertyPath = first?.propertyPath ?? "";
|
|
1062
|
+
const type = first ? fieldTypeOf(propertyPath) : "string";
|
|
1063
|
+
const op = defaultOperatorForType(type);
|
|
1064
|
+
return {
|
|
1065
|
+
id: nextId("leaf"),
|
|
1066
|
+
propertyPath,
|
|
1067
|
+
op,
|
|
1068
|
+
value: emptyValue(type, op)
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
function newGroup(combinator = "and") {
|
|
1072
|
+
return {
|
|
1073
|
+
id: nextId("grp"),
|
|
1074
|
+
combinator,
|
|
1075
|
+
children: [newLeaf()]
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
const rootGroup = ref14({
|
|
1079
|
+
id: "root",
|
|
1080
|
+
combinator: "and",
|
|
1081
|
+
children: [newLeaf()]
|
|
1082
|
+
});
|
|
1083
|
+
function findNode2(id, group = rootGroup.value) {
|
|
1084
|
+
for (const child of group.children) {
|
|
1085
|
+
if (child.id === id) return child;
|
|
1086
|
+
if (isConditionGroup(child)) {
|
|
1087
|
+
const found = findNode2(id, child);
|
|
1088
|
+
if (found) return found;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return null;
|
|
1092
|
+
}
|
|
1093
|
+
function findParentGroup(id, group = rootGroup.value) {
|
|
1094
|
+
for (const child of group.children) {
|
|
1095
|
+
if (child.id === id) return group;
|
|
1096
|
+
if (isConditionGroup(child)) {
|
|
1097
|
+
const found = findParentGroup(id, child);
|
|
1098
|
+
if (found) return found;
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
return null;
|
|
1102
|
+
}
|
|
1103
|
+
function addLeaf(parentId, index) {
|
|
1104
|
+
const parent = parentId === rootGroup.value.id ? rootGroup.value : findNode2(parentId);
|
|
1105
|
+
if (!parent || !isConditionGroup(parent)) return;
|
|
1106
|
+
const leaf = newLeaf();
|
|
1107
|
+
if (typeof index === "number") {
|
|
1108
|
+
parent.children.splice(index, 0, leaf);
|
|
1109
|
+
} else {
|
|
1110
|
+
parent.children.push(leaf);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
function addNestedGroup(parentId, index) {
|
|
1114
|
+
const parent = parentId === rootGroup.value.id ? rootGroup.value : findNode2(parentId);
|
|
1115
|
+
if (!parent || !isConditionGroup(parent)) return;
|
|
1116
|
+
const childCombinator = parent.combinator === "and" ? "or" : "and";
|
|
1117
|
+
const sub = newGroup(childCombinator);
|
|
1118
|
+
if (typeof index === "number") {
|
|
1119
|
+
parent.children.splice(index, 0, sub);
|
|
1120
|
+
} else {
|
|
1121
|
+
parent.children.push(sub);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
function removeNode(id) {
|
|
1125
|
+
if (id === rootGroup.value.id) return;
|
|
1126
|
+
const parent = findParentGroup(id);
|
|
1127
|
+
if (!parent) return;
|
|
1128
|
+
const idx = parent.children.findIndex((c) => c.id === id);
|
|
1129
|
+
if (idx >= 0) parent.children.splice(idx, 1);
|
|
1130
|
+
}
|
|
1131
|
+
function changeField(id, propertyPath) {
|
|
1132
|
+
const node = findNode2(id);
|
|
1133
|
+
if (!node || isConditionGroup(node)) return;
|
|
1134
|
+
node.propertyPath = propertyPath;
|
|
1135
|
+
const type = fieldTypeOf(propertyPath);
|
|
1136
|
+
node.op = defaultOperatorForType(type);
|
|
1137
|
+
node.value = emptyValue(type, node.op);
|
|
1138
|
+
}
|
|
1139
|
+
function changeOperator(id, op) {
|
|
1140
|
+
const node = findNode2(id);
|
|
1141
|
+
if (!node || isConditionGroup(node)) return;
|
|
1142
|
+
node.op = op;
|
|
1143
|
+
node.value = emptyValue(fieldTypeOf(node.propertyPath), op);
|
|
1144
|
+
}
|
|
1145
|
+
function setGroupCombinator(groupId, comb) {
|
|
1146
|
+
const node = groupId === rootGroup.value.id ? rootGroup.value : findNode2(groupId);
|
|
1147
|
+
if (node && isConditionGroup(node)) node.combinator = comb;
|
|
1148
|
+
}
|
|
1149
|
+
function isConditionEmpty(row) {
|
|
1150
|
+
if (isNullOperator(row.op)) return false;
|
|
1151
|
+
const type = fieldTypeOf(row.propertyPath);
|
|
1152
|
+
const v = row.value;
|
|
1153
|
+
if (isRangeOperator(row.op)) {
|
|
1154
|
+
if (!Array.isArray(v) || v.length !== 2) return true;
|
|
1155
|
+
return v[0] === null || v[0] === void 0 || v[0] === "" || v[1] === null || v[1] === void 0 || v[1] === "";
|
|
1156
|
+
}
|
|
1157
|
+
if (isMultiValueOperator(row.op)) {
|
|
1158
|
+
return !Array.isArray(v) || v.length === 0;
|
|
1159
|
+
}
|
|
1160
|
+
if (type === "boolean") {
|
|
1161
|
+
return v === null || v === void 0 || v === "";
|
|
1162
|
+
}
|
|
1163
|
+
if (type === "number" || type === "date") {
|
|
1164
|
+
return v === null || v === void 0 || v === "";
|
|
1165
|
+
}
|
|
1166
|
+
return v === null || v === void 0 || typeof v === "string" && v.trim() === "";
|
|
1167
|
+
}
|
|
1168
|
+
function buildConditionFilter(row) {
|
|
1169
|
+
if (isConditionEmpty(row)) return null;
|
|
1170
|
+
if (row.op === "is_null") {
|
|
1171
|
+
const type = fieldTypeOf(row.propertyPath);
|
|
1172
|
+
if (type === "string") {
|
|
1173
|
+
return FilterInfo2.createOr(
|
|
1174
|
+
filter5(row.propertyPath, Operator3.Equals, con5(null)),
|
|
1175
|
+
filter5(row.propertyPath, Operator3.Equals, con5(""))
|
|
1176
|
+
);
|
|
1177
|
+
}
|
|
1178
|
+
if (type === "array") {
|
|
1179
|
+
return FilterInfo2.createOr(
|
|
1180
|
+
filter5(row.propertyPath, Operator3.Equals, con5(null)),
|
|
1181
|
+
filter5(`${row.propertyPath}.count`, Operator3.Equals, con5(0))
|
|
1182
|
+
);
|
|
1183
|
+
}
|
|
1184
|
+
return filter5(
|
|
1185
|
+
row.propertyPath,
|
|
1186
|
+
Operator3.Equals,
|
|
1187
|
+
con5(null)
|
|
1188
|
+
);
|
|
1189
|
+
}
|
|
1190
|
+
if (row.op === "is_not_null") {
|
|
1191
|
+
const type = fieldTypeOf(row.propertyPath);
|
|
1192
|
+
if (type === "string") {
|
|
1193
|
+
return FilterInfo2.createAnd(
|
|
1194
|
+
filter5(row.propertyPath, Operator3.NotEquals, con5(null)),
|
|
1195
|
+
filter5(row.propertyPath, Operator3.NotEquals, con5(""))
|
|
1196
|
+
);
|
|
1197
|
+
}
|
|
1198
|
+
if (type === "array") {
|
|
1199
|
+
return FilterInfo2.createAnd(
|
|
1200
|
+
filter5(row.propertyPath, Operator3.NotEquals, con5(null)),
|
|
1201
|
+
filter5(`${row.propertyPath}.count`, Operator3.GreaterThan, con5(0))
|
|
1202
|
+
);
|
|
1203
|
+
}
|
|
1204
|
+
return filter5(
|
|
1205
|
+
row.propertyPath,
|
|
1206
|
+
Operator3.NotEquals,
|
|
1207
|
+
con5(null)
|
|
1208
|
+
);
|
|
1209
|
+
}
|
|
1210
|
+
return filter5(
|
|
1211
|
+
row.propertyPath,
|
|
1212
|
+
row.op,
|
|
1213
|
+
con5(row.value)
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
function buildNodeFilter(node) {
|
|
1217
|
+
if (isConditionGroup(node)) {
|
|
1218
|
+
const items = node.children.map(buildNodeFilter).filter((f) => f !== null);
|
|
1219
|
+
if (items.length === 0) return null;
|
|
1220
|
+
if (items.length === 1) return items[0];
|
|
1221
|
+
if (node.combinator === "or") {
|
|
1222
|
+
return FilterInfo2.createOr(
|
|
1223
|
+
...items
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
return FilterInfo2.createAnd(
|
|
1227
|
+
...items
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1230
|
+
return buildConditionFilter(node);
|
|
1231
|
+
}
|
|
1232
|
+
const filterInfo = computed11(() => {
|
|
1233
|
+
const f = buildNodeFilter(rootGroup.value);
|
|
1234
|
+
return f ?? emptyFilter2();
|
|
1235
|
+
});
|
|
1236
|
+
function reset() {
|
|
1237
|
+
rootGroup.value = newGroup("and");
|
|
1238
|
+
rootGroup.value.id = "root";
|
|
1239
|
+
}
|
|
1240
|
+
return {
|
|
1241
|
+
rootGroup,
|
|
1242
|
+
fieldTypeOf,
|
|
1243
|
+
operatorsOf,
|
|
1244
|
+
addLeaf,
|
|
1245
|
+
addNestedGroup,
|
|
1246
|
+
removeNode,
|
|
1247
|
+
changeField,
|
|
1248
|
+
changeOperator,
|
|
1249
|
+
setGroupCombinator,
|
|
1250
|
+
isConditionEmpty,
|
|
1251
|
+
isConditionGroup,
|
|
1252
|
+
filter: filterInfo,
|
|
1253
|
+
reset
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
export {
|
|
1257
|
+
FilterPanelKey,
|
|
1258
|
+
collectGroupKeys,
|
|
1259
|
+
filterNodesByKeyword,
|
|
1260
|
+
findNode,
|
|
1261
|
+
isEnabled,
|
|
1262
|
+
isGroupNode,
|
|
1263
|
+
useAdvancedFilter,
|
|
1264
|
+
useCustomConfig,
|
|
1265
|
+
useDateFilterItem,
|
|
1266
|
+
useDateRangeFilterItem,
|
|
1267
|
+
useDefault,
|
|
1268
|
+
useEnumFilterItem,
|
|
1269
|
+
useExportExcel,
|
|
1270
|
+
useFilterPanel,
|
|
1271
|
+
useImportExcel,
|
|
1272
|
+
useMainPanelNodes,
|
|
1273
|
+
useMainPanelTabs,
|
|
1274
|
+
useRowActions,
|
|
1275
|
+
useSelection,
|
|
1276
|
+
useTextFilterItem,
|
|
1277
|
+
visibleActions
|
|
1278
|
+
};
|
|
1279
|
+
//# sourceMappingURL=index.js.map
|