@motifx-community/ui 0.1.0-beta.14 → 0.1.0-beta.16
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/CHANGELOG.md +113 -0
- package/README.md +2 -2
- package/dist/components/diagram/Diagram.d.ts +607 -0
- package/dist/components/diagram/DiagramAutoPan.d.ts +17 -0
- package/dist/components/diagram/DiagramBends.d.ts +70 -0
- package/dist/components/diagram/DiagramDragging.d.ts +66 -0
- package/dist/components/diagram/DiagramEdgeView.d.ts +78 -0
- package/dist/components/diagram/DiagramGrid.d.ts +13 -0
- package/dist/components/diagram/DiagramHistory.d.ts +16 -0
- package/dist/components/diagram/DiagramLinks.d.ts +177 -0
- package/dist/components/diagram/DiagramMap.d.ts +62 -0
- package/dist/components/diagram/DiagramNodeView.d.ts +143 -0
- package/dist/components/diagram/DiagramOverview.d.ts +20 -0
- package/dist/components/diagram/DiagramPanning.d.ts +34 -0
- package/dist/components/diagram/diagram-alignment.d.ts +2 -0
- package/dist/components/diagram/diagram-auto-pan-host.d.ts +8 -0
- package/dist/components/diagram/diagram-background.d.ts +2 -0
- package/dist/components/diagram/diagram-bends-host.d.ts +17 -0
- package/dist/components/diagram/diagram-card-kind.d.ts +42 -0
- package/dist/components/diagram/diagram-card-node.d.ts +11 -0
- package/dist/components/diagram/diagram-center-options.d.ts +6 -0
- package/dist/components/diagram/diagram-change.d.ts +11 -0
- package/dist/components/diagram/diagram-class-member.d.ts +13 -0
- package/dist/components/diagram/diagram-class-node.d.ts +12 -0
- package/dist/components/diagram/diagram-column-checked-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connected-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-connecting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connection.d.ts +7 -0
- package/dist/components/diagram/diagram-data.d.ts +12 -0
- package/dist/components/diagram/diagram-deleted-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-deleting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-dragging-host.d.ts +47 -0
- package/dist/components/diagram/diagram-drawn-node.d.ts +10 -0
- package/dist/components/diagram/diagram-edge-end.d.ts +2 -0
- package/dist/components/diagram/diagram-edge-path.d.ts +5 -0
- package/dist/components/diagram/diagram-edge-reshaped-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-edge-reshaping-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-edge.d.ts +49 -0
- package/dist/components/diagram/diagram-edit.d.ts +10 -0
- package/dist/components/diagram/diagram-export-options.d.ts +14 -0
- package/dist/components/diagram/diagram-export.d.ts +28 -0
- package/dist/components/diagram/diagram-fit-options.d.ts +12 -0
- package/dist/components/diagram/diagram-history-changed-event-args.d.ts +14 -0
- package/dist/components/diagram/diagram-history-step.d.ts +9 -0
- package/dist/components/diagram/diagram-kinds.d.ts +9 -0
- package/dist/components/diagram/diagram-layered.d.ts +39 -0
- package/dist/components/diagram/diagram-layout-direction.d.ts +2 -0
- package/dist/components/diagram/diagram-layout-options.d.ts +24 -0
- package/dist/components/diagram/diagram-layout-type.d.ts +5 -0
- package/dist/components/diagram/diagram-layout.d.ts +33 -0
- package/dist/components/diagram/diagram-line-look.d.ts +6 -0
- package/dist/components/diagram/diagram-line.d.ts +11 -0
- package/dist/components/diagram/diagram-links-host.d.ts +45 -0
- package/dist/components/diagram/diagram-links-options.d.ts +7 -0
- package/dist/components/diagram/diagram-map-props.d.ts +10 -0
- package/dist/components/diagram/diagram-map-target.d.ts +27 -0
- package/dist/components/diagram/diagram-marker.d.ts +6 -0
- package/dist/components/diagram/diagram-mini-map-options.d.ts +13 -0
- package/dist/components/diagram/diagram-mini-map-position.d.ts +2 -0
- package/dist/components/diagram/diagram-node-context.d.ts +26 -0
- package/dist/components/diagram/diagram-node-host.d.ts +23 -0
- package/dist/components/diagram/diagram-node-kind.d.ts +43 -0
- package/dist/components/diagram/diagram-node-move.d.ts +11 -0
- package/dist/components/diagram/diagram-node-resized-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-node-resizing-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-node-size.d.ts +8 -0
- package/dist/components/diagram/diagram-node-status.d.ts +2 -0
- package/dist/components/diagram/diagram-node-toggled-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-node.d.ts +56 -0
- package/dist/components/diagram/diagram-nodes-moved-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-nodes-moving-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-panning-host.d.ts +19 -0
- package/dist/components/diagram/diagram-pasted-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-pasting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-path-ends.d.ts +11 -0
- package/dist/components/diagram/diagram-path-result.d.ts +6 -0
- package/dist/components/diagram/diagram-paths.d.ts +35 -0
- package/dist/components/diagram/diagram-point.d.ts +5 -0
- package/dist/components/diagram/diagram-port-place.d.ts +9 -0
- package/dist/components/diagram/diagram-port.d.ts +27 -0
- package/dist/components/diagram/diagram-props.d.ts +159 -0
- package/dist/components/diagram/diagram-reconnected-event-args.d.ts +10 -0
- package/dist/components/diagram/diagram-reconnecting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-rect.d.ts +7 -0
- package/dist/components/diagram/diagram-resize-side.d.ts +2 -0
- package/dist/components/diagram/diagram-route.d.ts +10 -0
- package/dist/components/diagram/diagram-selection-changed-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-selection.d.ts +5 -0
- package/dist/components/diagram/diagram-side.d.ts +2 -0
- package/dist/components/diagram/diagram-table-column.d.ts +13 -0
- package/dist/components/diagram/diagram-table-node.d.ts +12 -0
- package/dist/components/diagram/diagram-tree.d.ts +14 -0
- package/dist/components/diagram/diagram-viewport-changed-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-viewport-options.d.ts +5 -0
- package/dist/components/diagram/diagram-viewport.d.ts +6 -0
- package/dist/components/diagram/index.d.ts +65 -0
- package/dist/components/diagram/swallow-next-click.d.ts +5 -0
- package/dist/components/expressioneditor/ExpressionEditor.d.ts +111 -0
- package/dist/components/expressioneditor/expression-editor-props.d.ts +24 -0
- package/dist/components/expressioneditor/index.d.ts +2 -0
- package/dist/components/formlayout/FormLayout.d.ts +10 -3
- package/dist/components/formlayout/form-editor-type.d.ts +1 -1
- package/dist/components/formlayout/form-item.d.ts +4 -1
- package/dist/components/formlayout/form-layout-props.d.ts +1 -1
- package/dist/components/formlayout/index.d.ts +1 -0
- package/dist/components/formlayout/schema-items.d.ts +26 -0
- package/dist/components/pivotgrid/model/PivotAxisWindow.d.ts +53 -1
- package/dist/components/pivotgrid/model/PivotCellWindow.d.ts +2 -0
- package/dist/components/pivotgrid/model/PivotWindows.d.ts +3 -6
- package/dist/components/toolbar/Toolbar.d.ts +5 -3
- package/dist/core/control.d.ts +2 -0
- package/dist/core/keyboard.d.ts +5 -0
- package/dist/core/measure/index.d.ts +2 -0
- package/dist/core/measure/picture.d.ts +27 -0
- package/dist/core/shortcut-options.d.ts +5 -2
- package/dist/core/ui-strings.d.ts +191 -0
- package/dist/generated/tokens.d.ts +1615 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11501 -705
- package/dist/index.js.map +1 -1
- package/dist/locales/en.json +180 -1
- package/dist/locales/tr.json +180 -1
- package/dist/styles.css +2262 -3
- package/dist/tokens/index.d.ts +1615 -0
- package/dist/tokens/index.js +1615 -0
- package/dist/tokens/index.js.map +1 -1
- package/dist/{tr-BGbF9uCs.js → tr-4GNbFdQh.js} +361 -3
- package/dist/{tr-BGbF9uCs.js.map → tr-4GNbFdQh.js.map} +1 -1
- package/docs/accordion.md +1 -1
- package/docs/actionsheet.md +1 -1
- package/docs/appbar.md +1 -1
- package/docs/appmenu-api.md +1 -1
- package/docs/appmenu-programmatic.md +1 -1
- package/docs/appmenu-search.md +1 -1
- package/docs/appmenu.md +1 -1
- package/docs/avatar.md +1 -1
- package/docs/badge.md +1 -1
- package/docs/bar-gauge-angles.md +1 -1
- package/docs/bar-gauge-data.md +1 -1
- package/docs/bar-gauge-labels.md +1 -1
- package/docs/bar-gauge.md +1 -1
- package/docs/barcode.md +1 -1
- package/docs/bottomnavigation.md +1 -1
- package/docs/bottomsheet.md +1 -1
- package/docs/breadcrumb.md +1 -1
- package/docs/button.md +1 -1
- package/docs/buttongroup.md +1 -1
- package/docs/buttons.md +1 -1
- package/docs/calendar.md +1 -1
- package/docs/card.md +1 -1
- package/docs/chart-bars.md +1 -1
- package/docs/chart-candles.md +1 -1
- package/docs/chart-funnel.md +1 -1
- package/docs/chart-heatmap.md +1 -1
- package/docs/chart-lines.md +1 -1
- package/docs/chart-live.md +1 -1
- package/docs/chart-pie.md +1 -1
- package/docs/chart-radar.md +1 -1
- package/docs/chart-ranges.md +1 -1
- package/docs/chart-references.md +1 -1
- package/docs/chart-scatter.md +1 -1
- package/docs/chart-waterfall.md +1 -1
- package/docs/chart-zoom.md +1 -1
- package/docs/chart.md +1 -1
- package/docs/chat.md +1 -1
- package/docs/checkbox.md +1 -1
- package/docs/chip.md +1 -1
- package/docs/choice.md +1 -1
- package/docs/colorbox.md +1 -1
- package/docs/combobox.md +1 -1
- package/docs/commands.md +1 -1
- package/docs/control.md +1 -1
- package/docs/data.md +2 -2
- package/docs/datagrid-api.md +1 -1
- package/docs/datagrid-bands.md +1 -1
- package/docs/datagrid-drag-select.md +1 -1
- package/docs/datagrid-drag.md +1 -1
- package/docs/datagrid-editing.md +1 -1
- package/docs/datagrid-filtering.md +1 -1
- package/docs/datagrid-forms.md +1 -1
- package/docs/datagrid-grouping.md +1 -1
- package/docs/datagrid-layout.md +1 -1
- package/docs/datagrid-lookups.md +1 -1
- package/docs/datagrid-master-detail.md +1 -1
- package/docs/datagrid-scrolling.md +1 -1
- package/docs/datagrid-selection.md +1 -1
- package/docs/datagrid.md +1 -1
- package/docs/datalayer-api.md +491 -0
- package/docs/datalayer-changes.md +501 -0
- package/docs/datalayer-odata.md +998 -0
- package/docs/datalayer-pivot.md +850 -0
- package/docs/datalayer-queries.md +516 -0
- package/docs/datalayer-rest.md +529 -0
- package/docs/datalayer-soap.md +576 -0
- package/docs/datalayer-source.md +464 -0
- package/docs/datalayer.md +264 -0
- package/docs/datebox.md +1 -1
- package/docs/daterangebox.md +1 -1
- package/docs/diagram-api.md +200 -0
- package/docs/diagram-commands.md +79 -0
- package/docs/diagram-connections.md +157 -0
- package/docs/diagram-custom-nodes.md +325 -0
- package/docs/diagram-designer.md +816 -0
- package/docs/diagram-editing.md +62 -0
- package/docs/diagram-flow.md +157 -0
- package/docs/diagram-keyboard.md +58 -0
- package/docs/diagram-layout.md +354 -0
- package/docs/diagram-nodes.md +206 -0
- package/docs/diagram-tables.md +212 -0
- package/docs/diagram.md +217 -0
- package/docs/display.md +1 -1
- package/docs/draggable.md +1 -1
- package/docs/drawer.md +1 -1
- package/docs/dropdown.md +1 -1
- package/docs/editors.md +1 -1
- package/docs/expressioneditor.md +469 -0
- package/docs/expressions.md +431 -0
- package/docs/feedback.md +1 -1
- package/docs/fileuploader.md +1 -1
- package/docs/filterbuilder.md +1 -1
- package/docs/formlayout-schema.md +143 -0
- package/docs/formlayout.md +16 -4
- package/docs/foundations.md +1 -1
- package/docs/gallery.md +1 -1
- package/docs/gauge-base.md +1 -1
- package/docs/gauge-circular.md +1 -1
- package/docs/gauge-dashboards.md +1 -1
- package/docs/gauge-linear.md +1 -1
- package/docs/gauge-markers.md +1 -1
- package/docs/gauge-marks.md +1 -1
- package/docs/gauge-ranges.md +1 -1
- package/docs/gauge-scales.md +1 -1
- package/docs/gauge.md +1 -1
- package/docs/home.md +1 -1
- package/docs/keyboard.md +2 -2
- package/docs/language.md +1 -1
- package/docs/layout.md +1 -1
- package/docs/listbox.md +1 -1
- package/docs/loadpanel.md +1 -1
- package/docs/lookup.md +1 -1
- package/docs/markdown.md +1 -1
- package/docs/menus-api.md +1 -1
- package/docs/menus-context.md +1 -1
- package/docs/menus-customization.md +1 -1
- package/docs/menus-merging.md +1 -1
- package/docs/menus-programmatic.md +1 -1
- package/docs/menus.md +1 -1
- package/docs/multiview.md +1 -1
- package/docs/navigation.md +1 -1
- package/docs/numberbox.md +1 -1
- package/docs/otpinput.md +1 -1
- package/docs/pagination.md +1 -1
- package/docs/pivotgrid-api.md +1 -1
- package/docs/pivotgrid-chart.md +1 -1
- package/docs/pivotgrid-drilldown.md +1 -1
- package/docs/pivotgrid-export.md +1 -1
- package/docs/pivotgrid-fields.md +1 -1
- package/docs/pivotgrid-keyboard.md +1 -1
- package/docs/pivotgrid-layout.md +1 -1
- package/docs/pivotgrid-panel.md +1 -1
- package/docs/pivotgrid-remote.md +122 -44
- package/docs/pivotgrid-sorting.md +3 -3
- package/docs/pivotgrid-state.md +1 -1
- package/docs/pivotgrid-summaries.md +1 -1
- package/docs/pivotgrid.md +1 -1
- package/docs/popover.md +1 -1
- package/docs/progress.md +1 -1
- package/docs/qrcode.md +1 -1
- package/docs/radiogroup.md +1 -1
- package/docs/rating.md +1 -1
- package/docs/resizable.md +1 -1
- package/docs/ribbonmenu-api.md +1 -1
- package/docs/ribbonmenu-commands.md +1 -1
- package/docs/ribbonmenu-customization.md +1 -1
- package/docs/ribbonmenu-display.md +1 -1
- package/docs/ribbonmenu-layouts.md +1 -1
- package/docs/ribbonmenu-programmatic.md +1 -1
- package/docs/ribbonmenu.md +1 -1
- package/docs/scheduler-agenda.md +1 -1
- package/docs/scheduler-big-data.md +1 -1
- package/docs/scheduler-editing.md +1 -1
- package/docs/scheduler-form.md +1 -1
- package/docs/scheduler-icalendar.md +1 -1
- package/docs/scheduler-mobile.md +1 -1
- package/docs/scheduler-recurrence.md +1 -1
- package/docs/scheduler-resources.md +1 -1
- package/docs/scheduler-timeline.md +1 -1
- package/docs/scheduler.md +1 -1
- package/docs/scrollview.md +1 -1
- package/docs/shortcuteditor.md +1 -1
- package/docs/showcase-flowstudio.md +61 -0
- package/docs/showcase-home.md +1 -1
- package/docs/showcase-mail.md +1 -1
- package/docs/showcase.md +1 -1
- package/docs/signature.md +1 -1
- package/docs/skeleton.md +1 -1
- package/docs/slider.md +1 -1
- package/docs/sortable.md +1 -1
- package/docs/sparkline.md +1 -1
- package/docs/speeddial.md +1 -1
- package/docs/splitter.md +1 -1
- package/docs/stepper.md +1 -1
- package/docs/switch.md +1 -1
- package/docs/tabdocs-dock.md +1 -1
- package/docs/tabdocs-drag.md +1 -1
- package/docs/tabdocs-menus.md +1 -1
- package/docs/tabdocs-mobile.md +1 -1
- package/docs/tabdocs-tabs.md +1 -1
- package/docs/tabdocs-windows.md +1 -1
- package/docs/tabdocs-workspace.md +1 -1
- package/docs/tabdocs.md +1 -1
- package/docs/tabs.md +1 -1
- package/docs/tagbox.md +1 -1
- package/docs/textarea.md +1 -1
- package/docs/textbox.md +1 -1
- package/docs/tileview.md +1 -1
- package/docs/timeline.md +1 -1
- package/docs/toast.md +1 -1
- package/docs/tokens.md +324 -1
- package/docs/toolbar-api.md +1 -1
- package/docs/toolbar-customization.md +1 -1
- package/docs/toolbar.md +1 -1
- package/docs/tooltip.md +1 -1
- package/docs/treeview.md +1 -1
- package/docs/validationsummary.md +1 -1
- package/docs/window.md +1 -1
- package/docs/wizard.md +1 -1
- package/llms-full.txt +10142 -462
- package/llms.txt +32 -1
- package/package.json +2 -2
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
# Data layer: Tracking and saving changes
|
|
2
|
+
|
|
3
|
+
> Data layer · @motifx-community/data 0.1.0-beta.3
|
|
4
|
+
|
|
5
|
+
`ChangeTracker` keeps the unsaved edits, new rows and deletions over a [`DataSource`](datalayer-source.md) and saves them through its adapter. Edits are kept by row key, not by position, so they survive paging, sorting and scrolling; the row object changes only when the row is saved.
|
|
6
|
+
|
|
7
|
+
## What a change tracker is
|
|
8
|
+
|
|
9
|
+
A component that edits rows (a grid, a form over a list) holds the entered values in a `ChangeTracker` instead of writing them onto the rows. Each row's draft is stored under its key, so it stays while the source pages, sorts or scrolls, and the row keeps the values it was read with until its save succeeds. The source's adapter must name a key; without one the constructor throws `MJD303`.
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { data } from '@motifx-community/data';
|
|
13
|
+
|
|
14
|
+
const orders = new data.DataSource<Order, number>({ adapter, loadMode: 'all' });
|
|
15
|
+
await orders.load();
|
|
16
|
+
|
|
17
|
+
const tracker = new data.ChangeTracker<Order, number>({ source: orders });
|
|
18
|
+
const row = orders.rowByKey(1)!;
|
|
19
|
+
|
|
20
|
+
tracker.setValue(row, 'amount', 12); // shown with tracker.getValue(row, 'amount')
|
|
21
|
+
await tracker.commit(row); // the user left the row: it is checked and saved
|
|
22
|
+
const added = tracker.add({ customer: 'Ada' });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The kit's [DataGrid](datagrid-editing.md) creates a change tracker of its own over its data source, configured through the grid's `saveMode`, `newRow` and `validateRow` props, and exposes it through its `changeTracker` property.
|
|
26
|
+
|
|
27
|
+
## Editing values
|
|
28
|
+
|
|
29
|
+
`setValue(row, field, value)` records an entered value in the row's draft; `getValue(row, field)` returns the value to show: the entered one when there is one, else the row's own. Entering the value the row already has removes the field from the draft, so the row returns to unchanged; the `sameValue` option decides that comparison (by default empty values are the same, dates compare by their moment, and everything else with `Object.is`). `isChanged(row, field?)` indicates whether the field, or without a field the row, has something to save.
|
|
30
|
+
|
|
31
|
+
`commit(row)` tells the tracker the user left the row: the row is checked and sent as the [save mode](#save-modes) says. A row with a message is not sent and `commit` returns `false`. When the save succeeds, the saved values are written onto the row object, which stays the same object.
|
|
32
|
+
|
|
33
|
+
The tracker is not reactive state: components subscribe to [`onChanged`](#events) and read again what they show, as the example below does with the `live` bindings.
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { data } from '@motifx-community/data';
|
|
37
|
+
|
|
38
|
+
/** Rows in memory that answer a little later, the way a server does; for the examples. */
|
|
39
|
+
export class SlowAdapter<T extends object, K> extends data.ArrayAdapter<T, K> {
|
|
40
|
+
constructor(
|
|
41
|
+
options: data.ArrayAdapterOptions<T>,
|
|
42
|
+
private readonly delayMs = 300,
|
|
43
|
+
) {
|
|
44
|
+
super(options);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override async load(options: data.LoadOptions = {}): Promise<data.LoadResult<T>> {
|
|
48
|
+
await new Promise((done) => setTimeout(done, this.delayMs));
|
|
49
|
+
return super.load(options);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Every editor above reads through `getValue` and writes through `setValue`; the badge shows `statusOf`, and the field messages come from `errorOf`. With `saveMode: 'batch'`, Delete marks a row and “Save changes” sends everything at once; a price below zero gets a message and its row is kept back. The service answers after a moment and runs in the page; it also rejects a product name another product already has, so two rows with the same name show how server messages reach the editors.
|
|
55
|
+
|
|
56
|
+
## New rows
|
|
57
|
+
|
|
58
|
+
`add(values?, position?)` adds a new row at a position of `newRows` (default: the end) and returns it. The row starts with the values of the `createRow` option; `values` count as entered values and join the draft. The row has no key yet: the tracker identifies it as `+1`, `+2`, … (`rowIdOf`).
|
|
59
|
+
|
|
60
|
+
A new row is the tracker's own object, the one in `newRows`. When it is saved, the entered values are written onto that object and the same object is sent, so a backend that keeps objects, such as an `ArrayAdapter`, keeps this one. A new row whose save failed stays in `newRows` with its values, until it is saved or discarded; a new row nothing was entered in (`isUntouched`) is dropped by `commit` and never sent.
|
|
61
|
+
|
|
62
|
+
A saved new row leaves `newRows` once the source has it among its rows, or when a new query starts. With `keepSavedNewRows: true` it stays until the source's `reload()` (or `releaseSaved()`), so a component can keep showing it where it was entered.
|
|
63
|
+
|
|
64
|
+
## Deleting rows
|
|
65
|
+
|
|
66
|
+
`remove(row)` deletes a row as the save mode says: `'batch'` marks it (status `'removed'`), the other modes send the deletion at once. A new row is dropped instead of sent. `restore(row)` takes a deletion mark back. When a deletion fails outside `'batch'`, the mark is cleared and the row stays, with status `'failed'` and the server's message; in `'batch'` the mark stays, so `saveChanges()` sends it again.
|
|
67
|
+
|
|
68
|
+
`removeRows(rows)` deletes several rows in one save: one request when the adapter saves in batches (OData `$batch`, a REST batch endpoint, a SOAP `save` operation), else one request per row. It resolves to whether every row was deleted; in `'batch'` and `'background'`, to whether every row was marked or queued. `removeKeys(keys)` does the same by key; a key whose row is not found counts as not deleted.
|
|
69
|
+
|
|
70
|
+
## Save modes
|
|
71
|
+
|
|
72
|
+
| Mode | Rows are sent |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| `'wait'` | A row is sent when the user leaves it (`commit`), and the caller waits for the answer: `commit` resolves to whether the row was saved. The default. |
|
|
75
|
+
| `'background'` | The same, through one queue, without waiting: `commit` returns `true` at once, and rows are sent in the order they were left. A row changed again while it is being saved is sent again, unless it is `currentRow`. |
|
|
76
|
+
| `'batch'` | Only `saveChanges()` sends. Edits, new rows and deletion marks collect until then. |
|
|
77
|
+
|
|
78
|
+
`saveChanges()` saves every unsaved change at once and resolves to `{ saved, failed }`. Rows with a message are not sent and count as failed. The changes go out in the order of `saveOrder` (default `['remove', 'update', 'insert']`: deletions first, so unique values are free again), in one request when the adapter saves in batches, else one by one ([the adapter contract](datalayer-api.md)). It works in every mode; in `'wait'` and `'background'` it is the way to send failed rows again.
|
|
79
|
+
|
|
80
|
+
`currentRow` names the row the user is in. A message in it does not count in `failedCount` yet, and `'background'` does not send that row again while it is edited.
|
|
81
|
+
|
|
82
|
+
## What is sent
|
|
83
|
+
|
|
84
|
+
The tracker saves `Change` objects through the adapter. An update carries only the changed fields in `data`, with the row as it was read in `original`; a deletion carries the key and `original`; an insert carries the values of the new row. Because `original` is the row exactly as the backend returned it, a version the backend needs travels with it: the [OData adapter](datalayer-odata.md#writing) sends the `@odata.etag` of `original` as `If-Match`, and a [SOAP or WCF service](datalayer-soap.md) can rebuild the whole record from `original` and the changed fields.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
const changes = tracker.changes();
|
|
88
|
+
// [
|
|
89
|
+
// { type: 'remove', key: 2, original: { id: 2, name: 'Oak bookshelf', … } },
|
|
90
|
+
// { type: 'update', key: 1, data: { price: 450 }, original: { id: 1, name: 'Walnut desk', price: 420, … } },
|
|
91
|
+
// { type: 'insert', data: { name: 'Pine stool', price: 49 } },
|
|
92
|
+
// ]
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`changes()` returns the unsaved changes in the order they would be saved, without sending them.
|
|
96
|
+
|
|
97
|
+
## Validating rows
|
|
98
|
+
|
|
99
|
+
`validateField(field, value, context)` is called as a value is entered (`setValue`) and again before the row is saved; it returns a message, or nothing when the value passes. `validateRow(row, context)` is called before the row is saved; it returns messages by field (`''` for the whole row), or one message for the whole row. For the same field, the message of `validateField` takes precedence. The checks receive the row itself, which keeps its values as they were read, and the entered values apart:
|
|
100
|
+
|
|
101
|
+
| Context field | Description |
|
|
102
|
+
| --- | --- |
|
|
103
|
+
| `row` | The row itself, which keeps its values as they were read until the row is saved. `validateField` receives it in the context; `validateRow` receives it as its first argument. |
|
|
104
|
+
| `values` | The entered values by field, not saved yet. |
|
|
105
|
+
| `isNew` | Indicates whether the row was added and never saved. |
|
|
106
|
+
| `changed` | The fields with entered values (`validateRow` only). |
|
|
107
|
+
| `getValue(field)` | The value the field would be saved with: the entered one when there is one, else the row's. |
|
|
108
|
+
|
|
109
|
+
A row with a message is not sent: `commit` returns `false`, and `saveChanges()` counts the row as failed. `validate(row)` runs the checks on demand and returns the first field with a message (`''` for the whole row), or `null` when the row passes.
|
|
110
|
+
|
|
111
|
+
`errorsOf(row)` returns the messages by field; the key `''` holds the message of the whole row. `errorOf(row, field)` returns one field's message and `messageOf(row)` the row's message, or the first field message. When a save fails, the messages come from the answer: the `fields` of the [`DataError`](datalayer-api.md) become the field messages and its text the row message. `setError(row, field, message)` places a message from outside, such as an editor that cannot read its text.
|
|
112
|
+
|
|
113
|
+
## The state of a row
|
|
114
|
+
|
|
115
|
+
`statusOf(row)` returns what the row shows. When several apply, the first of this list wins: `saving`, `failed`, `removed`, `invalid`, `new`, `changed`.
|
|
116
|
+
|
|
117
|
+
| Status | Meaning |
|
|
118
|
+
| --- | --- |
|
|
119
|
+
| `''` | Nothing to save: the tracker holds nothing for the row. |
|
|
120
|
+
| `'new'` | Added and never saved. |
|
|
121
|
+
| `'changed'` | Entered values wait to be saved. |
|
|
122
|
+
| `'removed'` | Marked for deletion (`'batch'`), or being deleted. |
|
|
123
|
+
| `'saving'` | Sent; the answer has not come yet. |
|
|
124
|
+
| `'failed'` | The last save failed. |
|
|
125
|
+
| `'invalid'` | A check left a message. |
|
|
126
|
+
|
|
127
|
+
`editOf(row)` returns everything the tracker holds for a row as a `RowEditState`, or `undefined` when it holds nothing; `editById(id)` looks it up by the id from `rowIdOf(row)`. The state is the tracker's own record and follows every change:
|
|
128
|
+
|
|
129
|
+
| Member | Description |
|
|
130
|
+
| --- | --- |
|
|
131
|
+
| `rowId` | The id edits are kept under: the key as text, or `+1`, `+2`, … for a new row. |
|
|
132
|
+
| `row` | The row object; for a new row, the one in `newRows`. |
|
|
133
|
+
| `isNew` | Added and never saved. |
|
|
134
|
+
| `removed` | Marked for deletion (`'batch'`), or being deleted. |
|
|
135
|
+
| `saving` | Sent; the answer has not come yet. |
|
|
136
|
+
| `failed` | The last save failed. |
|
|
137
|
+
| `draft` | The entered values by field. |
|
|
138
|
+
| `errors` | Messages by field; `''` holds the message of the whole row. |
|
|
139
|
+
|
|
140
|
+
`pendingRows()` lists the rows with something to save, in the order they would be saved; `failedRows()` the rows with a failed save or a message; `changes()` the changes themselves. `pendingCount`, `failedCount`, `savingCount` and `hasChanges` count the same without building lists. `discard(row)` drops the edits, marks and messages of a row (a new row is dropped altogether), and `discardChanges()` does it for every row; rows being saved stay.
|
|
141
|
+
|
|
142
|
+
## Events
|
|
143
|
+
|
|
144
|
+
`onChanged` occurs on every change the tracker makes, with a `reason` and the `rows` concerned:
|
|
145
|
+
|
|
146
|
+
| Reason | Meaning |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| `'edited'` | Values, marks or messages changed. |
|
|
149
|
+
| `'added'` | New rows were added. |
|
|
150
|
+
| `'dropped'` | New rows were taken away before they were saved. |
|
|
151
|
+
| `'saving'` | Rows were sent. |
|
|
152
|
+
| `'saved'` | The answer came and the rows were saved. |
|
|
153
|
+
| `'failed'` | The answer came and the rows were not saved. |
|
|
154
|
+
| `'released'` | Saved new rows left `newRows` because the source now has them. |
|
|
155
|
+
|
|
156
|
+
`onSaving` occurs when rows are about to be sent. `changes` holds what goes out and `rows` the row of each change. A listener may change a change's `data` to change what is sent, call `skip(row)` to keep one row back (its edits and marks stay, unsent), or set `cancel` to keep every row back; in `'wait'`, a row kept back makes `commit` resolve `false`. During the event an insert's `data` holds the values to save; after it the tracker writes them onto its row object and sends that object.
|
|
157
|
+
|
|
158
|
+
`onSaved` occurs when the answer came: `changes` and `rows` as they were sent, and one outcome per change, in the same order. An outcome is `{ change, ok: true, key, row? }`, or `{ change, ok: false, error }` with a [`DataError`](datalayer-api.md).
|
|
159
|
+
|
|
160
|
+
```tsx
|
|
161
|
+
import { data } from '@motifx-community/data';
|
|
162
|
+
|
|
163
|
+
/** Rows in memory that answer a little later, the way a server does; for the examples. */
|
|
164
|
+
export class SlowAdapter<T extends object, K> extends data.ArrayAdapter<T, K> {
|
|
165
|
+
constructor(
|
|
166
|
+
options: data.ArrayAdapterOptions<T>,
|
|
167
|
+
private readonly delayMs = 300,
|
|
168
|
+
) {
|
|
169
|
+
super(options);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override async load(options: data.LoadOptions = {}): Promise<data.LoadResult<T>> {
|
|
173
|
+
await new Promise((done) => setTimeout(done, this.delayMs));
|
|
174
|
+
return super.load(options);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The service above rejects a product name another product already has, with a message on the `name` field. Give a row the name of another and save it: the update goes out with only the changed field and `original`, the answer fails, and the field message from `DataError.fields` reaches `errorsOf(row)` and the editor. The log writes what `onSaving` sends and what `onSaved` reports. The service runs in the page.
|
|
180
|
+
|
|
181
|
+
## API reference
|
|
182
|
+
|
|
183
|
+
The options of `new data.ChangeTracker(options)`:
|
|
184
|
+
|
|
185
|
+
| Option | Default | Description |
|
|
186
|
+
| --- | --- | --- |
|
|
187
|
+
| `source` | — | The `DataSource` the changes are kept over and saved through. Its adapter must name a key; without one the constructor throws `MJD303`. |
|
|
188
|
+
| `saveMode` | `'wait'` | When rows are sent ([Save modes](#save-modes)). |
|
|
189
|
+
| `createRow` | — | The values a new row starts with. |
|
|
190
|
+
| `validateField` | — | Checks a value as it is entered and before the row is saved; returns a message, or nothing when the value passes ([Validation](#validation)). |
|
|
191
|
+
| `validateRow` | — | Checks a row before it is saved: messages by field (`''` for the whole row), or one message for the whole row. |
|
|
192
|
+
| `keepSavedNewRows` | `false` | Saved new rows stay in `newRows` until the source's `reload()` or `releaseSaved()`, even when the source has them, so a component can keep showing them where they were entered. |
|
|
193
|
+
| `saveOrder` | `['remove', 'update', 'insert']` | The order `saveChanges()` sends the changes in. The default sends deletions first, so unique values are free again. |
|
|
194
|
+
| `sameValue(a, b, field)` | — | Whether an entered value is the one the row has, so it is no change. By default empty values (`null`, `undefined`, `''`) are the same, dates compare by their moment, and everything else with `Object.is`. |
|
|
195
|
+
|
|
196
|
+
The properties of a tracker; none of them is reactive, so components follow `onChanged`:
|
|
197
|
+
|
|
198
|
+
| Member | Description |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| `source` | The `DataSource` the tracker was created over. |
|
|
201
|
+
| `saveMode` | The save mode the tracker was created with. |
|
|
202
|
+
| `currentRow` | The row the user is in, set by the editing component. A message in it does not count in `failedCount` yet, and `'background'` does not send it again while it is edited. |
|
|
203
|
+
| `newRows` | The rows added here: unsaved ones, and saved ones until the source has them among its rows or a new query starts (with `keepSavedNewRows`, until the source's `reload()`). |
|
|
204
|
+
| `pendingCount` | The number of rows with something to save. An untouched new row does not count. |
|
|
205
|
+
| `failedCount` | The number of rows with a failed save, plus the rows with a message other than `currentRow`. |
|
|
206
|
+
| `savingCount` | The number of rows being saved. |
|
|
207
|
+
| `hasChanges` | Indicates whether `pendingCount` is above zero. |
|
|
208
|
+
|
|
209
|
+
| Member | Returns | Description |
|
|
210
|
+
| --- | --- | --- |
|
|
211
|
+
| `getValue(row, field)` | `unknown` | The value to show: the entered one when there is one, else the row's. |
|
|
212
|
+
| `setValue(row, field, value)` | `void` | Records an entered value in the row's draft and checks the field at once. The value the row already has is no change. |
|
|
213
|
+
| `setError(row, field, message)` | `void` | Places a message on a field from outside, such as an editor that cannot read its text. |
|
|
214
|
+
| `isChanged(row, field?)` | `boolean` | Whether the field, or without a field the row, has something to save. |
|
|
215
|
+
| `commit(row)` | `boolean \| Promise<boolean>` | The user left the row: it is checked and sent as the save mode says. `false` when it has a message; in `'wait'`, a promise of whether it was saved. An untouched new row is dropped. |
|
|
216
|
+
| `add(values?, position?)` | `T` | Adds a new row at a position of `newRows` (default: the end) and returns it. `values` count as entered values. |
|
|
217
|
+
| `isUntouched(row)` | `boolean` | A new row nothing was entered in; leaving it (`commit`) drops it. |
|
|
218
|
+
| `remove(row)` | `Promise<boolean>` | Deletes a row as the save mode says: `'batch'` marks it, the other modes send it. A new row is dropped. |
|
|
219
|
+
| `removeRows(rows)` | `Promise<boolean>` | Deletes several rows in one save: one request when the adapter saves in batches, else one per row. Resolves to whether every row was deleted. |
|
|
220
|
+
| `removeKeys(keys)` | `Promise<boolean>` | `removeRows` by key; a key whose row is not found counts as not deleted. |
|
|
221
|
+
| `restore(row)` | `void` | Takes a deletion mark back. |
|
|
222
|
+
| `discard(row)` | `void` | Drops the edits, marks and messages of a row; a new row is dropped altogether. A row being saved stays. |
|
|
223
|
+
| `discardChanges()` | `void` | Drops every unsaved change; rows being saved stay. |
|
|
224
|
+
| `validate(row)` | `string \| null` | Checks the row now. Returns the first field with a message (`''`: the whole row), or `null` when it passes. |
|
|
225
|
+
| `statusOf(row)` | `RowStatus` | What the row shows ([The state of a row](#status)). |
|
|
226
|
+
| `errorsOf(row)` | `Record<string, string>` | The messages by field; `''` holds the message of the whole row. |
|
|
227
|
+
| `errorOf(row, field)` | `string` | One field's message, or `''`. |
|
|
228
|
+
| `messageOf(row)` | `string` | The message of the whole row, or the first field message. |
|
|
229
|
+
| `editOf(row)` | `RowEditState \| undefined` | Everything the tracker holds for the row, or `undefined` when it holds nothing. |
|
|
230
|
+
| `editById(rowId)` | `RowEditState \| undefined` | `editOf` by the id from `rowIdOf`. |
|
|
231
|
+
| `rowIdOf(row)` | `string` | The id edits are kept under: the key as text, or `+1`, `+2`, … for a new row. |
|
|
232
|
+
| `pendingRows()` | `T[]` | The rows with something to save, in the order they would be saved. |
|
|
233
|
+
| `failedRows()` | `T[]` | The rows with a failed save or a message. |
|
|
234
|
+
| `changes()` | `Change<T, K>[]` | The unsaved changes, in the order they would be saved, without sending them. |
|
|
235
|
+
| `saveChanges()` | `Promise<{ saved, failed }>` | Saves every unsaved change at once. Rows with a message are not sent and count as failed. |
|
|
236
|
+
| `releaseSaved()` | `void` | Takes saved new rows out of `newRows`, whether the source has them yet or not. |
|
|
237
|
+
| `dispose()` | `void` | Stops the tracker: the queue is dropped and the source subscription is removed. |
|
|
238
|
+
|
|
239
|
+
| Event | Arguments | Description |
|
|
240
|
+
| --- | --- | --- |
|
|
241
|
+
| `onChanged` | `{ reason, rows }` | Occurs on every change the tracker makes, with the reason and the rows concerned ([Events](#events)). |
|
|
242
|
+
| `onSaving` | `{ changes, rows, skip(row), cancel }` | Occurs when rows are about to be sent: change a change's `data`, keep a row back with `skip(row)`, or set `cancel` to keep every row back. |
|
|
243
|
+
| `onSaved` | `{ changes, rows, outcomes }` | Occurs when the answer to a save came, with one outcome per change, in the order they were sent. |
|
|
244
|
+
|
|
245
|
+
`RowStatus` and `RowEditState` are described under [The state of a row](#status), `SaveMode` under [Save modes](#save-modes), and the `Change` and `SaveOutcome` types of the adapter contract in the [API reference of the data layer](datalayer-api.md).
|
|
246
|
+
|
|
247
|
+
## Feedback
|
|
248
|
+
|
|
249
|
+
Loading issues…
|
|
250
|
+
|
|
251
|
+
## Example files
|
|
252
|
+
|
|
253
|
+
`BatchList.tsx`
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
import { reactive } from '@motifx/core';
|
|
257
|
+
import { data } from '@motifx-community/data';
|
|
258
|
+
import { Badge, Button, NumberBox, Stack, TextBox } from '@motifx-community/ui';
|
|
259
|
+
import { ProductService, type Product } from './products';
|
|
260
|
+
|
|
261
|
+
const TONES: Partial<Record<data.RowStatus, 'info' | 'accent' | 'caution' | 'standard' | 'danger'>> = {
|
|
262
|
+
new: 'info',
|
|
263
|
+
changed: 'accent',
|
|
264
|
+
removed: 'caution',
|
|
265
|
+
saving: 'standard',
|
|
266
|
+
failed: 'danger',
|
|
267
|
+
invalid: 'danger',
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** Products edited through a tracker and saved together; a price below zero keeps its row back. The service runs in the page. */
|
|
271
|
+
export default function BatchList() {
|
|
272
|
+
const s = reactive({ edits: 0, pending: 0, note: '' });
|
|
273
|
+
const source = new data.DataSource<Product, number>({ adapter: new ProductService(), loadMode: 'all' });
|
|
274
|
+
const tracker = new data.ChangeTracker<Product, number>({
|
|
275
|
+
source,
|
|
276
|
+
saveMode: 'batch',
|
|
277
|
+
createRow: () => ({ name: '', price: 0, stock: 0 }),
|
|
278
|
+
validateField: (field, value) => (field === 'price' && Number(value) < 0 ? 'The price cannot be below zero.' : null),
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// The tracker is not reactive: `onChanged` bumps `s.edits`, which every binding below reads.
|
|
282
|
+
const refresh = () => {
|
|
283
|
+
s.edits++;
|
|
284
|
+
s.pending = tracker.pendingCount;
|
|
285
|
+
};
|
|
286
|
+
source.onChanged(refresh);
|
|
287
|
+
tracker.onChanged(refresh);
|
|
288
|
+
void source.load();
|
|
289
|
+
|
|
290
|
+
// The rows to show: the loaded ones, then the unsaved new ones. The row objects stay as the
|
|
291
|
+
// data layer gave them, so the tracker finds its records by them.
|
|
292
|
+
const rowsNow = () => {
|
|
293
|
+
void s.edits;
|
|
294
|
+
return [...source.rows, ...tracker.newRows.filter((row) => !source.rows.includes(row))];
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
function live<V>(read: () => V): () => V {
|
|
298
|
+
return () => {
|
|
299
|
+
void s.edits;
|
|
300
|
+
return read();
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const save = () => void tracker.saveChanges().then((r) => (s.note = `${r.saved} saved, ${r.failed} failed`));
|
|
305
|
+
|
|
306
|
+
const Row = (props: { row: Product }) => {
|
|
307
|
+
const row = props.row;
|
|
308
|
+
return (
|
|
309
|
+
<Stack orientation="horizontal" align="end" wrap>
|
|
310
|
+
<TextBox
|
|
311
|
+
label="Product"
|
|
312
|
+
value={live(() => tracker.getValue(row, 'name') as string)}
|
|
313
|
+
error={live(() => tracker.errorOf(row, 'name') || null)}
|
|
314
|
+
onValueChanged={(e) => e.byUser && tracker.setValue(row, 'name', e.value)}
|
|
315
|
+
/>
|
|
316
|
+
<NumberBox
|
|
317
|
+
label="Price"
|
|
318
|
+
style="inline-size: 8rem"
|
|
319
|
+
value={live(() => tracker.getValue(row, 'price') as number | null)}
|
|
320
|
+
error={live(() => tracker.errorOf(row, 'price') || null)}
|
|
321
|
+
onValueChanged={(e) => e.byUser && tracker.setValue(row, 'price', e.value)}
|
|
322
|
+
/>
|
|
323
|
+
<Button
|
|
324
|
+
text={live(() => (tracker.statusOf(row) === 'removed' ? 'Restore' : 'Delete'))}
|
|
325
|
+
size="sm"
|
|
326
|
+
variant="ghost"
|
|
327
|
+
onclick={() => (tracker.statusOf(row) === 'removed' ? tracker.restore(row) : void tracker.remove(row))}
|
|
328
|
+
/>
|
|
329
|
+
<Badge text={live(() => tracker.statusOf(row))} tone={live(() => TONES[tracker.statusOf(row)] ?? 'standard')} visible={live(() => tracker.statusOf(row) !== '')} />
|
|
330
|
+
</Stack>
|
|
331
|
+
);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
return (
|
|
335
|
+
<>
|
|
336
|
+
{rowsNow().map((row) => (
|
|
337
|
+
<Row key={tracker.rowIdOf(row)} row={row} />
|
|
338
|
+
))}
|
|
339
|
+
<Stack orientation="horizontal" align="center" wrap>
|
|
340
|
+
<Button text="Add" variant="outline" onclick={() => void tracker.add()} />
|
|
341
|
+
<Button text="Save changes" tone="accent" enabled={() => s.pending > 0} onclick={save} />
|
|
342
|
+
<Button text="Discard" variant="ghost" enabled={() => s.pending > 0} onclick={() => tracker.discardChanges()} />
|
|
343
|
+
<output class="dl-changes-note">{() => `${s.pending} unsaved${s.note ? ` · ${s.note}` : ''}`}</output>
|
|
344
|
+
</Stack>
|
|
345
|
+
</>
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
`products.ts`
|
|
351
|
+
|
|
352
|
+
```tsx
|
|
353
|
+
import { data } from '@motifx-community/data';
|
|
354
|
+
import { SlowAdapter } from '../../../../shared/slow-adapter';
|
|
355
|
+
|
|
356
|
+
export interface Product {
|
|
357
|
+
id: number;
|
|
358
|
+
name: string;
|
|
359
|
+
price: number;
|
|
360
|
+
stock: number;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** A few products, fresh objects every time, so each example edits its own. */
|
|
364
|
+
export function makeProducts(): Product[] {
|
|
365
|
+
return [
|
|
366
|
+
{ id: 1, name: 'Walnut desk', price: 420, stock: 8 },
|
|
367
|
+
{ id: 2, name: 'Oak bookshelf', price: 260, stock: 14 },
|
|
368
|
+
{ id: 3, name: 'Beech chair', price: 95, stock: 40 },
|
|
369
|
+
{ id: 4, name: 'Pine side table', price: 70, stock: 22 },
|
|
370
|
+
{ id: 5, name: 'Cherry cabinet', price: 540, stock: 5 },
|
|
371
|
+
];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Products in memory that answer like a server: writes take a moment, and a name another product already has is rejected with a field message. */
|
|
375
|
+
export class ProductService extends SlowAdapter<Product, number> {
|
|
376
|
+
constructor() {
|
|
377
|
+
super({ data: makeProducts(), key: 'id', createKey: (_values, rows) => Math.max(0, ...rows.map((r) => r.id)) + 1 });
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
protected override insertRow(values: Partial<Product>): Promise<Partial<Product>> {
|
|
381
|
+
return this.write(values.name, null, () => super.insertRow(values));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
protected override updateRow(key: number, changes: Partial<Product>): Promise<Partial<Product>> {
|
|
385
|
+
return this.write(changes.name, key, () => super.updateRow(key, changes));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
protected override removeRow(key: number): Promise<void> {
|
|
389
|
+
return this.write(undefined, key, () => super.removeRow(key));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private async write<R>(name: string | undefined, key: number | null, save: () => Promise<R>): Promise<R> {
|
|
393
|
+
await new Promise((done) => setTimeout(done, 600));
|
|
394
|
+
const taken = name !== undefined && this.rows.some((row) => row.id !== key && row.name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
395
|
+
if (taken) {
|
|
396
|
+
const message = 'Another product already has this name.';
|
|
397
|
+
throw data.DataError.create('MJD202', { operation: 'save', fields: { name: message } }, 'The service rejected the row');
|
|
398
|
+
}
|
|
399
|
+
return save();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
`slow-adapter.ts`
|
|
405
|
+
|
|
406
|
+
```tsx
|
|
407
|
+
import { data } from '@motifx-community/data';
|
|
408
|
+
|
|
409
|
+
/** Rows in memory that answer a little later, the way a server does; for the examples. */
|
|
410
|
+
export class SlowAdapter<T extends object, K> extends data.ArrayAdapter<T, K> {
|
|
411
|
+
constructor(
|
|
412
|
+
options: data.ArrayAdapterOptions<T>,
|
|
413
|
+
private readonly delayMs = 300,
|
|
414
|
+
) {
|
|
415
|
+
super(options);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
override async load(options: data.LoadOptions = {}): Promise<data.LoadResult<T>> {
|
|
419
|
+
await new Promise((done) => setTimeout(done, this.delayMs));
|
|
420
|
+
return super.load(options);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
`RejectedSave.tsx`
|
|
426
|
+
|
|
427
|
+
```tsx
|
|
428
|
+
import { reactive } from '@motifx/core';
|
|
429
|
+
import { data } from '@motifx-community/data';
|
|
430
|
+
import { Button, Stack, TextBox } from '@motifx-community/ui';
|
|
431
|
+
import { ProductService, type Product } from './products';
|
|
432
|
+
|
|
433
|
+
/** A save the service turns down: `onSaving` logs what goes out, `onSaved` each outcome, and the field message reaches the editor. The service runs in the page. */
|
|
434
|
+
export default function RejectedSave() {
|
|
435
|
+
const s = reactive({ edits: 0, log: [] as { id: number; line: string }[] });
|
|
436
|
+
const source = new data.DataSource<Product, number>({ adapter: new ProductService(), loadMode: 'all' });
|
|
437
|
+
const tracker = new data.ChangeTracker<Product, number>({ source, saveMode: 'wait' });
|
|
438
|
+
|
|
439
|
+
source.onChanged(() => s.edits++);
|
|
440
|
+
tracker.onChanged(() => s.edits++);
|
|
441
|
+
void source.load();
|
|
442
|
+
|
|
443
|
+
// The row objects stay as the data layer gave them, so the tracker finds its records by them.
|
|
444
|
+
const rowsNow = () => {
|
|
445
|
+
void s.edits;
|
|
446
|
+
return [...source.rows];
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
let lineId = 0;
|
|
450
|
+
const log = (line: string) => {
|
|
451
|
+
s.log.push({ id: ++lineId, line });
|
|
452
|
+
if (s.log.length > 6) s.log.shift();
|
|
453
|
+
};
|
|
454
|
+
tracker.onSaving((e) => {
|
|
455
|
+
for (const change of e.changes) {
|
|
456
|
+
if (change.type === 'update') log(`sent: update key ${change.key} ${JSON.stringify(change.data)} (original name "${change.original?.name}")`);
|
|
457
|
+
else log(`sent: ${change.type}`);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
tracker.onSaved((e) => {
|
|
461
|
+
for (const outcome of e.outcomes) log(outcome.ok ? 'answer: saved' : `answer: failed, ${outcome.error.plainMessage}`);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
function live<V>(read: () => V): () => V {
|
|
465
|
+
return () => {
|
|
466
|
+
void s.edits;
|
|
467
|
+
return read();
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const Row = (props: { row: Product }) => {
|
|
472
|
+
const row = props.row;
|
|
473
|
+
return (
|
|
474
|
+
<Stack orientation="horizontal" align="end" wrap>
|
|
475
|
+
<TextBox
|
|
476
|
+
label="Product"
|
|
477
|
+
value={live(() => tracker.getValue(row, 'name') as string)}
|
|
478
|
+
error={live(() => tracker.errorOf(row, 'name') || null)}
|
|
479
|
+
onValueChanged={(e) => e.byUser && tracker.setValue(row, 'name', e.value)}
|
|
480
|
+
/>
|
|
481
|
+
<Button text="Save" size="sm" variant="outline" enabled={live(() => tracker.isChanged(row))} onclick={() => void tracker.commit(row)} />
|
|
482
|
+
</Stack>
|
|
483
|
+
);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
return (
|
|
487
|
+
<>
|
|
488
|
+
{rowsNow().map((row) => (
|
|
489
|
+
<Row key={tracker.rowIdOf(row)} row={row} />
|
|
490
|
+
))}
|
|
491
|
+
<ul class="dl-changes-log" aria-label="Requests and answers">
|
|
492
|
+
{s.log.map((entry) => (
|
|
493
|
+
<li key={entry.id}>
|
|
494
|
+
<code>{entry.line}</code>
|
|
495
|
+
</li>
|
|
496
|
+
))}
|
|
497
|
+
</ul>
|
|
498
|
+
</>
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
```
|