src/doc/api: add links to renderer code
[nit.git] / share / nitdoc / js / plugins / github.js
index f92f5c0..c77c0d0 100644 (file)
  */\r
 define([\r
        "jquery",\r
-       "base64",\r
-       "plugins/github/github_api",\r
+       "github-api",\r
+       "highlight",\r
+       "marked",\r
+       "nit",\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
+], function($, GithubAPI, hljs, marked) {\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
@@ -42,12 +44,23 @@ define([
                init: function(upstream, basesha1) {\r
                        console.info("Github plugin: init GitHub module (upstream: "+ upstream +", base: " + basesha1 + ")");\r
                        this.origin = this._parseUpstream(upstream);\r
+                       this._initMarked();\r
                        // Add github menu\r
-                       $("nav.main ul").append(\r
-                               $("<li/>")\r
-                               .attr("id", "nitdoc-github-li")\r
+                       $("#topmenu>.container-fluid").append(\r
+                               $("<a/>")\r
+                               .attr({\r
+                                       "id": "nitdoc-github-li",\r
+                                       "type": "button",\r
+                                       "class": "navbar-btn navbar-right btn-link",\r
+                                       "href": "#",\r
+                                       "data-container": "body",\r
+                                       "data-toggle": "popover",\r
+                                       "data-placement": "bottom",\r
+                                       "data-content": "bottom",\r
+                                       "data-html": "true",\r
+                               })\r
                                .loginbox()\r
-                               .loginbox("displayLogin")\r
+                               //.loginbox("displayLogin")\r
                                .bind("loginbox_logoff", function() {\r
                                        GithubUI.disactivate();\r
                                })\r
@@ -92,11 +105,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 +125,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 '" + this.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 +160,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 +194,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
@@ -221,9 +242,38 @@ define([
                                        GithubUI._reloadComments();\r
                                })\r
                                .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
+                                       $("<div/>")\r
+                                       .append($("<h4/>").text("Comment:"))\r
+                                       .append(\r
+                                               $("<div/>")\r
+                                               .addClass("description")\r
+                                               .append(\r
+                                                       $("<div/>")\r
+                                                       .addClass("comment")\r
+                                                       .append(\r
+                                                               $("<div/>")\r
+                                                               .addClass("nitdoc")\r
+                                                               .html(marked(data.value))\r
+                                                       )\r
+                                               )\r
+                                       )\r
+                                       .append($("<h4/>").text("Message:"))\r
+                                       .append(\r
+                                               $("<div/>")\r
+                                               .addClass("description")\r
+                                               .append(\r
+                                                       $("<div/>")\r
+                                                       .addClass("comment")\r
+                                                       .append(\r
+                                                               $("<div/>").html(marked(data.message))\r
+                                                       )\r
+                                               )\r
+                                       )\r
+                                       .modalbox({\r
+                                               title: "Preview comment",\r
+                                               css: {"min-width": "500px"}\r
+                                       })\r
+                                       .modalbox("open");\r
                                })\r
                                .bind("commentbox_open", function(event, data) {\r
                                        GithubUI.openedComments++;\r
@@ -240,7 +290,6 @@ define([
                _reloadComments: function() {\r
                        if(!localStorage.requests){ return; }\r
                        $("p.pullRequest").remove();\r
-                       var converter = new Markdown.Converter();\r
                        var requests = JSON.parse(localStorage.requests);\r
                        // Look for modified comments in page\r
                        for(i in requests) {\r
@@ -248,8 +297,8 @@ 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 htmlComment = converter.makeHtml(oldComment);\r
+                                               var oldComment = request.oldComment.base64Decode();\r
+                                               var htmlComment = marked(oldComment);\r
                                                $(this).val(oldComment);\r
                                                if(!$(this).val()) {\r
                                                        $(this).nextAll("div.comment:first").hide();\r
@@ -259,8 +308,8 @@ 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 htmlComment = converter.makeHtml(newComment);\r
+                                               var newComment = request.comment.base64Decode();\r
+                                               var htmlComment = marked(newComment);\r
                                                $(this).val(newComment);\r
                                                if(!$(this).val()) {\r
                                                        $(this).nextAll("div.comment:first").hide();\r
@@ -318,7 +367,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 +386,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 +404,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 +469,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
@@ -400,6 +497,26 @@ define([
 \r
                /* internals */\r
 \r
+               _initMarked: function() {\r
+                       var renderer = new marked.Renderer();\r
+                       renderer.code = function(code) {\r
+                               return '<pre class="nitcode hljs">' + hljs.highlight('nit', code).value + '</pre>';\r
+                       }\r
+                       renderer.codespan = function(code) {\r
+                               return '<code class="nitcode hljs">' + hljs.highlight('nit', code).value + '</code>';\r
+                       }\r
+                       marked.setOptions({\r
+                               renderer: renderer,\r
+                               gfm: true,\r
+                               tables: true,\r
+                               breaks: true,\r
+                               pedantic: false,\r
+                               sanitize: true,\r
+                               smartLists: true,\r
+                               smartypants: false\r
+                       });\r
+               },\r
+\r
                _parseUpstream: function(upstream) {\r
                        var parts = upstream.split(":");\r
                        return {\r
@@ -413,11 +530,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 +581,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