2017-10-31 06:47:10 -04:00
|
|
|
<script>
|
2017-12-21 10:05:47 -05:00
|
|
|
import { mapActions, mapState } from 'vuex';
|
2017-10-31 06:47:10 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2017-12-21 10:05:47 -05:00
|
|
|
branchId: {
|
2017-10-31 06:47:10 -04:00
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
2017-12-21 10:05:47 -05:00
|
|
|
parent: {
|
|
|
|
type: Object,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapState([
|
|
|
|
'trees',
|
|
|
|
'currentProjectId',
|
|
|
|
]),
|
2017-10-31 06:47:10 -04:00
|
|
|
},
|
|
|
|
methods: {
|
2017-10-31 08:13:29 -04:00
|
|
|
...mapActions([
|
|
|
|
'createTempEntry',
|
|
|
|
]),
|
2017-10-31 06:47:10 -04:00
|
|
|
createFile(target, file, isText) {
|
|
|
|
const { name } = file;
|
|
|
|
let { result } = target;
|
|
|
|
|
|
|
|
if (!isText) {
|
|
|
|
result = result.split('base64,')[1];
|
|
|
|
}
|
|
|
|
|
2017-10-31 08:13:29 -04:00
|
|
|
this.createTempEntry({
|
|
|
|
name,
|
2017-12-21 10:05:47 -05:00
|
|
|
projectId: this.currentProjectId,
|
|
|
|
branchId: this.branchId,
|
|
|
|
parent: this.parent,
|
2017-10-31 06:47:10 -04:00
|
|
|
type: 'blob',
|
|
|
|
content: result,
|
|
|
|
base64: !isText,
|
2017-10-31 08:13:29 -04:00
|
|
|
});
|
2017-10-31 06:47:10 -04:00
|
|
|
},
|
|
|
|
readFile(file) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
const isText = file.type.match(/text.*/) !== null;
|
|
|
|
|
|
|
|
reader.addEventListener('load', e => this.createFile(e.target, file, isText), { once: true });
|
|
|
|
|
|
|
|
if (isText) {
|
|
|
|
reader.readAsText(file);
|
|
|
|
} else {
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
openFile() {
|
|
|
|
Array.from(this.$refs.fileUpload.files).forEach(file => this.readFile(file));
|
|
|
|
},
|
2017-12-21 10:05:47 -05:00
|
|
|
startFileUpload() {
|
|
|
|
this.$refs.fileUpload.click();
|
|
|
|
},
|
2017-10-31 06:47:10 -04:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.$refs.fileUpload.addEventListener('change', this.openFile);
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.$refs.fileUpload.removeEventListener('change', this.openFile);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2017-12-21 10:05:47 -05:00
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
role="button"
|
|
|
|
@click.prevent="startFileUpload"
|
|
|
|
>
|
|
|
|
{{ __('Upload file') }}
|
|
|
|
</a>
|
2017-10-31 06:47:10 -04:00
|
|
|
<input
|
|
|
|
id="file-upload"
|
|
|
|
type="file"
|
|
|
|
class="hidden"
|
|
|
|
ref="fileUpload"
|
|
|
|
/>
|
2017-12-21 10:05:47 -05:00
|
|
|
</div>
|
2017-10-31 06:47:10 -04:00
|
|
|
</template>
|