]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
Close last active request on disconnect and erase timers.
[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();
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.QuakeNetUI = new Class({
252 Extends: qwebirc.ui.StandardUI,
253 urlDispatcher: function(name, window) {
254 if(name == "qwhois") {
255 return ["span", function(auth) {
256 this.client.exec("/MSG Q whois #" + auth);
257 }.bind(window)];
258 }
259 if(name == "whois") {
260 return ["span", function(nick) {
261 this.client.exec("/WHOIS " + nick);
262 }.bind(window)];
263 }
264 return this.parent(name);
265 },
266 logout: function() {
267 if(!qwebirc.auth.loggedin())
268 return;
269 if(confirm("Log out?")) {
270 for(var client in this.clients) {
271 this.clients[client].quit("Logged out");
272 };
273 document.location = "/auth?logout=1";
274 }
275 }
276 });
277
278 qwebirc.ui.NewLoginUI = new Class({
279 Extends: qwebirc.ui.QuakeNetUI,
280 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
281 this.postInitialize();
282
283 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
284 var callback = function(args) {
285 w.close();
286 callbackfn(args);
287 };
288
289 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick);
290 }
291 });