nitdoc: Fix bugs: Forgot variable definition
[nit.git] / share / nitdoc / scripts / js-facilities.js
index f310216..84aab54 100644 (file)
@@ -3,6 +3,9 @@ var userB64 = null;
 var sessionStarted = false;\r
 var editComment = 0;\r
 var currentfileContent = '';\r
+var addNewComment = false;\r
+var commentLineStart;\r
+var commentLineEnd;\r
 \r
 // SHA GitHub\r
 var shaLastCommit = "";\r
@@ -12,6 +15,29 @@ var shaNewCommit;
 var shaBlob;\r
 var shaMaster;\r
 var repoExist = false;\r
+var branchExist = false;\r
+var githubRepo;\r
+\r
+// Spinner vars\r
+var opts = {\r
+         lines: 11, // The number of lines to draw\r
+         length: 7, // The length of each line\r
+         width: 4, // The line thickness\r
+         radius: 10, // The radius of the inner circle\r
+         corners: 1, // Corner roundness (0..1)\r
+         rotate: 0, // The rotation offset\r
+         color: '#FFF', // #rgb or #rrggbb\r
+         speed: 1, // Rounds per second\r
+         trail: 60, // Afterglow percentage\r
+         shadow: false, // Whether to render a shadow\r
+         hwaccel: false, // Whether to use hardware acceleration\r
+         className: 'spinner', // The CSS class to assign to the spinner\r
+         zIndex: 99999, // The z-index (defaults to 2000000000)\r
+         top: '300', // Top position relative to parent in px\r
+         left: 'auto' // Left position relative to parent in px\r
+       };\r
+var targetSpinner = document.getElementById('waitCommit');\r
+var spinner = new Spinner(opts).spin(targetSpinner);\r
 \r
 /*\r
 * JQuery Case Insensitive :icontains selector\r
@@ -30,6 +56,12 @@ var currentTable = null;
 //Hightlighted index in search result preview table\r
 var currentIndex = -1;\r
 \r
+// Check if a comment is editing\r
+window.onbeforeunload = function() {\r
+       if(editComment > 0){\r
+       return 'Are you sure you want to leave this page?';\r
+       }\r
+};\r
 \r
 /*\r
 * Add folding and filtering facilities to class description page.\r
@@ -42,9 +74,9 @@ $(document).ready(function() {
        $('a[id=cancelBtn]').hide();\r
        // Hide Authenfication form\r
        $(".popover").hide();\r
+       githubRepo = $('#repoName').attr('name');\r
        // Update display\r
        updateDisplaying();\r
-       \r
        /*\r
        * Highlight the spoted element\r
        */\r
@@ -508,24 +540,78 @@ $(document).ready(function() {
                // Check if repo exist\r
                isRepoExisting();\r
                if(repoExist){\r
-                       editComment -= 1;\r
-                       commitMessage = $('#commitMessage').val();\r
-                       if(commitMessage == ""){ commitMessage = "New commit";}\r
-                       if(sessionStarted){\r
-                               if ($.trim(updateComment) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); }\r
-                               else{ startCommitProcess(); }\r
-                       }\r
-                       $('#modal, #modalQuestion').fadeOut(function() {\r
-                               $('#login').val("");\r
-                               $('#password').val("");\r
-                               $('textarea').hide();\r
-                               $('textarea').prev().show();\r
-                       });\r
-                       $('a[id=cancelBtn]').hide();\r
-                       $('a[id=commitBtn]').hide();\r
-               }\r
+                       isBranchExisting();\r
+                       if(branchExist){\r
+                               editComment -= 1;\r
+                               commitMessage = $('#commitMessage').val();\r
+                               if(commitMessage == ""){ commitMessage = "New commit";}\r
+                               if(sessionStarted){\r
+                                       if ($.trim(updateComment) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); }\r
+                                       else{ \r
+                                               displaySpinner();\r
+                                                       startCommitProcess();\r
+                                       }\r
+                               }\r
+                               $('#modal, #modalQuestion').fadeOut(function() {\r
+                                       $('#login').val("");\r
+                                       $('#password').val("");\r
+                                       $('textarea').hide();\r
+                                       $('textarea').prev().show();\r
+                               });\r
+                               $('a[id=cancelBtn]').hide();\r
+                               $('a[id=commitBtn]').hide();\r
+                               // Re-load all comment\r
+                               reloadComment();\r
+                       }\r
+               }\r
                else{ editComment -= 1; }\r
        });\r
