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
|
<!--
Matomo - free/libre analytics platform
@link https://matomo.org
@license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
-->
<template>
<div>
<div class="btn">
<span :for="name" v-html="$sanitize(title)"></span>
<input ref="fileInput" :name="name" type="file" :id="name" @change="onChange($event)" />
</div>
<div class="file-path-wrapper">
<input class="file-path validate" :value="filePath" type="text"/>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import AbortableModifiers from './AbortableModifiers';
export default defineComponent({
props: {
name: String,
title: String,
modelValue: [String, File],
modelModifiers: Object,
},
inheritAttrs: false,
emits: ['update:modelValue'],
watch: {
modelValue(v: string|File) {
if (!v || v === '') {
const fileInputElement = this.$refs.fileInput as HTMLInputElement;
fileInputElement!.value = '';
}
},
},
methods: {
onChange(event: Event) {
const { files } = event.target as HTMLInputElement;
if (!files) {
return;
}
const file = files.item(0);
if (!(this.modelModifiers as AbortableModifiers)?.abortable) {
this.$emit('update:modelValue', file);
return;
}
const emitEventData = {
value: file,
abort() {
// not supported
},
};
this.$emit('update:modelValue', emitEventData);
},
},
computed: {
filePath() {
if (this.modelValue instanceof File) {
return (this.$refs.fileInput as HTMLInputElement).value;
}
return undefined;
},
},
});
</script>
|