]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
IE fixes.
[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.CUSTOM_CLIENT = "custom";
7
8 qwebirc.ui.BaseUI = new Class({
9 Implements: [Events, Options],
10 options: {
11 appTitle: "QuakeNet Web IRC",
12 singleWindow: true
13 },
14 initialize: function(parentElement, windowClass, uiName, options) {
15 this.setOptions(options);
16
17 this.windows = {};
18 this.clients = {};
19 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
20 this.windowArray = [];
21 this.windowClass = windowClass;
22 this.parentElement = parentElement;
23 this.parentElement.addClass("qwebirc");
24 this.parentElement.addClass("qwebirc-" + uiName);
25 this.firstClient = false;
26 this.commandhistory = new qwebirc.irc.CommandHistory();
27 this.clientId = 0;
28 },
29 newClient: function(client) {
30 client.id = this.clientId++;
31 client.hilightController = new qwebirc.ui.HilightController(client);
32
33 this.windows[client.id] = {}
34 this.clients[client.id] = client;
35 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
36 this.selectWindow(w);
37 if(!this.firstClient) {
38 this.firstClient = true;
39 w.addLine("", "qwebirc v" + qwebirc.VERSION);
40 w.addLine("", "Copyright (C) 2008 Chris Porter. All rights reserved.");
41 w.addLine("", "http://webchat.quakenet.org/");
42 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
43 }
44 return w;
45 },
46 getClientId: function(client) {
47 if(client == qwebirc.ui.CUSTOM_CLIENT) {
48 return qwebirc.ui.CUSTOM_CLIENT;
49 } else {
50 return client.id;
51 }
52 },
53 newWindow: function(client, type, name) {
54 var identifier = name;
55 if(type == qwebirc.ui.WINDOW_STATUS)
56 identifier = "";
57
58 var w = this.windows[this.getClientId(client)][identifier] = new this.windowClass(this, client, type, name, identifier);
59 this.windowArray.push(w);
60
61 return w;
62 },
63 getActiveWindow: function() {
64 return this.active;
65 },
66 getActiveIRCWindow: function(client) {
67 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
68 return this.windows[this.getClientId(client)][""];
69 } else {
70 return this.active;
71 }
72 },
73 __setActiveWindow: function(window) {
74 this.active = window;
75 },
76 selectWindow: function(window) {
77 if(this.active)
78 this.active.deselect();
79 window.select(); /* calls setActiveWindow */
80 document.title = window.name + " - " + this.options.appTitle;
81 },
82 nextWindow: function(direction) {
83 if(this.windowArray.length == 0 || !this.active)
84 return;
85
86 if(!direction)
87 direction = 1;
88
89 var index = this.windowArray.indexOf(this.active);
90 if(index == -1)
91 return;
92
93 index = index + direction;
94 if(index < 0) {
95 index = this.windowArray.length - 1;
96 } else if(index >= this.windowArray.length) {
97 index = 0;
98 }
99
100 this.selectWindow(this.windowArray[index]);
101 },
102 prevWindow: function() {
103 this.nextWindow(-1);
104 },
105 __closed: function(window) {
106 if(window.active) {
107 this.active = undefined;
108 if(this.windowArray.length == 1) {
109 this.windowArray = [];
110 } else {
111 var index = this.windowArray.indexOf(window);
112 if(index == -1) {
113 return;
114 } else if(index == 0) {
115 this.selectWindow(this.windowArray[1]);
116 } else {
117 this.selectWindow(this.windowArray[index - 1]);
118 }
119 }
120 }
121
122 this.windowArray = this.windowArray.erase(window);
123 delete this.windows[this.getClientId(window.client)][window.identifier];
124 },
125 /*
126 this shouldn't be called by overriding classes!
127 they should implement their own!
128 some form of user input MUST be received before an
129 IRC connection is made, else users are going to get
130 tricked into getting themselves glined
131 */
132 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
133 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick);
134 }
135 });
136
137 qwebirc.ui.StandardUI = new Class({
138 Extends: qwebirc.ui.BaseUI,
139 initialize: function(parentElement, windowClass, uiName, options) {
140 this.parent(parentElement, windowClass, uiName, options);
141
142 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
143 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
144 this.customWindows = {};
145
146 window.addEvent("keydown", function(x) {
147 if(!x.alt || x.control)
148 return;
149
150 var success = false;
151 if(x.key == "a" || x.key == "A") {
152 var highestNum = 0;
153 var highestIndex = -1;
154 success = true;
155
156 new Event(x).stop();
157 for(var i=0;i<this.windowArray.length;i++) {
158 var h = this.windowArray[i].hilighted;
159 if(h > highestNum) {
160 highestIndex = i;
161 highestNum = h;
162 }
163 }
164 if(highestIndex > -1)
165 this.selectWindow(this.windowArray[highestIndex]);
166 } else if(x.key >= '0' && x.key <= '9') {
167 success = true;
168
169 number = x.key - '0';
170 if(number == 0)
171 number = 10
172
173 number = number - 1;
174
175 if(number >= this.windowArray.length)
176 return;
177
178 this.selectWindow(this.windowArray[number]);
179 } else if(x.key == "left") {
180 this.prevWindow();
181 success = true;
182 } else if(x.key == "right") {
183 this.nextWindow();
184 success = true;
185 }
186 if(success)
187 new Event(x).stop();
188 }.bind(this));
189 },
190 newCustomWindow: function(name, select, type) {
191 if(!type)
192 type = qwebirc.ui.WINDOW_CUSTOM;
193
194 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
195 w.addEvent("close", function(w) {
196 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][name];
197 }.bind(this));
198
199 if(select)
200 this.selectWindow(w);
201
202 return w;
203 },
204 addCustomWindow: function(windowName, class_, cssClass, options) {
205 if(!$defined(options))
206 options = {};
207
208 if(this.customWindows[windowName]) {
209 this.selectWindow(this.customWindows[windowName]);
210 return;
211 }
212
213 var d = this.newCustomWindow(windowName, true);
214 this.customWindows[windowName] = d;
215
216 d.addEvent("close", function() {
217 this.customWindows[windowName] = null;
218 }.bind(this));
219
220 if(cssClass)
221 d.lines.addClass(cssClass);
222
223 var ew = new class_(d.lines, options);
224 ew.addEvent("close", function() {
225 d.close();
226 }.bind(this));
227 },
228 embeddedWindow: function() {
229 this.addCustomWindow("Embedded Wizard", qwebirc.ui.EmbedWizard, "embeddedwizard");
230 },
231 optionsWindow: function() {
232 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
233 },
234 urlDispatcher: function(name) {
235 if(name == "embedded")
236 return ["a", this.embeddedWindow.bind(this)];
237
238 if(name == "options")
239 return ["a", this.optionsWindow.bind(this)];
240
241 return null;
242 },
243 tabComplete: function(element) {
244 this.tabCompleter.tabComplete(element);
245 },
246 resetTabComplete: function() {
247 this.tabCompleter.reset();
248 }
249 });
250
251 qwebirc.ui.SoundUI = new Class({
252 Extends: qwebirc.ui.StandardUI,
253 initialize: function(parentElement, windowClass, uiName, options) {
254 this.parent(parentElement, windowClass, uiName, options);
255
256 this.soundInited = false;
257 this.soundReady = false;
258
259 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
260 },
261 soundInit: function() {
262 if(this.soundInited)
263 return;
264 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
265 return;
266 this.soundInited = true;
267
268 this.soundPlayer = new qwebirc.sound.SoundPlayer();
269 this.soundPlayer.addEvent("ready", function() {
270 this.soundReady = true;
271 }.bind(this));
272 this.soundPlayer.go();
273 },
274 setBeepOnMention: function(value) {
275 if(value)
276 this.soundInit();
277 this.beepOnMention = value;
278 },
279 beep: function() {
280 if(!this.soundReady || !this.beepOnMention)
281 return;
282
283 this.soundPlayer.beep();
284 }
285 });
286
287 qwebirc.ui.QuakeNetUI = new Class({
288 Extends: qwebirc.ui.SoundUI,
289 urlDispatcher: function(name, window) {
290 if(name == "qwhois") {
291 return ["span", function(auth) {
292 this.client.exec("/MSG Q whois #" + auth);
293 }.bind(window)];
294 }
295 if(name == "whois") {
296 return ["span", function(nick) {
297 this.client.exec("/WHOIS " + nick);
298 }.bind(window)];
299 }
300 return this.parent(name);
301 },
302 logout: function() {
303 if(!qwebirc.auth.loggedin())
304 return;
305 if(confirm("Log out?")) {
306 for(var client in this.clients) {
307 this.clients[client].quit("Logged out");
308 };
309 document.location = "/auth?logout=1";
310 }
311 }
312 });
313
314 qwebirc.ui.NewLoginUI = new Class({
315 Extends: qwebirc.ui.QuakeNetUI,
316 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
317 this.postInitialize();
318
319 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
320 var callback = function(args) {
321 w.close();
322 callbackfn(args);
323 };
324
325 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick);
326 }
327 });