]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/baseui.js
Fix WEBIRC line, thanks thommey.
[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({
2cad083e 10 Implements: [Events],
a59dc700 11 initialize: function(parentElement, windowClass, uiName, options) {
2cad083e 12 this.options = options;
a59dc700 13
9e769c12 14 this.windows = {};
ffbb638d 15 this.clients = {};
e20e5a6b 16 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
9e769c12
CP
17 this.windowArray = [];
18 this.windowClass = windowClass;
19 this.parentElement = parentElement;
20 this.parentElement.addClass("qwebirc");
21 this.parentElement.addClass("qwebirc-" + uiName);
e8db8558 22 this.firstClient = false;
e20e5a6b 23 this.commandhistory = new qwebirc.irc.CommandHistory();
ffbb638d 24 this.clientId = 0;
9e769c12
CP
25 },
26 newClient: function(client) {
ffbb638d 27 client.id = this.clientId++;
96f28062
CP
28 client.hilightController = new qwebirc.ui.HilightController(client);
29
ffbb638d
CP
30 this.windows[client.id] = {}
31 this.clients[client.id] = client;
e20e5a6b 32 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
9e769c12 33 this.selectWindow(w);
e8db8558
CP
34 if(!this.firstClient) {
35 this.firstClient = true;
e20e5a6b 36 w.addLine("", "qwebirc v" + qwebirc.VERSION);
391f51ff 37 w.addLine("", "Copyright (C) 2008-2009 Chris Porter. All rights reserved.");
e8db8558 38 w.addLine("", "http://webchat.quakenet.org/");
4094890f 39 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
e8db8558 40 }
9e769c12
CP
41 return w;
42 },
ffbb638d
CP
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 },
fd3734d4 50 getWindowIdentifier: function(client, type, name) {
f74802c5
CP
51 if(type == qwebirc.ui.WINDOW_MESSAGES)
52 return "-M";
e20e5a6b 53 if(type == qwebirc.ui.WINDOW_STATUS)
f74802c5 54 return "";
fd3734d4
CP
55
56 if(client == qwebirc.ui.CUSTOM_CLIENT) /* HACK */
57 return "_" + name;
58
59 return "_" + client.toIRCLower(name);
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
fd3734d4 66 var wId = this.getWindowIdentifier(client, type, name);
f74802c5 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
fd3734d4 77 return c[this.getWindowIdentifier(client, type, name)];
f74802c5 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) {
fd3734d4 84 return this.windows[this.getClientId(client)][this.getWindowIdentifier(client, 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) {
2cad083e 149 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
9e769c12
CP
150 }
151});
381fddfd 152
e20e5a6b
CP
153qwebirc.ui.StandardUI = new Class({
154 Extends: qwebirc.ui.BaseUI,
f3d0c9f5 155 UICommands: qwebirc.ui.UI_COMMANDS,
381fddfd
CP
156 initialize: function(parentElement, windowClass, uiName, options) {
157 this.parent(parentElement, windowClass, uiName, options);
3184781b
CP
158
159 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
fb71087a 160 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
ebb21d2e
CP
161 this.customWindows = {};
162
2a802692 163 var ev;
20157c51
CP
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;
381fddfd 190 }
20157c51
CP
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
424608ac 200
20157c51
CP
201 number = number - 1;
202
203 if(number >= this.windowArray.length)
204 return;
381fddfd 205
20157c51
CP
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) {
deebe19a
CP
218 if($$("input").indexOf(x.target) == -1 && $$("textarea").indexOf(x.target) == -1)
219 return false;
220 return true;
841a451d 221 },
8af49135
CP
222 newCustomWindow: function(name, select, type) {
223 if(!type)
e20e5a6b 224 type = qwebirc.ui.WINDOW_CUSTOM;
8af49135 225
e20e5a6b 226 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
8af49135 227 w.addEvent("close", function(w) {
f74802c5 228 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
8af49135
CP
229 }.bind(this));
230
231 if(select)
232 this.selectWindow(w);
6c19eb8f 233
8af49135
CP
234 return w;
235 },
ebb21d2e
CP
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]);
8af49135 242 return;
841a451d 243 }
8af49135 244
ebb21d2e
CP
245 var d = this.newCustomWindow(windowName, true);
246 this.customWindows[windowName] = d;
247
248 d.addEvent("close", function() {
249 this.customWindows[windowName] = null;
8af49135
CP
250 }.bind(this));
251
ebb21d2e 252 if(cssClass)
e1a91a8a 253 d.lines.addClass("qwebirc-" + cssClass);
ebb21d2e
CP
254
255 var ew = new class_(d.lines, options);
8af49135 256 ew.addEvent("close", function() {
ebb21d2e 257 d.close();
8af49135 258 }.bind(this));
17f40fd9
CP
259
260 d.setSubWindow(ew);
841a451d 261 },
ebb21d2e 262 embeddedWindow: function() {
b35116e2 263 this.addCustomWindow("Embedding wizard", qwebirc.ui.EmbedWizard, "embeddedwizard");
ebb21d2e
CP
264 },
265 optionsWindow: function() {
266 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
267 },
e1a91a8a
CP
268 aboutWindow: function() {
269 this.addCustomWindow("About", qwebirc.ui.AboutPane, "aboutpane", this.uiOptions);
270 },
b35116e2
CP
271 privacyWindow: function() {
272 this.addCustomWindow("Privacy policy", qwebirc.ui.PrivacyPolicyPane, "privacypolicypane", this.uiOptions);
273 },
391f51ff
CP
274 feedbackWindow: function() {
275 this.addCustomWindow("Feedback", qwebirc.ui.FeedbackPane, "feedbackpane", this.uiOptions);
276 },
f3d0c9f5
CP
277 faqWindow: function() {
278 this.addCustomWindow("FAQ", qwebirc.ui.FAQPane, "faqpane", this.uiOptions);
279 },
8af49135
CP
280 urlDispatcher: function(name) {
281 if(name == "embedded")
925fc357 282 return ["a", this.embeddedWindow.bind(this)];
ebb21d2e
CP
283
284 if(name == "options")
285 return ["a", this.optionsWindow.bind(this)];
8af49135
CP
286
287 return null;
3184781b
CP
288 },
289 tabComplete: function(element) {
290 this.tabCompleter.tabComplete(element);
291 },
292 resetTabComplete: function() {
293 this.tabCompleter.reset();
8af49135 294 }
381fddfd 295});
6f2e4a37 296
fb71087a 297qwebirc.ui.SoundUI = new Class({
e20e5a6b 298 Extends: qwebirc.ui.StandardUI,
fb71087a
CP
299 initialize: function(parentElement, windowClass, uiName, options) {
300 this.parent(parentElement, windowClass, uiName, options);
301
127631e0 302 this.soundInited = false;
fb71087a 303 this.soundReady = false;
fb71087a 304
127631e0 305 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
fb71087a 306 },
127631e0
CP
307 soundInit: function() {
308 if(this.soundInited)
fb71087a 309 return;
127631e0
CP
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();
fb71087a 319 },
127631e0
CP
320 setBeepOnMention: function(value) {
321 if(value)
322 this.soundInit();
323 this.beepOnMention = value;
324 },
325 beep: function() {
326 if(!this.soundReady || !this.beepOnMention)
fb71087a 327 return;
127631e0
CP
328
329 this.soundPlayer.beep();
fb71087a
CP
330 }
331});
332
333qwebirc.ui.QuakeNetUI = new Class({
334 Extends: qwebirc.ui.SoundUI,
2cd9e32d
CP
335 urlDispatcher: function(name, window) {
336 if(name == "qwhois") {
7cb09779 337 return ["span", function(auth) {
2cd9e32d 338 this.client.exec("/MSG Q whois #" + auth);
925fc357
CP
339 }.bind(window)];
340 }
341 if(name == "whois") {
342 return ["span", function(nick) {
343 this.client.exec("/WHOIS " + nick);
344 }.bind(window)];
2cd9e32d 345 }
2cd9e32d 346 return this.parent(name);
ffbb638d
CP
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 };
4b9f894d
CP
355
356 /* HACK */
357 var foo = function() { document.location = "/auth?logout=1"; };
358 foo.delay(500);
ffbb638d 359 }
2cd9e32d
CP
360 }
361});
362
363qwebirc.ui.NewLoginUI = new Class({
364 Extends: qwebirc.ui.QuakeNetUI,
6f2e4a37
CP
365 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
366 this.postInitialize();
c38a0240 367
e20e5a6b 368 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
6f2e4a37
CP
369 var callback = function(args) {
370 w.close();
371 callbackfn(args);
372 };
373
2cad083e 374 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
6f2e4a37
CP
375 }
376});