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