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