542011b782787e4f5c04f2645544017e0cd2e82a
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
20 * Nitdoc.Github comment edition module
22 * Allows user to modify source code comments directly from the Nitdoc
25 var Nitdoc
= Nitdoc ||
{};
27 Nitdoc
.GitHub
= {}; // Declare Nitdoc.GitHub submodule
30 $
(document
).ready(function() {
31 //FIXME base should be choosen by user
32 var origin
= $
("body").attr("data-github-origin");
34 Nitdoc
.GitHub
.UI
.init(origin
);
39 * Nitdoc.Github.UI for comment edition module
41 Nitdoc
.GitHub
.UI
= function() {
42 var openedComments
= 0; // currently edited comments count
43 var user
= false; // logged user
46 var init
= function(originStr
) {
47 console
.log("init GitHub module (origin: "+ originStr
+")");
50 var parts
= originStr
.split(":");
57 // check local session
58 if(localStorage
.user
) {
59 var session
= JSON
.parse(localStorage
.user
);
60 var user
= tryLogin(session
.login
, Base64
.decode(session
.password
), session
.repo
, session
.branch
);
62 console
.log("Session found but authentification failed");
66 console
.log("No session found");
70 Nitdoc
.GitHub
.LoginBox
.init("nav.main ul");
72 Nitdoc
.GitHub
.LoginBox
.displayLogout(origin
, user
);
75 Nitdoc
.GitHub
.LoginBox
.displayLogin();
78 // Prevent page unload if there is comments in editing mode
79 $
(window
).on('beforeunload', function() {
80 if(Nitdoc
.GitHub
.UI
.getOpenedComments() > 0){
81 return "There is uncommited modified comments. Are you sure you want to leave this page?";
86 // Activate comment UI for a logged user
87 var activate
= function(loggedUser
) {
92 // check local storage synchro with branch
93 if(localStorage
.latestCommit
!= user
.latest
.sha
) {
94 console
.log("Latest commit changed: cleaned cache");
95 localStorage
.requests
= "[]";
96 localStorage
.latestCommit
= user
.latest
.sha
;
98 console
.log("Latest commit sha: " + localStorage
.latestCommit
);
100 attachCommentEvents();
105 var disactivate
= function() {
106 if(Nitdoc
.GitHub
.UI
.getOpenedComments() > 0){
107 if(!confirm('There is uncommited modified comments. Are you sure you want to leave this page?')) {
111 // close session and purge cookie
112 localStorage
.clear();
113 $
(window
).unbind('beforeunload');
114 window
.location
.reload();
117 // Attempt login through GitHub API
118 var tryLogin
= function(login
, password
, repo
, branch
) {
119 var user
= new Nitdoc
.GitHub
.User(login
, password
, repo
, branch
);
120 if(!Nitdoc
.GitHub
.API
.login(user
)) {
123 // get lastest commit
124 var latest
= Nitdoc
.GitHub
.API
.getLastCommit(user
);
125 if(!latest ||
!latest
.sha
) {
128 user
.latest
= latest
;
132 // Attach edit button on each comment
133 var attachCommentEvents
= function() {
134 // Blocks without comment
135 $
("span.noComment").each(function() {
136 //FIXME this should be done by nitdoc
137 var baseComment
= $
(this).parent().prev();
138 var location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseComment
.attr("data-comment-location"));
139 var locString
= "../" + location
.path
+ ":" + location
.lstart
+ "," + location
.tabpos
+ "--" + location
.lstart
+ ",0";
140 baseComment
.attr("data-comment-location", locString
);
141 $
(this).html("<a class='nitdoc-github-editComment noComment'>add comment</a> for ");
142 $
(this).addClass("nitdoc-github-editComment");
144 // Blocks with comment
145 $
('.description div.comment').each(function() {
146 var p
= $
(this).next();
147 p
.prepend("<span class='nitdoc-github-editComment'><a class='nitdoc-github-editComment'>edit comment</a> for </span>")
150 // Attach links events
151 $
('a.nitdoc-github-editComment').each(function() {
152 $
(this).css("cursor", "pointer")
153 $
(this).click(function() {
155 $
(this).parent().hide();
159 if(!$
(this).hasClass("noComment")) {
160 $
(this).parent().parent().prev().hide();
161 baseTextarea
= $
(this).parent().parent().prev().prev();
163 baseTextarea
= $
(this).parent().parent().prev();
166 infos
.user
= Nitdoc
.GitHub
.UI
.getUser();
167 infos
.location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseTextarea
.attr("data-comment-location"));
168 infos
.namespace = baseTextarea
.attr("data-comment-namespace");
169 infos
.oldComment
= baseTextarea
.val();
170 var box
= new Nitdoc
.GitHub
.CommentBox(infos
);
171 box
.open(baseTextarea
);
176 // reload comments from saved pull request
177 var reloadComments
= function() {
178 if(!localStorage
.requests
){ return; }
179 var requests
= JSON
.parse(localStorage
.requests
);
180 var converter
= new Markdown
.Converter();
181 // Look for modified comments in page
183 if(!requests
[i]) { continue; }
184 var request
= requests
[i];
185 $
("textarea[data-comment-location=\"" + request
.location
+ "\"]").each(function () {
186 var div
= $
(this).next();
187 if(request
.isClosed
) {
188 if(div
.is("div.comment.newComment")) {
189 // hide empty comment
191 div
.next().find("span.noComment").show();
193 } else if(div
.is("div.comment.locked")) {
196 div
.append(converter
.makeHtml($
(this).text()));
197 div
.removeClass("locked");
198 div
.css("cursor", "pointer")
200 div
.next().find("span.nitdoc-github-editComment").show();
203 // create div for the new coment
204 if(!div
.is("div.comment")) {
205 $
(this).after("<div class='comment newComment'></div>");
206 div
= $
(this).next();
208 // lock modified comment
209 if(!div
.hasClass("locked")) {
210 // convert modified comment to markdown
212 div
.append(converter
.makeHtml(Base64
.decode(request
.comment
)));
214 div
.css("cursor", "auto");
215 div
.addClass("locked");
216 div
.next().find("span.nitdoc-github-editComment").hide();
218 $
("<p class='locked inheritance'>")
219 .text("comment modified in ")
220 .append("<a href='"+ request
.request
.html_url
+"' target='_blank' title='Review on GitHub'>pull request #"+ request
.request
.number
+"</a>")
223 $
("<a data-pullrequest-number='"+ request
.request
.number
+"' class='nitdoc-github-update'>update</a>")
225 $
(this).parent().hide();
227 var baseTextarea
= div
.prev();
230 infos
.user
= Nitdoc
.GitHub
.UI
.getUser();
231 infos
.location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseTextarea
.attr("data-comment-location"));
232 infos
.namespace = baseTextarea
.attr("data-comment-namespace");
233 infos
.oldComment
= baseTextarea
.val();
234 infos
.requestID
= $
(this).attr("data-pullrequest-number");
235 var box
= new Nitdoc
.GitHub
.CommentBox(infos
);
236 box
.open(baseTextarea
);
241 $
("<a data-pullrequest-number='"+ request
.request
.number
+"' class='nitdoc-github-cancel'>cancel</a>")
243 ui
.closePullRequest($
(this).attr("data-pullrequest-number"));
249 // hide "add comment" link
250 if(div
.hasClass("newComment")) {
251 div
.next().next().find("span.noComment").hide();
259 // Commit changes and send pull request
260 var saveChanges
= function(infos
) {
261 // if pull request update close existing pull request for the comment
262 if(infos
.requestID
) {
263 closePullRequest(infos
.requestID
);
267 var fileContent
= getFileContent(infos
.location
.path
);
268 infos
.newContent
= Nitdoc
.GitHub
.Utils
.mergeComment(fileContent
, infos
.newComment
, infos
.location
);
271 infos
.request
= pushChanges(infos
)
273 Nitdoc
.GitHub
.ModalBox
.open("Unable to commit changes!", response
, true);
278 // close boxes and reload comments
279 Nitdoc
.GitHub
.CommitBox
.close();
280 infos
.commentBox
.close();
285 Creating a new pull request with the new comment take 5 steps:
286 1. get the base tree from latest commit
288 2. create a new blob with updated file content
289 3. post a new tree from base tree and blob
290 4. post the new commit with new tree
291 5. create the pull request
293 var pushChanges
= function(infos
) {
294 var baseTree
= Nitdoc
.GitHub
.API
.getTree(user
, localStorage
.latestCommit
);
296 Nitdoc
.GitHub
.ModalBox
.open("Unable to locate base tree!", baseTree
.status
+ ": " + baseTree
.statusText
, true);
299 console
.log("Base tree: " + baseTree
.url
);
300 var newBlob
= Nitdoc
.GitHub
.API
.createBlob(user
, infos
.newContent
);
302 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new blob!", newBlob
.status
+ ": " + newBlob
.statusText
, true);
305 console
.log("New blob: " + newBlob
.url
);
306 var newTree
= Nitdoc
.GitHub
.API
.createTree(user
, baseTree
, infos
.location
.path
, newBlob
);
308 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new tree!", newTree
.status
+ ": " + newTree
.statusText
, true);
311 console
.log("New tree: " + newTree
.url
);
312 var newCommit
= Nitdoc
.GitHub
.API
.createCommit(user
, infos
.message
, localStorage
.latestCommit
, newTree
);
314 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new commit!", newCommit
.status
+ ": " + newCommit
.statusText
, true);
317 console
.log("New commit: " + newCommit
.url
);
318 var pullRequest
= Nitdoc
.GitHub
.API
.createPullRequest(user
, infos
.message
.split("\n\n")[0], infos
.message
, origin
, newCommit
.sha
);
319 if(!pullRequest
.number
) {
320 Nitdoc
.GitHub
.ModalBox
.open("Unable to create pull request!", pullRequest
.status
+ ": " + pullRequest
.statusText
, true);
323 console
.log("New pull request: " + pullRequest
.url
);
327 // close previously opened pull request
328 var closePullRequest
= function(number
) {
329 var requests
= JSON
.parse(localStorage
.requests
);
330 if(!requests
[number]) {
331 Nitdoc
.GitHub
.ModalBox
.open("Unable to close pull request!", "Pull request " + number
+ "not found", true);
334 // close pull request
335 var res
= Nitdoc
.GitHub
.API
.updatePullRequest(user
, "Canceled from Nitdoc", "", "closed", requests
[number].request
);
337 Nitdoc
.GitHub
.ModalBox
.open("Unable to close pull request!", res
.status
+ ": " + res
.statusText
, true);
340 // update in localstorage
341 requests
[number].isClosed
= true;
342 localStorage
.requests
= JSON
.stringify(requests
);
345 // Get file content from github
346 var getFileContent
= function(githubUrl
) {
347 var origFile
= Nitdoc
.GitHub
.API
.getFile(user
, githubUrl
);
348 if(!origFile
.content
) {
349 Nitdoc
.GitHub
.ModalBox
.open("Unable to locate source file!", origFile
.status
+ ": " + origFile
.statusText
, true);
352 var base64Content
= origFile
.content
.substring(0, origFile
.content
.length
- 1)
353 return Base64
.decode(base64Content
);
356 // save pull request in local storage
357 var saveRequest
= function(infos
) {
359 if(localStorage
.requests
) {requests
= JSON
.parse(localStorage
.requests
)}
360 requests
[infos
.request
.number
] = {
361 request
: infos
.request
,
362 location
: infos
.location
.origin
,
363 comment
: Base64
.encode(infos
.newComment
)
365 localStorage
.requests
= JSON
.stringify(requests
);
368 // save user in local storage
369 var saveSession
= function(user
) {
372 password
: Base64
.encode(user
.password
),
376 localStorage
.user
= JSON
.stringify(session
);
381 var getUser
= function() { return user
; }
382 var getOrigin
= function() { return origin
; }
383 var getOpenedComments
= function() { return openedComments
; }
384 var addOpenedComments
= function() { openedComments
+= 1; }
385 var remOpenedComments
= function() { openedComments
-= 1; }
392 disactivate
: disactivate
,
394 getOrigin
: getOrigin
,
395 getOpenedComments
: getOpenedComments
,
396 addOpenedComments
: addOpenedComments
,
397 remOpenedComments
: remOpenedComments
,
398 saveChanges
: saveChanges
,
399 closePullRequest
: closePullRequest
,
400 reloadComments
: reloadComments
407 * GitHub API user object
409 Nitdoc
.GitHub
.User
= function(login
, password
, repo
, branch
) {
411 this.password
= password
;
413 this.auth
= "Basic " + Base64
.encode(login
+ ':' + password
);
414 this.branch
= branch
;
420 Nitdoc
.GitHub
.API
= function() {
422 // try to login the user to github API
423 var login
= function(user
) {
426 beforeSend
: function (xhr
) {
427 xhr
.setRequestHeader ("Authorization", user
.auth
);
430 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
,
433 success
: function() {
437 user
.infos
= getUserInfos(user
);
438 user
.signedOff
= getSignedOff(user
)
442 // request for user github account infos
443 var getUserInfos
= function(user
) {
446 beforeSend
: function (xhr
) {
447 xhr
.setRequestHeader ("Authorization", user
.auth
);
450 url
: "https://api.github.com/users/" + user
.login
,
453 success
: function(response
) {
456 error
: function(response
) {
463 // build signedoff user default signature
464 var getSignedOff
= function(user
) {
465 return user
.infos
.name
+ " <" + user
.infos
.email
+ ">";
468 // get the branches list from a repo
469 var getBranches
= function(user
) {
472 beforeSend
: function (xhr
) {
473 xhr
.setRequestHeader ("Authorization", user
.auth
);
476 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/branches",
479 success
: function(response
) {
482 error
: function(response
) {
491 // get the latest commit on `branchName`
492 var getLastCommit
= function(user
) {
495 beforeSend
: function (xhr
) {
496 xhr
.setRequestHeader ("Authorization", user
.auth
);
499 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/refs/heads/" + user
.branch
,
502 success
: function(response
) {
503 res
= response
.object
;
505 error
: function(response
) {
512 // get the base tree for a commit sha
513 var getTree
= function(user
, sha
) {
516 beforeSend
: function (xhr
) {
517 xhr
.setRequestHeader ("Authorization", user
.auth
);
520 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/trees/" + sha
+ "?recursive=1",
523 success
: function(response
) {
526 error
: function(response
) {
534 var createBlob
= function(user
, content
) {
537 beforeSend
: function (xhr
) {
538 xhr
.setRequestHeader ("Authorization", user
.auth
);
541 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/blobs",
544 data
: JSON
.stringify({
545 content
: Base64
.encode(content
),
548 success
: function(response
) {
551 error
: function(response
) {
558 // create a new tree from a base tree
559 var createTree
= function(user
, baseTree
, path
, blob
) {
562 beforeSend
: function (xhr
) {
563 xhr
.setRequestHeader ("Authorization", user
.auth
);
566 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/trees",
567 data
: JSON
.stringify({
568 base_tree
: baseTree
.sha
,
571 mode
: 100644, // file (blob)
578 success
: function(response
) {
581 error
: function(response
) {
588 // create a new commit
589 var createCommit
= function(user
, message
, parentCommit
, tree
) {
592 beforeSend
: function (xhr
) {
593 xhr
.setRequestHeader ("Authorization", user
.auth
);
596 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/commits",
597 data
: JSON
.stringify({
599 parents
: parentCommit
,
604 success
: function(response
) {
607 error
: function(response
) {
614 // create a pull request
615 var createPullRequest
= function(user
, title
, body
, origin
, head
) {
618 beforeSend
: function (xhr
) {
619 xhr
.setRequestHeader ("Authorization", user
.auth
);
622 url
: "https://api.github.com/repos/" + origin
.user
+ "/" + origin
.repo
+ "/pulls",
623 data
: JSON
.stringify({
627 head
: user
.login
+ ":" + head
631 success
: function(response
) {
634 error
: function(response
) {
641 // update a pull request
642 var updatePullRequest
= function(user
, title
, body
, state
, request
) {
645 beforeSend
: function (xhr
) {
646 xhr
.setRequestHeader ("Authorization", user
.auth
);
650 data
: JSON
.stringify({
657 success
: function(response
) {
660 error
: function(response
) {
669 var getFile
= function(user
, path
, branch
) {
673 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/contents/" + path
,
679 success
: function(response
) {
682 error
: function(response
) {
691 getLastCommit
: getLastCommit
,
692 getBranches
: getBranches
,
694 createBlob
: createBlob
,
695 createTree
: createTree
,
696 createCommit
: createCommit
,
697 createPullRequest
: createPullRequest
,
698 updatePullRequest
: updatePullRequest
,
706 * Nitdoc GitHub loginbox module
709 Nitdoc
.GitHub
.LoginBox
= function() {
714 var init
= function(containerSelector
) {
715 loginBoxLi
= $
(document
.createElement("li"))
716 .attr("id", "nitdoc-github-li")
718 $
(document
.createElement("a"))
720 $
(document
.createElement("img"))
722 src
: "resources/icons/github-icon.png",
725 .addClass("nitdoc-github-li-img")
727 .click(function() { Nitdoc
.GitHub
.LoginBox
.toggle() })
730 loginBoxContent
= $
(document
.createElement("div"));
731 loginBox
= $
(document
.createElement("div"))
732 .attr("id", "nitdoc-github-loginbox")
733 .css("display", "none")
735 $
(document
.createElement("div"))
736 .addClass("nitdoc-github-loginbox-arrow")
740 $
(document
.createElement("h3"))
741 .append("Github Sign In")
743 .append(loginBoxContent
);
745 loginBoxLi
.append(loginBox
);
746 $
(containerSelector
).append(loginBoxLi
);
749 // Panel of login box to display when the user is logged in
750 var displayLogout
= function(origin
, user
) {
751 var panel
= $
(document
.createElement("div"))
753 $
(document
.createElement("h4"))
756 $
(document
.createElement("a"))
757 .attr("href", "https://github.com/" + user
.login
)
762 $
(document
.createElement("label"))
763 .attr("for", "github-origin")
767 $
(document
.createElement("input"))
771 disabled
: "disabled",
772 value
: origin
.user
+ ":" + origin
.repo
+ ":" + origin
.branch
776 $
(document
.createElement("label"))
777 .attr("for", "github-base")
781 $
(document
.createElement("input"))
785 disabled
: "disabled",
786 value
: user
.login
+ ":" + user
.repo
+ ":" + user
.branch
790 $
(document
.createElement("button"))
791 .addClass("nitdoc-github-button")
792 .addClass("nitdoc-github-cancel")
794 $
(document
.createElement("img"))
795 .attr("src", "resources/icons/github-icon.png")
797 .click(function() { // log out user
798 Nitdoc
.GitHub
.UI
.disactivate();
799 Nitdoc
.GitHub
.LoginBox
.toggle();
802 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon-green.png");
803 loginBoxContent
.empty()
804 loginBoxContent
.append(panel
);
807 // Panel of login box to display when the user is logged out
808 var displayLogin
= function() {
809 var panel
= $
(document
.createElement("form"))
811 $
(document
.createElement("label"))
812 .attr("for", "nitdoc-github-login-field")
816 $
(document
.createElement("input"))
818 id
: "nitdoc-github-login-field",
823 $
(document
.createElement("label"))
824 .attr("for", "nitdoc-github-password-field")
828 $
(document
.createElement("input"))
830 id
: "nitdoc-github-password-field",
835 $
(document
.createElement("label"))
836 .attr("for", "nitdoc-github-repo-field")
837 .append("Repository")
840 $
(document
.createElement("input"))
842 id
: "nitdoc-github-repo-field",
847 $
(document
.createElement("label"))
848 .attr("for", "nitdoc-github-branch-field")
852 $
(document
.createElement("input"))
854 id
: "nitdoc-github-branch-field",
859 $
(document
.createElement("button"))
860 .addClass("nitdoc-github-button")
862 $
(document
.createElement("img"))
863 .attr("src", "resources/icons/github-icon.png")
866 var login
= $
('#nitdoc-github-login-field').val();
867 var password
= $
('#nitdoc-github-password-field').val();
868 var repo
= $
('#nitdoc-github-repo-field').val();
869 var branch
= $
('#nitdoc-github-branch-field').val();
870 if(!login ||
!password ||
!repo ||
!branch
) {
871 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "Please enter your GitHub username, password, repository and branch.", true);
873 var user
= Nitdoc
.GitHub
.UI
.tryLogin(login
, password
, repo
, branch
);
875 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "The username, password, repo or branch you entered is incorrect.", true);
877 Nitdoc
.GitHub
.UI
.activate(user
);
878 var origin
= Nitdoc
.GitHub
.UI
.getOrigin();
879 Nitdoc
.GitHub
.LoginBox
.displayLogout(origin
, user
);
885 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon.png");
886 loginBoxContent
.empty()
887 loginBoxContent
.append(panel
);
890 var toggle
= function() {
891 if(loginBox
.is(':hidden')) {
893 if (!$
('#loginGit').is(':hidden')) { $
('#loginGit').focus(); }
902 displayLogin
: displayLogin
,
903 displayLogout
: displayLogout
,
912 * Nitdoc.GitHub.CommentBox class
915 // Init new modal box instance
916 Nitdoc
.GitHub
.CommentBox
= function(infos
) {
921 Nitdoc
.GitHub
.CommentBox
.prototype.open
= function(baseArea
) {
922 Nitdoc
.GitHub
.UI
.addOpenedComments();
925 if(this.infos
.requestID
) {
926 // get comment from last pull request
927 var requests
= JSON
.parse(localStorage
.requests
);
928 this.infos
.newComment
= Base64
.decode(requests
[this.infos
.requestID
].comment
);
930 this.infos
.newComment
= false;
933 // create comment box
934 var tarea
= $
(document
.createElement("textarea"))
935 .append(this.infos
.newComment
=== false?
this.infos
.oldComment
: this.infos
.newComment
)
936 .keyup(function(event
) {
937 $
(event
.target
).css("height", (event
.target
.value
.split(/\r|
\n/).length
* 16) + "px");
938 if ( (!instance
.infos
.requestID
&& $
(event
.target
).val() != instance
.infos
.oldComment
) ||
(instance
.infos
.requestID
&& $
(event
.target
).val() != instance
.infos
.oldComment
&& $
(event
.target
).val() != instance
.infos
.newComment
) ) {
939 $
(event
.target
).parent().find("button.nitdoc-github-commit").removeAttr("disabled");
941 $
(event
.target
).parent().find("button.nitdoc-github-commit").attr("disabled", "disabled");
944 .keydown(function(event
) {
945 if(event
.keyCode
== 13){
946 $
(event
.target
).css("height", ($
(event
.target
).outerHeight() + 6) + "px");
950 this.commentBoxDiv
= $
(document
.createElement("div"))
951 .addClass("nitdoc-github-commentbox")
954 $
(document
.createElement("a"))
955 .addClass("nitdoc-github-preview")
957 var converter
= new Markdown
.Converter()
958 var html
= converter
.makeHtml(tarea
.val());
959 Nitdoc
.GitHub
.ModalBox
.open("Preview", html
, false);
963 $
(document
.createElement("button"))
964 .addClass("nitdoc-github-button")
965 .addClass("nitdoc-github-commit")
968 instance
.infos
.newComment
= tarea
.val();
969 instance
.infos
.commentBox
= instance
;
970 Nitdoc
.GitHub
.CommitBox
.open(instance
.infos
);
974 $
(document
.createElement("button"))
975 .addClass("nitdoc-github-button")
976 .addClass("nitdoc-github-cancel")
978 .click(function() {instance
.close()})
981 baseArea
.after(this.commentBoxDiv
);
982 var cbWidth
= this.commentBoxDiv
.innerWidth();
983 var taWidth
= tarea
.outerWidth();
984 tarea
.width(cbWidth
- (taWidth
- cbWidth
));
985 tarea
.trigger("keyup");
989 Nitdoc
.GitHub
.CommentBox
.prototype.close
= function() {
990 Nitdoc
.GitHub
.UI
.remOpenedComments();
991 if(this.infos
.isNew
) {
992 this.commentBoxDiv
.next().find("span.nitdoc-github-editComment").show();
993 } else if(this.infos
.requestID
) {
994 this.commentBoxDiv
.next().show();
995 this.commentBoxDiv
.next().next().show();
997 this.commentBoxDiv
.next().show();
998 this.commentBoxDiv
.next().next().find("span.nitdoc-github-editComment").show();
1000 this.commentBoxDiv
.remove();
1004 * Nitdoc.GitHub.ModalBox class
1007 // Init new modal box instance
1008 Nitdoc
.GitHub
.ModalBox
= function() {
1010 // Open modal box instance
1011 var open
= function(title
, content
, isError
) {
1013 $
(document
.createElement("div"))
1014 .attr("id", "nitdoc-github-modal-fade")
1015 .addClass("nitdoc-github-fade")
1018 $
(document
.createElement("div"))
1019 .attr("id", "nitdoc-github-modal")
1020 .addClass("nitdoc-github-modal")
1022 $
(document
.createElement("a"))
1023 .addClass("nitdoc-github-close")
1024 .attr("title", "Close")
1026 .click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1028 .append("<h3>" + title
+ "</h3>")
1029 .append("<div>" + content
+ "</div>")
1031 $
(document
.createElement("div"))
1032 .addClass("nitdoc-github-buttons")
1034 $
(document
.createElement("button"))
1035 .addClass("nitdoc-github-button")
1037 .click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1043 $
("#nitdoc-github-modal").addClass("nitdoc-github-error");
1046 $
("#nitdoc-github-modal").css({
1048 marginTop
: -($
("#nitdoc-github-modal").outerHeight() / 2) + "px",
1050 marginLeft
: -($
("#nitdoc-github-modal").outerWidth() / 2) + "px"
1054 // Close modal box instance
1055 var close
= function() {
1056 $
("#nitdoc-github-modal").remove();
1057 $
("#nitdoc-github-modal-fade").remove();
1070 * Nitdoc.GitHub.CommitBox instance
1073 // Init new commit box instance
1074 Nitdoc
.GitHub
.CommitBox
= function() {
1076 // Open commit box instance
1077 var open
= function(infos
) {
1079 $
(document
.createElement("div"))
1080 .attr("id", "nitdoc-github-commitBox-fade")
1081 .addClass("nitdoc-github-fade")
1084 $
(document
.createElement("div"))
1085 .attr("id", "nitdoc-github-commitBox")
1086 .addClass("nitdoc-github-modal")
1088 $
(document
.createElement("a"))
1089 .addClass("nitdoc-github-close")
1090 .attr("title", "Close")
1092 .click(function() { Nitdoc
.GitHub
.CommitBox
.close() })
1094 .append("<h3>Commit changes</h3>")
1096 $
(document
.createElement("div"))
1098 $
(document
.createElement("label"))
1099 .attr("for", "nitdoc-github-commit-message")
1103 $
(document
.createElement("textarea"))
1104 .attr("id", "nitdoc-github-commit-message")
1105 .append("doc: " + (infos
.isNew ?
"added" : "modified") + " comment for " + infos
.namespace)
1109 $
(document
.createElement("input"))
1111 id
: "nitdoc-github-commit-signedoff",
1113 value
: "Signed-off-by: " + infos
.user
.signedOff
1115 .change(function(e
) {
1116 if ($
(this).is(':checked')) {
1117 $
("#nitdoc-github-commit-button").removeAttr("disabled");
1119 $
("#nitdoc-github-commit-button").attr("disabled", "disabled");
1124 $
(document
.createElement("label"))
1125 .attr("for", "nitdoc-github-commit-signedoff")
1126 .append("Signed-off-by: " + infos
.user
.signedOff
)
1129 $
(document
.createElement("div"))
1130 .addClass("nitdoc-github-buttons")
1132 $
(document
.createElement("button"))
1134 id
: "nitdoc-github-commit-button",
1135 disabled
: "disabled"
1137 .addClass("nitdoc-github-button")
1139 $
(document
.createElement("img"))
1140 .attr("src", "resources/icons/github-icon.png")
1143 .mousedown(function() {
1144 $
(this).text("Commiting...");
1146 .mouseup(function() {
1147 infos
.message
= $
("#nitdoc-github-commit-message").val() + "\n\n" + infos
.user
.signedOff
;
1148 Nitdoc
.GitHub
.UI
.saveChanges(infos
);
1154 $
("#nitdoc-github-commitBox").css({
1156 marginTop
: -($
("#nitdoc-github-commitBox").outerHeight() / 2) + "px",
1158 marginLeft
: -($
("#nitdoc-github-commitBox").outerWidth() / 2) + "px"
1162 // Close commit box instance
1163 var close
= function() {
1164 $
("#nitdoc-github-commitBox").remove();
1165 $
("#nitdoc-github-commitBox-fade").remove();
1177 * Nitdoc.GitHub.Utils module
1180 Nitdoc
.GitHub
.Utils
= function() {
1181 // Extract infos from string location "../lib/standard/collection/array.nit:457,1--458,0"
1182 //FIXME this should be done by nitdoc
1183 var parseLocation
= function(location
) {
1184 var parts
= location
.split(":");
1185 var loc
= new Object();
1186 loc
.origin
= location
;
1187 loc
.path
= parts
[0].substr(3, parts
[0].length
);
1188 loc
.lstart
= parseInt(parts
[1].split("--")[0].split(",")[0]);
1189 loc
.tabpos
= parseInt(parts
[1].split("--")[0].split(",")[1]);
1190 loc
.lend
= parseInt(parts
[1].split("--")[1].split(",")[0]);
1194 // Meld modified comment into file content
1195 var mergeComment
= function(fileContent
, comment
, location
) {
1196 // replace comment in file content
1197 var res
= new String();
1198 var lines
= fileContent
.split("\n");
1199 // copy lines fron 0 to lstart
1200 for(var i
= 0; i
< location
.lstart
- 1; i
++) {
1201 res
+= lines
[i] + "\n";
1204 if(comment
&& comment
!= "") {
1205 var commentLines
= comment
.split("\n");
1206 for(var i
= 0; i
< commentLines
.length
; i
++) {
1207 var line
= commentLines
[i];
1208 var tab
= location
.tabpos
> 1 ?
"\t" : "";
1209 res
+= tab
+ (line
.length
> 0 ?
"# " : "#") + line
+ "\n";
1212 // copy lines fron lend to end
1213 for(var i
= location
.lend
- 1; i
< lines
.length
; i
++) {
1215 if(i
< lines
.length
- 1) { res
+= "\n"; }
1222 parseLocation
: parseLocation
,
1223 mergeComment
: mergeComment