File size: 4,061 Bytes
71174bc | 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 | <template>
<div class="input-group custom-file-button">
<label class="input-group-text" :for="id">
Choose File<span v-if="multiple">(s)</span>
</label>
<input
ref="fileinput"
type="file"
class="form-control"
:id="id"
:multiple="multiple"
@change="onFileChanged"
:accept="accept"
/>
<FormElementDescription>
{{ acceptableFileTypesMsg }}.
<span v-if="errorMsg !== ''" class="text-danger"
>{{ errorMsg }}.</span
>
</FormElementDescription>
</div>
</template>
<script lang="ts">
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { randomID } from "@/Core/Utils/MiscUtils";
import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import FormElementDescription from "@/UI/Forms/FormElementDescription.vue";
import { filesToFileInfos } from "@/FileSystem/FileUtils";
import { FileInfo } from "@/FileSystem/FileInfo";
/**
* FormFile component
*/
@Options({
components: {
FormElementDescription,
},
})
export default class FormFile extends Vue {
@Prop({ default: randomID() }) id!: string;
@Prop({ default: true }) multiple!: boolean;
@Prop({}) accept!: string;
@Prop({ default: false }) isZip!: boolean;
errorMsg = "";
/**
* Gets text describing the acceptable file types.
*
* @returns {string} Text describing the acceptable file types.
*/
get acceptableFileTypesMsg(): string {
return (
"Acceptable file types: " +
this.accept.replace(".biotite,", "").toUpperCase().replace(/,/g, ", ")
);
}
/**
* Gets all the acceptable file types.
*
* @returns {string[]} All the acceptable file types.
*/
get allAcceptableFileTypes(): string[] {
return this.accept.split(",").map((a) => a.toUpperCase().substring(1));
}
/**
* Runs when the file changes.
*/
onFileChanged(/* _e: Event */) {
let input = this.$refs.fileinput as HTMLInputElement;
let files = input.files;
if (files === null || files.length == 0) {
return;
}
// Make it as an array
let fileList = Array.from(files);
filesToFileInfos(fileList, this.isZip, this.allAcceptableFileTypes)
.then((filesLoaded: (FileInfo | string)[] | undefined) => {
if (filesLoaded === undefined) return;
const errorMsgs = filesLoaded.filter(
(a) => typeof a === "string"
);
const toLoad = filesLoaded.filter(
(a) => typeof a !== "string"
) as FileInfo[];
this.$emit("onFilesLoaded", toLoad);
if (errorMsgs.length > 0) {
this.clearFile();
this.errorMsg = errorMsgs.join(", ");
} else {
this.errorMsg = "";
}
// this.clearFile();
return;
})
.catch((err: any) => {
this.clearFile();
this.errorMsg = err;
// throw err;
});
}
/**
* Clears the file input.
*/
clearFile() {
// @ts-ignore
(this.$refs.fileinput as HTMLInputElement).value = null;
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
// See https://stackoverflow.com/questions/65770908/how-to-change-choose-file-text-using-bootstrap-5
.custom-file-button {
input[type="file"] {
margin-left: -2px !important;
&::-webkit-file-upload-button {
display: none;
}
&::file-selector-button {
display: none;
}
}
&:hover {
label {
background-color: #dde0e3;
cursor: pointer;
}
}
}
</style>
|