2017-01-10 18:02:20 -05:00
|
|
|
/* 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 */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global Dropzone */
|
2017-08-03 16:31:53 -04:00
|
|
|
import _ from 'underscore';
|
2017-05-16 16:52:40 -04:00
|
|
|
import './preview_markdown';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
window.DropzoneInput = (function() {
|
|
|
|
function DropzoneInput(form) {
|
2017-07-17 20:00:35 -04:00
|
|
|
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;
|
|
|
|
|
2017-05-16 04:52:17 -04:00
|
|
|
formTextarea.wrap('<div class="div-dropzone"></div>');
|
|
|
|
formTextarea.on('paste', (function(_this) {
|
2017-03-11 02:30:44 -05:00
|
|
|
return function(event) {
|
|
|
|
return handlePaste(event);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-03-11 02:30:44 -05:00
|
|
|
})(this));
|
2017-04-19 05:04:24 -04:00
|
|
|
|
2017-05-16 04:52:17 -04:00
|
|
|
// Add dropzone area to the form.
|
2017-07-17 20:00:35 -04:00
|
|
|
const $mdArea = formTextarea.closest('.md-area');
|
2017-05-16 04:52:17 -04:00
|
|
|
form.setupMarkdownPreview();
|
2017-07-17 20:00:35 -04:00
|
|
|
const $formDropzone = form.find('.div-dropzone');
|
2017-05-16 04:52:17 -04:00
|
|
|
$formDropzone.parent().addClass('div-dropzone-wrapper');
|
|
|
|
$formDropzone.append(divHover);
|
|
|
|
$formDropzone.find('.div-dropzone-hover').append(iconPaperclip);
|
|
|
|
|
|
|
|
if (!uploadsPath) return;
|
2017-04-19 05:04:24 -04:00
|
|
|
|
2017-07-17 20:00:35 -04:00
|
|
|
const dropzone = $formDropzone.dropzone({
|
2017-05-16 04:52:17 -04:00
|
|
|
url: uploadsPath,
|
|
|
|
dictDefaultMessage: '',
|
2017-03-11 02:30:44 -05:00
|
|
|
clickable: true,
|
2017-05-16 04:52:17 -04:00
|
|
|
paramName: 'file',
|
|
|
|
maxFilesize: maxFileSize,
|
2017-03-11 02:30:44 -05:00
|
|
|
uploadMultiple: false,
|
|
|
|
headers: {
|
2017-05-16 04:52:17 -04:00
|
|
|
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
previewContainer: false,
|
|
|
|
processing: function() {
|
2017-05-16 04:52:17 -04:00
|
|
|
return $('.div-dropzone-alert').alert('close');
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
dragover: function() {
|
|
|
|
$mdArea.addClass('is-dropzone-hover');
|
2017-05-16 04:52:17 -04:00
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0.7);
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
dragleave: function() {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
2017-05-16 04:52:17 -04:00
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0);
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
drop: function() {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
2017-05-16 04:52:17 -04:00
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0);
|
|
|
|
formTextarea.focus();
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
success: function(header, response) {
|
2017-04-07 12:29:08 -04:00
|
|
|
const processingFileCount = this.getQueuedFiles().length + this.getUploadingFiles().length;
|
|
|
|
const shouldPad = processingFileCount >= 1;
|
|
|
|
|
|
|
|
pasteText(response.link.markdown, shouldPad);
|
2017-05-16 04:52:17 -04:00
|
|
|
// Show 'Attach a file' link only when all files have been uploaded.
|
|
|
|
if (!processingFileCount) $attachButton.removeClass('hide');
|
2017-05-03 11:26:49 -04:00
|
|
|
addFileToForm(response.link.url);
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
2017-05-16 04:52:17 -04:00
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
totaluploadprogress: function(totalUploadProgress) {
|
2017-05-16 04:52:17 -04:00
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
},
|
2017-05-16 04:52:17 -04:00
|
|
|
removedfile: function() {
|
|
|
|
$attachButton.removeClass('hide');
|
|
|
|
$cancelButton.addClass('hide');
|
|
|
|
$uploadingProgressContainer.addClass('hide');
|
|
|
|
$uploadingErrorContainer.addClass('hide');
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
queuecomplete: function() {
|
2017-05-16 04:52:17 -04:00
|
|
|
$('.dz-preview').remove();
|
|
|
|
$('.markdown-area').trigger('input');
|
|
|
|
|
|
|
|
$uploadingProgressContainer.addClass('hide');
|
|
|
|
$cancelButton.addClass('hide');
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
});
|
2017-05-16 04:52:17 -04:00
|
|
|
|
2017-07-17 20:00:35 -04:00
|
|
|
const child = $(dropzone[0]).children('textarea');
|
2017-05-16 04:52:17 -04:00
|
|
|
|
|
|
|
// removeAllFiles(true) stops uploading files (if any)
|
|
|
|
// and remove them from dropzone files queue.
|
|
|
|
$cancelButton.on('click', (e) => {
|
2017-07-20 17:27:20 -04:00
|
|
|
const target = e.target.closest('.js-main-target-form').querySelector('.div-dropzone');
|
2017-05-16 04:52:17 -04:00
|
|
|
|
|
|
|
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) => {
|
2017-07-20 17:27:20 -04:00
|
|
|
const dropzoneInstance = Dropzone.forElement(e.target.closest('.js-main-target-form').querySelector('.div-dropzone'));
|
2017-05-16 04:52:17 -04:00
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
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-05-16 04:52:17 -04:00
|
|
|
filename = getFilename(pasteEvent) || 'image.png';
|
|
|
|
text = `{{${filename}}}`;
|
2017-03-11 02:30:44 -05:00
|
|
|
pasteText(text);
|
|
|
|
return uploadFile(image.getAsFile(), filename);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
2017-05-16 04:52:17 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
isImage = function(data) {
|
|
|
|
var i, item;
|
|
|
|
i = 0;
|
|
|
|
while (i < data.clipboardData.items.length) {
|
|
|
|
item = data.clipboardData.items[i];
|
2017-05-16 04:52:17 -04:00
|
|
|
if (item.type.indexOf('image') !== -1) {
|
2017-03-11 02:30:44 -05:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2017-05-16 04:52:17 -04:00
|
|
|
|
2017-04-07 12:29:08 -04:00
|
|
|
pasteText = function(text, shouldPad) {
|
2017-03-11 02:30:44 -05:00
|
|
|
var afterSelection, beforeSelection, caretEnd, caretStart, textEnd;
|
2017-04-07 12:29:08 -04:00
|
|
|
var formattedText = text;
|
|
|
|
if (shouldPad) formattedText += "\n\n";
|
2017-03-28 22:12:51 -04:00
|
|
|
const textarea = child.get(0);
|
|
|
|
caretStart = textarea.selectionStart;
|
|
|
|
caretEnd = textarea.selectionEnd;
|
2017-03-11 02:30:44 -05:00
|
|
|
textEnd = $(child).val().length;
|
|
|
|
beforeSelection = $(child).val().substring(0, caretStart);
|
|
|
|
afterSelection = $(child).val().substring(caretEnd, textEnd);
|
|
|
|
$(child).val(beforeSelection + formattedText + afterSelection);
|
2017-03-28 22:12:51 -04:00
|
|
|
textarea.setSelectionRange(caretStart + formattedText.length, caretEnd + formattedText.length);
|
|
|
|
textarea.style.height = `${textarea.scrollHeight}px`;
|
2017-05-25 06:17:31 -04:00
|
|
|
formTextarea.get(0).dispatchEvent(new Event('input'));
|
2017-05-16 04:52:17 -04:00
|
|
|
return formTextarea.trigger('input');
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2017-05-16 04:52:17 -04:00
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
addFileToForm = function(path) {
|
2017-05-29 03:54:35 -04:00
|
|
|
$(form).append('<input type="hidden" name="files[]" value="' + _.escape(path) + '">');
|
2017-05-03 11:26:49 -04:00
|
|
|
};
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
getFilename = function(e) {
|
|
|
|
var value;
|
|
|
|
if (window.clipboardData && window.clipboardData.getData) {
|
2017-05-16 04:52:17 -04:00
|
|
|
value = window.clipboardData.getData('Text');
|
2017-03-11 02:30:44 -05:00
|
|
|
} else if (e.clipboardData && e.clipboardData.getData) {
|
2017-05-16 04:52:17 -04:00
|
|
|
value = e.clipboardData.getData('text/plain');
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
value = value.split("\r");
|
2017-08-04 04:07:05 -04:00
|
|
|
return value[0];
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2017-05-16 04:52:17 -04:00
|
|
|
|
2017-07-17 20:00:35 -04:00
|
|
|
const showSpinner = function(e) {
|
|
|
|
return $uploadingProgressContainer.removeClass('hide');
|
|
|
|
};
|
|
|
|
|
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
uploadFile = function(item, filename) {
|
|
|
|
var formData;
|
|
|
|
formData = new FormData();
|
2017-05-16 04:52:17 -04:00
|
|
|
formData.append('file', item, filename);
|
2017-03-11 02:30:44 -05:00
|
|
|
return $.ajax({
|
2017-05-16 04:52:17 -04:00
|
|
|
url: uploadsPath,
|
|
|
|
type: 'POST',
|
2017-03-11 02:30:44 -05:00
|
|
|
data: formData,
|
2017-05-16 04:52:17 -04:00
|
|
|
dataType: 'json',
|
2017-03-11 02:30:44 -05:00
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
headers: {
|
2017-05-16 04:52:17 -04:00
|
|
|
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-05-16 04:52:17 -04:00
|
|
|
|
|
|
|
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 -';
|
|
|
|
}
|
|
|
|
|
|
|
|
messageContainer.text(attachingMessage);
|
|
|
|
};
|
|
|
|
|
|
|
|
form.find('.markdown-selector').click(function(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
e.preventDefault();
|
|
|
|
$(this).closest('.gfm-form').find('.div-dropzone').click();
|
2017-05-16 04:52:17 -04:00
|
|
|
formTextarea.focus();
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return DropzoneInput;
|
|
|
|
})();
|