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
|
<script>
import { GlSorting, GlSortingItem, GlFilteredSearch } from '@gitlab/ui';
import { SORT_DIRECTION_UI } from '~/search/sort/constants';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';
const ASCENDING_ORDER = 'asc';
const DESCENDING_ORDER = 'desc';
export default {
components: {
GlSorting,
GlSortingItem,
GlFilteredSearch,
},
props: {
filters: {
type: Array,
required: true,
},
sorting: {
type: Object,
required: true,
},
tokens: {
type: Array,
required: false,
default: () => [],
},
sortableFields: {
type: Array,
required: true,
},
},
computed: {
internalFilter: {
get() {
return this.filters;
},
set(value) {
this.$emit('filter:changed', value);
},
},
sortText() {
const field = this.sortableFields.find((s) => s.orderBy === this.sorting.orderBy);
return field ? field.label : '';
},
isSortAscending() {
return this.sorting.sort === ASCENDING_ORDER;
},
baselineQueryStringFilters() {
return this.tokens.reduce((acc, curr) => {
acc[curr.type] = '';
return acc;
}, {});
},
sortDirectionData() {
return this.isSortAscending ? SORT_DIRECTION_UI.asc : SORT_DIRECTION_UI.desc;
},
},
methods: {
generateQueryData({ sorting = {}, filter = [] } = {}) {
// Ensure that we clean up the query when we remove a token from the search
const result = { ...this.baselineQueryStringFilters, ...sorting, search: [] };
filter.forEach((f) => {
if (f.type === FILTERED_SEARCH_TERM) {
result.search.push(f.value.data);
} else {
result[f.type] = f.value.data;
}
});
return result;
},
onDirectionChange() {
const sort = this.isSortAscending ? DESCENDING_ORDER : ASCENDING_ORDER;
const newQueryString = this.generateQueryData({
sorting: { ...this.sorting, sort },
filter: this.filters,
});
this.$emit('sorting:changed', { sort });
this.$emit('query:changed', newQueryString);
},
onSortItemClick(item) {
const newQueryString = this.generateQueryData({
sorting: { ...this.sorting, orderBy: item },
filter: this.filters,
});
this.$emit('sorting:changed', { orderBy: item });
this.$emit('query:changed', newQueryString);
},
submitSearch() {
const newQueryString = this.generateQueryData({
sorting: this.sorting,
filter: this.filters,
});
this.$emit('filter:submit');
this.$emit('query:changed', newQueryString);
},
clearSearch() {
const newQueryString = this.generateQueryData({
sorting: this.sorting,
});
this.$emit('filter:changed', []);
this.$emit('filter:submit');
this.$emit('query:changed', newQueryString);
},
},
};
</script>
<template>
<div
class="gl-md-display-flex gl-p-5 gl-bg-gray-10 gl-border-solid gl-border-1 gl-border-gray-100"
>
<!-- `gl-w-full gl-md-w-15` forces fixed width needed to prevent
filtered component to grow beyond available width -->
<gl-filtered-search
v-model="internalFilter"
class="gl-w-full gl-md-w-15 gl-mr-4 gl-flex-grow-1"
:placeholder="__('Filter results')"
:available-tokens="tokens"
@submit="submitSearch"
@clear="clearSearch"
/>
<gl-sorting
data-testid="registry-sort-dropdown"
class="gl-mt-3 gl-md-mt-0 gl-w-full gl-md-w-auto"
dropdown-class="gl-w-full"
dropdown-toggle-class="gl-inset-border-1-gray-400!"
sort-direction-toggle-class="gl-inset-border-1-gray-400!"
:text="sortText"
:is-ascending="isSortAscending"
:sort-direction-tool-tip="sortDirectionData.tooltip"
@sortDirectionChange="onDirectionChange"
>
<gl-sorting-item
v-for="item in sortableFields"
ref="packageListSortItem"
:key="item.orderBy"
@click="onSortItemClick(item.orderBy)"
>
{{ item.label }}
</gl-sorting-item>
</gl-sorting>
</div>
</template>
|