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")
793 $
(document
.createElement("img"))
794 .attr("src", "resources/icons/github-icon.png")
796 .click(function() { // log out user
797 Nitdoc
.GitHub
.UI
.disactivate();
798 Nitdoc
.GitHub
.LoginBox
.toggle();
801 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon-green.png");
802 loginBoxContent
.empty()
803 loginBoxContent
.append(panel
);
806 // Panel of login box to display when the user is logged out
807 var displayLogin
= function() {
808 var panel
= $
(document
.createElement("form"))
810 $
(document
.createElement("label"))
811 .attr("for", "nitdoc-github-login-field")
815 $
(document
.createElement("input"))
817 id
: "nitdoc-github-login-field",
822 $
(document
.createElement("label"))
823 .attr("for", "nitdoc-github-password-field")
827 $
(document
.createElement("input"))
829 id
: "nitdoc-github-password-field",
834 $
(document
.createElement("label"))
835 .attr("for", "nitdoc-github-repo-field")
836 .append("Repository")
839 $
(document
.createElement("input"))
841 id
: "nitdoc-github-repo-field",
846 $
(document
.createElement("label"))
847 .attr("for", "nitdoc-github-branch-field")
851 $
(document
.createElement("input"))
853 id
: "nitdoc-github-branch-field",
858 $
(document
.createElement("button"))
859 .addClass("nitdoc-github-button")
861 $
(document
.createElement("img"))
862 .attr("src", "resources/icons/github-icon.png")
865 var login
= $
('#nitdoc-github-login-field').val();
866 var password
= $
('#nitdoc-github-password-field').val();
867 var repo
= $
('#nitdoc-github-repo-field').val();
868 var branch
= $
('#nitdoc-github-branch-field').val();
869 if(!login ||
!password ||
!repo ||
!branch
) {
870 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "Please enter your GitHub username, password, repository and branch.", true);
872 var user
= Nitdoc
.GitHub
.UI
.tryLogin(login
, password
, repo
, branch
);
874 Nitdoc
.GitHub
.ModalBox
.open("Sign in error", "The username, password, repo or branch you entered is incorrect.", true);
876 Nitdoc
.GitHub
.UI
.activate(user
);
877 var origin
= Nitdoc
.GitHub
.UI
.getOrigin();
878 Nitdoc
.GitHub
.LoginBox
.displayLogout(origin
, user
);
884 $
(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon.png");
885 loginBoxContent
.empty()
886 loginBoxContent
.append(panel
);
889 var toggle
= function() {
890 if(loginBox
.is(':hidden')) {
892 if (!$
('#loginGit').is(':hidden')) { $
('#loginGit').focus(); }
901 displayLogin
: displayLogin
,
902 displayLogout
: displayLogout
,
911 * Nitdoc.GitHub.CommentBox class
914 // Init new modal box instance
915 Nitdoc
.GitHub
.CommentBox
= function(infos
) {
920 Nitdoc
.GitHub
.CommentBox
.prototype.open
= function(baseArea
) {
921 Nitdoc
.GitHub
.UI
.addOpenedComments();
924 if(this.infos
.requestID
) {
925 // get comment from last pull request
926 var requests
= JSON
.parse(localStorage
.requests
);
927 this.infos
.newComment
= Base64
.decode(requests
[this.infos
.requestID
].comment
);
929 this.infos
.newComment
= false;
932 // create comment box
933 var tarea
= $
(document
.createElement("textarea"))
934 .append(this.infos
.newComment
=== false?
this.infos
.oldComment
: this.infos
.newComment
)
935 .keyup(function(event
) {
936 $
(event
.target
).css("height", (event
.target
.value
.split(/\r|
\n/).length
* 16) + "px");
937 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
) ) {
938 $
(event
.target
).parent().find("button.nitdoc-github-commit").removeAttr("disabled");
940 $
(event
.target
).parent().find("button.nitdoc-github-commit").attr("disabled", "disabled");
943 .keydown(function(event
) {
944 if(event
.keyCode
== 13){
945 $
(event
.target
).css("height", ($
(event
.target
).outerHeight() + 6) + "px");
949 this.commentBoxDiv
= $
(document
.createElement("div"))
950 .addClass("nitdoc-github-commentbox")
953 $
(document
.createElement("a"))
954 .addClass("nitdoc-github-preview")
956 var converter
= new Markdown
.Converter()
957 var html
= converter
.makeHtml(tarea
.val());
958 Nitdoc
.GitHub
.ModalBox
.open("Preview", html
, false);
962 $
(document
.createElement("button"))
963 .addClass("nitdoc-github-button")
964 .addClass("nitdoc-github-commit")
967 instance
.infos
.newComment
= tarea
.val();
968 instance
.infos
.commentBox
= instance
;
969 Nitdoc
.GitHub
.CommitBox
.open(instance
.infos
);
973 $
(document
.createElement("button"))
974 .addClass("nitdoc-github-button")
975 .addClass("nitdoc-github-cancel")
977 .click(function() {instance
.close()})
980 baseArea
.after(this.commentBoxDiv
);
981 var cbWidth
= this.commentBoxDiv
.innerWidth();
982 var taWidth
= tarea
.outerWidth();
983 tarea
.width(cbWidth
- (taWidth
- cbWidth
));
984 tarea
.trigger("keyup");
988 Nitdoc
.GitHub
.CommentBox
.prototype.close
= function() {
989 Nitdoc
.GitHub
.UI
.remOpenedComments();
990 if(this.infos
.isNew
) {
991 this.commentBoxDiv
.next().find("span.nitdoc-github-editComment").show();
992 } else if(this.infos
.requestID
) {
993 this.commentBoxDiv
.next().show();
994 this.commentBoxDiv
.next().next().show();
996 this.commentBoxDiv
.next().show();
997 this.commentBoxDiv
.next().next().find("span.nitdoc-github-editComment").show();
999 this.commentBoxDiv
.remove();
1003 * Nitdoc.GitHub.ModalBox class
1006 // Init new modal box instance
1007 Nitdoc
.GitHub
.ModalBox
= function() {
1009 // Open modal box instance
1010 var open
= function(title
, content
, isError
) {
1012 $
(document
.createElement("div"))
1013 .attr("id", "nitdoc-github-modal-fade")
1014 .addClass("nitdoc-github-fade")
1017 $
(document
.createElement("div"))
1018 .attr("id", "nitdoc-github-modal")
1019 .addClass("nitdoc-github-modal")
1021 $
(document
.createElement("a"))
1022 .addClass("nitdoc-github-close")
1024 $
(document
.createElement("img"))
1025 .addClass("nitdoc-github-imgClose")
1027 src
: "resources/icons/close.png",
1032 ).click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1034 .append("<h3>" + title
+ "</h3>")
1035 .append("<div>" + content
+ "</div>")
1037 $
(document
.createElement("div"))
1038 .addClass("nitdoc-github-buttons")
1040 $
(document
.createElement("button"))
1041 .addClass("nitdoc-github-button")
1043 .click(function() { Nitdoc
.GitHub
.ModalBox
.close() })
1049 $
("#nitdoc-github-modal").addClass("nitdoc-github-error");
1052 $
("#nitdoc-github-modal").css({
1054 marginTop
: -($
("#nitdoc-github-modal").outerHeight() / 2) + "px",
1056 marginLeft
: -($
("#nitdoc-github-modal").outerWidth() / 2) + "px"
1060 // Close modal box instance
1061 var close
= function() {
1062 $
("#nitdoc-github-modal").remove();
1063 $
("#nitdoc-github-modal-fade").remove();
1076 * Nitdoc.GitHub.CommitBox instance
1079 // Init new commit box instance
1080 Nitdoc
.GitHub
.CommitBox
= function() {
1082 // Open commit box instance
1083 var open
= function(infos
) {
1085 $
(document
.createElement("div"))
1086 .attr("id", "nitdoc-github-commitBox-fade")
1087 .addClass("nitdoc-github-fade")
1090 $
(document
.createElement("div"))
1091 .attr("id", "nitdoc-github-commitBox")
1092 .addClass("nitdoc-github-modal")
1094 $
(document
.createElement("a"))
1095 .addClass("nitdoc-github-close")
1097 $
(document
.createElement("img"))
1098 .addClass("nitdoc-github-imgClose")
1100 src
: "resources/icons/close.png",
1105 ).click(function() { Nitdoc
.GitHub
.CommitBox
.close() })
1107 .append("<h3>Commit changes</h3>")
1109 $
(document
.createElement("div"))
1111 $
(document
.createElement("label"))
1112 .attr("for", "nitdoc-github-commit-message")
1116 $
(document
.createElement("textarea"))
1117 .attr("id", "nitdoc-github-commit-message")
1118 .append("doc: " + (infos
.isNew ?
"added" : "modified") + " comment for " + infos
.namespace)
1122 $
(document
.createElement("input"))
1124 id
: "nitdoc-github-commit-signedoff",
1126 value
: "Signed-off-by: " + infos
.user
.signedOff
1128 .change(function(e
) {
1129 if ($
(this).is(':checked')) {
1130 $
("#nitdoc-github-commit-button").removeAttr("disabled");
1132 $
("#nitdoc-github-commit-button").attr("disabled", "disabled");
1137 $
(document
.createElement("label"))
1138 .attr("for", "nitdoc-github-commit-signedoff")
1139 .append("Signed-off-by: " + infos
.user
.signedOff
)
1142 $
(document
.createElement("div"))
1143 .addClass("nitdoc-github-buttons")
1145 $
(document
.createElement("button"))
1147 id
: "nitdoc-github-commit-button",
1148 disabled
: "disabled"
1150 .addClass("nitdoc-github-button")
1152 $
(document
.createElement("img"))
1153 .attr("src", "resources/icons/github-icon.png")
1156 .mousedown(function() {
1157 $
(this).text("Commiting...");
1159 .mouseup(function() {
1160 infos
.message
= $
("#nitdoc-github-commit-message").val() + "\n\n" + infos
.user
.signedOff
;
1161 Nitdoc
.GitHub
.UI
.saveChanges(infos
);
1167 $
("#nitdoc-github-commitBox").css({
1169 marginTop
: -($
("#nitdoc-github-commitBox").outerHeight() / 2) + "px",
1171 marginLeft
: -($
("#nitdoc-github-commitBox").outerWidth() / 2) + "px"
1175 // Close commit box instance
1176 var close
= function() {
1177 $
("#nitdoc-github-commitBox").remove();
1178 $
("#nitdoc-github-commitBox-fade").remove();
1190 * Nitdoc.GitHub.Utils module
1193 Nitdoc
.GitHub
.Utils
= function() {
1194 // Extract infos from string location "../lib/standard/collection/array.nit:457,1--458,0"
1195 //FIXME this should be done by nitdoc
1196 var parseLocation
= function(location
) {
1197 var parts
= location
.split(":");
1198 var loc
= new Object();
1199 loc
.origin
= location
;
1200 loc
.path
= parts
[0].substr(3, parts
[0].length
);
1201 loc
.lstart
= parseInt(parts
[1].split("--")[0].split(",")[0]);
1202 loc
.tabpos
= parseInt(parts
[1].split("--")[0].split(",")[1]);
1203 loc
.lend
= parseInt(parts
[1].split("--")[1].split(",")[0]);
1207 // Meld modified comment into file content
1208 var mergeComment
= function(fileContent
, comment
, location
) {
1209 // replace comment in file content
1210 var res
= new String();
1211 var lines
= fileContent
.split("\n");
1212 // copy lines fron 0 to lstart
1213 for(var i
= 0; i
< location
.lstart
- 1; i
++) {
1214 res
+= lines
[i] + "\n";
1217 if(comment
&& comment
!= "") {
1218 var commentLines
= comment
.split("\n");
1219 for(var i
= 0; i
< commentLines
.length
; i
++) {
1220 var line
= commentLines
[i];
1221 var tab
= location
.tabpos
> 1 ?
"\t" : "";
1222 res
+= tab
+ (line
.length
> 0 ?
"# " : "#") + line
+ "\n";
1225 // copy lines fron lend to end
1226 for(var i
= location
.lend
- 1; i
< lines
.length
; i
++) {
1228 if(i
< lines
.length
- 1) { res
+= "\n"; }
1235 parseLocation
: parseLocation
,
1236 mergeComment
: mergeComment