aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/mixins/filesSorting.ts
blob: 12515db103f5b6df37635002ce1fa9bc6d11dbdf (plain)
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
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import Vue from 'vue'

import { mapState } from 'pinia'
import { useViewConfigStore } from '../store/viewConfig'
import { useNavigation } from '../composables/useNavigation'

export default Vue.extend({
	setup() {
		const { currentView } = useNavigation()

		return {
			currentView,
		}
	},

	computed: {
		...mapState(useViewConfigStore, ['getConfig', 'setSortingBy', 'toggleSortingDirection']),

		/**
		 * Get the sorting mode for the current view
		 */
		sortingMode(): string {
			return this.getConfig(this.currentView.id)?.sorting_mode as string
				|| this.currentView?.defaultSortKey
				|| 'basename'
		},

		/**
		 * Get the sorting direction for the current view
		 */
		isAscSorting(): boolean {
			const sortingDirection = this.getConfig(this.currentView.id)?.sorting_direction
			return sortingDirection !== 'desc'
		},
	},

	methods: {
		toggleSortBy(key: string) {
			// If we're already sorting by this key, flip the direction
			if (this.sortingMode === key) {
				this.toggleSortingDirection(this.currentView.id)
				return
			}
			// else sort ASC by this new key
			this.setSortingBy(key, this.currentView.id)
		},
	},
})