]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/baseui.js
Close last active request on disconnect and erase timers.
[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;
6qwebirc.ui.CUSTOM_CLIENT = "custom";
9e769c12 7
e20e5a6b 8qwebirc.ui.BaseUI = new Class({
a59dc700
CP
9 Implements: [Events, Options],
10 options: {
7c633700
CP
11 appTitle: "QuakeNet Web IRC",
12 singleWindow: true
a59dc700
CP
13 },
14 initialize: function(parentElement, windowClass, uiName, options) {
15 this.setOptions(options);
16
9e769c12 17 this.windows = {};
ffbb638d 18 this.clients = {};
e20e5a6b 19 this.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
9e769c12
CP
20 this.windowArray = [];
21 this.windowClass = windowClass;
22 this.parentElement = parentElement;
23 this.parentElement.addClass("qwebirc");
24 this.parentElement.addClass("qwebirc-" + uiName);
e8db8558 25 this.firstClient = false;
e20e5a6b 26 this.commandhistory = new qwebirc.irc.CommandHistory();
ffbb638d 27 this.clientId = 0;
9e769c12
CP
28 },
29 newClient: function(client) {
ffbb638d 30 client.id = this.clientId++;
96f28062
CP
31 client.hilightController = new qwebirc.ui.HilightController(client);
32
ffbb638d
CP
33 this.windows[client.id] = {}
34 this.clients[client.id] = client;
e20e5a6b 35 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
9e769c12 36 this.selectWindow(w);
e8db8558
CP
37 if(!this.firstClient) {
38 this.firstClient = true;
e20e5a6b 39 w.addLine("", "qwebirc v" + qwebirc.VERSION);
4094890f 40 w.addLine("", "Copyright (C) 2008 Chris Porter. All rights reserved.");
e8db8558 41 w.addLine("", "http://webchat.quakenet.org/");
4094890f 42 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
e8db8558 43 }
9e769c12
CP
44 return w;
45 },
ffbb638d
CP
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 },
9e769c12
CP
53 newWindow: function(client, type, name) {
54 var identifier = name;
e20e5a6b 55 if(type == qwebirc.ui.WINDOW_STATUS)
9e769c12
CP
56 identifier = "";
57
ffbb638d 58 var w = this.windows[this.getClientId(client)][identifier] = new this.windowClass(this, client, type, name, identifier);
9e769c12
CP
59 this.windowArray.push(w);
60
61 return w;
62 },
63 getActiveWindow: function() {
64 return this.active;
65 },
1d42a76f
CP
66 getActiveIRCWindow: function(client) {
67 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
ffbb638d 68 return this.windows[this.getClientId(client)][""];
1d42a76f
CP
69 } else {
70 return this.active;
71 }
72 },
9e769c12
CP
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 */
a59dc700 80 document.title = window.name + " - " + this.options.appTitle;
9e769c12 81 },
ff4befd8
CP
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 },
9e769c12
CP
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);
6f2e4a37
CP
112 if(index == -1) {
113 return;
114 } else if(index == 0) {
9e769c12
CP
115 this.selectWindow(this.windowArray[1]);
116 } else {
117 this.selectWindow(this.windowArray[index - 1]);
118 }
9e769c12
CP
119 }
120 }
121
404cfb58 122 this.windowArray = this.windowArray.erase(window);
ffbb638d 123 delete this.windows[this.getClientId(window.client)][window.identifier];
eb9b087b 124 },
eb9b087b
CP
125 /*
126 this shouldn't be called by overriding classes!
66de775f 127 they should implement their own!
eb9b087b
CP
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 */
66de775f 132 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
e20e5a6b 133 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick);
9e769c12
CP
134 }
135});
381fddfd 136
e20e5a6b
CP
137qwebirc.ui.StandardUI = new Class({
138 Extends: qwebirc.ui.BaseUI,
381fddfd
CP
139 initialize: function(parentElement, windowClass, uiName, options) {
140 this.parent(parentElement, windowClass, uiName, options);
3184781b
CP
141
142 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
c38a0240 143 this.uiOptions = new qwebirc.ui.DefaultOptionsClass();
ebb21d2e
CP
144 this.customWindows = {};
145
381fddfd 146 window.addEvent("keydown", function(x) {
bc4d9f4a 147 if(!x.alt || x.control)
381fddfd
CP
148 return;
149
cf00dc43 150 var success = false;
381fddfd 151 if(x.key == "a" || x.key == "A") {
ffe442b0
CP
152 var highestNum = 0;
153 var highestIndex = -1;
cf00dc43
CP
154 success = true;
155
424608ac 156 new Event(x).stop();
381fddfd 157 for(var i=0;i<this.windowArray.length;i++) {
ffe442b0
CP
158 var h = this.windowArray[i].hilighted;
159 if(h > highestNum) {
160 highestIndex = i;
161 highestNum = h;
381fddfd
CP
162 }
163 }
ffe442b0
CP
164 if(highestIndex > -1)
165 this.selectWindow(this.windowArray[highestIndex]);
381fddfd 166 } else if(x.key >= '0' && x.key <= '9') {
cf00dc43 167 success = true;
424608ac 168
381fddfd
CP
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]);
ff4befd8
CP
179 } else if(x.key == "left") {
180 this.prevWindow();
cf00dc43 181 success = true;
ff4befd8
CP
182 } else if(x.key == "right") {
183 this.nextWindow();
cf00dc43 184 success = true;
381fddfd 185 }
cf00dc43
CP
186 if(success)
187 new Event(x).stop();
381fddfd 188 }.bind(this));
841a451d 189 },
8af49135
CP
190 newCustomWindow: function(name, select, type) {
191 if(!type)
e20e5a6b 192 type = qwebirc.ui.WINDOW_CUSTOM;
8af49135 193
e20e5a6b 194 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
8af49135 195 w.addEvent("close", function(w) {
ffbb638d 196 delete this.windows[qwebirc.ui.CUSTOM_CLIENT][name];
8af49135
CP
197 }.bind(this));
198
199 if(select)
200 this.selectWindow(w);
6c19eb8f 201
8af49135
CP
202 return w;
203 },
ebb21d2e
CP
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]);
8af49135 210 return;
841a451d 211 }
8af49135 212
ebb21d2e
CP
213 var d = this.newCustomWindow(windowName, true);
214 this.customWindows[windowName] = d;
215
216 d.addEvent("close", function() {
217 this.customWindows[windowName] = null;
8af49135
CP
218 }.bind(this));
219
ebb21d2e
CP
220 if(cssClass)
221 d.lines.addClass(cssClass);
222
223 var ew = new class_(d.lines, options);
8af49135 224 ew.addEvent("close", function() {
ebb21d2e 225 d.close();
8af49135 226 }.bind(this));
841a451d 227 },
ebb21d2e
CP
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 },
8af49135
CP
234 urlDispatcher: function(name) {
235 if(name == "embedded")
925fc357 236 return ["a", this.embeddedWindow.bind(this)];
ebb21d2e
CP
237
238 if(name == "options")
239 return ["a", this.optionsWindow.bind(this)];
8af49135
CP
240
241 return null;
3184781b
CP
242 },
243 tabComplete: function(element) {
244 this.tabCompleter.tabComplete(element);
245 },
246 resetTabComplete: function() {
247 this.tabCompleter.reset();
8af49135 248 }
381fddfd 249});
6f2e4a37 250
2cd9e32d 251qwebirc.ui.QuakeNetUI = new Class({
e20e5a6b 252 Extends: qwebirc.ui.StandardUI,
2cd9e32d
CP
253 urlDispatcher: function(name, window) {
254 if(name == "qwhois") {
7cb09779 255 return ["span", function(auth) {
2cd9e32d 256 this.client.exec("/MSG Q whois #" + auth);
925fc357
CP
257 }.bind(window)];
258 }
259 if(name == "whois") {
260 return ["span", function(nick) {
261 this.client.exec("/WHOIS " + nick);
262 }.bind(window)];
2cd9e32d 263 }
2cd9e32d 264 return this.parent(name);
ffbb638d
CP
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 }
2cd9e32d
CP
275 }
276});
277
278qwebirc.ui.NewLoginUI = new Class({
279 Extends: qwebirc.ui.QuakeNetUI,
6f2e4a37
CP
280 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
281 this.postInitialize();
c38a0240 282
e20e5a6b 283 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
6f2e4a37
CP
284 var callback = function(args) {
285 w.close();
286 callbackfn(args);
287 };
288
e20e5a6b 289 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick);
6f2e4a37
CP
290 }
291});