debian-mirror-gitlab/app/assets/javascripts/dropzone_input.js

309 lines
11 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable func-names, space-before-function-paren, wrap-iife, max-len, one-var, no-var, one-var-declaration-per-line, no-unused-vars, camelcase, quotes, no-useless-concat, prefer-template, quote-props, comma-dangle, object-shorthand, consistent-return, prefer-arrow-callback */
/* global Dropzone */
2017-09-10 17:25:29 +05:30
import _ from 'underscore';
import './preview_markdown';
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
window.DropzoneInput = (function() {
function DropzoneInput(form) {
2017-09-10 17:25:29 +05:30
const divHover = '<div class="div-dropzone-hover"></div>';
const iconPaperclip = '<i class="fa fa-paperclip div-dropzone-icon"></i>';
const $attachButton = form.find('.button-attach-file');
const $attachingFileMessage = form.find('.attaching-file-message');
const $cancelButton = form.find('.button-cancel-uploading-files');
const $retryLink = form.find('.retry-uploading-link');
const $uploadProgress = form.find('.uploading-progress');
const $uploadingErrorContainer = form.find('.uploading-error-container');
const $uploadingErrorMessage = form.find('.uploading-error-message');
const $uploadingProgressContainer = form.find('.uploading-progress-container');
const uploadsPath = window.uploads_path || null;
const maxFileSize = gon.max_file_size || 10;
const formTextarea = form.find('.js-gfm-input');
let handlePaste;
let pasteText;
let addFileToForm;
let updateAttachingMessage;
let isImage;
let getFilename;
let uploadFile;
formTextarea.wrap('<div class="div-dropzone"></div>');
formTextarea.on('paste', (function(_this) {
2017-08-17 22:00:37 +05:30
return function(event) {
return handlePaste(event);
2016-09-13 17:45:13 +05:30
};
2017-08-17 22:00:37 +05:30
})(this));
2017-09-10 17:25:29 +05:30
// Add dropzone area to the form.
const $mdArea = formTextarea.closest('.md-area');
form.setupMarkdownPreview();
const $formDropzone = form.find('.div-dropzone');
$formDropzone.parent().addClass('div-dropzone-wrapper');
$formDropzone.append(divHover);
$formDropzone.find('.div-dropzone-hover').append(iconPaperclip);
if (!uploadsPath) return;
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
const dropzone = $formDropzone.dropzone({
url: uploadsPath,
dictDefaultMessage: '',
2017-08-17 22:00:37 +05:30
clickable: true,
2017-09-10 17:25:29 +05:30
paramName: 'file',
maxFilesize: maxFileSize,
2017-08-17 22:00:37 +05:30
uploadMultiple: false,
headers: {
2017-09-10 17:25:29 +05:30
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
2017-08-17 22:00:37 +05:30
},
previewContainer: false,
processing: function() {
2017-09-10 17:25:29 +05:30
return $('.div-dropzone-alert').alert('close');
2017-08-17 22:00:37 +05:30
},
dragover: function() {
$mdArea.addClass('is-dropzone-hover');
2017-09-10 17:25:29 +05:30
form.find('.div-dropzone-hover').css('opacity', 0.7);
2017-08-17 22:00:37 +05:30
},
dragleave: function() {
$mdArea.removeClass('is-dropzone-hover');
2017-09-10 17:25:29 +05:30
form.find('.div-dropzone-hover').css('opacity', 0);
2017-08-17 22:00:37 +05:30
},
drop: function() {
$mdArea.removeClass('is-dropzone-hover');
2017-09-10 17:25:29 +05:30
form.find('.div-dropzone-hover').css('opacity', 0);
formTextarea.focus();
2017-08-17 22:00:37 +05:30
},
success: function(header, response) {
const processingFileCount = this.getQueuedFiles().length + this.getUploadingFiles().length;
const shouldPad = processingFileCount >= 1;
pasteText(response.link.markdown, shouldPad);
2017-09-10 17:25:29 +05:30
// Show 'Attach a file' link only when all files have been uploaded.
if (!processingFileCount) $attachButton.removeClass('hide');
addFileToForm(response.link.url);
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
error: function(file, errorMessage = 'Attaching the file failed.', xhr) {
// If 'error' event is fired by dropzone, the second parameter is error message.
// If the 'errorMessage' parameter is empty, the default error message is set.
// If the 'error' event is fired by backend (xhr) error response, the third parameter is
// xhr object (xhr.responseText is error message).
// On error we hide the 'Attach' and 'Cancel' buttons
// and show an error.
// If there's xhr error message, let's show it instead of dropzone's one.
const message = xhr ? xhr.responseText : errorMessage;
$uploadingErrorContainer.removeClass('hide');
$uploadingErrorMessage.html(message);
$attachButton.addClass('hide');
$cancelButton.addClass('hide');
2017-08-17 22:00:37 +05:30
},
totaluploadprogress: function(totalUploadProgress) {
2017-09-10 17:25:29 +05:30
updateAttachingMessage(this.files, $attachingFileMessage);
$uploadProgress.text(Math.round(totalUploadProgress) + '%');
},
sending: function(file) {
// DOM elements already exist.
// Instead of dynamically generating them,
// we just either hide or show them.
$attachButton.addClass('hide');
$uploadingErrorContainer.addClass('hide');
$uploadingProgressContainer.removeClass('hide');
$cancelButton.removeClass('hide');
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
removedfile: function() {
$attachButton.removeClass('hide');
$cancelButton.addClass('hide');
$uploadingProgressContainer.addClass('hide');
$uploadingErrorContainer.addClass('hide');
2017-08-17 22:00:37 +05:30
},
queuecomplete: function() {
2017-09-10 17:25:29 +05:30
$('.dz-preview').remove();
$('.markdown-area').trigger('input');
$uploadingProgressContainer.addClass('hide');
$cancelButton.addClass('hide');
2017-08-17 22:00:37 +05:30
}
});
2017-09-10 17:25:29 +05:30
const child = $(dropzone[0]).children('textarea');
// removeAllFiles(true) stops uploading files (if any)
// and remove them from dropzone files queue.
$cancelButton.on('click', (e) => {
const target = e.target.closest('form').querySelector('.div-dropzone');
e.preventDefault();
e.stopPropagation();
Dropzone.forElement(target).removeAllFiles(true);
});
// If 'error' event is fired, we store a failed files,
// clear dropzone files queue, change status of failed files to undefined,
// and add that files to the dropzone files queue again.
// addFile() adds file to dropzone files queue and upload it.
$retryLink.on('click', (e) => {
const dropzoneInstance = Dropzone.forElement(e.target.closest('form').querySelector('.div-dropzone'));
const failedFiles = dropzoneInstance.files;
e.preventDefault();
// 'true' parameter of removeAllFiles() cancels uploading of files that are being uploaded at the moment.
dropzoneInstance.removeAllFiles(true);
failedFiles.map((failedFile, i) => {
const file = failedFile;
if (file.status === Dropzone.ERROR) {
file.status = undefined;
file.accepted = undefined;
}
return dropzoneInstance.addFile(file);
});
});
2017-08-17 22:00:37 +05:30
handlePaste = function(event) {
var filename, image, pasteEvent, text;
pasteEvent = event.originalEvent;
if (pasteEvent.clipboardData && pasteEvent.clipboardData.items) {
image = isImage(pasteEvent);
if (image) {
event.preventDefault();
2017-09-10 17:25:29 +05:30
filename = getFilename(pasteEvent) || 'image.png';
text = `{{${filename}}}`;
2017-08-17 22:00:37 +05:30
pasteText(text);
return uploadFile(image.getAsFile(), filename);
}
}
};
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
isImage = function(data) {
var i, item;
i = 0;
while (i < data.clipboardData.items.length) {
item = data.clipboardData.items[i];
2017-09-10 17:25:29 +05:30
if (item.type.indexOf('image') !== -1) {
2017-08-17 22:00:37 +05:30
return item;
}
i += 1;
}
return false;
};
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
pasteText = function(text, shouldPad) {
var afterSelection, beforeSelection, caretEnd, caretStart, textEnd;
var formattedText = text;
if (shouldPad) formattedText += "\n\n";
const textarea = child.get(0);
caretStart = textarea.selectionStart;
caretEnd = textarea.selectionEnd;
textEnd = $(child).val().length;
beforeSelection = $(child).val().substring(0, caretStart);
afterSelection = $(child).val().substring(caretEnd, textEnd);
$(child).val(beforeSelection + formattedText + afterSelection);
textarea.setSelectionRange(caretStart + formattedText.length, caretEnd + formattedText.length);
textarea.style.height = `${textarea.scrollHeight}px`;
2017-09-10 17:25:29 +05:30
formTextarea.get(0).dispatchEvent(new Event('input'));
return formTextarea.trigger('input');
};
addFileToForm = function(path) {
$(form).append('<input type="hidden" name="files[]" value="' + _.escape(path) + '">');
2017-08-17 22:00:37 +05:30
};
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
getFilename = function(e) {
var value;
if (window.clipboardData && window.clipboardData.getData) {
2017-09-10 17:25:29 +05:30
value = window.clipboardData.getData('Text');
2017-08-17 22:00:37 +05:30
} else if (e.clipboardData && e.clipboardData.getData) {
2017-09-10 17:25:29 +05:30
value = e.clipboardData.getData('text/plain');
2017-08-17 22:00:37 +05:30
}
value = value.split("\r");
2017-09-10 17:25:29 +05:30
return value[0];
};
const showSpinner = function(e) {
return $uploadingProgressContainer.removeClass('hide');
2017-08-17 22:00:37 +05:30
};
2017-09-10 17:25:29 +05:30
const closeSpinner = function() {
return $uploadingProgressContainer.addClass('hide');
};
const showError = function(message) {
$uploadingErrorContainer.removeClass('hide');
$uploadingErrorMessage.html(message);
};
const closeAlertMessage = function() {
return form.find('.div-dropzone-alert').alert('close');
};
const insertToTextArea = function(filename, url) {
return $(child).val(function(index, val) {
return val.replace(`{{${filename}}}`, url);
});
};
const appendToTextArea = function(url) {
return $(child).val(function(index, val) {
return val + url + "\n";
});
};
2017-08-17 22:00:37 +05:30
uploadFile = function(item, filename) {
var formData;
formData = new FormData();
2017-09-10 17:25:29 +05:30
formData.append('file', item, filename);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url: uploadsPath,
type: 'POST',
2017-08-17 22:00:37 +05:30
data: formData,
2017-09-10 17:25:29 +05:30
dataType: 'json',
2017-08-17 22:00:37 +05:30
processData: false,
contentType: false,
headers: {
2017-09-10 17:25:29 +05:30
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
2017-08-17 22:00:37 +05:30
},
beforeSend: function() {
showSpinner();
return closeAlertMessage();
},
success: function(e, textStatus, response) {
return insertToTextArea(filename, response.responseJSON.link.markdown);
},
error: function(response) {
return showError(response.responseJSON.message);
},
complete: function() {
return closeSpinner();
2016-09-13 17:45:13 +05:30
}
});
2017-08-17 22:00:37 +05:30
};
2017-09-10 17:25:29 +05:30
updateAttachingMessage = (files, messageContainer) => {
let attachingMessage;
const filesCount = files.filter(function(file) {
return file.status === 'uploading' ||
file.status === 'queued';
}).length;
// Dinamycally change uploading files text depending on files number in
// dropzone files queue.
if (filesCount > 1) {
attachingMessage = 'Attaching ' + filesCount + ' files -';
} else {
attachingMessage = 'Attaching a file -';
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
messageContainer.text(attachingMessage);
2017-08-17 22:00:37 +05:30
};
2017-09-10 17:25:29 +05:30
form.find('.markdown-selector').click(function(e) {
2017-08-17 22:00:37 +05:30
e.preventDefault();
$(this).closest('.gfm-form').find('.div-dropzone').click();
2017-09-10 17:25:29 +05:30
formTextarea.focus();
2017-08-17 22:00:37 +05:30
});
}
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
return DropzoneInput;
})();