2018-05-09 12:01:36 +05:30
|
|
|
<script>
|
2018-11-18 11:00:15 +05:30
|
|
|
import Icon from '~/vue_shared/components/icon.vue';
|
|
|
|
import ItemButton from './button.vue';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
Icon,
|
|
|
|
ItemButton,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
path: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
2018-05-09 12:01:36 +05:30
|
|
|
},
|
2018-11-18 11:00:15 +05:30
|
|
|
showLabel: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true,
|
2018-05-09 12:01:36 +05:30
|
|
|
},
|
2018-11-18 11:00:15 +05:30
|
|
|
buttonCssClasses: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: null,
|
2018-05-09 12:01:36 +05:30
|
|
|
},
|
2018-11-18 11:00:15 +05:30
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
createFile(target, file, isText) {
|
|
|
|
const { name } = file;
|
|
|
|
let { result } = target;
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (!isText) {
|
|
|
|
// eslint-disable-next-line prefer-destructuring
|
|
|
|
result = result.split('base64,')[1];
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
this.$emit('create', {
|
|
|
|
name: `${this.path ? `${this.path}/` : ''}${name}`,
|
|
|
|
type: 'blob',
|
|
|
|
content: result,
|
|
|
|
base64: !isText,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
readFile(file) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
const isText = file.type.match(/text.*/) !== null;
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
reader.addEventListener('load', e => this.createFile(e.target, file, isText), { once: true });
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (isText) {
|
|
|
|
reader.readAsText(file);
|
|
|
|
} else {
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
openFile() {
|
|
|
|
Array.from(this.$refs.fileUpload.files).forEach(file => this.readFile(file));
|
2018-05-09 12:01:36 +05:30
|
|
|
},
|
2018-11-18 11:00:15 +05:30
|
|
|
startFileUpload() {
|
|
|
|
this.$refs.fileUpload.click();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-05-09 12:01:36 +05:30
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
2018-11-18 11:00:15 +05:30
|
|
|
<item-button
|
|
|
|
:class="buttonCssClasses"
|
|
|
|
:show-label="showLabel"
|
|
|
|
:icon-classes="showLabel ? 'mr-2' : ''"
|
|
|
|
:label="__('Upload file')"
|
|
|
|
class="d-flex"
|
|
|
|
icon="upload"
|
|
|
|
@click="startFileUpload"
|
|
|
|
/>
|
2018-05-09 12:01:36 +05:30
|
|
|
<input
|
|
|
|
id="file-upload"
|
2018-11-08 19:23:39 +05:30
|
|
|
ref="fileUpload"
|
2018-05-09 12:01:36 +05:30
|
|
|
type="file"
|
|
|
|
class="hidden"
|
2018-11-20 20:47:30 +05:30
|
|
|
multiple
|
|
|
|
@change="openFile"
|
2018-05-09 12:01:36 +05:30
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</template>
|