-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFileEntryCheckbox.vue
More file actions
111 lines (96 loc) · 3 KB
/
FileEntryCheckbox.vue
File metadata and controls
111 lines (96 loc) · 3 KB
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
<!--
- SPDX-FileCopyrightText: 2024 LibreCode coop and contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<td class="files-list__row-checkbox"
@keyup.esc.exact="resetSelection">
<NcLoadingIcon v-if="isLoading" :name="loadingLabel" />
<NcCheckboxRadioSwitch v-else
:aria-label="ariaLabel"
:checked="isSelected"
@update:checked="onSelectionChange" />
</td>
</template>
<script>
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import logger from '../../../logger.js'
import { useFilesStore } from '../../../store/files.js'
import { useKeyboardStore } from '../../../store/keyboard.js'
import { useSelectionStore } from '../../../store/selection.js'
export default {
name: 'FileEntryCheckbox',
components: {
NcCheckboxRadioSwitch,
NcLoadingIcon,
},
props: {
isLoading: {
type: Boolean,
default: false,
},
source: {
type: Object,
required: true,
},
},
setup() {
const filesStore = useFilesStore()
const keyboardStore = useKeyboardStore()
const selectionStore = useSelectionStore()
return {
filesStore,
keyboardStore,
selectionStore,
}
},
computed: {
selectedFiles() {
return this.selectionStore.selected
},
isSelected() {
return this.selectedFiles.includes(this.source.nodeId)
},
index() {
return this.filesStore.ordered.findIndex(nodeId => Number(nodeId) === this.source.nodeId)
},
ariaLabel() {
return t('libresign', 'Toggle selection for file "{displayName}"', { displayName: this.source.basename })
},
loadingLabel() {
return t('libresign', 'File is loading')
},
},
methods: {
onSelectionChange(selected) {
const newSelectedIndex = this.index
const lastSelectedIndex = this.selectionStore.lastSelectedIndex
// Get the last selected and select all files in between
if (this.keyboardStore?.shiftKey && lastSelectedIndex !== null) {
const isAlreadySelected = this.selectedFiles.includes(this.source.nodeId)
const start = Math.min(newSelectedIndex, lastSelectedIndex)
const end = Math.max(lastSelectedIndex, newSelectedIndex)
const lastSelection = this.selectionStore.lastSelection
const filesToSelect = this.filesStore.ordered
.slice(start, end + 1)
// If already selected, update the new selection _without_ the current file
const selection = [...new Set([...lastSelection, ...filesToSelect])]
.filter(nodeId => !isAlreadySelected || nodeId !== this.source.nodeId)
// Keep previous lastSelectedIndex to be use for further shift selections
this.selectionStore.set(selection)
return
}
const selection = selected
? [...this.selectedFiles, this.source.nodeId]
: this.selectedFiles.filter(nodeId => nodeId !== this.source.nodeId)
logger.debug('Updating selection', { selection })
this.selectionStore.set(selection)
this.selectionStore.setLastIndex(newSelectedIndex)
},
resetSelection() {
this.selectionStore.reset()
},
},
}
</script>