]> jfr.im git - irc/quakenet/qwebirc.git/blame_incremental - js/ui/baseui.js
dropdown menu is now acceptable.
[irc/quakenet/qwebirc.git] / js / ui / baseui.js
... / ...
CommitLineData
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;
6qwebirc.ui.WINDOW_MESSAGES = 6;
7qwebirc.ui.CUSTOM_CLIENT = "custom";
8
9qwebirc.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 Chris Porter. All rights reserved.");
38 w.addLine("", "http://webchat.quakenet.org/");
39 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
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(type, name) {
51 if(type == qwebirc.ui.WINDOW_MESSAGES)
52 return "-M";
53 if(type == qwebirc.ui.WINDOW_STATUS)
54 return "";
55 return "_" + name.toIRCLower();
56 },
57 newWindow: function(client, type, name) {
58 var w = this.getWindow(client, type, name);
59 if($defined(w))
60 return w;
61
62 var wId = this.getWindowIdentifier(type, name);
63 var w = this.windows[this.getClientId(client)][wId] = new this.windowClass(this, client, type, name, wId);
64 this.windowArray.push(w);
65
66 return w;
67 },
68 getWindow: function(client, type, name) {
69 var c = this.windows[this.getClientId(client)];
70 if(!$defined(c))
71 return null;
72
73 return c[this.getWindowIdentifier(type, name)];
74 },
75 getActiveWindow: function() {
76 return this.active;
77 },
78 getActiveIRCWindow: function(client) {
79 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
80 return this.windows[this.getClientId(client)][this.getWindowIdentifier(qwebirc.ui.WINDOW_STATUS)];
81 } else {
82 return this.active;
83 }
84 },
85 __setActiveWindow: function(window) {
86 this.active = window;
87 },
88 selectWindow: function(window) {
89 if(this.active)
90 this.active.deselect();
91 window.select(); /* calls setActiveWindow */
92 document.title = window.name + " - " + this.options.appTitle;
93 },
94 nextWindow: function(direction) {
95 if(this.windowArray.length == 0 || !this.active)
96 return;
97
98 if(!direction)
99 direction = 1;
100
101 var index = this.windowArray.indexOf(this.active);
102 if(index == -1)
103 return;
104
105 index = index + direction;
106 if(index < 0) {
107 index = this.windowArray.length - 1;
108 } else if(index >= this.windowArray.length) {
109 index = 0;
110 }
111
112 this.selectWindow(this.windowArray[index]);
113 },
114 prevWindow: function() {
115 this.nextWindow(-1);
116 },
117 __closed: function(window) {
118 if(window.active) {
119 this.active = undefined;
120 if(this.windowArray.length == 1) {
121 this.windowArray = [];
122 } else {
123 var index = this.windowArray.indexOf(window);
124 if(index == -1) {
125 return;
126 } else if(index == 0) {
127 this.selectWindow(this.windowArray[1]);
128 } else {
129 this.selectWindow(this.windowArray[index - 1]);
130 }
131 }
132 }
133
134 this.windowArray = this.windowArray.erase(window);
135 delete this.windows[this.getClientId(window.client)][window.identifier];
136 },
137 /*
138 this shouldn't be called by overriding classes!
139 they should implement their own!
140 some form of user input MUST be received before an
141 IRC connection is made, else users are going to get
142 tricked into getting themselves glined
143 */
144 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
145 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
146 }
147});
148
149qwebirc.ui.StandardUI = new Class({
150 Extends: qwebirc.ui.BaseUI,
151 UICommands: [
152 ["Options", "options"],
153 ["Add webchat to your site", "embedded"],
154 ["Privacy policy", "privacy"],
155 ["About qwebirc", "about"]
156 ],
157 initialize: function(parentElement, windowClass, uiName, options) {
158 this.parent(parentElement, windowClass, uiName, options);
159
160 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
161 this.uiOptions = new qwebirc.ui.DefaultOptionsClass(this);
162 this.customWindows = {};
163
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;
190 }
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
200
201 number = number - 1;
202
203 if(number >= this.windowArray.length)
204 return;
205
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) {
218 return $$("input").indexOf(x.target) > -1;
219 },
220 newCustomWindow: function(name, select, type) {
221 if(!type)
222 type = qwebirc.ui.WINDOW_CUSTOM;
223
224 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
225 w.addEvent("close", function(w) {
226 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][w.identifier];
227 }.bind(this));
228
229 if(select)
230 this.selectWindow(w);
231
232 return w;
233 },
234 addCustomWindow: function(windowName, class_, cssClass, options) {
235 if(!$defined(options))
236 options = {};
237
238 if(this.customWindows[windowName]) {
239 this.selectWindow(this.customWindows[windowName]);
240 return;
241 }
242
243 var d = this.newCustomWindow(windowName, true);
244 this.customWindows[windowName] = d;
245
246 d.addEvent("close", function() {
247 this.customWindows[windowName] = null;
248 }.bind(this));
249
250 if(cssClass)
251 d.lines.addClass("qwebirc-" + cssClass);
252
253 var ew = new class_(d.lines, options);
254 ew.addEvent("close", function() {
255 d.close();
256 }.bind(this));
257 },
258 embeddedWindow: function() {
259 this.addCustomWindow("Embedding wizard", qwebirc.ui.EmbedWizard, "embeddedwizard");
260 },
261 optionsWindow: function() {
262 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
263 },
264 aboutWindow: function() {
265 this.addCustomWindow("About", qwebirc.ui.AboutPane, "aboutpane", this.uiOptions);
266 },
267 privacyWindow: function() {
268 this.addCustomWindow("Privacy policy", qwebirc.ui.PrivacyPolicyPane, "privacypolicypane", this.uiOptions);
269 },
270 urlDispatcher: function(name) {
271 if(name == "embedded")
272 return ["a", this.embeddedWindow.bind(this)];
273
274 if(name == "options")
275 return ["a", this.optionsWindow.bind(this)];
276
277 return null;
278 },
279 tabComplete: function(element) {
280 this.tabCompleter.tabComplete(element);
281 },
282 resetTabComplete: function() {
283 this.tabCompleter.reset();
284 }
285});
286
287qwebirc.ui.SoundUI = new Class({
288 Extends: qwebirc.ui.StandardUI,
289 initialize: function(parentElement, windowClass, uiName, options) {
290 this.parent(parentElement, windowClass, uiName, options);
291
292 this.soundInited = false;
293 this.soundReady = false;
294
295 this.setBeepOnMention(this.uiOptions.BEEP_ON_MENTION);
296 },
297 soundInit: function() {
298 if(this.soundInited)
299 return;
300 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
301 return;
302 this.soundInited = true;
303
304 this.soundPlayer = new qwebirc.sound.SoundPlayer();
305 this.soundPlayer.addEvent("ready", function() {
306 this.soundReady = true;
307 }.bind(this));
308 this.soundPlayer.go();
309 },
310 setBeepOnMention: function(value) {
311 if(value)
312 this.soundInit();
313 this.beepOnMention = value;
314 },
315 beep: function() {
316 if(!this.soundReady || !this.beepOnMention)
317 return;
318
319 this.soundPlayer.beep();
320 }
321});
322
323qwebirc.ui.QuakeNetUI = new Class({
324 Extends: qwebirc.ui.SoundUI,
325 urlDispatcher: function(name, window) {
326 if(name == "qwhois") {
327 return ["span", function(auth) {
328 this.client.exec("/MSG Q whois #" + auth);
329 }.bind(window)];
330 }
331 if(name == "whois") {
332 return ["span", function(nick) {
333 this.client.exec("/WHOIS " + nick);
334 }.bind(window)];
335 }
336 return this.parent(name);
337 },
338 logout: function() {
339 if(!qwebirc.auth.loggedin())
340 return;
341 if(confirm("Log out?")) {
342 for(var client in this.clients) {
343 this.clients[client].quit("Logged out");
344 };
345 document.location = "/auth?logout=1";
346 }
347 }
348});
349
350qwebirc.ui.NewLoginUI = new Class({
351 Extends: qwebirc.ui.QuakeNetUI,
352 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
353 this.postInitialize();
354
355 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
356 var callback = function(args) {
357 w.close();
358 callbackfn(args);
359 };
360
361 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick, this.options.networkName);
362 }
363});