Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Trashbin.vue « AppNavigation « components « src - github.com/nextcloud/tasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7346c61c5bba8f7046dcf2eb3db1b900672c4148 (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<!--
Nextcloud - Tasks

@author Christoph Wurst
@copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
@copyright 2021 Raimund Schlüßler <raimund.schluessler@mailbox.org>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.

This library 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 library. If not, see <http://www.gnu.org/licenses/>.

-->

<template>
	<NcAppNavigationItem :title="t('tasks', 'Trash bin')"
		:pinned="true"
		@click.prevent="onShow">
		<template #icon>
			<Delete :size="20" />
		</template>
		<template #extra>
			<NcModal v-if="showModal"
				size="large"
				@close="showModal = false">
				<div class="modal__content">
					<NcEmptyContent v-if="loading" icon="icon-loading">
						{{ t('tasks', 'Loading deleted calendars, tasks and events.') }}
					</NcEmptyContent>
					<NcEmptyContent v-else-if="!items.length">
						<template #icon>
							<Delete :size="64" />
						</template>
						{{ t('tasks', 'You do not have any deleted calendars, tasks or events.') }}
					</NcEmptyContent>
					<template v-else>
						<h2>{{ t('tasks', 'Trash bin') }}</h2>
						<div class="table">
							<div class="table__header">
								{{ t('tasks', 'Name') }}
							</div>
							<div class="table__header table__header--deletedAt">
								{{ t('tasks', 'Deleted') }}
							</div>
							<div class="table__header">
								&nbsp;
							</div>
							<template v-for="item in items">
								<div :key="`${item.url}desc`" class="table__body">
									<div class="icon-bullet"
										:style="{ 'background-color': item.color }" />
									<div class="item-description">
										<div class="item-description__mainline">
											{{ item.name }}
										</div>
										<div v-if="item.subline" class="item-description__subline">
											{{ item.subline }}
										</div>
									</div>
								</div>
								<div :key="`${item.url}date`" class="table__body table__body--deletedAt">
									<Moment class="timestamp" :timestamp="item.deletedAt" />
								</div>
								<div :key="`${item.url}action`" class="table__body">
									<NcButton @click="restore(item)">
										<template #icon>
											<Undo :size="20" />
										</template>
										{{ t('tasks','Restore') }}
									</NcButton>
									<NcActions :force-menu="true">
										<NcActionButton @click="onDeletePermanently(item)">
											<template #icon>
												<Delete :size="20" />
											</template>
											{{ t('tasks','Delete permanently') }}
										</NcActionButton>
									</NcActions>
								</div>
							</template>
						</div>
						<div class="footer">
							<p v-if="retentionDuration">
								{{ n('tasks', 'Elements in the trash bin are deleted after {numDays} day', 'Elements in the trash bin are deleted after {numDays} days', retentionDuration, { numDays: retentionDuration }) }}
							</p>
							<NcButton type="primary" @click="onEmptyTrashBin()">
								<template #icon>
									<DeleteForever :size="20" />
								</template>
								{{ t('tasks','Empty trash bin') }}
							</NcButton>
						</div>
					</template>
				</div>
			</NcModal>
		</template>
	</NcAppNavigationItem>
</template>

<script>
import Moment from './Moment.vue'
import { uidToHexColor } from '../../utils/color.js'
import logger from '../../utils/logger.js'

import { showError } from '@nextcloud/dialogs'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import moment from '@nextcloud/moment'
import NcAppNavigationItem from '@nextcloud/vue/dist/Components/NcAppNavigationItem.js'
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'

import Delete from 'vue-material-design-icons/Delete.vue'
import DeleteForever from 'vue-material-design-icons/DeleteForever.vue'
import Undo from 'vue-material-design-icons/Undo.vue'

import { mapGetters } from 'vuex'

export default {
	name: 'Trashbin',
	components: {
		NcAppNavigationItem,
		Delete,
		NcEmptyContent,
		NcModal,
		Moment,
		NcActions,
		NcActionButton,
		NcButton,
		DeleteForever,
		Undo,
	},
	data() {
		return {
			showModal: false,
			loading: true,
		}
	},
	computed: {
		...mapGetters({
			trashBin: 'trashBin',
			calendars: 'sortedDeletedCalendars',
			objects: 'deletedCalendarObjects',
		}),
		items() {
			const formattedCalendars = this.calendars.map(calendar => ({
				calendar,
				type: 'calendar',
				key: calendar.url,
				name: calendar.displayname,
				url: calendar._url,
				deletedAt: calendar._props['{http://nextcloud.com/ns}deleted-at'],
				color: calendar.color ?? uidToHexColor(calendar.displayname),
			}))
			const formattedCalendarObjects = this.objects.map(vobject => {
				let name
				try {
					name = vobject?.calendarComponent.getComponentIterator().next().value?.title
				} catch (e) {
				}
				if (!name) {
					if (vobject.objectType === 'VTODO') {
						name = t('tasks', 'Untitled task')
					} else if (vobject.objectType === 'VEVENT') {
						name = t('tasks', 'Untitled event')
					} else if (vobject.objectType === 'VJOURNAL') {
						name = t('tasks', 'Untitled journal')
					} else {
						name = t('tasks', 'Untitled item')
					}
				}
				let subline = vobject.calendar?.displayName || t('tasks', 'Unknown calendar')
				if (vobject.isEvent) {
					const event = vobject?.calendarComponent.getFirstComponent('VEVENT')
					if (event?.startDate.jsDate && event?.isAllDay()) {
						subline += ' · ' + moment(event.startDate.jsDate).format('LL')
					} else if (event?.startDate.jsDate) {
						subline += ' · ' + moment(event?.startDate.jsDate).format('LLL')
					}
				}
				const color = vobject.calendarComponent.getComponentIterator().next().value?.color
						?? vobject.calendar?.color
						?? uidToHexColor(subline)
				return {
					vobject,
					type: 'object',
					key: vobject.id,
					name,
					subline,
					url: vobject.uri,
					deletedAt: vobject.dav._props['{http://nextcloud.com/ns}deleted-at'],
					color,
				}
			})

			return formattedCalendars.concat(formattedCalendarObjects).sort((item1, item2) => item2.deletedAt - item1.deletedAt)

		},
		retentionDuration() {
			return Math.ceil(
				this.trashBin.retentionDuration / (60 * 60 * 24)
			)
		},
	},
	methods: {
		t,
		n,

		async onShow() {
			this.showModal = true

			this.loading = true
			try {
				await Promise.all([
					this.$store.dispatch('loadDeletedCalendars'),
					this.$store.dispatch('loadDeletedCalendarObjects'),
				])

				logger.debug('deleted calendars and objects loaded', {
					calendars: this.calendars,
					objects: this.objects,
				})
			} catch (error) {
				logger.error('could not load deleted calendars and objects', {
					error,
				})

				showError(t('tasks', 'Could not load deleted calendars and objects'))
			}
			this.loading = false
		},
		async onDeletePermanently(item) {
			logger.debug('deleting ' + item.url + ' permanently', item)
			try {
				switch (item.type) {
				case 'calendar':
					await this.$store.dispatch('deleteCalendarPermanently', { calendar: item.calendar })
					break
				case 'object':
					await this.$store.dispatch('deleteCalendarObjectPermanently', { vobject: item.vobject })
					break
				}
			} catch (error) {
				logger.error('could not delete ' + item.url, { error })

				showError(t('tasks', 'Could not delete calendar or event'))
			}
		},
		async restore(item) {
			logger.debug('restoring ' + item.url, item)
			try {
				switch (item.type) {
				case 'calendar': {
					await this.$store.dispatch('restoreCalendar', { calendar: item.calendar })
					const { calendars } = await this.$store.dispatch('getCalendarsAndTrashBin')
					// Load the tasks of the restored calendar
					const calendar = calendars.find(cal => cal.url === item.calendar.url)
					if (calendar?.supportsTasks) {
						await this.$store.dispatch('getTasksFromCalendar', { calendar, completed: false, related: null })
					}
					break
				}
				case 'object':
					await this.$store.dispatch('restoreCalendarObject', { vobject: item.vobject })
					break
				}
			} catch (error) {
				logger.error('could not restore ' + item.url, { error })

				showError(t('tasks', 'Could not restore calendar or event'))
			}
		},
		onEmptyTrashBin() {
			OC.dialogs.confirm(
				t('tasks', 'Do you really want to empty the trash bin?'),
				t('tasks', 'Empty trash bin'),
				this.emptyTrashBin,
				true
			)
		},

		emptyTrashBin(confirm) {
			if (!confirm) {
				return
			}
			this.items.forEach((item) => {
				this.onDeletePermanently(item)
			})
		},
	},
}
</script>

<style lang="scss" scoped>

.modal__content {
	display: flex;
	flex-direction: column;
	margin: 2vw;
}

.table {
	display: grid;
	grid-template-columns: minmax(200px, 1fr) max-content max-content;
	overflow: scroll;
	margin-bottom: auto;

	&__header {
		position: sticky;
		top: 0;
		background-color: var(--color-main-background-translucent);
		z-index: 1;
	}

	&__header,
	&__body {
		&--deletedAt {
			justify-content: right;
		}
	}

	& > div {
		display: flex;
		align-items: center;
		padding: 4px;
	}
}

.item-description {
	overflow: hidden;

	&__mainline,
	&__subline {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	&__subline {
		color: var(--color-text-maxcontrast);
	}
}

.footer {
	display: flex;
	flex-direction: column;
	align-items: center;
	color: var(--color-text-lighter);
	font-size: small;
	margin-top: 16px;
	& > p {
		margin-bottom: 12px;
	}
}

.icon-bullet {
	display: inline-block;
	vertical-align: middle;
	width: 14px;
	height: 14px;
	margin-right: 14px;
	border: none;
	border-radius: 50%;
	flex-shrink: 0;
}
</style>