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 */
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-11-03 17:38:54 -04:00
|
|
|
require('./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-05-01 09:14:35 -04:00
|
|
|
var $mdArea, alertAttr, alertClass, appendToTextArea, btnAlert, child, closeAlertMessage, closeSpinner, divAlert, divHover, divSpinner, dropzone, form_dropzone, form_textarea, getFilename, handlePaste, iconPaperclip, iconSpinner, insertToTextArea, isImage, max_file_size, pasteText, uploads_path, showError, showSpinner, uploadFile, uploadProgress;
|
2017-03-11 02:30:44 -05:00
|
|
|
Dropzone.autoDiscover = false;
|
|
|
|
alertClass = "alert alert-danger alert-dismissable div-dropzone-alert";
|
|
|
|
alertAttr = "class=\"close\" data-dismiss=\"alert\"" + "aria-hidden=\"true\"";
|
|
|
|
divHover = "<div class=\"div-dropzone-hover\"></div>";
|
|
|
|
divSpinner = "<div class=\"div-dropzone-spinner\"></div>";
|
|
|
|
divAlert = "<div class=\"" + alertClass + "\"></div>";
|
|
|
|
iconPaperclip = "<i class=\"fa fa-paperclip div-dropzone-icon\"></i>";
|
|
|
|
iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>";
|
|
|
|
uploadProgress = $("<div class=\"div-dropzone-progress\"></div>");
|
|
|
|
btnAlert = "<button type=\"button\"" + alertAttr + ">×</button>";
|
2017-05-01 09:14:35 -04:00
|
|
|
uploads_path = window.uploads_path || null;
|
2017-03-11 02:30:44 -05:00
|
|
|
max_file_size = gon.max_file_size || 10;
|
|
|
|
form_textarea = $(form).find(".js-gfm-input");
|
|
|
|
form_textarea.wrap("<div class=\"div-dropzone\"></div>");
|
|
|
|
form_textarea.on('paste', (function(_this) {
|
|
|
|
return function(event) {
|
|
|
|
return handlePaste(event);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-03-11 02:30:44 -05:00
|
|
|
})(this));
|
|
|
|
$mdArea = $(form_textarea).closest('.md-area');
|
|
|
|
$(form).setupMarkdownPreview();
|
|
|
|
form_dropzone = $(form).find('.div-dropzone');
|
|
|
|
form_dropzone.parent().addClass("div-dropzone-wrapper");
|
|
|
|
form_dropzone.append(divHover);
|
|
|
|
form_dropzone.find(".div-dropzone-hover").append(iconPaperclip);
|
|
|
|
form_dropzone.append(divSpinner);
|
|
|
|
form_dropzone.find(".div-dropzone-spinner").append(iconSpinner);
|
|
|
|
form_dropzone.find(".div-dropzone-spinner").append(uploadProgress);
|
|
|
|
form_dropzone.find(".div-dropzone-spinner").css({
|
|
|
|
"opacity": 0,
|
|
|
|
"display": "none"
|
|
|
|
});
|
2017-04-19 05:04:24 -04:00
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
if (!uploads_path) return;
|
2017-04-19 05:04:24 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
dropzone = form_dropzone.dropzone({
|
2017-05-01 09:14:35 -04:00
|
|
|
url: uploads_path,
|
2017-03-11 02:30:44 -05:00
|
|
|
dictDefaultMessage: "",
|
|
|
|
clickable: true,
|
|
|
|
paramName: "file",
|
|
|
|
maxFilesize: max_file_size,
|
|
|
|
uploadMultiple: false,
|
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
|
|
|
|
},
|
|
|
|
previewContainer: false,
|
|
|
|
processing: function() {
|
|
|
|
return $(".div-dropzone-alert").alert("close");
|
|
|
|
},
|
|
|
|
dragover: function() {
|
|
|
|
$mdArea.addClass('is-dropzone-hover');
|
|
|
|
form.find(".div-dropzone-hover").css("opacity", 0.7);
|
|
|
|
},
|
|
|
|
dragleave: function() {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
|
|
|
form.find(".div-dropzone-hover").css("opacity", 0);
|
|
|
|
},
|
|
|
|
drop: function() {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
|
|
|
form.find(".div-dropzone-hover").css("opacity", 0);
|
|
|
|
form_textarea.focus();
|
|
|
|
},
|
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
error: function(temp) {
|
|
|
|
var checkIfMsgExists, errorAlert;
|
|
|
|
errorAlert = $(form).find('.error-alert');
|
|
|
|
checkIfMsgExists = errorAlert.children().length;
|
|
|
|
if (checkIfMsgExists === 0) {
|
|
|
|
errorAlert.append(divAlert);
|
|
|
|
$(".div-dropzone-alert").append(btnAlert + "Attaching the file failed.");
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
totaluploadprogress: function(totalUploadProgress) {
|
|
|
|
uploadProgress.text(Math.round(totalUploadProgress) + "%");
|
|
|
|
},
|
|
|
|
sending: function() {
|
|
|
|
form_dropzone.find(".div-dropzone-spinner").css({
|
2016-07-24 16:45:11 -04:00
|
|
|
"opacity": 0.7,
|
|
|
|
"display": "inherit"
|
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
queuecomplete: function() {
|
|
|
|
uploadProgress.text("");
|
|
|
|
$(".dz-preview").remove();
|
|
|
|
$(".markdown-area").trigger("input");
|
|
|
|
$(".div-dropzone-spinner").css({
|
2016-07-24 16:45:11 -04:00
|
|
|
"opacity": 0,
|
|
|
|
"display": "none"
|
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
child = $(dropzone[0]).children("textarea");
|
|
|
|
handlePaste = function(event) {
|
|
|
|
var filename, image, pasteEvent, text;
|
|
|
|
pasteEvent = event.originalEvent;
|
|
|
|
if (pasteEvent.clipboardData && pasteEvent.clipboardData.items) {
|
|
|
|
image = isImage(pasteEvent);
|
|
|
|
if (image) {
|
|
|
|
event.preventDefault();
|
|
|
|
filename = getFilename(pasteEvent) || "image.png";
|
|
|
|
text = "{{" + filename + "}}";
|
|
|
|
pasteText(text);
|
|
|
|
return uploadFile(image.getAsFile(), filename);
|
2016-07-24 16:45:11 -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];
|
|
|
|
if (item.type.indexOf("image") !== -1) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
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-12 10:52:45 -04:00
|
|
|
form_textarea.trigger("input");
|
|
|
|
form_textarea.get(0).dispatchEvent(new Event('input'));
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
getFilename = function(e) {
|
|
|
|
var value;
|
|
|
|
if (window.clipboardData && window.clipboardData.getData) {
|
|
|
|
value = window.clipboardData.getData("Text");
|
|
|
|
} else if (e.clipboardData && e.clipboardData.getData) {
|
|
|
|
value = e.clipboardData.getData("text/plain");
|
|
|
|
}
|
|
|
|
value = value.split("\r");
|
|
|
|
return value.first();
|
|
|
|
};
|
|
|
|
uploadFile = function(item, filename) {
|
|
|
|
var formData;
|
|
|
|
formData = new FormData();
|
|
|
|
formData.append("file", item, filename);
|
|
|
|
return $.ajax({
|
2017-05-01 09:14:35 -04:00
|
|
|
url: uploads_path,
|
2017-03-11 02:30:44 -05:00
|
|
|
type: "POST",
|
|
|
|
data: formData,
|
|
|
|
dataType: "json",
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
|
|
|
|
},
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
insertToTextArea = function(filename, url) {
|
|
|
|
return $(child).val(function(index, val) {
|
2017-03-28 22:12:51 -04:00
|
|
|
return val.replace("{{" + filename + "}}", url);
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
appendToTextArea = function(url) {
|
|
|
|
return $(child).val(function(index, val) {
|
|
|
|
return val + url + "\n";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
showSpinner = function(e) {
|
|
|
|
return form.find(".div-dropzone-spinner").css({
|
|
|
|
"opacity": 0.7,
|
|
|
|
"display": "inherit"
|
|
|
|
});
|
|
|
|
};
|
|
|
|
closeSpinner = function() {
|
|
|
|
return form.find(".div-dropzone-spinner").css({
|
|
|
|
"opacity": 0,
|
|
|
|
"display": "none"
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
showError = function(message) {
|
|
|
|
var checkIfMsgExists, errorAlert;
|
|
|
|
errorAlert = $(form).find('.error-alert');
|
|
|
|
checkIfMsgExists = errorAlert.children().length;
|
|
|
|
if (checkIfMsgExists === 0) {
|
|
|
|
errorAlert.append(divAlert);
|
|
|
|
return $(".div-dropzone-alert").append(btnAlert + message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
closeAlertMessage = function() {
|
|
|
|
return form.find(".div-dropzone-alert").alert("close");
|
|
|
|
};
|
|
|
|
form.find(".markdown-selector").click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(this).closest('.gfm-form').find('.div-dropzone').click();
|
2017-03-28 22:12:51 -04:00
|
|
|
form_textarea.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;
|
|
|
|
})();
|