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') 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 From 12fb42de5a5b07ed8dffe91d9536615bbadeedea Mon Sep 17 00:00:00 2001 From: Justin Nuß Date: Fri, 25 Jul 2014 11:13:42 +0200 Subject: Fix IE bug and show errors. --- public/css/gogs.css | 7 +++++++ public/js/app.js | 36 ++++++++++++++++++++++++++++++------ routers/repo/issue.go | 9 ++++----- templates/repo/issue/create.tmpl | 2 +- templates/repo/issue/view.tmpl | 2 +- 5 files changed, 43 insertions(+), 13 deletions(-) (limited to 'public/js') diff --git a/public/css/gogs.css b/public/css/gogs.css index cc48f211..361475bd 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -1836,4 +1836,11 @@ body { #issue-create-form #attached { margin-bottom: 0; +} + +#submit-error { + display: none; + padding: 10px 15px 15px 15px; + font-weight: bold; + text-align: center; } \ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index a88c8f6b..b0dff0ef 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -568,7 +568,7 @@ function initIssue() { }; var out = function() { - $hoverElement.hide(); + //$hoverElement.hide(); }; $(".issue-main .attachments .attachment").hover(over, out); @@ -598,6 +598,13 @@ function initIssue() { $("button,input[type=\"submit\"]", fileInput.form).on("click", function() { clickedButton = this; + + var $button = $(this); + + $button.removeClass("btn-success"); + $button.addClass("btn-warning"); + + $button.text("Submiting..."); }); fileInput.form.addEventListener("submit", function(event) { @@ -630,16 +637,33 @@ function initIssue() { }); xhr.addEventListener("load", function() { - if (xhr.response.ok === false) { - $("#submit-error").text(xhr.response.error); + var response = xhr.response; + + if (typeof response == "string") { + try { + response = JSON.parse(response); + } catch (err) { + response = { ok: false, error: "Could not parse JSON" }; + } + } + + if (response.ok === false) { + $("#submit-error").text(response.error); + $("#submit-error").show(); + + var $button = $(clickedButton); + + $button.removeClass("btn-warning"); + $button.addClass("btn-danger"); + + $button.text("An error encoured!") + return; } - window.location.href = xhr.response.data; + window.location.href = response.data; }); - xhr.responseType = "json"; - xhr.open("POST", this.action, true); xhr.send(data); diff --git a/routers/repo/issue.go b/routers/repo/issue.go index edfa39b0..4465a399 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -189,9 +189,9 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { send := func(status int, data interface{}, err error) { - log.Error("issue.Comment(?): %s", err) - if err != nil { + log.Error("issue.CreateIssuePost(?): %s", err.Error()) + ctx.JSON(status, map[string]interface{}{ "ok": false, "status": status, @@ -711,9 +711,9 @@ func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { func Comment(ctx *middleware.Context, params martini.Params) { send := func(status int, data interface{}, err error) { - log.Error("issue.Comment(?): %s", err) - if err != nil { + log.Error("issue.Comment(?): %s", err.Error()) + ctx.JSON(status, map[string]interface{}{ "ok": false, "status": status, @@ -860,7 +860,6 @@ func Comment(ctx *middleware.Context, params martini.Params) { } } - log.Error("url: %#v", fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index)) send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil) } diff --git a/templates/repo/issue/create.tmpl b/templates/repo/issue/create.tmpl index ad86d4ea..c0188842 100644 --- a/templates/repo/issue/create.tmpl +++ b/templates/repo/issue/create.tmpl @@ -95,7 +95,7 @@
-
+
diff --git a/templates/repo/issue/view.tmpl b/templates/repo/issue/view.tmpl index b5d72281..aec50ca6 100644 --- a/templates/repo/issue/view.tmpl +++ b/templates/repo/issue/view.tmpl @@ -134,7 +134,7 @@
-
+
-- cgit v1.2.3 From f0da8a68c2c409572902275888fec26689c2b0dd Mon Sep 17 00:00:00 2001 From: Justin Nuß Date: Fri, 25 Jul 2014 15:47:42 +0200 Subject: Remove debug comment. --- public/js/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public/js') diff --git a/public/js/app.js b/public/js/app.js index b0dff0ef..d7208119 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -568,7 +568,7 @@ function initIssue() { }; var out = function() { - //$hoverElement.hide(); + $hoverElement.hide(); }; $(".issue-main .attachments .attachment").hover(over, out); -- cgit v1.2.3