]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/connect.js
add spinner to connect dialog
[irc/quakenet/qwebirc.git] / js / ui / panes / connect.js
1 qwebirc.ui.ConnectPane = new Class({
2 Implements: [Events],
3 initialize: function(parent, options) {
4 var callback = options.callback, initialNickname = options.initialNickname, initialChannels = options.initialChannels, autoConnect = options.autoConnect, autoNick = options.autoNick;
5 this.options = options;
6 this.cookie = new Hash.Cookie("optconn", {duration: 3650, autoSave: false});
7 var uiOptions = options.uiOptions;
8 this.__windowName = "authgate_" + Math.floor(Math.random() * 100000);
9
10 var delayfn = function() { parent.set("html", "<div class=\"loading\">Loading. . .</div>"); };
11 var cb = delayfn.delay(500);
12
13 var r = qwebirc.ui.RequestTransformHTML({url: qwebirc.global.staticBaseURL + "panes/connect.html", update: parent, onSuccess: function() {
14 $clear(cb);
15
16 var rootElement = parent.getElement("[name=connectroot]");
17 this.rootElement = rootElement;
18
19 this.util.exec = function(n, x) { rootElement.getElements(n).each(x); };
20 var util = this.util;
21 var exec = util.exec;
22
23 var box = (autoConnect ? "confirm" : "login");
24 exec("[name=" + box + "box]", util.setVisible(true));
25
26 if(!autoConnect) {
27 if($defined(uiOptions.logoURL)) {
28 var logoBar = parent.getElement("[class=bar-logo]");
29 if(uiOptions.logoURL)
30 logoBar.setAttribute("style", "background: url(" + uiOptions.logoURL + ") no-repeat center top; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + uiOptions.logoURL + "',sizingMethod='crop');");
31
32 util.makeVisible(parent.getElement("[name=loginheader]"));
33 } else {
34 util.makeVisible(parent.getElement("[name=nologologinheader]"));
35 }
36 }
37
38 if(initialNickname === null && initialChannels === null) {
39 var n2 = this.cookie.get("nickname");
40 if(n2 !== null)
41 initialNickname = n2;
42
43 var c2 = this.cookie.get("autojoin");
44 if(c2 !== null)
45 initialChannels = c2;
46 }
47
48 if(initialChannels === null) {
49 initialChannels = "";
50 }
51
52 exec("[name=nickname]", util.setText(initialNickname));
53 exec("[name=channels]", util.setText(initialChannels));
54 exec("[name=prettychannels]", function(node) { this.__buildPrettyChannels(node, initialChannels); }.bind(this));
55 exec("[name=networkname]", util.setText(uiOptions.networkName));
56
57 var focus = "connect";
58 if(autoConnect) {
59 if(!autoNick)
60 exec("[name=nickselected]", util.makeVisible);
61
62 this.__validate = this.__validateConfirmData;
63 } else {
64 if(!initialNickname) {
65 focus = "nickname";
66 } else if(initialNickname && !initialChannels) {
67 focus = "channels";
68 }
69
70 this.__validate = this.__validateLoginData;
71 }
72
73 if(qwebirc.auth.loggedin()) {
74 exec("[name=authname]", util.setText(qwebirc.auth.loggedin()));
75 exec("[name=connectbutton]", util.makeVisible);
76 exec("[name=loginstatus]", util.makeVisible);
77 } else {
78 if(qwebirc.ui.isAuthRequired()) {
79 exec("[name=loginconnectbutton]", util.makeVisible);
80 if(focus == "connect")
81 focus = "loginconnect";
82 } else {
83 exec("[name=connectbutton]", util.makeVisible);
84 exec("[name=loginbutton]", util.makeVisible);
85 }
86 }
87
88 if(window == window.top) /* don't focus when we're iframe'd */
89 exec("[name=" + focus + "]", util.focus);
90 exec("[name=connect]", util.attachClick(this.__connect.bind(this)));
91 exec("[name=loginconnect]", util.attachClick(this.__loginConnect.bind(this)));
92
93 exec("[name=login]", util.attachClick(this.__login.bind(this)));
94
95 if(qwebirc.ui.isHideAuth())
96 exec("[name=login]", util.setVisible(false));
97 }.bind(this)});
98 r.get();
99 },
100 util: {
101 makeVisible: function(x) { x.setStyle("display", ""); },
102 setVisible: function(y) { return function(x) { x.setStyle("display", y ? "" : "none"); }; },
103 focus: function(x) { try { x.focus(); } catch (e) { } },
104 attachClick: function(fn) { return function(x) { x.addListener("click", fn); } },
105 setText: function(x) { return function(y) {
106 if(typeof y.value === "undefined") {
107 y.set("text", x);
108 } else {
109 y.value = x === null ? "" : x;
110 }
111 } }
112 },
113 validate: function() {
114 return this.__validate();
115 },
116 __connect: function(e) {
117 new Event(e).stop();
118 var data = this.validate();
119 if(data === false)
120 return;
121
122 this.__cancelLogin();
123 this.fireEvent("close");
124 this.cookie.extend(data);
125 this.cookie.save();
126 this.options.callback(data);
127 },
128 __cancelLogin: function(noUIModifications) {
129 if(this.__cancelLoginCallback)
130 this.__cancelLoginCallback(noUIModifications);
131 },
132 __loginConnect: function(e) {
133 new Event(e).stop();
134 if(this.validate() === false)
135 return;
136
137 this.__performLogin(function() {
138 var data = this.validate();
139 if(data === false) {
140 /* we're logged in -- show the normal join button */
141 this.util.exec("[name=connectbutton]", this.util.setVisible(true));
142 return;
143 }
144
145 this.fireEvent("close");
146 this.options.callback(data);
147 }.bind(this), "loginconnectbutton");
148 },
149 __login: function(e) {
150 new Event(e).stop();
151
152 this.__cancelLogin(true);
153
154 this.__performLogin(function() {
155 var focus = "connect";
156 if(!this.options.autoConnect) {
157 var nick = this.rootElement.getElement("input[name=nickname]").value, chan = this.rootElement.getElement("input[name=channels]").value;
158 if(!nick) {
159 focus = "nickname";
160 } else if(!chan) {
161 focus = "channels";
162 }
163 }
164 this.util.exec("[name=" + focus + "]", this.util.focus);
165 }.bind(this), "login");
166 },
167 __performLogin: function(callback, calleename) {
168 Cookie.write("jslogin", "1");
169
170 var handle = window.open("/auth", this.__windowName, "status=0,toolbar=0,location=1,menubar=0,directories=0,resizable=0,scrollbars=1,height=280,width=550");
171
172 if(handle === null || handle === undefined) {
173 Cookie.dispose("jslogin");
174 // Cookie.write("redirect", document.location);
175 // window.location = "auth?";
176 return;
177 }
178
179 var closeDetector = function() {
180 if(handle.closed)
181 this.__cancelLoginCallback();
182 }.bind(this);
183 var closeCallback = closeDetector.periodical(100);
184
185 this.__cancelLoginCallback = function(noUIModifications) {
186 $clear(closeCallback);
187
188 Cookie.dispose("jslogin");
189
190 try {
191 handle.close();
192 } catch(e) {
193 }
194
195 if(!noUIModifications) {
196 this.util.exec("[name=loggingin]", this.util.setVisible(false));
197 this.util.exec("[name=" + calleename + "]", this.util.setVisible(true));
198 }
199 this.__cancelLoginCallback = null;
200 }.bind(this);
201
202 this.util.exec("[name=loggingin]", this.util.setVisible(true));
203 this.util.exec("[name=" + calleename + "]", this.util.setVisible(false));
204
205 __qwebircAuthCallback = function(username, expiry, serverNow) {
206 this.__cancelLoginCallback(true);
207
208 var now = new Date().getTime();
209 var offset = (serverNow * 1000) - now;
210 var ourExpiry = expiry * 1000 - offset;
211 Cookie.write("ticketexpiry", ourExpiry)
212
213 this.util.exec("[name=loggingin]", this.util.setVisible(false));
214 this.util.exec("[name=loginstatus]", this.util.setVisible(true));
215 this.util.exec("[name=authname]", this.util.setText(username));
216 callback();
217 }.bind(this);
218
219 },
220 __validateConfirmData: function() {
221 return {nickname: this.options.initialNickname, autojoin: this.options.initialChannels};
222 },
223 __validateLoginData: function() {
224 var nick = this.rootElement.getElement("input[name=nickname]"), chan = this.rootElement.getElement("input[name=channels]");
225
226 var nickname = nick.value;
227 var chans = chan.value;
228 if(chans == "#") /* sorry channel "#" :P */
229 chans = "";
230
231 if(!nickname) {
232 alert("You must supply a nickname.");
233 nick.focus();
234 return false;
235 }
236
237 var stripped = qwebirc.global.nicknameValidator.validate(nickname);
238 if(stripped != nickname) {
239 nick.value = stripped;
240 alert("Your nickname was invalid and has been corrected; please check your altered nickname and try again.");
241 nick.focus();
242 return false;
243 }
244
245 var data = {nickname: nickname, autojoin: chans};
246 return data;
247 },
248 __buildPrettyChannels: function(node, channels) {
249 var c = channels.split(" ")[0].split(",");
250 node.appendChild(document.createTextNode("channel" + ((c.length>1)?"s":"") + " "));
251 for(var i=0;i<c.length;i++) {
252 if((c.length > 1) && (i == c.length - 1)) {
253 node.appendChild(document.createTextNode(" and "));
254 } else if(i > 0) {
255 node.appendChild(document.createTextNode(", "));
256 }
257 node.appendChild(new Element("b").set("text", c[i]));
258 }
259 }
260 });
261
262 qwebirc.ui.LoginBox2 = function(parentElement, callback, initialNickname, initialChannels, networkName) {
263 /*
264 if(qwebirc.auth.enabled()) {
265 if(qwebirc.auth.passAuth()) {
266 var authRow = createRow("Auth to services:");
267 var authCheckBox = qwebirc.util.createInput("checkbox", authRow, "connect_auth_to_services", false);
268
269 var usernameBox = new Element("input");
270 var usernameRow = createRow("Username:", usernameBox, {display: "none"})[0];
271
272 var passwordRow = createRow("Password:", null, {display: "none"});
273 var passwordBox = qwebirc.util.createInput("password", passwordRow[1], "connect_auth_password");
274
275 authCheckBox.addEvent("click", function(e) { qwebirc.ui.authShowHide(authCheckBox, authRow, usernameBox, usernameRow, passwordRow[0]) });
276 } else if(qwebirc.auth.bouncerAuth()) {
277 var passwordRow = createRow("Password:");
278 var passwordBox = qwebirc.util.createInput("password", passwordRow, "connect_auth_password");
279 }
280 }
281 */
282
283 var connbutton = new Element("input", {"type": "submit"});
284 connbutton.set("value", "Connect");
285 var r = createRow(undefined, connbutton);
286
287 form.addEvent("submit", function(e) {
288 new Event(e).stop();
289
290 var nickname = nick.value;
291 var chans = chan.value;
292 if(chans == "#") /* sorry channel "#" :P */
293 chans = "";
294
295 if(!nickname) {
296 alert("You must supply a nickname.");
297 nick.focus();
298 return;
299 }
300 var stripped = qwebirc.global.nicknameValidator.validate(nickname);
301 if(stripped != nickname) {
302 nick.value = stripped;
303 alert("Your nickname was invalid and has been corrected; please check your altered nickname and press Connect again.");
304 nick.focus();
305 return;
306 }
307
308 var data = {"nickname": nickname, "autojoin": chans};
309 if(qwebirc.auth.enabled()) {
310 if(qwebirc.auth.passAuth() && authCheckBox.checked) {
311 if(!usernameBox.value || !passwordBox.value) {
312 alert("You must supply your username and password in auth mode.");
313 if(!usernameBox.value) {
314 usernameBox.focus();
315 } else {
316 passwordBox.focus();
317 }
318 return;
319 }
320
321 data["serverPassword"] = usernameBox.value + " " + passwordBox.value;
322 } else if(qwebirc.auth.bouncerAuth()) {
323 if(!passwordBox.value) {
324 alert("You must supply a password.");
325 passwordBox.focus();
326 return;
327 }
328
329 data["serverPassword"] = passwordBox.value;
330 }
331 }
332 parentElement.removeChild(outerbox);
333
334 callback(data);
335 }.bind(this));
336
337 nick.set("value", initialNickname);
338 chan.set("value", initialChannels);
339
340 if(window == window.top)
341 nick.focus();
342 }
343
344 qwebirc.ui.authShowHide = function(checkbox, authRow, usernameBox, usernameRow, passwordRow) {
345 var visible = checkbox.checked;
346 var display = visible?null:"none";
347 usernameRow.setStyle("display", display);
348 passwordRow.setStyle("display", display);
349
350 if(visible) {
351 // authRow.parentNode.setStyle("display", "none");
352 usernameBox.focus();
353 }
354 }
355
356 qwebirc.ui.isAuthRequired = (function() {
357 var args = qwebirc.util.parseURI(String(document.location));
358 var value = $defined(args) && args.get("authrequired");
359 return function() {
360 return value && qwebirc.auth.enabled();
361 };
362 })();
363
364 qwebirc.ui.isHideAuth = (function() {
365 var args = qwebirc.util.parseURI(String(document.location));
366 var value = $defined(args) && args.get("hideauth");
367 return function() {
368 return value;
369 };
370 })();
371