-
Notifications
You must be signed in to change notification settings - Fork 159
/
deleteResources.js
181 lines (156 loc) · 5.73 KB
/
deleteResources.js
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
178
179
180
181
import { mapGetters, mapActions } from 'vuex'
import { cloneStateObject } from '../helpers/store'
const { default: PQueue } = require('p-queue')
export default {
data: () => ({
deleteResources_queue: new PQueue({ concurrency: 4 }),
deleteResources_deleteOps: []
}),
computed: {
...mapGetters('Files', ['selectedFiles']),
$_deleteResources_isInTrashbin() {
return this.$route.name === 'files-trashbin'
},
$_deleteResources_resources() {
return cloneStateObject(this.selectedFiles)
},
$_deleteResources_dialogTitle() {
const resources = this.$_deleteResources_resources
const isFolder = resources[0].type === 'folder'
let title = null
if (resources.length === 1) {
title = this.$_deleteResources_isInTrashbin
? this.$gettext('Permanently delete %{type} %{name}')
: this.$gettext('Delete %{type} %{name}')
return this.$gettextInterpolate(
title,
{
type: isFolder ? this.$gettext('folder') : this.$gettext('file'),
name: resources[0].name
},
true
)
}
title = this.$_deleteResources_isInTrashbin
? this.$gettext('Permanently delete %{amount} selected resources?')
: this.$gettext('Delete %{amount} selected resources')
return this.$gettextInterpolate(title, { amount: resources.length }, false)
},
$_deleteResources_dialogMessage() {
const resources = this.$_deleteResources_resources
const isFolder = resources[0].type === 'folder'
let message = null
if (resources.length === 1) {
message = this.$_deleteResources_isInTrashbin
? this.$gettext(
'Are you sure you want to delete this %{type}? All it’s content will be permanently removed. This action cannot be undone.'
)
: this.$gettext('Are you sure you want to delete this %{type}?')
return this.$gettextInterpolate(
message,
{ type: isFolder ? this.$gettext('folder') : this.$gettext('file') },
true
)
}
return this.$_deleteResources_isInTrashbin
? this.$gettext(
'Are you sure you want to delete all selected resources? All their content will be permanently removed. This action cannot be undone.'
)
: this.$gettext('Are you sure you want to delete all selected resources?')
}
},
methods: {
...mapActions('Files', [
'pushResourcesToDeleteList',
'removeFilesFromTrashbin',
'setHighlightedFile',
'resetFileSelection',
'addFileSelection',
'deleteFiles'
]),
...mapActions(['showMessage', 'toggleModalConfirmButton', 'hideModal', 'createModal']),
$_deleteResources_hideDialog() {
this.resetFileSelection()
this.setHighlightedFile(null)
this.hideModal()
},
$_deleteResources_trashbin_deleteOp(resource) {
return this.$client.fileTrash
.clearTrashBin(resource.id)
.then(() => {
this.removeFilesFromTrashbin([resource])
const translated = this.$gettext('%{file} was successfully deleted')
this.showMessage({
title: this.$gettextInterpolate(translated, { file: resource.name }, true)
})
})
.catch(error => {
if (error.statusCode === 423) {
// TODO: we need a may retry option ....
const p = this.deleteResources_queue.add(() => {
return this.$_deleteResources_trashbin_deleteOp(resource)
})
this.deleteResources_deleteOps.push(p)
return
}
const translated = this.$gettext('Deletion of %{file} failed')
this.showMessage({
title: this.$gettextInterpolate(translated, { file: resource.name }, true),
desc: error.message,
status: 'danger'
})
})
},
$_deleteResources_trashbin_delete() {
// TODO: use clear all if all files are selected
this.toggleModalConfirmButton()
for (const file of this.$_deleteResources_resources) {
const p = this.deleteResources_queue.add(() => {
return this.$_deleteResources_trashbin_deleteOp(file)
})
this.deleteResources_deleteOps.push(p)
}
Promise.all(this.deleteResources_deleteOps).then(() => {
this.$_deleteResources_hideDialog()
this.toggleModalConfirmButton()
})
},
$_deleteResources_filesList_delete() {
this.deleteFiles({
client: this.$client,
files: this.$_deleteResources_resources,
publicPage: this.publicPage(),
$gettext: this.$gettext,
$gettextInterpolate: this.$gettextInterpolate
}).then(() => {
this.$_deleteResources_hideDialog()
this.toggleModalConfirmButton()
})
},
$_deleteResources_delete() {
this.toggleModalConfirmButton()
this.$_deleteResources_isInTrashbin
? this.$_deleteResources_trashbin_delete()
: this.$_deleteResources_filesList_delete()
},
$_deleteResources_displayDialog(resources = null, direct = false) {
// Deleting a resource via direct action
// TODO: Do a direct delete without actually selecting the resource and resetting the selection
if (direct) {
this.resetFileSelection()
this.addFileSelection(resources)
}
const modal = {
variation: 'danger',
icon: 'warning',
title: this.$_deleteResources_dialogTitle,
message: this.$_deleteResources_dialogMessage,
cancelText: this.$gettext('Cancel'),
confirmText: this.$gettext('Delete'),
onCancel: this.$_deleteResources_hideDialog,
onConfirm: this.$_deleteResources_delete
}
this.createModal(modal)
}
}
}