+       \r
+       // Cancel creating branch\r
+       $('#btnCancelBranch').click(function(){\r
+               editComment -= 1;\r
+               $('#modalQuestion').hide();\r
+               $('#fade , #modal').fadeOut(function() { $('#fade, a.close').remove(); });\r
+               return;\r
+       });\r
+\r
+       // Create new branch and continu\r
+       $('#btnCreateBranch').click(function(){\r
+               $('#modalQuestion').hide();\r
+               if($('#btnCreateBranch').text() != 'Ok'){\r
+                       // Create the branch\r
+                       createBranch();\r
+                       commitMessage = $('#commitMessage').val();\r
+                       if(commitMessage == ""){ commitMessage = "New commit"; }\r
+                       if(userB64 != ""){      \r
+                               if ($.trim(updateComment) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); }\r
+                       else{ startCommitProcess(); }\r
+                   }\r
+               }\r
+               else\r
+               {\r
+                       $('#fade , #modalQuestion, #modal').fadeOut(function() { $('#fade, a.close').remove(); });\r
+               }\r
+       });\r
+\r
+       $('a[class=newComment]').click(function(){\r
+               addNewComment = true;                   \r
+               editComment += 1;\r
+               // hide comment\r
+               $(this).hide();\r
+               // Show edit box \r
+               $(this).next().show();\r
+               // Show cancel button\r
+               $(this).next().next().show();\r
+               // Show commit button\r
+               $(this).next().next().next().show();\r
+               // Resize edit box \r
+               $(this).next().height($(this).next().prop("scrollHeight"));\r
+               // Select it\r
+               $(this).next().select();\r
+               preElement = $(this);  \r
+        });\r
+\r
 });\r
 \r
 /* Parse current URL and return anchor name */\r
