2
0
mirror of https://github.com/frappe/books.git synced 2024-12-27 12:42:44 +00:00
books/ui/components/controls/File.vue

81 lines
1.9 KiB
Vue
Raw Normal View History

2018-06-27 14:38:27 +00:00
<script>
import Base from './Base';
export default {
extends: Base,
methods: {
getWrapperElement(h) {
2018-07-15 13:50:24 +00:00
let fileName = this.docfield.placeholder || this._('Choose a file..');
let filePath = null;
2018-06-27 14:38:27 +00:00
if (this.value && typeof this.value === 'string') {
filePath = this.value;
}
else if (this.$refs.input && this.$refs.input.files.length) {
2018-06-27 14:38:27 +00:00
fileName = this.$refs.input.files[0].name;
}
const fileLink = h('a', {
attrs: {
href: filePath,
target: '_blank'
},
domProps: {
textContent: this._('View File')
}
});
const helpText = h('small', {
class: 'form-text text-muted'
}, [fileLink]);
const fileNameLabel = h('label', {
class: ['custom-file-label'],
domProps: {
textContent: filePath || fileName
2018-06-27 14:38:27 +00:00
}
});
const fileInputWrapper = h('div', {
class: ['custom-file']
},
[this.getInputElement(h), fileNameLabel, filePath ? helpText : null]
);
return h(
'div',
{
2018-07-14 11:39:42 +00:00
class: ['form-group', ...this.wrapperClass],
attrs: {
2018-06-27 14:38:27 +00:00
'data-fieldname': this.docfield.fieldname
2018-07-14 11:39:42 +00:00
}
},
[this.getLabelElement(h), fileInputWrapper]
);
2018-06-27 14:38:27 +00:00
},
getInputAttrs() {
return {
id: this.id,
type: 'file',
value: this.value,
2018-07-12 11:29:10 +00:00
required: this.docfield.required,
2018-07-14 11:39:42 +00:00
disabled: this.disabled,
webkitdirectory: this.docfield.directory,
directory: this.docfield.directory,
accept: (this.docfield.filetypes || []).join(',')
2018-07-14 11:39:42 +00:00
};
},
getInputClass() {
return 'custom-file-input';
},
2018-07-14 11:39:42 +00:00
getInputListeners() {
return {
change: e => {
this.handleChange(e.target.files);
}
};
2018-06-27 14:38:27 +00:00
}
}
2018-07-14 11:39:42 +00:00
};
2018-06-27 14:38:27 +00:00
</script>