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