forked from directus/directus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-field-list.vue
142 lines (113 loc) · 3.82 KB
/
v-field-list.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
<template>
<v-list :mandatory="false" @toggle="loadFieldRelations($event.value)">
<slot name="prepend" />
<v-list-item v-if="fieldsCount > 20">
<v-list-item-content>
<v-input v-model="search" autofocus small :placeholder="t('search')" @click.stop>
<template #append>
<v-icon small name="search" />
</template>
</v-input>
</v-list-item-content>
</v-list-item>
<template v-if="allowSelectAll">
<v-list-item clickable :disabled="selectAllDisabled" @click="addAll">
{{ t('select_all') }}
</v-list-item>
<v-divider />
</template>
<v-field-list-item
v-for="fieldNode in treeList"
:key="fieldNode.field"
:field="fieldNode"
:search="search"
:include-functions="includeFunctions"
:relational-field-selectable="relationalFieldSelectable"
:allow-select-all="allowSelectAll"
:raw-field-names="rawFieldNames"
@add="$emit('add', $event)"
/>
</v-list>
</template>
<script setup lang="ts">
import { FieldNode, useFieldTree } from '@/composables/use-field-tree';
import { useFieldsStore } from '@/stores/fields';
import { Field } from '@directus/types';
import { debounce, isNil } from 'lodash';
import { computed, ref, toRefs, unref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import VFieldListItem from './v-field-list-item.vue';
interface Props {
collection: string;
field?: string;
disabledFields?: string[];
includeFunctions?: boolean;
includeRelations?: boolean;
relationalFieldSelectable?: boolean;
allowSelectAll?: boolean;
rawFieldNames?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
field: undefined,
disabledFields: () => [],
includeFunctions: false,
includeRelations: true,
relationalFieldSelectable: true,
allowSelectAll: false,
rawFieldNames: false,
});
const emit = defineEmits(['add']);
const fieldsStore = useFieldsStore();
const { collection, includeRelations } = toRefs(props);
const fieldsCount = computed(() => fieldsStore.getFieldsForCollection(collection.value)?.length ?? 0);
const search = ref('');
const { treeList: treeListOriginal, loadFieldRelations, refresh } = useFieldTree(collection, undefined, filter);
const debouncedRefresh = debounce(() => refresh(), 250);
watch(search, () => debouncedRefresh());
const { t } = useI18n();
const selectAllDisabled = computed(() => unref(treeList).every((field) => field.disabled === true));
const treeList = computed(() => {
const list = treeListOriginal.value.map(setDisabled);
if (props.field) return list.filter((fieldNode) => fieldNode.field === props.field);
return list;
function setDisabled(
field: (typeof treeListOriginal.value)[number]
): (typeof treeListOriginal.value)[number] & { disabled: boolean } {
let disabled = field.group || false;
if (props.disabledFields?.includes(field.key)) disabled = true;
return {
...field,
disabled,
children: field.children?.map(setDisabled),
};
}
});
const addAll = () => {
const allFields = unref(treeList).map((field) => field.field);
emit('add', unref(allFields));
};
function filter(field: Field, parent?: FieldNode): boolean {
if (
!includeRelations.value &&
(field.collection !== collection.value || (field.type === 'alias' && !field.meta?.special?.includes('group')))
) {
return false;
}
if (!search.value || isNil(parent) === false) return true;
const children = isNil(field.schema?.foreign_key_table)
? fieldsStore.getFieldGroupChildren(field.collection, field.field)
: fieldsStore.getFieldsForCollection(field.schema!.foreign_key_table);
return children?.some((field) => matchesSearch(field)) || matchesSearch(field);
function matchesSearch(field: Field) {
return (
field.field.toLowerCase().includes(search.value.toLowerCase()) ||
field.name.toLowerCase().includes(search.value.toLowerCase())
);
}
}
</script>
<style lang="scss" scoped>
.v-list {
--v-list-min-width: 300px;
}
</style>