2016-05-27 15:27:46 +03:00
|
|
|
import XFileInput from 'emberx-file-input/components/x-file-input';
|
2022-02-01 12:34:03 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2017-11-22 20:04:48 +03:00
|
|
|
// TODO: remove this override and use {{x-file-input}} directly once we've
|
|
|
|
// upgraded to emberx-file-input@1.2.0
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@classic
|
|
|
|
export default class GhFileInput extends XFileInput {
|
2021-06-07 18:36:21 +03:00
|
|
|
didInsertElement() {
|
2022-02-01 12:34:03 +03:00
|
|
|
super.didInsertElement(...arguments);
|
2021-06-07 18:36:21 +03:00
|
|
|
this.onInsert?.(this.element.querySelector('input[type="file"]'));
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-06-07 18:36:21 +03:00
|
|
|
|
2016-05-27 15:27:46 +03:00
|
|
|
change(e) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let action = this.action;
|
2017-12-12 14:53:35 +03:00
|
|
|
let files = this.files(e);
|
|
|
|
|
|
|
|
if (files.length && action) {
|
|
|
|
action(files, this.resetInput.bind(this));
|
2017-11-25 02:18:35 +03:00
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2017-11-22 20:04:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets files from event object.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @private
|
|
|
|
* @param {$.Event || Event}
|
|
|
|
*/
|
|
|
|
files(e) {
|
2022-02-11 14:07:13 +03:00
|
|
|
return (e.originalEvent || e).testingFiles || e.originalEvent?.files || e.target.files;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2020-06-12 15:44:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the value of the input so you can select the same file
|
|
|
|
* multiple times.
|
|
|
|
*
|
|
|
|
* NOTE: fixes reset in Firefox which doesn't reset like other browsers
|
|
|
|
* when doing input.value = null;
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
resetInput() {
|
|
|
|
let input = this.element.querySelector('.x-file--input');
|
|
|
|
input.removeAttribute('value');
|
|
|
|
input.value = null;
|
2021-06-16 19:56:25 +03:00
|
|
|
|
|
|
|
const clone = input.cloneNode(true);
|
|
|
|
input.parentNode.replaceChild(clone, input);
|
|
|
|
|
|
|
|
return clone;
|
2016-05-27 15:27:46 +03:00
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|