android: exit application on destroy request
[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 $("<a/>")
44 .append(
45 $("<img/>")
46 .attr({
47 src: this.options.icon,
48 alt: this.options.iconAlt
49 })
50 .addClass("nitdoc-github-li-img")
51 )
52 .click($.proxy(this.toggle, this))
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 $("<label/>")
137 .attr("for", "nitdoc-github-login-field")
138 .append(this.options.usernameTxt)
139 )
140 .append(
141 $("<input/>")
142 .attr({
143 id: "nitdoc-github-login-field",
144 type: "text"
145 })
146 )
147 .append(
148 $("<label/>")
149 .attr("for", "nitdoc-github-password-field")
150 .append(this.options.passwordTxt)
151 )
152 .append(
153 $("<input/>")
154 .attr({
155 id: "nitdoc-github-password-field",
156 type: "password"
157 })
158 )
159 .append(
160 $("<label/>")
161 .attr("for", "nitdoc-github-repo-field")
162 .append(this.options.repoTxt)
163 )
164 .append(
165 $("<input/>")
166 .attr({
167 id: "nitdoc-github-repo-field",
168 type: "text"
169 })
170 )
171 .append(
172 $("<label/>")
173 .attr("for", "nitdoc-github-branch-field")
174 .append(this.options.branchTxt)
175 )
176 .append(
177 $("<input/>")
178 .attr({
179 id: "nitdoc-github-branch-field",
180 type: "text"
181 })
182 )
183 .append(
184 $("<button/>")
185 .addClass("nitdoc-github-button")
186 .attr("disabled", "disabled")
187 .append(
188 $("<img/>")
189 .attr("src", this.options.icon)
190 ).text(this.options.signinTxt)
191 .click($.proxy(this._doClickLogin, this))
192 )
193 );
194 $(".nitdoc-github-li-img").attr("src", this.options.icon);
195 },
196
197 toggle: function() {
198 if(this.loginBox.is(':hidden')) {
199 this.loginBox.show();
200 if ($('#nitdoc-github-login-field').is(':visible')) { $('#nitdoc-github-login-field').focus(); }
201 } else {
202 this.loginBox.hide();
203 }
204 },
205
206 /* events */
207
208 _doClickLogoff: function(event) {
209 this._trigger("_logoff", event);
210 },
211
212 _doClickLogin: function(event) {
213 this._trigger("_login", event, {
214 login: $('#nitdoc-github-login-field').val(),
215 password: $('#nitdoc-github-password-field').val(),
216 repo: $('#nitdoc-github-repo-field').val(),
217 branch: $('#nitdoc-github-branch-field').val()
218 });
219 return false;
220 },
221
222 _doFormChange: function(event) {
223 login = $('#nitdoc-github-login-field').val();
224 password = $('#nitdoc-github-password-field').val();
225 repo = $('#nitdoc-github-repo-field').val();
226 branch = $('#nitdoc-github-branch-field').val();
227 if(login && password && repo && branch) {
228 this.loginBox.find("form .nitdoc-github-button").removeAttr("disabled");
229 } else {
230 this.loginBox.find("form .nitdoc-github-button").attr("disabled", "disabled");
231 }
232 }
233 });
234 });