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

Timeline.vue « views « src - github.com/nextcloud/photos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4aded99893e7c6e2b728142070a211e61e6018ca (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
<!--
 - @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
 -
 - @author John Molakvoæ <skjnldsv@protonmail.com>
 - @author Corentin Mors <medias@pixelswap.fr>
 -
 - @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>
	<!-- Errors handlers-->
	<EmptyContent v-if="error === 404" illustration-name="folder">
		{{ t('photos', 'This folder does not exist') }}
	</EmptyContent>
	<EmptyContent v-else-if="error">
		{{ t('photos', 'An error occurred') }}
	</EmptyContent>

	<!-- Folder content -->
	<div v-else>
		<Navigation v-if="isEmpty"
			key="navigation"
			:basename="path"
			:filename="'/'"
			:root-title="rootTitle" />

		<EmptyContent v-if="isEmpty" illustration-name="empty">
			{{ t('photos', 'No photos in here') }}
		</EmptyContent>

		<div class="grid-container">
			<VirtualGrid ref="virtualgrid"
				:items="contentList"
				:update-function="getContent"
				:get-column-count="() => gridConfig.count"
				:get-grid-gap="() => gridConfig.gap"
				:update-trigger-margin="700"
				:loader="loaderComponent" />
		</div>
	</div>
</template>

<script>
import moment from '@nextcloud/moment'
import { mapGetters } from 'vuex'

import getPhotos from '../services/PhotoSearch'

import EmptyContent from '../components/EmptyContent'
import File from '../components/File'
import SeparatorVirtualGrid from '../components/SeparatorVirtualGrid'
import VirtualGrid from 'vue-virtual-grid'
import Navigation from '../components/Navigation'
import Loader from '../components/Loader'

import cancelableRequest from '../utils/CancelableRequest'
import GridConfigMixin from '../mixins/GridConfig'
import { allMimes } from '../services/AllowedMimes'

export default {
	name: 'Timeline',
	components: {
		EmptyContent,
		VirtualGrid,
		Navigation,
	},
	mixins: [GridConfigMixin],
	props: {
		loading: {
			type: Boolean,
			required: true,
		},
		onlyFavorites: {
			type: Boolean,
			default: false,
		},
		mimesType: {
			type: Array,
			default: () => allMimes,
		},
		rootTitle: {
			type: String,
			required: true,
		},
		path: {
			type: String,
			default: '',
		},
		onThisDay: {
			type: Boolean,
			default: false,
		},
	},

	data() {
		return {
			cancelRequest: null,
			done: false,
			error: null,
			page: 0,
			loaderComponent: Loader,
		}
	},

	computed: {
		// global lists
		...mapGetters([
			'files',
			'timeline',
		]),
		// list of loaded medias
		fileList() {
			return this.timeline
				.map((fileId) => this.files[fileId])
				.filter((file) => !!file)
		},
		// list of displayed content in the grid (titles + medias)
		contentList() {
			/**
			 * The goal of this flat map is to return an array of images separated by titles (months)
			 * ie: [{month1}, {image1}, {image2}, {month2}, {image3}, {image4}, {image5}]
			 * First we get the current month+year of the image
			 * We compare it to the previous image month+year
			 * If there is a difference we have to insert a title object before the current image
			 * If it's equal we just add the current image to the array
			 * Note: the injected param of objects are used to pass custom params to the grid lib
			 * In our case injected could be an image/video (aka file) or a title (year/month)
			 * Note2: titles are rendered full width and images are rendered on 1 column and 256x256 ratio
			 */
			let lastSection = ''
			return this.fileList.flatMap((file, index) => {
				const finalArray = []
				const currentSection = this.getFormatedDate(file.lastmod, 'YYYY MMMM')
				if (lastSection !== currentSection) {
					finalArray.push({
						id: `title-${index}`,
						injected: {
							year: this.getFormatedDate(file.lastmod, 'YYYY'),
							month: this.getFormatedDate(file.lastmod, 'MMMM'),
							onThisDay: this.onThisDay ? Math.round(moment(Date.now()).diff(moment(file.lastmod), 'years', true)) : false,
						},
						height: 90,
						columnSpan: 0, // means full width
						newRow: true,
						renderComponent: SeparatorVirtualGrid,
					})
					lastSection = currentSection // we keep track of the last section for the next batch
				}
				finalArray.push({
					id: `img-${file.fileid}`,
					injected: {
						...file,
						list: this.fileList,
						loadMore: this.getContent,
						canLoop: false,
					},
					width: 256,
					height: 256,
					columnSpan: 1,
					renderComponent: File,
				})
				return finalArray
			})
		},
		// is current folder empty?
		isEmpty() {
			return this.fileList.length === 0
		},
	},

	watch: {
		$route(from, to) {
			// cancel any pending requests
			if (this.cancelRequest) {
				this.cancelRequest('Changed view')
			}
			this.resetState()
		},
		async onThisDay() {
			// reset component
			this.resetState()
			this.getContent()
		},
	},

	beforeRouteLeave(from, to, next) {
		// cancel any pending requests
		if (this.cancelRequest) {
			this.cancelRequest('Changed view')
		}
		this.resetState()
		next()
	},

	beforeDestroy() {
		// cancel any pending requests
		if (this.cancelRequest) {
			this.cancelRequest('Changed view')
		}
	},

	methods: {
		/**
		 * Return next batch of data depending on global offset
		 *
		 * @param {boolean} doReturn Returns a Promise with the list instead of a boolean
		 * @return {Promise<boolean>} Returns a Promise with a boolean that stops infinite loading
		 */
		async getContent(doReturn) {
			if (this.done) {
				return Promise.resolve(true)
			}

			// cancel any pending requests
			if (this.cancelRequest) {
				this.cancelRequest('Changed view')
			}

			// if we don't already have some cached data let's show a loader
			if (this.timeline.length === 0) {
				this.$emit('update:loading', true)
			}

			// done loading even with errors
			const { request, cancel } = cancelableRequest(getPhotos)
			this.cancelRequest = cancel

			const numberOfImagesPerBatch = this.gridConfig.count * 5 // loading 5 rows

			try {
				// Load next batch of images
				const files = await request(this.onlyFavorites, {
					page: this.page,
					perPage: numberOfImagesPerBatch,
					mimesType: this.mimesType,
					onThisDay: this.onThisDay,
				})

				// If we get less files than requested that means we got to the end
				if (files.length !== numberOfImagesPerBatch) {
					this.done = true
				}

				this.$store.dispatch('updateTimeline', files)
				this.$store.dispatch('appendFiles', files)

				this.page += 1

				if (doReturn) {
					return Promise.resolve(files)
				}

				return Promise.resolve(false)
			} catch (error) {
				if (error.response && error.response.status) {
					if (error.response.status === 404) {
						this.error = 404
						setTimeout(() => {
							this.$router.push({ name: this.$route.name })
						}, 3000)
					} else {
						this.error = error
					}
				}

				// cancelled request, moving on...
				console.error('Error fetching timeline', error)
				return Promise.resolve(true)
			} finally {
				// done loading even with errors
				this.$emit('update:loading', false)
				this.cancelRequest = null
			}
		},

		/**
		 * Reset this component data to a pristine state
		 */
		resetState() {
			this.$store.dispatch('resetTimeline')
			this.done = false
			this.error = null
			this.page = 0
			this.lastSection = ''
			this.$emit('update:loading', true)
			if (this.$refs.virtualgrid) {
				this.$refs.virtualgrid.resetGrid()
			}
		},

		getFormatedDate(string, format) {
			return moment(string).format(format)
		},

	},

}
</script>

<style lang="scss" scoped>
@import '../mixins/GridSizes';

.grid-container {
	@include grid-sizes using ($marginTop, $marginW) {
		padding: 0px #{$marginW}px 256px #{$marginW}px;
	}
}
</style>