nitdoc: refactor Nitdoc.GitHub JS module into github/* plugin
[nit.git] / share / nitdoc / js / plugins / github / loginbox.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
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
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
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.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 define([
20 "jquery",
21 "plugins/github/ui",
22 "plugins/github/modalbox",
23 ], function($, UI, ModalBox) {
24 var LoginBox = {
25 init: function(containerSelector) {
26 this.loginBoxLi = $(document.createElement("li"))
27 .attr("id", "nitdoc-github-li")
28 .append(
29 $(document.createElement("a"))
30 .append(
31 $(document.createElement("img"))
32 .attr({
33 src: "resources/icons/github-icon.png",
34 alt: "GitHub"
35 })
36 .addClass("nitdoc-github-li-img")
37 )
38 .click(function() { LoginBox.toggle() })
39 )
40
41 this.loginBoxContent = $(document.createElement("div"));
42 this.loginBox = $(document.createElement("div"))
43 .attr("id", "nitdoc-github-loginbox")
44 .css("display", "none")
45 .append(
46 $(document.createElement("div"))
47 .addClass("nitdoc-github-loginbox-arrow")
48 .append(" ")
49 )
50 .append(
51 $(document.createElement("h3"))
52 .append("Github Sign In")
53 )
54 .append(this.loginBoxContent);
55
56 this.loginBoxLi.append(this.loginBox);
57 $(containerSelector).append(this.loginBoxLi);
58 },
59
60 // Panel of login box to display when the user is logged in
61 displayLogout: function(origin, user) {
62 var panel = $(document.createElement("div"))
63 .append(
64 $(document.createElement("h4"))
65 .append("Hello ")
66 .append(
67 $(document.createElement("a"))
68 .attr("href", "https://github.com/" + user.login)
69 .append(user.login)
70 ).append("!")
71 )
72 .append(
73 $(document.createElement("label"))
74 .attr("for", "github-origin")
75 .append("Origin")
76 )
77 .append(
78 $(document.createElement("input"))
79 .attr({
80 id: "github-origin",
81 type: "text",
82 disabled: "disabled",
83 value: origin.user + ":" + origin.repo + ":" + origin.branch
84 })
85 )
86 .append(
87 $(document.createElement("label"))
88 .attr("for", "github-base")
89 .append("Base")
90 )
91 .append(
92 $(document.createElement("input"))
93 .attr({
94 id: "github-base",
95 type: "text",
96 disabled: "disabled",
97 value: user.login + ":" + user.repo + ":" + user.branch
98 })
99 )
100 .append(
101 $(document.createElement("button"))
102 .addClass("nitdoc-github-button")
103 .addClass("nitdoc-github-cancel")
104 .append(
105 $(document.createElement("img"))
106 .attr("src", "resources/icons/github-icon.png")
107 ).append("Sign Off")
108 .click(function() { // log out user
109 UI.disactivate();
110 LoginBox.toggle();
111 })
112 );
113 $(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon-green.png");
114 this.loginBoxContent.empty()
115 this.loginBoxContent.append(panel);
116 },
117
118 // Panel of login box to display when the user is logged out
119 displayLogin: function() {
120 var panel = $(document.createElement("form"))
121 .append(
122 $(document.createElement("label"))
123 .attr("for", "nitdoc-github-login-field")
124 .append("Username")
125 )
126 .append(
127 $(document.createElement("input"))
128 .attr({
129 id: "nitdoc-github-login-field",
130 type: "text"
131 })
132 )
133 .append(
134 $(document.createElement("label"))
135 .attr("for", "nitdoc-github-password-field")
136 .append("Password")
137 )
138 .append(
139 $(document.createElement("input"))
140 .attr({
141 id: "nitdoc-github-password-field",
142 type: "password"
143 })
144 )
145 .append(
146 $(document.createElement("label"))
147 .attr("for", "nitdoc-github-repo-field")
148 .append("Repository")
149 )
150 .append(
151 $(document.createElement("input"))
152 .attr({
153 id: "nitdoc-github-repo-field",
154 type: "text"
155 })
156 )
157 .append(
158 $(document.createElement("label"))
159 .attr("for", "nitdoc-github-branch-field")
160 .append("Branch")
161 )
162 .append(
163 $(document.createElement("input"))
164 .attr({
165 id: "nitdoc-github-branch-field",
166 type: "text"
167 })
168 )
169 .append(
170 $(document.createElement("button"))
171 .addClass("nitdoc-github-button")
172 .append(
173 $(document.createElement("img"))
174 .attr("src", "resources/icons/github-icon.png")
175 ).append("Sign In")
176 .click(function() {
177 var login = $('#nitdoc-github-login-field').val();
178 var password = $('#nitdoc-github-password-field').val();
179 var repo = $('#nitdoc-github-repo-field').val();
180 var branch = $('#nitdoc-github-branch-field').val();
181 if(!login || !password || !repo || !branch) {
182 ModalBox.open("Sign in error", "Please enter your GitHub username, password, repository and branch.", true);
183 } else {
184 var user = UI.tryLogin(login, password, repo, branch);
185 if(user == "error:login") {
186 ModalBox.open("Sign in error", "The username, password, repo or branch you entered is incorrect.", true);
187 } else if(user == "error:sha") {
188 ModalBox.open("Base commit not found", "The provided GitHub repository must contains the base commit '" + UI.origin.sha + "'", true);
189 } else if(user == "error:profile") {
190 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);
191 } else {
192 UI.activate(user);
193 LoginBox.displayLogout(UI.origin, user);
194 }
195 }
196 return false;
197 })
198 )
199 $(".nitdoc-github-li-img").attr("src", "resources/icons/github-icon.png");
200 this.loginBoxContent.empty()
201 this.loginBoxContent.append(panel);
202 },
203
204 toggle: function() {
205 if(this.loginBox.is(':hidden')) {
206 this.loginBox.show();
207 if (!$('#loginGit').is(':hidden')) { $('#loginGit').focus(); }
208 } else {
209 this.loginBox.hide();
210 }
211 }
212 };
213
214 return LoginBox;
215 });