friendica/view/js/dropzone-factory.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

Dropzone.autoDiscover = false;
var DzFactory = function (max_imagesize) {
2023-03-17 10:49:12 +01:00
this.createDropzone = function(dropSelector, textareaElementId) {
return new Dropzone(dropSelector, {
2023-03-15 04:47:20 +01:00
paramName: 'userfile', // The name that will be used to transfer the file
maxFilesize: max_imagesize, // MB
2023-03-17 10:54:31 +01:00
url: '/media/photo/upload?album=',
2023-03-16 12:04:15 +01:00
acceptedFiles: 'image/*',
clickable: true,
dictDefaultMessage: dzStrings.dictDefaultMessage,
dictFallbackMessage: dzStrings.dictFallbackMessage,
dictFallbackText: dzStrings.dictFallbackText,
dictFileTooBig: dzStrings.dictFileTooBig,
dictInvalidFileType: dzStrings.dictInvalidFileType,
dictResponseError: dzStrings.dictResponseError,
dictCancelUpload: dzStrings.dictCancelUpload,
dictUploadCanceled: dzStrings.dictUploadCanceled,
dictCancelUploadConfirmation: dzStrings.dictCancelUploadConfirmation,
dictRemoveFile: dzStrings.dictRemoveFile,
dictMaxFilesExceeded: dzStrings.dictMaxFilesExceeded,
2023-03-15 04:47:20 +01:00
accept: function(file, done) {
done();
},
init: function() {
this.on('success', function(file, serverResponse) {
2023-03-17 10:49:12 +01:00
const targetTextarea = document.getElementById(textareaElementId);
2023-03-15 21:05:16 +01:00
if (targetTextarea.setRangeText) {
2023-03-15 04:47:20 +01:00
//if setRangeText function is supported by current browser
targetTextarea.setRangeText(' ' + serverResponse + ' ');
2023-03-15 04:47:20 +01:00
} else {
2023-03-15 21:05:16 +01:00
targetTextarea.focus();
document.execCommand('insertText', false /*no UI*/, '\n' + serverResponse + '\n');
2023-03-15 04:47:20 +01:00
}
});
this.on('complete', function(file) {
2023-03-17 10:49:12 +01:00
const dz = this;
2023-03-15 04:47:20 +01:00
// Remove just uploaded file from dropzone, makes interface more clear.
// Image can be seen in posting-preview
// We need preview to get optical feedback about upload-progress.
// you see success, when the bb-code link for image is inserted
setTimeout(function(){
dz.removeFile(file);
},5000);
});
},
paste: function(event){
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
items.forEach((item) => {
if (item.kind === 'file') {
// adds the file to your dropzone instance
2023-03-15 21:05:16 +01:00
dz.addFile(item.getAsFile());
2023-03-15 04:47:20 +01:00
}
})
},
});
};
this.copyPaste = function(event, dz) {
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
items.forEach((item) => {
if (item.kind === 'file') {
// adds the file to your dropzone instance
2023-03-15 21:05:16 +01:00
dz.addFile(item.getAsFile());
2023-03-15 04:47:20 +01:00
}
})
};
2023-03-17 10:49:12 +01:00
this.setupDropzone = function(dropSelector, textareaElementId) {
const self = this;
2023-03-17 10:49:12 +01:00
var dropzone = this.createDropzone(dropSelector, textareaElementId);
2023-03-15 21:05:16 +01:00
$(dropSelector).on('paste', function(event) {
self.copyPaste(event, dropzone);
2023-03-15 04:47:20 +01:00
})
};
}