2022-09-01 00:16:41 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {guidFor} from '@ember/object/internals';
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2022-09-01 00:16:41 +03:00
|
|
|
export default class GhFileInput extends Component {
|
|
|
|
inputId = `fileInput-${guidFor(this)}`;
|
|
|
|
inputElement = null;
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2022-09-01 00:16:41 +03:00
|
|
|
get alt() {
|
|
|
|
return this.args.alt === undefined ? 'Upload' : this.args.alt;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-06-07 18:36:21 +03:00
|
|
|
|
2022-09-01 00:16:41 +03:00
|
|
|
@action
|
|
|
|
onChange(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
const files = this.files(e);
|
2017-12-12 14:53:35 +03:00
|
|
|
|
2022-09-01 00:16:41 +03:00
|
|
|
if (files.length) {
|
|
|
|
this.args.action?.(files, this.resetInput);
|
2017-11-25 02:18:35 +03:00
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2017-11-22 20:04:48 +03:00
|
|
|
|
2022-09-01 00:16:41 +03:00
|
|
|
@action
|
|
|
|
registerFileInput(inputElement) {
|
|
|
|
this.inputElement = inputElement;
|
2022-09-02 11:19:11 +03:00
|
|
|
this.args.onInsert?.(this.inputElement);
|
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
|
|
|
|
*/
|
2022-09-01 00:16:41 +03:00
|
|
|
@action
|
2020-06-12 15:44:58 +03:00
|
|
|
resetInput() {
|
2022-09-01 00:16:41 +03:00
|
|
|
const input = this.inputElement;
|
2020-06-12 15:44:58 +03:00
|
|
|
input.removeAttribute('value');
|
|
|
|
input.value = null;
|
2021-06-16 19:56:25 +03:00
|
|
|
|
|
|
|
const clone = input.cloneNode(true);
|
2022-09-01 00:16:41 +03:00
|
|
|
|
|
|
|
this.inputElement = clone;
|
2021-06-16 19:56:25 +03:00
|
|
|
input.parentNode.replaceChild(clone, input);
|
|
|
|
|
|
|
|
return clone;
|
2016-05-27 15:27:46 +03:00
|
|
|
}
|
2022-09-01 00:16:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets files from event object.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @private
|
|
|
|
* @param {$.Event || Event}
|
|
|
|
*/
|
|
|
|
files(e) {
|
|
|
|
return (e.originalEvent || e).testingFiles || e.originalEvent?.files || e.target.files || e.files;
|
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|