2019-06-06 11:59:11 +00:00
|
|
|
import ngModule from '../../module';
|
2019-10-28 20:40:24 +00:00
|
|
|
import Field from '../field';
|
2019-06-06 11:59:11 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
/**
|
|
|
|
* Input file selector.
|
|
|
|
*
|
|
|
|
* @property {String} accept List of valid file types
|
|
|
|
*/
|
|
|
|
export default class InputFile extends Field {
|
|
|
|
constructor($element, $) {
|
|
|
|
super($element, $);
|
2019-06-06 11:59:11 +00:00
|
|
|
this._multiple = false;
|
2019-10-18 19:36:30 +00:00
|
|
|
this.input = this.element.querySelector('input');
|
2019-06-06 11:59:11 +00:00
|
|
|
this.registerEvents();
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
/*
|
2019-06-06 11:59:11 +00:00
|
|
|
* Registers all event emitters
|
|
|
|
*/
|
|
|
|
registerEvents() {
|
2019-10-28 20:40:24 +00:00
|
|
|
this.input.addEventListener('change', () => {
|
|
|
|
const files = this.input.files;
|
|
|
|
const fileList = Array
|
|
|
|
.from(files)
|
|
|
|
.map(file => file.name)
|
|
|
|
.join(', ');
|
|
|
|
|
2019-06-06 11:59:11 +00:00
|
|
|
const label = this.element.querySelector('.value');
|
2019-10-28 20:40:24 +00:00
|
|
|
label.innerHTML = fileList;
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
this.field = files.length ? files : null;
|
2019-06-06 11:59:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
get field() {
|
|
|
|
return super.field;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
set field(value) {
|
|
|
|
if (!value) this.input.value = '';
|
|
|
|
super.field = value;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-28 20:40:24 +00:00
|
|
|
* @property {FileList} files Selected files list
|
2019-06-06 11:59:11 +00:00
|
|
|
*/
|
|
|
|
get files() {
|
2019-10-28 20:40:24 +00:00
|
|
|
return this.input.files;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set files(value) {
|
2019-10-28 20:40:24 +00:00
|
|
|
this.input.files = value;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-28 20:40:24 +00:00
|
|
|
* @property {Boolean} multiple Whether to allow multiple file selection
|
2019-06-06 11:59:11 +00:00
|
|
|
*/
|
|
|
|
get multiple() {
|
2019-10-28 20:40:24 +00:00
|
|
|
return this.input.multiple;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set multiple(value) {
|
2019-10-28 20:40:24 +00:00
|
|
|
this.input.multiple = value;
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:40:24 +00:00
|
|
|
/*
|
2019-06-06 11:59:11 +00:00
|
|
|
* Fires file selection explorer event
|
|
|
|
*/
|
|
|
|
openFileSelector() {
|
|
|
|
this.input.click();
|
|
|
|
}
|
2019-10-28 20:40:24 +00:00
|
|
|
|
|
|
|
onChange() {
|
|
|
|
this.emit('change', {
|
|
|
|
value: this.field,
|
|
|
|
$files: this.files
|
|
|
|
});
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 19:36:30 +00:00
|
|
|
ngModule.vnComponent('vnInputFile', {
|
2019-06-06 11:59:11 +00:00
|
|
|
template: require('./index.html'),
|
|
|
|
controller: InputFile,
|
|
|
|
bindings: {
|
|
|
|
multiple: '<?',
|
|
|
|
accept: '@?',
|
2019-10-28 20:40:24 +00:00
|
|
|
files: '=?'
|
2019-06-06 11:59:11 +00:00
|
|
|
}
|
|
|
|
});
|