@littlekit/ui-core 0.1.0 → 0.2.0-beta.2

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.
Files changed (91) hide show
  1. package/dist/capability/execution-context.d.ts +12 -0
  2. package/dist/capability/execution-context.d.ts.map +1 -1
  3. package/dist/context/little-ui-context.d.ts +8 -1
  4. package/dist/context/little-ui-context.d.ts.map +1 -1
  5. package/dist/context/little-ui-context.js +24 -1
  6. package/dist/context/little-ui-context.js.map +1 -1
  7. package/dist/dialog/dialog-controller.d.ts +48 -0
  8. package/dist/dialog/dialog-controller.d.ts.map +1 -0
  9. package/dist/dialog/dialog-controller.js +224 -0
  10. package/dist/dialog/dialog-controller.js.map +1 -0
  11. package/dist/dialog/dialog-schema.d.ts +31 -0
  12. package/dist/dialog/dialog-schema.d.ts.map +1 -0
  13. package/dist/dialog/dialog-schema.js +69 -0
  14. package/dist/dialog/dialog-schema.js.map +1 -0
  15. package/dist/dialog/form-dialog.d.ts +7 -0
  16. package/dist/dialog/form-dialog.d.ts.map +1 -0
  17. package/dist/dialog/form-dialog.js +27 -0
  18. package/dist/dialog/form-dialog.js.map +1 -0
  19. package/dist/index.d.ts +17 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +7 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/normalize/normalize-date-limit.d.ts +5 -0
  24. package/dist/normalize/normalize-date-limit.d.ts.map +1 -0
  25. package/dist/normalize/normalize-date-limit.js +208 -0
  26. package/dist/normalize/normalize-date-limit.js.map +1 -0
  27. package/dist/normalize/normalize-form-item.d.ts.map +1 -1
  28. package/dist/normalize/normalize-form-item.js +81 -1
  29. package/dist/normalize/normalize-form-item.js.map +1 -1
  30. package/dist/normalize/normalize-upload.d.ts +5 -0
  31. package/dist/normalize/normalize-upload.d.ts.map +1 -0
  32. package/dist/normalize/normalize-upload.js +45 -0
  33. package/dist/normalize/normalize-upload.js.map +1 -0
  34. package/dist/registry/capability-registries.d.ts +17 -1
  35. package/dist/registry/capability-registries.d.ts.map +1 -1
  36. package/dist/registry/capability-registries.js +14 -0
  37. package/dist/registry/capability-registries.js.map +1 -1
  38. package/dist/runtime/date-rules.d.ts +48 -0
  39. package/dist/runtime/date-rules.d.ts.map +1 -0
  40. package/dist/runtime/date-rules.js +247 -0
  41. package/dist/runtime/date-rules.js.map +1 -0
  42. package/dist/runtime/form-controller.d.ts +23 -3
  43. package/dist/runtime/form-controller.d.ts.map +1 -1
  44. package/dist/runtime/form-controller.js +367 -2
  45. package/dist/runtime/form-controller.js.map +1 -1
  46. package/dist/runtime/option-runtime.d.ts +2 -0
  47. package/dist/runtime/option-runtime.d.ts.map +1 -1
  48. package/dist/runtime/option-runtime.js +161 -3
  49. package/dist/runtime/option-runtime.js.map +1 -1
  50. package/dist/runtime/tree-search.d.ts +6 -0
  51. package/dist/runtime/tree-search.d.ts.map +1 -0
  52. package/dist/runtime/tree-search.js +60 -0
  53. package/dist/runtime/tree-search.js.map +1 -0
  54. package/dist/runtime/upload-runtime.d.ts +30 -0
  55. package/dist/runtime/upload-runtime.d.ts.map +1 -0
  56. package/dist/runtime/upload-runtime.js +208 -0
  57. package/dist/runtime/upload-runtime.js.map +1 -0
  58. package/dist/runtime/validation-runtime.d.ts +3 -0
  59. package/dist/runtime/validation-runtime.d.ts.map +1 -1
  60. package/dist/runtime/validation-runtime.js +29 -2
  61. package/dist/runtime/validation-runtime.js.map +1 -1
  62. package/dist/schema/form-item.d.ts +17 -1
  63. package/dist/schema/form-item.d.ts.map +1 -1
  64. package/dist/schema/upload.d.ts +15 -0
  65. package/dist/schema/upload.d.ts.map +1 -0
  66. package/dist/schema/upload.js +2 -0
  67. package/dist/schema/upload.js.map +1 -0
  68. package/dist/state/form-state-store.d.ts +5 -5
  69. package/dist/state/form-state-store.d.ts.map +1 -1
  70. package/dist/state/form-state-store.js.map +1 -1
  71. package/dist/state/form-state.d.ts +24 -1
  72. package/dist/state/form-state.d.ts.map +1 -1
  73. package/dist/state/form-state.js +2 -0
  74. package/dist/state/form-state.js.map +1 -1
  75. package/dist/table/table-controller.d.ts +123 -0
  76. package/dist/table/table-controller.d.ts.map +1 -0
  77. package/dist/table/table-controller.js +647 -0
  78. package/dist/table/table-controller.js.map +1 -0
  79. package/dist/table/table-edit.d.ts +58 -0
  80. package/dist/table/table-edit.d.ts.map +1 -0
  81. package/dist/table/table-edit.js +224 -0
  82. package/dist/table/table-edit.js.map +1 -0
  83. package/dist/table/table-schema.d.ts +130 -0
  84. package/dist/table/table-schema.d.ts.map +1 -0
  85. package/dist/table/table-schema.js +342 -0
  86. package/dist/table/table-schema.js.map +1 -0
  87. package/dist/table/table-search.d.ts +17 -0
  88. package/dist/table/table-search.d.ts.map +1 -0
  89. package/dist/table/table-search.js +47 -0
  90. package/dist/table/table-search.js.map +1 -0
  91. package/package.json +1 -1
