@jskit-ai/users-web 0.1.172 → 0.1.174

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/package.json +18 -149
  2. package/src/client/composables/account-settings/accountSettingsAvatarUploadRuntime.js +2 -2
  3. package/src/client/composables/useAccountSettingsRuntime.js +3 -3
  4. package/src/client/index.js +0 -11
  5. package/test/exportsContract.test.js +3 -29
  6. package/test/settingsPlacementContract.test.js +10 -0
  7. package/fixtures/crud-list-date-filters/App.vue +0 -107
  8. package/fixtures/crud-list-date-filters/index.html +0 -12
  9. package/fixtures/crud-list-date-filters/main.js +0 -24
  10. package/fixtures/crud-list-date-filters/vite.config.mjs +0 -18
  11. package/src/client/bulkActions.js +0 -47
  12. package/src/client/components/CrudAddEditScreen.vue +0 -186
  13. package/src/client/components/CrudDeleteAction.vue +0 -93
  14. package/src/client/components/CrudListBulkActionSurface.vue +0 -126
  15. package/src/client/components/CrudListDateFilterControl.vue +0 -188
  16. package/src/client/components/CrudListFilterSurface.vue +0 -388
  17. package/src/client/components/CrudListRecordActionMenu.vue +0 -120
  18. package/src/client/components/CrudListScreen.vue +0 -501
  19. package/src/client/components/CrudViewScreen.vue +0 -197
  20. package/src/client/composables/crud/crudBindingSupport.js +0 -75
  21. package/src/client/composables/crud/crudHttpClientSupport.js +0 -57
  22. package/src/client/composables/crud/crudJsonApiTransportSupport.js +0 -145
  23. package/src/client/composables/crud/crudLookupFieldLabelSupport.js +0 -151
  24. package/src/client/composables/crud/crudLookupFieldRuntime.js +0 -251
  25. package/src/client/composables/crud/crudSchemaFormHelpers.js +0 -464
  26. package/src/client/composables/internal/crudListFilterLookupSupport.js +0 -161
  27. package/src/client/composables/internal/crudListParentTitleSupport.js +0 -171
  28. package/src/client/composables/internal/useOperationScope.js +0 -144
  29. package/src/client/composables/records/useAddEdit.js +0 -224
  30. package/src/client/composables/records/useCrudAddEdit.js +0 -227
  31. package/src/client/composables/records/useCrudList.js +0 -101
  32. package/src/client/composables/records/useCrudView.js +0 -47
  33. package/src/client/composables/records/useList.js +0 -500
  34. package/src/client/composables/records/useView.js +0 -164
  35. package/src/client/composables/runtime/addEditUiRuntime.js +0 -130
  36. package/src/client/composables/runtime/listUiRuntime.js +0 -123
  37. package/src/client/composables/runtime/modelStateHelpers.js +0 -90
  38. package/src/client/composables/runtime/operationAdapters.js +0 -34
  39. package/src/client/composables/runtime/operationUiHelpers.js +0 -125
  40. package/src/client/composables/runtime/operationValidationHelpers.js +0 -51
  41. package/src/client/composables/runtime/useAddEditCore.js +0 -112
  42. package/src/client/composables/runtime/useCommandCore.js +0 -123
  43. package/src/client/composables/runtime/useEndpointResource.js +0 -199
  44. package/src/client/composables/runtime/useFieldErrorBag.js +0 -61
  45. package/src/client/composables/runtime/useListCore.js +0 -112
  46. package/src/client/composables/runtime/useUiFeedback.js +0 -98
  47. package/src/client/composables/runtime/useViewCore.js +0 -93
  48. package/src/client/composables/runtime/viewUiRuntime.js +0 -96
  49. package/src/client/composables/support/errorMessageHelpers.js +0 -66
  50. package/src/client/composables/support/listQueryParamSupport.js +0 -473
  51. package/src/client/composables/support/listSearchSupport.js +0 -70
  52. package/src/client/composables/support/refValueHelpers.js +0 -19
  53. package/src/client/composables/support/requestQueryRuntimeSupport.js +0 -141
  54. package/src/client/composables/support/resourceLoadStateHelpers.js +0 -208
  55. package/src/client/composables/support/routeTemplateHelpers.js +0 -285
  56. package/src/client/composables/support/scopeHelpers.js +0 -145
  57. package/src/client/composables/useAccess.js +0 -109
  58. package/src/client/composables/useCommand.js +0 -118
  59. package/src/client/composables/useCrudAddEditScreen.js +0 -88
  60. package/src/client/composables/useCrudDeleteAction.js +0 -160
  61. package/src/client/composables/useCrudListBulkActions.js +0 -147
  62. package/src/client/composables/useCrudListFilterLookups.js +0 -146
  63. package/src/client/composables/useCrudListFilters.js +0 -339
  64. package/src/client/composables/useCrudListParentTitle.js +0 -151
  65. package/src/client/composables/useCrudListRowActions.js +0 -144
  66. package/src/client/composables/useCrudListScreen.js +0 -237
  67. package/src/client/composables/useCrudViewScreen.js +0 -77
  68. package/src/client/composables/usePagedCollection.js +0 -181
  69. package/src/client/composables/useRealtimeQueryInvalidation.js +0 -156
  70. package/src/client/composables/useScopeRuntime.js +0 -135
  71. package/src/client/filters.js +0 -15
  72. package/src/client/lib/httpClient.js +0 -47
  73. package/src/client/lib/permissions.js +0 -34
  74. package/src/client/rowActions.js +0 -47
  75. package/src/client/support/contractGuards.js +0 -34
  76. package/src/client/support/crudListDateFilterSupport.js +0 -91
  77. package/test/addEditUiRuntime.test.js +0 -110
  78. package/test/crudBindingSupport.test.js +0 -110
  79. package/test/crudHttpClientSupport.test.js +0 -77
  80. package/test/crudJsonApiTransportSupport.test.js +0 -179
  81. package/test/crudListBulkActionSurface.test.js +0 -27
  82. package/test/crudListDateFilterSupport.test.js +0 -92
  83. package/test/crudListDateFilterSurface.browser.test.js +0 -182
  84. package/test/crudListFilterSurface.test.js +0 -90
  85. package/test/crudLookupFieldRuntime.test.js +0 -264
  86. package/test/crudScreenComponents.test.js +0 -117
  87. package/test/errorIntentContract.test.js +0 -31
  88. package/test/errorMessageHelpers.test.js +0 -28
  89. package/test/listQueryParamSupport.test.js +0 -223
  90. package/test/listUiRuntime.test.js +0 -102
  91. package/test/operationValidationHelpers.test.js +0 -64
  92. package/test/permissions.test.js +0 -35
  93. package/test/refValueHelpers.test.js +0 -14
  94. package/test/requestTransportOptions.test.js +0 -457
  95. package/test/resourceLoadStateHelpers.test.js +0 -189
  96. package/test/routeTemplateHelpers.test.js +0 -85
  97. package/test/scopeHelpers.test.js +0 -57
  98. package/test/useAddEditCore.test.js +0 -124
  99. package/test/useAddEditRequestQueryParams.test.js +0 -190
  100. package/test/useCrudAddEdit.test.js +0 -415
  101. package/test/useCrudDeleteAction.test.js +0 -139
  102. package/test/useCrudListBulkActions.test.js +0 -65
  103. package/test/useCrudListFilterLookups.test.js +0 -114
  104. package/test/useCrudListFilters.test.js +0 -293
  105. package/test/useCrudListParentTitle.test.js +0 -262
  106. package/test/useCrudListRowActions.test.js +0 -77
  107. package/test/useListRouteInitialization.test.js +0 -337
  108. package/test/useListSearchSupport.test.js +0 -61
  109. package/test/usePagedCollection.test.js +0 -53
  110. package/test/useViewRequestQueryParams.test.js +0 -20
  111. package/test/viewCoreLoading.test.js +0 -44
  112. package/test/viewUiRuntime.test.js +0 -95
@@ -1,147 +0,0 @@
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 };
@@ -1,146 +0,0 @@
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/kernel/shared/support/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 };
@@ -1,339 +0,0 @@
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/kernel/shared/support/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 };