aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/src/components/admin/FileInputField.vue
blob: 717f222abbfe06e1ffc8159bc375e4c88bc58bdc (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
<!--
  - SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<div class="field">
		<label :for="id">{{ displayName }}</label>
		<div class="field__row">
			<NcButton type="secondary"
				:id="id"
				:aria-label="ariaLabel"
				data-admin-theming-setting-file-picker
				@click="activateLocalFilePicker">
				<template #icon>
					<Upload :size="20" />
				</template>
				{{ t('theming', 'Upload') }}
			</NcButton>
			<NcButton v-if="showReset"
				type="tertiary"
				:aria-label="t('theming', 'Reset to default')"
				data-admin-theming-setting-file-reset
				@click="undo">
				<template #icon>
					<Undo :size="20" />
				</template>
			</NcButton>
			<NcButton v-if="showRemove"
				type="tertiary"
				:aria-label="t('theming', 'Remove background image')"
				data-admin-theming-setting-file-remove
				@click="removeBackground">
				<template #icon>
					<Delete :size="20" />
				</template>
			</NcButton>
			<NcLoadingIcon v-if="showLoading"
				class="field__loading-icon"
				:size="20" />
		</div>

		<div v-if="(name === 'logoheader' || name === 'favicon') && mimeValue !== defaultMimeValue"
			class="field__preview"
			:class="{
				'field__preview--logoheader': name === 'logoheader',
				'field__preview--favicon': name === 'favicon',
			}" />

		<NcNoteCard v-if="errorMessage"
			type="error"
			:show-alert="true">
			<p>{{ errorMessage }}</p>
		</NcNoteCard>

		<input ref="input"
			:accept="acceptMime"
			type="file"
			@change="onChange">
	</div>
</template>

<script>
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'

import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import NcNoteCard from '@nextcloud/vue/dist/Components/NcNoteCard.js'
import Delete from 'vue-material-design-icons/Delete.vue'
import Undo from 'vue-material-design-icons/UndoVariant.vue'
import Upload from 'vue-material-design-icons/Upload.vue'

import FieldMixin from '../../mixins/admin/FieldMixin.js'

const {
	allowedMimeTypes,
} = loadState('theming', 'adminThemingParameters', {})

export default {
	name: 'FileInputField',

	components: {
		Delete,
		NcButton,
		NcLoadingIcon,
		NcNoteCard,
		Undo,
		Upload,
	},

	mixins: [
		FieldMixin,
	],

	props: {
		name: {
			type: String,
			required: true,
		},
		mimeName: {
			type: String,
			required: true,
		},
		mimeValue: {
			type: String,
			required: true,
		},
		defaultMimeValue: {
			type: String,
			default: '',
		},
		displayName: {
			type: String,
			required: true,
		},
		ariaLabel: {
			type: String,
			required: true,
		},
	},

	data() {
		return {
			showLoading: false,
			acceptMime: (allowedMimeTypes[this.name]
				|| ['image/jpeg', 'image/png', 'image/gif', 'image/webp']).join(','),
		}
	},

	computed: {
		showReset() {
			return this.mimeValue !== this.defaultMimeValue
		},

		showRemove() {
			if (this.name === 'background') {
				if (this.mimeValue.startsWith('image/')) {
					return true
				}
				if (this.mimeValue === this.defaultMimeValue) {
					return true
				}
			}
			return false
		},
	},

	methods: {
		activateLocalFilePicker() {
			this.reset()
			// Set to null so that selecting the same file will trigger the change event
			this.$refs.input.value = null
			this.$refs.input.click()
		},

		async onChange(e) {
			const file = e.target.files[0]

			const formData = new FormData()
			formData.append('key', this.name)
			formData.append('image', file)

			const url = generateUrl('/apps/theming/ajax/uploadImage')
			try {
				this.showLoading = true
				const { data } = await axios.post(url, formData)
				this.showLoading = false
				this.$emit('update:mime-value', file.type)
				this.$emit('uploaded', data.data.url)
				this.handleSuccess()
			} catch (e) {
				this.showLoading = false
				this.errorMessage = e.response.data.data?.message
			}
		},

		async undo() {
			this.reset()
			const url = generateUrl('/apps/theming/ajax/undoChanges')
			try {
				await axios.post(url, {
					setting: this.mimeName,
				})
				this.$emit('update:mime-value', this.defaultMimeValue)
				this.handleSuccess()
			} catch (e) {
				this.errorMessage = e.response.data.data?.message
			}
		},

		async removeBackground() {
			this.reset()
			const url = generateUrl('/apps/theming/ajax/updateStylesheet')
			try {
				await axios.post(url, {
					setting: this.mimeName,
					value: 'backgroundColor',
				})
				this.$emit('update:mime-value', 'backgroundColor')
				this.handleSuccess()
			} catch (e) {
				this.errorMessage = e.response.data.data?.message
			}
		},
	},
}
</script>

<style lang="scss" scoped>
@import './shared/field.scss';

.field {
	&__loading-icon {
		width: 44px;
		height: 44px;
	}

	&__preview {
		width: 70px;
		height: 70px;
		background-size: contain;
		background-position: center;
		background-repeat: no-repeat;
		margin: 10px 0;

		&--logoheader {
			background-image: var(--image-logoheader);
		}

		&--favicon {
			background-image: var(--image-favicon);
		}
	}
}

input[type="file"] {
	display: none;
}
</style>