@jskit-ai/http-web 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fixtures/crud-list-date-filters/App.vue +107 -0
- package/fixtures/crud-list-date-filters/index.html +12 -0
- package/fixtures/crud-list-date-filters/main.js +24 -0
- package/fixtures/crud-list-date-filters/vite.config.mjs +18 -0
- package/package.json +150 -0
- package/src/client/bulkActions.js +47 -0
- package/src/client/components/CrudAddEditScreen.vue +186 -0
- package/src/client/components/CrudDeleteAction.vue +93 -0
- package/src/client/components/CrudListBulkActionSurface.vue +126 -0
- package/src/client/components/CrudListDateFilterControl.vue +188 -0
- package/src/client/components/CrudListFilterSurface.vue +388 -0
- package/src/client/components/CrudListRecordActionMenu.vue +120 -0
- package/src/client/components/CrudListScreen.vue +501 -0
- package/src/client/components/CrudViewScreen.vue +197 -0
- package/src/client/composables/crud/crudBindingSupport.js +75 -0
- package/src/client/composables/crud/crudHttpClientSupport.js +57 -0
- package/src/client/composables/crud/crudJsonApiTransportSupport.js +145 -0
- package/src/client/composables/crud/crudLookupFieldLabelSupport.js +151 -0
- package/src/client/composables/crud/crudLookupFieldRuntime.js +251 -0
- package/src/client/composables/crud/crudSchemaFormHelpers.js +464 -0
- package/src/client/composables/internal/crudListFilterLookupSupport.js +161 -0
- package/src/client/composables/internal/crudListParentTitleSupport.js +171 -0
- package/src/client/composables/internal/useOperationScope.js +144 -0
- package/src/client/composables/records/useAddEdit.js +224 -0
- package/src/client/composables/records/useCrudAddEdit.js +227 -0
- package/src/client/composables/records/useCrudList.js +101 -0
- package/src/client/composables/records/useCrudView.js +47 -0
- package/src/client/composables/records/useList.js +500 -0
- package/src/client/composables/records/useView.js +164 -0
- package/src/client/composables/runtime/addEditUiRuntime.js +130 -0
- package/src/client/composables/runtime/listUiRuntime.js +123 -0
- package/src/client/composables/runtime/modelStateHelpers.js +90 -0
- package/src/client/composables/runtime/operationAdapters.js +34 -0
- package/src/client/composables/runtime/operationUiHelpers.js +125 -0
- package/src/client/composables/runtime/operationValidationHelpers.js +51 -0
- package/src/client/composables/runtime/useAddEditCore.js +112 -0
- package/src/client/composables/runtime/useCommandCore.js +123 -0
- package/src/client/composables/runtime/useEndpointResource.js +199 -0
- package/src/client/composables/runtime/useFieldErrorBag.js +61 -0
- package/src/client/composables/runtime/useListCore.js +112 -0
- package/src/client/composables/runtime/useUiFeedback.js +98 -0
- package/src/client/composables/runtime/useViewCore.js +93 -0
- package/src/client/composables/runtime/viewUiRuntime.js +96 -0
- package/src/client/composables/support/errorMessageHelpers.js +66 -0
- package/src/client/composables/support/listQueryParamSupport.js +473 -0
- package/src/client/composables/support/listSearchSupport.js +70 -0
- package/src/client/composables/support/refValueHelpers.js +19 -0
- package/src/client/composables/support/requestQueryRuntimeSupport.js +141 -0
- package/src/client/composables/support/resourceLoadStateHelpers.js +208 -0
- package/src/client/composables/support/routeTemplateHelpers.js +285 -0
- package/src/client/composables/support/scopeHelpers.js +145 -0
- package/src/client/composables/useAccess.js +109 -0
- package/src/client/composables/useCommand.js +118 -0
- package/src/client/composables/useCrudAddEditScreen.js +88 -0
- package/src/client/composables/useCrudDeleteAction.js +160 -0
- package/src/client/composables/useCrudListBulkActions.js +147 -0
- package/src/client/composables/useCrudListFilterLookups.js +146 -0
- package/src/client/composables/useCrudListFilters.js +339 -0
- package/src/client/composables/useCrudListParentTitle.js +151 -0
- package/src/client/composables/useCrudListRowActions.js +144 -0
- package/src/client/composables/useCrudListScreen.js +237 -0
- package/src/client/composables/useCrudViewScreen.js +77 -0
- package/src/client/composables/usePagedCollection.js +181 -0
- package/src/client/composables/useRealtimeQueryInvalidation.js +156 -0
- package/src/client/composables/useScopeRuntime.js +135 -0
- package/src/client/filters.js +15 -0
- package/src/client/index.js +24 -0
- package/src/client/lib/httpClient.js +47 -0
- package/src/client/lib/permissions.js +34 -0
- package/src/client/rowActions.js +47 -0
- package/src/client/support/contractGuards.js +34 -0
- package/src/client/support/crudListDateFilterSupport.js +91 -0
- package/test/addEditUiRuntime.test.js +110 -0
- package/test/crudBindingSupport.test.js +110 -0
- package/test/crudHttpClientSupport.test.js +77 -0
- package/test/crudJsonApiTransportSupport.test.js +179 -0
- package/test/crudListBulkActionSurface.test.js +27 -0
- package/test/crudListDateFilterSupport.test.js +92 -0
- package/test/crudListDateFilterSurface.browser.test.js +182 -0
- package/test/crudListFilterSurface.test.js +90 -0
- package/test/crudLookupFieldRuntime.test.js +264 -0
- package/test/crudScreenComponents.test.js +110 -0
- package/test/errorIntentContract.test.js +31 -0
- package/test/errorMessageHelpers.test.js +28 -0
- package/test/exportsContract.test.js +68 -0
- package/test/listQueryParamSupport.test.js +223 -0
- package/test/listUiRuntime.test.js +102 -0
- package/test/operationValidationHelpers.test.js +64 -0
- package/test/packageBoundary.test.js +31 -0
- package/test/permissions.test.js +35 -0
- package/test/refValueHelpers.test.js +14 -0
- package/test/requestTransportOptions.test.js +457 -0
- package/test/resourceLoadStateHelpers.test.js +189 -0
- package/test/routeTemplateHelpers.test.js +85 -0
- package/test/scopeHelpers.test.js +57 -0
- package/test/useAddEditCore.test.js +124 -0
- package/test/useAddEditRequestQueryParams.test.js +190 -0
- package/test/useCrudAddEdit.test.js +415 -0
- package/test/useCrudDeleteAction.test.js +139 -0
- package/test/useCrudListBulkActions.test.js +65 -0
- package/test/useCrudListFilterLookups.test.js +114 -0
- package/test/useCrudListFilters.test.js +293 -0
- package/test/useCrudListParentTitle.test.js +262 -0
- package/test/useCrudListRowActions.test.js +77 -0
- package/test/useListRouteInitialization.test.js +337 -0
- package/test/useListSearchSupport.test.js +61 -0
- package/test/usePagedCollection.test.js +53 -0
- package/test/useViewRequestQueryParams.test.js +20 -0
- package/test/viewCoreLoading.test.js +44 -0
- package/test/viewUiRuntime.test.js +95 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
+
import {
|
|
3
|
+
normalizeCrudLookupApiPath,
|
|
4
|
+
normalizeCrudLookupNamespace,
|
|
5
|
+
normalizeCrudLookupContainerKey,
|
|
6
|
+
resolveCrudLookupApiPathFromNamespace
|
|
7
|
+
} from "@jskit-ai/resource-crud-core/shared/crudLookup";
|
|
8
|
+
import { normalizeSurfaceId } from "@jskit-ai/kernel/shared/surface/registry";
|
|
9
|
+
import { useList } from "../records/useList.js";
|
|
10
|
+
import {
|
|
11
|
+
resolveLookupItemLabel,
|
|
12
|
+
resolveLookupFieldDisplayValue
|
|
13
|
+
} from "./crudLookupFieldLabelSupport.js";
|
|
14
|
+
import { inferCrudLookupJsonApiTransport } from "./crudJsonApiTransportSupport.js";
|
|
15
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
16
|
+
|
|
17
|
+
function normalizeQueryKeyPrefix(value) {
|
|
18
|
+
const source = Array.isArray(value) ? value : [];
|
|
19
|
+
return source
|
|
20
|
+
.map((entry) => normalizeText(entry))
|
|
21
|
+
.filter(Boolean);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isSameLookupValue(left, right) {
|
|
25
|
+
if (left === right) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return String(left ?? "") === String(right ?? "");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normalizeLookupValue(value) {
|
|
33
|
+
if (value == null) {
|
|
34
|
+
return "";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return String(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function createSelectedLookupItem(selectedValue, selectedRecord = {}, entry = {}) {
|
|
41
|
+
if (selectedValue == null || selectedValue === "") {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const sourceRecord = asPlainObject(selectedRecord);
|
|
46
|
+
const sourceLookups = asPlainObject(sourceRecord[entry.lookupContainerKey]);
|
|
47
|
+
const hydratedLookup = asPlainObject(sourceLookups[entry.fieldKey]);
|
|
48
|
+
const hydratedValue = hydratedLookup[entry.valueKey];
|
|
49
|
+
const value = normalizeLookupValue(
|
|
50
|
+
hydratedValue == null || hydratedValue === "" ? selectedValue : hydratedValue
|
|
51
|
+
);
|
|
52
|
+
if (!value) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const displayValue = resolveLookupFieldDisplayValue(
|
|
57
|
+
{
|
|
58
|
+
[entry.fieldKey]: value,
|
|
59
|
+
[entry.lookupContainerKey]: {
|
|
60
|
+
[entry.fieldKey]: hydratedLookup
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
key: entry.fieldKey,
|
|
65
|
+
relation: entry.relation
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
const label = displayValue == null || displayValue === "" ? value : displayValue;
|
|
69
|
+
return {
|
|
70
|
+
value,
|
|
71
|
+
label: String(label ?? ""),
|
|
72
|
+
record: hydratedLookup
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createCrudLookupFieldRuntime({
|
|
77
|
+
formFields = [],
|
|
78
|
+
adapter = null,
|
|
79
|
+
recordIdParam = "recordId",
|
|
80
|
+
queryKeyPrefix = [],
|
|
81
|
+
placementSourcePrefix = "",
|
|
82
|
+
lookupContainerKey = "lookups"
|
|
83
|
+
} = {}) {
|
|
84
|
+
const runtimes = new Map();
|
|
85
|
+
const normalizedRecordIdParam = normalizeText(recordIdParam) || "recordId";
|
|
86
|
+
const normalizedPlacementSourcePrefix = normalizeText(placementSourcePrefix);
|
|
87
|
+
const normalizedQueryKeyPrefix = normalizeQueryKeyPrefix(queryKeyPrefix);
|
|
88
|
+
const defaultLookupContainerKey = normalizeCrudLookupContainerKey(lookupContainerKey, {
|
|
89
|
+
context: "createCrudLookupFieldRuntime lookupContainerKey"
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
for (const field of Array.isArray(formFields) ? formFields : []) {
|
|
93
|
+
const key = normalizeText(field?.key);
|
|
94
|
+
const rawRelation = field?.relation;
|
|
95
|
+
if (!key || !rawRelation || typeof rawRelation !== "object" || Array.isArray(rawRelation) || runtimes.has(key)) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const relationKind = normalizeText(rawRelation.kind).toLowerCase();
|
|
100
|
+
const namespace =
|
|
101
|
+
normalizeCrudLookupNamespace(rawRelation.namespace) ||
|
|
102
|
+
normalizeCrudLookupNamespace(rawRelation.apiPath);
|
|
103
|
+
if (relationKind !== "lookup" || !namespace) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const explicitApiPath = normalizeCrudLookupApiPath(rawRelation.apiPath);
|
|
107
|
+
const apiPath = explicitApiPath || resolveCrudLookupApiPathFromNamespace(namespace);
|
|
108
|
+
const transport = inferCrudLookupJsonApiTransport({
|
|
109
|
+
namespace,
|
|
110
|
+
apiPath
|
|
111
|
+
});
|
|
112
|
+
const valueKey = normalizeText(rawRelation.valueKey);
|
|
113
|
+
const labelKey = normalizeText(rawRelation.labelKey);
|
|
114
|
+
const relationLookupContainerKey = normalizeCrudLookupContainerKey(rawRelation.containerKey, {
|
|
115
|
+
defaultValue: defaultLookupContainerKey,
|
|
116
|
+
context: `createCrudLookupFieldRuntime formFields["${key}"].relation.containerKey`
|
|
117
|
+
});
|
|
118
|
+
const relationSurfaceId = normalizeSurfaceId(rawRelation.surfaceId);
|
|
119
|
+
if (!valueKey) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const runtime = useList({
|
|
124
|
+
adapter: adapter || undefined,
|
|
125
|
+
...(relationSurfaceId ? { surfaceId: relationSurfaceId } : {}),
|
|
126
|
+
apiSuffix: apiPath,
|
|
127
|
+
...(transport ? { transport } : {}),
|
|
128
|
+
queryKeyFactory: (surfaceId = "", scopeParamValue = "") => [
|
|
129
|
+
...normalizedQueryKeyPrefix,
|
|
130
|
+
key,
|
|
131
|
+
String(surfaceId || ""),
|
|
132
|
+
String(scopeParamValue || "")
|
|
133
|
+
],
|
|
134
|
+
search: {
|
|
135
|
+
enabled: true,
|
|
136
|
+
mode: "query",
|
|
137
|
+
queryParam: "q"
|
|
138
|
+
},
|
|
139
|
+
placementSource: normalizedPlacementSourcePrefix
|
|
140
|
+
? `${normalizedPlacementSourcePrefix}.${key}`
|
|
141
|
+
: `lookup.${key}`,
|
|
142
|
+
fallbackLoadError: `Unable to load lookup options (${apiPath}).`,
|
|
143
|
+
recordIdParam: normalizedRecordIdParam,
|
|
144
|
+
recordIdSelector: (item = {}) => item[valueKey],
|
|
145
|
+
viewUrlTemplate: "",
|
|
146
|
+
editUrlTemplate: ""
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
runtimes.set(key, Object.freeze({
|
|
150
|
+
runtime,
|
|
151
|
+
fieldKey: key,
|
|
152
|
+
lookupContainerKey: relationLookupContainerKey,
|
|
153
|
+
valueKey,
|
|
154
|
+
labelKey,
|
|
155
|
+
relation: Object.freeze({
|
|
156
|
+
kind: "lookup",
|
|
157
|
+
namespace,
|
|
158
|
+
...(explicitApiPath ? { apiPath: explicitApiPath } : {}),
|
|
159
|
+
...(relationSurfaceId ? { surfaceId: relationSurfaceId } : {}),
|
|
160
|
+
containerKey: relationLookupContainerKey,
|
|
161
|
+
valueKey,
|
|
162
|
+
...(labelKey ? { labelKey } : {})
|
|
163
|
+
})
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function resolveLookupItems(fieldKey = "", options = {}) {
|
|
168
|
+
const key = normalizeText(fieldKey);
|
|
169
|
+
const entry = runtimes.get(key);
|
|
170
|
+
if (!entry) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const items = (Array.isArray(entry.runtime.items) ? entry.runtime.items : []).map((item = {}) => {
|
|
175
|
+
const sourceRecord = asPlainObject(item);
|
|
176
|
+
const value = normalizeLookupValue(item?.[entry.valueKey]);
|
|
177
|
+
const resolvedLabel = resolveLookupItemLabel(item, entry.labelKey);
|
|
178
|
+
const label = resolvedLabel || value;
|
|
179
|
+
return {
|
|
180
|
+
value,
|
|
181
|
+
label: String(label ?? ""),
|
|
182
|
+
record: sourceRecord
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const selectedItem = createSelectedLookupItem(
|
|
187
|
+
options?.selectedValue,
|
|
188
|
+
options?.selectedRecord,
|
|
189
|
+
entry
|
|
190
|
+
);
|
|
191
|
+
if (!selectedItem) {
|
|
192
|
+
return items;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (items.some((item) => item?.value === selectedItem.value)) {
|
|
196
|
+
return items;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const matchingItem = items.find((item) => isSameLookupValue(item?.value, selectedItem.value));
|
|
200
|
+
if (matchingItem) {
|
|
201
|
+
return [
|
|
202
|
+
{
|
|
203
|
+
...selectedItem,
|
|
204
|
+
label: String(matchingItem.label ?? selectedItem.label ?? "")
|
|
205
|
+
},
|
|
206
|
+
...items
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return [selectedItem, ...items];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function resolveLookupLoading(fieldKey = "") {
|
|
214
|
+
const key = normalizeText(fieldKey);
|
|
215
|
+
const entry = runtimes.get(key);
|
|
216
|
+
if (!entry) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return Boolean(entry.runtime.isInitialLoading || entry.runtime.isFetching || entry.runtime.isRefetching);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function resolveLookupSearch(fieldKey = "") {
|
|
224
|
+
const key = normalizeText(fieldKey);
|
|
225
|
+
const entry = runtimes.get(key);
|
|
226
|
+
if (!entry) {
|
|
227
|
+
return "";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return String(entry.runtime.searchQuery || "");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function setLookupSearch(fieldKey = "", searchValue = "") {
|
|
234
|
+
const key = normalizeText(fieldKey);
|
|
235
|
+
const entry = runtimes.get(key);
|
|
236
|
+
if (!entry) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
entry.runtime.searchQuery = String(searchValue || "");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return Object.freeze({
|
|
244
|
+
resolveLookupItems,
|
|
245
|
+
resolveLookupLoading,
|
|
246
|
+
resolveLookupSearch,
|
|
247
|
+
setLookupSearch
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export { createCrudLookupFieldRuntime };
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
2
|
+
import { toRouteParamValue } from "../support/routeTemplateHelpers.js";
|
|
3
|
+
|
|
4
|
+
const EMPTY_FIELD_ERROR_LIST = Object.freeze([]);
|
|
5
|
+
const fieldErrorListCache = new Map();
|
|
6
|
+
|
|
7
|
+
function resolveStableFieldErrorList(fieldKey, message) {
|
|
8
|
+
if (!message) {
|
|
9
|
+
return EMPTY_FIELD_ERROR_LIST;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const cacheKey = `${fieldKey}::${message}`;
|
|
13
|
+
if (fieldErrorListCache.has(cacheKey)) {
|
|
14
|
+
return fieldErrorListCache.get(cacheKey);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const nextValue = Object.freeze([message]);
|
|
18
|
+
fieldErrorListCache.set(cacheKey, nextValue);
|
|
19
|
+
return nextValue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function normalizeCrudFormFields(fields = []) {
|
|
23
|
+
const normalizedFields = [];
|
|
24
|
+
const seenKeys = new Set();
|
|
25
|
+
for (const field of Array.isArray(fields) ? fields : []) {
|
|
26
|
+
const source = asPlainObject(field);
|
|
27
|
+
const key = String(source.key || "").trim();
|
|
28
|
+
if (!key || seenKeys.has(key)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
seenKeys.add(key);
|
|
33
|
+
normalizedFields.push({
|
|
34
|
+
...source,
|
|
35
|
+
key
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Object.freeze(normalizedFields);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function resolveFormFieldType(field = {}) {
|
|
43
|
+
return String(field.type || "").trim().toLowerCase();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveFormFieldFormat(field = {}) {
|
|
47
|
+
return String(field.format || "").trim().toLowerCase();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isNullableFormField(field = {}) {
|
|
51
|
+
return field?.nullable === true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isLookupFormField(field = {}) {
|
|
55
|
+
return field?.component === "lookup" || field?.relation?.kind === "lookup";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function padDateTimePart(value) {
|
|
59
|
+
return String(value).padStart(2, "0");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function normalizeTimeWhitespace(value) {
|
|
63
|
+
return String(value ?? "").replaceAll(/\s+/gu, " ").trim();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function toTimeInputValue(value) {
|
|
67
|
+
const normalized = normalizeTimeWhitespace(value);
|
|
68
|
+
if (!normalized) {
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const twentyFourHourMatch = normalized.match(/^(\d{1,2}):(\d{2})(?::(\d{2})(\.\d+)?)?$/u);
|
|
73
|
+
if (twentyFourHourMatch) {
|
|
74
|
+
const hours = Number(twentyFourHourMatch[1]);
|
|
75
|
+
const minutes = Number(twentyFourHourMatch[2]);
|
|
76
|
+
const seconds = Number(twentyFourHourMatch[3] || 0);
|
|
77
|
+
if (
|
|
78
|
+
hours >= 0 && hours <= 23 &&
|
|
79
|
+
minutes >= 0 && minutes <= 59 &&
|
|
80
|
+
seconds >= 0 && seconds <= 59
|
|
81
|
+
) {
|
|
82
|
+
const secondsValue = twentyFourHourMatch[3]
|
|
83
|
+
? `:${padDateTimePart(seconds)}${twentyFourHourMatch[4] || ""}`
|
|
84
|
+
: "";
|
|
85
|
+
return `${padDateTimePart(hours)}:${padDateTimePart(minutes)}${secondsValue}`;
|
|
86
|
+
}
|
|
87
|
+
return normalized;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const meridiemMatch = normalized.match(/^(\d{1,2}):(\d{2})\s*([ap]\.?m\.?)$/iu);
|
|
91
|
+
if (!meridiemMatch) {
|
|
92
|
+
return normalized;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const rawHours = Number(meridiemMatch[1]);
|
|
96
|
+
const minutes = Number(meridiemMatch[2]);
|
|
97
|
+
if (rawHours < 1 || rawHours > 12 || minutes < 0 || minutes > 59) {
|
|
98
|
+
return normalized;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let hours = rawHours % 12;
|
|
102
|
+
if (String(meridiemMatch[3] || "").toLowerCase().startsWith("p")) {
|
|
103
|
+
hours += 12;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return `${padDateTimePart(hours)}:${padDateTimePart(minutes)}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function toDateTimeLocalInputValue(value) {
|
|
110
|
+
if (value == null || value === "") {
|
|
111
|
+
return "";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const sourceText = typeof value === "string" ? value.trim() : "";
|
|
115
|
+
const sourceFraction = sourceText.match(/T\d{2}:\d{2}:\d{2}\.(\d+)/u)?.[1] || "";
|
|
116
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
117
|
+
if (Number.isNaN(date.getTime())) {
|
|
118
|
+
return String(value);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const minuteValue = [
|
|
122
|
+
date.getFullYear(),
|
|
123
|
+
padDateTimePart(date.getMonth() + 1),
|
|
124
|
+
padDateTimePart(date.getDate())
|
|
125
|
+
].join("-") + `T${padDateTimePart(date.getHours())}:${padDateTimePart(date.getMinutes())}`;
|
|
126
|
+
if (sourceFraction && !/^0+$/u.test(sourceFraction)) {
|
|
127
|
+
return `${minuteValue}:${padDateTimePart(date.getSeconds())}.${sourceFraction}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const milliseconds = date.getMilliseconds();
|
|
131
|
+
if (milliseconds > 0) {
|
|
132
|
+
return `${minuteValue}:${padDateTimePart(date.getSeconds())}.${String(milliseconds).padStart(3, "0")}`;
|
|
133
|
+
}
|
|
134
|
+
if (date.getSeconds() > 0) {
|
|
135
|
+
return `${minuteValue}:${padDateTimePart(date.getSeconds())}`;
|
|
136
|
+
}
|
|
137
|
+
return minuteValue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function toDateInputValue(value) {
|
|
141
|
+
if (value == null || value === "") {
|
|
142
|
+
return "";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (value instanceof Date) {
|
|
146
|
+
return Number.isNaN(value.getTime())
|
|
147
|
+
? String(value)
|
|
148
|
+
: value.toISOString().slice(0, 10);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const normalized = String(value).trim();
|
|
152
|
+
if (!normalized) {
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const calendarDateMatch = normalized.match(/^(\d{4}-\d{2}-\d{2})(?:$|[T\s])/u);
|
|
157
|
+
if (calendarDateMatch) {
|
|
158
|
+
const calendarDate = calendarDateMatch[1];
|
|
159
|
+
const parsedCalendarDate = new Date(`${calendarDate}T00:00:00.000Z`);
|
|
160
|
+
if (
|
|
161
|
+
!Number.isNaN(parsedCalendarDate.getTime()) &&
|
|
162
|
+
parsedCalendarDate.toISOString().slice(0, 10) === calendarDate
|
|
163
|
+
) {
|
|
164
|
+
return calendarDate;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return normalized;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function applyDateTimePrecision(isoValue, temporalPrecision) {
|
|
172
|
+
if (!Number.isInteger(temporalPrecision) || temporalPrecision < 0) {
|
|
173
|
+
return isoValue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const match = String(isoValue || "").match(/^(.*?)(?:\.(\d+))?Z$/u);
|
|
177
|
+
if (!match?.[2]) {
|
|
178
|
+
return isoValue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const fraction = match[2];
|
|
182
|
+
if (fraction.length <= temporalPrecision) {
|
|
183
|
+
return isoValue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const excess = fraction.slice(temporalPrecision);
|
|
187
|
+
if (!/^0+$/u.test(excess)) {
|
|
188
|
+
return isoValue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const retained = fraction.slice(0, temporalPrecision);
|
|
192
|
+
return `${match[1]}${retained ? `.${retained}` : ""}Z`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function toIsoUtcDateTimeValue(value, temporalPrecision) {
|
|
196
|
+
const normalized = String(value ?? "").trim();
|
|
197
|
+
if (!normalized) {
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const date = new Date(normalized);
|
|
202
|
+
if (Number.isNaN(date.getTime())) {
|
|
203
|
+
return normalized;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const sourceFraction = normalized.match(/T\d{2}:\d{2}:\d{2}\.(\d+)/u)?.[1] || "";
|
|
207
|
+
const dateIsoValue = date.toISOString();
|
|
208
|
+
const preciseIsoValue = sourceFraction
|
|
209
|
+
? dateIsoValue.replace(/\.\d{3}Z$/u, `.${sourceFraction}Z`)
|
|
210
|
+
: dateIsoValue;
|
|
211
|
+
return applyDateTimePrecision(preciseIsoValue, temporalPrecision);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function resolveFormFieldInitialValue(field = {}) {
|
|
215
|
+
if (Object.prototype.hasOwnProperty.call(field, "initialValue")) {
|
|
216
|
+
return field.initialValue;
|
|
217
|
+
}
|
|
218
|
+
if (Object.prototype.hasOwnProperty.call(field, "defaultValue")) {
|
|
219
|
+
return field.defaultValue;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const fieldType = resolveFormFieldType(field);
|
|
223
|
+
if (fieldType === "boolean") {
|
|
224
|
+
return isNullableFormField(field) ? null : false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (isNullableFormField(field) && isLookupFormField(field)) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return "";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function shouldSerializeClearedFieldAsNull(field = {}) {
|
|
235
|
+
if (field?.nullable !== true) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (isLookupFormField(field)) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const fieldType = resolveFormFieldType(field);
|
|
244
|
+
const fieldFormat = resolveFormFieldFormat(field);
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
fieldType === "integer" ||
|
|
248
|
+
fieldType === "number" ||
|
|
249
|
+
fieldFormat === "date" ||
|
|
250
|
+
fieldFormat === "date-time" ||
|
|
251
|
+
fieldFormat === "time"
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function createCrudFormModel(fields = []) {
|
|
256
|
+
const model = {};
|
|
257
|
+
for (const field of normalizeCrudFormFields(fields)) {
|
|
258
|
+
model[field.key] = resolveFormFieldInitialValue(field);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return model;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function buildCrudFormPayload(fields = [], model = {}) {
|
|
265
|
+
const payload = {};
|
|
266
|
+
const sourceModel = asPlainObject(model);
|
|
267
|
+
|
|
268
|
+
for (const field of normalizeCrudFormFields(fields)) {
|
|
269
|
+
const fieldKey = field.key;
|
|
270
|
+
const fieldType = resolveFormFieldType(field);
|
|
271
|
+
const fieldFormat = resolveFormFieldFormat(field);
|
|
272
|
+
const clearAsNull = shouldSerializeClearedFieldAsNull(field);
|
|
273
|
+
const rawValue = sourceModel[fieldKey];
|
|
274
|
+
|
|
275
|
+
if (fieldType === "boolean") {
|
|
276
|
+
payload[fieldKey] = rawValue == null && isNullableFormField(field)
|
|
277
|
+
? null
|
|
278
|
+
: Boolean(rawValue);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (fieldType === "integer" || fieldType === "number") {
|
|
283
|
+
const normalizedValue = String(rawValue ?? "").trim();
|
|
284
|
+
if (!normalizedValue) {
|
|
285
|
+
if (clearAsNull) {
|
|
286
|
+
payload[fieldKey] = null;
|
|
287
|
+
}
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const parsedNumber = Number(normalizedValue);
|
|
292
|
+
payload[fieldKey] = Number.isFinite(parsedNumber)
|
|
293
|
+
? (fieldType === "integer" ? Math.trunc(parsedNumber) : parsedNumber)
|
|
294
|
+
: rawValue;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (rawValue == null) {
|
|
299
|
+
if (clearAsNull) {
|
|
300
|
+
payload[fieldKey] = null;
|
|
301
|
+
}
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (isLookupFormField(field)) {
|
|
306
|
+
const normalizedLookupValue = String(rawValue).trim();
|
|
307
|
+
if (!normalizedLookupValue) {
|
|
308
|
+
if (clearAsNull) {
|
|
309
|
+
payload[fieldKey] = null;
|
|
310
|
+
}
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
payload[fieldKey] = normalizedLookupValue;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (fieldFormat === "date") {
|
|
319
|
+
const normalizedValue = String(rawValue).trim();
|
|
320
|
+
if (!normalizedValue) {
|
|
321
|
+
if (clearAsNull) {
|
|
322
|
+
payload[fieldKey] = null;
|
|
323
|
+
}
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
payload[fieldKey] = normalizedValue;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (fieldFormat === "date-time") {
|
|
332
|
+
const normalizedValue = toIsoUtcDateTimeValue(rawValue, field.temporalPrecision);
|
|
333
|
+
if (!normalizedValue) {
|
|
334
|
+
if (clearAsNull) {
|
|
335
|
+
payload[fieldKey] = null;
|
|
336
|
+
}
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
payload[fieldKey] = normalizedValue;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (fieldFormat === "time") {
|
|
345
|
+
const normalizedValue = toTimeInputValue(rawValue);
|
|
346
|
+
if (!normalizedValue) {
|
|
347
|
+
if (clearAsNull) {
|
|
348
|
+
payload[fieldKey] = null;
|
|
349
|
+
}
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
payload[fieldKey] = normalizedValue;
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
payload[fieldKey] = rawValue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return payload;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function applyCrudPayloadToForm(fields = [], model = {}, payload = {}) {
|
|
364
|
+
const targetModel = asPlainObject(model);
|
|
365
|
+
const sourcePayload = asPlainObject(payload);
|
|
366
|
+
for (const field of normalizeCrudFormFields(fields)) {
|
|
367
|
+
const fieldKey = field.key;
|
|
368
|
+
const fieldType = resolveFormFieldType(field);
|
|
369
|
+
const fieldFormat = resolveFormFieldFormat(field);
|
|
370
|
+
const rawValue = sourcePayload[fieldKey];
|
|
371
|
+
|
|
372
|
+
if (fieldType === "boolean") {
|
|
373
|
+
targetModel[fieldKey] = rawValue == null && isNullableFormField(field)
|
|
374
|
+
? null
|
|
375
|
+
: Boolean(rawValue);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (fieldType === "integer" || fieldType === "number") {
|
|
380
|
+
targetModel[fieldKey] = rawValue == null ? "" : String(rawValue);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (fieldFormat === "date") {
|
|
385
|
+
targetModel[fieldKey] = toDateInputValue(rawValue);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (fieldFormat === "date-time") {
|
|
390
|
+
targetModel[fieldKey] = toDateTimeLocalInputValue(rawValue);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (fieldFormat === "time") {
|
|
395
|
+
targetModel[fieldKey] = toTimeInputValue(rawValue);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (isLookupFormField(field)) {
|
|
400
|
+
targetModel[fieldKey] = rawValue == null ? null : String(rawValue);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
targetModel[fieldKey] = rawValue == null ? "" : String(rawValue);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function resolveCrudRouteBoundFieldValues(fields = [], routeParams = {}) {
|
|
409
|
+
const sourceRouteParams = asPlainObject(routeParams);
|
|
410
|
+
const values = {};
|
|
411
|
+
|
|
412
|
+
for (const field of normalizeCrudFormFields(fields)) {
|
|
413
|
+
const routeParamKey = String(field?.routeParamKey || "").trim();
|
|
414
|
+
if (!routeParamKey) {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (!Object.prototype.hasOwnProperty.call(sourceRouteParams, routeParamKey)) {
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const routeValue = toRouteParamValue(sourceRouteParams[routeParamKey]);
|
|
422
|
+
if (!routeValue) {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
values[field.key] = routeValue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return values;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function applyCrudRouteBoundFieldValues(fields = [], target = {}, routeParams = {}) {
|
|
432
|
+
const resolved = resolveCrudRouteBoundFieldValues(fields, routeParams);
|
|
433
|
+
const destination = asPlainObject(target);
|
|
434
|
+
for (const [key, value] of Object.entries(resolved)) {
|
|
435
|
+
destination[key] = value;
|
|
436
|
+
}
|
|
437
|
+
return resolved;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function resolveCrudFieldErrors(fieldErrors = {}, fieldKey = "") {
|
|
441
|
+
const key = String(fieldKey || "").trim();
|
|
442
|
+
if (!key) {
|
|
443
|
+
return [];
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const source = asPlainObject(fieldErrors);
|
|
447
|
+
const message = String(source[key] || "").trim();
|
|
448
|
+
|
|
449
|
+
if (!message) {
|
|
450
|
+
return resolveStableFieldErrorList(key, "");
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return resolveStableFieldErrorList(key, message);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export {
|
|
457
|
+
normalizeCrudFormFields,
|
|
458
|
+
createCrudFormModel,
|
|
459
|
+
buildCrudFormPayload,
|
|
460
|
+
applyCrudPayloadToForm,
|
|
461
|
+
resolveCrudRouteBoundFieldValues,
|
|
462
|
+
applyCrudRouteBoundFieldValues,
|
|
463
|
+
resolveCrudFieldErrors
|
|
464
|
+
};
|