@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,147 @@
|
|
|
1
|
+
import { computed, ref } from "vue";
|
|
2
|
+
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import { defineCrudListBulkActions } from "../bulkActions.js";
|
|
4
|
+
|
|
5
|
+
function normalizeSelectedId(value = "") {
|
|
6
|
+
return normalizeText(value);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function resolveActionKey(actionOrKey = "") {
|
|
10
|
+
return normalizeText(
|
|
11
|
+
actionOrKey && typeof actionOrKey === "object"
|
|
12
|
+
? actionOrKey.key
|
|
13
|
+
: actionOrKey
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function useCrudListBulkActions(actions = [], {
|
|
18
|
+
resolveRecordId = null,
|
|
19
|
+
resolveContext = null
|
|
20
|
+
} = {}) {
|
|
21
|
+
const normalizedActions = defineCrudListBulkActions(actions);
|
|
22
|
+
const selectedIds = ref([]);
|
|
23
|
+
const selectedRecordMap = new Map();
|
|
24
|
+
const executingActionKey = ref("");
|
|
25
|
+
|
|
26
|
+
const selectedCount = computed(() => selectedIds.value.length);
|
|
27
|
+
const hasSelection = computed(() => selectedCount.value > 0);
|
|
28
|
+
const hasActions = computed(() => normalizedActions.length > 0);
|
|
29
|
+
|
|
30
|
+
function getRecordId(record = {}, index = 0) {
|
|
31
|
+
const resolvedId = typeof resolveRecordId === "function"
|
|
32
|
+
? resolveRecordId(record, index)
|
|
33
|
+
: record?.id ?? record?.attributes?.id ?? index;
|
|
34
|
+
return normalizeSelectedId(resolvedId);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function setRecordSelected(record = {}, index = 0, selected = true) {
|
|
38
|
+
const recordId = getRecordId(record, index);
|
|
39
|
+
if (!recordId) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const nextIds = new Set(selectedIds.value);
|
|
44
|
+
if (selected) {
|
|
45
|
+
nextIds.add(recordId);
|
|
46
|
+
selectedRecordMap.set(recordId, record);
|
|
47
|
+
} else {
|
|
48
|
+
nextIds.delete(recordId);
|
|
49
|
+
selectedRecordMap.delete(recordId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
selectedIds.value = Array.from(nextIds);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isRecordSelected(record = {}, index = 0) {
|
|
56
|
+
const recordId = getRecordId(record, index);
|
|
57
|
+
return recordId ? selectedIds.value.includes(recordId) : false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function setVisibleSelected(records = [], selected = true) {
|
|
61
|
+
for (const [index, record] of (Array.isArray(records) ? records : []).entries()) {
|
|
62
|
+
setRecordSelected(record, index, selected);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function allVisibleSelected(records = []) {
|
|
67
|
+
const visibleRecords = Array.isArray(records) ? records : [];
|
|
68
|
+
return visibleRecords.length > 0 && visibleRecords.every((record, index) => isRecordSelected(record, index));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function someVisibleSelected(records = []) {
|
|
72
|
+
return (Array.isArray(records) ? records : []).some((record, index) => isRecordSelected(record, index));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function clearSelection() {
|
|
76
|
+
selectedIds.value = [];
|
|
77
|
+
selectedRecordMap.clear();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function findAction(actionOrKey = "") {
|
|
81
|
+
const actionKey = resolveActionKey(actionOrKey);
|
|
82
|
+
return normalizedActions.find((action) => action.key === actionKey) || null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isActionExecuting(actionOrKey = "") {
|
|
86
|
+
const actionKey = resolveActionKey(actionOrKey);
|
|
87
|
+
return Boolean(actionKey && executingActionKey.value === actionKey);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function isActionDisabled(actionOrKey = "") {
|
|
91
|
+
const action = findAction(actionOrKey);
|
|
92
|
+
if (!action || !hasSelection.value || executingActionKey.value) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (typeof action.disabled === "function") {
|
|
96
|
+
return Boolean(action.disabled({
|
|
97
|
+
selectedIds: selectedIds.value.slice(),
|
|
98
|
+
selectedRecords: Array.from(selectedRecordMap.values()),
|
|
99
|
+
action
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
return Boolean(action.disabled);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function execute(actionOrKey = "") {
|
|
106
|
+
const action = findAction(actionOrKey);
|
|
107
|
+
if (!action || typeof action.run !== "function" || isActionDisabled(action)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
executingActionKey.value = action.key;
|
|
112
|
+
try {
|
|
113
|
+
const baseContext = typeof resolveContext === "function" ? resolveContext() : {};
|
|
114
|
+
return await action.run({
|
|
115
|
+
...(baseContext && typeof baseContext === "object" && !Array.isArray(baseContext) ? baseContext : {}),
|
|
116
|
+
action,
|
|
117
|
+
ids: selectedIds.value.slice(),
|
|
118
|
+
selectedIds: selectedIds.value.slice(),
|
|
119
|
+
selectedRecords: Array.from(selectedRecordMap.values()),
|
|
120
|
+
clearSelection
|
|
121
|
+
});
|
|
122
|
+
} finally {
|
|
123
|
+
executingActionKey.value = "";
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return Object.freeze({
|
|
128
|
+
actions: normalizedActions,
|
|
129
|
+
selectedIds,
|
|
130
|
+
selectedCount,
|
|
131
|
+
hasActions,
|
|
132
|
+
hasSelection,
|
|
133
|
+
executingActionKey,
|
|
134
|
+
setRecordSelected,
|
|
135
|
+
isRecordSelected,
|
|
136
|
+
setVisibleSelected,
|
|
137
|
+
allVisibleSelected,
|
|
138
|
+
someVisibleSelected,
|
|
139
|
+
clearSelection,
|
|
140
|
+
findAction,
|
|
141
|
+
isActionDisabled,
|
|
142
|
+
isActionExecuting,
|
|
143
|
+
execute
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { useCrudListBulkActions };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { computed, proxyRefs, ref, watch } from "vue";
|
|
2
|
+
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import {
|
|
4
|
+
defineCrudListFilters,
|
|
5
|
+
CRUD_LIST_FILTER_TYPE_RECORD_ID,
|
|
6
|
+
CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY
|
|
7
|
+
} from "@jskit-ai/resource-crud-core/shared/crudListFilters";
|
|
8
|
+
import { useList } from "./records/useList.js";
|
|
9
|
+
import {
|
|
10
|
+
normalizeLookupQueryKeyPrefix,
|
|
11
|
+
normalizeLookupLabelResolverMap,
|
|
12
|
+
normalizeLookupRequestQueryParamsMap,
|
|
13
|
+
resolveLookupSelectedValues,
|
|
14
|
+
createLookupOptionsFromItems,
|
|
15
|
+
mergeSelectedLookupOptions,
|
|
16
|
+
resolveLookupOptionLabel
|
|
17
|
+
} from "./internal/crudListFilterLookupSupport.js";
|
|
18
|
+
import { inferCrudLookupJsonApiTransport } from "./crud/crudJsonApiTransportSupport.js";
|
|
19
|
+
|
|
20
|
+
function useCrudListFilterLookups(
|
|
21
|
+
definitions = {},
|
|
22
|
+
{
|
|
23
|
+
values = {},
|
|
24
|
+
adapter = null,
|
|
25
|
+
recordIdParam = "recordId",
|
|
26
|
+
queryKeyPrefix = [],
|
|
27
|
+
placementSourcePrefix = "",
|
|
28
|
+
requestQueryParams = {},
|
|
29
|
+
labelResolvers = {}
|
|
30
|
+
} = {}
|
|
31
|
+
) {
|
|
32
|
+
const filters = defineCrudListFilters(definitions);
|
|
33
|
+
const filterEntries = Object.values(filters);
|
|
34
|
+
const normalizedQueryKeyPrefix = normalizeLookupQueryKeyPrefix(queryKeyPrefix);
|
|
35
|
+
const normalizedPlacementSourcePrefix = normalizeText(placementSourcePrefix);
|
|
36
|
+
const normalizedLabelResolvers = normalizeLookupLabelResolverMap(labelResolvers);
|
|
37
|
+
const normalizedRequestQueryParams = normalizeLookupRequestQueryParamsMap(requestQueryParams);
|
|
38
|
+
const lookups = {};
|
|
39
|
+
|
|
40
|
+
for (const filter of filterEntries) {
|
|
41
|
+
if (
|
|
42
|
+
filter.type !== CRUD_LIST_FILTER_TYPE_RECORD_ID &&
|
|
43
|
+
filter.type !== CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY
|
|
44
|
+
) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (!filter.lookup?.apiSuffix) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const transport = inferCrudLookupJsonApiTransport({
|
|
51
|
+
apiPath: filter.lookup.apiSuffix
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const runtime = useList({
|
|
55
|
+
adapter: adapter || undefined,
|
|
56
|
+
apiSuffix: filter.lookup.apiSuffix,
|
|
57
|
+
...(transport ? { transport } : {}),
|
|
58
|
+
queryKeyFactory: (surfaceId = "", scopeParamValue = "") => [
|
|
59
|
+
...normalizedQueryKeyPrefix,
|
|
60
|
+
filter.key,
|
|
61
|
+
String(surfaceId || ""),
|
|
62
|
+
String(scopeParamValue || "")
|
|
63
|
+
],
|
|
64
|
+
search: {
|
|
65
|
+
enabled: true,
|
|
66
|
+
mode: "query"
|
|
67
|
+
},
|
|
68
|
+
...(Object.hasOwn(normalizedRequestQueryParams, filter.key)
|
|
69
|
+
? { requestQueryParams: normalizedRequestQueryParams[filter.key] }
|
|
70
|
+
: {}),
|
|
71
|
+
placementSource: normalizedPlacementSourcePrefix
|
|
72
|
+
? `${normalizedPlacementSourcePrefix}.${filter.key}`
|
|
73
|
+
: `crud.list-filter.lookup.${filter.key}`,
|
|
74
|
+
fallbackLoadError: `Unable to load filter options (${filter.lookup.apiSuffix}).`,
|
|
75
|
+
recordIdParam,
|
|
76
|
+
recordIdSelector: (item = {}) => item[filter.lookup.valueKey || "id"],
|
|
77
|
+
viewUrlTemplate: "",
|
|
78
|
+
editUrlTemplate: ""
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const cachedOptions = ref(new Map());
|
|
82
|
+
const selectedValues = computed(() => resolveLookupSelectedValues(filter, values?.[filter.key]));
|
|
83
|
+
const currentOptions = computed(() => createLookupOptionsFromItems(
|
|
84
|
+
Array.isArray(runtime.items) ? runtime.items : [],
|
|
85
|
+
filter,
|
|
86
|
+
normalizedLabelResolvers[filter.key]
|
|
87
|
+
));
|
|
88
|
+
const options = computed(() => {
|
|
89
|
+
return mergeSelectedLookupOptions(
|
|
90
|
+
currentOptions.value,
|
|
91
|
+
selectedValues.value,
|
|
92
|
+
cachedOptions.value
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
watch(currentOptions, (nextOptions) => {
|
|
97
|
+
const nextCache = new Map(cachedOptions.value);
|
|
98
|
+
for (const option of nextOptions) {
|
|
99
|
+
nextCache.set(option.value, option);
|
|
100
|
+
}
|
|
101
|
+
cachedOptions.value = nextCache;
|
|
102
|
+
}, { immediate: true });
|
|
103
|
+
|
|
104
|
+
lookups[filter.key] = proxyRefs({
|
|
105
|
+
filter,
|
|
106
|
+
options,
|
|
107
|
+
searchQuery: computed(() => String(runtime.searchQuery || "")),
|
|
108
|
+
isLoading: computed(() => {
|
|
109
|
+
return Boolean(runtime.isInitialLoading || runtime.isFetching || runtime.isRefetching || runtime.isSearchDebouncing);
|
|
110
|
+
}),
|
|
111
|
+
resolveLabel(value, fallback = filter.label || "Option") {
|
|
112
|
+
return resolveLookupOptionLabel(options.value, cachedOptions.value, value, fallback);
|
|
113
|
+
},
|
|
114
|
+
setSearch(value = "") {
|
|
115
|
+
runtime.searchQuery = normalizeText(value);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function resolveLookup(filterKey = "") {
|
|
121
|
+
return lookups[normalizeText(filterKey)] || null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return Object.freeze({
|
|
125
|
+
lookups: Object.freeze(lookups),
|
|
126
|
+
resolveLookup,
|
|
127
|
+
resolveLookupItems(filterKey = "") {
|
|
128
|
+
return resolveLookup(filterKey)?.options || [];
|
|
129
|
+
},
|
|
130
|
+
resolveLookupLoading(filterKey = "") {
|
|
131
|
+
return Boolean(resolveLookup(filterKey)?.isLoading);
|
|
132
|
+
},
|
|
133
|
+
resolveLookupSearch(filterKey = "") {
|
|
134
|
+
return String(resolveLookup(filterKey)?.searchQuery || "");
|
|
135
|
+
},
|
|
136
|
+
setLookupSearch(filterKey = "", value = "") {
|
|
137
|
+
resolveLookup(filterKey)?.setSearch(value);
|
|
138
|
+
},
|
|
139
|
+
resolveLookupLabel(filterKey = "", value = "", fallback = "Option") {
|
|
140
|
+
return resolveLookup(filterKey)?.resolveLabel(value, fallback)
|
|
141
|
+
|| resolveLookupOptionLabel([], new Map(), value, fallback);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { useCrudListFilterLookups };
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { computed, reactive } from "vue";
|
|
2
|
+
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import {
|
|
4
|
+
defineCrudListFilters,
|
|
5
|
+
createCrudListFilterEmptyValue,
|
|
6
|
+
createCrudListFilterInitialValue,
|
|
7
|
+
isCrudListFilterMultiValue,
|
|
8
|
+
isCrudListFilterStructuredValue,
|
|
9
|
+
normalizeCrudListFilterUiValue,
|
|
10
|
+
areCrudListFilterUiValuesEqual,
|
|
11
|
+
listCrudListFilterChipValues,
|
|
12
|
+
formatCrudListFilterDefaultChipLabel,
|
|
13
|
+
formatCrudListFilterQueryValue,
|
|
14
|
+
parseCrudListFilterQueryValue,
|
|
15
|
+
resolveCrudListFilterOptionLabel,
|
|
16
|
+
INVALID_CRUD_LIST_FILTER_QUERY_VALUE,
|
|
17
|
+
CRUD_LIST_FILTER_TYPE_FLAG
|
|
18
|
+
} from "@jskit-ai/resource-crud-core/shared/crudListFilters";
|
|
19
|
+
import { formatCrudListDateFilterChipLabel } from "../support/crudListDateFilterSupport.js";
|
|
20
|
+
|
|
21
|
+
function normalizeFunctionMap(value = {}) {
|
|
22
|
+
const source = value && typeof value === "object" && !Array.isArray(value)
|
|
23
|
+
? value
|
|
24
|
+
: {};
|
|
25
|
+
const normalized = {};
|
|
26
|
+
|
|
27
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
28
|
+
const normalizedKey = normalizeText(key);
|
|
29
|
+
if (!normalizedKey || typeof entry !== "function") {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
normalized[normalizedKey] = entry;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return Object.freeze(normalized);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizePresetEntries(presets = []) {
|
|
40
|
+
const source = Array.isArray(presets) ? presets : [];
|
|
41
|
+
const normalized = [];
|
|
42
|
+
const seenKeys = new Set();
|
|
43
|
+
|
|
44
|
+
for (const rawPreset of source) {
|
|
45
|
+
if (!rawPreset || typeof rawPreset !== "object" || Array.isArray(rawPreset)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const key = normalizeText(rawPreset.key);
|
|
50
|
+
const label = normalizeText(rawPreset.label);
|
|
51
|
+
if (!key || !label || seenKeys.has(key)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
seenKeys.add(key);
|
|
55
|
+
|
|
56
|
+
const values = rawPreset.values && typeof rawPreset.values === "object" && !Array.isArray(rawPreset.values)
|
|
57
|
+
? rawPreset.values
|
|
58
|
+
: {};
|
|
59
|
+
const resolveValues = typeof rawPreset.resolveValues === "function"
|
|
60
|
+
? rawPreset.resolveValues
|
|
61
|
+
: null;
|
|
62
|
+
|
|
63
|
+
normalized.push(Object.freeze({
|
|
64
|
+
key,
|
|
65
|
+
label,
|
|
66
|
+
values,
|
|
67
|
+
resolveValues
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return Object.freeze(normalized);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolvePresetValues(preset = {}, { values = {}, filters = {} } = {}) {
|
|
75
|
+
const rawValues = typeof preset.resolveValues === "function"
|
|
76
|
+
? preset.resolveValues({
|
|
77
|
+
values,
|
|
78
|
+
filters,
|
|
79
|
+
presetKey: preset.key,
|
|
80
|
+
preset
|
|
81
|
+
})
|
|
82
|
+
: preset.values;
|
|
83
|
+
|
|
84
|
+
return rawValues && typeof rawValues === "object" && !Array.isArray(rawValues)
|
|
85
|
+
? rawValues
|
|
86
|
+
: {};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function createRuntimeFilterValue(filter = {}) {
|
|
90
|
+
const initialValue = createCrudListFilterInitialValue(filter);
|
|
91
|
+
return isCrudListFilterStructuredValue(filter)
|
|
92
|
+
? reactive(initialValue)
|
|
93
|
+
: initialValue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function assignFilterValue(values, filter = {}, rawValue) {
|
|
97
|
+
const normalizedValue = normalizeCrudListFilterUiValue(filter, rawValue);
|
|
98
|
+
|
|
99
|
+
if (!isCrudListFilterStructuredValue(filter)) {
|
|
100
|
+
values[filter.key] = normalizedValue;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (values[filter.key] && typeof values[filter.key] === "object" && !Array.isArray(values[filter.key])) {
|
|
105
|
+
Object.assign(values[filter.key], normalizedValue);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
values[filter.key] = reactive(normalizedValue);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function resetFilterValue(values, filter = {}) {
|
|
113
|
+
assignFilterValue(values, filter, createCrudListFilterEmptyValue(filter));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function applyPresetFilterValue(values, filter = {}, rawValue) {
|
|
117
|
+
assignFilterValue(values, filter, rawValue);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function createQueryParams(values, filterEntries = []) {
|
|
121
|
+
const queryParams = {};
|
|
122
|
+
|
|
123
|
+
for (const filter of filterEntries) {
|
|
124
|
+
queryParams[filter.queryKey] = computed({
|
|
125
|
+
get() {
|
|
126
|
+
return formatCrudListFilterQueryValue(
|
|
127
|
+
filter,
|
|
128
|
+
normalizeCrudListFilterUiValue(filter, values[filter.key])
|
|
129
|
+
);
|
|
130
|
+
},
|
|
131
|
+
set(nextValue) {
|
|
132
|
+
assignFilterValue(values, filter, nextValue);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return Object.freeze(queryParams);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function resolveFilterRouteValue(filter, routeValue, { initial = false } = {}) {
|
|
141
|
+
if (routeValue === undefined) {
|
|
142
|
+
return initial
|
|
143
|
+
? createCrudListFilterInitialValue(filter)
|
|
144
|
+
: createCrudListFilterEmptyValue(filter);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const parsedValue = parseCrudListFilterQueryValue(filter, routeValue);
|
|
148
|
+
return parsedValue === INVALID_CRUD_LIST_FILTER_QUERY_VALUE
|
|
149
|
+
? createCrudListFilterInitialValue(filter)
|
|
150
|
+
: parsedValue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function createRouteQueryValueResolvers(filterEntries = []) {
|
|
154
|
+
return Object.freeze(Object.fromEntries(
|
|
155
|
+
filterEntries.map((filter) => [
|
|
156
|
+
filter.queryKey,
|
|
157
|
+
(routeValue, context) => resolveFilterRouteValue(filter, routeValue, context)
|
|
158
|
+
])
|
|
159
|
+
));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function resolveAtomicValueLabel(filter = {}, value = "", labelResolvers = {}) {
|
|
163
|
+
const customResolver = labelResolvers[filter.key];
|
|
164
|
+
if (typeof customResolver === "function") {
|
|
165
|
+
const customLabel = normalizeText(customResolver(value, filter));
|
|
166
|
+
if (customLabel) {
|
|
167
|
+
return customLabel;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return resolveCrudListFilterOptionLabel(filter, value, {
|
|
172
|
+
fallback: String(value || "")
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function formatDefaultChipLabel(filter = {}, chipValue, labelResolvers = {}) {
|
|
177
|
+
return formatCrudListDateFilterChipLabel(filter, chipValue)
|
|
178
|
+
|| formatCrudListFilterDefaultChipLabel(filter, chipValue, {
|
|
179
|
+
resolveAtomicValue(value) {
|
|
180
|
+
return resolveAtomicValueLabel(filter, value, labelResolvers);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function useCrudListFilters(definitions = {}, { labelResolvers = {}, chipLabels = {}, presets = [] } = {}) {
|
|
186
|
+
const filters = defineCrudListFilters(definitions);
|
|
187
|
+
const filterEntries = Object.values(filters);
|
|
188
|
+
const normalizedLabelResolvers = normalizeFunctionMap(labelResolvers);
|
|
189
|
+
const normalizedChipLabels = normalizeFunctionMap(chipLabels);
|
|
190
|
+
const normalizedPresets = normalizePresetEntries(presets);
|
|
191
|
+
const values = reactive({});
|
|
192
|
+
const options = {};
|
|
193
|
+
|
|
194
|
+
for (const filter of filterEntries) {
|
|
195
|
+
values[filter.key] = createRuntimeFilterValue(filter);
|
|
196
|
+
if (Array.isArray(filter.options) && filter.options.length > 0) {
|
|
197
|
+
options[filter.key] = filter.options;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const queryParams = createQueryParams(values, filterEntries);
|
|
202
|
+
const routeQueryValueResolvers = createRouteQueryValueResolvers(filterEntries);
|
|
203
|
+
|
|
204
|
+
const activeChips = computed(() => {
|
|
205
|
+
const chips = [];
|
|
206
|
+
|
|
207
|
+
for (const filter of filterEntries) {
|
|
208
|
+
const customChipLabel = normalizedChipLabels[filter.key];
|
|
209
|
+
const chipValues = listCrudListFilterChipValues(filter, values[filter.key]);
|
|
210
|
+
|
|
211
|
+
for (const chipValue of chipValues) {
|
|
212
|
+
chips.push({
|
|
213
|
+
id: isCrudListFilterMultiValue(filter) ? `${filter.key}:${chipValue}` : filter.key,
|
|
214
|
+
filterKey: filter.key,
|
|
215
|
+
...(isCrudListFilterMultiValue(filter) ? { value: chipValue } : {}),
|
|
216
|
+
label: normalizeText(customChipLabel?.(chipValue, filter, values))
|
|
217
|
+
|| normalizeText(filter.chipLabel?.(chipValue, filter, values))
|
|
218
|
+
|| formatDefaultChipLabel(filter, chipValue, normalizedLabelResolvers)
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return chips;
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const hasActiveFilters = computed(() => activeChips.value.length > 0);
|
|
227
|
+
|
|
228
|
+
function clearFilter(filterKey = "") {
|
|
229
|
+
const filter = filters[normalizeText(filterKey)];
|
|
230
|
+
if (!filter) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
resetFilterValue(values, filter);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function clearFilters() {
|
|
238
|
+
for (const filter of filterEntries) {
|
|
239
|
+
resetFilterValue(values, filter);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function clearChip(chip = {}) {
|
|
244
|
+
const filter = filters[normalizeText(chip.filterKey)];
|
|
245
|
+
if (!filter) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (isCrudListFilterMultiValue(filter)) {
|
|
250
|
+
assignFilterValue(
|
|
251
|
+
values,
|
|
252
|
+
filter,
|
|
253
|
+
(Array.isArray(values[filter.key]) ? values[filter.key] : [])
|
|
254
|
+
.filter((entry) => entry !== chip.value)
|
|
255
|
+
);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
resetFilterValue(values, filter);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function toggle(filterKey = "") {
|
|
263
|
+
const filter = filters[normalizeText(filterKey)];
|
|
264
|
+
if (!filter || filter.type !== CRUD_LIST_FILTER_TYPE_FLAG) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
values[filter.key] = !values[filter.key];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function applyPreset(presetKey = "", { mode = "replace" } = {}) {
|
|
272
|
+
const preset = normalizedPresets.find((entry) => entry.key === normalizeText(presetKey));
|
|
273
|
+
if (!preset) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const presetValues = resolvePresetValues(preset, {
|
|
278
|
+
values,
|
|
279
|
+
filters
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
if (mode !== "merge") {
|
|
283
|
+
clearFilters();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (const filter of filterEntries) {
|
|
287
|
+
if (!Object.hasOwn(presetValues, filter.key)) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
applyPresetFilterValue(values, filter, presetValues[filter.key]);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function matchesPreset(presetKey = "") {
|
|
296
|
+
const preset = normalizedPresets.find((entry) => entry.key === normalizeText(presetKey));
|
|
297
|
+
if (!preset) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const presetValues = resolvePresetValues(preset, {
|
|
302
|
+
values,
|
|
303
|
+
filters
|
|
304
|
+
});
|
|
305
|
+
let matchedFilter = false;
|
|
306
|
+
|
|
307
|
+
for (const filter of filterEntries) {
|
|
308
|
+
if (!Object.hasOwn(presetValues, filter.key)) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
matchedFilter = true;
|
|
313
|
+
if (!areCrudListFilterUiValuesEqual(filter, values[filter.key], presetValues[filter.key])) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return matchedFilter;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return Object.freeze({
|
|
322
|
+
filters,
|
|
323
|
+
values,
|
|
324
|
+
queryParams,
|
|
325
|
+
routeQueryValueResolvers,
|
|
326
|
+
options: Object.freeze(options),
|
|
327
|
+
presets: normalizedPresets,
|
|
328
|
+
activeChips,
|
|
329
|
+
hasActiveFilters,
|
|
330
|
+
clearFilter,
|
|
331
|
+
clearFilters,
|
|
332
|
+
clearChip,
|
|
333
|
+
toggle,
|
|
334
|
+
applyPreset,
|
|
335
|
+
matchesPreset
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export { useCrudListFilters };
|