aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/store/selection.ts
blob: fa35d953406108759cef6ab7ea0a4ad770be3fa2 (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
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { FileSource, SelectionStore } from '../types'
import { defineStore } from 'pinia'
import Vue from 'vue'

export const useSelectionStore = defineStore('selection', {
	state: () => ({
		selected: [],
		lastSelection: [],
		lastSelectedIndex: null,
	} as SelectionStore),

	actions: {
		/**
		 * Set the selection of fileIds
		 * @param selection
		 */
		set(selection = [] as FileSource[]) {
			Vue.set(this, 'selected', [...new Set(selection)])
		},

		/**
		 * Set the last selected index
		 * @param lastSelectedIndex
		 */
		setLastIndex(lastSelectedIndex = null as number | null) {
			// Update the last selection if we provided a new selection starting point
			Vue.set(this, 'lastSelection', lastSelectedIndex ? this.selected : [])
			Vue.set(this, 'lastSelectedIndex', lastSelectedIndex)
		},

		/**
		 * Reset the selection
		 */
		reset() {
			Vue.set(this, 'selected', [])
			Vue.set(this, 'lastSelection', [])
			Vue.set(this, 'lastSelectedIndex', null)
		},
	},
})