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 upstream
= $
("body").attr("data-github-upstream");
33 var basesha1
= $
("body").attr("data-github-base-sha1");
34 if(upstream
&& basesha1
) {
35 Nitdoc
.GitHub
.UI
.init(upstream
, basesha1
);
40 * Nitdoc.Github.UI for comment edition module
42 Nitdoc
.GitHub
.UI
= function() {
43 var openedComments
= 0; // currently edited comments count
44 var user
= false; // logged user
47 var init
= function(upstream
, basesha1
) {
48 console
.log("init GitHub module (upstream: "+ upstream
+", base: " + basesha1
+ ")");
51 var parts
= upstream
.split(":");
52 if(parts
.length
< 3) {
53 console
.error("Incorrect upstream name `" + upstream
+ "`, should be of the form user:repo:branch");
62 // check local session
63 if(localStorage
.user
) {
64 var session
= JSON
.parse(localStorage
.user
);
65 var user
= tryLogin(session
.login
, Base64
.decode(session
.password
), session
.repo
, session
.branch
);
67 console
.log("Session found but authentification failed");
71 console
.log("No session found");
75 Nitdoc
.GitHub
.LoginBox
.init("nav.main ul");
76 if(user
&& user
.login
) {
77 Nitdoc
.GitHub
.LoginBox
.displayLogout(origin
, user
);
80 Nitdoc
.GitHub
.LoginBox
.displayLogin();
83 // Prevent page unload if there is comments in editing mode
84 $
(window
).on('beforeunload', function() {
85 if(Nitdoc
.GitHub
.UI
.getOpenedComments() > 0){
86 return "There is uncommited modified comments. Are you sure you want to leave this page?";
91 // Activate comment UI for a logged user
92 var activate
= function(loggedUser
) {
97 // check local storage synchro with branch
98 if(localStorage
.base
!= origin
.sha
) {
99 console
.log("Base changed: cleaned cache");
100 localStorage
.requests
= "[]";
101 localStorage
.base
= origin
.sha
;
104 attachCommentEvents();
109 var disactivate
= function() {
110 if(Nitdoc
.GitHub
.UI
.getOpenedComments() > 0){
111 if(!confirm('There is uncommited modified comments. Are you sure you want to leave this page?')) {
115 // close session and purge cookie
116 localStorage
.clear();
117 $
(window
).unbind('beforeunload');
118 window
.location
.reload();
121 // Attempt login through GitHub API
122 var tryLogin
= function(login
, password
, repo
, branch
) {
123 var user
= new Nitdoc
.GitHub
.User(login
, password
, repo
, branch
);
124 if(!Nitdoc
.GitHub
.API
.login(user
)) {
125 return "error:login";
127 // check github profile fields
128 if(!user
.infos
.name ||
!user
.infos
.email
) {
129 return "error:profile";
131 // check correct base commit
132 var commit
= Nitdoc
.GitHub
.API
.getCommit(user
, origin
.sha
);
133 if(!commit ||
!commit
.sha
) {
139 // Attach edit button on each comment
140 var attachCommentEvents
= function() {
141 // Blocks without comment
142 $
("span.noComment").each(function() {
143 //FIXME this should be done by nitdoc
144 var baseComment
= $
(this).parent().prev();
145 var location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseComment
.attr("data-comment-location"));
146 var locString
= location
.path
+ ":" + location
.lstart
+ "," + location
.tabpos
+ "--" + location
.lstart
+ ",0";
147 baseComment
.attr("data-comment-location", locString
);
148 $
(this).html("<a class='nitdoc-github-editComment noComment'>add comment</a> for ");
149 $
(this).addClass("nitdoc-github-editComment");
151 // Blocks with comment
152 $
('.description div.comment').each(function() {
153 var p
= $
(this).next();
154 p
.prepend("<span class='nitdoc-github-editComment'><a class='nitdoc-github-editComment'>edit comment</a> for </span>")
157 // Attach links events
158 $
('a.nitdoc-github-editComment').each(function() {
159 $
(this).css("cursor", "pointer")
160 $
(this).click(function() {
162 $
(this).parent().hide();
166 if(!$
(this).hasClass("noComment")) {
167 $
(this).parent().parent().prev().hide();
168 baseTextarea
= $
(this).parent().parent().prev().prev();
170 baseTextarea
= $
(this).parent().parent().prev();
173 infos
.user
= Nitdoc
.GitHub
.UI
.getUser();
174 infos
.location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseTextarea
.attr("data-comment-location"));
175 infos
.namespace = baseTextarea
.attr("data-comment-namespace");
176 infos
.oldComment
= baseTextarea
.val();
177 var box
= new Nitdoc
.GitHub
.CommentBox(infos
);
178 box
.open(baseTextarea
);
183 // reload comments from saved pull request
184 var reloadComments
= function() {
185 if(!localStorage
.requests
){ return; }
186 var requests
= JSON
.parse(localStorage
.requests
);
187 var converter
= new Markdown
.Converter();
188 // Look for modified comments in page
190 if(!requests
[i]) { continue; }
191 var request
= requests
[i];
192 $
("textarea[data-comment-location=\"" + request
.location
+ "\"]").each(function () {
193 var div
= $
(this).next();
194 if(request
.isClosed
) {
195 if(div
.is("div.comment.newComment")) {
196 // hide empty comment
198 div
.next().find("span.noComment").show();
200 } else if(div
.is("div.comment.locked")) {
203 div
.append(converter
.makeHtml($
(this).text()));
204 div
.removeClass("locked");
205 div
.css("cursor", "pointer")
207 div
.next().find("span.nitdoc-github-editComment").show();
210 // create div for the new coment
211 if(!div
.is("div.comment")) {
212 $
(this).after("<div class='comment newComment'></div>");
213 div
= $
(this).next();
215 // lock modified comment
216 if(!div
.hasClass("locked")) {
217 // convert modified comment to markdown
219 div
.append(converter
.makeHtml(Base64
.decode(request
.comment
)));
221 div
.css("cursor", "auto");
222 div
.addClass("locked");
223 div
.next().find("span.nitdoc-github-editComment").hide();
225 $
("<p class='locked inheritance'>")
226 .text("comment modified in ")
227 .append("<a href='"+ request
.request
.html_url
+"' target='_blank' title='Review on GitHub'>pull request #"+ request
.request
.number
+"</a>")
230 $
("<a data-pullrequest-number='"+ request
.request
.number
+"' class='nitdoc-github-update'>update</a>")
232 $
(this).parent().hide();
234 var baseTextarea
= div
.prev();
237 infos
.user
= Nitdoc
.GitHub
.UI
.getUser();
238 infos
.location
= Nitdoc
.GitHub
.Utils
.parseLocation(baseTextarea
.attr("data-comment-location"));
239 infos
.namespace = baseTextarea
.attr("data-comment-namespace");
240 infos
.oldComment
= baseTextarea
.val();
241 infos
.requestID
= $
(this).attr("data-pullrequest-number");
242 var box
= new Nitdoc
.GitHub
.CommentBox(infos
);
243 box
.open(baseTextarea
);
248 $
("<a data-pullrequest-number='"+ request
.request
.number
+"' class='nitdoc-github-cancel'>cancel</a>")
250 ui
.closePullRequest($
(this).attr("data-pullrequest-number"));
256 // hide "add comment" link
257 if(div
.hasClass("newComment")) {
258 div
.next().next().find("span.noComment").hide();
266 // Commit changes and send pull request
267 var saveChanges
= function(infos
) {
268 // if pull request update close existing pull request for the comment
269 if(infos
.requestID
) {
270 closePullRequest(infos
.requestID
);
274 var fileContent
= getFileContent(infos
.location
.path
);
275 infos
.newContent
= Nitdoc
.GitHub
.Utils
.mergeComment(fileContent
, infos
.newComment
, infos
.location
);
278 infos
.request
= pushChanges(infos
)
280 Nitdoc
.GitHub
.ModalBox
.open("Unable to commit changes!", response
, true);
285 // close boxes and reload comments
286 Nitdoc
.GitHub
.CommitBox
.close();
287 infos
.commentBox
.close();
292 Creating a new pull request with the new comment take 5 steps:
293 1. get the base tree from latest commit
295 2. create a new blob with updated file content
296 3. post a new tree from base tree and blob
297 4. post the new commit with new tree
298 5. create the pull request
300 var pushChanges
= function(infos
) {
301 var baseTree
= Nitdoc
.GitHub
.API
.getTree(user
, origin
.sha
);
303 Nitdoc
.GitHub
.ModalBox
.open("Unable to locate base tree!", baseTree
.status
+ ": " + baseTree
.statusText
, true);
306 console
.log("Base tree: " + baseTree
.url
);
307 var newBlob
= Nitdoc
.GitHub
.API
.createBlob(user
, infos
.newContent
);
309 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new blob!", newBlob
.status
+ ": " + newBlob
.statusText
, true);
312 console
.log("New blob: " + newBlob
.url
);
313 var newTree
= Nitdoc
.GitHub
.API
.createTree(user
, baseTree
, infos
.location
.path
, newBlob
);
315 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new tree!", newTree
.status
+ ": " + newTree
.statusText
, true);
318 console
.log("New tree: " + newTree
.url
);
319 var newCommit
= Nitdoc
.GitHub
.API
.createCommit(user
, infos
.message
, baseTree
.sha
, newTree
);
321 Nitdoc
.GitHub
.ModalBox
.open("Unable to create new commit!", newCommit
.status
+ ": " + newCommit
.statusText
, true);
324 console
.log("New commit: " + newCommit
.url
);
325 var pullRequest
= Nitdoc
.GitHub
.API
.createPullRequest(user
, infos
.message
.split("\n\n")[0], "Pull request from Nitdoc", origin
, newCommit
.sha
);
326 if(!pullRequest
.number
) {
327 Nitdoc
.GitHub
.ModalBox
.open("Unable to create pull request!", pullRequest
.status
+ ": " + pullRequest
.statusText
, true);
330 console
.log("New pull request: " + pullRequest
.url
);
334 // close previously opened pull request
335 var closePullRequest
= function(number
) {
336 var requests
= JSON
.parse(localStorage
.requests
);
337 if(!requests
[number]) {
338 Nitdoc
.GitHub
.ModalBox
.open("Unable to close pull request!", "Pull request " + number
+ "not found", true);
341 // close pull request
342 var res
= Nitdoc
.GitHub
.API
.updatePullRequest(user
, "Canceled from Nitdoc", "", "closed", requests
[number].request
);
344 Nitdoc
.GitHub
.ModalBox
.open("Unable to close pull request!", res
.status
+ ": " + res
.statusText
, true);
347 // update in localstorage
348 requests
[number].isClosed
= true;
349 localStorage
.requests
= JSON
.stringify(requests
);
352 // Get file content from github
353 var getFileContent
= function(githubUrl
) {
354 var origFile
= Nitdoc
.GitHub
.API
.getFile(user
, githubUrl
);
355 if(!origFile
.content
) {
356 Nitdoc
.GitHub
.ModalBox
.open("Unable to locate source file!", origFile
.status
+ ": " + origFile
.statusText
, true);
359 var base64Content
= origFile
.content
.substring(0, origFile
.content
.length
- 1)
360 return Base64
.decode(base64Content
);
363 // save pull request in local storage
364 var saveRequest
= function(infos
) {
366 if(localStorage
.requests
) {requests
= JSON
.parse(localStorage
.requests
)}
367 requests
[infos
.request
.number
] = {
368 request
: infos
.request
,
369 location
: infos
.location
.origin
,
370 comment
: Base64
.encode(infos
.newComment
)
372 localStorage
.requests
= JSON
.stringify(requests
);
375 // save user in local storage
376 var saveSession
= function(user
) {
379 password
: Base64
.encode(user
.password
),
383 localStorage
.user
= JSON
.stringify(session
);
388 var getUser
= function() { return user
; }
389 var getOrigin
= function() { return origin
; }
390 var getOpenedComments
= function() { return openedComments
; }
391 var addOpenedComments
= function() { openedComments
+= 1; }
392 var remOpenedComments
= function() { openedComments
-= 1; }
399 disactivate
: disactivate
,
401 getOrigin
: getOrigin
,
402 getOpenedComments
: getOpenedComments
,
403 addOpenedComments
: addOpenedComments
,
404 remOpenedComments
: remOpenedComments
,
405 saveChanges
: saveChanges
,
406 closePullRequest
: closePullRequest
,
407 reloadComments
: reloadComments
414 * GitHub API user object
416 Nitdoc
.GitHub
.User
= function(login
, password
, repo
, branch
) {
418 this.password
= password
;
420 this.auth
= "Basic " + Base64
.encode(login
+ ':' + password
);
421 this.branch
= branch
;
427 Nitdoc
.GitHub
.API
= function() {
429 // try to login the user to github API
430 var login
= function(user
) {
433 beforeSend
: function (xhr
) {
434 xhr
.setRequestHeader ("Authorization", user
.auth
);
437 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
,
440 success
: function() {
444 user
.infos
= getUserInfos(user
);
445 user
.signedOff
= getSignedOff(user
)
449 // request for user github account infos
450 var getUserInfos
= function(user
) {
453 beforeSend
: function (xhr
) {
454 xhr
.setRequestHeader ("Authorization", user
.auth
);
457 url
: "https://api.github.com/users/" + user
.login
,
460 success
: function(response
) {
463 error
: function(response
) {
470 // build signedoff user default signature
471 var getSignedOff
= function(user
) {
472 return user
.infos
.name
+ " <" + user
.infos
.email
+ ">";
475 // get the branches list from a repo
476 var getBranches
= function(user
) {
479 beforeSend
: function (xhr
) {
480 xhr
.setRequestHeader ("Authorization", user
.auth
);
483 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/branches",
486 success
: function(response
) {
489 error
: function(response
) {
498 // get the latest commit on `branchName`
499 var getCommit
= function(user
, sha
) {
502 beforeSend
: function (xhr
) {
503 xhr
.setRequestHeader ("Authorization", user
.auth
);
506 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/commits/" + sha
,
509 success
: function(response
) {
512 error
: function(response
) {
519 // get the base tree for a commit sha
520 var getTree
= function(user
, sha
) {
523 beforeSend
: function (xhr
) {
524 xhr
.setRequestHeader ("Authorization", user
.auth
);
527 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/trees/" + sha
+ "?recursive=1",
530 success
: function(response
) {
533 error
: function(response
) {
541 var createBlob
= function(user
, content
) {
544 beforeSend
: function (xhr
) {
545 xhr
.setRequestHeader ("Authorization", user
.auth
);
548 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/blobs",
551 data
: JSON
.stringify({
552 content
: Base64
.encode(content
),
555 success
: function(response
) {
558 error
: function(response
) {
565 // create a new tree from a base tree
566 var createTree
= function(user
, baseTree
, path
, blob
) {
569 beforeSend
: function (xhr
) {
570 xhr
.setRequestHeader ("Authorization", user
.auth
);
573 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/trees",
574 data
: JSON
.stringify({
575 base_tree
: baseTree
.sha
,
578 mode
: "100644", // file (blob)
585 success
: function(response
) {
588 error
: function(response
) {
595 // create a new commit
596 var createCommit
= function(user
, message
, parentCommit
, tree
) {
599 beforeSend
: function (xhr
) {
600 xhr
.setRequestHeader ("Authorization", user
.auth
);
603 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/git/commits",
604 data
: JSON
.stringify({
606 parents
: parentCommit
,
611 success
: function(response
) {
614 error
: function(response
) {
621 // create a pull request
622 var createPullRequest
= function(user
, title
, body
, origin
, head
) {
625 beforeSend
: function (xhr
) {
626 xhr
.setRequestHeader ("Authorization", user
.auth
);
629 url
: "https://api.github.com/repos/" + origin
.user
+ "/" + origin
.repo
+ "/pulls",
630 data
: JSON
.stringify({
634 head
: user
.login
+ ":" + head
638 success
: function(response
) {
641 error
: function(response
) {
648 // update a pull request
649 var updatePullRequest
= function(user
, title
, body
, state
, request
) {
652 beforeSend
: function (xhr
) {
653 xhr
.setRequestHeader ("Authorization", user
.auth
);
657 data
: JSON
.stringify({
664 success
: function(response
) {
667 error
: function(response
) {
676 var getFile
= function(user
, path
, branch
) {
680 url
: "https://api.github.com/repos/" + user
.login
+ "/" + user
.repo
+ "/contents/" + path
,
686 success
: function(response
) {
689 error
: function(response
) {
698 getCommit
: getCommit
,
699 getBranches
: getBranches
,
701 createBlob
: createBlob
,
702 createTree
: createTree
,
703 createCommit
: createCommit
,
704 createPullRequest
: createPullRequest
,
705 updatePullRequest
: updatePullRequest
,
713 * Nitdoc GitHub loginbox module
716 Nitdoc
.GitHub
.LoginBox
= function() {
721 var init
= function(containerSelector
) {
722 loginBoxLi
= $
(document
.createElement("li"))
723 .attr("id", "nitdoc-github-li")
725 $
(document
.createElement("a"))
727 $
(document
.createElement("img"))
729 src
: "resources/icons/github-icon.png",
732 .addClass("nitdoc-github-li-img")
734 .click(function() { Nitdoc
.GitHub
.LoginBox
.toggle() })
737 loginBoxContent
= $
(document
.createElement("div"));
738 loginBox
= $
(document
.createElement("div"))
739 .attr("id", "nitdoc-github-loginbox")
740 .css("display", "none")
742 $
(document
.createElement("div"))
743 .addClass("nitdoc-github-loginbox-arrow")
746 .append(loginBoxContent
);
748 loginBoxLi
.append(loginBox
);
749 $
(containerSelector
).append(loginBoxLi
);
752 // Panel of login box to display when the user is logged in
753 var displayLogout
= function(origin
, user
) {
754 var panel
= $
(document
.createElement("div"))
756 $
(document
.createElement("h3"))
757 .append("Signed in Github")
760 $
(document
.createElement("h4"))
763 $
(document
.createElement("a"))
764 .attr("href", "https://github.com/" + user
.login
)
769 $
(document
.createElement("label"))
770 .attr("for", "github-origin")
771 .append("Upstram branch")
774 $
(document
.createElement("a"))
775 .addClass("nitdoc-github-loginbox-githublink")
777 title
: "Open branch in GitHub",
778 href
: "https://github.com/" + origin
.user
+ "/" + origin
.repo
+ "/tree/" + origin
.branch
,
781 .append(origin
.user
+ ":" + origin
.repo
+ ":" + origin
.branch
)
784 $
(document
.createElement("label"))
785 .attr("for", "github-base")
786 .append("Your branch")
789 $
(document
.createElement("a"))
790 .addClass("nitdoc-github-loginbox-githublink")
792 title
: "Open branch in GitHub",
793 href
: "https://github.com/" + user
.login
+ "/" + user
.repo
+ "/tree/" + user
.branch
,
796 .append(origin
.user
+ ":" + origin
.repo
+ ":" + origin
.branch
)
799 $
(document
.createElement("button"))
800 .addClass("nitdoc-github-button")
801 .addClass("nitdoc-github-cancel")
803 $
(document
.createElement("img"))
804 .attr("src", "resources/icons/github-icon.png")
806 .click(function() { // log out user
807 Nitdoc
.GitHub
.UI
.disactivate();
808 Nitdoc
.GitHub
.LoginBox
.toggle();
811 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon-green.png");
812 loginBoxContent
.empty()
813 loginBoxContent
.append(panel
);
816 // Panel of login box to display when the user is logged out
817 var displayLogin
= function() {
818 var panel
= $
(document
.createElement("form"))
820 $
(document
.createElement("h3"))
821 .append("Sign in Github")
824 $
(document
.createElement("label"))
825 .attr("for", "nitdoc-github-login-field")
829 $
(document
.createElement("input"))
831 id
: "nitdoc-github-login-field",
836 $
(document
.createElement("label"))
837 .attr("for", "nitdoc-github-password-field")
841 $
(document
.createElement("input"))
843 id
: "nitdoc-github-password-field",
848 $
(document
.createElement("label"))
849 .attr("for", "nitdoc-github-repo-field")
850 .append("Repository")
853 $
(document
.createElement("input"))
855 id
: "nitdoc-github-repo-field",
860 $
(document
.createElement("label"))
861 .attr("for", "nitdoc-github-branch-field")
865 $
(document
.createElement("input"))
867 id
: "nitdoc-github-branch-field",
872 $
(document
.createElement("button"))
873 .addClass("nitdoc-github-button")
875 $
(document
.createElement("img"))
876 .attr("src", "resources/icons/github-icon.png")
879 var login
= $
('#nitdoc-github-login-field').val();
880 var password
= $
('#nitdoc-github-password-field').val();
881 var repo
= $
('#nitdoc-github-repo-field').val();
882 var branch
= $
('#nitdoc-github-branch-field').val();
883 if(!login ||
!password ||
!repo ||
!branch
) {
884 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "Please enter your GitHub username, password, repository and branch.", true);
886 var user
= Nitdoc
.GitHub
.UI
.tryLogin(login
, password
, repo
, branch
);
887 if(user
== "error:login") {
888 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "The username, password, repo or branch you entered is incorrect.", true);
889 } else if(user
== "error:sha") {
890 Nitdoc
.GitHub
.ModalBox
.open("Base commit not found", "The provided GitHub repository must contains the base commit '" + Nitdoc
.GitHub
.UI
.getOrigin().sha
+ "'", true);
891 } else if(user
== "error:profile") {
892 Nitdoc
.GitHub
.ModalBox
.open("Incomplete GitHub profile", "Please set your public name and email in your <a href='https://github.com/settings/profile'>GitHub profile</a>.<br/><br/>Your public profile informations are used to sign-off your commits.", true);
894 Nitdoc
.GitHub
.UI
.activate(user
);
895 var origin
= Nitdoc
.GitHub
.UI
.getOrigin();
896 Nitdoc
.GitHub
.LoginBox
.displayLogout(origin
, user
);
902 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon.png");
903 loginBoxContent
.empty()
904 loginBoxContent
.append(panel
);
907 var toggle
= function() {
908 if(loginBox
.is(':hidden')) {
910 if (!$
('#loginGit').is(':hidden')) { $
('#loginGit').focus(); }
919 displayLogin
: displayLogin
,
920 displayLogout
: displayLogout
,
929 * Nitdoc.GitHub.CommentBox class
932 // Init new modal box instance
933 Nitdoc
.GitHub
.CommentBox
= function(infos
) {
938 Nitdoc
.GitHub
.CommentBox
.prototype.open
= function(baseArea
) {
939 Nitdoc
.GitHub
.UI
.addOpenedComments();
942 if(this.infos
.requestID
) {
943 // get comment from last pull request
944 var requests
= JSON
.parse(localStorage
.requests
);
945 this.infos
.newComment
= Base64
.decode(requests
[this.infos
.requestID
].comment
);
947 this.infos
.newComment
= false;
950 // create comment box
951 var tarea
= $
(document
.createElement("textarea"))
952 .append(this.infos
.newComment
=== false?
this.infos
.oldComment
: this.infos
.newComment
)
953 .keyup(function(event
) {
954 $
(event
.target
).css("height", (event
.target
.value
.split(/\r|
\n/).length
* 16) + "px");
955 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
) ) {
956 $
(event
.target
).parent().find("button.nitdoc-github-commit").removeAttr("disabled");
958 $
(event
.target
).parent().find("button.nitdoc-github-commit").attr("disabled", "disabled");
961 .keydown(function(event
) {
962 if(event
.keyCode
== 13){
963 $
(event
.target
).css("height", ($
(event
.target
).outerHeight() + 6) + "px");
967 this.commentBoxDiv
= $
(document
.createElement("div"))
968 .addClass("nitdoc-github-commentbox")
971 $
(document
.createElement("a"))
972 .addClass("nitdoc-github-preview")
974 var converter
= new Markdown
.Converter()
975 var html
= converter
.makeHtml(tarea
.val());
976 Nitdoc
.GitHub
.ModalBox
.open("Preview", html
, false);
980 $
(document
.createElement("button"))
981 .addClass("nitdoc-github-button")
982 .addClass("nitdoc-github-commit")
985 instance
.infos
.newComment
= tarea
.val();
986 instance
.infos
.commentBox
= instance
;
987 Nitdoc
.GitHub
.CommitBox
.open(instance
.infos
);
991 $
(document
.createElement("button"))
992 .addClass("nitdoc-github-button")
993 .addClass("nitdoc-github-cancel")
995 .click(function() {instance
.close()})
998 baseArea
.after(this.commentBoxDiv
);
999 var cbWidth
= this.commentBoxDiv
.innerWidth();
1000 var taWidth
= tarea
.outerWidth();
1001 tarea
.width(cbWidth
- (taWidth
- cbWidth
));
1002 tarea
.trigger("keyup");
1006 Nitdoc
.GitHub
.CommentBox
.prototype.close
= function() {
1007 Nitdoc
.GitHub
.UI
.remOpenedComments();
1008 if(this.infos
.isNew
) {
1009 this.commentBoxDiv
.next().find("span.nitdoc-github-editComment").show();
1010 } else if(this.infos
.requestID
) {
1011 this.commentBoxDiv
.next().show();
1012 this.commentBoxDiv
.next().next().show();
1014 this.commentBoxDiv
.next().show();
1015 this.commentBoxDiv
.next().next().find("span.nitdoc-github-editComment").show();
1017 this.commentBoxDiv
.remove();
1021 * Nitdoc.GitHub.ModalBox class
1024 // Init new modal box instance
1025 Nitdoc
.GitHub
.ModalBox
= function() {
1027 // Open modal box instance
1028 var open
= function(title
, content
, isError
) {
1030 $
(document
.createElement("div"))
1031 .attr("id", "nitdoc-github-modal-fade")
1032 .addClass("nitdoc-github-fade")
1035 $
(document
.createElement("div"))
1036 .attr("id", "nitdoc-github-modal")
1037 .addClass("nitdoc-github-modal")
1039 $
(document
.createElement("a"))
1040 .addClass("nitdoc-github-close")
1041 .attr("title", "Close")
1043 .click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1045 .append("<h3>" + title
+ "</h3>")
1046 .append("<div>" + content
+ "</div>")
1048 $
(document
.createElement("div"))
1049 .addClass("nitdoc-github-buttons")
1051 $
(document
.createElement("button"))
1052 .addClass("nitdoc-github-button")
1054 .click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1060 $
("#nitdoc-github-modal").addClass("nitdoc-github-error");
1063 $
("#nitdoc-github-modal")
1066 marginTop
: -($
("#nitdoc-github-modal").outerHeight() / 2) + "px",
1068 marginLeft
: -($
("#nitdoc-github-modal").outerWidth() / 2) + "px"
1070 .find("button.nitdoc-github-button").focus();
1073 // Close modal box instance
1074 var close
= function() {
1075 $
("#nitdoc-github-modal").remove();
1076 $
("#nitdoc-github-modal-fade").remove();
1089 * Nitdoc.GitHub.CommitBox instance
1092 // Init new commit box instance
1093 Nitdoc
.GitHub
.CommitBox
= function() {
1095 // Open commit box instance
1096 var open
= function(infos
) {
1098 $
(document
.createElement("div"))
1099 .attr("id", "nitdoc-github-commitBox-fade")
1100 .addClass("nitdoc-github-fade")
1103 $
(document
.createElement("div"))
1104 .attr("id", "nitdoc-github-commitBox")
1105 .addClass("nitdoc-github-modal")
1107 $
(document
.createElement("a"))
1108 .addClass("nitdoc-github-close")
1109 .attr("title", "Close")
1111 .click(function() { Nitdoc
.GitHub
.CommitBox
.close() })
1113 .append("<h3>Commit changes</h3>")
1115 $
(document
.createElement("div"))
1117 $
(document
.createElement("label"))
1118 .attr("for", "nitdoc-github-commit-message")
1122 $
(document
.createElement("textarea"))
1123 .attr("id", "nitdoc-github-commit-message")
1124 .append("doc: " + (infos
.isNew ?
"added" : "modified") + " comment for " + infos
.namespace)
1128 $
(document
.createElement("input"))
1130 id
: "nitdoc-github-commit-signedoff",
1132 value
: "Signed-off-by: " + infos
.user
.signedOff
1134 .change(function(e
) {
1135 if ($
(this).is(':checked')) {
1136 $
("#nitdoc-github-commit-button").removeAttr("disabled");
1138 $
("#nitdoc-github-commit-button").attr("disabled", "disabled");
1143 $
(document
.createElement("label"))
1144 .attr("for", "nitdoc-github-commit-signedoff")
1145 .text("Signed-off-by: " + infos
.user
.signedOff
)
1148 $
(document
.createElement("div"))
1149 .addClass("nitdoc-github-buttons")
1151 $
(document
.createElement("button"))
1153 id
: "nitdoc-github-commit-button",
1154 disabled
: "disabled"
1156 .addClass("nitdoc-github-button")
1158 $
(document
.createElement("img"))
1159 .attr("src", "resources/icons/github-icon.png")
1162 .mousedown(function() {
1163 $
(this).text("Commiting...");
1165 .mouseup(function() {
1166 infos
.message
= $
("#nitdoc-github-commit-message").val() + "\n\n" + infos
.user
.signedOff
;
1167 Nitdoc
.GitHub
.UI
.saveChanges(infos
);
1173 $
("#nitdoc-github-commitBox")
1176 marginTop
: -($
("#nitdoc-github-commitBox").outerHeight() / 2) + "px",
1178 marginLeft
: -($
("#nitdoc-github-commitBox").outerWidth() / 2) + "px"
1180 .find("#nitdoc-github-commit-message").focus();
1183 // Close commit box instance
1184 var close
= function() {
1185 $
("#nitdoc-github-commitBox").remove();
1186 $
("#nitdoc-github-commitBox-fade").remove();
1198 * Nitdoc.GitHub.Utils module
1201 Nitdoc
.GitHub
.Utils
= function() {
1202 // Extract infos from string location "lib/standard/collection/array.nit:457,1--458,0"
1203 var parseLocation
= function(location
) {
1204 var parts
= location
.split(":");
1205 var loc
= new Object();
1206 loc
.origin
= location
;
1207 loc
.path
= parts
[0];
1208 loc
.lstart
= parseInt(parts
[1].split("--")[0].split(",")[0]);
1209 loc
.tabpos
= parseInt(parts
[1].split("--")[0].split(",")[1]);
1210 loc
.lend
= parseInt(parts
[1].split("--")[1].split(",")[0]);
1214 // Meld modified comment into file conten
1215 var mergeComment
= function(fileContent
, comment
, location
) {
1216 // replace comment in file content
1217 var res
= new String();
1218 var lines
= fileContent
.split("\n");
1219 // copy lines fron 0 to lstart
1220 for(var i
= 0; i
< location
.lstart
- 1; i
++) {
1221 res
+= lines
[i] + "\n";
1224 if(comment
&& comment
!= "") {
1225 var commentLines
= comment
.split("\n");
1226 for(var i
= 0; i
< commentLines
.length
; i
++) {
1227 var line
= commentLines
[i];
1228 var tab
= location
.tabpos
> 1 ?
"\t" : "";
1229 res
+= tab
+ (line
.length
> 0 ?
"# " : "#") + line
+ "\n";
1232 // copy lines fron lend to end
1233 for(var i
= location
.lend
- 1; i
< lines
.length
; i
++) {
1235 if(i
< lines
.length
- 1) { res
+= "\n"; }
1242 parseLocation
: parseLocation
,
1243 mergeComment
: mergeComment