]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/baseui.js
Don't use WindowsException.
[irc/quakenet/qwebirc.git] / js / ui / baseui.js
CommitLineData
e20e5a6b
CP
1qwebirc.ui.WINDOW_STATUS = 1;
2qwebirc.ui.WINDOW_QUERY = 2;
3qwebirc.ui.WINDOW_CHANNEL = 3;
4qwebirc.ui.WINDOW_CUSTOM = 4;
5qwebirc.ui.WINDOW_CONNECT = 5;
f74802c5 6qwebirc.ui.WINDOW_MESSAGES = 6;
e20e5a6b 7qwebirc.ui.CUSTOM_CLIENT = "custom";
9e769c12 8
e20e5a6b 9qwebirc.ui.BaseUI = new Class({
2cad083e 10 Implements: [Events],
a59dc700 11 initialize: function(parentElement, windowClass, uiName, options) {
2cad083e 12 this.options = options;
a59dc700 13
9e769c12 14 this.windows = {};
ffbb638d 15 this.clients = {};
e20e5a6b 16 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
9e769c12
CP
17 this.windowArray = [];
18 this.windowClass = windowClass;
19 this.parentElement = parentElement;
20 this.parentElement.addClass("qwebirc");
21 this.parentElement.addClass("qwebirc-" + uiName);
e8db8558 22 this.firstClient = false;
e20e5a6b 23 this.commandhistory = new qwebirc.irc.CommandHistory();
ffbb638d 24 this.clientId = 0;
9e769c12
CP
25 },
26 newClient: function(client) {
ffbb638d 27 client.id = this.clientId++;
96f28062
CP
28 client.hilightController = new qwebirc.ui.HilightController(client);
29
ffbb638d
CP
30 this.windows[client.id] = {}
31 this.clients[client.id] = client;
e20e5a6b 32 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
9e769c12 33 this.selectWindow(w);
e8db8558
CP
34 if(!this.firstClient) {
35 this.firstClient = true;
e20e5a6b 36 w.addLine("", "qwebirc v" + qwebirc.VERSION);
2dfab0e1
CP
37 w.addLine("", "Copyright (C) 2008-2009 Chris Porter and the qwebirc project.");
38 w.addLine("", "http://www.qwebirc.org");
39 w.addLine("", "Licensed under the GNU General Public License, Version 2.");
e8db8558 40 }
9e769c12
CP
41 return w;
42 },
ffbb638d
CP
43 getClientId: function(client) {
44 if(client == qwebirc.ui.CUSTOM_CLIENT) {
45 return qwebirc.ui.CUSTOM_CLIENT;
46 } else {
47 return client.id;
48 }
49 },
fd3734d4 50 getWindowIdentifier: function(client, type, name) {
f74802c5
CP
51 if(type == qwebirc.ui.WINDOW_MESSAGES)
52 return "-M";
e20e5a6b 53 if(type == qwebirc.ui.WINDOW_STATUS)
f74802c5 54 return "";
fd3734d4
CP
55
56 if(client == qwebirc.ui.CUSTOM_CLIENT) /* HACK */
57 return "_" + name;
58
59 return "_" + client.toIRCLower(name);
f74802c5
CP
60 },
61 newWindow: function(client, type, name) {
62 var w = this.getWindow(client, type, name);
63 if($defined(w))
64 return w;
9e769c12 65
fd3734d4 66 var wId = this.getWindowIdentifier(client, type, name);
f74802c5 67 var w = this.windows[this.getClientId(client)][wId] = new this.windowClass(this, client, type, name, wId);
9e769c12
CP
68 this.windowArray.push(w);
69
70 return w;
71 },
f74802c5
CP
72 getWindow: function(client, type, name) {
73 var c = this.windows[this.getClientId(client)];
74 if(!$defined(c))
75 return null;
76
fd3734d4 77 return c[this.getWindowIdentifier(client, type, name)];
f74802c5 78 },
9e769c12
CP
79 getActiveWindow: function() {
80 return this.active;
81 },
1d42a76f
CP
82 getActiveIRCWindow: function(client) {
83 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
fd3734d4 84 return this.windows[this.getClientId(client)][this.getWindowIdentifier(client, qwebirc.ui.WINDOW_STATUS)];
1d42a76f
CP
85 } else {
86 return this.active;
87 }
88 },
9e769c12
CP
89 __setActiveWindow: function(window) {
90 this.active = window;
91 },
92 selectWindow: function(window) {
93 if(this.active)
94 this.active.deselect();
95 window.select(); /* calls setActiveWindow */
a59dc700 96 document.title = window.name + " - " + this.options.appTitle;
9e769c12 97 },
ff4befd8
CP
98 nextWindow: function(direction) {
99 if(this.windowArray.length == 0 || !this.active)
100 return;
101
102 if(!direction)
103 direction = 1;
104
105 var index = this.windowArray.indexOf(this.active);
106 if(index == -1)
107 return;
108
109 index = index + direction;
110 if(index < 0) {
111 index = this.windowArray.length - 1;
112 } else if(index >= this.windowArray.length) {
113 index = 0;
114 }
115
116 this.selectWindow(this.windowArray[index]);
117 },
118 prevWindow: function() {
119 this.nextWindow(-1);
120 },
9e769c12
CP
121 __closed: function(window) {
122 if(window.active) {
123 this.active = undefined;
124 if(this.windowArray.length == 1) {
125 this.windowArray = [];
126 } else {
127 var index = this.windowArray.indexOf(window);
6f2e4a37
CP
128 if(index == -1) {
129 return;
130 } else if(index == 0) {
9e769c12
CP
131 this.selectWindow(this.windowArray[1]);
132 } else {
133 this.selectWindow(this.windowArray[index - 1]);
134 }
9e769c12
CP
135 }
136 }
137
404cfb58 138 this.windowArray = this.windowArray.erase(window);
ffbb638d 139 delete this.windows[this.getClientId(window.client)][window.identifier];
eb9b087b 140 },
eb9b087b
CP
141 /*
142 this shouldn't be called by overriding classes!
66de775f 143 they should implement their own!
eb9b087b
CP
144 some form of user input MUST be received before an
145 IRC connection is made, else users are going to get
146 tricked into getting themselves glined
147 */
66de775f 148 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
2cad083e 149 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
9e769c12
CP
150 }
151});
381fddfd 152
e20e5a6b
CP
153qwebirc.ui.StandardUI = new Class({
154 Extends: qwebirc.ui.BaseUI,
f3d0c9f5 155 UICommands: qwebirc.ui.UI_COMMANDS,
381fddfd
CP
156 initialize: function(parentElement, windowClass, uiName, options) {
157 this.parent(parentElement, windowClass, uiName, options);
3184781b
CP
158
159 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
fb71087a 160 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
ebb21d2e
CP
161 this.customWindows = {};
162
2a802692 163 var ev;
20157c51
CP
164 if(Browser.Engine.trident) {
165 ev = "keydown";
166 } else {
167 ev = "keypress";
168 }
169 document.addEvent(ev, this.__handleHotkey.bind(this));
170 },
171 __handleHotkey: function(x) {
172 if(!x.alt || x.control) {
173 if(x.key == "backspace" || x.key == "/")
174 if(!this.getInputFocused(x))
175 new Event(x).stop();
176 return;
177 }
178 var success = false;
179 if(x.key == "a" || x.key == "A") {
180 var highestNum = 0;
181 var highestIndex = -1;
182 success = true;
183
184 new Event(x).stop();
185 for(var i=0;i<this.windowArray.length;i++) {
186 var h = this.windowArray[i].hilighted;
187 if(h > highestNum) {
188 highestIndex = i;
189 highestNum = h;
381fddfd 190 }
20157c51
CP
191 }
192 if(highestIndex > -1)
193 this.selectWindow(this.windowArray[highestIndex]);
194 } else if(x.key >= '0' && x.key <= '9') {
195 success = true;
196
197 number = x.key - '0';
198 if(number == 0)
199 number = 10
424608ac 200
20157c51
CP
201 number = number - 1;
202
203 if(number >= this.windowArray.length)
204 return;
381fddfd 205
20157c51
CP
206 this.selectWindow(this.windowArray[number]);
207 } else if(x.key == "left") {
208 this.prevWindow();
209 success = true;
210 } else if(x.key == "right") {
211 this.nextWindow();
212 success = true;
213 }
214 if(success)
215 new Event(x).stop();
216 },
217 getInputFocused: function(x) {
deebe19a
CP
218 if($$("input").indexOf(x.target) == -1 && $$("textarea").indexOf(x.target) == -1)
219 return false;
220 return true;
841a451d 221 },
8af49135
CP
222 newCustomWindow: function(name, select, type) {
223 if(!type)
e20e5a6b 224 type = qwebirc.ui.WINDOW_CUSTOM;
8af49135 225
e20e5a6b 226 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
8af49135 227 w.addEvent("close", function(w) {
f74802c5 228 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
8af49135
CP
229 }.bind(this));
230
231 if(select)
232 this.selectWindow(w);
6c19eb8f 233
8af49135
CP
234 return w;
235 },
ebb21d2e
CP
236 addCustomWindow: function(windowName, class_, cssClass, options) {
237 if(!$defined(options))
238 options = {};
239
240 if(this.customWindows[windowName]) {
241 this.selectWindow(this.customWindows[windowName]);
8af49135 242 return;
841a451d 243 }
8af49135 244
ebb21d2e
CP
245 var d = this.newCustomWindow(windowName, true);
246 this.customWindows[windowName] = d;
247
248 d.addEvent("close", function() {
249 this.customWindows[windowName] = null;
8af49135
CP
250 }.bind(this));
251
ebb21d2e 252 if(cssClass)
e1a91a8a 253 d.lines.addClass("qwebirc-" + cssClass);
ebb21d2e
CP
254
255 var ew = new class_(d.lines, options);
8af49135 256 ew.addEvent("close", function() {
ebb21d2e 257 d.close();
8af49135 258 }.bind(this));
17f40fd9
CP
259
260 d.setSubWindow(ew);
841a451d 261 },
ebb21d2e 262 embeddedWindow: function() {
2dfab0e1 263 this.addCustomWindow("Embedding wizard", qwebirc.ui.EmbedWizard, "embeddedwizard", {baseURL: this.options.baseURL});
ebb21d2e
CP
264 },
265 optionsWindow: function() {
266 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
267 },
e1a91a8a
CP
268 aboutWindow: function() {
269 this.addCustomWindow("About", qwebirc.ui.AboutPane, "aboutpane", this.uiOptions);
270 },
b35116e2
CP
271 privacyWindow: function() {
272 this.addCustomWindow("Privacy policy", qwebirc.ui.PrivacyPolicyPane, "privacypolicypane", this.uiOptions);
273 },
391f51ff
CP
274 feedbackWindow: function() {
275 this.addCustomWindow("Feedback", qwebirc.ui.FeedbackPane, "feedbackpane", this.uiOptions);
276 },
f3d0c9f5
CP
277 faqWindow: function() {
278 this.addCustomWindow("FAQ", qwebirc.ui.FAQPane, "faqpane", this.uiOptions);
279 },
8af49135
CP
280 urlDispatcher: function(name) {
281 if(name == "embedded")
925fc357 282 return ["a", this.embeddedWindow.bind(this)];
ebb21d2e
CP
283
284 if(name == "options")
285 return ["a", this.optionsWindow.bind(this)];
8af49135 286
5f2808af
CP
287 /* doesn't really belong here */
288 if(name == "whois") {
289 return ["span", function(nick) {
290 this.client.exec("/WHOIS " + nick);
291 }.bind(window)];
292 }
293
8af49135 294 return null;
3184781b
CP
295 },
296 tabComplete: function(element) {
297 this.tabCompleter.tabComplete(element);
298 },
299 resetTabComplete: function() {
300 this.tabCompleter.reset();
8af49135 301 }
381fddfd 302});
6f2e4a37 303
fb71087a 304qwebirc.ui.SoundUI = new Class({
e20e5a6b 305 Extends: qwebirc.ui.StandardUI,
fb71087a
CP
306 initialize: function(parentElement, windowClass, uiName, options) {
307 this.parent(parentElement, windowClass, uiName, options);
308
127631e0 309 this.soundInited = false;
fb71087a 310 this.soundReady = false;
fb71087a 311
127631e0 312 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
fb71087a 313 },
127631e0
CP
314 soundInit: function() {
315 if(this.soundInited)
fb71087a 316 return;
127631e0
CP
317 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
318 return;
319 this.soundInited = true;
320
321 this.soundPlayer = new qwebirc.sound.SoundPlayer();
322 this.soundPlayer.addEvent("ready", function() {
323 this.soundReady = true;
324 }.bind(this));
325 this.soundPlayer.go();
fb71087a 326 },
127631e0
CP
327 setBeepOnMention: function(value) {
328 if(value)
329 this.soundInit();
330 this.beepOnMention = value;
331 },
332 beep: function() {
333 if(!this.soundReady || !this.beepOnMention)
fb71087a 334 return;
127631e0
CP
335
336 this.soundPlayer.beep();
fb71087a
CP
337 }
338});
339
5f2808af 340qwebirc.ui.NewLoginUI = new Class({
fb71087a 341 Extends: qwebirc.ui.SoundUI,
5f2808af
CP
342 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
343 this.postInitialize();
344
345 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
346 var callback = function(args) {
347 w.close();
348 callbackfn(args);
349 };
350
351 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
352 }
353});
354
355qwebirc.ui.QuakeNetUI = new Class({
356 Extends: qwebirc.ui.NewLoginUI,
2cd9e32d
CP
357 urlDispatcher: function(name, window) {
358 if(name == "qwhois") {
7cb09779 359 return ["span", function(auth) {
2cd9e32d 360 this.client.exec("/MSG Q whois #" + auth);
925fc357
CP
361 }.bind(window)];
362 }
2cd9e32d 363 return this.parent(name);
ffbb638d
CP
364 },
365 logout: function() {
366 if(!qwebirc.auth.loggedin())
367 return;
368 if(confirm("Log out?")) {
369 for(var client in this.clients) {
370 this.clients[client].quit("Logged out");
371 };
4b9f894d
CP
372
373 /* HACK */
374 var foo = function() { document.location = "/auth?logout=1"; };
375 foo.delay(500);
ffbb638d 376 }
2cd9e32d
CP
377 }
378});