buefy
Version:
Lightweight UI components for Vue.js based on Bulma
211 lines (180 loc) • 5.98 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
require('./chunk-545f01b1.js');
var __chunk_4 = require('./chunk-c146745e.js');
var __chunk_6 = require('./chunk-13e039f5.js');
var __chunk_15 = require('./chunk-f1df1c63.js');
//
var script = {
name: 'BUpload',
mixins: [__chunk_4.FormElementMixin],
inheritAttrs: false,
props: {
value: {
type: [Object, Function, __chunk_15.File, Array]
},
multiple: Boolean,
disabled: Boolean,
accept: String,
dragDrop: Boolean,
type: {
type: String,
default: 'is-primary'
},
native: {
type: Boolean,
default: false
}
},
data: function data() {
return {
newValue: this.value,
dragDropFocus: false,
_elementRef: 'input'
};
},
watch: {
/**
* When v-model is changed:
* 1. Get value from input file
* 2. Set internal value.
* 3. Reset input value if array is empty or when input file is not found in newValue
* 4. If it's invalid, validate again.
*/
value: function value(_value) {
var inputFiles = this.$refs.input.files;
this.newValue = _value;
if (!this.newValue || Array.isArray(this.newValue) && this.newValue.length === 0 || !inputFiles[0] || Array.isArray(this.newValue) && !this.newValue.some(function (a) {
return a.name === inputFiles[0].name;
})) {
this.$refs.input.value = null;
}
!this.isValid && !this.dragDrop && this.checkHtml5Validity();
}
},
methods: {
/**
* Listen change event on input type 'file',
* emit 'input' event and validate
*/
onFileChange: function onFileChange(event) {
if (this.disabled || this.loading) return;
if (this.dragDrop) {
this.updateDragDropFocus(false);
}
var value = event.target.files || event.dataTransfer.files;
if (value.length === 0) {
if (!this.newValue) {
return;
}
if (this.native) {
this.newValue = null;
}
} else if (!this.multiple) {
// only one element in case drag drop mode and isn't multiple
if (this.dragDrop && value.length !== 1) return;else {
var file = value[0];
if (this.checkType(file)) {
this.newValue = file;
} else if (this.newValue) {
this.newValue = null;
} else {
return;
}
}
} else {
// always new values if native or undefined local
var newValues = false;
if (this.native || !this.newValue) {
this.newValue = [];
newValues = true;
}
for (var i = 0; i < value.length; i++) {
var _file = value[i];
if (this.checkType(_file)) {
this.newValue.push(_file);
newValues = true;
}
}
if (!newValues) {
return;
}
}
this.$emit('input', this.newValue);
!this.dragDrop && this.checkHtml5Validity();
},
/**
* Listen drag-drop to update internal variable
*/
updateDragDropFocus: function updateDragDropFocus(focus) {
if (!this.disabled && !this.loading) {
this.dragDropFocus = focus;
}
},
/**
* Check mime type of file
*/
checkType: function checkType(file) {
if (!this.accept) return true;
var types = this.accept.split(',');
if (types.length === 0) return true;
var valid = false;
for (var i = 0; i < types.length && !valid; i++) {
var type = types[i].trim();
if (type) {
if (type.substring(0, 1) === '.') {
// check extension
var extIndex = file.name.lastIndexOf('.');
var extension = extIndex >= 0 ? file.name.substring(extIndex) : '';
if (extension.toLowerCase() === type.toLowerCase()) {
valid = true;
}
} else {
// check mime type
if (file.type.match(type)) {
valid = true;
}
}
}
}
return valid;
}
}
};
/* script */
const __vue_script__ = script;
/* template */
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('label',{staticClass:"upload control"},[(!_vm.dragDrop)?[_vm._t("default")]:_c('div',{staticClass:"upload-draggable",class:[_vm.type, {
'is-loading': _vm.loading,
'is-disabled': _vm.disabled,
'is-hovered': _vm.dragDropFocus
}],on:{"dragover":function($event){$event.preventDefault();_vm.updateDragDropFocus(true);},"dragleave":function($event){$event.preventDefault();_vm.updateDragDropFocus(false);},"dragenter":function($event){$event.preventDefault();_vm.updateDragDropFocus(true);},"drop":function($event){$event.preventDefault();_vm.onFileChange($event);}}},[_vm._t("default")],2),_vm._v(" "),_c('input',_vm._b({ref:"input",attrs:{"type":"file","multiple":_vm.multiple,"accept":_vm.accept,"disabled":_vm.disabled},on:{"change":_vm.onFileChange}},'input',_vm.$attrs,false))],2)};
var __vue_staticRenderFns__ = [];
/* style */
const __vue_inject_styles__ = undefined;
/* scoped */
const __vue_scope_id__ = undefined;
/* module identifier */
const __vue_module_identifier__ = undefined;
/* functional template */
const __vue_is_functional_template__ = false;
/* style inject */
/* style inject SSR */
var Upload = __chunk_6.__vue_normalize__(
{ render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
__vue_inject_styles__,
__vue_script__,
__vue_scope_id__,
__vue_is_functional_template__,
__vue_module_identifier__,
undefined,
undefined
);
var Plugin = {
install: function install(Vue) {
__chunk_6.registerComponent(Vue, Upload);
}
};
__chunk_6.use(Plugin);
exports.Upload = Upload;
exports.default = Plugin;