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: 6bdac23defdc39b1d2a26074d015f4c9a9e485d1 (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
<!--
 - @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
 -
 - @author John Molakvoæ <skjnldsv@protonmail.com>
 -
 - @license GNU AGPL version 3 or any later version
 -
 - 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 exists') }}
	</EmptyContent>
	<EmptyContent v-else-if="error">
		{{ t('photos', 'An error occurred') }}
	</EmptyContent>
	<EmptyContent v-else-if="!loading && isEmpty" illustration-name="empty">
		{{ t('photos', 'No photos in here') }}
	</EmptyContent>

	<!-- Folder content -->
	<VirtualGrid v-else-if="!loading"
		:component="getComponent"
		:list="fileList"
		:loading-page="loadingPage"
		:props="getProps"
		@bottomReached="onBottomReached" />
</template>

<script>
import { mapGetters } from 'vuex'
import debounce from 'debounce'

import getPhotos from '../services/PhotoSearch'

import EmptyContent from '../components/EmptyContent'
import File from '../components/File'
import VirtualGrid from '../components/VirtualGrid'

import cancelableRequest from '../utils/CancelableRequest'
import arrayToChunk from '../utils/ArrayChunk'
import GridConfigMixin from '../mixins/GridConfig'

export default {
	name: 'Timeline',
	components: {
		EmptyContent,
		VirtualGrid,
	},
	mixins: [GridConfigMixin],
	props: {
		loading: {
			type: Boolean,
			required: true,
		},
		onlyFavorites: {
			type: Boolean,
			default: false,
		},
	},

	data() {
		return {
			cancelRequest: () => {},
			done: false,
			error: null,
			loadingPage: false,
			page: 0,
		}
	},

	computed: {
		// global lists
		...mapGetters([
			'files',
			'timeline',
		]),

		fileList() {
			return this.timeline
				.map(id => this.files[id])
				.filter(file => !!file)
		},

		// is current folder empty?
		isEmpty() {
			return this.fileList.length === 0
		},

		// the list chunked in rows for the virtual list
		chunkedList() {
			return arrayToChunk(this.fileList, this.gridConfig.count)
		},
	},

	watch: {
		async onlyFavorites() {
			// reset component
			this.resetState()

			// content is completely different
			this.$emit('update:loading', true)
			this.fetchContent()
		},
	},

	async beforeMount() {
		this.resetState()
		this.fetchContent()
	},

	beforeDestroy() {
		this.cancelRequest()
	},

	methods: {
		async fetchContent() {
			// only one simultaneous page load
			if (this.loadingPage) {
				return
			}

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

			// close any potential opened viewer
			OCA.Viewer.close()

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

			// init cancellable request
			const { request, cancel } = cancelableRequest(getPhotos)
			this.cancelRequest = cancel

			try {
				// get content and current folder info
				const files = await request(this.onlyFavorites, {
					page: this.page,
					perPage: this.gridConfig.count * 5, // we load 5 rows,
				})
				this.$store.dispatch('updateTimeline', files)
				this.$store.dispatch('appendFiles', files)

				// next time we load this script, we load the next page if the list returned
				if (files.length === this.gridConfig.count * 5) {
					this.page++
				} else {
					console.debug('We loaded the last page')
					this.done = true
				}
			} 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)
			} finally {
				// done loading even with errors
				this.$emit('update:loading', false)
				this.loadingPage = false
			}
		},

		/**
		 * Return the props based on the element
		 * Here we want to bind the full fileinfo
		 * object so we stupidly return it whole!
		 *
		 * @param {Object} item the scoped item from the VirtualGrid
		 * @returns {Object}
		 */
		getProps(item) {
			return item
		},

		/**
		 * Return the component based on the element
		 * We only have files in the Timeline,
		 * so we return Files!
		 *
		 * @returns {Object}
		 */
		getComponent() {
			return File
		},

		debounceOnBottomReached: debounce(function() {
			this.onBottomReached()
		}, 1000),

		/**
		 * When virtual grid reach the bottom,
		 * we load the next page
		 */
		onBottomReached() {
			// if we're currently loading or if a previous
			// request returned the last page, we stop
			if (this.loadingPage || this.done) {
				return
			}

			console.debug('Loading next page', this.page)
			this.fetchContent()
		},

		/**
		 * Reset this component data to a pristine state
		 */
		resetState() {
			this.$store.dispatch('resetTimeline')
			this.done = false
			this.error = null
			this.loadingPage = false
			this.page = 0
			this.page = 0
		},

	},

}
</script>