diff --git a/frontend/src/components/SortBy.vue b/frontend/src/components/SortBy.vue index cf44b430..7655c258 100644 --- a/frontend/src/components/SortBy.vue +++ b/frontend/src/components/SortBy.vue @@ -3,29 +3,29 @@ - + - {{ sortValues.length }} + {{ sortValues.size }} - - + + - + { + sort.direction = e.target.value + apply() + } + " placeholder="Sort by" /> @@ -50,7 +56,7 @@ Empty - Choose a field to sort by @@ -75,7 +81,7 @@ { + if (!list.value?.data) return new Set() + let allSortValues = list.value?.params?.order_by + if (!allSortValues || !sortOptions.data) return new Set() + if (allSortValues.trim() === 'modified desc') return new Set() + allSortValues = allSortValues.split(', ').map((sortValue) => { + const [fieldname, direction] = sortValue.split(' ') + return { fieldname, direction } + }) + return new Set(allSortValues) + }, + set: (value) => { + list.value.params.order_by = convertToString(value) + }, +}) + const options = computed(() => { if (!sortOptions.data) return [] - const selectedOptions = sortValues.value.map((sort) => sort.fieldname) + if (!sortValues.value.size) return sortOptions.data + const selectedOptions = [...sortValues.value].map((sort) => sort.fieldname) + restartSort() return sortOptions.data.filter((option) => { return !selectedOptions.includes(option.value) }) }) -function initialOrderBy() { - const orderBy = getOrderBy() - if (!orderBy) return [] - const sortOptions = orderBy.split(', ') - return sortOptions.map((sortOption) => { - const [fieldname, direction] = sortOption.split(' ') - return { fieldname, direction } - }) -} - const sortSortable = useSortable('#sort-list', sortValues, { handle: '.handle', animation: 200, + onEnd: () => apply(), }) -watch( - () => sortValues.value, - (value) => { - const updatedSort = value - .map((sort) => { - const option = sortOptions.data.find((o) => o.value === sort.fieldname) - return `${option.value} ${sort.direction}` - }) - .join(', ') - setOrderBy(updatedSort) - }, - { - deep: true, - } -) - -watch( - () => getOrderBy(), - (value) => { - if (!value) { - sortValues.value = [] - } - } -) - function setSort(data) { - sortValues.value = [ - ...sortValues.value, - { fieldname: data.value, direction: 'asc' }, - ] - sortSortable.start() + sortValues.value.add({ fieldname: data.value, direction: 'asc' }) + restartSort() + apply() } function updateSort(data, index) { - sortValues.value[index] = { + let oldSort = Array.from(sortValues.value)[index] + sortValues.value.delete(oldSort) + sortValues.value.add({ fieldname: data.value, - direction: sortValues.value[index].direction, - } + direction: oldSort.direction, + }) + apply() } function removeSort(index) { - sortValues.value.splice(index, 1) + sortValues.value.delete(Array.from(sortValues.value)[index]) + apply() } function clearSort(close) { - sortValues.value = [] + sortValues.value.clear() + apply() close() } + +function apply() { + nextTick(() => { + emit('update', convertToString(sortValues.value)) + }) +} + +function convertToString(values) { + let _sortValues = '' + values.forEach((f) => { + _sortValues += `${f.fieldname} ${f.direction}, ` + }) + _sortValues = _sortValues.slice(0, -2) + return _sortValues +} + +function restartSort() { + sortSortable.stop() + sortSortable.start() +} diff --git a/frontend/src/composables/orderby.js b/frontend/src/composables/orderby.js deleted file mode 100644 index dc0fb420..00000000 --- a/frontend/src/composables/orderby.js +++ /dev/null @@ -1,23 +0,0 @@ -import { useRoute, useRouter } from 'vue-router' - -export function useOrderBy() { - const route = useRoute() - const router = useRouter() - - function get() { - const q = route.query.sort ?? '' - const d = decodeURIComponent(q) - return d - } - - function set(sort, r) { - r = r || route - const q = encodeURIComponent(sort) - router.push({ ...r, query: { ...r.query, sort: q } }) - } - - return { - get, - set, - } -}