/list response now shows in a new tab
[KiwiIRC.git] / js / front.js
CommitLineData
fde6ea95 1/*jslint nomen: true, devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */
2fc64ec2 2/*global gateway, io, $, iScroll, agent, touchscreen, init_data, plugs, plugins, registerTouches, randomString */
673a7c8f 3kiwi.front = {
a3364605
JA
4 revision: 38,
5
6 cur_channel: '',
7 windows: {},
8 tabviews: {},
604c5174 9 utilityviews: {},
a3364605
JA
10 boxes: {},
11
12 buffer: [],
13 buffer_pos: 0,
110ce6d4
JA
14
15 original_topic: '',
a3364605
JA
16
17 init: function () {
c6ef62a3
JA
18 /*global Box, touch_scroll:true */
19 var about_info, supportsOrientationChange, orientationEvent, scroll_opts;
673a7c8f
D
20 kiwi.gateway.nick = 'kiwi_' + Math.ceil(100 * Math.random()) + Math.ceil(100 * Math.random());
21 kiwi.gateway.session_id = null;
a3364605 22
673a7c8f
D
23 $(kiwi.gateway).bind("onmsg", kiwi.front.onMsg);
24 $(kiwi.gateway).bind("onnotice", kiwi.front.onNotice);
25 $(kiwi.gateway).bind("onaction", kiwi.front.onAction);
26 $(kiwi.gateway).bind("onmotd", kiwi.front.onMOTD);
27 $(kiwi.gateway).bind("onoptions", kiwi.front.onOptions);
28 $(kiwi.gateway).bind("onconnect", kiwi.front.onConnect);
29 $(kiwi.gateway).bind("onconnect_fail", kiwi.front.onConnectFail);
30 $(kiwi.gateway).bind("ondisconnect", kiwi.front.onDisconnect);
31 $(kiwi.gateway).bind("onnick", kiwi.front.onNick);
32 $(kiwi.gateway).bind("onuserlist", kiwi.front.onUserList);
33 $(kiwi.gateway).bind("onuserlist_end", kiwi.front.onUserListEnd);
b10afa2d
D
34 $(kiwi.gateway).bind("onlist_start", kiwi.front.onChannelListStart);
35 $(kiwi.gateway).bind("onlist_channel", kiwi.front.onChannelList);
36 $(kiwi.gateway).bind("onlist_end", kiwi.front.onChannelListEnd);
673a7c8f
D
37 $(kiwi.gateway).bind("onjoin", kiwi.front.onJoin);
38 $(kiwi.gateway).bind("ontopic", kiwi.front.onTopic);
39 $(kiwi.gateway).bind("onpart", kiwi.front.onPart);
40 $(kiwi.gateway).bind("onkick", kiwi.front.onKick);
41 $(kiwi.gateway).bind("onquit", kiwi.front.onQuit);
42 $(kiwi.gateway).bind("onmode", kiwi.front.onMode);
43 $(kiwi.gateway).bind("onwhois", kiwi.front.onWhois);
44 $(kiwi.gateway).bind("onsync", kiwi.front.onSync);
45 $(kiwi.gateway).bind("onchannel_redirect", kiwi.front.onChannelRedirect);
46 $(kiwi.gateway).bind("ondebug", kiwi.front.onDebug);
47 $(kiwi.gateway).bind("onctcp_request", kiwi.front.onCTCPRequest);
48 $(kiwi.gateway).bind("onctcp_response", kiwi.front.onCTCPResponse);
49 $(kiwi.gateway).bind("onirc_error", kiwi.front.onIRCError);
50 $(kiwi.gateway).bind("onkiwi", kiwi.front.onKiwi);
a3364605
JA
51
52 this.buffer = [];
53
54 // Build the about box
673a7c8f 55 kiwi.front.boxes.about = new Box("about");
a3364605
JA
56 about_info = 'UI adapted for ' + agent;
57 if (touchscreen) {
58 about_info += ' touchscreen ';
59 }
60 about_info += 'usage';
61 $('#tmpl_about_box').tmpl({
62 about: about_info,
673a7c8f
D
63 front_revision: kiwi.front.revision,
64 gateway_revision: kiwi.gateway.revision
65 }).appendTo(kiwi.front.boxes.about.content);
a3364605 66
673a7c8f 67 //$(window).bind("beforeunload", function(){ kiwi.gateway.quit(); });
a3364605
JA
68
69 if (touchscreen) {
70 $('#kiwi').addClass('touchscreen');
71
72 // Single touch scrolling through scrollback for touchscreens
73 scroll_opts = {};
74 touch_scroll = new iScroll('windows', scroll_opts);
75 }
76
673a7c8f 77 kiwi.front.registerKeys();
a3364605 78
673a7c8f 79 $('#kiwi .toolbars').resize(kiwi.front.doLayoutSize);
b10afa2d
D
80 $(window).resize(kiwi.front.doLayoutSize);
81
82 // Add the resizer for the userlist
83 $('<div id="nicklist_resize" style="position:absolute; cursor:w-resize; width:5px;"></div>').appendTo('#kiwi');
80c584e7
JA
84 $('#nicklist_resize').draggable({axis: "x", drag: function () {
85 var t = $(this),
fde6ea95
JA
86 new_width = $(document).width() - parseInt(t.css('left'), 10);
87
b10afa2d
D
88 new_width = new_width - parseInt($('#kiwi .userlist').css('margin-left'), 10);
89 new_width = new_width - parseInt($('#kiwi .userlist').css('margin-right'), 10);
90
91 // Make sure we don't remove the userlist alltogether
92 console.log(new_width);
93 if (new_width < 20) {
94 $(this).data('draggable').offset.click.left = 10;
95 console.log('whoaa');
96 }
97
98 kiwi.front.cur_channel.setUserlistWidth(new_width);
99 }});
100
a3364605
JA
101
102 $('#kiwi .formconnectwindow').submit(function () {
103 var netsel = $('#kiwi .formconnectwindow .network'),
104 nick = $('#kiwi .formconnectwindow .nick'),
105 tmp;
106
107 if (nick.val() === '') {
108 nick.val('Nick please!');
109 nick.focus();
110 return false;
111 }
112
113 tmp = nick.val().split(' ');
673a7c8f 114 kiwi.gateway.nick = tmp[0];
c89b9fdf
D
115
116 init_data.channel = $('#channel').val();
117
673a7c8f 118 kiwi.front.doLayout();
a3364605 119 try {
673a7c8f 120 kiwi.front.run('/connect ' + netsel.val());
b10afa2d 121 } catch (e) {}
a3364605 122
673a7c8f 123 $('#kiwi .connectwindow').slideUp('', kiwi.front.barsShow);
a3364605
JA
124 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
125
126 return false;
127 });
128
129 supportsOrientationChange = (typeof window.onorientationchange !== undefined);
130 orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
d30da6a0 131 if (window.addEventListener) {
673a7c8f 132 window.addEventListener(orientationEvent, kiwi.front.doLayoutSize, false);
d30da6a0
D
133 } else {
134 // < IE9
673a7c8f 135 window.attachEvent(orientationEvent, kiwi.front.doLayoutSize, false);
d30da6a0 136 }
673a7c8f 137 //$('#kiwi').bind("resize", kiwi.front.doLayoutSize, false);
a3364605 138
673a7c8f
D
139 kiwi.front.doLayout();
140 kiwi.front.barsHide();
875d0c71 141
673a7c8f
D
142 kiwi.front.tabviewAdd('server');
143 kiwi.front.tabviews.server.userlist_width = 0; // Disable the userlist
a3364605
JA
144
145 // Any pre-defined nick?
c89b9fdf 146 if (typeof window.init_data.nick === "string") {
bad1ea63
JA
147 $('#kiwi .formconnectwindow .nick').val(init_data.nick);
148 }
c89b9fdf
D
149
150 // Any pre-defined channels?
151 if (typeof window.init_data.channel === 'string') {
152 $('#channel').val(init_data.channel);
153 }
a3364605 154
8ee99eba
JA
155 // Fix for Opera inserting a spurious <br/>
156 $('#kiwi .cur_topic br').remove();
a3364605
JA
157
158 $('#kiwi .cur_topic').keydown(function (e) {
aa191535
D
159 if (e.which === 13) {
160 // enter
161 e.preventDefault();
162 $(this).change();
163 $('#kiwi_msginput').focus();
164 } else if (e.which === 27) {
165 // escape
166 e.preventDefault();
673a7c8f 167 $(this).text(kiwi.front.original_topic);
aa191535
D
168 $('#kiwi_msginput').focus();
169 }
a3364605 170 });
aa191535 171 /*$('.cur_topic').live('keypress', function(e) {
110ce6d4 172 if (e.keyCode === 13) {
1666d8d9 173 // enter
110ce6d4
JA
174 e.preventDefault();
175 $(this).change();
176 $('#kiwi_msginput').focus();
1666d8d9
JA
177 } else if (e.keyCode === 27) {
178 // escape
179 e.preventDefault();
673a7c8f 180 $(this).text(kiwi.front.original_topic);
110ce6d4 181 }
aa191535 182 });*/
a3364605 183 $('.cur_topic').live('change', function () {
110ce6d4
JA
184 var chan, text;
185 text = $(this).text();
673a7c8f
D
186 if (text !== kiwi.front.original_topic) {
187 chan = kiwi.front.cur_channel.name;
188 kiwi.gateway.setTopic(chan, text);
110ce6d4
JA
189 }
190 });
ff19dc74
D
191
192
c6ef62a3 193 $('#windows a.chan').live('click', function () {
673a7c8f 194 kiwi.front.joinChannel($(this).text());
ff19dc74
D
195 return false;
196 });
fde6ea95
JA
197
198 kiwi.data.set('chanList', []);
abb46d2d 199
2fc64ec2
JA
200 (function () {
201 var i;
202 for (i in plugins) {
673a7c8f 203 kiwi.plugs.loadPlugin(plugins[i]);
2fc64ec2
JA
204 }
205 }());
a3364605
JA
206 },
207
208 doLayoutSize: function () {
80c584e7 209 var kiwi, toolbars, ul, n_top, n_bottom, nl;
a3364605 210 kiwi = $('#kiwi');
875d0c71 211
a3364605
JA
212 if (kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')) {
213 console.log("switching to small kiwi");
214 kiwi.removeClass('large_kiwi');
215 kiwi.addClass('small_kiwi');
216 } else if (kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) {
217 kiwi.removeClass('small_kiwi');
218 kiwi.addClass('large_kiwi');
219 }
220
875d0c71 221 toolbars = $('#kiwi .cur_topic');
a3364605
JA
222 ul = $('#kiwi .userlist');
223
875d0c71 224 n_top = parseInt(toolbars.offset().top, 10) + parseInt(toolbars.outerHeight(true), 10);
a3364605
JA
225 n_bottom = $(document).height() - parseInt($('#kiwi .control').offset().top, 10);
226
227 $('#kiwi .windows').css({top: n_top + 'px', bottom: n_bottom + 'px'});
b10afa2d
D
228 ul.css({top: n_top + 'px', bottom: n_bottom + 'px'});
229
80c584e7
JA
230 nl = $('#nicklist_resize');
231 nl.css({top: n_top + 'px', bottom: n_bottom + 'px', left: $(document).width() - ul.outerWidth(true)});
a3364605
JA
232 },
233
234
235 doLayout: function () {
673a7c8f 236 $('#kiwi .msginput .nick a').text(kiwi.gateway.nick);
a3364605
JA
237 $('#kiwi_msginput').val(' ');
238 $('#kiwi_msginput').focus();
239 },
240
241
242 joinChannel: function (chan_name) {
243 var chans = chan_name.split(','),
c6ef62a3
JA
244 i,
245 chan;
a3364605
JA
246 for (i in chans) {
247 chan = chans[i];
673a7c8f
D
248 if (kiwi.front.tabviews[chan.toLowerCase()] === undefined || (kiwi.front.tabviews[chan.toLowerCase()] !== undefined && kiwi.front.tabviews[chan.toLowerCase()].safe_to_close === true)) {
249 kiwi.gateway.join(chan);
250 kiwi.front.tabviewAdd(chan);
a3364605 251 } else {
673a7c8f 252 kiwi.front.tabviews[chan.toLowerCase()].show();
a3364605
JA
253 }
254 }
255 },
256
257
258 run: function (msg) {
c6ef62a3 259 var parts, dest, t, pos, textRange, d, plugin_event, msg_sliced;
875d0c71
D
260
261 // Run through any plugins
262 plugin_event = {command: msg};
673a7c8f 263 plugin_event = kiwi.plugs.run('command_run', plugin_event);
c6ef62a3
JA
264 if (!plugin_event || typeof plugin_event.command === 'undefined') {
265 return;
266 }
875d0c71
D
267
268 // Update msg if it's been changed by any plugins
269 msg = plugin_event.command.toString();
270
a3364605
JA
271 console.log("running " + msg);
272 if (msg.substring(0, 1) === '/') {
273 parts = msg.split(' ');
274 switch (parts[0].toLowerCase()) {
275 case '/j':
276 case '/join':
673a7c8f 277 kiwi.front.joinChannel(parts[1]);
a3364605
JA
278 break;
279
280 case '/connect':
281 case '/server':
282 if (parts[1] === undefined) {
283 alert('Usage: /connect servername [port]');
284 break;
285 }
286
287 if (parts[2] === undefined) {
bad1ea63
JA
288 parts[2] = 6667;
289 }
673a7c8f
D
290 kiwi.front.cur_channel.addMsg(null, ' ', '=== Connecting to ' + parts[1] + '...', 'status');
291 kiwi.gateway.connect(parts[1], parts[2], 0);
a3364605
JA
292 break;
293
294 case '/nick':
295 console.log("/nick");
296 if (parts[1] === undefined) {
297 console.log("calling show nick");
673a7c8f 298 kiwi.front.showChangeNick();
a3364605
JA
299 } else {
300 console.log("sending raw");
673a7c8f 301 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
302 }
303 break;
304
305 case '/part':
306 if (typeof parts[1] === "undefined") {
673a7c8f
D
307 if (kiwi.front.cur_channel.safe_to_close) {
308 kiwi.front.cur_channel.close();
a3364605 309 } else {
673a7c8f 310 kiwi.gateway.raw(msg.substring(1) + ' ' + kiwi.front.cur_channel.name);
a3364605
JA
311 }
312 } else {
673a7c8f 313 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
314 }
315 break;
316
317 case '/names':
318 if (typeof parts[1] !== "undefined") {
673a7c8f 319 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
320 }
321 break;
322
323 case '/debug':
673a7c8f 324 kiwi.gateway.debug();
a3364605
JA
325 break;
326
327 case '/q':
328 case '/query':
329 if (typeof parts[1] !== "undefined") {
673a7c8f 330 kiwi.front.tabviewAdd(parts[1]);
a3364605
JA
331 }
332 break;
323e90f6
D
333
334
335 case '/m':
336 case '/msg':
337 if (typeof parts[1] !== "undefined") {
c6ef62a3 338 msg_sliced = msg.split(' ').slice(2).join(' ');
673a7c8f 339 kiwi.gateway.msg(parts[1], msg_sliced);
a5ffcf00 340
673a7c8f
D
341 if (!kiwi.front.tabviewExists(parts[1])) {
342 kiwi.front.tabviewAdd(parts[1]);
a5ffcf00 343 }
673a7c8f 344 kiwi.front.tabviews[parts[1].toLowerCase()].addMsg(null, kiwi.gateway.nick, msg_sliced);
323e90f6
D
345 }
346 break;
347
cdd803f6
D
348 case '/k':
349 case '/kick':
80c584e7 350 if (typeof parts[1] === 'undefined') {
fde6ea95
JA
351 return;
352 }
673a7c8f 353 kiwi.gateway.raw('KICK ' + kiwi.front.cur_channel.name + ' ' + msg.split(' ', 2)[1]);
cdd803f6
D
354 break;
355
a3364605 356 case '/quote':
673a7c8f 357 kiwi.gateway.raw(msg.replace(/^\/quote /i, ''));
a3364605
JA
358 break;
359
360 case '/me':
673a7c8f
D
361 kiwi.gateway.action(kiwi.front.cur_channel.name, msg.substring(4));
362 //kiwi.front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
363 kiwi.front.cur_channel.addMsg(null, ' ', '* ' + kiwi.gateway.nick + ' ' + msg.substring(4), 'action', 'color:#555;');
a3364605
JA
364 break;
365
366 case '/notice':
367 dest = parts[1];
368 msg = parts.slice(2).join(' ');
369
673a7c8f
D
370 kiwi.gateway.notice(dest, msg);
371 this.onNotice({}, {nick: kiwi.gateway.nick, channel: dest, msg: msg});
a3364605
JA
372 break;
373
374 case '/win':
375 if (parts[1] !== undefined) {
673a7c8f 376 kiwi.front.windowsShow(parseInt(parts[1], 10));
a3364605
JA
377 }
378 break;
379
380 case '/quit':
673a7c8f 381 kiwi.gateway.quit(msg.split(" ", 2)[1]);
f4f7781b
JA
382 break;
383
45c03116 384 case '/topic':
a3364605
JA
385 if (parts[1] === undefined) {
386 t = $('.cur_topic');
387 if (t.createTextRange) {
388 pos = t.text().length();
389 textRange = t.createTextRange();
390 textRange.collapse(true);
391 textRange.moveEnd(pos);
392 textRange.moveStart(pos);
393 textRange.select();
394 } else if (t.setSelectionRange) {
395 t.setSelectionRange(pos, pos);
396 }
397 } else {
673a7c8f
D
398 kiwi.gateway.setTopic(kiwi.front.cur_channel.name, msg.split(' ', 2)[1]);
399 //kiwi.gateway.raw('TOPIC ' + kiwi.front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
aa191535 400 }
45c03116 401 break;
abb46d2d
D
402
403 case '/kiwi':
673a7c8f 404 kiwi.gateway.kiwi(kiwi.front.cur_channel.name, msg.substring(6));
abb46d2d
D
405 break;
406
a3364605 407 default:
673a7c8f
D
408 //kiwi.front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
409 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
410 }
411
412 } else {
413 //alert('Sending message: '+msg);
414 if (msg.trim() === '') {
bad1ea63
JA
415 return;
416 }
673a7c8f
D
417 if (kiwi.front.cur_channel.name !== 'server') {
418 kiwi.gateway.msg(kiwi.front.cur_channel.name, msg);
a3364605
JA
419 d = new Date();
420 d = d.getHours() + ":" + d.getMinutes();
673a7c8f
D
421 //kiwi.front.addMsg(d, kiwi.gateway.nick, msg);
422 kiwi.front.cur_channel.addMsg(null, kiwi.gateway.nick, msg);
a3364605
JA
423 }
424 }
425 },
426
427
428 onMsg: function (e, data) {
875d0c71 429 var destination, plugin_event;
a3364605 430 // Is this message from a user?
673a7c8f 431 if (data.channel === kiwi.gateway.nick) {
a3364605
JA
432 destination = data.nick.toLowerCase();
433 } else {
434 destination = data.channel.toLowerCase();
435 }
436
c6ef62a3 437 plugin_event = {nick: data.nick, msg: data.msg, destination: destination};
673a7c8f 438 plugin_event = kiwi.plugs.run('msg_recieved', plugin_event);
c6ef62a3
JA
439 if (!plugin_event) {
440 return;
441 }
875d0c71 442
673a7c8f
D
443 if (!kiwi.front.tabviewExists(plugin_event.destination)) {
444 kiwi.front.tabviewAdd(plugin_event.destination);
bad1ea63 445 }
673a7c8f 446 kiwi.front.tabviews[plugin_event.destination].addMsg(null, plugin_event.nick, plugin_event.msg);
a3364605
JA
447 },
448
449 onDebug: function (e, data) {
673a7c8f
D
450 if (!kiwi.front.tabviewExists('kiwi_debug')) {
451 kiwi.front.tabviewAdd('kiwi_debug');
bad1ea63 452 }
673a7c8f 453 kiwi.front.tabviews.kiwi_debug.addMsg(null, ' ', data.msg);
a3364605
JA
454 },
455
456 onAction: function (e, data) {
bad1ea63 457 var destination;
a3364605 458 // Is this message from a user?
673a7c8f 459 if (data.channel === kiwi.gateway.nick) {
a3364605
JA
460 destination = data.nick;
461 } else {
462 destination = data.channel;
463 }
464
673a7c8f
D
465 if (!kiwi.front.tabviewExists(destination)) {
466 kiwi.front.tabviewAdd(destination);
bad1ea63 467 }
673a7c8f 468 kiwi.front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* ' + data.nick + ' ' + data.msg, 'action', 'color:#555;');
a3364605
JA
469 },
470
471 onTopic: function (e, data) {
673a7c8f
D
472 if (kiwi.front.tabviewExists(data.channel)) {
473 kiwi.front.tabviews[data.channel.toLowerCase()].changeTopic(data.topic);
a3364605
JA
474 }
475 },
476
477 onNotice: function (e, data) {
c6ef62a3
JA
478 var nick = (data.nick === undefined) ? '' : data.nick,
479 enick = '[' + nick + ']';
c89b9fdf 480
673a7c8f
D
481 if (kiwi.front.tabviewExists(data.target)) {
482 kiwi.front.tabviews[data.target.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
483 } else if (kiwi.front.tabviewExists(nick)) {
484 kiwi.front.tabviews[nick.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
a3364605 485 } else {
673a7c8f 486 kiwi.front.tabviews.server.addMsg(null, enick, data.msg, 'notice');
a3364605
JA
487 }
488 },
423a590e
JA
489
490 onCTCPRequest: function (e, data) {
491 var msg = data.msg.split(" ", 2);
492 switch (msg[0]) {
493 case 'PING':
a3364605
JA
494 if (typeof msg[1] === 'undefined') {
495 msg[1] = '';
496 }
673a7c8f 497 kiwi.gateway.notice(data.nick, String.fromCharCode(1) + 'PING ' + msg[1] + String.fromCharCode(1));
423a590e 498 break;
d9e91fed 499 case 'TIME':
673a7c8f 500 kiwi.gateway.notice(data.nick, String.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String.fromCharCode(1));
423a590e
JA
501 break;
502 }
673a7c8f 503 kiwi.front.tabviews.server.addMsg(null, 'CTCP [' + data.nick + ']', data.msg, 'ctcp');
423a590e
JA
504 },
505
a3364605 506 onCTCPResponse: function (e, data) {
423a590e 507 },
abb46d2d
D
508
509 onKiwi: function (e, data) {
510 //console.log(data);
511 },
423a590e 512
a3364605
JA
513 onConnect: function (e, data) {
514 if (data.connected) {
673a7c8f
D
515 if (kiwi.gateway.nick !== data.nick) {
516 kiwi.gateway.nick = data.nick;
517 kiwi.front.doLayout();
337c9866
D
518 }
519
673a7c8f 520 kiwi.front.tabviews.server.addMsg(null, ' ', '=== Connected OK :)', 'status');
a3364605 521 if (typeof init_data.channel === "string") {
673a7c8f 522 kiwi.front.joinChannel(init_data.channel);
a3364605 523 }
673a7c8f 524 kiwi.plugs.run('connect', {success: true});
a3364605 525 } else {
673a7c8f
D
526 kiwi.front.tabviews.server.addMsg(null, ' ', '=== Failed to connect :(', 'status');
527 kiwi.plugs.run('connect', {success: false});
a3364605
JA
528 }
529 },
b7be4766
D
530 onConnectFail: function (e, data) {
531 var reason = (typeof data.reason === 'string') ? data.reason : '';
673a7c8f
D
532 kiwi.front.tabviews.server.addMsg(null, '', 'There\'s a problem connecting! (' + reason + ')', 'error');
533 kiwi.plugs.run('connect', {success: false});
b7be4766 534 },
a3364605
JA
535 onDisconnect: function (e, data) {
536 var tab;
673a7c8f
D
537 for (tab in kiwi.front.tabviews) {
538 kiwi.front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error');
a3364605 539 }
673a7c8f 540 kiwi.plugs.run('disconnect', {success: false});
a3364605
JA
541 },
542 onOptions: function (e, data) {
673a7c8f
D
543 if (typeof kiwi.gateway.network_name === "string" && kiwi.gateway.network_name !== "") {
544 kiwi.front.tabviews.server.tab.text(kiwi.gateway.network_name);
a3364605
JA
545 }
546 },
547 onMOTD: function (e, data) {
673a7c8f 548 kiwi.front.tabviews.server.addMsg(null, data.server, data.msg, 'motd');
a3364605
JA
549 },
550 onWhois: function (e, data) {
0c5e2aaa
JA
551 var d;
552 if (data.msg) {
673a7c8f 553 kiwi.front.cur_channel.addMsg(null, data.nick, data.msg, 'whois');
0c5e2aaa
JA
554 } else if (data.logon) {
555 d = new Date();
556 d.setTime(data.logon * 1000);
557 d = d.toLocaleString();
673a7c8f 558 kiwi.front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' second' + ((data.idle !== 1) ? 's' : '') + ', signed on ' + d, 'whois');
0c5e2aaa 559 } else {
673a7c8f 560 kiwi.front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' seconds', 'whois');
0c5e2aaa 561 }
a3364605 562 },
abb46d2d 563 onMode: function (e, data) {
75681d88
D
564 var i, new_nick_text;
565
566 // TODO: Store the modes in the elements data, then work out the current
567 // mode symbol from the highest mode. Eg. -h may leave +o from previous modes; It
568 // doesn't simply clear it! ~Darren
569 if (typeof data.channel === 'string' && typeof data.effected_nick === 'string') {
673a7c8f
D
570 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '[' + data.mode + '] ' + data.effected_nick + ' by ' + data.nick, 'mode', '');
571 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () {
572 if (kiwi.front.nickStripPrefix($(this).text()) === data.effected_nick) {
75681d88
D
573
574 if (data.mode.split('')[0] === '+') {
80c584e7
JA
575 for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) {
576 if (kiwi.gateway.user_prefixes[i].mode === data.mode.split('')[1]) {
673a7c8f 577 new_nick_text = kiwi.gateway.user_prefixes[i].symbol + data.effected_nick;
75681d88
D
578 break;
579 }
580 }
581 } else if (data.mode.split('')[0] === '-') {
582 new_nick_text = data.effected_nick;
583 }
584
585 if (new_nick_text !== '') {
586 $(this).text(new_nick_text);
587 return false;
588 }
589
590 }
591 });
592 }
abb46d2d 593 },
a3364605
JA
594 onUserList: function (e, data) {
595 var ul, nick, mode;
673a7c8f 596 if (kiwi.front.tabviews[data.channel.toLowerCase()] === undefined) {
bad1ea63 597 return;
a3364605 598 }
673a7c8f 599 ul = kiwi.front.tabviews[data.channel.toLowerCase()].userlist;
a3364605
JA
600
601 if (!document.userlist_updating) {
602 document.userlist_updating = true;
603 ul.empty();
604 }
605
606 $.each(data.users, function (i, item) {
607 nick = i; //i.match(/^.+!/g);
608 mode = item;
673a7c8f 609 $('<li><a class="nick" onclick="kiwi.front.userClick(this);">' + mode + nick + '</a></li>').appendTo(ul);
a3364605
JA
610 });
611
673a7c8f 612 kiwi.front.tabviews[data.channel.toLowerCase()].userlistSort();
a3364605
JA
613 },
614 onUserListEnd: function (e, data) {
615 document.userlist_updating = false;
616 },
617
b10afa2d 618 onChannelListStart: function (e, data) {
fde6ea95 619 kiwi.data.set('chanList', []);
b10afa2d
D
620 },
621 onChannelList: function (e, data) {
fde6ea95
JA
622 var network_name = kiwi.gateway.network_name,
623 chanList = kiwi.data.get('chanList');
624 chanList.push(data);
625 kiwi.data.set('chanList', chanList);
b10afa2d
D
626 },
627 onChannelListEnd: function (e, data) {
fde6ea95
JA
628 var chanList, tab, table, body, chan;
629
630 chanList = kiwi.data.get('chanList');
631 tab = new Utilityview('Channel List');
632 table = $('<table><thead style="font-weight: bold;"><tr><td>Channel Name</td><td>Members</td><td style="padding-left: 2em;">Topic</td></tr></thead><tbody style="vertical-align: top;"></tbody>');
633 body = table.children('tbody:first');
634 chanList = _.sortBy(chanList, function (channel) {
635 return parseInt(channel.num_users, 10);
636 }).reverse();
637 for (chan in chanList) {
638 body.append($('<tr><td><a class="chan">' + chanList[chan].channel + '</a></td><td style="text-align: center;">' + chanList[chan].num_users + '</td><td style="padding-left: 2em;">' + chanList[chan].topic + '</td></tr>'));
639 }
640 tab.div.append(table);
641 tab.div.css('overflow-y', 'scroll');
642 tab.show();
b10afa2d
D
643 },
644
645
a3364605 646 onJoin: function (e, data) {
673a7c8f
D
647 if (!kiwi.front.tabviewExists(data.channel)) {
648 kiwi.front.tabviewAdd(data.channel.toLowerCase());
a3364605
JA
649 }
650
673a7c8f 651 if (data.nick === kiwi.gateway.nick) {
bad1ea63
JA
652 return; // Not needed as it's already in nicklist
653 }
673a7c8f
D
654 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action join', 'color:#009900;');
655 $('<li><a class="nick" onclick="kiwi.front.userClick(this);">' + data.nick + '</a></li>').appendTo(kiwi.front.tabviews[data.channel.toLowerCase()].userlist);
656 kiwi.front.tabviews[data.channel.toLowerCase()].userlistSort();
a3364605
JA
657 },
658 onPart: function (e, data) {
673a7c8f 659 if (kiwi.front.tabviewExists(data.channel)) {
a3364605 660 // If this is us, close the tabview
673a7c8f
D
661 if (data.nick === kiwi.gateway.nick) {
662 kiwi.front.tabviews[data.channel.toLowerCase()].close();
663 kiwi.front.tabviews.server.show();
a3364605
JA
664 return;
665 }
666
673a7c8f
D
667 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.nick + ' has left (' + data.message + ')', 'action part', 'color:#990000;');
668 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () {
a3364605
JA
669 if ($(this).text() === data.nick) {
670 $(this).remove();
671 }
672 });
673 }
674 },
675 onKick: function (e, data) {
673a7c8f 676 if (kiwi.front.tabviewExists(data.channel)) {
a3364605 677 // If this is us, close the tabview
673a7c8f
D
678 if (data.kicked === kiwi.gateway.nick) {
679 //kiwi.front.tabviews[data.channel.toLowerCase()].close();
680 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data.channel + '. ' + data.message, 'status kick');
681 kiwi.front.tabviews[data.channel.toLowerCase()].safe_to_close = true;
682 $('li', kiwi.front.tabviews[data.channel.toLowerCase()].userlist).remove();
a3364605
JA
683 return;
684 }
685
673a7c8f
D
686 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.kicked + ' kicked by ' + data.nick + '(' + data.message + ')', 'action kick', 'color:#990000;');
687 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () {
a3364605
JA
688 if ($(this).text() === data.nick) {
689 $(this).remove();
690 }
691 });
692 }
693 },
694 onNick: function (e, data) {
673a7c8f
D
695 if (data.nick === kiwi.gateway.nick) {
696 kiwi.gateway.nick = data.newnick;
697 kiwi.front.doLayout();
a3364605
JA
698 }
699
673a7c8f
D
700 $.each(kiwi.front.tabviews, function (i, item) {
701 $.each(kiwi.front.tabviews, function (i, item) {
a3364605
JA
702 item.changeNick(data.newnick, data.nick);
703 });
704 });
705 },
706 onQuit: function (e, data) {
673a7c8f
D
707 $.each(kiwi.front.tabviews, function (i, item) {
708 $.each(kiwi.front.tabviews, function (i, item) {
a3364605
JA
709 item.userlist.children().each(function () {
710 if ($(this).text() === data.nick) {
711 $(this).remove();
673a7c8f 712 item.addMsg(null, ' ', '<-- ' + data.nick + ' has quit (' + data.message + ')', 'action quit', 'color:#990000;');
a3364605
JA
713 }
714 });
715 });
716 });
717 },
718 onChannelRedirect: function (e, data) {
673a7c8f
D
719 kiwi.front.tabviews[data.from.toLowerCase()].close();
720 kiwi.front.tabviewAdd(data.to.toLowerCase());
721 kiwi.front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data.from, 'action');
a3364605
JA
722 },
723
81122538 724 onIRCError: function (e, data) {
a3364605 725 var t_view;
673a7c8f 726 if (data.channel !== undefined && kiwi.front.tabviewExists(data.channel)) {
a3364605
JA
727 t_view = data.channel;
728 } else {
729 t_view = 'server';
730 }
731
732 switch (data.error) {
81122538 733 case 'banned_from_channel':
673a7c8f 734 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== You are banned from ' + data.channel + '. ' + data.reason, 'status');
a3364605 735 if (t_view !== 'server') {
673a7c8f 736 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 737 }
81122538
JA
738 break;
739 case 'bad_channel_key':
673a7c8f 740 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== Bad channel key for ' + data.channel, 'status');
a3364605 741 if (t_view !== 'server') {
673a7c8f 742 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 743 }
81122538
JA
744 break;
745 case 'invite_only_channel':
673a7c8f 746 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is invite only.', 'status');
a3364605 747 if (t_view !== 'server') {
673a7c8f 748 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 749 }
81122538
JA
750 break;
751 case 'channel_is_full':
673a7c8f 752 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is full.', 'status');
a3364605 753 if (t_view !== 'server') {
673a7c8f 754 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 755 }
81122538 756 break;
818ef327 757 case 'chanop_privs_needed':
673a7c8f 758 kiwi.front.tabviews[data.channel].addMsg(null, ' ', '=== ' + data.reason, 'status');
818ef327
JA
759 break;
760 case 'no_such_nick':
673a7c8f 761 kiwi.front.tabviews.server.addMsg(null, ' ', '=== ' + data.nick + ': ' + data.reason, 'status');
818ef327 762 break;
337c9866 763 case 'nickname_in_use':
673a7c8f
D
764 kiwi.front.tabviews.server.addMsg(null, ' ', '=== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');
765 kiwi.front.showChangeNick();
337c9866 766 break;
81122538 767 default:
a3364605 768 // We don't know what data contains, so don't do anything with it.
673a7c8f 769 //kiwi.front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
81122538
JA
770 }
771 },
772
a3364605
JA
773 registerKeys: function () {
774 $('#kiwi_msginput').bind('keydown', function (e) {
c6ef62a3 775 var windows, meta, num, msg, data, candidates, word_pos, word, i;
a3364605
JA
776 windows = $('#windows');
777 //var meta = e.altKey;
778 meta = e.ctrlKey;
779
780 switch (true) {
781 case (e.which >= 48) && (e.which <= 57):
782 if (meta) {
783 num = e.which - 48;
784 if (num === 0) {
785 num = 10;
786 }
787 num = num - 1;
673a7c8f 788 kiwi.front.windowsShow(num);
a3364605
JA
789 return false;
790 }
791 break;
792 case e.which === 27: // escape
793 return false;
794 case e.which === 13: // return
795 msg = $('#kiwi_msginput').val();
796 msg = msg.trim();
797
673a7c8f
D
798 kiwi.front.buffer.push(msg);
799 kiwi.front.buffer_pos = kiwi.front.buffer.length;
a3364605 800
673a7c8f 801 kiwi.front.run(msg);
a3364605
JA
802 $('#kiwi_msginput').val('');
803
804 break;
805 case e.which === 33: // page up
806 console.log("page up");
807 windows[0].scrollTop = windows[0].scrollTop - windows.height();
808 return false;
809 case e.which === 34: // page down
810 windows[0].scrollTop = windows[0].scrollTop + windows.height();
811 return false;
812 case e.which === 37: // left
813 if (meta) {
673a7c8f 814 kiwi.front.windowsPrevious();
a3364605
JA
815 return false;
816 }
817 break;
818 case e.which === 38: // up
673a7c8f
D
819 if (kiwi.front.buffer_pos > 0) {
820 kiwi.front.buffer_pos--;
821 $('#kiwi_msginput').val(kiwi.front.buffer[kiwi.front.buffer_pos]);
a3364605
JA
822 }
823 break;
824 case e.which === 39: // right
825 if (meta) {
673a7c8f 826 kiwi.front.windowsNext();
a3364605
JA
827 return false;
828 }
829 break;
830 case e.which === 40: // down
673a7c8f
D
831 if (kiwi.front.buffer_pos < kiwi.front.buffer.length) {
832 kiwi.front.buffer_pos++;
833 $('#kiwi_msginput').val(kiwi.front.buffer[kiwi.front.buffer_pos]);
a3364605
JA
834 }
835 break;
836
837 case e.which === 9: // tab
838 // Get possible autocompletions
839 data = [];
673a7c8f 840 kiwi.front.cur_channel.userlist.children().each(function () {
c6ef62a3 841 var nick;
673a7c8f 842 nick = kiwi.front.nickStripPrefix($('a.nick', this).text());
a3364605
JA
843 data.push(nick);
844 });
845
846 // Do the autocomplete
847 if (this.value.length === this.selectionStart && this.value.length === this.selectionEnd) {
848 candidates = [];
849
850 word_pos = this.value.lastIndexOf(' ');
851 word = "";
852 if (word_pos === -1) {
853 word = this.value;
854 } else {
855 word = this.value.substr(word_pos);
856 }
857 word = word.trim();
858
859 // filter data to find only strings that start with existing value
860 for (i = 0; i < data.length; i++) {
861 if (data[i].indexOf(word) === 0 && data[i].length > word.length) {
862 candidates.push(data[i]);
bad1ea63 863 }
a3364605
JA
864 }
865
866 if (candidates.length > 0) {
867 // some candidates for autocompletion are found
868 this.value = this.value.substring(0, word_pos) + ' ' + candidates[0] + ': ';
869 this.selectionStart = this.value.length;
870 }
871 }
872 return false;
873 }
874 });
875
876
877 $('#kiwi .control .msginput .nick').click(function () {
673a7c8f 878 kiwi.front.showChangeNick();
a3364605
JA
879 });
880
881
882
883
884
885 $('#kiwi .plugins .load_plugin_file').click(function () {
673a7c8f 886 if (typeof kiwi.front.boxes.plugins !== "undefined") {
bad1ea63
JA
887 return;
888 }
a3364605 889
673a7c8f
D
890 kiwi.front.boxes.plugins = new Box("plugin_file");
891 $('#tmpl_plugins').tmpl({}).appendTo(kiwi.front.boxes.plugins.content);
892 kiwi.front.boxes.plugins.box.css('top', -(kiwi.front.boxes.plugins.height + 40));
a3364605
JA
893
894 // Populate the plugin list..
c6ef62a3
JA
895 function enumPlugins() {
896 var lst, j, txt;
4528138d
D
897 lst = $('#plugin_list');
898 lst.find('option').remove();
673a7c8f
D
899 for (j in kiwi.plugs.loaded) {
900 txt = kiwi.plugs.loaded[j].name;
4528138d
D
901 lst.append('<option value="' + txt + '">' + txt + '</option>');
902 }
a3364605 903 }
4528138d 904 enumPlugins();
a3364605
JA
905
906 // Event bindings
907 $('#kiwi .plugin_file').submit(function () {
c6ef62a3 908 $('<div></div>').load($('.txtpluginfile').val(), function (e) {
4528138d 909 enumPlugins();
a3364605
JA
910 });
911 return false;
912 });
913 $('#kiwi .cancelpluginfile').click(function () {
673a7c8f 914 kiwi.front.boxes.plugins.destroy();
a3364605
JA
915 });
916
917 $('#kiwi #plugins_list_unload').click(function () {
4528138d 918 var selected_plugin;
a3364605 919 selected_plugin = $('#plugin_list').val();
673a7c8f 920 kiwi.plugs.unloadPlugin(selected_plugin);
4528138d 921 enumPlugins();
a3364605
JA
922 });
923
924 $('#kiwi .txtpluginfile').focus();
925
926 });
927
928 $('#kiwi .plugins .reload_css').click(function () {
929 var links = document.getElementsByTagName("link"),
930 i;
931 for (i = 0; i < links.length; i++) {
932 if (links[i].rel === "stylesheet") {
933 if (links[i].href.indexOf("?") === -1) {
934 links[i].href += "?";
935 }
936 links[i].href += "x";
937 }
938 }
939 });
940
941
942 $('#kiwi .about .about_close').click(function () {
943 $('#kiwi .about').css('display', 'none');
944 });
945
946
947 $('#kiwi .poweredby').click(function () {
948 $('#kiwi .about').css('display', 'block');
949 });
950
951 },
952
953
954 showChangeNick: function () {
955 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
956
957 $('#kiwi .form_newnick').submit(function () {
673a7c8f 958 kiwi.front.run('/NICK ' + $('#kiwi .txtnewnick').val());
a3364605
JA
959 $('#kiwi .newnick').remove();
960 return false;
961 });
962
963 $('#kiwi .txtnewnick').keypress(function (ev) {
964 if (!this.first_press) {
965 this.first_press = true;
966 return false;
967 }
968 });
969
970 $('#kiwi .txtnewnick').keydown(function (ev) {
971 if (ev.which === 27) { // ESC
972 $('#kiwi_msginput').focus();
973 $('#kiwi .newnick').remove();
974 }
975 });
976
977 $('#kiwi .cancelnewnick').click(function () {
978 $('#kiwi .newnick').remove();
979 });
980
981 $('#kiwi .txtnewnick').focus();
982 },
983
984
985 tabviewExists: function (name) {
673a7c8f 986 return (typeof kiwi.front.tabviews[name.toLowerCase()] !== 'undefined');
a3364605
JA
987 },
988
989 tabviewAdd: function (v_name) {
c6ef62a3 990 /*global Tabview */
875d0c71
D
991 var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname, userlist_enabled = true;
992
673a7c8f
D
993 if (v_name.charAt(0) === kiwi.gateway.channel_prefix) {
994 re = new RegExp(kiwi.gateway.channel_prefix, "g");
a3364605
JA
995 htmlsafe_name = v_name.replace(re, 'pre');
996 htmlsafe_name = "chan_" + htmlsafe_name;
997 } else {
998 htmlsafe_name = 'query_' + v_name;
875d0c71 999 userlist_enabled = false;
a3364605
JA
1000 }
1001
1002 tmp_divname = 'kiwi_window_' + htmlsafe_name;
1003 tmp_userlistname = 'kiwi_userlist_' + htmlsafe_name;
1004 tmp_tabname = 'kiwi_tab_' + htmlsafe_name;
1005
673a7c8f 1006 if (!kiwi.front.tabviewExists(v_name)) {
a3364605
JA
1007 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>');
1008 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname + '"></ul>');
673a7c8f 1009 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname + '" onclick="kiwi.front.tabviews[\'' + v_name.toLowerCase() + '\'].show();">' + v_name + '</li>');
a3364605 1010 }
673a7c8f
D
1011 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ kiwi.front.windowShow(v_name); });
1012 //kiwi.front.windowShow(v_name);
a3364605 1013
673a7c8f
D
1014 kiwi.front.tabviews[v_name.toLowerCase()] = new Tabview();
1015 kiwi.front.tabviews[v_name.toLowerCase()].name = v_name;
1016 kiwi.front.tabviews[v_name.toLowerCase()].div = $('#' + tmp_divname);
1017 kiwi.front.tabviews[v_name.toLowerCase()].userlist = $('#' + tmp_userlistname);
1018 kiwi.front.tabviews[v_name.toLowerCase()].tab = $('#' + tmp_tabname);
c6ef62a3 1019 if (!userlist_enabled) {
673a7c8f 1020 kiwi.front.tabviews[v_name.toLowerCase()].userlist_width = 0;
c6ef62a3 1021 }
673a7c8f 1022 kiwi.front.tabviews[v_name.toLowerCase()].show();
a3364605
JA
1023
1024 if (typeof registerTouches === "function") {
1025 //alert("Registering touch interface");
1026 //registerTouches($('#'+tmp_divname));
1027 registerTouches(document.getElementById(tmp_divname));
1028 }
1029 /*
673a7c8f 1030 kiwi.front.tabviews[v_name.toLowerCase()].userlist.click(function(){
a3364605
JA
1031 alert($(this).attr('id'));
1032 });
1033 */
1034
673a7c8f 1035 kiwi.front.doLayoutSize();
a3364605
JA
1036 },
1037
1038
1039 userClick: function (item) {
ef885bff
JA
1040 var li = $(item).parent();
1041
a3364605
JA
1042 // Remove any existing userboxes
1043 $('#kiwi .userbox').remove();
ef885bff
JA
1044
1045 if ($(li).data('userbox') === item) {
1046 $(li).removeData('userbox');
1047 } else {
673a7c8f 1048 $('#tmpl_user_box').tmpl({nick: kiwi.front.nickStripPrefix($(item).text())}).appendTo(li);
ef885bff
JA
1049
1050 $('#kiwi .userbox .userbox_query').click(function (ev) {
1051 var nick = $('#kiwi .userbox_nick').val();
673a7c8f 1052 kiwi.front.run('/query ' + nick);
ef885bff
JA
1053 });
1054
1055 $('#kiwi .userbox .userbox_whois').click(function (ev) {
1056 var nick = $('#kiwi .userbox_nick').val();
673a7c8f 1057 kiwi.front.run('/whois ' + nick);
ef885bff
JA
1058 });
1059 $(li).data('userbox', item);
1060 }
a3364605
JA
1061 },
1062
1063
1064 sync: function () {
673a7c8f 1065 kiwi.gateway.sync();
a3364605
JA
1066 },
1067
1068 onSync: function (e, data) {
1069 // Set any settings
1070 if (data.nick !== undefined) {
673a7c8f 1071 kiwi.gateway.nick = data.nick;
a3364605
JA
1072 }
1073
1074 // Add the tabviews
1075 if (data.tabviews !== undefined) {
1076 $.each(data.tabviews, function (i, tab) {
673a7c8f
D
1077 if (!kiwi.front.tabviewExists(tab.name)) {
1078 kiwi.front.tabviewAdd(kiwi.gateway.channel_prefix + tab.name);
a3364605
JA
1079
1080 if (tab.userlist !== undefined) {
673a7c8f 1081 kiwi.front.onUserList({'channel': kiwi.gateway.channel_prefix + tab.name, 'users': tab.userlist});
a3364605
JA
1082 }
1083 }
1084 });
1085 }
1086
673a7c8f 1087 kiwi.front.doLayout();
a3364605
JA
1088 },
1089
1090
1091 setTopicText: function (new_topic) {
673a7c8f 1092 kiwi.front.original_topic = new_topic;
a3364605 1093 $('#kiwi .cur_topic .topic').text(new_topic);
673a7c8f 1094 kiwi.front.doLayoutSize();
a3364605
JA
1095 },
1096
1097
1098
1099
1100
1101
1102
1103 nickStripPrefix: function (nick) {
c6ef62a3 1104 var tmp = nick, i, prefix;
a3364605
JA
1105
1106 prefix = tmp.charAt(0);
673a7c8f
D
1107 for (i in kiwi.gateway.user_prefixes) {
1108 if (kiwi.gateway.user_prefixes[i].symbol === prefix) {
c6ef62a3 1109 return tmp.substring(1);
a3364605 1110 }
a3364605
JA
1111 }
1112
1113 return tmp;
1114 },
1115
1116 nickGetPrefix: function (nick) {
c6ef62a3 1117 var tmp = nick, i, prefix;
a3364605
JA
1118
1119 prefix = tmp.charAt(0);
673a7c8f
D
1120 for (i in kiwi.gateway.user_prefixes) {
1121 if (kiwi.gateway.user_prefixes[i].symbol === prefix) {
a3364605
JA
1122 return prefix;
1123 }
1124 }
1125
1126 return '';
1127 },
1128
1129 isChannel: function (name) {
c6ef62a3 1130 var prefix, is_chan;
a3364605 1131 prefix = name.charAt(0);
673a7c8f 1132 if (kiwi.gateway.channel_prefix.indexOf(prefix) > -1) {
a3364605
JA
1133 is_chan = true;
1134 } else {
1135 is_chan = false;
1136 }
1137
1138 return is_chan;
1139 },
1140
1141 tabviewsNext: function () {
1142 var wl = $('#kiwi .windowlist ul'),
1143 next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) + 170;
1144 wl.css('text-indent', next_left);
1145 },
1146
1147 tabviewsPrevious: function () {
1148 var wl = $('#kiwi .windowlist ul'),
1149 next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) - 170;
1150 wl.css('text-indent', next_left);
1151 },
1152
1153 windowsNext: function () {
1154 var tab, next;
1155 next = false;
673a7c8f 1156 for (tab in kiwi.front.tabviews) {
a3364605 1157 if (!next) {
673a7c8f 1158 if (kiwi.front.tabviews[tab] === kiwi.front.cur_channel) {
a3364605
JA
1159 next = true;
1160 continue;
1161 }
1162 } else {
673a7c8f 1163 kiwi.front.tabviews[tab].show();
a3364605
JA
1164 return;
1165 }
1166 }
1167 },
1168
1169 windowsPrevious: function () {
1170 var tab, prev_tab, next;
1171 next = false;
673a7c8f
D
1172 for (tab in kiwi.front.tabviews) {
1173 if (kiwi.front.tabviews[tab] === kiwi.front.cur_channel) {
a3364605
JA
1174 if (prev_tab) {
1175 prev_tab.show();
1176 }
1177 return;
1178 }
673a7c8f 1179 prev_tab = kiwi.front.tabviews[tab];
a3364605
JA
1180 }
1181 },
1182
1183 windowsShow: function (num) {
1184 num = parseInt(num, 10);
1185 console.log('Showing window ' + num.toString());
1186 var i = 0, tab;
673a7c8f 1187 for (tab in kiwi.front.tabviews) {
a3364605 1188 if (i === num) {
673a7c8f 1189 kiwi.front.tabviews[tab].show();
a3364605
JA
1190 return;
1191 }
1192 i++;
1193 }
c432172c
D
1194 },
1195
1196
1197
1198 barsShow: function () {
1199 $('#kiwi .toolbars').slideDown();
1200 $('#kiwi .control').slideDown();
1201 },
1202
1203 barsHide: function () {
1204 $('#kiwi .toolbars').slideUp();
1205 $('#kiwi .control').slideUp();
a3364605
JA
1206 }
1207};
54f4a22e
D
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1bb74900
D
1224/*
1225 * MISC VIEW
1226 */
1227
604c5174 1228var Utilityview = function (name) {
c6ef62a3
JA
1229 var rand_name = randomString(15),
1230 tmp_divname = 'kiwi_window_' + rand_name,
1231 tmp_userlistname = 'kiwi_userlist_' + rand_name,
1232 tmp_tabname = 'kiwi_tab_' + rand_name;
1bb74900 1233
604c5174
D
1234 this.name = rand_name;
1235 this.title = name;
1236 this.topic = ' ';
1bb74900 1237
604c5174
D
1238 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>');
1239
1240 this.tab = $('<li id="' + tmp_tabname + '">' + this.title + '</li>');
c6ef62a3 1241 this.tab.click(function () {
673a7c8f 1242 kiwi.front.utilityviews[rand_name.toLowerCase()].show();
604c5174
D
1243 });
1244 $('#kiwi .utilityviewlist ul').append(this.tab);
1bb74900
D
1245
1246 this.div = $('#' + tmp_divname);
1247 this.div.css('overflow', 'hidden');
1248
673a7c8f 1249 kiwi.front.utilityviews[rand_name.toLowerCase()] = this;
1bb74900
D
1250};
1251
1252Utilityview.prototype.name = null;
604c5174 1253Utilityview.prototype.title = null;
1bb74900
D
1254Utilityview.prototype.div = null;
1255Utilityview.prototype.tab = null;
ca7e63ea 1256Utilityview.prototype.topic = ' ';
1bb74900
D
1257Utilityview.prototype.show = function () {
1258 $('#kiwi .messages').removeClass("active");
1259 $('#kiwi .userlist ul').removeClass("active");
2b9dcc03 1260 $('#kiwi .toolbars ul li').removeClass("active");
1bb74900
D
1261
1262 $('#windows').css('overflow-y', 'hidden');
2b9dcc03 1263 $('#windows').css('right', 0);
1bb74900
D
1264 // Activate this tab!
1265 this.div.addClass('active');
1266 this.tab.addClass('active');
1267
1268 this.addPartImage();
1269
673a7c8f
D
1270 kiwi.front.setTopicText(this.topic);
1271 kiwi.front.cur_channel = this;
1bb74900
D
1272
1273 // If we're using fancy scrolling, refresh it
1274 if (touch_scroll) {
1275 touch_scroll.refresh();
1276 }
c6ef62a3 1277};
1bb74900
D
1278
1279Utilityview.prototype.close = function () {
1280 this.div.remove();
1281 this.tab.remove();
1282
673a7c8f
D
1283 if (kiwi.front.cur_channel === this) {
1284 kiwi.front.tabviews.server.show();
1bb74900 1285 }
673a7c8f 1286 delete kiwi.front.utilityviews[this.name.toLowerCase()];
1bb74900
D
1287};
1288
1289Utilityview.prototype.addPartImage = function () {
1290 this.clearPartImage();
1291
1292 // We can't close this tab, so don't have the close image
1293 if (this.name === 'server') {
1294 return;
1295 }
1296
8397d902 1297 var del_html = '<img src="/img/redcross.png" class="tab_part" />';
1bb74900
D
1298 this.tab.append(del_html);
1299
1300 $('.tab_part', this.tab).click(function () {
673a7c8f
D
1301 if (kiwi.front.cur_channel.name !== 'server') {
1302 kiwi.front.cur_channel.close();
1bb74900
D
1303 }
1304 });
1305};
1306
1307Utilityview.prototype.clearPartImage = function () {
2b9dcc03 1308 $('#kiwi .toolbars .tab_part').remove();
1bb74900
D
1309};
1310
1311
1312
1313
54f4a22e
D
1314
1315/*
1316 *
1317 * TABVIEWS
1318 *
1319 */
1320
1321
a3364605
JA
1322var Tabview = function () {};
1323Tabview.prototype.name = null;
1324Tabview.prototype.div = null;
1325Tabview.prototype.userlist = null;
875d0c71 1326Tabview.prototype.userlist_width = 100; // 0 for disabled
a3364605
JA
1327Tabview.prototype.tab = null;
1328Tabview.prototype.topic = "";
1329Tabview.prototype.safe_to_close = false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1330
1331Tabview.prototype.show = function () {
875d0c71
D
1332 var w, u;
1333
a3364605
JA
1334 $('#kiwi .messages').removeClass("active");
1335 $('#kiwi .userlist ul').removeClass("active");
2b9dcc03 1336 $('#kiwi .toolbars ul li').removeClass("active");
a3364605 1337
875d0c71
D
1338 w = $('#windows');
1339 u = $('#kiwi .userlist');
1340
1341 w.css('overflow-y', 'scroll');
1342
1343 // Set the window size accordingly
b10afa2d 1344 this.setUserlistWidth();
1bb74900 1345
a3364605
JA
1346 // Activate this tab!
1347 this.div.addClass('active');
c6ef62a3
JA
1348 if (this.userlist_width > 0) {
1349 this.userlist.addClass('active');
b10afa2d
D
1350 // Enable the userlist resizer
1351 $('#nicklist_resize').css('display', 'block');
1352 } else {
1353 // Disable the userlist resizer
1354 $('#nicklist_resize').css('display', 'none');
c6ef62a3 1355 }
a3364605
JA
1356 this.tab.addClass('active');
1357
a3364605
JA
1358 // Add the part image to the tab
1359 this.addPartImage();
1360
1361 this.clearHighlight();
673a7c8f
D
1362 kiwi.front.setTopicText(this.topic);
1363 kiwi.front.cur_channel = this;
a3364605
JA
1364
1365 // If we're using fancy scrolling, refresh it
1366 if (touch_scroll) {
1367 touch_scroll.refresh();
1368 }
1369
1370 this.scrollBottom();
1371 if (!touchscreen) {
1372 $('#kiwi_msginput').focus();
1373 }
1374};
1375
1376Tabview.prototype.close = function () {
1377 this.div.remove();
1378 this.userlist.remove();
1379 this.tab.remove();
1380
673a7c8f
D
1381 if (kiwi.front.cur_channel === this) {
1382 kiwi.front.tabviews.server.show();
a3364605 1383 }
673a7c8f 1384 delete kiwi.front.tabviews[this.name.toLowerCase()];
a3364605
JA
1385};
1386
b10afa2d
D
1387Tabview.prototype.setUserlistWidth = function (new_width) {
1388 var w, u;
80c584e7 1389 if (typeof new_width === 'number') {
fde6ea95
JA
1390 this.userlist_width = new_width;
1391 }
b10afa2d
D
1392
1393 w = $('#windows');
1394 u = $('#kiwi .userlist');
1395
1396 // Set the window size accordingly
1397 if (this.userlist_width > 0) {
1398 u.width(this.userlist_width);
1399 w.css('right', u.outerWidth(true));
1400 } else {
1401 w.css('right', 0);
1402 }
1403};
1404
a3364605
JA
1405Tabview.prototype.addPartImage = function () {
1406 this.clearPartImage();
1407
1408 // We can't close this tab, so don't have the close image
1409 if (this.name === 'server') {
1410 return;
1411 }
1412
8397d902 1413 var del_html = '<img src="/img/redcross.png" class="tab_part" />';
a3364605
JA
1414 this.tab.append(del_html);
1415
1416 $('.tab_part', this.tab).click(function () {
673a7c8f
D
1417 if (kiwi.front.isChannel($(this).parent().text())) {
1418 kiwi.front.run("/part");
a3364605
JA
1419 } else {
1420 // Make sure we don't close the server tab
673a7c8f
D
1421 if (kiwi.front.cur_channel.name !== 'server') {
1422 kiwi.front.cur_channel.close();
a3364605
JA
1423 }
1424 }
1425 });
1426};
1427
1428Tabview.prototype.clearPartImage = function () {
2b9dcc03 1429 $('#kiwi .toolbars .tab_part').remove();
a3364605
JA
1430};
1431
1432Tabview.prototype.addMsg = function (time, nick, msg, type, style) {
c6ef62a3 1433 var self, tmp, plugin_ret, i, d, re, line_msg, next;
a3364605
JA
1434
1435 self = this;
1436
ca7e63ea 1437 tmp = {msg: msg, time: time, nick: nick, tabview: this.name};
673a7c8f 1438 tmp = kiwi.plugs.run('addmsg', tmp);
c6ef62a3
JA
1439 if (!tmp) {
1440 return;
1441 }
a3364605 1442
ca7e63ea
D
1443
1444 msg = tmp.msg;
1445 time = tmp.time;
1446 nick = tmp.nick;
1447
a3364605
JA
1448 if (time === null) {
1449 d = new Date();
1450 time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0");
1451 }
1452
1453 // The CSS class (action, topic, notice, etc)
1454 if (typeof type !== "string") {
1455 type = '';
1456 }
1457
1458 // Make sure we don't have NaN or something
1459 if (typeof msg !== "string") {
1460 msg = '';
1461 }
1462
1463 // Text formatting
1464 // bold
1465 if (msg.indexOf(String.fromCharCode(2)) !== -1) {
1466 next = '<b>';
1467 while (msg.indexOf(String.fromCharCode(2)) !== -1) {
1468 msg = msg.replace(String.fromCharCode(2), next);
1469 next = (next === '<b>') ? '</b>' : '<b>';
1470 }
1471 if (next === '</b>') {
1472 msg = msg + '</b>';
1473 }
1474 }
1475
1476 // Wierd thing noticed by Dux0r on the irc.esper.net server
1477 if (typeof msg !== "string") {
1478 msg = '';
1479 }
1480
1481 // underline
1482 if (msg.indexOf(String.fromCharCode(31)) !== -1) {
1483 next = '<u>';
1484 while (msg.indexOf(String.fromCharCode(31)) !== -1) {
1485 msg = msg.replace(String.fromCharCode(31), next);
1486 next = (next === '<u>') ? '</u>' : '<u>';
1487 }
1488 if (next === '</u>') {
1489 msg = msg + '</u>';
1490 }
1491 }
1492
ca7e63ea 1493 // Make the channels clickable
673a7c8f 1494 re = new RegExp('\\B(' + kiwi.gateway.channel_prefix + '[^ ,.\\007]+)', 'g');
a3364605 1495 msg = msg.replace(re, function (match) {
ca7e63ea 1496 return '<a class="chan">' + match + '</a>';
9545f343
JA
1497 });
1498
ca7e63ea
D
1499 // Build up and add the line
1500 line_msg = $('<div class="msg ' + type + '"><div class="time">' + time + '</div><div class="nick">' + nick + '</div><div class="text" style="' + style + '">' + msg + ' </div></div>');
a3364605
JA
1501 this.div.append(line_msg);
1502
1503 if (!touchscreen) {
1504 this.scrollBottom();
1505 } else {
1506 touch_scroll.refresh();
1507 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1508 this.scrollBottom();
1509 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1510 // touch_scroll.scrollTo(0, this.div.height());
1511 //}
1512 }
1513};
1514
1515Tabview.prototype.scrollBottom = function () {
1516 var w = $('#windows');
1517 w[0].scrollTop = w[0].scrollHeight;
1518};
1519
1520Tabview.prototype.changeNick = function (newNick, oldNick) {
1521 this.userlist.children().each(function () {
1522 var item = $('a.nick', this);
673a7c8f
D
1523 if (kiwi.front.nickStripPrefix(item.text()) === oldNick) {
1524 item.text(kiwi.front.nickGetPrefix(item.text()) + newNick);
a3364605
JA
1525 document.temp_chan = 1;
1526 }
1527 });
1528
1529 if (typeof document.temp_chan !== "undefined") {
673a7c8f 1530 this.addMsg(null, ' ', '=== ' + oldNick + ' is now known as ' + newNick, 'action changenick');
a3364605
JA
1531 delete document.temp_chan;
1532 this.userlistSort();
1533 }
1534};
1535
1536Tabview.prototype.userlistSort = function () {
1537 var ul = this.userlist,
c6ef62a3
JA
1538 listitems = ul.children('li').get(),
1539 prefix;
a3364605
JA
1540 listitems.sort(function (a, b) {
1541 var compA = $(a).text().toUpperCase(),
1542 compB = $(b).text().toUpperCase(),
1543 i;
1544
1545 // Sort by prefixes first
673a7c8f
D
1546 for (i in kiwi.gateway.user_prefixes) {
1547 prefix = kiwi.gateway.user_prefixes[i].symbol;
a3364605
JA
1548
1549 if (compA.charAt(0) === prefix && compB.charAt(0) === prefix) {
1550 // Both have the same prefix, string compare time
1551 return 0;
1552 }
1553
1554 if (compA.charAt(0) === prefix && compB.charAt(0) !== prefix) {
1555 return -1;
1556 }
1557
1558 if (compA.charAt(0) !== prefix && compB.charAt(0) === prefix) {
1559 return 1;
1560 }
1561 }
1562
1563 // No prefixes, compare by string
1564 return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
1565 });
1566 $.each(listitems, function (idx, itm) { ul.append(itm); });
1567};
1568
1569Tabview.prototype.highlight = function () {
1570 this.tab.addClass('highlight');
1571};
1572Tabview.prototype.activity = function () {
1573 this.tab.addClass('activity');
1574};
1575Tabview.prototype.clearHighlight = function () {
1576 this.tab.removeClass('highlight');
1577 this.tab.removeClass('activity');
1578};
1579Tabview.prototype.changeTopic = function (new_topic) {
1580 this.topic = new_topic;
1581 this.addMsg(null, ' ', '=== Topic for ' + this.name + ' is: ' + new_topic, 'topic');
673a7c8f
D
1582 if (kiwi.front.cur_channel.name === this.name) {
1583 kiwi.front.setTopicText(new_topic);
a3364605
JA
1584 }
1585};
1586
1587
1588
1589
1590
1591var Box = function (classname) {
1592 this.id = randomString(10);
1593 var tmp = $('<div id="' + this.id + '" class="box ' + classname + '"><div class="boxarea"></div></div>');
1594 $('#kiwi').append(tmp);
1595 this.box = $('#' + this.id);
1596 this.content = $('#' + this.id + ' .boxarea');
1597
1598 this.box.draggable({ stack: ".box" });
1599 this.content.click(function () {});
1600 //this.box.click(function(){ $(this)..css });
1601};
1602Box.prototype.create = function (name, classname) {
1603
1604};
1605Box.prototype.id = null;
1606Box.prototype.box = null;
1607Box.prototype.content = null;
1608Box.prototype.destroy = function () {
1609 var name;
1610 this.box.remove();
673a7c8f
D
1611 for (name in kiwi.front.boxes) {
1612 if (kiwi.front.boxes[name].id === this.id) {
1613 delete kiwi.front.boxes[name];
a3364605
JA
1614 }
1615 }
1616};
1617Box.prototype.height = function () {
1618 return this.box.height();
1619};