]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
Fix IE7.
[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, Options],
11 options: {
12 appTitle: "QuakeNet Web IRC",
13 singleWindow: true
14 },
15 initialize: function(parentElement, windowClass, uiName, options) {
16 this.setOptions(options);
17
18 this.windows = {};
19 this.clients = {};
20 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
21 this.windowArray = [];
22 this.windowClass = windowClass;
23 this.parentElement = parentElement;
24 this.parentElement.addClass("qwebirc");
25 this.parentElement.addClass("qwebirc-" + uiName);
26 this.firstClient = false;
27 this.commandhistory = new qwebirc.irc.CommandHistory();
28 this.clientId = 0;
29 },
30 newClient: function(client) {
31 client.id = this.clientId++;
32 client.hilightController = new qwebirc.ui.HilightController(client);
33
34 this.windows[client.id] = {}
35 this.clients[client.id] = client;
36 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
37 this.selectWindow(w);
38 if(!this.firstClient) {
39 this.firstClient = true;
40 w.addLine("", "qwebirc v" + qwebirc.VERSION);
41 w.addLine("", "Copyright (C) 2008 Chris Porter. All rights reserved.");
42 w.addLine("", "http://webchat.quakenet.org/");
43 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
44 }
45 return w;
46 },
47 getClientId: function(client) {
48 if(client == qwebirc.ui.CUSTOM_CLIENT) {
49 return qwebirc.ui.CUSTOM_CLIENT;
50 } else {
51 return client.id;
52 }
53 },
54 getWindowIdentifier: function(type, name) {
55 if(type == qwebirc.ui.WINDOW_MESSAGES)
56 return "-M";
57 if(type == qwebirc.ui.WINDOW_STATUS)
58 return "";
59 return "_" + name.toIRCLower();
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(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(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(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);
150 }
151 });
152
153 qwebirc.ui.StandardUI = new Class({
154 Extends: qwebirc.ui.BaseUI,
155 initialize: function(parentElement, windowClass, uiName, options) {
156 this.parent(parentElement, windowClass, uiName, options);
157
158 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
159 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
160 this.customWindows = {};
161
162 window.addEvent("keydown", function(x) {
163 if(!x.alt || x.control)
164 return;
165
166 var success = false;
167 if(x.key == "a" || x.key == "A") {
168 var highestNum = 0;
169 var highestIndex = -1;
170 success = true;
171
172 new Event(x).stop();
173 for(var i=0;i<this.windowArray.length;i++) {
174 var h = this.windowArray[i].hilighted;
175 if(h > highestNum) {
176 highestIndex = i;
177 highestNum = h;
178 }
179 }
180 if(highestIndex > -1)
181 this.selectWindow(this.windowArray[highestIndex]);
182 } else if(x.key >= '0' && x.key <= '9') {
183 success = true;
184
185 number = x.key - '0';
186 if(number == 0)
187 number = 10
188
189 number = number - 1;
190
191 if(number >= this.windowArray.length)
192 return;
193
194 this.selectWindow(this.windowArray[number]);
195 } else if(x.key == "left") {
196 this.prevWindow();
197 success = true;
198 } else if(x.key == "right") {
199 this.nextWindow();
200 success = true;
201 }
202 if(success)
203 new Event(x).stop();
204 }.bind(this));
205 },
206 newCustomWindow: function(name, select, type) {
207 if(!type)
208 type = qwebirc.ui.WINDOW_CUSTOM;
209
210 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
211 w.addEvent("close", function(w) {
212 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
213 }.bind(this));
214
215 if(select)
216 this.selectWindow(w);
217
218 return w;
219 },
220 addCustomWindow: function(windowName, class_, cssClass, options) {
221 if(!$defined(options))
222 options = {};
223
224 if(this.customWindows[windowName]) {
225 this.selectWindow(this.customWindows[windowName]);
226 return;
227 }
228
229 var d = this.newCustomWindow(windowName, true);
230 this.customWindows[windowName] = d;
231
232 d.addEvent("close", function() {
233 this.customWindows[windowName] = null;
234 }.bind(this));
235
236 if(cssClass)
237 d.lines.addClass(cssClass);
238
239 var ew = new class_(d.lines, options);
240 ew.addEvent("close", function() {
241 d.close();
242 }.bind(this));
243 },
244 embeddedWindow: function() {
245 this.addCustomWindow("Embedded Wizard", qwebirc.ui.EmbedWizard, "embeddedwizard");
246 },
247 optionsWindow: function() {
248 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
249 },
250 urlDispatcher: function(name) {
251 if(name == "embedded")
252 return ["a", this.embeddedWindow.bind(this)];
253
254 if(name == "options")
255 return ["a", this.optionsWindow.bind(this)];
256
257 return null;
258 },
259 tabComplete: function(element) {
260 this.tabCompleter.tabComplete(element);
261 },
262 resetTabComplete: function() {
263 this.tabCompleter.reset();
264 }
265 });
266
267 qwebirc.ui.SoundUI = new Class({
268 Extends: qwebirc.ui.StandardUI,
269 initialize: function(parentElement, windowClass, uiName, options) {
270 this.parent(parentElement, windowClass, uiName, options);
271
272 this.soundInited = false;
273 this.soundReady = false;
274
275 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
276 },
277 soundInit: function() {
278 if(this.soundInited)
279 return;
280 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
281 return;
282 this.soundInited = true;
283
284 this.soundPlayer = new qwebirc.sound.SoundPlayer();
285 this.soundPlayer.addEvent("ready", function() {
286 this.soundReady = true;
287 }.bind(this));
288 this.soundPlayer.go();
289 },
290 setBeepOnMention: function(value) {
291 if(value)
292 this.soundInit();
293 this.beepOnMention = value;
294 },
295 beep: function() {
296 if(!this.soundReady || !this.beepOnMention)
297 return;
298
299 this.soundPlayer.beep();
300 }
301 });
302
303 qwebirc.ui.QuakeNetUI = new Class({
304 Extends: qwebirc.ui.SoundUI,
305 urlDispatcher: function(name, window) {
306 if(name == "qwhois") {
307 return ["span", function(auth) {
308 this.client.exec("/MSG Q whois #" + auth);
309 }.bind(window)];
310 }
311 if(name == "whois") {
312 return ["span", function(nick) {
313 this.client.exec("/WHOIS " + nick);
314 }.bind(window)];
315 }
316 return this.parent(name);
317 },
318 logout: function() {
319 if(!qwebirc.auth.loggedin())
320 return;
321 if(confirm("Log out?")) {
322 for(var client in this.clients) {
323 this.clients[client].quit("Logged out");
324 };
325 document.location = "/auth?logout=1";
326 }
327 }
328 });
329
330 qwebirc.ui.NewLoginUI = new Class({
331 Extends: qwebirc.ui.QuakeNetUI,
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);
342 }
343 });