Ghost/ghost/admin/app/components/gh-file-input.js
Kevin Ansfield d14c432264 🐛 Fixed file inputs not allowing same file to be re-uploaded in Firefox
closes https://github.com/TryGhost/Ghost/issues/11707

- Firefox is different to every other browser and doesn't allow file input values to be reset by doing `input.value = null`
- outcome of reset not working is that Firefox won't trigger the change event when the same file is re-selected
- fixed by cloning the input element and replacing it
2020-06-12 13:45:28 +01:00

43 lines
1.1 KiB
JavaScript

import XFileInput from 'emberx-file-input/components/x-file-input';
// TODO: remove this override and use {{x-file-input}} directly once we've
// upgraded to emberx-file-input@1.2.0
export default XFileInput.extend({
change(e) {
let action = this.action;
let files = this.files(e);
if (files.length && action) {
action(files, this.resetInput.bind(this));
}
},
/**
* Gets files from event object.
*
* @method
* @private
* @param {$.Event || Event}
*/
files(e) {
return (e.originalEvent || e).testingFiles || e.target.files;
},
/**
* 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;
input.parentNode.replaceChild(input.cloneNode(true), input);
}
});