@@ -580,6 +666,7 @@ function startCommitProcess()
 {\r
        var numL = preElement.attr("title");\r
        commentLineStart = numL.split('-')[0] - 1;      \r
+       if(addNewComment) { commentLineStart++; }\r
        commentLineEnd = (commentLineStart + preElement.text().split('\n').length) - 1;\r
        state = true;   \r
        replaceComment(updateComment, currentfileContent);\r
@@ -597,6 +684,7 @@ function displayLogginModal(){
 function updateDisplaying(){\r
        if (checkCookie())\r
        {\r
+               userB64 = "Basic " + getUserPass("logginNitdoc"); \r
                $('#loginGit').hide();\r
                $('#passwordGit').hide();\r
                $('#lbpasswordGit').hide();             \r
@@ -614,6 +702,7 @@ function updateDisplaying(){
                $('.popover').css({'height' : '80px'}); \r
                $('#signIn').text("Sign out");  \r
                sessionStarted = true;\r
+               reloadComment();\r
        }\r
        else\r
        {\r
@@ -800,21 +889,7 @@ base64.encode = function(s) {
     return x.join('');\r
 }\r
 \r
-$.fn.spin = function(opts) {\r
-  this.each(function() {\r
-    var $this = $(this),\r
-        data = $this.data();\r
 \r
-    if (data.spinner) {\r
-      data.spinner.stop();\r
-      delete data.spinner;\r
-    }\r
-    if (opts !== false) {\r
-      data.spinner = new Spinner($.extend({color: $this.css('color')}, opts)).spin(this);\r
-    }\r
-  });\r
-  return this;\r
-};\r
 \r
 function getLastCommit() \r
 {   \r
@@ -974,6 +1049,13 @@ function replaceComment(newComment, fileContent){
        var lines = fileContent.split("\n");\r
        for (var i = 0; i < lines.length; i++) {\r
                if(i == commentLineStart){\r
+                       if(addNewComment){\r
+                               for(var indexLine=0; indexLine < lines[i+1].length; indexxLine++){\r
+                                       if(lines[i+1].substr(indexLine,1) == "\t" || lines[i+1].substr(indexLine,1) == "#"){ text += lines[i+1].substr(indexLine,1); }\r
+                                       else{ break;}\r
+                               }\r
+                               text += lines[i] + "\n"; \r
+                       }\r
                        // We change the comment\r
                        for(var j = 0; j < lNew; j++){\r
                                if(commentType == 1){ text += "\t# " + arrayNew[j] + "\n"; }\r
@@ -988,6 +1070,9 @@ function replaceComment(newComment, fileContent){
                        else{ text += lines[i] + "\n"; }\r
                }\r
        }\r
+       if(addNewComment){\r
+               addNewComment = false;\r
+       }\r
 }\r
 \r
 function getCommentLastCommit(path){\r
@@ -1010,6 +1095,7 @@ function getCommentLastCommit(path){
 }\r
 \r
 function displayMessage(msg, widthDiv, margModal){\r
+       spinner.stop();\r
        $('#modal').hide();\r
        $('#btnCreateBranch').css('margin-left',widthDiv + '%');\r
        $('#txtQuestion').text(msg);\r
@@ -1022,6 +1108,11 @@ function displayMessage(msg, widthDiv, margModal){
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();\r
 }\r
 \r
+function displaySpinner(){\r
+       spinner.spin(targetSpinner);\r
+       $("#waitCommit").show();\r
+}\r
+\r
 // Check if the repo already exist\r
 function isRepoExisting(){\r
        $.ajax({\r
@@ -1040,3 +1131,102 @@ function isRepoExisting(){
         }\r
     });\r
 }\r
+\r
+// Check if the branch already exist\r
+function isBranchExisting(){\r
+       $.ajax({\r
+               beforeSend: function (xhr) {\r
+                       if (userB64 != "") { xhr.setRequestHeader ("Authorization", userB64); }\r
+               },\r
+               type: "GET",\r
+               url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/refs/heads/"+branchName,\r
+               async:false,\r
+               dataType:'json',\r
+               success: function(){ branchExist = true; },\r
+               error: function()\r
+               {\r
+                       branchExist = false;\r
+                       editComment -= 1;\r
+                       $('#modal').hide();\r
+                       $('#txtQuestion').text("Are you sure you want to create that branch ?");\r
+                       $('#btnCancelBranch').show();\r
+                       $('#btnCreateBranch').text("Yes");\r
+                       $('#modalQuestion').show();\r
+                       $('#modalQuestion').show().prepend('<a class="close"><img src="resources/icons/close.png" class="btnCloseQuestion" title="Close" alt="Close" /></a>');\r
+                       $('body').append('<div id="fade"></div>');\r
+                       $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();\r
+               }\r
+       });\r
+}\r
+\r
+function getMasterSha() \r
+{\r
+    $.ajax({\r
+        beforeSend: function (xhr) { \r
+            if (userB64 != ""){ xhr.setRequestHeader ("Authorization", userB64); }\r
+        },\r
+        type: "GET",\r
+        url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/refs/heads/master",\r
+        dataType:"json",\r
+        async: false,\r
+        success: function(success) { shaMaster = success.object.sha; }\r
+    });\r
+}\r
+\r
+function createBranch(){\r
+\r
+       getMasterSha();\r
+\r
+       $.ajax({ \r
+        beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", userB64); },\r
+        type: "POST",\r
+        url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/refs", \r
+        data:'{ "ref" : "refs/heads/'+branchName+'",'+\r
+                       '"sha" : "'+shaMaster+'"'+\r
+            '}',\r
+        success: function(){ return; },\r
+        error: function(){\r
+               editComment -= 1;\r
+               displayMessage('Impossible to create the new branch : ' + branchName, 40, 40);\r
+        }\r
+    });\r
+}\r
+\r
+$.fn.spin = function(opts) {\r
+  this.each(function() {\r
+    var $this = $(this),\r
+        data = $this.data();\r
+\r
+    if (data.spinner) {\r
+      data.spinner.stop();\r
+      delete data.spinner;\r
+    }\r
+    if (opts !== false) {\r
+      data.spinner = new Spinner($.extend({color: $this.css('color')}, opts)).spin(this);\r
+    }\r
+  });\r
+  return this;\r
+};\r
+\r
+function reloadComment(){\r
+       $.when(getCommentLastCommit($('pre[class=text_label]').attr('tag'))).done(function(){\r
+               $('pre[class=text_label]').each(function(){ getCommentOfFunction($(this)); });\r
+       });\r
+}\r
+\r
+function getCommentOfFunction(element){\r
+       var textC = ""; \r
+       var numL = element.attr("title");\r
+       if(numL != null){                                       \r
+               commentLineStart = numL.split('-')[0] - 1;\r
+               commentLineEnd = (commentLineStart + element.text().split('\n').length) - 1;\r
+               var lines = currentfileContent.split("\n");\r
+               for (var i = 0; i < lines.length; i++) {\r
+                       if(i >= commentLineStart-1 && i <= commentLineEnd){\r
+                               if (lines[i].substr(1,1) == "#"){ textC += lines[i].substr(3,lines[i].length) + "\n";}\r
+                               else if(lines[i].substr(0,1) == '#'){ textC += lines[i].substr(2,lines[i].length) + "\n"; }\r
+               }\r
+           }\r
+           if (textC != ""){ element.text(textC); }\r
+       }\r
+}\r