]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/baseui.js
Fix IE7.
[irc/quakenet/qwebirc.git] / js / ui / baseui.js
CommitLineData
e20e5a6b
CP
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;
f74802c5 6qwebirc.ui.WINDOW_MESSAGES = 6;
e20e5a6b 7qwebirc.ui.CUSTOM_CLIENT = "custom";
9e769c12 8
e20e5a6b 9qwebirc.ui.BaseUI = new Class({
a59dc700
CP
10 Implements: [Events, Options],
11 options: {
7c633700
CP
12 appTitle: "QuakeNet Web IRC",
13 singleWindow: true
a59dc700
CP
14 },
15 initialize: function(parentElement, windowClass, uiName, options) {
16 this.setOptions(options);
17
9e769c12 18 this.windows = {};
ffbb638d 19 this.clients = {};
e20e5a6b 20 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
9e769c12
CP
21 this.windowArray = [];
22 this.windowClass = windowClass;
23 this.parentElement = parentElement;
24 this.parentElement.addClass("qwebirc");
25 this.parentElement.addClass("qwebirc-" + uiName);
e8db8558 26 this.firstClient = false;
e20e5a6b 27 this.commandhistory = new qwebirc.irc.CommandHistory();
ffbb638d 28 this.clientId = 0;
9e769c12
CP
29 },
30 newClient: function(client) {
ffbb638d 31 client.id = this.clientId++;
96f28062
CP
32 client.hilightController = new qwebirc.ui.HilightController(client);
33
ffbb638d
CP
34 this.windows[client.id] = {}
35 this.clients[client.id] = client;
e20e5a6b 36 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
9e769c12 37 this.selectWindow(w);
e8db8558
CP
38 if(!this.firstClient) {
39 this.firstClient = true;
e20e5a6b 40 w.addLine("", "qwebirc v" + qwebirc.VERSION);
4094890f 41 w.addLine("", "Copyright (C) 2008 Chris Porter. All rights reserved.");
e8db8558 42 w.addLine("", "http://webchat.quakenet.org/");
4094890f 43 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
e8db8558 44 }
9e769c12
CP
45 return w;
46 },
ffbb638d
CP
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 },
f74802c5
CP
54 getWindowIdentifier: function(type, name) {
55 if(type == qwebirc.ui.WINDOW_MESSAGES)
56 return "-M";
e20e5a6b 57 if(type == qwebirc.ui.WINDOW_STATUS)
f74802c5 58 return "";
26c81b65 59 return "_" + name.toIRCLower();
f74802c5
CP
60 },
61 newWindow: function(client, type, name) {
62 var w = this.getWindow(client, type, name);
63 if($defined(w))
64 return w;
9e769c12 65
f74802c5
CP
66 var wId = this.getWindowIdentifier(type, name);
67 var w = this.windows[this.getClientId(client)][wId] = new this.windowClass(this, client, type, name, wId);
9e769c12
CP
68 this.windowArray.push(w);
69
70 return w;
71 },
f74802c5
CP
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 },
9e769c12
CP
79 getActiveWindow: function() {
80 return this.active;
81 },
1d42a76f
CP
82 getActiveIRCWindow: function(client) {
83 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
f74802c5 84 return this.windows[this.getClientId(client)][this.getWindowIdentifier(qwebirc.ui.WINDOW_STATUS)];
1d42a76f
CP
85 } else {
86 return this.active;
87 }
88 },
9e769c12
CP
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 */
a59dc700 96 document.title = window.name + " - " + this.options.appTitle;
9e769c12 97 },
ff4befd8
CP
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 },
9e769c12
CP
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);
6f2e4a37
CP
128 if(index == -1) {
129 return;
130 } else if(index == 0) {
9e769c12
CP
131 this.selectWindow(this.windowArray[1]);
132 } else {
133 this.selectWindow(this.windowArray[index - 1]);
134 }
9e769c12
CP
135 }
136 }
137
404cfb58 138 this.windowArray = this.windowArray.erase(window);
ffbb638d 139 delete this.windows[this.getClientId(window.client)][window.identifier];
eb9b087b 140 },
eb9b087b
CP
141 /*
142 this shouldn't be called by overriding classes!
66de775f 143 they should implement their own!
eb9b087b
CP
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 */
66de775f 148 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
e20e5a6b 149 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick);
9e769c12
CP
150 }
151});
381fddfd 152
e20e5a6b
CP
153qwebirc.ui.StandardUI = new Class({
154 Extends: qwebirc.ui.BaseUI,
381fddfd
CP
155 initialize: function(parentElement, windowClass, uiName, options) {
156 this.parent(parentElement, windowClass, uiName, options);
3184781b
CP
157
158 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
fb71087a 159 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
ebb21d2e
CP
160 this.customWindows = {};
161
381fddfd 162 window.addEvent("keydown", function(x) {
bc4d9f4a 163 if(!x.alt || x.control)
381fddfd
CP
164 return;
165
cf00dc43 166 var success = false;
381fddfd 167 if(x.key == "a" || x.key == "A") {
ffe442b0
CP
168 var highestNum = 0;
169 var highestIndex = -1;
cf00dc43
CP
170 success = true;
171
424608ac 172 new Event(x).stop();
381fddfd 173 for(var i=0;i<this.windowArray.length;i++) {
ffe442b0
CP
174 var h = this.windowArray[i].hilighted;
175 if(h > highestNum) {
176 highestIndex = i;
177 highestNum = h;
381fddfd
CP
178 }
179 }
ffe442b0
CP
180 if(highestIndex > -1)
181 this.selectWindow(this.windowArray[highestIndex]);
381fddfd 182 } else if(x.key >= '0' && x.key <= '9') {
cf00dc43 183 success = true;
424608ac 184
381fddfd
CP
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]);
ff4befd8
CP
195 } else if(x.key == "left") {
196 this.prevWindow();
cf00dc43 197 success = true;
ff4befd8
CP
198 } else if(x.key == "right") {
199 this.nextWindow();
cf00dc43 200 success = true;
381fddfd 201 }
cf00dc43
CP
202 if(success)
203 new Event(x).stop();
381fddfd 204 }.bind(this));
841a451d 205 },
8af49135
CP
206 newCustomWindow: function(name, select, type) {
207 if(!type)
e20e5a6b 208 type = qwebirc.ui.WINDOW_CUSTOM;
8af49135 209
e20e5a6b 210 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
8af49135 211 w.addEvent("close", function(w) {
f74802c5 212 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
8af49135
CP
213 }.bind(this));
214
215 if(select)
216 this.selectWindow(w);
6c19eb8f 217
8af49135
CP
218 return w;
219 },
ebb21d2e
CP
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]);
8af49135 226 return;
841a451d 227 }
8af49135 228
ebb21d2e
CP
229 var d = this.newCustomWindow(windowName, true);
230 this.customWindows[windowName] = d;
231
232 d.addEvent("close", function() {
233 this.customWindows[windowName] = null;
8af49135
CP
234 }.bind(this));
235
ebb21d2e
CP
236 if(cssClass)
237 d.lines.addClass(cssClass);
238
239 var ew = new class_(d.lines, options);
8af49135 240 ew.addEvent("close", function() {
ebb21d2e 241 d.close();
8af49135 242 }.bind(this));
841a451d 243 },
ebb21d2e
CP
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 },
8af49135
CP
250 urlDispatcher: function(name) {
251 if(name == "embedded")
925fc357 252 return ["a", this.embeddedWindow.bind(this)];
ebb21d2e
CP
253
254 if(name == "options")
255 return ["a", this.optionsWindow.bind(this)];
8af49135
CP
256
257 return null;
3184781b
CP
258 },
259 tabComplete: function(element) {
260 this.tabCompleter.tabComplete(element);
261 },
262 resetTabComplete: function() {
263 this.tabCompleter.reset();
8af49135 264 }
381fddfd 265});
6f2e4a37 266
fb71087a 267qwebirc.ui.SoundUI = new Class({
e20e5a6b 268 Extends: qwebirc.ui.StandardUI,
fb71087a
CP
269 initialize: function(parentElement, windowClass, uiName, options) {
270 this.parent(parentElement, windowClass, uiName, options);
271
127631e0 272 this.soundInited = false;
fb71087a 273 this.soundReady = false;
fb71087a 274
127631e0 275 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
fb71087a 276 },
127631e0
CP
277 soundInit: function() {
278 if(this.soundInited)
fb71087a 279 return;
127631e0
CP
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();
fb71087a 289 },
127631e0
CP
290 setBeepOnMention: function(value) {
291 if(value)
292 this.soundInit();
293 this.beepOnMention = value;
294 },
295 beep: function() {
296 if(!this.soundReady || !this.beepOnMention)
fb71087a 297 return;
127631e0
CP
298
299 this.soundPlayer.beep();
fb71087a
CP
300 }
301});
302
303qwebirc.ui.QuakeNetUI = new Class({
304 Extends: qwebirc.ui.SoundUI,
2cd9e32d
CP
305 urlDispatcher: function(name, window) {
306 if(name == "qwhois") {
7cb09779 307 return ["span", function(auth) {
2cd9e32d 308 this.client.exec("/MSG Q whois #" + auth);
925fc357
CP
309 }.bind(window)];
310 }
311 if(name == "whois") {
312 return ["span", function(nick) {
313 this.client.exec("/WHOIS " + nick);
314 }.bind(window)];
2cd9e32d 315 }
2cd9e32d 316 return this.parent(name);
ffbb638d
CP
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 }
2cd9e32d
CP
327 }
328});
329
330qwebirc.ui.NewLoginUI = new Class({
331 Extends: qwebirc.ui.QuakeNetUI,
6f2e4a37
CP
332 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
333 this.postInitialize();
c38a0240 334
e20e5a6b 335 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
6f2e4a37
CP
336 var callback = function(args) {
337 w.close();
338 callbackfn(args);
339 };
340
e20e5a6b 341 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick);
6f2e4a37
CP
342 }
343});