-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathAppNavigationForm.vue
More file actions
335 lines (311 loc) Β· 7.59 KB
/
AppNavigationForm.vue
File metadata and controls
335 lines (311 loc) Β· 7.59 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!--
- SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcListItem
ref="navigationItem"
:active="isActive"
:actions-aria-label="t('forms', 'Form actions')"
:counter-number="form.submissionCount"
compact
:force-display-actions="forceDisplayActions"
:name="formTitle"
:to="{
name: routerTarget,
params: { hash: form.hash },
}"
@click="mobileCloseNavigation">
<template #icon>
<NcLoadingIcon v-if="loading" :size="16" />
<IconCheck v-else-if="isExpired" :size="16" />
<FormsIcon v-else :size="16" />
</template>
<template v-if="hasSubtitle" #subname>
{{ formSubtitle }}
</template>
<template v-if="!loading && !readOnly" #actions>
<NcActionRouter
v-if="!isArchived"
close-after-click
exact
:to="{ name: 'edit', params: { hash: form.hash } }"
@click="mobileCloseNavigation">
<template #icon>
<IconPencil :size="20" />
</template>
{{ t('forms', 'Edit form') }}
</NcActionRouter>
<NcActionButton
v-if="!isArchived"
close-after-click
@click="onShareForm">
<template #icon>
<IconShareVariant :size="20" />
</template>
{{ t('forms', 'Share form') }}
</NcActionButton>
<NcActionRouter
close-after-click
exact
:to="{ name: 'results', params: { hash: form.hash } }"
@click="mobileCloseNavigation">
<template #icon>
<IconPoll :size="20" />
</template>
{{ t('forms', 'Results') }}
</NcActionRouter>
<NcActionButton v-if="canEdit" close-after-click @click="onCloneForm">
<template #icon>
<IconContentCopy :size="20" />
</template>
{{ t('forms', 'Copy form') }}
</NcActionButton>
<NcActionSeparator v-if="canEdit" />
<NcActionButton
v-if="canEdit"
close-after-click
@click="onToggleArchive">
<template #icon>
<IconArchiveOff v-if="isArchived" :size="20" />
<IconArchive v-else :size="20" />
</template>
{{
isArchived
? t('forms', 'Unarchive form')
: t('forms', 'Archive form')
}}
</NcActionButton>
<NcActionButton
v-if="canEdit"
close-after-click
@click="showDeleteDialog = true">
<template #icon>
<IconDelete :size="20" />
</template>
{{ t('forms', 'Delete form') }}
</NcActionButton>
<NcDialog
:open.sync="showDeleteDialog"
:name="t('forms', 'Delete form')"
:message="
t('forms', 'Are you sure you want to delete {title}?', {
title: formTitle,
})
"
:buttons="buttons" />
</template>
</NcListItem>
</template>
<script>
import { generateOcsUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
import NcActionRouter from '@nextcloud/vue/components/NcActionRouter'
import NcActionSeparator from '@nextcloud/vue/components/NcActionSeparator'
import NcDialog from '@nextcloud/vue/components/NcDialog'
import NcListItem from '@nextcloud/vue/components/NcListItem'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import axios from '@nextcloud/axios'
import moment from '@nextcloud/moment'
import IconArchive from 'vue-material-design-icons/Archive.vue'
import IconArchiveOff from 'vue-material-design-icons/ArchiveOff.vue'
import IconCheck from 'vue-material-design-icons/Check.vue'
import IconContentCopy from 'vue-material-design-icons/ContentCopy.vue'
import IconDelete from 'vue-material-design-icons/Delete.vue'
import IconPencil from 'vue-material-design-icons/Pencil.vue'
import IconPoll from 'vue-material-design-icons/Poll.vue'
import IconShareVariant from 'vue-material-design-icons/ShareVariant.vue'
import IconDeleteSvg from '@mdi/svg/svg/delete.svg?raw'
import FormsIcon from './Icons/FormsIcon.vue'
import { FormState } from '../models/FormStates.ts'
import PermissionTypes from '../mixins/PermissionTypes.js'
import logger from '../utils/Logger.js'
export default {
name: 'AppNavigationForm',
components: {
FormsIcon,
IconArchive,
IconArchiveOff,
IconCheck,
IconContentCopy,
IconDelete,
IconPencil,
IconPoll,
IconShareVariant,
NcActionButton,
NcActionRouter,
NcActionSeparator,
NcDialog,
NcListItem,
NcLoadingIcon,
},
mixins: [PermissionTypes],
props: {
form: {
type: Object,
required: true,
},
forceDisplayActions: {
type: Boolean,
default: false,
required: false,
},
readOnly: {
type: Boolean,
default: true,
},
},
data() {
return {
loading: false,
showDeleteDialog: false,
buttons: [
{
label: t('forms', 'Delete form'),
icon: IconDeleteSvg,
type: 'error',
callback: () => {
this.onDeleteForm()
},
},
],
}
},
computed: {
canEdit() {
return this.form.permissions.includes(
this.PERMISSION_TYPES.PERMISSION_EDIT,
)
},
/**
* Check if form is current form and set active
*/
isActive() {
return this.form.hash === this.$route.params.hash
},
/**
* Check if the form is archived
*/
isArchived() {
return this.form.state === FormState.FormArchived
},
/**
* Check if form is expired
*/
isExpired() {
return this.form.expires && moment().unix() > this.form.expires
},
/**
* Return form title, or placeholder if not set
*
* @return {string}
*/
formTitle() {
if (this.form.title) {
return this.form.title
}
return t('forms', 'New form')
},
/**
* Return expiration details for subtitle
*/
formSubtitle() {
if (this.form.state === FormState.FormClosed) {
// TRANSLATORS: The form was closed manually so it does not take new submissions
return t('forms', 'Form closed')
}
if (this.form.expires) {
const relativeDate = moment(this.form.expires, 'X').fromNow()
if (this.isExpired) {
return t('forms', 'Expired {relativeDate}', {
relativeDate,
})
}
return t('forms', 'Expires {relativeDate}', { relativeDate })
}
return ''
},
/**
* Return, if form has Subtitle
*/
hasSubtitle() {
return this.formSubtitle !== ''
},
/**
* Route to use, depending on readOnly
*
* @return {string} Route to 'submit' or 'formRoot'
*/
routerTarget() {
if (this.readOnly) {
return 'submit'
}
return 'formRoot'
},
},
methods: {
/**
* Closes the App-Navigation on mobile-devices
*/
mobileCloseNavigation() {
this.$emit('mobile-close-navigation')
},
onShareForm() {
this.$emit('open-sharing', this.form.hash)
},
onCloneForm() {
this.$emit('clone', this.form.id)
},
async onToggleArchive() {
try {
// TODO: add loading status feedback ?
await axios.patch(
generateOcsUrl('apps/forms/api/v3/forms/{id}', {
id: this.form.id,
}),
{
keyValuePairs: {
state: this.isArchived
? FormState.FormClosed
: FormState.FormArchived,
},
},
)
this.$set(
this.form,
'state',
this.isArchived ? FormState.FormClosed : FormState.FormArchived,
)
} catch (error) {
logger.error('Error changing archived state of form', {
error,
})
showError(t('forms', 'Error changing archived state of form'))
}
},
async onDeleteForm() {
this.loading = true
try {
await axios.delete(
generateOcsUrl('apps/forms/api/v3/forms/{id}', {
id: this.form.id,
}),
)
this.$emit('delete', this.form.id)
} catch (error) {
logger.error(`Error while deleting ${this.formTitle}`, {
error: error.response,
})
showError(
t('forms', 'Error while deleting {title}', {
title: this.formTitle,
}),
)
} finally {
this.loading = false
}
},
},
}
</script>