2020-12-08 09:09:41 +00:00
|
|
|
<script>
|
2020-12-10 18:10:16 +00:00
|
|
|
import { GlSorting, GlSortingItem } from '@gitlab/ui';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { mapState } from 'vuex';
|
2020-12-10 18:10:16 +00:00
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
2020-12-08 09:09:41 +00:00
|
|
|
import { FIELDS } from '~/members/constants';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { parseSortParam, buildSortHref } from '~/members/utils';
|
2020-12-08 09:09:41 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'SortDropdown',
|
2020-12-10 18:10:16 +00:00
|
|
|
components: { GlSorting, GlSortingItem },
|
2021-04-13 21:11:25 +00:00
|
|
|
inject: ['namespace'],
|
2020-12-08 09:09:41 +00:00
|
|
|
computed: {
|
2021-04-13 21:11:25 +00:00
|
|
|
...mapState({
|
|
|
|
tableSortableFields(state) {
|
|
|
|
return state[this.namespace].tableSortableFields;
|
|
|
|
},
|
|
|
|
filteredSearchBar(state) {
|
|
|
|
return state[this.namespace].filteredSearchBar;
|
|
|
|
},
|
|
|
|
}),
|
2020-12-08 09:09:41 +00:00
|
|
|
sort() {
|
|
|
|
return parseSortParam(this.tableSortableFields);
|
|
|
|
},
|
2020-12-10 18:10:16 +00:00
|
|
|
activeOption() {
|
2020-12-23 21:10:24 +00:00
|
|
|
return FIELDS.find((field) => field.key === this.sort.sortByKey);
|
2020-12-10 18:10:16 +00:00
|
|
|
},
|
|
|
|
activeOptionLabel() {
|
|
|
|
return this.activeOption?.label;
|
|
|
|
},
|
|
|
|
isAscending() {
|
|
|
|
return !this.sort.sortDesc;
|
|
|
|
},
|
2020-12-08 09:09:41 +00:00
|
|
|
filteredOptions() {
|
2020-12-23 21:10:24 +00:00
|
|
|
return FIELDS.filter(
|
|
|
|
(field) => this.tableSortableFields.includes(field.key) && field.sort,
|
|
|
|
).map((field) => ({
|
|
|
|
key: field.key,
|
|
|
|
label: field.label,
|
|
|
|
href: buildSortHref({
|
|
|
|
sortBy: field.key,
|
|
|
|
sortDesc: false,
|
|
|
|
filteredSearchBarTokens: this.filteredSearchBar.tokens,
|
|
|
|
filteredSearchBarSearchParam: this.filteredSearchBar.searchParam,
|
2020-12-08 09:09:41 +00:00
|
|
|
}),
|
2020-12-23 21:10:24 +00:00
|
|
|
}));
|
2020-12-08 09:09:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2020-12-10 18:10:16 +00:00
|
|
|
isActive(key) {
|
|
|
|
return this.activeOption.key === key;
|
|
|
|
},
|
|
|
|
handleSortDirectionChange() {
|
|
|
|
visitUrl(
|
|
|
|
buildSortHref({
|
|
|
|
sortBy: this.activeOption.key,
|
|
|
|
sortDesc: !this.sort.sortDesc,
|
|
|
|
filteredSearchBarTokens: this.filteredSearchBar.tokens,
|
|
|
|
filteredSearchBarSearchParam: this.filteredSearchBar.searchParam,
|
|
|
|
}),
|
|
|
|
);
|
2020-12-08 09:09:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2020-12-10 18:10:16 +00:00
|
|
|
<gl-sorting
|
|
|
|
class="gl-display-flex"
|
|
|
|
dropdown-class="gl-w-full"
|
|
|
|
data-testid="members-sort-dropdown"
|
|
|
|
:text="activeOptionLabel"
|
|
|
|
:is-ascending="isAscending"
|
|
|
|
:sort-direction-tool-tip="__('Sort direction')"
|
|
|
|
@sortDirectionChange="handleSortDirectionChange"
|
2020-12-08 09:09:41 +00:00
|
|
|
>
|
2020-12-10 18:10:16 +00:00
|
|
|
<gl-sorting-item
|
|
|
|
v-for="option in filteredOptions"
|
|
|
|
:key="option.key"
|
|
|
|
:href="option.href"
|
|
|
|
:active="isActive(option.key)"
|
2020-12-08 09:09:41 +00:00
|
|
|
>
|
2020-12-10 18:10:16 +00:00
|
|
|
{{ option.label }}
|
|
|
|
</gl-sorting-item>
|
|
|
|
</gl-sorting>
|
2020-12-08 09:09:41 +00:00
|
|
|
</template>
|