var userB64 = null;\r
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
+var shaBaseTree;\r
+var shaNewTree;\r
+var shaNewCommit;\r
+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
//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
$('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
displayLogginModal();\r
});\r
\r
- // Open edit file\r
+ // Activate edit mode\r
$('pre[class=text_label]').click(function(){\r
// the customer is loggued ?\r
if(!sessionStarted || userName == ""){\r
}\r
});\r
\r
+ // Disable the edit mode\r
$('a[id=cancelBtn]').click(function(){\r
if(editComment > 0){ editComment -= 1; }\r
// Hide itself\r
$(this).prev().prev().show();\r
});\r
\r
+ // Display commit form\r
+ $('a[id=commitBtn]').click(function(){\r
+ updateComment = $(this).prev().prev().val();\r
+ commentType = $(this).prev().prev().prev().attr('type');\r
+\r
+ if(updateComment == ""){ displayMessage('The comment field is empty!', 40, 45); }\r
+ else{\r
+ if(!sessionStarted){\r
+ displayMessage("You need to be loggued before commit something", 45, 40);\r
+ displayLogginModal();\r
+ return;\r
+ }\r
+ $('#commitMessage').val('New commit');\r
+ pathFile = $(this).prev().prev().prev().attr('tag');\r
+ $('#modal').show().prepend('<a class="close"><img src="resources/icons/close.png" class="btn_close" title="Close" alt="Close" /></a>');\r
+ $('body').append('<div id="fade"></div>');\r
+ $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();\r
+ }\r
+ });\r
+\r
+ // Close commit form\r
+ $('.btn_close').click(function(){\r
+ $(this).hide(); \r
+ $(this).next().hide(); \r
+ if(editComment > 0){ editComment -= 1; }\r
+ });\r
+\r
+ //Close Popups and Fade Layer\r
+ $('body').on('click', 'a.close, #fade', function() {\r
+ if(editComment > 0){ editComment -= 1; }\r
+ $('#fade , #modal').fadeOut(function() {\r
+ $('#fade, a.close').remove(); \r
+ }); \r
+ $('#modalQuestion').hide();\r
+ });\r
+\r
+ $('#loginAction').click(function(){\r
+ var text;\r
+ var url;\r
+ var line;\r
+ // Look if the customer is logged\r
+ if(!sessionStarted){\r
+ displayMessage("You need to be loggued before commit something", 100, 40);\r
+ $('.popover').show();\r
+ return;\r
+ } \r
+ else{ userB64 = "Basic " + getUserPass("logginNitdoc"); }\r
+ githubRepo = repoName;\r
+ // Check if repo exist\r
+ isRepoExisting();\r
+ if(repoExist){\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
target.show();\r
}\r
\r
+// Init process to commit the new comment\r
+function startCommitProcess()\r
+{\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
+ getLastCommit(); \r
+ getBaseTree(); \r
+ editComment = false;\r
+}\r
+\r
function displayLogginModal(){\r
if ($('.popover').is(':hidden')) { $('.popover').show(); }\r
else { $('.popover').hide(); } \r
function updateDisplaying(){\r
if (checkCookie())\r
{\r
+ userB64 = "Basic " + getUserPass("logginNitdoc"); \r
$('#loginGit').hide();\r
$('#passwordGit').hide();\r
$('#lbpasswordGit').hide(); \r
$('.popover').css({'height' : '80px'}); \r
$('#signIn').text("Sign out"); \r
sessionStarted = true;\r
+ reloadComment();\r
}\r
else\r
{\r
return x.join('');\r
}\r
\r
+\r
+\r
+function getLastCommit() \r
+{ \r
+ var urlHead = '';\r
+ if(sessionStarted){ urlHead = "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/refs/heads/"+branchName;}\r
+ else{ \r
+ // TODO: get url of the original repo.\r
+ return;\r
+ }\r
+\r
+ $.ajax({\r
+ beforeSend: function (xhr) { \r
+ if (userB64 != ""){ xhr.setRequestHeader ("Authorization", userB64); }\r
+ },\r
+ type: "GET",\r
+ url: urlHead,\r
+ dataType:"json",\r
+ async: false,\r
+ success: function(success)\r
+ { \r
+ shaLastCommit = success.object.sha; \r
+ }\r
+ });\r
+}\r
+\r
+function getBaseTree()\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/commits/" + shaLastCommit,\r
+ dataType:"json",\r
+ async: false,\r
+ success: function(success)\r
+ { \r
+ shaBaseTree = success.tree.sha;\r
+ if (state){ setBlob(); }\r
+ else{ return; } \r
+ },\r
+ error: function(){\r
+ return;\r
+ }\r
+ }); \r
+}\r
+\r
+function setNewTree()\r
+{\r
+ $.ajax({ \r
+ beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", userB64); },\r
+ type: "POST",\r
+ url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/trees", \r
+ async: false,\r
+ data:'{ "base_tree" : "'+shaBaseTree+'", '+\r
+ '"tree":[{ '+\r
+ '"path":"'+ pathFile +'",'+\r
+ '"mode":"100644",'+\r
+ '"type":"blob",'+\r
+ '"sha": "'+ shaBlob +'"'+\r
+ '}] '+\r
+ '}', \r
+ success: function(success)\r
+ { // si l'appel a bien fonctionné\r
+ shaNewTree = JSON.parse(success).sha;\r
+ setNewCommit(); \r
+ },\r
+ error: function(){\r
+ return;\r
+ }\r
+ });\r
+}\r
+\r
+function setNewCommit()\r
+{\r
+ $.ajax({ \r
+ beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", userB64); },\r
+ type: "POST",\r
+ url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/commits", \r
+ async: false,\r
+ data:'{ "message" : "'+ commitMessage +'", '+\r
+ '"parents" :"'+shaLastCommit+'",'+ \r
+ '"tree": "'+shaNewTree+'"'+\r
+ '}', \r
+ success: function(success)\r
+ {\r
+ shaNewCommit = JSON.parse(success).sha;\r
+ commit(); \r
+ },\r
+ error: function(){\r
+ return;\r
+ }\r
+ });\r
+}\r
+\r
+//Create a commit\r
+function commit()\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/heads/"+branchName, \r
+ data:'{ "sha" : "'+shaNewCommit+'", '+\r
+ '"force" :"true"'+\r
+ '}',\r
+ success: function(success) { displayMessage('Commit created successfully', 40, 40); },\r
+ error:function(error){ displayMessage('Error ' + JSON.parse(error).object.message, 40, 40); }\r
+ });\r
+}\r
+\r
+// Create a blob\r
+function setBlob()\r
+{\r
+ $.ajax({\r
+ beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", userB64); },\r
+ type: "POST", \r
+ url: "https://api.github.com/repos/"+userName+"/"+githubRepo+"/git/blobs", \r
+ async: false,\r
+ data:'{ "content" : "'+text.replace(/\r?\n/g, '\\n').replace(/\t/g, '\\t').replace(/\"/g,'\\"')+'", '+\r
+ '"encoding" :"utf-8"'+\r
+ '}',\r
+ success: function(success)\r
+ { \r
+ shaBlob = JSON.parse(success).sha;\r
+ setNewTree(); \r
+ },\r
+ error:function(error){ \r
+ displayMessage('Error : Problem parsing JSON', 40, 40); \r
+ return;\r
+ }\r
+ });\r
+}\r
+\r
+// Display file content\r
+function getFileContent(urlFile, newComment)\r
+{\r
+ $.ajax({\r
+ beforeSend: function (xhr) { \r
+ xhr.setRequestHeader ("Accept", "application/vnd.github-blob.raw");\r
+ if (userB64 != ""){ xhr.setRequestHeader ("Authorization", userB64); }\r
+ },\r
+ type: "GET", \r
+ url: urlFile, \r
+ async:false,\r
+ success: function(success)\r
+ {\r
+ state = true;\r
+ replaceComment(newComment, success); \r
+ }\r
+ });\r
+}\r
+\r
+function replaceComment(newComment, fileContent){\r
+ var arrayNew = newComment.split('\n'); \r
+ var lNew = arrayNew.length;\r
+ text = "";\r
+ 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
+ else{\r
+ if(arrayNew[j] == ""){ text += "#"+"\n"; }\r
+ else{ text += "# " + arrayNew[j] + "\n"; }\r
+ }\r
+ }\r
+ }\r
+ else if(i < commentLineStart || i >= commentLineEnd){\r
+ if(i == lines.length-1){ text += lines[i]; }\r
+ else{ text += lines[i] + "\n"; }\r
+ }\r
+ }\r
+ if(addNewComment){\r
+ addNewComment = false;\r
+ }\r
+}\r
+\r
+function getCommentLastCommit(path){\r
+ var urlRaw;\r
+ getLastCommit();\r
+ if(shaLastCommit != ""){\r
+ if (checkCookie()) { urlRaw="https://rawgithub.com/"+ userName +"/"+ repoName +"/" + shaLastCommit + "/" + path; }\r
+ else{ urlRaw="https://rawgithub.com/StefanLage/"+ $('#repoName').attr('name') +"/" + shaLastCommit + "/" + path; }\r
+\r
+ $.ajax({ \r
+ type: "GET", \r
+ url: urlRaw, \r
+ async: false,\r
+ success: function(success)\r
+ {\r
+ currentfileContent = success; \r
+ }\r
+ });\r
+ }\r
+}\r
+\r
+function displayMessage(msg, widthDiv, margModal){\r
+ spinner.stop();\r
+ $('#modal').hide();\r
+ $('#btnCreateBranch').css('margin-left',widthDiv + '%');\r
+ $('#txtQuestion').text(msg);\r
+ $('#btnCreateBranch').text("Ok");\r
+ $('#btnCancelBranch').hide();\r
+ $('#modalQuestion').css({'left' : margModal + '%'})\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
+function displaySpinner(){\r
+ spinner.spin(targetSpinner);\r
+ $("#waitCommit").show();\r
+}\r
+\r
+// Check if the repo already exist\r
+function isRepoExisting(){\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, \r
+ async:false,\r
+ dataType:'json',\r
+ success: function(){ repoExist = true; },\r
+ error: function()\r
+ {\r
+ displayMessage('Repo not found !', 35, 45);\r
+ repoExist = false;\r
+ }\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
});\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