From 4e2477a1a5ae59ff4cb34d58eab74297b4ea2d24 Mon Sep 17 00:00:00 2001 From: Justin Nuß Date: Fri, 25 Jul 2014 10:47:37 +0200 Subject: Fix #318. Switch to JS(ON) implementation for issue/comment creation. --- public/js/app.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) (limited to 'public/js/app.js') diff --git a/public/js/app.js b/public/js/app.js index 62482965..a88c8f6b 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -579,26 +579,97 @@ function initIssue() { var $attachedList = $("#attached-list"); var $addButton = $("#attachments-button"); + var files = []; + var fileInput = document.getElementById("attachments-input"); if (fileInput === null) { return; } - fileInput.addEventListener("change", function(event) { - $attachedList.empty(); - $attachedList.append("Attachments: "); + $attachedList.on("click", "span.attachment-remove", function(event) { + var $parent = $(this).parent(); + + files.splice($parent.data("index"), 1); + $parent.remove(); + }); + + var clickedButton = undefined; + + $("button,input[type=\"submit\"]", fileInput.form).on("click", function() { + clickedButton = this; + }); + + fileInput.form.addEventListener("submit", function(event) { + event.stopImmediatePropagation(); + event.preventDefault(); + + //var data = new FormData(this); + + // Internet Explorer ... -_- + var data = new FormData(); + + $.each($("[name]", this), function(i, e) { + if (e.name == "attachments" || e.type == "submit") { + return; + } + + data.append(e.name, $(e).val()); + }); + + data.append(clickedButton.name, $(clickedButton).val()); + + files.forEach(function(file) { + data.append("attachments", file); + }); + + var xhr = new XMLHttpRequest(); + + xhr.addEventListener("error", function() { + debugger; + }); + + xhr.addEventListener("load", function() { + if (xhr.response.ok === false) { + $("#submit-error").text(xhr.response.error); + return; + } + + window.location.href = xhr.response.data; + }); + + xhr.responseType = "json"; + + xhr.open("POST", this.action, true); + xhr.send(data); + + return false; + }); + + fileInput.addEventListener("change", function(event) { for (var index = 0; index < fileInput.files.length; index++) { var file = fileInput.files[index]; + if (files.indexOf(file) > -1) { + continue; + } + var $span = $(""); $span.addClass("label"); $span.addClass("label-default"); - $span.append(file.name.toLowerCase()); + $span.data("index", files.length); + + $span.append(file.name); + $span.append(" "); + $attachedList.append($span); + + files.push(file); } + + this.value = ""; }); $addButton.on("click", function() { -- cgit v1.2.3