@dolusoft/query-table 3.17.0 → 3.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-table.js +283 -0
- package/dist/data-table.js.map +1 -0
- package/dist/local-DDJEp4Nn.js +88 -0
- package/dist/local-DDJEp4Nn.js.map +1 -0
- package/dist/local.js +1 -87
- package/dist/query-table.js +1 -4782
- package/dist/src-BTIXBmwM.js +4928 -0
- package/dist/src-BTIXBmwM.js.map +1 -0
- package/dist/types/contract.d.ts +49 -1
- package/dist/types/data-table/contract.d.ts +64 -0
- package/dist/types/data-table/data-table.vue.d.ts +31 -0
- package/dist/types/data-table/derive-dataset.d.ts +32 -0
- package/dist/types/data-table/index.d.ts +4 -0
- package/dist/types/query-table.vue.d.ts +2 -1
- package/dist/types/use-query-table.d.ts +21 -3
- package/dist/types/virtual/use-row-window.d.ts +4 -0
- package/package.json +7 -3
- package/dist/local.js.map +0 -1
- package/dist/query-table.js.map +0 -1
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { r as query_table_default } from "./src-BTIXBmwM.js";
|
|
2
|
+
import { t as useLocalQuery } from "./local-DDJEp4Nn.js";
|
|
3
|
+
import { camelize, computed, createBlock, createSlots, defineComponent, getCurrentInstance, guardReactiveProps, mergeProps, normalizeProps, openBlock, renderList, renderSlot, shallowRef, unref, useSlots, useTemplateRef, watch, withCtx } from "vue";
|
|
4
|
+
import { columnTypeOf, sameQuery } from "@dolusoft/query-protocol";
|
|
5
|
+
import { defineDataset } from "@dolusoft/query-protocol/local";
|
|
6
|
+
//#region src/data-table/derive-dataset.ts
|
|
7
|
+
/**
|
|
8
|
+
* The spec of the columns: one field per column `field` (the first column of
|
|
9
|
+
* a name wins), hidden ones included, `ip` columns left out (C-118).
|
|
10
|
+
*/
|
|
11
|
+
var datasetSpecOf = (source) => {
|
|
12
|
+
const fields = [];
|
|
13
|
+
const ip = [];
|
|
14
|
+
const seen = /* @__PURE__ */ new Set();
|
|
15
|
+
for (const column of source.columns) {
|
|
16
|
+
if (seen.has(column.field)) continue;
|
|
17
|
+
seen.add(column.field);
|
|
18
|
+
const type = columnTypeOf(column);
|
|
19
|
+
if (type === "ip") {
|
|
20
|
+
ip.push(column.field);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
fields.push({
|
|
24
|
+
name: column.field,
|
|
25
|
+
type,
|
|
26
|
+
sortable: source.sortable && column.sortable !== false,
|
|
27
|
+
filterable: source.filterable && column.filterable !== false
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const byName = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
31
|
+
fields.sort((a, b) => byName(a.name, b.name));
|
|
32
|
+
return {
|
|
33
|
+
key: source.rowKey,
|
|
34
|
+
search: [...new Set(source.search ?? [])].sort(byName),
|
|
35
|
+
fields,
|
|
36
|
+
ip: ip.sort(byName)
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
/** Equal specs build the same dataset. */
|
|
40
|
+
var sameSpec = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
41
|
+
/** The dataset of a spec. Throws a `TypeError` for a malformed one (C-126). */
|
|
42
|
+
var datasetOf = (spec) => {
|
|
43
|
+
if (spec.key.includes(".")) throw new TypeError(`DataTable: the rowKey "${spec.key}" is not a top-level field`);
|
|
44
|
+
const fields = Object.create(null);
|
|
45
|
+
for (const field of spec.fields) fields[field.name] = {
|
|
46
|
+
type: field.type,
|
|
47
|
+
sortable: field.sortable,
|
|
48
|
+
filterable: field.filterable
|
|
49
|
+
};
|
|
50
|
+
for (const name of spec.search) {
|
|
51
|
+
if (!Object.prototype.hasOwnProperty.call(fields, name)) throw new TypeError(spec.ip.includes(name) ? `DataTable: the search field "${name}" is an ip column; only string columns are searched` : `DataTable: the search field "${name}" is not a column`);
|
|
52
|
+
fields[name] = {
|
|
53
|
+
...fields[name],
|
|
54
|
+
search: true
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return defineDataset({
|
|
58
|
+
key: spec.key,
|
|
59
|
+
fields
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/data-table/data-table.vue
|
|
64
|
+
var data_table_default = /* @__PURE__ */ defineComponent({
|
|
65
|
+
name: "DataTable",
|
|
66
|
+
inheritAttrs: false,
|
|
67
|
+
__name: "data-table",
|
|
68
|
+
props: {
|
|
69
|
+
query: { default: void 0 },
|
|
70
|
+
rows: {},
|
|
71
|
+
rowKey: {},
|
|
72
|
+
profile: { default: "tr-2" },
|
|
73
|
+
search: { default: void 0 },
|
|
74
|
+
paginate: {
|
|
75
|
+
type: Boolean,
|
|
76
|
+
default: true
|
|
77
|
+
},
|
|
78
|
+
dataset: { default: void 0 },
|
|
79
|
+
selection: {},
|
|
80
|
+
rowPinning: {},
|
|
81
|
+
searchDebounce: {},
|
|
82
|
+
columns: {},
|
|
83
|
+
footerRows: {},
|
|
84
|
+
loading: { type: Boolean },
|
|
85
|
+
sortable: { type: Boolean },
|
|
86
|
+
filterable: { type: Boolean },
|
|
87
|
+
resizable: { type: Boolean },
|
|
88
|
+
autoWidth: { type: Boolean },
|
|
89
|
+
autoWidthMax: {},
|
|
90
|
+
reorderable: { type: Boolean },
|
|
91
|
+
filterDebounce: {},
|
|
92
|
+
pagination: {},
|
|
93
|
+
hasSubtable: { type: Boolean },
|
|
94
|
+
rowExpandable: {},
|
|
95
|
+
expandMode: {},
|
|
96
|
+
rowKind: {},
|
|
97
|
+
groupBy: {},
|
|
98
|
+
hasRightPanel: { type: Boolean },
|
|
99
|
+
labels: {},
|
|
100
|
+
virtual: { type: [Boolean, Object] },
|
|
101
|
+
rowNavigation: { type: [Boolean, Object] },
|
|
102
|
+
scrollBody: { type: Boolean },
|
|
103
|
+
header: { type: Boolean },
|
|
104
|
+
flash: { type: [Boolean, Object] }
|
|
105
|
+
},
|
|
106
|
+
emits: [
|
|
107
|
+
"update:query",
|
|
108
|
+
"update:selection",
|
|
109
|
+
"rowRightPanelClick",
|
|
110
|
+
"cellContextMenu",
|
|
111
|
+
"headerContextMenu",
|
|
112
|
+
"columnResize",
|
|
113
|
+
"update:columns",
|
|
114
|
+
"update:rowPinning",
|
|
115
|
+
"rowFocus",
|
|
116
|
+
"rowExpansionChange"
|
|
117
|
+
],
|
|
118
|
+
setup(__props, { expose: __expose, emit: __emit }) {
|
|
119
|
+
const props = __props;
|
|
120
|
+
const emit = __emit;
|
|
121
|
+
const slots = useSlots();
|
|
122
|
+
const start = computed((previous) => {
|
|
123
|
+
const pageSize = props.pagination?.pageSizeOptions?.[0] ?? 10;
|
|
124
|
+
return previous?.pageSize === pageSize ? previous : {
|
|
125
|
+
page: 1,
|
|
126
|
+
pageSize,
|
|
127
|
+
sort: null,
|
|
128
|
+
filters: []
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
const held = shallowRef(null);
|
|
132
|
+
watch(() => props.query !== void 0, (given) => {
|
|
133
|
+
if (given) held.value = null;
|
|
134
|
+
});
|
|
135
|
+
const query = computed(() => props.query ?? held.value ?? start.value);
|
|
136
|
+
const onQuery = (next, reason) => {
|
|
137
|
+
if (props.query === void 0) held.value = next;
|
|
138
|
+
emit("update:query", next, reason);
|
|
139
|
+
};
|
|
140
|
+
const spec = computed((previous) => {
|
|
141
|
+
const next = datasetSpecOf({
|
|
142
|
+
rowKey: props.rowKey,
|
|
143
|
+
columns: props.columns,
|
|
144
|
+
search: props.search,
|
|
145
|
+
sortable: props.sortable,
|
|
146
|
+
filterable: props.filterable
|
|
147
|
+
});
|
|
148
|
+
return previous && sameSpec(previous, next) ? previous : next;
|
|
149
|
+
});
|
|
150
|
+
const derived = computed(() => datasetOf(spec.value));
|
|
151
|
+
const dataset = computed(() => props.dataset ?? derived.value);
|
|
152
|
+
const local = useLocalQuery({
|
|
153
|
+
allRows: () => props.rows,
|
|
154
|
+
query,
|
|
155
|
+
dataset,
|
|
156
|
+
get profile() {
|
|
157
|
+
return props.profile;
|
|
158
|
+
},
|
|
159
|
+
paginate: () => props.paginate
|
|
160
|
+
});
|
|
161
|
+
const drawn = computed((previous) => {
|
|
162
|
+
const context = {
|
|
163
|
+
query: query.value,
|
|
164
|
+
dataset: dataset.value,
|
|
165
|
+
profile: props.profile,
|
|
166
|
+
paginate: props.paginate
|
|
167
|
+
};
|
|
168
|
+
const before = previous?.context;
|
|
169
|
+
const same = before !== void 0 && sameQuery(before.query, context.query) && before.dataset === context.dataset && before.profile === context.profile && before.paginate === context.paginate;
|
|
170
|
+
return {
|
|
171
|
+
rows: local.rows.value,
|
|
172
|
+
hint: same ? "live" : "snapshot",
|
|
173
|
+
context
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
const isIp = (column) => columnTypeOf(column) === "ip";
|
|
177
|
+
const closed = (column) => isIp(column) && (column.sortable !== false || column.filterable !== false);
|
|
178
|
+
const columns = computed(() => props.columns.some(closed) ? props.columns.map((column) => closed(column) ? {
|
|
179
|
+
...column,
|
|
180
|
+
sortable: false,
|
|
181
|
+
filterable: false
|
|
182
|
+
} : column) : props.columns);
|
|
183
|
+
const onColumns = (next, reason) => {
|
|
184
|
+
const drawnColumns = columns.value;
|
|
185
|
+
const originals = new Map(props.columns.filter(closed).map((column) => [column.field, column]));
|
|
186
|
+
const restored = next.map((column) => {
|
|
187
|
+
const original = originals.get(column.field);
|
|
188
|
+
if (!original || !isIp(column)) return column;
|
|
189
|
+
if (drawnColumns.includes(column)) return original;
|
|
190
|
+
const copy = { ...column };
|
|
191
|
+
for (const key of ["sortable", "filterable"]) if (key in original) copy[key] = original[key];
|
|
192
|
+
else delete copy[key];
|
|
193
|
+
return copy;
|
|
194
|
+
});
|
|
195
|
+
emit("update:columns", restored, reason);
|
|
196
|
+
};
|
|
197
|
+
const instance = getCurrentInstance();
|
|
198
|
+
const own = /* @__PURE__ */ new Set([
|
|
199
|
+
"columns",
|
|
200
|
+
"query",
|
|
201
|
+
"rows",
|
|
202
|
+
"profile",
|
|
203
|
+
"search",
|
|
204
|
+
"paginate",
|
|
205
|
+
"dataset",
|
|
206
|
+
"totalRows",
|
|
207
|
+
"cursors",
|
|
208
|
+
"infinite",
|
|
209
|
+
"rowsUpdate"
|
|
210
|
+
]);
|
|
211
|
+
const reserved = /* @__PURE__ */ new Set([
|
|
212
|
+
"key",
|
|
213
|
+
"ref",
|
|
214
|
+
"ref_for",
|
|
215
|
+
"ref_key"
|
|
216
|
+
]);
|
|
217
|
+
const passed = () => {
|
|
218
|
+
const raw = instance.vnode.props ?? {};
|
|
219
|
+
const declared = props;
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const key of Object.keys(raw)) {
|
|
222
|
+
if (reserved.has(key) || key.startsWith("onVnode")) continue;
|
|
223
|
+
if (/^on[A-Z]/.test(key)) {
|
|
224
|
+
if (!/^onUpdate:(query|columns)/.test(key)) out[key] = raw[key];
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const name = camelize(key);
|
|
228
|
+
if (own.has(name)) continue;
|
|
229
|
+
if (name in declared) out[name] = declared[name];
|
|
230
|
+
else out[key] = raw[key];
|
|
231
|
+
}
|
|
232
|
+
return out;
|
|
233
|
+
};
|
|
234
|
+
const showError = () => local.error.value !== null && slots.error !== void 0;
|
|
235
|
+
const slotNames = () => Object.keys(slots).filter((name) => name !== "error" && !(name === "pagination" && !props.paginate) && !(name === "empty" && showError()));
|
|
236
|
+
const table = useTemplateRef("table");
|
|
237
|
+
__expose({
|
|
238
|
+
collapseAll: () => table.value?.collapseAll(),
|
|
239
|
+
expandAll: () => table.value?.expandAll(),
|
|
240
|
+
focusFilter: (field) => table.value?.focusFilter(field) ?? false,
|
|
241
|
+
flushPendingFilters: () => table.value?.flushPendingFilters(),
|
|
242
|
+
scrollToIndex: (index, options) => table.value?.scrollToIndex(index, options),
|
|
243
|
+
loadMore: () => table.value?.loadMore(),
|
|
244
|
+
measureColumnWidths: (fields) => table.value?.measureColumnWidths(fields) ?? {}
|
|
245
|
+
});
|
|
246
|
+
return (_ctx, _cache) => {
|
|
247
|
+
return openBlock(), createBlock(unref(query_table_default), mergeProps({
|
|
248
|
+
ref_key: "table",
|
|
249
|
+
ref: table
|
|
250
|
+
}, passed(), {
|
|
251
|
+
columns: columns.value,
|
|
252
|
+
query: query.value,
|
|
253
|
+
rows: drawn.value.rows,
|
|
254
|
+
"total-rows": unref(local).totalRows.value,
|
|
255
|
+
"rows-update": drawn.value.hint,
|
|
256
|
+
"onUpdate:query": onQuery,
|
|
257
|
+
"onUpdate:columns": onColumns
|
|
258
|
+
}), createSlots({ _: 2 }, [renderList(slotNames(), (name) => {
|
|
259
|
+
return {
|
|
260
|
+
name,
|
|
261
|
+
fn: withCtx((scope) => [renderSlot(_ctx.$slots, name, normalizeProps(guardReactiveProps(scope ?? {})))])
|
|
262
|
+
};
|
|
263
|
+
}), showError() ? {
|
|
264
|
+
name: "empty",
|
|
265
|
+
fn: withCtx(() => [renderSlot(_ctx.$slots, "error", { error: unref(local).error.value })]),
|
|
266
|
+
key: "0"
|
|
267
|
+
} : void 0]), 1040, [
|
|
268
|
+
"columns",
|
|
269
|
+
"query",
|
|
270
|
+
"rows",
|
|
271
|
+
"total-rows",
|
|
272
|
+
"rows-update"
|
|
273
|
+
]);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/data-table/index.ts
|
|
279
|
+
var data_table_default$1 = data_table_default;
|
|
280
|
+
//#endregion
|
|
281
|
+
export { data_table_default as DataTable, data_table_default$1 as default };
|
|
282
|
+
|
|
283
|
+
//# sourceMappingURL=data-table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-table.js","names":["_useSlots","_openBlock","_createBlock","_unref","QueryTable","_mergeProps","_createSlots","_renderList","_withCtx","_renderSlot","_normalizeProps","_guardReactiveProps","_sfc_main","DataTable"],"sources":["../src/data-table/derive-dataset.ts","../src/data-table/data-table.vue","../src/data-table/data-table.vue","../src/data-table/index.ts"],"sourcesContent":["import { columnTypeOf, type ColumnType } from '@dolusoft/query-protocol'\nimport {\n defineDataset,\n type Dataset,\n type DatasetField\n} from '@dolusoft/query-protocol/local'\n\nimport type { Column } from '../contract'\n\n/** What the dataset of a `DataTable` is built from, and nothing else (C-126). */\nexport interface DatasetSpec {\n key: string\n search: string[]\n fields: Array<{\n name: string\n type: ColumnType\n sortable: boolean\n filterable: boolean\n }>\n /** The `ip` columns, left out of the fields (C-118). */\n ip: string[]\n}\n\nexport interface DatasetSource {\n rowKey: string\n columns: readonly Column[]\n search: readonly string[] | undefined\n sortable: boolean\n filterable: boolean\n}\n\n/**\n * The spec of the columns: one field per column `field` (the first column of\n * a name wins), hidden ones included, `ip` columns left out (C-118).\n */\nexport const datasetSpecOf = (source: DatasetSource): DatasetSpec => {\n const fields: DatasetSpec['fields'] = []\n const ip: string[] = []\n const seen = new Set<string>()\n for (const column of source.columns) {\n if (seen.has(column.field)) {\n continue\n }\n seen.add(column.field)\n const type = columnTypeOf(column)\n if (type === 'ip') {\n ip.push(column.field)\n continue\n }\n fields.push({\n name: column.field,\n type,\n sortable: source.sortable && column.sortable !== false,\n filterable: source.filterable && column.filterable !== false\n })\n }\n // The order of the fields and of the search means nothing to the\n // evaluator: sorted, a reordered column list gives an equal spec.\n const byName = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0)\n fields.sort((a, b) => byName(a.name, b.name))\n return {\n key: source.rowKey,\n search: [...new Set(source.search ?? [])].sort(byName),\n fields,\n ip: ip.sort(byName)\n }\n}\n\n/** Equal specs build the same dataset. */\nexport const sameSpec = (a: DatasetSpec, b: DatasetSpec): boolean =>\n JSON.stringify(a) === JSON.stringify(b)\n\n/** The dataset of a spec. Throws a `TypeError` for a malformed one (C-126). */\nexport const datasetOf = <T>(spec: DatasetSpec): Dataset<T> => {\n // QueryTable reads `row[rowKey]` (C-26) while the evaluator reads a dotted\n // path: only a top-level name means the same field to both.\n if (spec.key.includes('.')) {\n throw new TypeError(\n `DataTable: the rowKey \"${spec.key}\" is not a top-level field`\n )\n }\n // No prototype: a field named `__proto__` or `toString` stays a field.\n const fields = Object.create(null) as Record<string, DatasetField<T>>\n for (const field of spec.fields) {\n fields[field.name] = {\n type: field.type,\n sortable: field.sortable,\n filterable: field.filterable\n }\n }\n for (const name of spec.search) {\n if (!Object.prototype.hasOwnProperty.call(fields, name)) {\n throw new TypeError(\n spec.ip.includes(name)\n ? `DataTable: the search field \"${name}\" is an ip column; only string columns are searched`\n : `DataTable: the search field \"${name}\" is not a column`\n )\n }\n fields[name] = { ...fields[name], search: true }\n }\n return defineDataset<T>({ key: spec.key, fields })\n}\n","<script setup lang=\"ts\" generic=\"T extends object\">\nimport { columnTypeOf, sameQuery } from '@dolusoft/query-protocol'\nimport type { Dataset, SemanticsProfile } from '@dolusoft/query-protocol/local'\nimport {\n camelize,\n computed,\n getCurrentInstance,\n shallowRef,\n useTemplateRef,\n watch\n} from 'vue'\n\nimport type { DataTableEmits, DataTableProps, DataTableSlots } from './contract'\nimport {\n datasetOf,\n datasetSpecOf,\n sameSpec,\n type DatasetSpec\n} from './derive-dataset'\nimport type {\n Column,\n ColumnChangeReason,\n QueryChangeReason,\n QueryTableExpose,\n RowsUpdate,\n TableQuery\n} from '../contract'\nimport { QueryTable } from '../index'\nimport { useLocalQuery } from '../local'\n\n// `useLocalQuery` and `QueryTable` side by side (ADR 0021, K319-1): no\n// markup, no table state and no table behavior of its own.\ndefineOptions({ name: 'DataTable', inheritAttrs: false })\n\nconst props = withDefaults(defineProps<DataTableProps<T>>(), {\n query: undefined,\n profile: 'tr-2',\n search: undefined,\n paginate: true,\n dataset: undefined\n})\n\nconst emit = defineEmits<DataTableEmits<T>>()\n\nconst slots = defineSlots<DataTableSlots<T>>()\n\n// The query (C-125): the consumer's when given, else held here. Until the\n// first update the held query is the start query, which follows\n// `pageSizeOptions`; while the consumer gives one, the held query is dropped.\nconst start = computed<TableQuery>(previous => {\n const pageSize = props.pagination?.pageSizeOptions?.[0] ?? 10\n return previous?.pageSize === pageSize\n ? previous\n : { page: 1, pageSize, sort: null, filters: [] }\n})\nconst held = shallowRef<TableQuery | null>(null)\nwatch(\n () => props.query !== undefined,\n given => {\n if (given) {\n held.value = null\n }\n }\n)\nconst query = computed(() => props.query ?? held.value ?? start.value)\nconst onQuery = (next: TableQuery, reason: QueryChangeReason) => {\n if (props.query === undefined) {\n held.value = next\n }\n emit('update:query', next, reason)\n}\n\n// The dataset (C-126): built again only when what it is built from changes,\n// compared by value.\nconst spec = computed<DatasetSpec>(previous => {\n const next = datasetSpecOf({\n rowKey: props.rowKey,\n columns: props.columns,\n search: props.search,\n sortable: props.sortable,\n filterable: props.filterable\n })\n return previous && sameSpec(previous, next) ? previous : next\n})\nconst derived = computed(() => datasetOf<T>(spec.value))\nconst dataset = computed(() => props.dataset ?? derived.value)\n\nconst local = useLocalQuery<T>({\n allRows: () => props.rows,\n query,\n dataset,\n // Read on every evaluation, so a new profile evaluates again.\n get profile() {\n return props.profile\n },\n paginate: () => props.paginate\n})\n\n// The flash hint (C-124): a new context is a snapshot; a new page in the\n// same context comes from the source. The query is compared by value, so a\n// new but equal query object is the same context.\ninterface Context {\n query: TableQuery\n dataset: Dataset<T>\n profile: SemanticsProfile\n paginate: boolean\n}\nconst drawn = computed<{ rows: T[]; hint: RowsUpdate; context: Context }>(\n previous => {\n const context: Context = {\n query: query.value,\n dataset: dataset.value,\n profile: props.profile,\n paginate: props.paginate\n }\n const before = previous?.context\n const same =\n before !== undefined &&\n sameQuery(before.query, context.query) &&\n before.dataset === context.dataset &&\n before.profile === context.profile &&\n before.paginate === context.paginate\n return { rows: local.rows.value, hint: same ? 'live' : 'snapshot', context }\n }\n)\n\n// An `ip` column is not a field (C-118, C-126), so the table offers no sort\n// and no filter on it. `update:columns` gives the consumer's own `sortable`\n// and `filterable` back, so the change never reaches the consumer's columns.\nconst isIp = (column: Column) => columnTypeOf(column) === 'ip'\nconst closed = (column: Column) =>\n isIp(column) && (column.sortable !== false || column.filterable !== false)\nconst columns = computed(() =>\n props.columns.some(closed)\n ? props.columns.map(column =>\n closed(column)\n ? { ...column, sortable: false, filterable: false }\n : column\n )\n : props.columns\n)\nconst onColumns = (next: Column[], reason: ColumnChangeReason) => {\n const drawnColumns = columns.value\n const originals = new Map(\n props.columns.filter(closed).map(column => [column.field, column])\n )\n const restored = next.map(column => {\n const original = originals.get(column.field)\n if (!original || !isIp(column)) {\n return column\n }\n if (drawnColumns.includes(column)) {\n return original\n }\n const copy: Column = { ...column }\n for (const key of ['sortable', 'filterable'] as const) {\n if (key in original) {\n copy[key] = original[key]\n } else {\n delete copy[key]\n }\n }\n return copy\n })\n emit('update:columns', restored, reason)\n}\n\n// What the consumer gave reaches QueryTable as given; what it left out is\n// left out, so QueryTable applies its own default (C-124). A declared prop\n// is passed as Vue read it (`sortable=\"\"` is `true`); any other attribute\n// keeps its name (`data-testid`, `aria-label`).\nconst instance = getCurrentInstance()!\nconst own = new Set([\n 'columns',\n 'query',\n 'rows',\n 'profile',\n 'search',\n 'paginate',\n 'dataset',\n 'totalRows',\n 'cursors',\n 'infinite',\n 'rowsUpdate'\n])\nconst reserved = new Set(['key', 'ref', 'ref_for', 'ref_key'])\nconst passed = (): Record<string, unknown> => {\n const raw = instance.vnode.props ?? {}\n const declared = props as Record<string, unknown>\n const out: Record<string, unknown> = {}\n for (const key of Object.keys(raw)) {\n if (reserved.has(key) || key.startsWith('onVnode')) {\n continue\n }\n if (/^on[A-Z]/.test(key)) {\n // `update:query` and `update:columns`, `.once` included, are emitted\n // by DataTable itself.\n if (!/^onUpdate:(query|columns)/.test(key)) {\n out[key] = raw[key]\n }\n continue\n }\n const name = camelize(key)\n if (own.has(name)) {\n continue\n }\n if (name in declared) {\n out[name] = declared[name]\n } else {\n out[key] = raw[key]\n }\n }\n return out\n}\n\n// The slots QueryTable gets: the consumer's, without `error`, without\n// `pagination` when nothing is paged, and with `empty` replaced while the\n// evaluator reports an error and an `error` slot is given.\nconst showError = () => local.error.value !== null && slots.error !== undefined\nconst slotNames = () =>\n Object.keys(slots).filter(\n name =>\n name !== 'error' &&\n !(name === 'pagination' && !props.paginate) &&\n !(name === 'empty' && showError())\n ) as Array<keyof DataTableSlots<T>>\n\nconst table = useTemplateRef<QueryTableExpose>('table')\nconst exposed: QueryTableExpose = {\n collapseAll: () => table.value?.collapseAll(),\n expandAll: () => table.value?.expandAll(),\n focusFilter: field => table.value?.focusFilter(field) ?? false,\n flushPendingFilters: () => table.value?.flushPendingFilters(),\n scrollToIndex: (index, options) => table.value?.scrollToIndex(index, options),\n loadMore: () => table.value?.loadMore(),\n measureColumnWidths: fields => table.value?.measureColumnWidths(fields) ?? {}\n}\ndefineExpose(exposed)\n</script>\n\n<template>\n <QueryTable\n ref=\"table\"\n v-bind=\"passed()\"\n :columns=\"columns\"\n :query=\"query\"\n :rows=\"drawn.rows\"\n :total-rows=\"local.totalRows.value\"\n :rows-update=\"drawn.hint\"\n @update:query=\"onQuery\"\n @update:columns=\"onColumns\"\n >\n <template v-for=\"name in slotNames()\" :key=\"name\" #[name]=\"scope\">\n <slot :name=\"name\" v-bind=\"scope ?? {}\" />\n </template>\n <template v-if=\"showError()\" #empty>\n <slot name=\"error\" :error=\"local.error.value!\" />\n </template>\n </QueryTable>\n</template>\n","<script setup lang=\"ts\" generic=\"T extends object\">\nimport { columnTypeOf, sameQuery } from '@dolusoft/query-protocol'\nimport type { Dataset, SemanticsProfile } from '@dolusoft/query-protocol/local'\nimport {\n camelize,\n computed,\n getCurrentInstance,\n shallowRef,\n useTemplateRef,\n watch\n} from 'vue'\n\nimport type { DataTableEmits, DataTableProps, DataTableSlots } from './contract'\nimport {\n datasetOf,\n datasetSpecOf,\n sameSpec,\n type DatasetSpec\n} from './derive-dataset'\nimport type {\n Column,\n ColumnChangeReason,\n QueryChangeReason,\n QueryTableExpose,\n RowsUpdate,\n TableQuery\n} from '../contract'\nimport { QueryTable } from '../index'\nimport { useLocalQuery } from '../local'\n\n// `useLocalQuery` and `QueryTable` side by side (ADR 0021, K319-1): no\n// markup, no table state and no table behavior of its own.\ndefineOptions({ name: 'DataTable', inheritAttrs: false })\n\nconst props = withDefaults(defineProps<DataTableProps<T>>(), {\n query: undefined,\n profile: 'tr-2',\n search: undefined,\n paginate: true,\n dataset: undefined\n})\n\nconst emit = defineEmits<DataTableEmits<T>>()\n\nconst slots = defineSlots<DataTableSlots<T>>()\n\n// The query (C-125): the consumer's when given, else held here. Until the\n// first update the held query is the start query, which follows\n// `pageSizeOptions`; while the consumer gives one, the held query is dropped.\nconst start = computed<TableQuery>(previous => {\n const pageSize = props.pagination?.pageSizeOptions?.[0] ?? 10\n return previous?.pageSize === pageSize\n ? previous\n : { page: 1, pageSize, sort: null, filters: [] }\n})\nconst held = shallowRef<TableQuery | null>(null)\nwatch(\n () => props.query !== undefined,\n given => {\n if (given) {\n held.value = null\n }\n }\n)\nconst query = computed(() => props.query ?? held.value ?? start.value)\nconst onQuery = (next: TableQuery, reason: QueryChangeReason) => {\n if (props.query === undefined) {\n held.value = next\n }\n emit('update:query', next, reason)\n}\n\n// The dataset (C-126): built again only when what it is built from changes,\n// compared by value.\nconst spec = computed<DatasetSpec>(previous => {\n const next = datasetSpecOf({\n rowKey: props.rowKey,\n columns: props.columns,\n search: props.search,\n sortable: props.sortable,\n filterable: props.filterable\n })\n return previous && sameSpec(previous, next) ? previous : next\n})\nconst derived = computed(() => datasetOf<T>(spec.value))\nconst dataset = computed(() => props.dataset ?? derived.value)\n\nconst local = useLocalQuery<T>({\n allRows: () => props.rows,\n query,\n dataset,\n // Read on every evaluation, so a new profile evaluates again.\n get profile() {\n return props.profile\n },\n paginate: () => props.paginate\n})\n\n// The flash hint (C-124): a new context is a snapshot; a new page in the\n// same context comes from the source. The query is compared by value, so a\n// new but equal query object is the same context.\ninterface Context {\n query: TableQuery\n dataset: Dataset<T>\n profile: SemanticsProfile\n paginate: boolean\n}\nconst drawn = computed<{ rows: T[]; hint: RowsUpdate; context: Context }>(\n previous => {\n const context: Context = {\n query: query.value,\n dataset: dataset.value,\n profile: props.profile,\n paginate: props.paginate\n }\n const before = previous?.context\n const same =\n before !== undefined &&\n sameQuery(before.query, context.query) &&\n before.dataset === context.dataset &&\n before.profile === context.profile &&\n before.paginate === context.paginate\n return { rows: local.rows.value, hint: same ? 'live' : 'snapshot', context }\n }\n)\n\n// An `ip` column is not a field (C-118, C-126), so the table offers no sort\n// and no filter on it. `update:columns` gives the consumer's own `sortable`\n// and `filterable` back, so the change never reaches the consumer's columns.\nconst isIp = (column: Column) => columnTypeOf(column) === 'ip'\nconst closed = (column: Column) =>\n isIp(column) && (column.sortable !== false || column.filterable !== false)\nconst columns = computed(() =>\n props.columns.some(closed)\n ? props.columns.map(column =>\n closed(column)\n ? { ...column, sortable: false, filterable: false }\n : column\n )\n : props.columns\n)\nconst onColumns = (next: Column[], reason: ColumnChangeReason) => {\n const drawnColumns = columns.value\n const originals = new Map(\n props.columns.filter(closed).map(column => [column.field, column])\n )\n const restored = next.map(column => {\n const original = originals.get(column.field)\n if (!original || !isIp(column)) {\n return column\n }\n if (drawnColumns.includes(column)) {\n return original\n }\n const copy: Column = { ...column }\n for (const key of ['sortable', 'filterable'] as const) {\n if (key in original) {\n copy[key] = original[key]\n } else {\n delete copy[key]\n }\n }\n return copy\n })\n emit('update:columns', restored, reason)\n}\n\n// What the consumer gave reaches QueryTable as given; what it left out is\n// left out, so QueryTable applies its own default (C-124). A declared prop\n// is passed as Vue read it (`sortable=\"\"` is `true`); any other attribute\n// keeps its name (`data-testid`, `aria-label`).\nconst instance = getCurrentInstance()!\nconst own = new Set([\n 'columns',\n 'query',\n 'rows',\n 'profile',\n 'search',\n 'paginate',\n 'dataset',\n 'totalRows',\n 'cursors',\n 'infinite',\n 'rowsUpdate'\n])\nconst reserved = new Set(['key', 'ref', 'ref_for', 'ref_key'])\nconst passed = (): Record<string, unknown> => {\n const raw = instance.vnode.props ?? {}\n const declared = props as Record<string, unknown>\n const out: Record<string, unknown> = {}\n for (const key of Object.keys(raw)) {\n if (reserved.has(key) || key.startsWith('onVnode')) {\n continue\n }\n if (/^on[A-Z]/.test(key)) {\n // `update:query` and `update:columns`, `.once` included, are emitted\n // by DataTable itself.\n if (!/^onUpdate:(query|columns)/.test(key)) {\n out[key] = raw[key]\n }\n continue\n }\n const name = camelize(key)\n if (own.has(name)) {\n continue\n }\n if (name in declared) {\n out[name] = declared[name]\n } else {\n out[key] = raw[key]\n }\n }\n return out\n}\n\n// The slots QueryTable gets: the consumer's, without `error`, without\n// `pagination` when nothing is paged, and with `empty` replaced while the\n// evaluator reports an error and an `error` slot is given.\nconst showError = () => local.error.value !== null && slots.error !== undefined\nconst slotNames = () =>\n Object.keys(slots).filter(\n name =>\n name !== 'error' &&\n !(name === 'pagination' && !props.paginate) &&\n !(name === 'empty' && showError())\n ) as Array<keyof DataTableSlots<T>>\n\nconst table = useTemplateRef<QueryTableExpose>('table')\nconst exposed: QueryTableExpose = {\n collapseAll: () => table.value?.collapseAll(),\n expandAll: () => table.value?.expandAll(),\n focusFilter: field => table.value?.focusFilter(field) ?? false,\n flushPendingFilters: () => table.value?.flushPendingFilters(),\n scrollToIndex: (index, options) => table.value?.scrollToIndex(index, options),\n loadMore: () => table.value?.loadMore(),\n measureColumnWidths: fields => table.value?.measureColumnWidths(fields) ?? {}\n}\ndefineExpose(exposed)\n</script>\n\n<template>\n <QueryTable\n ref=\"table\"\n v-bind=\"passed()\"\n :columns=\"columns\"\n :query=\"query\"\n :rows=\"drawn.rows\"\n :total-rows=\"local.totalRows.value\"\n :rows-update=\"drawn.hint\"\n @update:query=\"onQuery\"\n @update:columns=\"onColumns\"\n >\n <template v-for=\"name in slotNames()\" :key=\"name\" #[name]=\"scope\">\n <slot :name=\"name\" v-bind=\"scope ?? {}\" />\n </template>\n <template v-if=\"showError()\" #empty>\n <slot name=\"error\" :error=\"local.error.value!\" />\n </template>\n </QueryTable>\n</template>\n","// `DataTable`: a table for rows held in the page, `useLocalQuery` and\n// `QueryTable` composed behind their own entry (ADR 0021, P9, P14). The\n// default entry and `/local` never import it.\nimport DataTable from './data-table.vue'\n\nexport { DataTable }\nexport default DataTable\n\nexport type {\n DataTableEmits,\n DataTableOmittedProp,\n DataTableProps,\n DataTableSlots,\n ErrorSlotProps\n} from './contract'\n"],"mappings":";;;;;;;;;;AAmCA,IAAa,iBAAiB,WAAuC;CACnE,MAAM,SAAgC,CAAC;CACvC,MAAM,KAAe,CAAC;CACtB,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,UAAU,OAAO,SAAS;EACnC,IAAI,KAAK,IAAI,OAAO,KAAK,GACvB;EAEF,KAAK,IAAI,OAAO,KAAK;EACrB,MAAM,OAAO,aAAa,MAAM;EAChC,IAAI,SAAS,MAAM;GACjB,GAAG,KAAK,OAAO,KAAK;GACpB;EACF;EACA,OAAO,KAAK;GACV,MAAM,OAAO;GACb;GACA,UAAU,OAAO,YAAY,OAAO,aAAa;GACjD,YAAY,OAAO,cAAc,OAAO,eAAe;EACzD,CAAC;CACH;CAGA,MAAM,UAAU,GAAW,MAAe,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;CACnE,OAAO,MAAM,GAAG,MAAM,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC;CAC5C,OAAO;EACL,KAAK,OAAO;EACZ,QAAQ,CAAC,GAAG,IAAI,IAAI,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM;EACrD;EACA,IAAI,GAAG,KAAK,MAAM;CACpB;AACF;;AAGA,IAAa,YAAY,GAAgB,MACvC,KAAK,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC;;AAGxC,IAAa,aAAgB,SAAkC;CAG7D,IAAI,KAAK,IAAI,SAAS,GAAG,GACvB,MAAM,IAAI,UACR,0BAA0B,KAAK,IAAI,2BACrC;CAGF,MAAM,SAAS,OAAO,OAAO,IAAI;CACjC,KAAK,MAAM,SAAS,KAAK,QACvB,OAAO,MAAM,QAAQ;EACnB,MAAM,MAAM;EACZ,UAAU,MAAM;EACh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MAAM,OAAO,cAAc,OAAO,OAAO;GAC5E,gBAAgB,MAAM,OAAO,SAAS;GACtC,sBAAqB,WAAU,MAAM,OAAO,oBAAoB,MAAM,KAAK,CAAC;EAEjE,CAAO;;GAIlB,OAAAC,UAAA,GAAAC,YAiBaC,MAAAC,mBAAA,GAjBbC,WAiBa;IAhBP,SAAA;IAAJ,KAAI;GACI,GAAA,OAAM,GAAA;IACb,SAAS,QAAA;IACT,OAAO,MAAA;IACP,MAAM,MAAA,MAAM;IACZ,cAAYF,MAAA,KAAA,CAAK,CAAC,UAAU;IAC5B,eAAa,MAAA,MAAM;IACnB,kBAAc;IACd,oBAAgB;GAEQI,CAAAA,GAAAA,YAAAA,EAAAA,GAAAA,EAAAA,GAAAA,CAAAA,WAAA,UAAS,IAAjB,SAAI;;KAA8B;KACjD,IAAAC,SADyD,UAAK,CAC9DC,WAA0C,KAAA,QAA7B,MAAIC,eAAAC,mBAAU,SAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;GAElB,CAAA,GAAA,UAAS,IAAA;IAAK,MAAA;IAC5B,IAAAH,cAAiD,CAAjDC,WAAiD,KAAA,QAAA,SAAA,EAA7B,OAAON,MAAA,KAAA,CAAK,CAAC,MAAM,MAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;AE1P7C,IAAA,uBAAeU"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { computed, toValue, watch } from "vue";
|
|
2
|
+
import { cloneQuery, sameRules, searchOf } from "@dolusoft/query-protocol";
|
|
3
|
+
import { applyQuery, slicePage } from "@dolusoft/query-protocol/local";
|
|
4
|
+
//#region src/local/use-local-query.ts
|
|
5
|
+
var isDevelopment = () => {
|
|
6
|
+
try {
|
|
7
|
+
return process.env.NODE_ENV !== "production";
|
|
8
|
+
} catch {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/** Evaluates locally without rerunning filtering, search or sorting for a page-only change. */
|
|
13
|
+
function useLocalQuery(options) {
|
|
14
|
+
const semantic = computed((previous) => {
|
|
15
|
+
const query = toValue(options.query);
|
|
16
|
+
const copy = cloneQuery({
|
|
17
|
+
page: 1,
|
|
18
|
+
pageSize: 1,
|
|
19
|
+
filters: query.filters,
|
|
20
|
+
sort: query.sort,
|
|
21
|
+
search: searchOf(query)
|
|
22
|
+
});
|
|
23
|
+
const next = {
|
|
24
|
+
filters: copy.filters,
|
|
25
|
+
sort: copy.sort,
|
|
26
|
+
search: searchOf(copy)
|
|
27
|
+
};
|
|
28
|
+
return previous && sameRules(previous.filters, next.filters) && previous.sort?.field === next.sort?.field && previous.sort?.direction === next.sort?.direction && previous.sort === null === (next.sort === null) && previous.search === next.search ? previous : next;
|
|
29
|
+
});
|
|
30
|
+
const evaluated = computed(() => applyQuery(toValue(options.allRows), {
|
|
31
|
+
page: 1,
|
|
32
|
+
pageSize: 1,
|
|
33
|
+
...semantic.value
|
|
34
|
+
}, toValue(options.dataset), {
|
|
35
|
+
profile: options.profile,
|
|
36
|
+
paginate: false
|
|
37
|
+
}));
|
|
38
|
+
const result = computed(() => {
|
|
39
|
+
const query = toValue(options.query);
|
|
40
|
+
const paginate = toValue(options.paginate) ?? true;
|
|
41
|
+
const check = applyQuery([], query, toValue(options.dataset), {
|
|
42
|
+
profile: options.profile,
|
|
43
|
+
paginate
|
|
44
|
+
});
|
|
45
|
+
if (!check.ok) return check;
|
|
46
|
+
const all = evaluated.value;
|
|
47
|
+
if (!all.ok) return all;
|
|
48
|
+
if (!paginate) return {
|
|
49
|
+
ok: true,
|
|
50
|
+
rows: [...all.rows],
|
|
51
|
+
totalRows: all.totalRows
|
|
52
|
+
};
|
|
53
|
+
const page = slicePage(all.rows, query);
|
|
54
|
+
return page.ok ? {
|
|
55
|
+
ok: true,
|
|
56
|
+
rows: page.rows,
|
|
57
|
+
totalRows: all.totalRows
|
|
58
|
+
} : page;
|
|
59
|
+
});
|
|
60
|
+
const rows = computed(() => result.value.ok ? result.value.rows : []);
|
|
61
|
+
const totalRows = computed(() => result.value.ok ? result.value.totalRows : 0);
|
|
62
|
+
const error = computed(() => result.value.ok ? null : result.value.error);
|
|
63
|
+
let last = "";
|
|
64
|
+
watch(error, (value) => {
|
|
65
|
+
if (!value) {
|
|
66
|
+
last = "";
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const key = JSON.stringify([
|
|
70
|
+
value.code,
|
|
71
|
+
value.path,
|
|
72
|
+
value.field
|
|
73
|
+
]);
|
|
74
|
+
if (isDevelopment() && key !== last) {
|
|
75
|
+
last = key;
|
|
76
|
+
console.error("[useLocalQuery]", value);
|
|
77
|
+
}
|
|
78
|
+
}, { immediate: true });
|
|
79
|
+
return {
|
|
80
|
+
rows,
|
|
81
|
+
totalRows,
|
|
82
|
+
error
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
export { useLocalQuery as t };
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=local-DDJEp4Nn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-DDJEp4Nn.js","names":[],"sources":["../src/local/use-local-query.ts"],"sourcesContent":["import {\n cloneQuery,\n sameRules,\n searchOf,\n type FilterRule,\n type Query,\n type SortState\n} from '@dolusoft/query-protocol'\nimport {\n applyQuery,\n slicePage,\n type Dataset,\n type LocalQueryError,\n type LocalQueryResult,\n type SemanticsProfile\n} from '@dolusoft/query-protocol/local'\nimport {\n computed,\n toValue,\n watch,\n type ComputedRef,\n type MaybeRefOrGetter\n} from 'vue'\n\n// Replaced by the consumer's bundler, just as in Vue's esm-bundler build.\ndeclare const process: { env: { NODE_ENV?: string } }\n\nexport interface UseLocalQueryOptions<T> {\n /** Every row of the source context. In-place changes in a deeply reactive array are seen; replace a shallowRef's array to trigger evaluation. */\n allRows: MaybeRefOrGetter<readonly T[]>\n /** The consumer's query, read without writing or correcting it. */\n query: MaybeRefOrGetter<Query>\n /** Replace together with allRows when the schema changes. */\n dataset: MaybeRefOrGetter<Dataset<T>>\n profile: SemanticsProfile\n /** false returns every matching row for print or export. Defaults to true. */\n paginate?: MaybeRefOrGetter<boolean>\n}\n\nexport interface LocalQuery<T> {\n /** The supplied row objects; empty while error is set. */\n rows: ComputedRef<T[]>\n /** The matching count; zero while error is set. */\n totalRows: ComputedRef<number>\n /** Render this separately from an empty result. */\n error: ComputedRef<LocalQueryError | null>\n}\n\n// A try/catch preserves constant replacement in production browser builds.\n// A typeof guard would stay true when the browser has no process global.\nconst isDevelopment = (): boolean => {\n try {\n return process.env.NODE_ENV !== 'production'\n } catch {\n return true\n }\n}\n\ninterface Semantic {\n filters: FilterRule[]\n sort: SortState | null\n search: string\n}\n\n/** Evaluates locally without rerunning filtering, search or sorting for a page-only change. */\nexport function useLocalQuery<T>(\n options: UseLocalQueryOptions<T>\n): LocalQuery<T> {\n const semantic = computed<Semantic>(previous => {\n const query = toValue(options.query)\n // Include nested extra rule properties: sameRules compares them by value.\n const copy = cloneQuery({\n page: 1,\n pageSize: 1,\n filters: query.filters,\n sort: query.sort,\n search: searchOf(query)\n })\n const next = {\n filters: copy.filters,\n sort: copy.sort,\n search: searchOf(copy)\n }\n return previous &&\n sameRules(previous.filters, next.filters) &&\n previous.sort?.field === next.sort?.field &&\n previous.sort?.direction === next.sort?.direction &&\n (previous.sort === null) === (next.sort === null) &&\n previous.search === next.search\n ? previous\n : next\n })\n const evaluated = computed(() =>\n applyQuery(\n toValue(options.allRows),\n { page: 1, pageSize: 1, ...semantic.value },\n toValue(options.dataset),\n { profile: options.profile, paginate: false }\n )\n )\n const result = computed<LocalQueryResult<T>>(() => {\n const query = toValue(options.query)\n const paginate = toValue(options.paginate) ?? true\n const check = applyQuery([], query, toValue(options.dataset), {\n profile: options.profile,\n paginate\n })\n if (!check.ok) {\n return check\n }\n const all = evaluated.value\n if (!all.ok) {\n return all\n }\n if (!paginate) {\n return { ok: true, rows: [...all.rows], totalRows: all.totalRows }\n }\n // The precheck already validated paging.\n const page = slicePage(all.rows, query)\n return page.ok\n ? { ok: true, rows: page.rows, totalRows: all.totalRows }\n : page\n })\n const rows = computed(() => (result.value.ok ? result.value.rows : []))\n const totalRows = computed(() =>\n result.value.ok ? result.value.totalRows : 0\n )\n const error = computed(() => (result.value.ok ? null : result.value.error))\n let last = ''\n watch(\n error,\n value => {\n if (!value) {\n last = ''\n return\n }\n const key = JSON.stringify([value.code, value.path, value.field])\n if (isDevelopment() && key !== last) {\n last = key\n // oxlint-disable-next-line no-console -- development-only warning to the library user\n console.error('[useLocalQuery]', value)\n }\n },\n { immediate: true }\n )\n return { rows, totalRows, error }\n}\n"],"mappings":";;;;AAkDA,IAAM,sBAA+B;CACnC,IAAI;EACF,OAAA,QAAA,IAAA,aAAgC;CAClC,QAAQ;EACN,OAAO;CACT;AACF;;AASA,SAAgB,cACd,SACe;CACf,MAAM,WAAW,UAAmB,aAAY;EAC9C,MAAM,QAAQ,QAAQ,QAAQ,KAAK;EAEnC,MAAM,OAAO,WAAW;GACtB,MAAM;GACN,UAAU;GACV,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,QAAQ,SAAS,KAAK;EACxB,CAAC;EACD,MAAM,OAAO;GACX,SAAS,KAAK;GACd,MAAM,KAAK;GACX,QAAQ,SAAS,IAAI;EACvB;EACA,OAAO,YACL,UAAU,SAAS,SAAS,KAAK,OAAO,KACxC,SAAS,MAAM,UAAU,KAAK,MAAM,SACpC,SAAS,MAAM,cAAc,KAAK,MAAM,aACvC,SAAS,SAAS,UAAW,KAAK,SAAS,SAC5C,SAAS,WAAW,KAAK,SACvB,WACA;CACN,CAAC;CACD,MAAM,YAAY,eAChB,WACE,QAAQ,QAAQ,OAAO,GACvB;EAAE,MAAM;EAAG,UAAU;EAAG,GAAG,SAAS;CAAM,GAC1C,QAAQ,QAAQ,OAAO,GACvB;EAAE,SAAS,QAAQ;EAAS,UAAU;CAAM,CAC9C,CACF;CACA,MAAM,SAAS,eAAoC;EACjD,MAAM,QAAQ,QAAQ,QAAQ,KAAK;EACnC,MAAM,WAAW,QAAQ,QAAQ,QAAQ,KAAK;EAC9C,MAAM,QAAQ,WAAW,CAAC,GAAG,OAAO,QAAQ,QAAQ,OAAO,GAAG;GAC5D,SAAS,QAAQ;GACjB;EACF,CAAC;EACD,IAAI,CAAC,MAAM,IACT,OAAO;EAET,MAAM,MAAM,UAAU;EACtB,IAAI,CAAC,IAAI,IACP,OAAO;EAET,IAAI,CAAC,UACH,OAAO;GAAE,IAAI;GAAM,MAAM,CAAC,GAAG,IAAI,IAAI;GAAG,WAAW,IAAI;EAAU;EAGnE,MAAM,OAAO,UAAU,IAAI,MAAM,KAAK;EACtC,OAAO,KAAK,KACR;GAAE,IAAI;GAAM,MAAM,KAAK;GAAM,WAAW,IAAI;EAAU,IACtD;CACN,CAAC;CACD,MAAM,OAAO,eAAgB,OAAO,MAAM,KAAK,OAAO,MAAM,OAAO,CAAC,CAAE;CACtE,MAAM,YAAY,eAChB,OAAO,MAAM,KAAK,OAAO,MAAM,YAAY,CAC7C;CACA,MAAM,QAAQ,eAAgB,OAAO,MAAM,KAAK,OAAO,OAAO,MAAM,KAAM;CAC1E,IAAI,OAAO;CACX,MACE,QACA,UAAS;EACP,IAAI,CAAC,OAAO;GACV,OAAO;GACP;EACF;EACA,MAAM,MAAM,KAAK,UAAU;GAAC,MAAM;GAAM,MAAM;GAAM,MAAM;EAAK,CAAC;EAChE,IAAI,cAAc,KAAK,QAAQ,MAAM;GACnC,OAAO;GAEP,QAAQ,MAAM,mBAAmB,KAAK;EACxC;CACF,GACA,EAAE,WAAW,KAAK,CACpB;CACA,OAAO;EAAE;EAAM;EAAW;CAAM;AAClC"}
|
package/dist/local.js
CHANGED
|
@@ -1,88 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { cloneQuery, sameRules, searchOf } from "@dolusoft/query-protocol";
|
|
3
|
-
import { applyQuery, slicePage } from "@dolusoft/query-protocol/local";
|
|
4
|
-
//#region src/local/use-local-query.ts
|
|
5
|
-
var isDevelopment = () => {
|
|
6
|
-
try {
|
|
7
|
-
return process.env.NODE_ENV !== "production";
|
|
8
|
-
} catch {
|
|
9
|
-
return true;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
/** Evaluates locally without rerunning filtering, search or sorting for a page-only change. */
|
|
13
|
-
function useLocalQuery(options) {
|
|
14
|
-
const semantic = computed((previous) => {
|
|
15
|
-
const query = toValue(options.query);
|
|
16
|
-
const copy = cloneQuery({
|
|
17
|
-
page: 1,
|
|
18
|
-
pageSize: 1,
|
|
19
|
-
filters: query.filters,
|
|
20
|
-
sort: query.sort,
|
|
21
|
-
search: searchOf(query)
|
|
22
|
-
});
|
|
23
|
-
const next = {
|
|
24
|
-
filters: copy.filters,
|
|
25
|
-
sort: copy.sort,
|
|
26
|
-
search: searchOf(copy)
|
|
27
|
-
};
|
|
28
|
-
return previous && sameRules(previous.filters, next.filters) && previous.sort?.field === next.sort?.field && previous.sort?.direction === next.sort?.direction && previous.sort === null === (next.sort === null) && previous.search === next.search ? previous : next;
|
|
29
|
-
});
|
|
30
|
-
const evaluated = computed(() => applyQuery(toValue(options.allRows), {
|
|
31
|
-
page: 1,
|
|
32
|
-
pageSize: 1,
|
|
33
|
-
...semantic.value
|
|
34
|
-
}, toValue(options.dataset), {
|
|
35
|
-
profile: options.profile,
|
|
36
|
-
paginate: false
|
|
37
|
-
}));
|
|
38
|
-
const result = computed(() => {
|
|
39
|
-
const query = toValue(options.query);
|
|
40
|
-
const paginate = toValue(options.paginate) ?? true;
|
|
41
|
-
const check = applyQuery([], query, toValue(options.dataset), {
|
|
42
|
-
profile: options.profile,
|
|
43
|
-
paginate
|
|
44
|
-
});
|
|
45
|
-
if (!check.ok) return check;
|
|
46
|
-
const all = evaluated.value;
|
|
47
|
-
if (!all.ok) return all;
|
|
48
|
-
if (!paginate) return {
|
|
49
|
-
ok: true,
|
|
50
|
-
rows: [...all.rows],
|
|
51
|
-
totalRows: all.totalRows
|
|
52
|
-
};
|
|
53
|
-
const page = slicePage(all.rows, query);
|
|
54
|
-
return page.ok ? {
|
|
55
|
-
ok: true,
|
|
56
|
-
rows: page.rows,
|
|
57
|
-
totalRows: all.totalRows
|
|
58
|
-
} : page;
|
|
59
|
-
});
|
|
60
|
-
const rows = computed(() => result.value.ok ? result.value.rows : []);
|
|
61
|
-
const totalRows = computed(() => result.value.ok ? result.value.totalRows : 0);
|
|
62
|
-
const error = computed(() => result.value.ok ? null : result.value.error);
|
|
63
|
-
let last = "";
|
|
64
|
-
watch(error, (value) => {
|
|
65
|
-
if (!value) {
|
|
66
|
-
last = "";
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const key = JSON.stringify([
|
|
70
|
-
value.code,
|
|
71
|
-
value.path,
|
|
72
|
-
value.field
|
|
73
|
-
]);
|
|
74
|
-
if (isDevelopment() && key !== last) {
|
|
75
|
-
last = key;
|
|
76
|
-
console.error("[useLocalQuery]", value);
|
|
77
|
-
}
|
|
78
|
-
}, { immediate: true });
|
|
79
|
-
return {
|
|
80
|
-
rows,
|
|
81
|
-
totalRows,
|
|
82
|
-
error
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
//#endregion
|
|
1
|
+
import { t as useLocalQuery } from "./local-DDJEp4Nn.js";
|
|
86
2
|
export { useLocalQuery };
|
|
87
|
-
|
|
88
|
-
//# sourceMappingURL=local.js.map
|