nitdoc: migrate github modalbox to JQuery.UI widget
[nit.git] / share / nitdoc / js / plugins / github.js
index f92f5c0..92405d9 100644 (file)
  */\r
 define([\r
        "jquery",\r
-       "base64",\r
-       "plugins/github/github_api",\r
+       "github-api",\r
+       "plugins/modalbox",\r
        "plugins/github/loginbox",\r
-       "plugins/github/modalbox",\r
        "plugins/github/commentbox",\r
-       "Markdown.Converter",\r
-], function($, Base64, GithubAPI, LoginBox, ModalBox, CommentBox) {\r
+       "utils",\r
+       "Markdown.Converter"\r
+], function($, GithubAPI) {\r
        var GithubUser = function(login, password, repo, branch) {\r
                this.login = login;\r
                this.password = password;\r
                this.repo = repo;\r
-               this.auth = "Basic " +  Base64.encode(login + ':' + password);\r
+               this.auth = "Basic " +  (login + ':' + password).base64Encode();\r
                this.branch = branch;\r
        }\r
 \r
@@ -92,11 +92,13 @@ define([
 \r
                _checkLoginInfos: function(infos) {\r
                        if(!infos.login || !infos.password || !infos.repo || !infos.branch) {\r
-                               ModalBox.open(\r
-                                       "Sign in error",\r
-                                       "Please enter your GitHub username, password, repository and branch.",\r
-                                       true\r
-                               );\r
+                               $("<p/>")\r
+                               .text("Please enter your GitHub username, password, repository and branch.")\r
+                               .modalbox({\r
+                                       title: "Sign in error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        } else {\r
                                return true;\r
@@ -110,25 +112,31 @@ define([
                                        this.activate(this.user, this.origin);\r
                                } else {\r
                                        if(isok == "error:login") {\r
-                                               ModalBox.open(\r
-                                                       "Sign in error",\r
-                                                       "The username, password, repo or branch you entered is incorrect.",\r
-                                                       true\r
-                                               );\r
+                                               $("<p/>")\r
+                                               .text("The username, password, repo or branch you entered is incorrect.")\r
+                                               .modalbox({\r
+                                                       title: "Github sign in error",\r
+                                                       isError: true\r
+                                               })\r
+                                               .modalbox("open");\r
                                        } else if(isok == "error:sha") {\r
-                                               ModalBox.open(\r
-                                                       "Base commit not found",\r
-                                                       "The provided Github repository must contain the base commit '" + UI.origin.sha + "'",\r
-                                                       true\r
-                                               );\r
+                                               $("<p/>")\r
+                                               .text("The provided Github repository must contain the base commit '" + UI.origin.sha + "'.")\r
+                                               .modalbox({\r
+                                                       title: "Github base commit error",\r
+                                                       isError: true\r
+                                               })\r
+                                               .modalbox("open");\r
                                        } else if(isok == "error:profile") {\r
-                                               ModalBox.open(\r
-                                                       "Incomplete Github profile",\r
-                                                       "Please set your public name and email in your " +\r
+                                               $("<p/>")\r
+                                               .text("Please set your public name and email in your " +\r
                                                        "<a href='https://github.com/settings/profile'>GitHub profile</a>." +\r
-                                                       "<br/><br/>Your public profile informations are used to sign-off your commits.",\r
-                                                       true\r
-                                               );\r
+                                                       "<br/><br/>Your public profile informations are used to sign-off your commits.")\r
+                                               .modalbox({\r
+                                                       title: "Github profile error",\r
+                                                       isError: true\r
+                                               })\r
+                                               .modalbox("open");\r
                                        }\r
                                }\r
                        }\r
@@ -139,7 +147,7 @@ define([
                                var session = JSON.parse(localStorage.user);\r
                                var isok = this._tryLogin(\r
                                        session.login,\r
-                                       Base64.decode(session.password),\r
+                                       session.password.base64Decode(),\r
                                        session.repo,\r
                                        session.branch\r
                                );\r
@@ -173,7 +181,7 @@ define([
                _saveSession: function(user) {\r
                        localStorage.user = JSON.stringify({\r
                                login: user.login,\r
-                               password: Base64.encode(user.password),\r
+                               password: user.password.base64Encode(),\r
                                repo: user.repo,\r
                                branch: user.branch,\r
                        });\r
@@ -223,7 +231,12 @@ define([
                                .bind("commentbox_preview", function(event, data) {\r
                                        var converter = new Markdown.Converter()\r
                                        var html = converter.makeHtml(data.value);\r
-                                       ModalBox.open("Preview", html, false);\r
+                                       $("<p/>")\r
+                                       .html(html)\r
+                                       .modalbox({\r
+                                               title: "Preview comment"\r
+                                       })\r
+                                       .modalbox("open");\r
                                })\r
                                .bind("commentbox_open", function(event, data) {\r
                                        GithubUI.openedComments++;\r
@@ -248,7 +261,7 @@ define([
                                var request = requests[i];\r
                                $("textarea[data-comment-location=\"" + request.location + "\"]").each(function () {\r
                                        if(request.isClosed) {\r
-                                               var oldComment = Base64.decode(request.oldComment);\r
+                                               var oldComment = request.oldComment.base64Decode();\r
                                                var htmlComment = converter.makeHtml(oldComment);\r
                                                $(this).val(oldComment);\r
                                                if(!$(this).val()) {\r
@@ -259,7 +272,7 @@ define([
                                                $(this).nextAll("div.comment").find("div.nitdoc").empty().html(htmlComment);\r
                                                $(this).nextAll("p.info").find("a.nitdoc-github-editComment").show();\r
                                        } else {\r
-                                               var newComment = Base64.decode(request.comment);\r
+                                               var newComment = request.comment.base64Decode();\r
                                                var htmlComment = converter.makeHtml(newComment);\r
                                                $(this).val(newComment);\r
                                                if(!$(this).val()) {\r
@@ -318,7 +331,13 @@ define([
                        edit.newContent = this._mergeComment(edit.oldContent, edit.newComment, edit.location);\r
                        edit.request = this._pushChanges(edit)\r
                        if(!edit.request) {\r
-                               ModalBox.open("Unable to commit changes!", response, true);\r
+                               $("<p/>")\r
+                               .text("Unable to commit changes.<br/>" + response)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return;\r
                        }\r
                        this._saveRequest(edit);\r
@@ -331,8 +350,8 @@ define([
                        requests[edit.request.number] = {\r
                                request: edit.request,\r
                                location: edit.location.origin,\r
-                               comment: Base64.encode(edit.newComment),\r
-                               oldComment: Base64.encode(edit.oldComment)\r
+                               comment: edit.newComment.base64Encode(),\r
+                               oldComment: edit.oldComment.base64Encode()\r
                        };\r
                        localStorage.requests = JSON.stringify(requests);\r
                },\r
@@ -349,31 +368,61 @@ define([
                _pushChanges: function(edit) {\r
                        var baseTree = GithubAPI.getTree(this.user, this.origin.sha);\r
                        if(!baseTree.sha) {\r
-                               ModalBox.open("Unable to locate base tree!", baseTree.status + ": " + baseTree.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to locate base tree.<br/>" + baseTree.status + ": " + baseTree.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        console.log("Base tree: " + baseTree.url);\r
                        var newBlob = GithubAPI.createBlob(this.user, edit.newContent);\r
                        if(!newBlob.sha) {\r
-                               ModalBox.open("Unable to create new blob!", newBlob.status + ": " + newBlob.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to create new blob.<br/>" + newBlob.status + ": " + newBlob.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        console.log("New blob: " + newBlob.url);\r
                        var newTree = GithubAPI.createTree(this.user, baseTree, edit.location.path, newBlob);\r
                        if(!newTree.sha) {\r
-                               ModalBox.open("Unable to create new tree!", newTree.status + ": " + newTree.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to create new tree.<br/>" + newTree.status + ": " + newTree.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        console.log("New tree: " + newTree.url);\r
                        var newCommit = GithubAPI.createCommit(this.user, edit.message, baseTree.sha, newTree);\r
                        if(!newCommit.sha) {\r
-                               ModalBox.open("Unable to create new commit!", newCommit.status + ": " + newCommit.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to create new commit.<br/>" + newCommit.status + ": " + newCommit.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        console.log("New commit: " + newCommit.url);\r
                        var pullRequest = GithubAPI.createPullRequest(this.user, edit.title, "Pull request from Nitdoc", this.origin, newCommit.sha);\r
                        if(!pullRequest.number) {\r
-                               ModalBox.open("Unable to create pull request!", pullRequest.status + ": " + pullRequest.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to create pull request.<br/>" + pullRequest.status + ": " + pullRequest.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        console.log("New pull request: " + pullRequest.url);\r
@@ -384,13 +433,25 @@ define([
                _closePullRequest: function(number) {\r
                        var requests = JSON.parse(localStorage.requests);\r
                        if(!requests[number]) {\r
-                               ModalBox.open("Unable to close pull request!", "Pull request " + number + "not found", true);\r
+                               $("<p/>")\r
+                               .text("Unable to close pull request.<br/>" + "Pull request " + number + "not found")\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        // close pull request\r
                        var res = GithubAPI.updatePullRequest(this.user, "Closed from Nitdoc", "", "closed", requests[number].request);\r
                        if(!res.id) {\r
-                               ModalBox.open("Unable to close pull request!", res.status + ": " + res.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to close pull request.<br/>" + res.status + ": " + res.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return false;\r
                        }\r
                        // update in localstorage\r
@@ -413,11 +474,17 @@ define([
                _getFileContent: function(githubUrl) {\r
                        var origFile = GithubAPI.getFile(this.user, githubUrl);\r
                        if(!origFile.content) {\r
-                               ModalBox.open("Unable to locate source file!", origFile.status + ": " + origFile.statusText, true);\r
+                               $("<p/>")\r
+                               .text("Unable to locate source file.<br/>" + origFile.status + ": " + origFile.statusText)\r
+                               .modalbox({\r
+                                       title: "Github commit error",\r
+                                       isError: true\r
+                               })\r
+                               .modalbox("open");\r
                                return;\r
                        }\r
                        var base64Content = origFile.content.substring(0, origFile.content.length - 1)\r
-                       return Base64.decode(base64Content);\r
+                       return base64Content.base64Decode();\r
                },\r
 \r
                _mergeComment: function(fileContent, comment, location) {\r
@@ -458,7 +525,7 @@ define([
 \r
                _doUpdateRequest: function(event, baseArea, request) {\r
                        baseArea.commentbox("open", this.user, request.request.number);\r
-               }\r
+               },\r
        }\r
 \r
        // Get github plugin data\r