]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseui.js
Add auth namespace.
[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.windows[qwebirc.ui.CUSTOM_CLIENT] = {};
19 this.windowArray = [];
20 this.windowClass = windowClass;
21 this.parentElement = parentElement;
22 this.parentElement.addClass("qwebirc");
23 this.parentElement.addClass("qwebirc-" + uiName);
24 this.firstClient = false;
25 this.commandhistory = new qwebirc.irc.CommandHistory();
26 },
27 newClient: function(client) {
28 client.hilightController = new qwebirc.ui.HilightController(client);
29
30 this.windows[client] = {}
31 var w = this.newWindow(client, qwebirc.ui.WINDOW_STATUS, "Status");
32 this.selectWindow(w);
33 if(!this.firstClient) {
34 this.firstClient = true;
35 w.addLine("", "qwebirc v" + qwebirc.VERSION);
36 w.addLine("", "Copyright (C) 2008 Chris Porter. All rights reserved.");
37 w.addLine("", "http://webchat.quakenet.org/");
38 w.addLine("", "This is BETA quality software, please report bugs to slug@quakenet.org");
39 }
40 return w;
41 },
42 newWindow: function(client, type, name) {
43 var identifier = name;
44 if(type == qwebirc.ui.WINDOW_STATUS)
45 identifier = "";
46
47 var w = this.windows[client][identifier] = new this.windowClass(this, client, type, name, identifier);
48 this.windowArray.push(w);
49
50 return w;
51 },
52 getActiveWindow: function() {
53 return this.active;
54 },
55 getActiveIRCWindow: function(client) {
56 if(!this.active || this.active.type == qwebirc.ui.WINDOW_CUSTOM) {
57 return this.windows[client][""];
58 } else {
59 return this.active;
60 }
61 },
62 __setActiveWindow: function(window) {
63 this.active = window;
64 },
65 selectWindow: function(window) {
66 if(this.active)
67 this.active.deselect();
68 window.select(); /* calls setActiveWindow */
69 document.title = window.name + " - " + this.options.appTitle;
70 },
71 nextWindow: function(direction) {
72 if(this.windowArray.length == 0 || !this.active)
73 return;
74
75 if(!direction)
76 direction = 1;
77
78 var index = this.windowArray.indexOf(this.active);
79 if(index == -1)
80 return;
81
82 index = index + direction;
83 if(index < 0) {
84 index = this.windowArray.length - 1;
85 } else if(index >= this.windowArray.length) {
86 index = 0;
87 }
88
89 this.selectWindow(this.windowArray[index]);
90 },
91 prevWindow: function() {
92 this.nextWindow(-1);
93 },
94 __closed: function(window) {
95 if(window.active) {
96 this.active = undefined;
97 if(this.windowArray.length == 1) {
98 this.windowArray = [];
99 } else {
100 var index = this.windowArray.indexOf(window);
101 if(index == -1) {
102 return;
103 } else if(index == 0) {
104 this.selectWindow(this.windowArray[1]);
105 } else {
106 this.selectWindow(this.windowArray[index - 1]);
107 }
108 }
109 }
110
111 this.windowArray = this.windowArray.erase(window);
112 delete this.windows[window.client][window.identifier];
113 },
114 /*
115 this shouldn't be called by overriding classes!
116 they should implement their own!
117 some form of user input MUST be received before an
118 IRC connection is made, else users are going to get
119 tricked into getting themselves glined
120 */
121 loginBox: function(callback, initialNickname, initialChannels, autoConnect, autoNick) {
122 qwebirc.ui.GenericLoginBox(this.parentElement, callback, initialNickname, initialChannels, autoConnect, autoNick);
123 }
124 });
125
126 qwebirc.ui.StandardUI = new Class({
127 Extends: qwebirc.ui.BaseUI,
128 initialize: function(parentElement, windowClass, uiName, options) {
129 this.parent(parentElement, windowClass, uiName, options);
130
131 this.tabCompleter = new qwebirc.ui.TabCompleterFactory(this);
132 this.uiOptions = new qwebirc.ui.DefaultOptionsClass();
133 this.customWindows = {};
134
135 window.addEvent("keydown", function(x) {
136 if(!x.alt || x.control)
137 return;
138
139 var success = false;
140 if(x.key == "a" || x.key == "A") {
141 var highestNum = 0;
142 var highestIndex = -1;
143 success = true;
144
145 new Event(x).stop();
146 for(var i=0;i<this.windowArray.length;i++) {
147 var h = this.windowArray[i].hilighted;
148 if(h > highestNum) {
149 highestIndex = i;
150 highestNum = h;
151 }
152 }
153 if(highestIndex > -1)
154 this.selectWindow(this.windowArray[highestIndex]);
155 } else if(x.key >= '0' && x.key <= '9') {
156 success = true;
157
158 number = x.key - '0';
159 if(number == 0)
160 number = 10
161
162 number = number - 1;
163
164 if(number >= this.windowArray.length)
165 return;
166
167 this.selectWindow(this.windowArray[number]);
168 } else if(x.key == "left") {
169 this.prevWindow();
170 success = true;
171 } else if(x.key == "right") {
172 this.nextWindow();
173 success = true;
174 }
175 if(success)
176 new Event(x).stop();
177 }.bind(this));
178 },
179 newCustomWindow: function(name, select, type) {
180 if(!type)
181 type = qwebirc.ui.WINDOW_CUSTOM;
182
183 var w = this.newWindow(qwebirc.ui.CUSTOM_CLIENT, type, name);
184 w.addEvent("close", function(w) {
185 delete this.windows[name];
186 }.bind(this));
187
188 if(select)
189 this.selectWindow(w);
190
191 return w;
192 },
193 addCustomWindow: function(windowName, class_, cssClass, options) {
194 if(!$defined(options))
195 options = {};
196
197 if(this.customWindows[windowName]) {
198 this.selectWindow(this.customWindows[windowName]);
199 return;
200 }
201
202 var d = this.newCustomWindow(windowName, true);
203 this.customWindows[windowName] = d;
204
205 d.addEvent("close", function() {
206 this.customWindows[windowName] = null;
207 }.bind(this));
208
209 if(cssClass)
210 d.lines.addClass(cssClass);
211
212 var ew = new class_(d.lines, options);
213 ew.addEvent("close", function() {
214 d.close();
215 }.bind(this));
216 },
217 embeddedWindow: function() {
218 this.addCustomWindow("Embedded Wizard", qwebirc.ui.EmbedWizard, "embeddedwizard");
219 },
220 optionsWindow: function() {
221 this.addCustomWindow("Options", qwebirc.ui.OptionsPane, "optionspane", this.uiOptions);
222 },
223 urlDispatcher: function(name) {
224 if(name == "embedded")
225 return ["a", this.embeddedWindow.bind(this)];
226
227 if(name == "options")
228 return ["a", this.optionsWindow.bind(this)];
229
230 return null;
231 },
232 tabComplete: function(element) {
233 this.tabCompleter.tabComplete(element);
234 },
235 resetTabComplete: function() {
236 this.tabCompleter.reset();
237 }
238 });
239
240 qwebirc.ui.QuakeNetUI = new Class({
241 Extends: qwebirc.ui.StandardUI,
242 urlDispatcher: function(name, window) {
243 if(name == "qwhois") {
244 return ["span", function(auth) {
245 this.client.exec("/MSG Q whois #" + auth);
246 }.bind(window)];
247 }
248 if(name == "whois") {
249 return ["span", function(nick) {
250 this.client.exec("/WHOIS " + nick);
251 }.bind(window)];
252 }
253 return this.parent(name);
254 }
255 });
256
257 qwebirc.ui.NewLoginUI = new Class({
258 Extends: qwebirc.ui.QuakeNetUI,
259 loginBox: function(callbackfn, initialNickname, initialChannels, autoConnect, autoNick) {
260 this.postInitialize();
261
262 var w = this.newCustomWindow("Connect", true, qwebirc.ui.WINDOW_CONNECT);
263 var callback = function(args) {
264 w.close();
265 callbackfn(args);
266 };
267
268 qwebirc.ui.GenericLoginBox(w.lines, callback, initialNickname, initialChannels, autoConnect, autoNick);
269 }
270 });