]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
Merge in default.
[irc/quakenet/qwebirc.git] / js / ui / baseui.js
1 qwebirc.ui.WINDOW_STATUS = 1;
2 qwebirc.ui.WINDOW_QUERY = 2;
3 qwebirc.ui.WINDOW_CHANNEL = 3;
4 qwebirc.ui.WINDOW_CUSTOM = 4;
5 qwebirc.ui.WINDOW_CONNECT = 5;
6 qwebirc.ui.WINDOW_MESSAGES = 6;
7 qwebirc.ui.CUSTOM_CLIENT = "custom";
8
9 qwebirc.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
153 qwebirc.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 return null;
288 },
289 tabComplete: function(element) {
290 this.tabCompleter.tabComplete(element);
291 },
292 resetTabComplete: function() {
293 this.tabCompleter.reset();
294 }
295 });
296
297 qwebirc.ui.SoundUI = new Class({
298 Extends: qwebirc.ui.StandardUI,
299 initialize: function(parentElement, windowClass, uiName, options) {
300 this.parent(parentElement, windowClass, uiName, options);
301
302 this.soundInited = false;
303 this.soundReady = false;
304
305 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
306 },
307 soundInit: function() {
308 if(this.soundInited)
309 return;
310 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
311 return;
312 this.soundInited = true;
313
314 this.soundPlayer = new qwebirc.sound.SoundPlayer();
315 this.soundPlayer.addEvent("ready", function() {
316 this.soundReady = true;
317 }.bind(this));
318 this.soundPlayer.go();
319 },
320 setBeepOnMention: function(value) {
321 if(value)
322 this.soundInit();
323 this.beepOnMention = value;
324 },
325 beep: function() {
326 if(!this.soundReady || !this.beepOnMention)
327 return;
328
329 this.soundPlayer.beep();
330 }
331 });
332
333 qwebirc.ui.QuakeNetUI = new Class({
334 Extends: qwebirc.ui.SoundUI,
335 urlDispatcher: function(name, window) {
336 if(name == "qwhois") {
337 return ["span", function(auth) {
338 this.client.exec("/MSG Q whois #" + auth);
339 }.bind(window)];
340 }
341 if(name == "whois") {
342 return ["span", function(nick) {
343 this.client.exec("/WHOIS " + nick);
344 }.bind(window)];
345 }
346 return this.parent(name);
347 },
348 logout: function() {
349 if(!qwebirc.auth.loggedin())
350 return;
351 if(confirm("Log out?")) {
352 for(var client in this.clients) {
353 this.clients[client].quit("Logged out");
354 };
355
356 /* HACK */
357 var foo = function() { document.location = "/auth?logout=1"; };
358 foo.delay(500);
359 }
360 }
361 });
362
363 qwebirc.ui.NewLoginUI = new Class({
364 Extends: qwebirc.ui.QuakeNetUI,
365 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
366 this.postInitialize();
367
368 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
369 var callback = function(args) {
370 w.close();
371 callbackfn(args);
372 };
373
374 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
375 }
376 });