@@ -0,0 +1,647 @@
1
+ import { LittleUIContext } from '../context/little-ui-context.js';
2
+ import { LittleUIError } from '../error/error-model.js';
3
+ import { getByPath } from '../model/path-utils.js';
4
+ import { normalizeTableSchema } from './table-schema.js';
5
+ import { FormController } from '../runtime/form-controller.js';
6
+ import { tableSearchSubmit } from './table-search.js';
7
+ import { TableRowEdit, tableEditFormSchema } from './table-edit.js';
8
+ import { DialogController } from '../dialog/dialog-controller.js';
9
+ function copyQuery(value, ancestors = new Set(), fromSearch = false) {
10
+ if (value === undefined && fromSearch)
11
+ return null;
12
+ if (value === null ||
13
+ typeof value === 'string' ||
14
+ typeof value === 'boolean' ||
15
+ (typeof value === 'number' && Number.isFinite(value)))
16
+ return value;
17
+ if (!value || typeof value !== 'object' || ancestors.has(value))
18
+ throw new LittleUIError('LUI_TABLE_QUERY_INVALID', '查询条件必须是可序列化数据');
19
+ const prototype = Object.getPrototypeOf(value);
20
+ if (!Array.isArray(value) &&
21
+ prototype !== Object.prototype &&
22
+ prototype !== null)
23
+ throw new LittleUIError('LUI_TABLE_QUERY_INVALID', '查询条件必须是普通对象');
24
+ ancestors.add(value);
25
+ try {
26
+ if (Array.isArray(value))
27
+ return Object.freeze(value.map((item) => copyQuery(item, ancestors, fromSearch)));
28
+ const result = Object.create(null);
29
+ for (const [key, item] of Object.entries(value)) {
30
+ if (['__proto__', 'constructor', 'prototype'].includes(key))
31
+ throw new LittleUIError('LUI_TABLE_QUERY_INVALID', '查询条件包含保留字段');
32
+ if (item !== undefined || !fromSearch)
33
+ result[key] = copyQuery(item, ancestors, fromSearch);
34
+ }
35
+ return Object.freeze(result);
36
+ }
37
+ finally {
38
+ ancestors.delete(value);
39
+ }
40
+ }
41
+ function copyCellValue(value, seen = new WeakMap()) {
42
+ if (!value || typeof value !== 'object')
43
+ return value;
44
+ const existing = seen.get(value);
45
+ if (existing)
46
+ return existing;
47
+ if (value instanceof Date)
48
+ return new Date(value.getTime());
49
+ if (Array.isArray(value)) {
50
+ const copy = [];
51
+ seen.set(value, copy);
52
+ for (const item of value)
53
+ copy.push(copyCellValue(item, seen));
54
+ return Object.freeze(copy);
55
+ }
56
+ const copy = Object.create(null);
57
+ seen.set(value, copy);
58
+ for (const [key, item] of Object.entries(value))
59
+ copy[key] = copyCellValue(item, seen);
60
+ return Object.freeze(copy);
61
+ }
62
+ export class TableController {
63
+ schema;
64
+ searchForm;
65
+ resettingSearch = false;
66
+ state;
67
+ listeners = new Set();
68
+ context;
69
+ ownsContext;
70
+ request;
71
+ epoch = 0;
72
+ actionRequest;
73
+ editor;
74
+ pendingEditLeave;
75
+ destroyed = false;
76
+ constructor(schema, options = {}) {
77
+ this.schema = normalizeTableSchema(schema);
78
+ if (this.schema.edit)
79
+ tableEditFormSchema(this.schema.columns);
80
+ this.context = options.context ?? new LittleUIContext();
81
+ this.ownsContext = options.context === undefined;
82
+ const search = this.schema.search;
83
+ const query = search
84
+ ? copyQuery(search.form.model, new Set(), true)
85
+ : Object.freeze({});
86
+ this.searchForm =
87
+ search &&
88
+ new FormController(search.form, {
89
+ context: this.context,
90
+ onSubmit: ({ model }) => this.applyQuery(copyQuery(model, new Set(), true))
91
+ });
92
+ this.state = {
93
+ actionBusy: false,
94
+ rows: [],
95
+ selectedKeys: [],
96
+ total: 0,
97
+ page: 1,
98
+ pageSize: this.schema.pageSize,
99
+ sort: null,
100
+ query,
101
+ loading: false,
102
+ loaded: false
103
+ };
104
+ }
105
+ startEdit(rowKey) {
106
+ this.assertActive();
107
+ const config = this.schema.edit;
108
+ if (!config ||
109
+ this.state.loading ||
110
+ this.state.error ||
111
+ this.state.actionBusy ||
112
+ this.state.pendingAction)
113
+ return Promise.resolve('blocked');
114
+ if (this.editor?.rowKey === rowKey)
115
+ return Promise.resolve('success');
116
+ if (!this.state.rows.some((row) => getByPath(row, this.schema.rowKey) === rowKey))
117
+ return Promise.resolve('blocked');
118
+ return this.leaveEdit('change-row', async () => {
119
+ const row = this.state.rows.find((item) => getByPath(item, this.schema.rowKey) === rowKey);
120
+ if (!row)
121
+ return 'blocked';
122
+ this.editor = new TableRowEdit(rowKey, copyCellValue(row), this.schema.columns, config, this.context, () => this.publish({ ...this.state, edit: this.editor?.snapshot() }));
123
+ this.publish({ ...this.state, edit: this.editor.snapshot() });
124
+ return 'success';
125
+ });
126
+ }
127
+ setEditValue(columnKey, value) {
128
+ this.assertActive();
129
+ return this.pendingEditLeave
130
+ ? Promise.resolve()
131
+ : (this.editor?.setValue(columnKey, value) ?? Promise.resolve());
132
+ }
133
+ blurEditField(columnKey) {
134
+ this.assertActive();
135
+ return this.pendingEditLeave
136
+ ? Promise.resolve()
137
+ : (this.editor?.blur(columnKey) ?? Promise.resolve());
138
+ }
139
+ async saveEdit() {
140
+ this.assertActive();
141
+ const editor = this.editor;
142
+ if (!editor || this.pendingEditLeave)
143
+ return 'blocked';
144
+ const reload = editor.dirty;
145
+ const result = await editor.save();
146
+ if (this.destroyed || this.editor !== editor)
147
+ return 'cancelled';
148
+ if (result === 'success') {
149
+ this.closeEdit();
150
+ const token = this.epoch;
151
+ this.publish(this.state);
152
+ if (reload && !this.destroyed && token === this.epoch && !this.editor)
153
+ await this.load();
154
+ }
155
+ return result;
156
+ }
157
+ cancelEdit() {
158
+ this.assertActive();
159
+ return this.leaveEdit('cancel-edit', async () => {
160
+ this.publish(this.state);
161
+ return 'success';
162
+ });
163
+ }
164
+ confirmEditLeave() {
165
+ this.assertActive();
166
+ const pending = this.pendingEditLeave;
167
+ if (!pending)
168
+ return;
169
+ this.pendingEditLeave = undefined;
170
+ this.closeEdit();
171
+ pending.confirm();
172
+ }
173
+ cancelEditLeave() {
174
+ this.assertActive();
175
+ const pending = this.pendingEditLeave;
176
+ if (!pending)
177
+ return;
178
+ this.pendingEditLeave = undefined;
179
+ this.publish({ ...this.state, pendingEditLeave: undefined });
180
+ pending.cancel();
181
+ }
182
+ closeEdit() {
183
+ if (!this.editor)
184
+ return;
185
+ this.editor.destroy();
186
+ this.editor = undefined;
187
+ this.state = { ...this.state, edit: undefined, pendingEditLeave: undefined };
188
+ }
189
+ leaveEdit(reason, work) {
190
+ if (this.pendingEditLeave || this.editor?.busy)
191
+ return Promise.resolve('cancelled');
192
+ if (!this.editor?.dirty) {
193
+ this.closeEdit();
194
+ return work();
195
+ }
196
+ return new Promise((resolve, reject) => {
197
+ this.pendingEditLeave = {
198
+ confirm: () => {
199
+ try {
200
+ void work().then(resolve, reject);
201
+ }
202
+ catch (error) {
203
+ reject(error);
204
+ }
205
+ },
206
+ cancel: () => resolve('cancelled')
207
+ };
208
+ this.publish({ ...this.state, pendingEditLeave: reason });
209
+ });
210
+ }
211
+ getState() {
212
+ return this.state;
213
+ }
214
+ getCellContext(row, column, rowIndex) {
215
+ const snapshot = copyCellValue(row);
216
+ return Object.freeze({
217
+ row: snapshot,
218
+ value: getByPath(snapshot, column.field),
219
+ column: Object.freeze({ ...column }),
220
+ rowIndex
221
+ });
222
+ }
223
+ formatCell(cell) {
224
+ const key = cell.column.formatterKey;
225
+ if (key === undefined)
226
+ return undefined;
227
+ const format = this.context.tableFormatters.get(key);
228
+ if (!format)
229
+ throw new LittleUIError('LUI_TABLE_FORMATTER_MISSING', `未注册列格式化器:${key}`);
230
+ const result = format(cell);
231
+ if (result != null &&
232
+ !['string', 'number', 'boolean'].includes(typeof result))
233
+ throw new LittleUIError('LUI_TABLE_FORMATTER_RESULT_INVALID', '列格式化器必须返回文本或基本值');
234
+ return result;
235
+ }
236
+ subscribe(listener) {
237
+ this.assertActive();
238
+ const notify = () => listener();
239
+ this.listeners.add(notify);
240
+ return () => this.listeners.delete(notify);
241
+ }
242
+ publish(state) {
243
+ this.state = state;
244
+ for (const listener of [...this.listeners])
245
+ if (this.listeners.has(listener))
246
+ listener();
247
+ }
248
+ setQuery(query) {
249
+ this.assertActive();
250
+ if (!query || typeof query !== 'object' || Array.isArray(query))
251
+ throw new LittleUIError('LUI_TABLE_QUERY_INVALID', '查询条件必须为对象');
252
+ const snapshot = copyQuery(query);
253
+ return this.applyQuery(snapshot);
254
+ }
255
+ applyQuery(query, accepted) {
256
+ return this.leaveEdit('query', () => {
257
+ accepted?.();
258
+ if (this.destroyed)
259
+ return Promise.resolve('cancelled');
260
+ return this.updateAndLoad({ page: 1, query }, true);
261
+ });
262
+ }
263
+ async submitSearch() {
264
+ this.assertActive();
265
+ if (!this.searchForm)
266
+ return 'blocked';
267
+ if (this.resettingSearch)
268
+ return 'busy';
269
+ const result = await this.searchForm.executeAction(tableSearchSubmit);
270
+ if (result.status === 'success')
271
+ return result.value;
272
+ if (result.status === 'hidden' || result.status === 'disabled')
273
+ return 'blocked';
274
+ return result.status;
275
+ }
276
+ async resetSearch() {
277
+ this.assertActive();
278
+ const search = this.schema.search;
279
+ const form = this.searchForm;
280
+ if (!search || !form)
281
+ return 'blocked';
282
+ if (this.resettingSearch || form.getState().submitting)
283
+ return 'busy';
284
+ this.resettingSearch = true;
285
+ try {
286
+ const query = copyQuery(search.form.model, new Set(), true);
287
+ return await this.applyQuery(query, () => form.setInitialValues(search.form.model));
288
+ }
289
+ finally {
290
+ this.resettingSearch = false;
291
+ }
292
+ }
293
+ setPage(page) {
294
+ this.assertActive();
295
+ if (!Number.isSafeInteger(page) || page < 1)
296
+ throw new LittleUIError('LUI_TABLE_PAGE_INVALID', '页码必须为正整数');
297
+ return this.leaveEdit('page', () => this.updateAndLoad({ page }));
298
+ }
299
+ setPageSize(pageSize) {
300
+ this.assertActive();
301
+ if (!Number.isSafeInteger(pageSize) || pageSize < 1 || pageSize > 500)
302
+ throw new LittleUIError('LUI_TABLE_PAGE_INVALID', '每页数量应为 1 至 500');
303
+ return this.leaveEdit('page-size', () => this.updateAndLoad({ page: 1, pageSize }));
304
+ }
305
+ setSort(key, direction) {
306
+ this.assertActive();
307
+ let sort = null;
308
+ if (key !== null) {
309
+ const column = this.schema.columns.find((item) => item.key === key);
310
+ if (!column?.sortable || (direction !== 'asc' && direction !== 'desc'))
311
+ throw new LittleUIError('LUI_TABLE_SORT_INVALID', '排序列或方向无效');
312
+ sort = { key: column.key, field: column.field, direction };
313
+ }
314
+ return this.leaveEdit('sort', () => this.updateAndLoad({ page: 1, sort }));
315
+ }
316
+ /** 可选择当前页行;跨页模式允许保留已选的其他页行键。 */
317
+ setSelectedKeys(keys) {
318
+ this.assertActive();
319
+ if (!this.schema.selection ||
320
+ this.state.loading ||
321
+ !!this.state.error ||
322
+ !Array.isArray(keys))
323
+ throw new LittleUIError('LUI_TABLE_SELECTION_INVALID', '当前不能选择表格行');
324
+ const available = new Set(this.state.rows.map((row) => getByPath(row, this.schema.rowKey)));
325
+ const existing = new Set(this.state.selectedKeys);
326
+ if (new Set(keys).size !== keys.length ||
327
+ keys.some((key) => !available.has(key) &&
328
+ !(this.schema.selectionMode === 'across-pages' && existing.has(key))))
329
+ throw new LittleUIError('LUI_TABLE_SELECTION_INVALID', '选择必须是当前页中不重复的行键');
330
+ this.publish({ ...this.state, selectedKeys: [...keys] });
331
+ }
332
+ clearSelection() {
333
+ this.assertActive();
334
+ if (this.state.selectedKeys.length)
335
+ this.publish({ ...this.state, selectedKeys: [] });
336
+ }
337
+ isActionAllowed(key, rowKey) {
338
+ if (this.destroyed ||
339
+ this.state.loading ||
340
+ this.state.error ||
341
+ this.state.actionBusy ||
342
+ this.editor !== undefined)
343
+ return false;
344
+ const action = this.schema.actions.find((item) => item.key === key);
345
+ const row = this.state.rows.find((item) => getByPath(item, this.schema.rowKey) === rowKey);
346
+ if (!action || !row)
347
+ return false;
348
+ try {
349
+ const handler = this.context.tableActions.get(action.handlerKey);
350
+ return (!!handler &&
351
+ typeof handler.execute === 'function' &&
352
+ (!handler.canExecute || handler.canExecute({ row }) === true));
353
+ }
354
+ catch {
355
+ return false;
356
+ }
357
+ }
358
+ async requestAction(key, rowKey) {
359
+ this.assertActive();
360
+ if (this.state.actionBusy || this.state.pendingAction)
361
+ return 'busy';
362
+ if (!this.isActionAllowed(key, rowKey))
363
+ return 'blocked';
364
+ const action = this.schema.actions.find((item) => item.key === key);
365
+ if (action.confirm) {
366
+ this.publish({
367
+ ...this.state,
368
+ actionError: undefined,
369
+ pendingAction: { key, rowKey, message: action.confirm }
370
+ });
371
+ return 'confirmation-required';
372
+ }
373
+ return this.executeRowAction(key, rowKey);
374
+ }
375
+ cancelAction() {
376
+ this.assertActive();
377
+ if (this.state.pendingAction)
378
+ this.publish({ ...this.state, pendingAction: undefined });
379
+ }
380
+ async confirmAction() {
381
+ this.assertActive();
382
+ const pending = this.state.pendingAction;
383
+ if (!pending)
384
+ return 'blocked';
385
+ this.publish({ ...this.state, pendingAction: undefined });
386
+ if (!this.isActionAllowed(pending.key, pending.rowKey))
387
+ return 'blocked';
388
+ return this.executeRowAction(pending.key, pending.rowKey);
389
+ }
390
+ async executeRowAction(key, rowKey) {
391
+ const action = this.schema.actions.find((item) => item.key === key);
392
+ const row = this.state.rows.find((item) => getByPath(item, this.schema.rowKey) === rowKey);
393
+ const handler = this.context.tableActions.get(action.handlerKey);
394
+ const request = new AbortController();
395
+ this.actionRequest = request;
396
+ this.publish({ ...this.state, actionBusy: true, actionError: undefined });
397
+ let stop = () => { };
398
+ const aborted = new Promise((resolve) => {
399
+ stop = () => resolve();
400
+ request.signal.addEventListener('abort', stop, { once: true });
401
+ });
402
+ try {
403
+ const prepared = await Promise.race([
404
+ Promise.resolve().then(() => request.signal.aborted
405
+ ? undefined
406
+ : handler.execute({ row, signal: request.signal })),
407
+ aborted
408
+ ]);
409
+ if (request.signal.aborted ||
410
+ this.destroyed ||
411
+ this.actionRequest !== request)
412
+ return 'cancelled';
413
+ let saved = true;
414
+ if (action.dialog) {
415
+ saved = await this.openActionDialog({ ...action.dialog, key, title: action.dialog.title ?? action.label }, row, prepared, action.refreshOnSuccess === true, request.signal);
416
+ if (request.signal.aborted ||
417
+ this.destroyed ||
418
+ this.actionRequest !== request)
419
+ return 'cancelled';
420
+ }
421
+ const epoch = this.epoch;
422
+ this.actionRequest = undefined;
423
+ this.publish({ ...this.state, actionBusy: false });
424
+ if (!action.dialog &&
425
+ saved &&
426
+ action.refreshOnSuccess &&
427
+ !this.destroyed &&
428
+ epoch === this.epoch &&
429
+ !this.editor)
430
+ await this.load();
431
+ return saved ? 'success' : 'cancelled';
432
+ }
433
+ catch (cause) {
434
+ if (request.signal.aborted ||
435
+ this.destroyed ||
436
+ this.actionRequest !== request)
437
+ return 'cancelled';
438
+ this.actionRequest = undefined;
439
+ this.publish({
440
+ ...this.state,
441
+ actionBusy: false,
442
+ actionError: cause instanceof Error ? cause.message : '行操作失败'
443
+ });
444
+ return 'failed';
445
+ }
446
+ finally {
447
+ request.signal.removeEventListener('abort', stop);
448
+ }
449
+ }
450
+ async openActionDialog(schema, row, prepared, refreshOnSuccess, signal) {
451
+ if (prepared !== undefined && (!prepared || typeof prepared !== 'object'))
452
+ throw new Error('弹窗准备函数应返回配置对象');
453
+ const options = (prepared ?? {});
454
+ let saved = false;
455
+ const controller = new DialogController(schema, {
456
+ ...options,
457
+ onConfirm: async (args) => {
458
+ const result = await (options.onConfirm?.(args) ?? {
459
+ status: 'success'
460
+ });
461
+ if (!args.signal.aborted && result.status === 'success') {
462
+ saved = true;
463
+ if (refreshOnSuccess && !signal.aborted)
464
+ await this.load(false, true);
465
+ }
466
+ return result;
467
+ }
468
+ });
469
+ const abort = () => controller.setOpen(false);
470
+ controller.open();
471
+ let unsubscribe = () => { };
472
+ try {
473
+ await new Promise((resolve) => {
474
+ unsubscribe = controller.subscribe(() => {
475
+ if (!controller.getState().open)
476
+ resolve();
477
+ });
478
+ signal.addEventListener('abort', abort, { once: true });
479
+ this.publish({
480
+ ...this.state,
481
+ actionDialog: {
482
+ controller,
483
+ key: schema.key,
484
+ row,
485
+ ...(options.form ? { form: options.form } : {})
486
+ }
487
+ });
488
+ });
489
+ return saved;
490
+ }
491
+ finally {
492
+ signal.removeEventListener('abort', abort);
493
+ unsubscribe();
494
+ controller.destroy();
495
+ if (this.state.actionDialog?.controller === controller)
496
+ this.publish({ ...this.state, actionDialog: undefined });
497
+ }
498
+ }
499
+ cancelRowAction() {
500
+ this.actionRequest?.abort();
501
+ this.actionRequest = undefined;
502
+ }
503
+ refresh() {
504
+ this.assertActive();
505
+ return this.leaveEdit('refresh', () => this.load());
506
+ }
507
+ destroy() {
508
+ if (this.destroyed)
509
+ return;
510
+ this.destroyed = true;
511
+ this.epoch++;
512
+ this.request?.abort();
513
+ this.request = undefined;
514
+ this.cancelRowAction();
515
+ this.pendingEditLeave?.cancel();
516
+ this.pendingEditLeave = undefined;
517
+ this.closeEdit();
518
+ this.searchForm?.destroy();
519
+ this.listeners.clear();
520
+ if (this.ownsContext)
521
+ this.context.dispose();
522
+ }
523
+ updateAndLoad(patch, clearSelection = false) {
524
+ const token = ++this.epoch;
525
+ this.request?.abort();
526
+ this.cancelRowAction();
527
+ this.publish({
528
+ ...this.state,
529
+ ...patch,
530
+ selectedKeys: clearSelection || this.schema.selectionMode === 'page'
531
+ ? []
532
+ : this.state.selectedKeys,
533
+ actionBusy: false,
534
+ actionError: undefined,
535
+ pendingAction: undefined
536
+ });
537
+ if (this.destroyed || token !== this.epoch)
538
+ return Promise.resolve('cancelled');
539
+ return this.load();
540
+ }
541
+ async load(reconciled = false, keepAction = false) {
542
+ const token = ++this.epoch;
543
+ this.request?.abort();
544
+ const request = new AbortController();
545
+ this.request = request;
546
+ if (!keepAction)
547
+ this.cancelRowAction();
548
+ const snapshot = this.state;
549
+ this.publish({
550
+ ...snapshot,
551
+ selectedKeys: this.schema.selectionMode === 'page' ? [] : snapshot.selectedKeys,
552
+ actionBusy: keepAction && snapshot.actionBusy,
553
+ actionError: undefined,
554
+ pendingAction: undefined,
555
+ loading: true,
556
+ error: undefined
557
+ });
558
+ if (this.destroyed ||
559
+ token !== this.epoch ||
560
+ request.signal.aborted ||
561
+ this.request !== request)
562
+ return 'cancelled';
563
+ let stop = () => { };
564
+ const aborted = new Promise((resolve) => {
565
+ stop = () => resolve(undefined);
566
+ request.signal.addEventListener('abort', stop, { once: true });
567
+ });
568
+ try {
569
+ const loader = this.context.tables.get(this.schema.dataSource.loaderKey);
570
+ if (!loader)
571
+ throw new LittleUIError('LUI_TABLE_LOADER_MISSING', '未注册表格数据源');
572
+ const result = await Promise.race([
573
+ Promise.resolve().then(() => request.signal.aborted
574
+ ? undefined
575
+ : loader({
576
+ query: snapshot.query,
577
+ page: snapshot.page,
578
+ pageSize: snapshot.pageSize,
579
+ sort: snapshot.sort,
580
+ signal: request.signal
581
+ })),
582
+ aborted
583
+ ]);
584
+ if (request.signal.aborted || this.destroyed || this.request !== request)
585
+ return 'cancelled';
586
+ if (!result || typeof result !== 'object')
587
+ throw new LittleUIError('LUI_TABLE_DATA_INVALID', '表格数据格式无效');
588
+ if (!Array.isArray(result.rows) ||
589
+ !Number.isSafeInteger(result.total) ||
590
+ result.total < 0 ||
591
+ result.rows.length > snapshot.pageSize ||
592
+ result.rows.length > result.total)
593
+ throw new LittleUIError('LUI_TABLE_DATA_INVALID', '表格数据格式无效');
594
+ const keys = new Set();
595
+ for (const row of result.rows) {
596
+ if (!row || typeof row !== 'object' || Array.isArray(row))
597
+ throw new LittleUIError('LUI_TABLE_DATA_INVALID', '表格行必须为对象');
598
+ const key = getByPath(row, this.schema.rowKey);
599
+ if ((typeof key !== 'string' || !key) &&
600
+ (typeof key !== 'number' || !Number.isFinite(key)))
601
+ throw new LittleUIError('LUI_TABLE_ROW_KEY_INVALID', '表格行键无效');
602
+ if (keys.has(key))
603
+ throw new LittleUIError('LUI_TABLE_ROW_KEY_INVALID', '表格行键重复');
604
+ keys.add(key);
605
+ }
606
+ const lastPage = Math.max(1, Math.ceil(result.total / snapshot.pageSize));
607
+ if (snapshot.page > lastPage) {
608
+ if (reconciled)
609
+ throw new LittleUIError('LUI_TABLE_DATA_INVALID', '分页结果持续变化,请重试');
610
+ this.request = undefined;
611
+ this.publish({ ...this.state, page: lastPage, loading: true });
612
+ if (this.destroyed || token !== this.epoch)
613
+ return 'cancelled';
614
+ return this.load(true, keepAction);
615
+ }
616
+ this.request = undefined;
617
+ this.publish({
618
+ ...this.state,
619
+ rows: [...result.rows],
620
+ total: result.total,
621
+ loading: false,
622
+ loaded: true,
623
+ error: undefined
624
+ });
625
+ return 'success';
626
+ }
627
+ catch (cause) {
628
+ if (request.signal.aborted || this.destroyed || this.request !== request)
629
+ return 'cancelled';
630
+ this.request = undefined;
631
+ this.publish({
632
+ ...this.state,
633
+ loading: false,
634
+ error: cause instanceof Error ? cause.message : '表格加载失败'
635
+ });
636
+ return 'failed';
637
+ }
638
+ finally {
639
+ request.signal.removeEventListener('abort', stop);
640
+ }
641
+ }
642
+ assertActive() {
643
+ if (this.destroyed)
644
+ throw new LittleUIError('LUI_TABLE_DESTROYED', '表格控制器已销毁');
645
+ }
646
+ }
647
+ //# sourceMappingURL=table-controller.js.map