ni_nitdoc: new workflow for comment modification
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 29 Aug 2013 17:12:31 +0000 (13:12 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 29 Aug 2013 17:12:31 +0000 (13:12 -0400)
Edit original comment
1- Create commit
2- Create pull request
3- Reload comment from pull request

Cancel modification
1- Close pull request
2- Reload comment from original

Update modification
1- Open edition from last pull request
2- Close previous pull request
3- Create commit and new pull request
4- Reload comment from last pull request

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

share/ni_nitdoc/scripts/github.js
share/ni_nitdoc/styles/github.css
src/ni_nitdoc.nit

index 1e932ad..37daf4a 100644 (file)
@@ -10,7 +10,7 @@ $(document).ready(function() {
        if(session) {\r
                githubAPI = new GitHubAPI(session.user, session.password, session.repo)\r
                ui.activate();\r
-               console.log("Session started from cookie (head: "+ $("body").attr("data-github-head") +", head: "+ $("body").attr("data-github-base") +")");\r
+               console.log("Session started from cookie (head: "+ $("body").attr("data-github-head") +", base: "+ $("body").attr("data-github-base") +")");\r
 \r
        } else {\r
                console.log("No cookie found");\r
@@ -254,26 +254,26 @@ function GitHubAPI(login, password, repo) {
        // update a pull request\r
        this.updatePullRequest = function(title, body, state, number) {\r
                var res = false;\r
-               $.ajax({\r
-               beforeSend: function (xhr) {\r
-                               xhr.setRequestHeader ("Authorization", githubAPI.auth);\r
-               },\r
-               type: "PATCH",\r
-               url: "https://api.github.com/repos/" + this.login + "/" + this.repo + "/pulls/" + number,\r
-                       data: JSON.stringify({\r
-                               title: title,\r
-                               body: body,\r
-                               state: state\r
-                       }),\r
-                       async: false,\r
-               dataType: 'json',\r
-               success: function(response) {\r
-                               res = response;\r
+                       $.ajax({\r
+                       beforeSend: function (xhr) {\r
+                                       xhr.setRequestHeader ("Authorization", githubAPI.auth);\r
                        },\r
-                       error: function(response) {\r
-                               res = response;\r
-                       }\r
-       });\r
+                       type: "PATCH",\r
+                       url: "https://api.github.com/repos/" + this.login + "/" + this.repo + "/pulls/" + number,\r
+                               data: JSON.stringify({\r
+                                       title: title,\r
+                                       body: body,\r
+                                       state: state\r
+                               }),\r
+                               async: false,\r
+                       dataType: 'json',\r
+                       success: function(response) {\r
+                                       res = response;\r
+                               },\r
+                               error: function(response) {\r
+                                       res = response;\r
+                               }\r
+               });\r
                return res;\r
        }\r
 }\r
@@ -503,27 +503,24 @@ function GitHubUI() {
                $('#fade , #modal').hide();\r
        }\r
 \r
-       this.openCommentBox = function(baseArea) {\r
+       this.openCommentBox = function(baseArea, requestID) {\r
                this.openedComments += 1;\r
                // get text and format it\r
-               var formated = "";\r
-               var len = 1;\r
-               var commentLines = baseArea.text().split('\n');\r
-               for (var i = 0; i < commentLines.length; i++) {\r
-                       formated += commentLines[i];\r
-                       if(i < commentLines.length - 2){ formated += "\n"; }\r
+               var originalComment = baseArea.text();\r
+               var modifiedComment;\r
+               if(!!requestID) {\r
+                       // get comment from last pull request\r
+                       var requests = JSON.parse(localStorage.requests);\r
+                       modifiedComment = Base64.decode(requests[requestID].comment);\r
                }\r
-               len = commentLines.length - 1;\r
-\r
                // create comment box\r
-               var tarea = $("<textarea rows='" + len + "'>" + formated + "</textarea>");\r
+               var tarea = $("<textarea>" + (!modifiedComment? originalComment: modifiedComment) + "</textarea>");\r
                var width = width = baseArea.parent().innerWidth() - 13;\r
                tarea.css("width", width + "px");\r
                tarea.css("display", "block");\r
                tarea.keyup(function(event) {\r
                        $(event.target).css("height", (event.target.value.split(/\r|\n/).length * 16) + "px");\r
-                       var baseComment =  $(event.target).parents("div.description").find("textarea.baseComment").text();\r
-                       if ($(event.target).val() != baseComment) {\r
+                       if ( (!requestID && $(event.target).val() != originalComment) || (requestID && $(event.target).val() != originalComment && $(event.target).val() != modifiedComment) ) {\r
                                $(event.target).parent().find("button.commit").removeAttr("disabled");\r
                        } else {\r
                                $(event.target).parent().find("button.commit").attr("disabled", "disabled");\r
@@ -549,7 +546,7 @@ function GitHubUI() {
                        .append(\r
                                $("<button class='commit'>Commit</button>")\r
                                .click(function() {\r
-                                       ui.openCommitBox($(this).parent());\r
+                                       ui.openCommitBox($(this).parent(), requestID);\r
                                })\r
                        )\r
                        .append(\r
@@ -574,7 +571,7 @@ function GitHubUI() {
                commentBox.remove();\r
        }\r
 \r
-       this.openCommitBox = function(commentBox) {\r
+       this.openCommitBox = function(commentBox,  requestID) {\r
                $('#fade').show();\r
                $('#modal')\r
                        .empty()\r
@@ -599,7 +596,7 @@ function GitHubUI() {
                                                $(this).text("Commiting...");\r
                                        })\r
                                        .mouseup(function() {\r
-                                               ui.commit($(this).parent().parent(), commentBox)\r
+                                               ui.commit($(this).parent().parent(), commentBox, requestID)\r
                                        })\r
                                )\r
                        )\r
@@ -611,7 +608,12 @@ function GitHubUI() {
        }\r
 \r
 \r
-       this.commit = function(commitBox, commentBox) {\r
+       this.commit = function(commitBox, commentBox,  requestID) {\r
+               // close existing pull request for the comment\r
+               if(!!requestID) {\r
+                       this.closePullRequest(requestID);\r
+               }\r
+\r
                // get comments datas\r
                var location = this.parseLocation(commentBox.attr("data-comment-location"));\r
                var comment = commentBox.find("textarea").val();\r
@@ -637,7 +639,7 @@ function GitHubUI() {
                // save pull request in cookie\r
                var requests = [];\r
                if(!!localStorage.requests) {requests = JSON.parse(localStorage.requests)}\r
-               requests[requests.length] = {\r
+               requests[response.number] = {\r
                        request: response,\r
                        location: commentBox.attr("data-comment-location"),\r
                        comment: Base64.encode(comment)\r
@@ -698,6 +700,7 @@ function GitHubUI() {
                var converter = new Markdown.Converter();\r
                // Look for modified comments in page\r
                for(i in requests) {\r
+                       if(!requests[i]) { continue; }\r
                        var request = requests[i];\r
                        $("textarea[data-comment-location=\"" + request.location + "\"]").each(function () {\r
                                var div = $(this).next();\r
@@ -709,12 +712,12 @@ function GitHubUI() {
                                                div.remove();\r
                                        } else if(div.is("div.comment.locked")) {\r
                                                // unlock comment\r
+                                               div.empty();\r
+                                               div.append(converter.makeHtml($(this).text()));\r
                                                div.removeClass("locked");\r
                                                div.css("cursor", "pointer")\r
-                                               div.click(function() {\r
-                                                       ui.openCommentBox(div.prev());\r
-                                               });\r
                                                div.next().remove();\r
+                                               div.next().find("span.editComment").show();\r
                                        }\r
                                } else {\r
                                        // create div for the new coment\r
@@ -729,17 +732,26 @@ function GitHubUI() {
                                                div.append(converter.makeHtml(Base64.decode(request.comment)));\r
                                                // lock click\r
                                                div.css("cursor", "auto");\r
-                                               div.unbind("click");\r
                                                div.addClass("locked");\r
+                                               div.next().find("span.editComment").hide();\r
                                                div.after(\r
                                                        $("<p class='locked inheritance'>")\r
                                                        .text("comment modified in ")\r
                                                        .append("<a href='"+ request.request.html_url +"' target='_blank' title='Review on GitHub'>pull request #"+ request.request.number +"</a>")\r
                                                        .append(" ")\r
                                                        .append(\r
+                                                               $("<a data-pullrequest-number='"+ request.request.number +"' class='update'>update</a>")\r
+                                                               .click(function (){\r
+                                                                       div.hide();\r
+                                                                       ui.openCommentBox(div.prev(), $(this).attr("data-pullrequest-number"));\r
+                                                               })\r
+                                                       )\r
+                                                       .append(" ")\r
+                                                       .append(\r
                                                                $("<a data-pullrequest-number='"+ request.request.number +"' class='cancel'>cancel</a>")\r
                                                                .click(function (){\r
                                                                        ui.closePullRequest($(this).attr("data-pullrequest-number"));\r
+                                                                       ui.reloadComments();\r
                                                                })\r
                                                        )\r
                                                );\r
@@ -761,15 +773,12 @@ function GitHubUI() {
                        this.openModalBox("Unable to close pull request!", res.status + ": " + res.statusText, true);\r
                        return false;\r
                }\r
-               // remove from localstorage\r
+               // update in localstorage\r
                var requests = JSON.parse(localStorage.requests);\r
-               for(i in requests) {\r
-                       if(requests[i].request.number == number) {\r
-                               requests[i].isClosed = true;\r
-                       }\r
+               if(!!requests[number]) {\r
+                       requests[number].isClosed = true;\r
                }\r
                localStorage.requests = JSON.stringify(requests);\r
-               ui.reloadComments()\r
        }\r
 \r
        /* Utility */\r
index fc5a225..6280f96 100644 (file)
@@ -263,3 +263,8 @@ a.cancel {
        color: #b33630;\r
        cursor: pointer;\r
 }\r
+\r
+a.update {\r
+       color: orange;\r
+       cursor: pointer;\r
+}\r
index cc0b946..5405e99 100644 (file)
@@ -1734,7 +1734,8 @@ redef class ADoc
                        if text.first == ' ' then text = text.substring_from(1)
                        res.append(text.html_escape)
                end
-               return res.to_s
+               var str = res.to_s
+               return str.substring(0, str.length - 1)
        end
 end