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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<!--
- @copyright 2022 Carl Schwan <carl@carlschwan.eu>
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<ul data-files-versions-versions-list>
<Version v-for="version in orderedVersions"
:key="version.mtime"
:version="version"
:file-info="fileInfo"
:is-current="version.mtime === fileInfo.mtime"
:is-first-version="version.mtime === initialVersionMtime"
@restore="handleRestore"
@label-update="handleLabelUpdate"
@delete="handleDelete" />
</ul>
</template>
<script>
import { showError, showSuccess } from '@nextcloud/dialogs'
import { fetchVersions, deleteVersion, restoreVersion, setVersionLabel } from '../utils/versions.js'
import Version from '../components/Version.vue'
export default {
name: 'VersionTab',
components: {
Version,
},
data() {
return {
fileInfo: null,
/** @type {import('../utils/versions.js').Version[]} */
versions: [],
loading: false,
}
},
computed: {
/**
* Order versions by mtime.
* Put the current version at the top.
*
* @return {import('../utils/versions.js').Version[]}
*/
orderedVersions() {
return [...this.versions].sort((a, b) => {
if (a.mtime === this.fileInfo.mtime) {
return -1
} else if (b.mtime === this.fileInfo.mtime) {
return 1
} else {
return b.mtime - a.mtime
}
})
},
/**
* Return the mtime of the first version to display "Initial version" label
* @return {number}
*/
initialVersionMtime() {
return this.versions
.map(version => version.mtime)
.reduce((a, b) => Math.min(a, b))
},
},
methods: {
/**
* Update current fileInfo and fetch new data
*
* @param {object} fileInfo the current file FileInfo
*/
async update(fileInfo) {
this.fileInfo = fileInfo
this.resetState()
this.fetchVersions()
},
/**
* Get the existing versions infos
*/
async fetchVersions() {
try {
this.loading = true
this.versions = await fetchVersions(this.fileInfo)
} finally {
this.loading = false
}
},
/**
* Handle restored event from Version.vue
*
* @param {import('../utils/versions.js').Version} version
*/
async handleRestore(version) {
// Update local copy of fileInfo as rendering depends on it.
const oldFileInfo = this.fileInfo
this.fileInfo = {
...this.fileInfo,
size: version.size,
mtime: version.mtime,
}
try {
await restoreVersion(version)
if (version.label !== '') {
showSuccess(t('files_versions', `${version.label} restored`))
} else if (version.mtime === this.initialVersionMtime) {
showSuccess(t('files_versions', 'Initial version restored'))
} else {
showSuccess(t('files_versions', 'Version restored'))
}
await this.fetchVersions()
} catch (exception) {
this.fileInfo = oldFileInfo
showError(t('files_versions', 'Could not restore version'))
}
},
/**
* Handle label-updated event from Version.vue
*
* @param {import('../utils/versions.js').Version} version
* @param {string} newName
*/
async handleLabelUpdate(version, newName) {
const oldLabel = version.label
version.label = newName
try {
await setVersionLabel(version, newName)
} catch (exception) {
version.label = oldLabel
showError(t('files_versions', 'Could not set version name'))
}
},
/**
* Handle deleted event from Version.vue
*
* @param {import('../utils/versions.js').Version} version
* @param {string} newName
*/
async handleDelete(version) {
const index = this.versions.indexOf(version)
this.versions.splice(index, 1)
try {
await deleteVersion(version)
} catch (exception) {
this.versions.push(version)
showError(t('files_versions', 'Could not delete version'))
}
},
/**
* Reset the current view to its default state
*/
resetState() {
this.$set(this, 'versions', [])
},
},
}
</script>
|