]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/panes/connect.js
fix connect button not closing 'Connect" tab
[irc/quakenet/qwebirc.git] / js / ui / panes / connect.js
CommitLineData
e89c812f
CP
1qwebirc.ui.ConnectPane = new Class({
2 Implements: [Events],
3 initialize: function(parent, options) {
13afa38f 4 var callback = options.callback, initialNickname = options.initialNickname, initialChannels = options.initialChannels, autoConnect = options.autoConnect, autoNick = options.autoNick;
e89c812f 5 this.options = options;
cbd6ec8c 6 this.cookie = new Hash.Cookie("optconn", {duration: 3650, autoSave: false});
13afa38f 7 var uiOptions = options.uiOptions;
65f2c94c 8 this.__windowName = "authgate_" + Math.floor(Math.random() * 100000);
e89c812f
CP
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
13afa38f 16 var rootElement = parent.getElement("[name=connectroot]");
e89c812f
CP
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;
e89c812f 22
13afa38f
CP
23 var box = (autoConnect ? "confirm" : "login");
24 exec("[name=" + box + "box]", util.setVisible(true));
25
26 if(!autoConnect) {
614427b2 27 if($defined(uiOptions.logoURL)) {
13afa38f 28 var logoBar = parent.getElement("[class=bar-logo]");
614427b2
CP
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
13afa38f
CP
32 util.makeVisible(parent.getElement("[name=loginheader]"));
33 } else {
34 util.makeVisible(parent.getElement("[name=nologologinheader]"));
35 }
36 }
66619e20 37
cbd6ec8c
CP
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
e89c812f
CP
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));
13afa38f 55 exec("[name=networkname]", util.setText(uiOptions.networkName));
e89c812f
CP
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
65f2c94c
CP
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
00bd0d60
CP
88 if(window == window.top) /* don't focus when we're iframe'd */
89 exec("[name=" + focus + "]", util.focus);
e89c812f 90 exec("[name=connect]", util.attachClick(this.__connect.bind(this)));
65f2c94c 91 exec("[name=loginconnect]", util.attachClick(this.__loginConnect.bind(this)));
3bf9d70e 92
65f2c94c 93 exec("[name=login]", util.attachClick(this.__login.bind(this)));
3bf9d70e
CP
94
95 if(qwebirc.ui.isHideAuth())
96 exec("[name=login]", util.setVisible(false));
e89c812f
CP
97 }.bind(this)});
98 r.get();
99 },
100 util: {
101 makeVisible: function(x) { x.setStyle("display", ""); },
65f2c94c 102 setVisible: function(y) { return function(x) { x.setStyle("display", y ? "" : "none"); }; },
2ba5d023 103 focus: function(x) { try { x.focus(); } catch (e) { } },
e89c812f
CP
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;
66de775f 121
65f2c94c 122 this.__cancelLogin();
264025fd 123 this.fireEvent("close");
cbd6ec8c
CP
124 this.cookie.extend(data);
125 this.cookie.save();
e89c812f
CP
126 this.options.callback(data);
127 },
65f2c94c
CP
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
a205506c 205 __qwebircAuthCallback = function(username, expiry, serverNow) {
65f2c94c
CP
206 this.__cancelLoginCallback(true);
207
a205506c
CP
208 var now = new Date().getTime();
209 var offset = (serverNow * 1000) - now;
210 var ourExpiry = expiry * 1000 - offset;
211 Cookie.write("ticketexpiry", ourExpiry)
212
65f2c94c
CP
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 },
e89c812f
CP
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]");
66de775f 225
e89c812f
CP
226 var nickname = nick.value;
227 var chans = chan.value;
228 if(chans == "#") /* sorry channel "#" :P */
229 chans = "";
66de775f 230
e89c812f
CP
231 if(!nickname) {
232 alert("You must supply a nickname.");
233 nick.focus();
234 return false;
235 }
66de775f 236
fc25cf01
CP
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;
2f74dea9
CP
243 }
244
e89c812f
CP
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]));
2f74dea9 258 }
66de775f 259 }
e89c812f 260});
66de775f 261
e89c812f
CP
262qwebirc.ui.LoginBox2 = function(parentElement, callback, initialNickname, initialChannels, networkName) {
263/*
6ce70043
CP
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);
2f74dea9 268
6ce70043
CP
269 var usernameBox = new Element("input");
270 var usernameRow = createRow("Username:", usernameBox, {display: "none"})[0];
2f74dea9 271
6ce70043
CP
272 var passwordRow = createRow("Password:", null, {display: "none"});
273 var passwordBox = qwebirc.util.createInput("password", passwordRow[1], "connect_auth_password");
2f74dea9 274
6ce70043
CP
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 }
2f74dea9 280 }
e89c812f
CP
281 */
282
66de775f
CP
283 var connbutton = new Element("input", {"type": "submit"});
284 connbutton.set("value", "Connect");
2f74dea9
CP
285 var r = createRow(undefined, connbutton);
286
66de775f
CP
287 form.addEvent("submit", function(e) {
288 new Event(e).stop();
66de775f 289
66de775f
CP
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 }
cc608160
CP
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
2f74dea9 308 var data = {"nickname": nickname, "autojoin": chans};
6f848bbb
CP
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;
6ce70043 319 }
6f848bbb
CP
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();
6ce70043
CP
326 return;
327 }
328
6f848bbb 329 data["serverPassword"] = passwordBox.value;
6ce70043 330 }
6ce70043 331 }
6ce70043
CP
332 parentElement.removeChild(outerbox);
333
2f74dea9 334 callback(data);
66de775f 335 }.bind(this));
6ce70043 336
66de775f
CP
337 nick.set("value", initialNickname);
338 chan.set("value", initialChannels);
339
b1a46dae 340 if(window == window.top)
9a1c691c 341 nick.focus();
cad18909 342}
2f74dea9
CP
343
344qwebirc.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}
65f2c94c
CP
355
356qwebirc.ui.isAuthRequired = (function() {
357 var args = qwebirc.util.parseURI(String(document.location));
86100f08 358 var value = $defined(args) && args.get("authrequired");
65f2c94c
CP
359 return function() {
360 return value && qwebirc.auth.enabled();
361 };
362})();
363
3bf9d70e
CP
364qwebirc.ui.isHideAuth = (function() {
365 var args = qwebirc.util.parseURI(String(document.location));
86100f08 366 var value = $defined(args) && args.get("hideauth");
3bf9d70e
CP
367 return function() {
368 return value;
369 };
370})();
371