]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
IE7 fix (excess comma at end of class).
[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 this.updateTitle(window.name + " - " + this.options.appTitle);
97 },
98 updateTitle: function(text) {
99 document.title = text;
100 },
101 nextWindow: function(direction) {
102 if(this.windowArray.length == 0 || !this.active)
103 return;
104
105 if(!direction)
106 direction = 1;
107
108 var index = this.windowArray.indexOf(this.active);
109 if(index == -1)
110 return;
111
112 index = index + direction;
113 if(index < 0) {
114 index = this.windowArray.length - 1;
115 } else if(index >= this.windowArray.length) {
116 index = 0;
117 }
118
119 this.selectWindow(this.windowArray[index]);
120 },
121 prevWindow: function() {
122 this.nextWindow(-1);
123 },
124 __closed: function(window) {
125 if(window.active) {
126 this.active = undefined;
127 if(this.windowArray.length == 1) {
128 this.windowArray = [];
129 } else {
130 var index = this.windowArray.indexOf(window);
131 if(index == -1) {
132 return;
133 } else if(index == 0) {
134 this.selectWindow(this.windowArray[1]);
135 } else {
136 this.selectWindow(this.windowArray[index - 1]);
137 }
138 }
139 }
140
141 this.windowArray = this.windowArray.erase(window);
142 delete this.windows[this.getClientId(window.client)][window.identifier];
143 },
144 /*
145 this shouldn't be called by overriding classes!
146 they should implement their own!
147 some form of user input MUST be received before an
148 IRC connection is made, else users are going to get
149 tricked into getting themselves glined
150 */
151 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
152 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
153 }
154 });
155
156 qwebirc.ui.StandardUI = new Class({
157 Extends: qwebirc.ui.BaseUI,
158 UICommands: qwebirc.ui.UI_COMMANDS,
159 initialize: function(parentElement, windowClass, uiName, options) {
160 this.parent(parentElement, windowClass, uiName, options);
161
162 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
163 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
164 this.customWindows = {};
165
166 var ev;
167 if(Browser.Engine.trident) {
168 ev = "keydown";
169 } else {
170 ev = "keypress";
171 }
172 document.addEvent(ev, this.__handleHotkey.bind(this));
173 },
174 __handleHotkey: function(x) {
175 if(!x.alt || x.control) {
176 if(x.key == "backspace" || x.key == "/")
177 if(!this.getInputFocused(x))
178 new Event(x).stop();
179 return;
180 }
181 var success = false;
182 if(x.key == "a" || x.key == "A") {
183 var highestNum = 0;
184 var highestIndex = -1;
185 success = true;
186
187 new Event(x).stop();
188 for(var i=0;i<this.windowArray.length;i++) {
189 var h = this.windowArray[i].hilighted;
190 if(h > highestNum) {
191 highestIndex = i;
192 highestNum = h;
193 }
194 }
195 if(highestIndex > -1)
196 this.selectWindow(this.windowArray[highestIndex]);
197 } else if(x.key >= '0' && x.key <= '9') {
198 success = true;
199
200 number = x.key - '0';
201 if(number == 0)
202 number = 10
203
204 number = number - 1;
205
206 if(number >= this.windowArray.length)
207 return;
208
209 this.selectWindow(this.windowArray[number]);
210 } else if(x.key == "left") {
211 this.prevWindow();
212 success = true;
213 } else if(x.key == "right") {
214 this.nextWindow();
215 success = true;
216 }
217 if(success)
218 new Event(x).stop();
219 },
220 getInputFocused: function(x) {
221 if($$("input").indexOf(x.target) == -1 && $$("textarea").indexOf(x.target) == -1)
222 return false;
223 return true;
224 },
225 newCustomWindow: function(name, select, type) {
226 if(!type)
227 type = qwebirc.ui.WINDOW_CUSTOM;
228
229 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
230 w.addEvent("close", function(w) {
231 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
232 }.bind(this));
233
234 if(select)
235 this.selectWindow(w);
236
237 return w;
238 },
239 addCustomWindow: function(windowName, class_, cssClass, options) {
240 if(!$defined(options))
241 options = {};
242
243 if(this.customWindows[windowName]) {
244 this.selectWindow(this.customWindows[windowName]);
245 return;
246 }
247
248 var d = this.newCustomWindow(windowName, true);
249 this.customWindows[windowName] = d;
250
251 d.addEvent("close", function() {
252 this.customWindows[windowName] = null;
253 }.bind(this));
254
255 if(cssClass)
256 d.lines.addClass("qwebirc-" + cssClass);
257
258 var ew = new class_(d.lines, options);
259 ew.addEvent("close", function() {
260 d.close();
261 }.bind(this));
262
263 d.setSubWindow(ew);
264 },
265 embeddedWindow: function() {
266 this.addCustomWindow("Embedding wizard", qwebirc.ui.EmbedWizard, "embeddedwizard", {baseURL: this.options.baseURL});
267 },
268 optionsWindow: function() {
269 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
270 },
271 aboutWindow: function() {
272 this.addCustomWindow("About", qwebirc.ui.AboutPane, "aboutpane", this.uiOptions);
273 },
274 privacyWindow: function() {
275 this.addCustomWindow("Privacy policy", qwebirc.ui.PrivacyPolicyPane, "privacypolicypane", this.uiOptions);
276 },
277 feedbackWindow: function() {
278 this.addCustomWindow("Feedback", qwebirc.ui.FeedbackPane, "feedbackpane", this.uiOptions);
279 },
280 faqWindow: function() {
281 this.addCustomWindow("FAQ", qwebirc.ui.FAQPane, "faqpane", this.uiOptions);
282 },
283 urlDispatcher: function(name, window) {
284 if(name == "embedded")
285 return ["a", this.embeddedWindow.bind(this)];
286
287 if(name == "options")
288 return ["a", this.optionsWindow.bind(this)];
289
290 /* doesn't really belong here */
291 if(name == "whois") {
292 return ["span", function(nick) {
293 this.client.exec("/WHOIS " + nick);
294 }.bind(window)];
295 }
296
297 return null;
298 },
299 tabComplete: function(element) {
300 this.tabCompleter.tabComplete(element);
301 },
302 resetTabComplete: function() {
303 this.tabCompleter.reset();
304 }
305 });
306
307 qwebirc.ui.NotificationUI = new Class({
308 Extends: qwebirc.ui.StandardUI,
309 initialize: function(parentElement, windowClass, uiName, options) {
310 this.parent(parentElement, windowClass, uiName, options);
311
312 this.__beeper = new qwebirc.ui.Beeper(this.uiOptions);
313 this.__flasher = new qwebirc.ui.Flasher(this.uiOptions);
314
315 this.beep = this.__beeper.beep.bind(this.__beeper);
316
317 this.flash = this.__flasher.flash.bind(this.__flasher);
318 this.cancelFlash = this.__flasher.cancelFlash.bind(this.__flasher);
319 },
320 setBeepOnMention: function(value) {
321 if(value)
322 this.__beeper.soundInit();
323 },
324 updateTitle: function(text) {
325 if(this.__flasher.updateTitle(text))
326 this.parent(text);
327 }
328 });
329
330 qwebirc.ui.NewLoginUI = new Class({
331 Extends: qwebirc.ui.NotificationUI,
332 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
333 this.postInitialize();
334
335 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
336 var callback = function(args) {
337 w.close();
338 callbackfn(args);
339 };
340
341 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
342 }
343 });
344
345 qwebirc.ui.QuakeNetUI = new Class({
346 Extends: qwebirc.ui.NewLoginUI,
347 urlDispatcher: function(name, window) {
348 if(name == "qwhois") {
349 return ["span", function(auth) {
350 this.client.exec("/MSG Q whois #" + auth);
351 }.bind(window)];
352 }
353 return this.parent(name, window);
354 },
355 logout: function() {
356 if(!qwebirc.auth.loggedin())
357 return;
358 if(confirm("Log out?")) {
359 for(var client in this.clients) {
360 this.clients[client].quit("Logged out");
361 };
362
363 /* HACK */
364 var foo = function() { document.location = "/auth?logout=1"; };
365 foo.delay(500);
366 }
367 }
368 });
369
370 qwebirc.ui.RootUI = qwebirc.ui.QuakeNetUI;