Skip to content

Commit

Permalink
refactor: Use composable for currentView and views to make it rea…
Browse files Browse the repository at this point in the history
…ctive when shared with other Vue apps

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux authored and AndyScherzinger committed Jul 10, 2024
1 parent 7a2fbe1 commit 57869f5
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 158 deletions.
20 changes: 12 additions & 8 deletions apps/files/src/components/BreadCrumbs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<script lang="ts">
import type { Node } from '@nextcloud/files'
import type { FileSource } from '../types.ts'

import { basename } from 'path'
import { defineComponent } from 'vue'
Expand All @@ -62,6 +63,7 @@ import NcBreadcrumb from '@nextcloud/vue/dist/Components/NcBreadcrumb.js'
import NcBreadcrumbs from '@nextcloud/vue/dist/Components/NcBreadcrumbs.js'
import NcIconSvgWrapper from '@nextcloud/vue/dist/Components/NcIconSvgWrapper.js'

import { useNavigation } from '../composables/useNavigation'
import { onDropInternalFiles, dataTransferToFileTree, onDropExternalFiles } from '../services/DropService'
import { showError } from '@nextcloud/dialogs'
import { useDragAndDropStore } from '../store/dragging.ts'
Expand All @@ -71,7 +73,6 @@ import { useSelectionStore } from '../store/selection.ts'
import { useUploaderStore } from '../store/uploader.ts'
import filesListWidthMixin from '../mixins/filesListWidth.ts'
import logger from '../logger'
import type { FileSource } from '../types.ts'

