src/doc/api: add links to renderer code
[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
16 /*
17 * LoginBox allows user to login and logoff from GitHub API
18 */
19 define([
20 "jquery",
21 "jQueryUI"
22 ], function($) {
23 $.widget("nitdoc.loginbox", {
24 options: {
25 icon: "resources/icons/github-icon.png",
26 iconActive: "resources/icons/github-icon-green.png",
27 iconAlt: "GitHub",
28 signedinTxt: "Signed in Github",
29 signedoutTxt: "Sign in Github",
30 welcomeTxt: "Hello",
31 upstreamTxt: "Upstream branch",
32 baseTxt: "Base",
33 signoffTxt: "Sign Off",
34 usernameTxt: "Username",
35 passwordTxt: "Password",
36 repoTxt: "Repository",
37 branchTxt: "Branch",
38 signinTxt: "Sign In"
39 },
40
41 _create: function() {
42 this.element.append(
43 $("<span/>")
44 .addClass("glyphicon glyphicon-off")
45 //.click($.proxy(this.toggle, this))
46 .attr({
47 "data-container": "body",
48 "data-toggle": "popover",
49 "data-placement": "bottom",
50 "data-content": "bottom",
51 "data-html": "true",
52 })
53 );
54
55 this.content = $("<div/>");
56 this.loginBox = $("<div/>")
57 .attr("id", "nitdoc-github-loginbox")
58 .css("display", "none")
59 .append(
60 $(document.createElement("div"))
61 .addClass("nitdoc-github-loginbox-arrow")
62 .append("&nbsp;")
63 )
64 .append(this.content);
65 this.element.append(this.loginBox);
66 },
67
68 /* public actions */
69
70 displayLogout: function(origin, user) {
71 this.content.empty();
72 this.content.append(
73 $("<h3/>").text(this.options.signedinTxt)
74 )
75 this.content.append(
76 $("<div/>")
77 .append(
78 $("<h4/>")
79 .append(this.options.welcomeTxt + " ")
80 .append(
81 $("<a/>")
82 .attr("href", "https://github.com/" + user.login)
83 .append(user.login)
84 ).append(",")
85 )
86 .append(
87 $("<label/>")
88 .text("Upstream Branch")
89 )
90 .append(
91 $("<a/>")
92 .text(origin.user + ":" + origin.repo + ":" + origin.branch)
93 .addClass("nitdoc-github-loginbox-githublink")
94 .attr({
95 title: "Open branch in GitHub",
96 href: "https://github.com/" + origin.user + "/" + origin.repo + "/tree/" + origin.branch
97 })
98 )
99 .append(
100 $("<label/>")
101 .attr("for", "github-base")
102 .append("Your branch")
103 )
104 .append(
105 $("<a/>")
106 .text(user.login + ":" + user.repo + ":" + user.branch)
107 .addClass("nitdoc-github-loginbox-githublink")
108 .attr({
109 title: "Open branch in GitHub",
110 href: "https://github.com/" + user.login + "/" + user.repo + "/tree/" + user.branch
111 })
112 )
113 .append(
114 $("<button/>")
115 .addClass("nitdoc-github-button")
116 .addClass("nitdoc-github-cancel")
117 .append(
118 $("<img/>")
119 .attr("src", this.options.icon)
120 ).text(this.options.signoffTxt)
121 .click($.proxy(this._doClickLogoff, this))
122 )
123 );
124 $(".nitdoc-github-li-img").attr("src", this.options.iconActive);
125 },
126
127 displayLogin: function() {
128 this.content.empty();
129 this.content.append(
130 $("<h3/>").text(this.options.signedoutTxt)
131 )
132 this.content.append(
133 $("<form/>")
134 .keyup($.proxy(this._doFormChange, this))
135 .append(
136 $("<div/>")
137 .addClass("form-group")
138 .append(
139 $("<label/>")
140 .attr("for", "nitdoc-github-login-field")
141 .append(this.options.usernameTxt)
142 )
143 .addClass("form-group")
144 .append(
145 $("<input/>")
146 .attr({
147 id: "nitdoc-github-login-field",
148 type: "text",
149 "class": "form-control"
150 })
151 )
152 )
153 .append(
154 $("<div/>")
155 .addClass("form-group")
156 .append(
157 $("<label/>")
158 .attr("for", "nitdoc-github-password-field")
159 .append(this.options.passwordTxt)
160 )
161 .append(
162 $("<input/>")
163 .attr({
164 id: "nitdoc-github-password-field",
165 type: "password",
166 "class": "form-control"
167 })
168 )
169 )
170 .append(
171 $("<div/>")
172 .addClass("form-group")
173 .append(
174 $("<label/>")
175 .attr("for", "nitdoc-github-repo-field")
176 .append(this.options.repoTxt)
177 )
178 .append(
179 $("<input/>")
180 .attr({
181 id: "nitdoc-github-repo-field",
182 type: "text",
183 "class": "form-control"
184 })
185 )
186 )
187 .append(
188 $("<div/>")
189 .addClass("form-group")
190 .append(
191 $("<label/>")
192 .attr("for", "nitdoc-github-branch-field")
193 .append(this.options.branchTxt)
194 )
195 .append(
196 $("<input/>")
197 .attr({
198 id: "nitdoc-github-branch-field",
199 type: "text",
200 "class": "form-control"
201 })
202 )
203 )
204 .append(
205 $("<button/>")
206 .addClass("nitdoc-github-button btn btn-primary btn-lg pull-right")
207 .attr("disabled", "disabled")
208 .append(
209 $("<img/>")
210 .attr("src", this.options.icon)
211 ).text(this.options.signinTxt)
212 .click($.proxy(this._doClickLogin, this))
213 )
214 );
215 $(".nitdoc-github-li-img").attr("src", this.options.icon);
216 },
217
218 toggle: function() {
219 if(this.loginBox.is(':hidden')) {
220 this.loginBox.show();
221 if ($('#nitdoc-github-login-field').is(':visible')) { $('#nitdoc-github-login-field').focus(); }
222 } else {
223 this.loginBox.hide();
224 }
225 },
226
227 /* events */
228
229 _doClickLogoff: function(event) {
230 this._trigger("_logoff", event);
231 },
232
233 _doClickLogin: function(event) {
234 this._trigger("_login", event, {
235 login: $('#nitdoc-github-login-field').val(),
236 password: $('#nitdoc-github-password-field').val(),
237 repo: $('#nitdoc-github-repo-field').val(),
238 branch: $('#nitdoc-github-branch-field').val()
239 });
240 return false;
241 },
242
243 _doFormChange: function(event) {
244 login = $('#nitdoc-github-login-field').val();
245 password = $('#nitdoc-github-password-field').val();
246 repo = $('#nitdoc-github-repo-field').val();
247 branch = $('#nitdoc-github-branch-field').val();
248 if(login && password && repo && branch) {
249 this.loginBox.find("form .nitdoc-github-button").removeAttr("disabled");
250 } else {
251 this.loginBox.find("form .nitdoc-github-button").attr("disabled", "disabled");
252 }
253 }
254 });
255 });