From 0445322625d9faf926bc0019765e01f14a51e74d Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Mon, 27 Nov 2017 21:28:33 -0800 Subject: [PATCH] compose: Export the major upload-related methods. This is a preparatory commit to moving these to their own module. --- static/js/compose.js | 184 ++++++++++++++++++++----------------------- 1 file changed, 87 insertions(+), 97 deletions(-) diff --git a/static/js/compose.js b/static/js/compose.js index 529abee0b5..b5c1134c10 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -43,6 +43,89 @@ function clear_out_file_list(jq_file_list) { // $("#file_input").val(""); } +exports.uploadStarted = function () { + $("#compose-send-button").attr("disabled", ""); + $("#send-status").addClass("alert-info") + .show(); + $(".send-status-close").one('click', exports.abort_xhr); + $("#error-msg").html( + $("

").text(i18n.t("Uploading…")) + .after('

' + + '
' + + '
')); +}; + +exports.progressUpdated = function (i, file, progress) { + $("#upload-bar").width(progress + "%"); +}; + +exports.uploadError = function (err, file) { + var msg; + $("#send-status").addClass("alert-error") + .removeClass("alert-info"); + $("#compose-send-button").prop("disabled", false); + switch (err) { + case 'BrowserNotSupported': + msg = i18n.t("File upload is not yet available for your browser."); + break; + case 'TooManyFiles': + msg = i18n.t("Unable to upload that many files at once."); + break; + case 'FileTooLarge': + // sanitization not needed as the file name is not potentially parsed as HTML, etc. + var context = { file_name: file.name }; + msg = i18n.t('"__file_name__" was too large; the maximum file size is 25MiB.', context); + break; + case 'REQUEST ENTITY TOO LARGE': + msg = i18n.t("Sorry, the file was too large."); + break; + case 'QuotaExceeded': + var translation_part1 = i18n.t('Upload would exceed your maximum quota. You can delete old attachments to free up space.'); + var translation_part2 = i18n.t('Click here'); + msg = translation_part1 + ' ' + translation_part2 + ''; + $("#error-msg").html(msg); + return; + default: + msg = i18n.t("An unknown error occurred."); + break; + } + $("#error-msg").text(msg); +}; + +exports.uploadFinished = function (i, file, response) { + if (response.uri === undefined) { + return; + } + var textbox = $("#new_message_content"); + var split_uri = response.uri.split("/"); + var filename = split_uri[split_uri.length - 1]; + // Urgh, yet another hack to make sure we're "composing" + // when text gets added into the composebox. + if (!compose_state.composing()) { + compose_actions.start('stream'); + } + + var uri = make_upload_absolute(response.uri); + + if (i === -1) { + // This is a paste, so there's no filename. Show the image directly + textbox.val(textbox.val() + "[pasted image](" + uri + ") "); + } else { + // This is a dropped file, so make the filename a link to the image + textbox.val(textbox.val() + "[" + filename + "](" + uri + ")" + " "); + } + compose_ui.autosize_textarea(); + $("#compose-send-button").prop("disabled", false); + $("#send-status").removeClass("alert-info") + .hide(); + + // In order to upload the same file twice in a row, we need to clear out + // the #file_input element, so that the next time we use the file dialog, + // an actual change event is fired. This is extracted to a function + // to abstract away some IE hacks. + clear_out_file_list($("#file_input")); +}; + function show_all_everyone_warnings() { var stream_count = stream_data.get_subscriber_count(compose_state.stream_name()) || 0; @@ -789,99 +872,6 @@ exports.initialize = function () { Dropbox.choose(options); }); - function uploadStarted() { - $("#compose-send-button").attr("disabled", ""); - $("#send-status").addClass("alert-info") - .show(); - $(".send-status-close").one('click', exports.abort_xhr); - $("#error-msg").html( - $("

").text(i18n.t("Uploading…")) - .after('

' + - '
' + - '
')); - } - - function progressUpdated(i, file, progress) { - $("#upload-bar").width(progress + "%"); - } - - function uploadError(err, file) { - var msg; - $("#send-status").addClass("alert-error") - .removeClass("alert-info"); - $("#compose-send-button").prop("disabled", false); - switch (err) { - case 'BrowserNotSupported': - msg = i18n.t("File upload is not yet available for your browser."); - break; - case 'TooManyFiles': - msg = i18n.t("Unable to upload that many files at once."); - break; - case 'FileTooLarge': - // sanitization not needed as the file name is not potentially parsed as HTML, etc. - var context = { file_name: file.name }; - msg = i18n.t('"__file_name__" was too large; the maximum file size is 25MiB.', context); - break; - case 'REQUEST ENTITY TOO LARGE': - msg = i18n.t("Sorry, the file was too large."); - break; - case 'QuotaExceeded': - var translation_part1 = i18n.t('Upload would exceed your maximum quota. You can delete old attachments to free up space.'); - var translation_part2 = i18n.t('Click here'); - msg = translation_part1 + ' ' + translation_part2 + ''; - $("#error-msg").html(msg); - return; - default: - msg = i18n.t("An unknown error occurred."); - break; - } - $("#error-msg").text(msg); - } - - function uploadFinished(i, file, response) { - if (response.uri === undefined) { - return; - } - var textbox = $("#new_message_content"); - var split_uri = response.uri.split("/"); - var filename = split_uri[split_uri.length - 1]; - // Urgh, yet another hack to make sure we're "composing" - // when text gets added into the composebox. - if (!compose_state.composing()) { - compose_actions.start('stream'); - } - - var uri = make_upload_absolute(response.uri); - - if (i === -1) { - // This is a paste, so there's no filename. Show the image directly - textbox.val(textbox.val() + "[pasted image](" + uri + ") "); - } else { - // This is a dropped file, so make the filename a link to the image - textbox.val(textbox.val() + "[" + filename + "](" + uri + ")" + " "); - } - compose_ui.autosize_textarea(); - $("#compose-send-button").prop("disabled", false); - $("#send-status").removeClass("alert-info") - .hide(); - - // In order to upload the same file twice in a row, we need to clear out - // the #file_input element, so that the next time we use the file dialog, - // an actual change event is fired. This is extracted to a function - // to abstract away some IE hacks. - clear_out_file_list($("#file_input")); - } - - // Expose the internal file upload functions to the desktop app, - // since the linux/windows QtWebkit based apps upload images - // directly to the server - if (window.bridge) { - exports.uploadStarted = uploadStarted; - exports.progressUpdated = progressUpdated; - exports.uploadError = uploadError; - exports.uploadFinished = uploadFinished; - } - $("#compose").filedrop({ url: "/json/user_uploads", fallback_id: "file_input", @@ -892,10 +882,10 @@ exports.initialize = function () { csrfmiddlewaretoken: csrf_token, }, raw_droppable: ['text/uri-list', 'text/plain'], - drop: uploadStarted, - progressUpdated: progressUpdated, - error: uploadError, - uploadFinished: uploadFinished, + drop: exports.uploadStarted, + progressUpdated: exports.progressUpdated, + error: exports.uploadError, + uploadFinished: exports.uploadFinished, rawDrop: function (contents) { var textbox = $("#new_message_content"); if (!compose_state.composing()) {