-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathFileInputField.vue
More file actions
183 lines (162 loc) · 4.35 KB
/
Copy pathFileInputField.vue
File metadata and controls
183 lines (162 loc) · 4.35 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
<!--
- SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import type { AdminThemingParameters } from '../../types.d.ts'
import { mdiImageOutline, mdiUndo } from '@mdi/js'
import axios, { isAxiosError } from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { computed, ref, useTemplateRef } from 'vue'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
const props = defineProps<{
name: string
label: string
disabled?: boolean
}>()
const emit = defineEmits<{
updated: []
}>()
const isSaving = ref(false)
const mime = ref(loadState<AdminThemingParameters>('theming', 'adminThemingParameters')[props.name + 'Mime'] as string)
const cacheKey = ref(Date.now())
const inputElement = useTemplateRef('input')
const background = computed(() => {
const baseUrl = generateUrl('/apps/theming/image/{key}', { key: props.name })
return `url(${baseUrl}?v=${cacheKey.value}&m=${encodeURIComponent(mime.value)})`
})
/**
* Open the file picker dialog
*/
function pickFile() {
if (isSaving.value) {
return
}
inputElement.value!.files = null
inputElement.value!.click()
}
/**
* Handle file input change event
*/
async function onChange() {
if (!inputElement.value!.files?.[0]) {
return
}
const file = inputElement.value!.files[0]!
if (file.type && !file.type.startsWith('image/')) {
showError(t('theming', 'Non image file selected'))
return
}
isSaving.value = true
const formData = new FormData()
formData.append('image', file)
formData.append('key', props.name)
try {
await axios.post(generateUrl('/apps/theming/ajax/uploadImage'), formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
mime.value = file.type
cacheKey.value = Date.now()
emit('updated')
} catch (error) {
if (isAxiosError(error) && error.response?.status === 422) {
const serverMessage = error.response.data?.data?.message
showError(serverMessage || t('theming', 'Failed to upload image'))
} else {
showError(t('theming', 'Failed to upload image'))
}
} finally {
isSaving.value = false
// Reset input to allow re-selecting the same file and show validation errors on every attempt
inputElement.value!.value = ''
}
}
/**
* Reset the image to default
*/
async function resetToDefault() {
if (isSaving.value) {
return
}
isSaving.value = true
try {
await axios.post(generateUrl('/apps/theming/ajax/undoChanges'), {
setting: props.name,
})
mime.value = ''
emit('updated')
} finally {
isSaving.value = false
}
}
</script>
<template>
<div :class="$style.fileInputField">
<NcButton
:class="$style.fileInputField__button"
alignment="start"
:disabled
size="large"
@click="pickFile">
<template #icon>
<NcLoadingIcon v-if="isSaving" />
<NcIconSvgWrapper v-else :path="mdiImageOutline" />
</template>
{{ label }}
</NcButton>
<div
v-if="mime.startsWith('image/')"
:class="$style.fileInputField__preview"
role="img"
:aria-label="t('theming', 'Preview of the selected image')" />
<NcButton
v-if="mime && !disabled"
:aria-label="t('theming', 'Reset to default')"
:title="t('theming', 'Reset to default')"
size="large"
variant="tertiary"
@click="resetToDefault">
<template #icon>
<NcIconSvgWrapper :path="mdiUndo" />
</template>
</NcButton>
<input
ref="input"
class="hidden-visually"
aria-hidden="true"
:disabled
type="file"
accept="image/*"
:name
@change="onChange">
</div>
</template>
<style module>
.fileInputField {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
gap: calc(1.5 * var(--default-grid-baseline));
}
.fileInputField__button {
min-width: clamp(200px, 25vw, 300px) !important;
}
.fileInputField__preview {
height: var(--clickable-area-large);
width: calc(var(--clickable-area-large) / 9 * 16);
background: v-bind('background');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
border: 2px solid var(--color-border-maxcontrast);
border-radius: var(--border-radius-element);
}
</style>