2023-10-12 04:05:02 -04:00
|
|
|
<!--
|
2024-05-28 10:42:42 -04:00
|
|
|
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
- SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
-->
|
2023-10-12 04:05:02 -04:00
|
|
|
<template>
|
2023-12-20 19:32:30 -05:00
|
|
|
<td
|
|
|
|
|
class="files-list__row-checkbox"
|
|
|
|
|
@keyup.esc.exact="resetSelection">
|
2024-08-22 19:22:33 -04:00
|
|
|
<NcLoadingIcon v-if="isLoading" :name="loadingLabel" />
|
2023-10-13 05:30:34 -04:00
|
|
|
<NcCheckboxRadioSwitch
|
|
|
|
|
v-else
|
2024-01-18 17:25:53 -05:00
|
|
|
:aria-label="ariaLabel"
|
2023-10-12 04:05:02 -04:00
|
|
|
:checked="isSelected"
|
2024-10-24 09:35:19 -04:00
|
|
|
data-cy-files-list-row-checkbox
|
2023-10-12 04:05:02 -04:00
|
|
|
@update:checked="onSelectionChange" />
|
|
|
|
|
</td>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
2024-06-24 18:24:22 -04:00
|
|
|
import type { Node } from '@nextcloud/files'
|
|
|
|
|
import type { PropType } from 'vue'
|
|
|
|
|
import type { FileSource } from '../../types.ts'
|
|
|
|
|
|
|
|
|
|
import { FileType } from '@nextcloud/files'
|
2023-10-12 04:05:02 -04:00
|
|
|
import { translate as t } from '@nextcloud/l10n'
|
2024-12-13 06:08:34 -05:00
|
|
|
import { useHotKey } from '@nextcloud/vue/composables/useHotKey'
|
2024-06-24 18:24:22 -04:00
|
|
|
import { defineComponent } from 'vue'
|
2023-10-12 04:05:02 -04:00
|
|
|
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
|
|
|
|
|
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
|
2024-06-10 15:21:26 -04:00
|
|
|
import logger from '../../logger.ts'
|
2024-12-13 06:08:34 -05:00
|
|
|
import { useActiveStore } from '../../store/active.ts'
|
2023-10-12 04:05:02 -04:00
|
|
|
import { useKeyboardStore } from '../../store/keyboard.ts'
|
|
|
|
|
import { useSelectionStore } from '../../store/selection.ts'
|
|
|
|
|
|
2024-02-01 13:00:46 -05:00
|
|
|
export default defineComponent({
|
2023-10-12 04:05:02 -04:00
|
|
|
name: 'FileEntryCheckbox',
|
|
|
|
|
|
|
|
|
|
components: {
|
|
|
|
|
NcCheckboxRadioSwitch,
|
|
|
|
|
NcLoadingIcon,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
props: {
|
|
|
|
|
fileid: {
|
2024-02-01 13:00:46 -05:00
|
|
|
type: Number,
|
2023-10-12 04:05:02 -04:00
|
|
|
required: true,
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2023-10-13 05:30:34 -04:00
|
|
|
isLoading: {
|
2023-10-12 04:05:02 -04:00
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
nodes: {
|
|
|
|
|
type: Array as PropType<Node[]>,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2024-01-18 17:25:53 -05:00
|
|
|
source: {
|
|
|
|
|
type: Object as PropType<Node>,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2023-10-12 04:05:02 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
|
const selectionStore = useSelectionStore()
|
|
|
|
|
const keyboardStore = useKeyboardStore()
|
2024-12-13 06:08:34 -05:00
|
|
|
const activeStore = useActiveStore()
|
|
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
return {
|
2024-12-13 06:08:34 -05:00
|
|
|
activeStore,
|
2023-10-12 04:05:02 -04:00
|
|
|
keyboardStore,
|
|
|
|
|
selectionStore,
|
2024-12-13 06:08:34 -05:00
|
|
|
t,
|
2023-10-12 04:05:02 -04:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
computed: {
|
2024-12-13 06:08:34 -05:00
|
|
|
isActive() {
|
|
|
|
|
return this.activeStore.activeNode?.source === this.source.source
|
|
|
|
|
},
|
|
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
selectedFiles() {
|
|
|
|
|
return this.selectionStore.selected
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
isSelected() {
|
2024-05-10 07:57:03 -04:00
|
|
|
return this.selectedFiles.includes(this.source.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
index() {
|
2024-05-10 07:57:03 -04:00
|
|
|
return this.nodes.findIndex((node: Node) => node.source === this.source.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2024-01-18 17:25:53 -05:00
|
|
|
isFile() {
|
|
|
|
|
return this.source.type === FileType.File
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2024-01-18 17:25:53 -05:00
|
|
|
ariaLabel() {
|
|
|
|
|
return this.isFile
|
|
|
|
|
? t('files', 'Toggle selection for file "{displayName}"', { displayName: this.source.basename })
|
|
|
|
|
: t('files', 'Toggle selection for folder "{displayName}"', { displayName: this.source.basename })
|
|
|
|
|
},
|
2025-10-01 10:03:40 -04:00
|
|
|
|
2024-08-22 19:22:33 -04:00
|
|
|
loadingLabel() {
|
|
|
|
|
return this.isFile
|
|
|
|
|
? t('files', 'File is loading')
|
|
|
|
|
: t('files', 'Folder is loading')
|
|
|
|
|
},
|
2023-10-12 04:05:02 -04:00
|
|
|
},
|
|
|
|
|
|
2024-12-13 06:08:34 -05:00
|
|
|
created() {
|
|
|
|
|
// ctrl+space toggle selection
|
|
|
|
|
useHotKey(' ', this.onToggleSelect, {
|
|
|
|
|
stop: true,
|
|
|
|
|
prevent: true,
|
|
|
|
|
ctrl: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// ctrl+shift+space toggle range selection
|
|
|
|
|
useHotKey(' ', this.onToggleSelect, {
|
|
|
|
|
stop: true,
|
|
|
|
|
prevent: true,
|
|
|
|
|
ctrl: true,
|
|
|
|
|
shift: true,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
2023-10-12 04:05:02 -04:00
|
|
|
methods: {
|
|
|
|
|
onSelectionChange(selected: boolean) {
|
|
|
|
|
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) {
|
2024-05-10 07:57:03 -04:00
|
|
|
const isAlreadySelected = this.selectedFiles.includes(this.source.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
|
|
|
|
|
const start = Math.min(newSelectedIndex, lastSelectedIndex)
|
|
|
|
|
const end = Math.max(lastSelectedIndex, newSelectedIndex)
|
|
|
|
|
|
|
|
|
|
const lastSelection = this.selectionStore.lastSelection
|
|
|
|
|
const filesToSelect = this.nodes
|
2024-05-10 07:57:03 -04:00
|
|
|
.map((file) => file.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
.slice(start, end + 1)
|
2024-05-10 07:57:03 -04:00
|
|
|
.filter(Boolean) as FileSource[]
|
2023-10-12 04:05:02 -04:00
|
|
|
|
|
|
|
|
// If already selected, update the new selection _without_ the current file
|
|
|
|
|
const selection = [...lastSelection, ...filesToSelect]
|
2024-05-10 07:57:03 -04:00
|
|
|
.filter((source) => !isAlreadySelected || source !== this.source.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
|
|
|
|
|
logger.debug('Shift key pressed, selecting all files in between', { start, end, filesToSelect, isAlreadySelected })
|
|
|
|
|
// Keep previous lastSelectedIndex to be use for further shift selections
|
|
|
|
|
this.selectionStore.set(selection)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selection = selected
|
2024-05-10 07:57:03 -04:00
|
|
|
? [...this.selectedFiles, this.source.source]
|
|
|
|
|
: this.selectedFiles.filter((source) => source !== this.source.source)
|
2023-10-12 04:05:02 -04:00
|
|
|
|
|
|
|
|
logger.debug('Updating selection', { selection })
|
|
|
|
|
this.selectionStore.set(selection)
|
|
|
|
|
this.selectionStore.setLastIndex(newSelectedIndex)
|
|
|
|
|
},
|
|
|
|
|
|
2023-12-20 19:32:30 -05:00
|
|
|
resetSelection() {
|
|
|
|
|
this.selectionStore.reset()
|
|
|
|
|
},
|
|
|
|
|
|
2024-12-13 06:08:34 -05:00
|
|
|
onToggleSelect() {
|
|
|
|
|
// Don't react if the node is not active
|
|
|
|
|
if (!this.isActive) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.debug('Toggling selection for file', { source: this.source })
|
|
|
|
|
this.onSelectionChange(!this.isSelected)
|
|
|
|
|
},
|
2023-10-12 04:05:02 -04:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
</script>
|