mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Cleanup Jua upload handler
This commit is contained in:
parent
9130da9dca
commit
7a0ec76d25
3 changed files with 46 additions and 120 deletions
|
|
@ -1045,9 +1045,6 @@ export class ComposePopupView extends AbstractViewPopup {
|
||||||
attachmentSizeLimit = pInt(SettingsGet('AttachmentLimit'));
|
attachmentSizeLimit = pInt(SettingsGet('AttachmentLimit'));
|
||||||
|
|
||||||
oJua
|
oJua
|
||||||
// .on('onLimitReached', (limit) => {
|
|
||||||
// alert(limit);
|
|
||||||
// })
|
|
||||||
.on('onDragEnter', () => {
|
.on('onDragEnter', () => {
|
||||||
this.dragAndDropOver(true);
|
this.dragAndDropOver(true);
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -281,7 +281,6 @@ export class ContactsPopupView extends AbstractViewPopup {
|
||||||
const j = new Jua({
|
const j = new Jua({
|
||||||
action: serverRequest('UploadContacts'),
|
action: serverRequest('UploadContacts'),
|
||||||
limit: 1,
|
limit: 1,
|
||||||
disableDocumentDropPrevent: true,
|
|
||||||
clickElement: this.importButton()
|
clickElement: this.importButton()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
162
vendors/jua/jua.js
vendored
162
vendors/jua/jua.js
vendored
|
|
@ -6,32 +6,28 @@
|
||||||
* @param {*} aItems
|
* @param {*} aItems
|
||||||
* @param {Function} fFileCallback
|
* @param {Function} fFileCallback
|
||||||
* @param {number=} iLimit = 20
|
* @param {number=} iLimit = 20
|
||||||
* @param {Function=} fLimitCallback
|
|
||||||
*/
|
*/
|
||||||
getDataFromFiles = (aItems, fFileCallback, iLimit, fLimitCallback) =>
|
getDataFromFiles = (aItems, fFileCallback, iLimit) =>
|
||||||
{
|
{
|
||||||
if (aItems?.length)
|
if (aItems?.length)
|
||||||
{
|
{
|
||||||
let
|
let
|
||||||
oFile,
|
oFile,
|
||||||
iInputLimit = iLimit,
|
iCount = 0,
|
||||||
bUseLimit = 0 < iLimit,
|
bCallLimit = false
|
||||||
bCallLimit = !fLimitCallback
|
|
||||||
;
|
;
|
||||||
|
|
||||||
[...aItems].forEach(oItem => {
|
[...aItems].forEach(oItem => {
|
||||||
if (oItem)
|
if (oItem) {
|
||||||
{
|
if (iLimit && iLimit < ++iCount) {
|
||||||
if (!bUseLimit || 0 <= --iLimit)
|
if (!bCallLimit) {
|
||||||
{
|
bCallLimit = true;
|
||||||
|
// fLimitCallback(iLimit);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
oFile = getDataFromFile(oItem);
|
oFile = getDataFromFile(oItem);
|
||||||
oFile && fFileCallback(oFile);
|
oFile && fFileCallback(oFile);
|
||||||
}
|
}
|
||||||
else if (bUseLimit && !bCallLimit)
|
|
||||||
{
|
|
||||||
bCallLimit = true;
|
|
||||||
fLimitCallback(iInputLimit);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -57,14 +53,7 @@
|
||||||
: null; // Folder
|
: null; // Folder
|
||||||
},
|
},
|
||||||
|
|
||||||
eventContainsFiles = oEvent =>
|
eventContainsFiles = oEvent => oEvent.dataTransfer.types.includes('Files');
|
||||||
{
|
|
||||||
try {
|
|
||||||
return oEvent.dataTransfer.types.includes('Files');
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Queue extends Array
|
class Queue extends Array
|
||||||
{
|
{
|
||||||
|
|
@ -111,8 +100,7 @@
|
||||||
onDragEnter: null,
|
onDragEnter: null,
|
||||||
onDragLeave: null,
|
onDragLeave: null,
|
||||||
onBodyDragEnter: null,
|
onBodyDragEnter: null,
|
||||||
onBodyDragLeave: null,
|
onBodyDragLeave: null
|
||||||
onLimitReached: null
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.oXhrs = {};
|
self.oXhrs = {};
|
||||||
|
|
@ -120,11 +108,13 @@
|
||||||
self.options = Object.assign({
|
self.options = Object.assign({
|
||||||
action: '',
|
action: '',
|
||||||
name: 'uploader',
|
name: 'uploader',
|
||||||
hidden: {},
|
limit: 0,
|
||||||
limit: 0
|
// clickElement:
|
||||||
|
// dragAndDropElement:
|
||||||
}, options || {});
|
}, options || {});
|
||||||
self.oQueue = new Queue();
|
self.oQueue = new Queue();
|
||||||
|
|
||||||
|
// clickElement
|
||||||
if (el) {
|
if (el) {
|
||||||
el.style.position = 'relative';
|
el.style.position = 'relative';
|
||||||
el.style.overflow = 'hidden';
|
el.style.overflow = 'hidden';
|
||||||
|
|
@ -136,111 +126,58 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
el = options.dragAndDropElement;
|
el = options.dragAndDropElement;
|
||||||
if (el)
|
if (el) {
|
||||||
{
|
addEventListeners(doc, {
|
||||||
let oBigDropZone = options.dragAndDropBodyElement || doc;
|
dragover: oEvent => {
|
||||||
|
if (eventContainsFiles(oEvent)) {
|
||||||
if (!options.disableDocumentDropPrevent)
|
timerStop();
|
||||||
{
|
if (el.contains(oEvent.target)) {
|
||||||
doc.addEventListener('dragover', oEvent => {
|
oEvent.dataTransfer.dropEffect = 'copy';
|
||||||
if (eventContainsFiles(oEvent))
|
oEvent.stopPropagation();
|
||||||
{
|
} else {
|
||||||
try
|
|
||||||
{
|
|
||||||
oEvent.dataTransfer.dropEffect = 'none';
|
oEvent.dataTransfer.dropEffect = 'none';
|
||||||
oEvent.preventDefault();
|
|
||||||
}
|
|
||||||
catch (oExc) {
|
|
||||||
console.error(oExc);
|
|
||||||
}
|
}
|
||||||
|
oEvent.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
}
|
|
||||||
|
|
||||||
if (oBigDropZone)
|
|
||||||
{
|
|
||||||
addEventListeners(oBigDropZone, {
|
|
||||||
dragover: () => timerStop(),
|
|
||||||
dragenter: oEvent => {
|
|
||||||
if (eventContainsFiles(oEvent))
|
|
||||||
{
|
|
||||||
timerStop();
|
|
||||||
oEvent.preventDefault();
|
|
||||||
|
|
||||||
self.runEvent('onBodyDragEnter', oEvent);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
dragleave: oEvent =>
|
|
||||||
oEvent.dataTransfer && timerStart(() => self.runEvent('onBodyDragLeave', oEvent)),
|
|
||||||
drop: oEvent => {
|
|
||||||
if (oEvent.dataTransfer) {
|
|
||||||
let bFiles = eventContainsFiles(oEvent);
|
|
||||||
bFiles && oEvent.preventDefault();
|
|
||||||
|
|
||||||
self.runEvent('onBodyDragLeave', oEvent);
|
|
||||||
|
|
||||||
return !bFiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
addEventListeners(el, {
|
|
||||||
dragenter: oEvent => {
|
dragenter: oEvent => {
|
||||||
if (eventContainsFiles(oEvent)) {
|
if (eventContainsFiles(oEvent)) {
|
||||||
timerStop();
|
timerStop();
|
||||||
|
|
||||||
oEvent.preventDefault();
|
oEvent.preventDefault();
|
||||||
self.runEvent('onDragEnter', el, oEvent);
|
self.runEvent('onBodyDragEnter', oEvent);
|
||||||
}
|
if (el.contains(oEvent.target)) {
|
||||||
},
|
|
||||||
dragover: oEvent => {
|
|
||||||
if (eventContainsFiles(oEvent)) {
|
|
||||||
try
|
|
||||||
{
|
|
||||||
let sEffect = oEvent.dataTransfer.effectAllowed;
|
|
||||||
|
|
||||||
timerStop();
|
timerStop();
|
||||||
|
self.runEvent('onDragEnter', el, oEvent);
|
||||||
oEvent.dataTransfer.dropEffect = (sEffect === 'move' || sEffect === 'linkMove') ? 'move' : 'copy';
|
|
||||||
|
|
||||||
oEvent.stopPropagation();
|
|
||||||
oEvent.preventDefault();
|
|
||||||
}
|
|
||||||
catch (oExc) {
|
|
||||||
console.error(oExc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
dragleave: oEvent => {
|
dragleave: oEvent => {
|
||||||
if (oEvent.dataTransfer) {
|
if (eventContainsFiles(oEvent)) {
|
||||||
let oRelatedTarget = doc.elementFromPoint(oEvent.clientX, oEvent.clientY);
|
let oRelatedTarget = doc.elementFromPoint(oEvent.clientX, oEvent.clientY);
|
||||||
if (!oRelatedTarget || !el.contains(oRelatedTarget)) {
|
if (!oRelatedTarget || !el.contains(oRelatedTarget)) {
|
||||||
timerStop();
|
|
||||||
self.runEvent('onDragLeave', el, oEvent);
|
self.runEvent('onDragLeave', el, oEvent);
|
||||||
}
|
}
|
||||||
|
timerStart(() => self.runEvent('onBodyDragLeave', oEvent))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
drop: oEvent => {
|
drop: oEvent => {
|
||||||
if (eventContainsFiles(oEvent)) {
|
if (eventContainsFiles(oEvent)) {
|
||||||
|
timerStop();
|
||||||
oEvent.preventDefault();
|
oEvent.preventDefault();
|
||||||
|
if (el.contains(oEvent.target)) {
|
||||||
getDataFromFiles(
|
getDataFromFiles(
|
||||||
oEvent.files || oEvent.dataTransfer.files,
|
oEvent.files || oEvent.dataTransfer.files,
|
||||||
oFile => {
|
oFile => {
|
||||||
if (oFile) {
|
if (oFile) {
|
||||||
self.addFile(oFile);
|
self.addFile(oFile);
|
||||||
timerStop();
|
}
|
||||||
}
|
},
|
||||||
},
|
self.options.limit
|
||||||
self.options.limit,
|
);
|
||||||
self.getEvent('onLimitReached')
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.runEvent('onDragLeave', oEvent);
|
self.runEvent('onDragLeave', oEvent);
|
||||||
|
self.runEvent('onBodyDragLeave', oEvent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -308,7 +245,6 @@
|
||||||
oXhr = new XMLHttpRequest(),
|
oXhr = new XMLHttpRequest(),
|
||||||
oFormData = new FormData(),
|
oFormData = new FormData(),
|
||||||
sAction = this.options.action,
|
sAction = this.options.action,
|
||||||
aHidden = this.options.hidden,
|
|
||||||
fStartFunction = this.getEvent('onStart'),
|
fStartFunction = this.getEvent('onStart'),
|
||||||
fProgressFunction = this.getEvent('onProgress')
|
fProgressFunction = this.getEvent('onProgress')
|
||||||
;
|
;
|
||||||
|
|
@ -350,9 +286,6 @@
|
||||||
fStartFunction && fStartFunction(sUid);
|
fStartFunction && fStartFunction(sUid);
|
||||||
|
|
||||||
oFormData.append(this.options.name, oFileInfo['File']);
|
oFormData.append(this.options.name, oFileInfo['File']);
|
||||||
Object.entries(aHidden).forEach(([key, value]) =>
|
|
||||||
oFormData.append(key, (typeof value === "function" ? value(oFileInfo) : value).toString())
|
|
||||||
);
|
|
||||||
|
|
||||||
oXhr.send(oFormData);
|
oXhr.send(oFormData);
|
||||||
|
|
||||||
|
|
@ -393,10 +326,7 @@
|
||||||
}, 10);
|
}, 10);
|
||||||
};
|
};
|
||||||
if (oInput.files?.length) {
|
if (oInput.files?.length) {
|
||||||
getDataFromFiles(oInput.files, fFileCallback,
|
getDataFromFiles(oInput.files, fFileCallback, limit);
|
||||||
limit,
|
|
||||||
self.getEvent('onLimitReached')
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fFileCallback({
|
fFileCallback({
|
||||||
FileName: oInput.value.split(/\\\//).pop(),
|
FileName: oInput.value.split(/\\\//).pop(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue