generated from symbioquine/farmOS_vue_page_demo
/
EntitySelect.vue
153 lines (136 loc) · 4.14 KB
/
EntitySelect.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<script>
/**
* Provides a UI to search for and choose entities by name.
*
* ### Usage
*
* ```js
* <entity-select
* label="Parent Asset"
* entity-type="asset"
* v-model="parent"
* ></entity-select>
* ```
*
* @category components
* @vue-prop {String} [label=''] - the label to show in the select field
* @vue-prop {String} [hint=''] - the hint to show in the select field
* @vue-prop {String} entityType - the entity type to select from
* @vue-prop {String[]} [entityBundles=null] - the entity bundles to select from - or all if null
* @vue-prop {Boolean} [multiple=false] - allow selecting multiple entities
* @vue-prop {Object[]} [additionalFilters=[]] - Additional [Orbit.js filters]{@link https://orbitjs.com/docs/querying-data#attribute-filtering} to apply to the searches
* @vue-prop {Object} [modelValue=null] - The initially selected entity
* @vue-event {String} update:modelValue - Emitted when an entity has been selected
*/
export default {};
</script>
<script setup>
import { inject, ref, computed } from "vue";
import { v4 as uuidv4 } from "uuid";
import RacingLocalRemoteAsyncIterator from "../RacingLocalRemoteAsyncIterator";
const props = defineProps({
label: { type: String, default: "" },
hint: { type: String, default: "" },
entityType: { type: String, required: true },
entityBundles: { type: Array, default: null },
multiple: { type: Boolean, default: false },
additionalFilters: { type: Array, default: () => [] },
modelValue: { type: Object, default: null },
});
const emit = defineEmits(["update:modelValue"]);
const assetLink = inject("assetLink");
const model = computed({
get: () => props.modelValue,
set: (value) => emit("update:modelValue", value),
});
const options = ref([]);
const maxDesiredSearchEntries = 10;
const filterFn = async (val, update, abort) => {
const searchRequest = {
id: uuidv4(),
type: "text-search",
entityType: props.entityType,
entityBundles: props.entityBundles,
term: val,
};
searchRequest.additionalFilters = [
...(searchRequest.additionalFilters || []),
...props.additionalFilters,
];
let entitySearchResultCursor = assetLink.searchEntities(
searchRequest,
"local"
);
if (assetLink.connectionStatus.isOnline.value) {
entitySearchResultCursor = new RacingLocalRemoteAsyncIterator(
entitySearchResultCursor,
assetLink.searchEntities(searchRequest, "remote")
);
}
update(() => {
options.value = [];
});
const alreadyFoundEntityIds = new Set();
let searchIterItem = {};
while (
options.value.length < maxDesiredSearchEntries &&
!searchIterItem.done
) {
searchIterItem = await entitySearchResultCursor.next();
if (
searchIterItem.value &&
!alreadyFoundEntityIds.has(searchIterItem.value.entity.id)
) {
alreadyFoundEntityIds.add(searchIterItem.value.entity.id);
update(() => {
options.value.push(searchIterItem.value);
});
}
}
};
const abortFilterFn = () => {
console.log("delayed filter aborted");
};
</script>
<template>
<q-select
filled
dense
clearable
:modelValue="model && { entity: model }"
@update:modelValue="(item) => (model = item?.entity)"
:multiple="props.multiple"
:use-input="!model"
input-debounce="0"
:label="props.label"
:hint="props.hint"
:options="options"
@filter="filterFn"
@filter-abort="abortFilterFn"
>
<template v-slot:option="scope">
<q-item v-bind="scope.itemProps">
<q-item-section>
<q-item-label
><entity-name :entity="scope.opt.entity"></entity-name
></q-item-label>
<q-item-label caption>{{ scope.opt.weightText }}</q-item-label>
</q-item-section>
</q-item>
</template>
<template v-slot:selected-item="scope">
<q-item>
<q-item-section>
<q-item-label
><entity-name :entity="scope.opt.entity"></entity-name
></q-item-label>
</q-item-section>
</q-item>
</template>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey"> No results </q-item-section>
</q-item>
</template>
</q-select>
</template>