import Vue from 'vue'; import {mapState} from "vuex"; import { map } from 'lodash'; import getDirectoryContents from "@/api/server/getDirectoryContents"; import FileRow from "@/components/server/components/filemanager/FileRow"; import FolderRow from "@/components/server/components/filemanager/FolderRow"; type DataStructure = { loading: boolean, errorMessage: string | null, currentDirectory: string, files: Array, directories: Array, editableFiles: Array, } export default Vue.component('file-manager', { components: { FileRow, FolderRow }, computed: { ...mapState('server', ['server', 'credentials']), ...mapState('socket', ['connected']), /** * Configure the breadcrumbs that display on the filemanager based on the directory that the * user is currently in. */ breadcrumbs: function () { const directories = this.currentDirectory.replace(/^\/|\/$/, '').split('/'); if (directories.length < 1 || !directories[0]) { return []; } return map(directories, function (value: string, key: number) { if (key === directories.length - 1) { return { directoryName: value }; } return { directoryName: value, path: directories.slice(0, key + 1).join('/'), }; }); }, }, watch: { /** * When the route changes reload the directory. */ '$route': function (to) { this.currentDirectory = to.params.path || '/'; }, /** * Watch the current directory setting and when it changes update the file listing. */ currentDirectory: function () { this.listDirectory(); }, /** * When we reconnect to the Daemon make sure we grab a listing of all of the files * so that the error message disappears and we then load in a fresh listing. */ connected: function () { // @ts-ignore if (this.connected) { this.listDirectory(); } }, }, data: function (): DataStructure { return { currentDirectory: this.$route.params.path || '/', loading: true, errorMessage: null, directories: [], editableFiles: [], files: [], }; }, mounted: function () { this.listDirectory(); }, methods: { /** * List the contents of a directory. */ listDirectory: function () { this.loading = true; const directory = encodeURI(this.currentDirectory.replace(/^\/|\/$/, '')); getDirectoryContents(this.$route.params.id, directory) .then((response) => { this.files = response.files; this.directories = response.directories; this.editableFiles = response.editable; this.errorMessage = null; }) .catch((err) => { if (typeof err === 'string') { this.errorMessage = err; return; } console.error('An error was encountered while processing this request.', { err }); }) .then(() => { this.loading = false; }); }, }, template: `
/home/container /{{crumb.directoryName}} /{{crumb.directoryName}}

This directory is empty.

Name
Size
Modified
Actions
`, });