|
42 | 42 | </template> |
43 | 43 |
|
44 | 44 | <script> |
45 | | - import map from 'lodash/map'; |
46 | | - import filter from 'lodash/filter'; |
47 | | - import isObject from 'lodash/isObject'; |
48 | | - import { mapState } from 'vuex'; |
49 | | - import FileManagerFileRow from '../components/filemanager/FileManagerFileRow'; |
50 | | - import FileManagerFolderRow from '../components/filemanager/FileManagerFolderRow'; |
51 | | -
|
52 | | - export default { |
53 | | - name: 'file-manager-page', |
54 | | - components: {FileManagerFolderRow, FileManagerFileRow}, |
55 | | -
|
56 | | - computed: { |
57 | | - ...mapState('server', ['server', 'credentials']), |
58 | | - ...mapState('socket', ['connected']), |
| 45 | +// @flow |
| 46 | +import map from 'lodash/map'; |
| 47 | +import { mapState } from 'vuex'; |
| 48 | +import type { Route } from 'vue-router'; |
| 49 | +import FileManagerFileRow from '../components/filemanager/FileManagerFileRow'; |
| 50 | +import FileManagerFolderRow from '../components/filemanager/FileManagerFolderRow'; |
| 51 | +import { getDirectoryContents, DirectoryContentsResponse } from '../../../api/server/getDirectoryContents'; |
| 52 | +
|
| 53 | +export default { |
| 54 | + name: 'file-manager-page', |
| 55 | + components: { FileManagerFolderRow, FileManagerFileRow }, |
| 56 | +
|
| 57 | + computed: { |
| 58 | + ...mapState('server', ['server', 'credentials']), |
| 59 | + ...mapState('socket', ['connected']), |
| 60 | +
|
| 61 | + /** |
| 62 | + * Configure the breadcrumbs that display on the filemanager based on the directory that the |
| 63 | + * user is currently in. |
| 64 | + */ |
| 65 | + breadcrumbs: function (): Array<Object> { |
| 66 | + const directories: Array<string> = this.currentDirectory.replace(/^\/|\/$/, '').split('/'); |
| 67 | + if (directories.length < 1 || !directories[0]) { |
| 68 | + return []; |
| 69 | + } |
59 | 70 |
|
60 | | - /** |
61 | | - * Configure the breadcrumbs that display on the filemanager based on the directory that the |
62 | | - * user is currently in. |
63 | | - */ |
64 | | - breadcrumbs: function () { |
65 | | - const directories = this.currentDirectory.replace(/^\/|\/$/, '').split('/'); |
66 | | - if (directories.length < 1 || !directories[0]) { |
67 | | - return []; |
| 71 | + return map(directories, function (value: string, key: number) { |
| 72 | + if (key === directories.length - 1) { |
| 73 | + return { directoryName: value }; |
68 | 74 | } |
69 | 75 |
|
70 | | - return map(directories, function (value, key) { |
71 | | - if (key === directories.length - 1) { |
72 | | - return {directoryName: value}; |
73 | | - } |
74 | | -
|
75 | | - return { |
76 | | - directoryName: value, |
77 | | - path: directories.slice(0, key + 1).join('/'), |
78 | | - }; |
79 | | - }); |
80 | | - } |
| 76 | + return { |
| 77 | + directoryName: value, |
| 78 | + path: directories.slice(0, key + 1).join('/'), |
| 79 | + }; |
| 80 | + }); |
| 81 | + }, |
| 82 | + }, |
| 83 | +
|
| 84 | + watch: { |
| 85 | + /** |
| 86 | + * When the route changes reload the directory. |
| 87 | + */ |
| 88 | + '$route': function (to: Route) { |
| 89 | + this.currentDirectory = to.params.path || '/'; |
81 | 90 | }, |
82 | 91 |
|
83 | | - watch: { |
84 | | - /** |
85 | | - * When the route changes reload the directory. |
86 | | - */ |
87 | | - '$route': function (to) { |
88 | | - this.currentDirectory = to.params.path || '/'; |
89 | | - }, |
| 92 | + /** |
| 93 | + * Watch the current directory setting and when it changes update the file listing. |
| 94 | + */ |
| 95 | + currentDirectory: function (): void { |
| 96 | + this.listDirectory(); |
| 97 | + }, |
90 | 98 |
|
91 | | - /** |
92 | | - * Watch the current directory setting and when it changes update the file listing. |
93 | | - */ |
94 | | - currentDirectory: function () { |
| 99 | + /** |
| 100 | + * When we reconnect to the Daemon make sure we grab a listing of all of the files |
| 101 | + * so that the error message disappears and we then load in a fresh listing. |
| 102 | + */ |
| 103 | + connected: function (): void { |
| 104 | + if (this.connected) { |
95 | 105 | this.listDirectory(); |
96 | | - }, |
97 | | -
|
98 | | - /** |
99 | | - * When we reconnect to the Daemon make sure we grab a listing of all of the files |
100 | | - * so that the error message disappears and we then load in a fresh listing. |
101 | | - */ |
102 | | - connected: function () { |
103 | | - if (this.connected) { |
104 | | - this.listDirectory(); |
105 | | - } |
106 | 106 | } |
107 | 107 | }, |
| 108 | + }, |
| 109 | +
|
| 110 | + data: function () { |
| 111 | + return { |
| 112 | + currentDirectory: this.$route.params.path || '/', |
| 113 | + loading: true, |
| 114 | + errorMessage: null, |
| 115 | +
|
| 116 | + directories: [], |
| 117 | + editableFiles: [], |
| 118 | + files: [], |
| 119 | + }; |
| 120 | + }, |
| 121 | +
|
| 122 | + mounted: function () { |
| 123 | + this.listDirectory(); |
| 124 | + }, |
| 125 | +
|
| 126 | + methods: { |
| 127 | + /** |
| 128 | + * List the contents of a directory. |
| 129 | + */ |
| 130 | + listDirectory: function (): void { |
| 131 | + this.loading = true; |
| 132 | +
|
| 133 | + const directory: string = encodeURI(this.currentDirectory.replace(/^\/|\/$/, '')); |
| 134 | + getDirectoryContents(this.$route.params.id, directory) |
| 135 | + .then((response: DirectoryContentsResponse) => { |
| 136 | + this.files = response.files; |
| 137 | + this.directories = response.directories; |
| 138 | + this.editableFiles = response.editable; |
| 139 | + this.errorMessage = null; |
| 140 | + }) |
| 141 | + .catch((err: string|Object) => { |
| 142 | + if (err instanceof String) { |
| 143 | + this.errorMessage = err; |
| 144 | + return; |
| 145 | + } |
108 | 146 |
|
109 | | - data: function () { |
110 | | - return { |
111 | | - currentDirectory: this.$route.params.path || '/', |
112 | | - loading: true, |
113 | | - errorMessage: null, |
114 | | -
|
115 | | - directories: [], |
116 | | - editableFiles: [], |
117 | | - files: [], |
118 | | - }; |
119 | | - }, |
120 | | -
|
121 | | - mounted: function () { |
122 | | - this.listDirectory(); |
| 147 | + console.error('An error was encountered while processing this request.', { err }); |
| 148 | + }) |
| 149 | + .then(() => { |
| 150 | + this.loading = false; |
| 151 | + }); |
123 | 152 | }, |
124 | | -
|
125 | | - methods: { |
126 | | - /** |
127 | | - * List the contents of a directory. |
128 | | - */ |
129 | | - listDirectory: function () { |
130 | | - this.loading = true; |
131 | | -
|
132 | | - window.axios.get(this.route('server.files', { |
133 | | - server: this.$route.params.id, |
134 | | - directory: encodeURI(this.currentDirectory.replace(/^\/|\/$/, '')), |
135 | | - })) |
136 | | - .then((response) => { |
137 | | - this.files = filter(response.data.contents, function (o) { |
138 | | - return o.file; |
139 | | - }); |
140 | | -
|
141 | | - this.directories = filter(response.data.contents, function (o) { |
142 | | - return o.directory; |
143 | | - }); |
144 | | -
|
145 | | - this.editableFiles = response.data.editable; |
146 | | - this.errorMessage = null; |
147 | | - }) |
148 | | - .catch(err => { |
149 | | - console.error({err}); |
150 | | - if (err.response.status === 404) { |
151 | | - this.errorMessage = 'The directory you requested could not be located on the server.'; |
152 | | - return; |
153 | | - } |
154 | | -
|
155 | | - if (err.response.data && isObject(err.response.data.errors)) { |
156 | | - err.response.data.errors.forEach(error => { |
157 | | - this.errorMessage = error.detail; |
158 | | - }); |
159 | | - } |
160 | | - }) |
161 | | - .finally(() => { |
162 | | - this.loading = false; |
163 | | - }); |
164 | | - }, |
165 | | - } |
166 | | - }; |
| 153 | + }, |
| 154 | +}; |
167 | 155 | </script> |
0 commit comments