export default defineComponent({
name: 'BreadCrumbs',
Expand Down Expand Up @@ -99,21 +100,20 @@ export default defineComponent({
const pathsStore = usePathsStore()
const selectionStore = useSelectionStore()
const uploaderStore = useUploaderStore()
const { currentView } = useNavigation()

return {
draggingStore,
filesStore,
pathsStore,
selectionStore,
uploaderStore,

currentView,
}
},

computed: {
currentView() {
return this.$navigation.active
},

dirs(): string[] {
const cumulativePath = (acc: string) => (value: string) => (acc += `${value}/`)
// Generate a cumulative path for each path segment: ['/', '/foo', '/foo/bar', ...] etc
Expand Down Expand Up @@ -167,15 +167,15 @@ export default defineComponent({
getNodeFromSource(source: FileSource): Node | undefined {
return this.filesStore.getNode(source)
},
getFileSourceFromPath(path: string): FileSource | undefined {
return this.pathsStore.getPath(this.currentView?.id, path)
getFileSourceFromPath(path: string): FileSource | null {
return (this.currentView && this.pathsStore.getPath(this.currentView.id, path)) ?? null
},
getDirDisplayName(path: string): string {
if (path === '/') {
return this.$navigation?.active?.name || t('files', 'Home')
}

const source: FileSource | undefined = this.getFileSourceFromPath(path)
const source: FileSource | null = this.getFileSourceFromPath(path)
const node: Node | undefined = source ? this.getNodeFromSource(source) : undefined
return node?.attributes?.displayname || basename(path)
},
Expand All @@ -187,6 +187,10 @@ export default defineComponent({
},

onDragOver(event: DragEvent, path: string) {
if (!event.dataTransfer) {
return
}

// Cannot drop on the current directory
if (path === this.dirs[this.dirs.length - 1]) {
event.dataTransfer.dropEffect = 'none'
Expand Down
22 changes: 14 additions & 8 deletions apps/files/src/components/DragAndDropNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { Folder, Permission } from '@nextcloud/files'
import type { Folder } from '@nextcloud/files'
import { Permission } from '@nextcloud/files'
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { UploadStatus } from '@nextcloud/upload'
import { defineComponent, type PropType } from 'vue'

import TrayArrowDownIcon from 'vue-material-design-icons/TrayArrowDown.vue'

import logger from '../logger.js'
import { useNavigation } from '../composables/useNavigation'
import { dataTransferToFileTree, onDropExternalFiles } from '../services/DropService'
import logger from '../logger.js'

export default defineComponent({
name: 'DragAndDropNotice',
Expand All @@ -64,22 +66,26 @@ export default defineComponent({

props: {
currentFolder: {
type: Folder,
type: Object as PropType<Folder>,
required: true,
},
},

setup() {
const { currentView } = useNavigation()

return {
currentView,
}
},

data() {
return {
dragover: false,
}
},

computed: {
currentView() {
return this.$navigation.active
},

/**
* Check if the current folder has create permissions
*/
Expand Down
16 changes: 11 additions & 5 deletions apps/files/src/components/FileEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@

<script lang="ts">
import { defineComponent } from 'vue'
import { Permission, formatFileSize } from '@nextcloud/files'
import { formatFileSize } from '@nextcloud/files'
import moment from '@nextcloud/moment'

import { useNavigation } from '../composables/useNavigation'
import { useActionsMenuStore } from '../store/actionsmenu.ts'
import { useDragAndDropStore } from '../store/dragging.ts'
import { useFilesStore } from '../store/files.ts'
Expand Down Expand Up @@ -157,12 +158,16 @@ export default defineComponent({
const filesStore = useFilesStore()
const renamingStore = useRenamingStore()
const selectionStore = useSelectionStore()
const { currentView } = useNavigation()

return {
actionsMenuStore,
draggingStore,
filesStore,
renamingStore,
selectionStore,

currentView,
}
},

Expand Down Expand Up @@ -196,21 +201,22 @@ export default defineComponent({
},

size() {
const size = parseInt(this.source.size, 10)
if (typeof size !== 'number' || isNaN(size) || size < 0) {
const size = this.source.size
if (!size || size < 0) {
return this.t('files', 'Pending')
}
return formatFileSize(size, true)
},

sizeOpacity() {
const maxOpacitySize = 10 * 1024 * 1024

const size = parseInt(this.source.size, 10)
const size = this.source.size
if (!size || isNaN(size) || size < 0) {
return {}
}

const ratio = Math.round(Math.min(100, 100 * Math.pow((this.source.size / maxOpacitySize), 2)))
const ratio = Math.round(Math.min(100, 100 * Math.pow((size / maxOpacitySize), 2)))
return {
color: `color-mix(in srgb, var(--color-main-text) ${ratio}%, var(--color-text-maxcontrast))`,
}
Expand Down
24 changes: 16 additions & 8 deletions apps/files/src/components/FileEntry/FileEntryActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,22 @@
</template>

<script lang="ts">
import type { PropType } from 'vue'
import type { PropType, ShallowRef } from 'vue'
import type { FileAction, Node, View } from '@nextcloud/files'

import { DefaultType, FileAction, Node, NodeStatus, View, getFileActions } from '@nextcloud/files'
import { DefaultType, NodeStatus, getFileActions } from '@nextcloud/files'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { defineComponent } from 'vue'

import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcActionSeparator from '@nextcloud/vue/dist/Components/NcActionSeparator.js'
import NcIconSvgWrapper from '@nextcloud/vue/dist/Components/NcIconSvgWrapper.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import ArrowLeftIcon from 'vue-material-design-icons/ArrowLeft.vue'
import Vue, { defineComponent } from 'vue'

import { useNavigation } from '../../composables/useNavigation'
import CustomElementRender from '../CustomElementRender.vue'
import logger from '../../logger.js'

Expand Down Expand Up @@ -149,6 +151,15 @@ export default defineComponent({
},
},

setup() {
const { currentView } = useNavigation()

return {
// The file list is guaranteed to be only shown with active view
currentView: currentView as ShallowRef<View>,
}
},

data() {
return {
openedSubmenu: null as FileAction | null,
Expand All @@ -160,9 +171,6 @@ export default defineComponent({
// Remove any trailing slash but leave root slash
return (this.$route?.query?.dir?.toString() || '/').replace(/^(.+)\/$/, '$1')
},
currentView(): View {
return this.$navigation.active as View
},
isLoading() {
return this.source.status === NodeStatus.LOADING
},
Expand Down Expand Up @@ -286,7 +294,7 @@ export default defineComponent({
try {
// Set the loading marker
this.$emit('update:loading', action.id)
Vue.set(this.source, 'status', NodeStatus.LOADING)
this.$set(this.source, 'status', NodeStatus.LOADING)

const success = await action.exec(this.source, this.currentView, this.currentDir)

Expand All @@ -306,7 +314,7 @@ export default defineComponent({
} finally {
// Reset the loading marker
this.$emit('update:loading', '')
Vue.set(this.source, 'status', undefined)
this.$set(this.source, 'status', undefined)

// If that was a submenu, we just go back after the action
if (isSubmenu) {
Expand Down
6 changes: 6 additions & 0 deletions apps/files/src/components/FileEntry/FileEntryName.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</template>

<script lang="ts">
import type { Node } from '@nextcloud/files'
import type { PropType } from 'vue'

import { emit } from '@nextcloud/event-bus'
Expand All @@ -66,6 +67,7 @@ import Vue from 'vue'

import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'

import { useNavigation } from '../../composables/useNavigation'
import { useRenamingStore } from '../../store/renaming.ts'
import logger from '../../logger.js'

Expand Down Expand Up @@ -106,8 +108,12 @@ export default Vue.extend({
},

setup() {
const { currentView } = useNavigation()
const renamingStore = useRenamingStore()

return {
currentView,

renamingStore,
}
},
Expand Down
5 changes: 5 additions & 0 deletions apps/files/src/components/FileEntryGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<script lang="ts">
import { defineComponent } from 'vue'

import { useNavigation } from '../composables/useNavigation'
import { useActionsMenuStore } from '../store/actionsmenu.ts'
import { useDragAndDropStore } from '../store/dragging.ts'
import { useFilesStore } from '../store/files.ts'
Expand Down Expand Up @@ -110,12 +111,16 @@ export default defineComponent({
const filesStore = useFilesStore()
const renamingStore = useRenamingStore()
const selectionStore = useSelectionStore()
const { currentView } = useNavigation()

return {
actionsMenuStore,
draggingStore,
filesStore,
renamingStore,
selectionStore,

currentView,
}
},

Expand Down
4 changes: 0 additions & 4 deletions apps/files/src/components/FileEntryMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export default defineComponent({
},

computed: {
currentView(): View {
return this.$navigation.active as View
},

currentDir() {
// Remove any trailing slash but leave root slash
return (this.$route?.query?.dir?.toString() || '/').replace(/^(.+)\/$/, '$1')
Expand Down
20 changes: 12 additions & 8 deletions apps/files/src/components/FilesListTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@
</template>

<script lang="ts">
import type { Node } from '@nextcloud/files'
import type { PropType } from 'vue'
import type { FileSource } from '../types.ts'

import { translate as t } from '@nextcloud/l10n'
import { defineComponent } from 'vue'

import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import { defineComponent, type PropType } from 'vue'
import FilesListTableHeaderButton from './FilesListTableHeaderButton.vue'

import { useNavigation } from '../composables/useNavigation'
import { useFilesStore } from '../store/files.ts'
import { useSelectionStore } from '../store/selection.ts'
import FilesListTableHeaderButton from './FilesListTableHeaderButton.vue'
import filesSortingMixin from '../mixins/filesSorting.ts'
import logger from '../logger.js'
import type { Node } from '@nextcloud/files'
import type { FileSource } from '../types.ts'

export default defineComponent({
name: 'FilesListTableHeader',
Expand Down Expand Up @@ -117,17 +121,17 @@ export default defineComponent({
setup() {
const filesStore = useFilesStore()
const selectionStore = useSelectionStore()
const { currentView } = useNavigation()

return {
filesStore,
selectionStore,

currentView,
}
},

computed: {
currentView() {
return this.$navigation.active
},

columns() {
// Hide columns if the list is too small
if (this.filesListWidth < 512) {
Expand Down
Loading

0 comments on commit 57869f5

Please sign in to comment.