]> jfr.im git - irc/quakenet/qwebirc.git/blame_incremental - js/ui/baseui.js
Inheritance tidying + tidying!
[irc/quakenet/qwebirc.git] / js / ui / baseui.js
... / ...
CommitLineData
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;
6qwebirc.ui.WINDOW_MESSAGES = 6;
7qwebirc.ui.CUSTOM_CLIENT = "custom";
8
9qwebirc.ui.BaseUI = new Class({
10 Implements: [Events],
11 initialize: function(parentElement, windowClass, uiName, options) {
12 this.options = options;
13
14 this.windows = {};
15 this.clients = {};
16 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
17 this.windowArray = [];
18 this.windowClass = windowClass;
19 this.parentElement = parentElement;
20 this.parentElement.addClass("qwebirc");
21 this.parentElement.addClass("qwebirc-" + uiName);
22 this.firstClient = false;
23 this.commandhistory = new qwebirc.irc.CommandHistory();
24 this.clientId = 0;
25 },
26 newClient: function(client) {
27 client.id = this.clientId++;
28 client.hilightController = new qwebirc.ui.HilightController(client);
29
30 this.windows[client.id] = {}
31 this.clients[client.id] = client;
32 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
33 this.selectWindow(w);
34 if(!this.firstClient) {
35 this.firstClient = true;
36 w.addLine("", "qwebirc v" + qwebirc.VERSION);
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.");
40 }
41 return w;
42 },
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 },
50 getWindowIdentifier: function(client, type, name) {
51 if(type == qwebirc.ui.WINDOW_MESSAGES)
52 return "-M";
53 if(type == qwebirc.ui.WINDOW_STATUS)
54 return "";
55
56 if(client == qwebirc.ui.CUSTOM_CLIENT) /* HACK */
57 return "_" + name;
58
59 return "_" + client.toIRCLower(name);
60 },
61 newWindow: function(client, type, name) {
62 var w = this.getWindow(client, type, name);
63 if($defined(w))
64 return w;
65
66 var wId = this.getWindowIdentifier(client, type, name);
67 var w = this.windows[this.getClientId(client)][wId] = new this.windowClass(this, client, type, name, wId);
68 this.windowArray.push(w);
69
70 return w;
71 },
72 getWindow: function(client, type, name) {
73 var c = this.windows[this.getClientId(client)];
74 if(!$defined(c))
75 return null;
76
77 return c[this.getWindowIdentifier(client, type, name)];
78 },
79 getActiveWindow: function() {
80 return this.active;
81 },
82 getActiveIRCWindow: function(client) {
83 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
84 return this.windows[this.getClientId(client)][this.getWindowIdentifier(client, qwebirc.ui.WINDOW_STATUS)];
85 } else {
86 return this.active;
87 }
88 },
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 */
96 document.title = window.name + " - " + this.options.appTitle;
97 },
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 },
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);
128 if(index == -1) {
129 return;
130 } else if(index == 0) {
131 this.selectWindow(this.windowArray[1]);
132 } else {
133 this.selectWindow(this.windowArray[index - 1]);
134 }
135 }
136 }
137
138 this.windowArray = this.windowArray.erase(window);
139 delete this.windows[this.getClientId(window.client)][window.identifier];
140 },
141 /*
142 this shouldn't be called by overriding classes!
143 they should implement their own!
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 */
148 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
149 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
150 }
151});
152
153qwebirc.ui.StandardUI = new Class({
154 Extends: qwebirc.ui.BaseUI,
155 UICommands: qwebirc.ui.UI_COMMANDS,
156 initialize: function(parentElement, windowClass, uiName, options) {
157 this.parent(parentElement, windowClass, uiName, options);
158
159 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
160 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
161 this.customWindows = {};
162
163 var ev;
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;
190 }
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
200
201 number = number - 1;
202
203 if(number >= this.windowArray.length)
204 return;
205
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) {
218 if($$("input").indexOf(x.target) == -1 && $$("textarea").indexOf(x.target) == -1)
219 return false;
220 return true;
221 },
222 newCustomWindow: function(name, select, type) {
223 if(!type)
224 type = qwebirc.ui.WINDOW_CUSTOM;
225
226 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
227 w.addEvent("close", function(w) {
228 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
229 }.bind(this));
230
231 if(select)
232 this.selectWindow(w);
233
234 return w;
235 },
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]);
242 return;
243 }
244
245 var d = this.newCustomWindow(windowName, true);
246 this.customWindows[windowName] = d;
247
248 d.addEvent("close", function() {
249 this.customWindows[windowName] = null;
250 }.bind(this));
251
252 if(cssClass)
253 d.lines.addClass("qwebirc-" + cssClass);
254
255 var ew = new class_(d.lines, options);
256 ew.addEvent("close", function() {
257 d.close();
258 }.bind(this));
259
260 d.setSubWindow(ew);
261 },
262 embeddedWindow: function() {
263 this.addCustomWindow("Embedding wizard", qwebirc.ui.EmbedWizard, "embeddedwizard", {baseURL: this.options.baseURL});
264 },
265 optionsWindow: function() {
266 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
267 },
268 aboutWindow: function() {
269 this.addCustomWindow("About", qwebirc.ui.AboutPane, "aboutpane", this.uiOptions);
270 },
271 privacyWindow: function() {
272 this.addCustomWindow("Privacy policy", qwebirc.ui.PrivacyPolicyPane, "privacypolicypane", this.uiOptions);
273 },
274 feedbackWindow: function() {
275 this.addCustomWindow("Feedback", qwebirc.ui.FeedbackPane, "feedbackpane", this.uiOptions);
276 },
277 faqWindow: function() {
278 this.addCustomWindow("FAQ", qwebirc.ui.FAQPane, "faqpane", this.uiOptions);
279 },
280 urlDispatcher: function(name) {
281 if(name == "embedded")
282 return ["a", this.embeddedWindow.bind(this)];
283
284 if(name == "options")
285 return ["a", this.optionsWindow.bind(this)];
286
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
294 return null;
295 },
296 tabComplete: function(element) {
297 this.tabCompleter.tabComplete(element);
298 },
299 resetTabComplete: function() {
300 this.tabCompleter.reset();
301 }
302});
303
304qwebirc.ui.SoundUI = new Class({
305 Extends: qwebirc.ui.StandardUI,
306 initialize: function(parentElement, windowClass, uiName, options) {
307 this.parent(parentElement, windowClass, uiName, options);
308
309 this.soundInited = false;
310 this.soundReady = false;
311
312 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
313 },
314 soundInit: function() {
315 if(this.soundInited)
316 return;
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();
326 },
327 setBeepOnMention: function(value) {
328 if(value)
329 this.soundInit();
330 this.beepOnMention = value;
331 },
332 beep: function() {
333 if(!this.soundReady || !this.beepOnMention)
334 return;
335
336 this.soundPlayer.beep();
337 }
338});
339
340qwebirc.ui.NewLoginUI = new Class({
341 Extends: qwebirc.ui.SoundUI,
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,
357 urlDispatcher: function(name, window) {
358 if(name == "qwhois") {
359 return ["span", function(auth) {
360 this.client.exec("/MSG Q whois #" + auth);
361 }.bind(window)];
362 }
363 return this.parent(name);
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 };
372
373 /* HACK */
374 var foo = function() { document.location = "/auth?logout=1"; };
375 foo.delay(500);
376 }
377 }
378});