2018-02-02 18:22:32 +00:00
|
|
|
/* global MAXFILESIZE */
|
|
|
|
const { bytes } = require('./utils');
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
export default function(state, emitter) {
|
|
|
|
emitter.on('DOMContentLoaded', () => {
|
|
|
|
document.body.addEventListener('dragover', event => {
|
|
|
|
if (state.route === '/') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
document.body.addEventListener('drop', event => {
|
2018-01-31 23:47:34 +00:00
|
|
|
if (state.route === '/' && !state.uploading) {
|
2017-08-24 21:54:02 +00:00
|
|
|
event.preventDefault();
|
|
|
|
document.querySelector('.upload-window').classList.remove('ondrag');
|
|
|
|
const target = event.dataTransfer;
|
|
|
|
if (target.files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-02 18:22:32 +00:00
|
|
|
if (target.files.length > 1) {
|
2017-08-24 21:54:02 +00:00
|
|
|
return alert(state.translate('uploadPageMultipleFilesAlert'));
|
|
|
|
}
|
|
|
|
const file = target.files[0];
|
2018-02-02 18:22:32 +00:00
|
|
|
if (file.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file.size > MAXFILESIZE) {
|
|
|
|
window.alert(
|
|
|
|
state.translate('fileTooBig', { size: bytes(MAXFILESIZE) })
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-24 21:54:02 +00:00
|
|
|
emitter.emit('upload', { file, type: 'drop' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|