]>
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 | ||
e89c812f CP |
59 | exec("[name=" + focus + "]", util.focus); |
60 | exec("[name=connect]", util.attachClick(this.__connect.bind(this))); | |
65f2c94c CP |
61 | exec("[name=loginconnect]", util.attachClick(this.__loginConnect.bind(this))); |
62 | exec("[name=login]", util.attachClick(this.__login.bind(this))); | |
e89c812f CP |
63 | }.bind(this)}); |
64 | r.get(); | |
65 | }, | |
66 | util: { | |
67 | makeVisible: function(x) { x.setStyle("display", ""); }, | |
65f2c94c | 68 | setVisible: function(y) { return function(x) { x.setStyle("display", y ? "" : "none"); }; }, |
e89c812f CP |
69 | focus: function(x) { x.focus(); }, |
70 | attachClick: function(fn) { return function(x) { x.addListener("click", fn); } }, | |
71 | setText: function(x) { return function(y) { | |
72 | if(typeof y.value === "undefined") { | |
73 | y.set("text", x); | |
74 | } else { | |
75 | y.value = x === null ? "" : x; | |
76 | } | |
77 | } } | |
78 | }, | |
79 | validate: function() { | |
80 | return this.__validate(); | |
81 | }, | |
82 | __connect: function(e) { | |
83 | new Event(e).stop(); | |
84 | var data = this.validate(); | |
85 | if(data === false) | |
86 | return; | |
66de775f | 87 | |
65f2c94c | 88 | this.__cancelLogin(); |
e89c812f CP |
89 | this.fireEvent("close"); |
90 | this.options.callback(data); | |
91 | }, | |
65f2c94c CP |
92 | __cancelLogin: function(noUIModifications) { |
93 | if(this.__cancelLoginCallback) | |
94 | this.__cancelLoginCallback(noUIModifications); | |
95 | }, | |
96 | __loginConnect: function(e) { | |
97 | new Event(e).stop(); | |
98 | if(this.validate() === false) | |
99 | return; | |
100 | ||
101 | this.__performLogin(function() { | |
102 | var data = this.validate(); | |
103 | if(data === false) { | |
104 | /* we're logged in -- show the normal join button */ | |
105 | this.util.exec("[name=connectbutton]", this.util.setVisible(true)); | |
106 | return; | |
107 | } | |
108 | ||
109 | this.fireEvent("close"); | |
110 | this.options.callback(data); | |
111 | }.bind(this), "loginconnectbutton"); | |
112 | }, | |
113 | __login: function(e) { | |
114 | new Event(e).stop(); | |
115 | ||
116 | this.__cancelLogin(true); | |
117 | ||
118 | this.__performLogin(function() { | |
119 | var focus = "connect"; | |
120 | if(!this.options.autoConnect) { | |
121 | var nick = this.rootElement.getElement("input[name=nickname]").value, chan = this.rootElement.getElement("input[name=channels]").value; | |
122 | if(!nick) { | |
123 | focus = "nickname"; | |
124 | } else if(!chan) { | |
125 | focus = "channels"; | |
126 | } | |
127 | } | |
128 | this.util.exec("[name=" + focus + "]", this.util.focus); | |
129 | }.bind(this), "login"); | |
130 | }, | |
131 | __performLogin: function(callback, calleename) { | |
132 | Cookie.write("jslogin", "1"); | |
133 | ||
134 | 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"); | |
135 | ||
136 | if(handle === null || handle === undefined) { | |
137 | Cookie.dispose("jslogin"); | |
138 | // Cookie.write("redirect", document.location); | |
139 | // window.location = "auth?"; | |
140 | return; | |
141 | } | |
142 | ||
143 | var closeDetector = function() { | |
144 | if(handle.closed) | |
145 | this.__cancelLoginCallback(); | |
146 | }.bind(this); | |
147 | var closeCallback = closeDetector.periodical(100); | |
148 | ||
149 | this.__cancelLoginCallback = function(noUIModifications) { | |
150 | $clear(closeCallback); | |
151 | ||
152 | Cookie.dispose("jslogin"); | |
153 | ||
154 | try { | |
155 | handle.close(); | |
156 | } catch(e) { | |
157 | } | |
158 | ||
159 | if(!noUIModifications) { | |
160 | this.util.exec("[name=loggingin]", this.util.setVisible(false)); | |
161 | this.util.exec("[name=" + calleename + "]", this.util.setVisible(true)); | |
162 | } | |
163 | this.__cancelLoginCallback = null; | |
164 | }.bind(this); | |
165 | ||
166 | this.util.exec("[name=loggingin]", this.util.setVisible(true)); | |
167 | this.util.exec("[name=" + calleename + "]", this.util.setVisible(false)); | |
168 | ||
169 | __qwebircAuthCallback = function(username) { | |
170 | this.__cancelLoginCallback(true); | |
171 | ||
172 | this.util.exec("[name=loggingin]", this.util.setVisible(false)); | |
173 | this.util.exec("[name=loginstatus]", this.util.setVisible(true)); | |
174 | this.util.exec("[name=authname]", this.util.setText(username)); | |
175 | callback(); | |
176 | }.bind(this); | |
177 | ||
178 | }, | |
e89c812f CP |
179 | __validateConfirmData: function() { |
180 | return {nickname: this.options.initialNickname, autojoin: this.options.initialChannels}; | |
181 | }, | |
182 | __validateLoginData: function() { | |
183 | var nick = this.rootElement.getElement("input[name=nickname]"), chan = this.rootElement.getElement("input[name=channels]"); | |
66de775f | 184 | |
e89c812f CP |
185 | var nickname = nick.value; |
186 | var chans = chan.value; | |
187 | if(chans == "#") /* sorry channel "#" :P */ | |
188 | chans = ""; | |
66de775f | 189 | |
e89c812f CP |
190 | if(!nickname) { |
191 | alert("You must supply a nickname."); | |
192 | nick.focus(); | |
193 | return false; | |
194 | } | |
66de775f | 195 | |
e89c812f CP |
196 | var data = {nickname: nickname, autojoin: chans}; |
197 | return data; | |
198 | }, | |
199 | __buildPrettyChannels: function(node, channels) { | |
200 | var c = channels.split(" ")[0].split(","); | |
201 | node.appendChild(document.createTextNode("channel" + ((c.length>1)?"s":"") + " ")); | |
202 | for(var i=0;i<c.length;i++) { | |
203 | if((c.length > 1) && (i == c.length - 1)) { | |
204 | node.appendChild(document.createTextNode(" and ")); | |
205 | } else if(i > 0) { | |
206 | node.appendChild(document.createTextNode(", ")); | |
207 | } | |
208 | node.appendChild(new Element("b").set("text", c[i])); | |
2f74dea9 | 209 | } |
66de775f | 210 | } |
e89c812f | 211 | }); |
66de775f | 212 | |
e89c812f CP |
213 | qwebirc.ui.LoginBox2 = function(parentElement, callback, initialNickname, initialChannels, networkName) { |
214 | /* | |
6ce70043 CP |
215 | if(qwebirc.auth.enabled()) { |
216 | if(qwebirc.auth.passAuth()) { | |
217 | var authRow = createRow("Auth to services:"); | |
218 | var authCheckBox = qwebirc.util.createInput("checkbox", authRow, "connect_auth_to_services", false); | |
2f74dea9 | 219 | |
6ce70043 CP |
220 | var usernameBox = new Element("input"); |
221 | var usernameRow = createRow("Username:", usernameBox, {display: "none"})[0]; | |
2f74dea9 | 222 | |
6ce70043 CP |
223 | var passwordRow = createRow("Password:", null, {display: "none"}); |
224 | var passwordBox = qwebirc.util.createInput("password", passwordRow[1], "connect_auth_password"); | |
2f74dea9 | 225 | |
6ce70043 CP |
226 | authCheckBox.addEvent("click", function(e) { qwebirc.ui.authShowHide(authCheckBox, authRow, usernameBox, usernameRow, passwordRow[0]) }); |
227 | } else if(qwebirc.auth.bouncerAuth()) { | |
228 | var passwordRow = createRow("Password:"); | |
229 | var passwordBox = qwebirc.util.createInput("password", passwordRow, "connect_auth_password"); | |
230 | } | |
2f74dea9 | 231 | } |
e89c812f CP |
232 | */ |
233 | ||
66de775f CP |
234 | var connbutton = new Element("input", {"type": "submit"}); |
235 | connbutton.set("value", "Connect"); | |
2f74dea9 CP |
236 | var r = createRow(undefined, connbutton); |
237 | ||
66de775f CP |
238 | form.addEvent("submit", function(e) { |
239 | new Event(e).stop(); | |
66de775f | 240 | |
2f74dea9 | 241 | var data = {"nickname": nickname, "autojoin": chans}; |
6f848bbb CP |
242 | if(qwebirc.auth.enabled()) { |
243 | if(qwebirc.auth.passAuth() && authCheckBox.checked) { | |
244 | if(!usernameBox.value || !passwordBox.value) { | |
245 | alert("You must supply your username and password in auth mode."); | |
246 | if(!usernameBox.value) { | |
247 | usernameBox.focus(); | |
248 | } else { | |
249 | passwordBox.focus(); | |
250 | } | |
251 | return; | |
6ce70043 | 252 | } |
6f848bbb CP |
253 | |
254 | data["serverPassword"] = usernameBox.value + " " + passwordBox.value; | |
255 | } else if(qwebirc.auth.bouncerAuth()) { | |
256 | if(!passwordBox.value) { | |
257 | alert("You must supply a password."); | |
258 | passwordBox.focus(); | |
6ce70043 CP |
259 | return; |
260 | } | |
261 | ||
6f848bbb | 262 | data["serverPassword"] = passwordBox.value; |
6ce70043 | 263 | } |
6ce70043 | 264 | } |
6ce70043 CP |
265 | parentElement.removeChild(outerbox); |
266 | ||
2f74dea9 | 267 | callback(data); |
66de775f | 268 | }.bind(this)); |
6ce70043 | 269 | |
66de775f CP |
270 | nick.set("value", initialNickname); |
271 | chan.set("value", initialChannels); | |
272 | ||
b1a46dae | 273 | if(window == window.top) |
9a1c691c | 274 | nick.focus(); |
cad18909 | 275 | } |
2f74dea9 CP |
276 | |
277 | qwebirc.ui.authShowHide = function(checkbox, authRow, usernameBox, usernameRow, passwordRow) { | |
278 | var visible = checkbox.checked; | |
279 | var display = visible?null:"none"; | |
280 | usernameRow.setStyle("display", display); | |
281 | passwordRow.setStyle("display", display); | |
282 | ||
283 | if(visible) { | |
284 | // authRow.parentNode.setStyle("display", "none"); | |
285 | usernameBox.focus(); | |
286 | } | |
287 | } | |
65f2c94c CP |
288 | |
289 | qwebirc.ui.isAuthRequired = (function() { | |
290 | var args = qwebirc.util.parseURI(String(document.location)); | |
291 | var value = $defined(args) && args["authrequired"]; | |
292 | return function() { | |
293 | return value && qwebirc.auth.enabled(); | |
294 | }; | |
295 | })(); | |
296 |