Nicklist refactor pull
[KiwiIRC.git] / js / front.js
CommitLineData
8343584e
D
1/*jslint white:true, regexp: true, nomen: true, devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */
2/*global kiwi, _, io, $, iScroll, agent, touchscreen, init_data, plugs, plugins, registerTouches, randomString */
673a7c8f 3kiwi.front = {
a3364605 4 revision: 38,
8343584e 5
a3364605
JA
6 cur_channel: '',
7 windows: {},
8 tabviews: {},
604c5174 9 utilityviews: {},
a3364605 10 boxes: {},
8343584e 11
a3364605
JA
12 buffer: [],
13 buffer_pos: 0,
110ce6d4 14
5c7ac96f 15 cache: {},
8343584e 16
110ce6d4 17 original_topic: '',
8343584e 18
a3364605 19 init: function () {
c6ef62a3
JA
20 /*global Box, touch_scroll:true */
21 var about_info, supportsOrientationChange, orientationEvent, scroll_opts;
673a7c8f
D
22 kiwi.gateway.nick = 'kiwi_' + Math.ceil(100 * Math.random()) + Math.ceil(100 * Math.random());
23 kiwi.gateway.session_id = null;
8343584e 24
673a7c8f
D
25 $(kiwi.gateway).bind("onmsg", kiwi.front.onMsg);
26 $(kiwi.gateway).bind("onnotice", kiwi.front.onNotice);
27 $(kiwi.gateway).bind("onaction", kiwi.front.onAction);
28 $(kiwi.gateway).bind("onmotd", kiwi.front.onMOTD);
29 $(kiwi.gateway).bind("onoptions", kiwi.front.onOptions);
30 $(kiwi.gateway).bind("onconnect", kiwi.front.onConnect);
31 $(kiwi.gateway).bind("onconnect_fail", kiwi.front.onConnectFail);
32 $(kiwi.gateway).bind("ondisconnect", kiwi.front.onDisconnect);
8717f02d 33 $(kiwi.gateway).bind("onreconnecting", kiwi.front.onReconnecting);
673a7c8f
D
34 $(kiwi.gateway).bind("onnick", kiwi.front.onNick);
35 $(kiwi.gateway).bind("onuserlist", kiwi.front.onUserList);
36 $(kiwi.gateway).bind("onuserlist_end", kiwi.front.onUserListEnd);
b10afa2d
D
37 $(kiwi.gateway).bind("onlist_start", kiwi.front.onChannelListStart);
38 $(kiwi.gateway).bind("onlist_channel", kiwi.front.onChannelList);
39 $(kiwi.gateway).bind("onlist_end", kiwi.front.onChannelListEnd);
673a7c8f
D
40 $(kiwi.gateway).bind("onjoin", kiwi.front.onJoin);
41 $(kiwi.gateway).bind("ontopic", kiwi.front.onTopic);
42 $(kiwi.gateway).bind("onpart", kiwi.front.onPart);
43 $(kiwi.gateway).bind("onkick", kiwi.front.onKick);
44 $(kiwi.gateway).bind("onquit", kiwi.front.onQuit);
45 $(kiwi.gateway).bind("onmode", kiwi.front.onMode);
46 $(kiwi.gateway).bind("onwhois", kiwi.front.onWhois);
47 $(kiwi.gateway).bind("onsync", kiwi.front.onSync);
48 $(kiwi.gateway).bind("onchannel_redirect", kiwi.front.onChannelRedirect);
49 $(kiwi.gateway).bind("ondebug", kiwi.front.onDebug);
50 $(kiwi.gateway).bind("onctcp_request", kiwi.front.onCTCPRequest);
51 $(kiwi.gateway).bind("onctcp_response", kiwi.front.onCTCPResponse);
52 $(kiwi.gateway).bind("onirc_error", kiwi.front.onIRCError);
53 $(kiwi.gateway).bind("onkiwi", kiwi.front.onKiwi);
8343584e 54
a3364605 55 this.buffer = [];
8343584e 56
a3364605 57 // Build the about box
673a7c8f 58 kiwi.front.boxes.about = new Box("about");
a3364605
JA
59 about_info = 'UI adapted for ' + agent;
60 if (touchscreen) {
61 about_info += ' touchscreen ';
62 }
63 about_info += 'usage';
64 $('#tmpl_about_box').tmpl({
65 about: about_info,
673a7c8f
D
66 front_revision: kiwi.front.revision,
67 gateway_revision: kiwi.gateway.revision
68 }).appendTo(kiwi.front.boxes.about.content);
a3364605 69
673a7c8f 70 //$(window).bind("beforeunload", function(){ kiwi.gateway.quit(); });
8343584e 71
a3364605
JA
72 if (touchscreen) {
73 $('#kiwi').addClass('touchscreen');
74
75 // Single touch scrolling through scrollback for touchscreens
76 scroll_opts = {};
77 touch_scroll = new iScroll('windows', scroll_opts);
78 }
79
673a7c8f 80 kiwi.front.registerKeys();
8343584e 81
673a7c8f 82 $('#kiwi .toolbars').resize(kiwi.front.doLayoutSize);
b10afa2d
D
83 $(window).resize(kiwi.front.doLayoutSize);
84
85 // Add the resizer for the userlist
86 $('<div id="nicklist_resize" style="position:absolute; cursor:w-resize; width:5px;"></div>').appendTo('#kiwi');
80c584e7
JA
87 $('#nicklist_resize').draggable({axis: "x", drag: function () {
88 var t = $(this),
fde6ea95 89 new_width = $(document).width() - parseInt(t.css('left'), 10);
8343584e 90
b10afa2d
D
91 new_width = new_width - parseInt($('#kiwi .userlist').css('margin-left'), 10);
92 new_width = new_width - parseInt($('#kiwi .userlist').css('margin-right'), 10);
93
94 // Make sure we don't remove the userlist alltogether
b10afa2d
D
95 if (new_width < 20) {
96 $(this).data('draggable').offset.click.left = 10;
97 console.log('whoaa');
98 }
99
8343584e 100 kiwi.front.cur_channel.userlist.setWidth(new_width);
b10afa2d
D
101 }});
102
a3364605
JA
103
104 $('#kiwi .formconnectwindow').submit(function () {
105 var netsel = $('#kiwi .formconnectwindow .network'),
46f98bec
JA
106 netport = $('#kiwi .formconnectwindow .port'),
107 netssl = $('#kiwi .formconnectwindow .ssl'),
a3364605
JA
108 nick = $('#kiwi .formconnectwindow .nick'),
109 tmp;
8343584e 110
a3364605
JA
111 if (nick.val() === '') {
112 nick.val('Nick please!');
113 nick.focus();
114 return false;
115 }
8343584e 116
a3364605 117 tmp = nick.val().split(' ');
673a7c8f 118 kiwi.gateway.nick = tmp[0];
c89b9fdf
D
119
120 init_data.channel = $('#channel').val();
121
673a7c8f 122 kiwi.front.doLayout();
a3364605 123 try {
46f98bec 124 kiwi.front.run('/connect ' + netsel.val() + ' ' + netport.val() + ' ' + (netssl.attr('checked') ? 'true' : ''));
4c408915
D
125 } catch (e) {
126 console.log(e);
127 }
8343584e 128
673a7c8f 129 $('#kiwi .connectwindow').slideUp('', kiwi.front.barsShow);
a3364605
JA
130 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
131
132 return false;
133 });
134
135 supportsOrientationChange = (typeof window.onorientationchange !== undefined);
136 orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
d30da6a0 137 if (window.addEventListener) {
673a7c8f 138 window.addEventListener(orientationEvent, kiwi.front.doLayoutSize, false);
d30da6a0
D
139 } else {
140 // < IE9
673a7c8f 141 window.attachEvent(orientationEvent, kiwi.front.doLayoutSize, false);
d30da6a0 142 }
673a7c8f 143 //$('#kiwi').bind("resize", kiwi.front.doLayoutSize, false);
a3364605 144
673a7c8f
D
145 kiwi.front.doLayout();
146 kiwi.front.barsHide();
8343584e 147
673a7c8f 148 kiwi.front.tabviewAdd('server');
8343584e
D
149 //kiwi.front.tabviews.server.userlist.setWidth(0); // Disable the userlist
150
a3364605 151 // Any pre-defined nick?
c89b9fdf 152 if (typeof window.init_data.nick === "string") {
bad1ea63
JA
153 $('#kiwi .formconnectwindow .nick').val(init_data.nick);
154 }
c89b9fdf
D
155
156 // Any pre-defined channels?
157 if (typeof window.init_data.channel === 'string') {
158 $('#channel').val(init_data.channel);
159 }
8343584e 160
8ee99eba
JA
161 // Fix for Opera inserting a spurious <br/>
162 $('#kiwi .cur_topic br').remove();
8343584e 163
a3364605 164 $('#kiwi .cur_topic').keydown(function (e) {
aa191535
D
165 if (e.which === 13) {
166 // enter
167 e.preventDefault();
168 $(this).change();
169 $('#kiwi_msginput').focus();
170 } else if (e.which === 27) {
171 // escape
172 e.preventDefault();
673a7c8f 173 $(this).text(kiwi.front.original_topic);
aa191535
D
174 $('#kiwi_msginput').focus();
175 }
a3364605 176 });
aa191535 177 /*$('.cur_topic').live('keypress', function(e) {
110ce6d4 178 if (e.keyCode === 13) {
1666d8d9 179 // enter
110ce6d4
JA
180 e.preventDefault();
181 $(this).change();
182 $('#kiwi_msginput').focus();
1666d8d9
JA
183 } else if (e.keyCode === 27) {
184 // escape
185 e.preventDefault();
673a7c8f 186 $(this).text(kiwi.front.original_topic);
110ce6d4 187 }
aa191535 188 });*/
a3364605 189 $('.cur_topic').live('change', function () {
110ce6d4
JA
190 var chan, text;
191 text = $(this).text();
673a7c8f
D
192 if (text !== kiwi.front.original_topic) {
193 chan = kiwi.front.cur_channel.name;
194 kiwi.gateway.setTopic(chan, text);
110ce6d4
JA
195 }
196 });
ff19dc74
D
197
198
c6ef62a3 199 $('#windows a.chan').live('click', function () {
673a7c8f 200 kiwi.front.joinChannel($(this).text());
ff19dc74
D
201 return false;
202 });
8343584e 203
fde6ea95 204 kiwi.data.set('chanList', []);
abb46d2d 205
2fc64ec2
JA
206 (function () {
207 var i;
208 for (i in plugins) {
673a7c8f 209 kiwi.plugs.loadPlugin(plugins[i]);
2fc64ec2
JA
210 }
211 }());
a3364605 212 },
8343584e 213
a3364605 214 doLayoutSize: function () {
80c584e7 215 var kiwi, toolbars, ul, n_top, n_bottom, nl;
a3364605 216 kiwi = $('#kiwi');
875d0c71 217
a3364605
JA
218 if (kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')) {
219 console.log("switching to small kiwi");
220 kiwi.removeClass('large_kiwi');
221 kiwi.addClass('small_kiwi');
222 } else if (kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) {
223 kiwi.removeClass('small_kiwi');
224 kiwi.addClass('large_kiwi');
225 }
226
875d0c71 227 toolbars = $('#kiwi .cur_topic');
a3364605
JA
228 ul = $('#kiwi .userlist');
229
875d0c71 230 n_top = parseInt(toolbars.offset().top, 10) + parseInt(toolbars.outerHeight(true), 10);
a3364605
JA
231 n_bottom = $(document).height() - parseInt($('#kiwi .control').offset().top, 10);
232
233 $('#kiwi .windows').css({top: n_top + 'px', bottom: n_bottom + 'px'});
b10afa2d
D
234 ul.css({top: n_top + 'px', bottom: n_bottom + 'px'});
235
80c584e7
JA
236 nl = $('#nicklist_resize');
237 nl.css({top: n_top + 'px', bottom: n_bottom + 'px', left: $(document).width() - ul.outerWidth(true)});
a3364605
JA
238 },
239
240
241 doLayout: function () {
673a7c8f 242 $('#kiwi .msginput .nick a').text(kiwi.gateway.nick);
a3364605
JA
243 $('#kiwi_msginput').val(' ');
244 $('#kiwi_msginput').focus();
245 },
8343584e
D
246
247
a3364605
JA
248 joinChannel: function (chan_name) {
249 var chans = chan_name.split(','),
c6ef62a3
JA
250 i,
251 chan;
a3364605
JA
252 for (i in chans) {
253 chan = chans[i];
673a7c8f
D
254 if (kiwi.front.tabviews[chan.toLowerCase()] === undefined || (kiwi.front.tabviews[chan.toLowerCase()] !== undefined && kiwi.front.tabviews[chan.toLowerCase()].safe_to_close === true)) {
255 kiwi.gateway.join(chan);
256 kiwi.front.tabviewAdd(chan);
a3364605 257 } else {
673a7c8f 258 kiwi.front.tabviews[chan.toLowerCase()].show();
a3364605
JA
259 }
260 }
261 },
8343584e
D
262
263
a3364605 264 run: function (msg) {
c6ef62a3 265 var parts, dest, t, pos, textRange, d, plugin_event, msg_sliced;
8343584e 266
875d0c71
D
267 // Run through any plugins
268 plugin_event = {command: msg};
673a7c8f 269 plugin_event = kiwi.plugs.run('command_run', plugin_event);
c6ef62a3
JA
270 if (!plugin_event || typeof plugin_event.command === 'undefined') {
271 return;
272 }
875d0c71
D
273
274 // Update msg if it's been changed by any plugins
275 msg = plugin_event.command.toString();
276
8343584e 277
a3364605 278 if (msg.substring(0, 1) === '/') {
cd81360a 279 console.log("running " + msg);
a3364605
JA
280 parts = msg.split(' ');
281 switch (parts[0].toLowerCase()) {
282 case '/j':
283 case '/join':
673a7c8f 284 kiwi.front.joinChannel(parts[1]);
a3364605 285 break;
8343584e 286
a3364605
JA
287 case '/connect':
288 case '/server':
4c408915
D
289 if (typeof parts[1] === 'undefined') {
290 alert('Usage: /connect servername [port] [ssl]');
a3364605
JA
291 break;
292 }
8343584e 293
4c408915 294 if (typeof parts[2] === 'undefined') {
bad1ea63
JA
295 parts[2] = 6667;
296 }
8343584e 297
4c408915 298 if ((typeof parts[3] === 'undefined') || !parts[3] || (parts[3] === 'false') || (parts[3] === 'no')) {
46f98bec
JA
299 parts[3] = false;
300 } else {
301 parts[3] = true;
302 }
8343584e 303
4c408915 304 kiwi.front.cur_channel.addMsg(null, ' ', '=== Connecting to ' + parts[1] + ' on port ' + parts[2] + (parts[3] ? ' using SSL' : '') + '...', 'status');
46f98bec 305 kiwi.gateway.connect(parts[1], parts[2], parts[3]);
a3364605 306 break;
8343584e 307
a3364605
JA
308 case '/nick':
309 console.log("/nick");
310 if (parts[1] === undefined) {
311 console.log("calling show nick");
673a7c8f 312 kiwi.front.showChangeNick();
a3364605
JA
313 } else {
314 console.log("sending raw");
673a7c8f 315 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
316 }
317 break;
318
319 case '/part':
320 if (typeof parts[1] === "undefined") {
673a7c8f
D
321 if (kiwi.front.cur_channel.safe_to_close) {
322 kiwi.front.cur_channel.close();
a3364605 323 } else {
673a7c8f 324 kiwi.gateway.raw(msg.substring(1) + ' ' + kiwi.front.cur_channel.name);
a3364605
JA
325 }
326 } else {
673a7c8f 327 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
328 }
329 break;
8343584e 330
a3364605
JA
331 case '/names':
332 if (typeof parts[1] !== "undefined") {
673a7c8f 333 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
334 }
335 break;
8343584e 336
a3364605 337 case '/debug':
673a7c8f 338 kiwi.gateway.debug();
a3364605 339 break;
8343584e 340
a3364605
JA
341 case '/q':
342 case '/query':
343 if (typeof parts[1] !== "undefined") {
673a7c8f 344 kiwi.front.tabviewAdd(parts[1]);
a3364605
JA
345 }
346 break;
323e90f6 347
8343584e 348
323e90f6
D
349 case '/m':
350 case '/msg':
351 if (typeof parts[1] !== "undefined") {
c6ef62a3 352 msg_sliced = msg.split(' ').slice(2).join(' ');
673a7c8f 353 kiwi.gateway.msg(parts[1], msg_sliced);
a5ffcf00 354
673a7c8f
D
355 if (!kiwi.front.tabviewExists(parts[1])) {
356 kiwi.front.tabviewAdd(parts[1]);
a5ffcf00 357 }
673a7c8f 358 kiwi.front.tabviews[parts[1].toLowerCase()].addMsg(null, kiwi.gateway.nick, msg_sliced);
323e90f6
D
359 }
360 break;
8343584e 361
cdd803f6
D
362 case '/k':
363 case '/kick':
80c584e7 364 if (typeof parts[1] === 'undefined') {
fde6ea95
JA
365 return;
366 }
673a7c8f 367 kiwi.gateway.raw('KICK ' + kiwi.front.cur_channel.name + ' ' + msg.split(' ', 2)[1]);
cdd803f6
D
368 break;
369
a3364605 370 case '/quote':
673a7c8f 371 kiwi.gateway.raw(msg.replace(/^\/quote /i, ''));
a3364605 372 break;
8343584e 373
a3364605 374 case '/me':
673a7c8f
D
375 kiwi.gateway.action(kiwi.front.cur_channel.name, msg.substring(4));
376 //kiwi.front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
377 kiwi.front.cur_channel.addMsg(null, ' ', '* ' + kiwi.gateway.nick + ' ' + msg.substring(4), 'action', 'color:#555;');
a3364605
JA
378 break;
379
380 case '/notice':
381 dest = parts[1];
382 msg = parts.slice(2).join(' ');
383
673a7c8f
D
384 kiwi.gateway.notice(dest, msg);
385 this.onNotice({}, {nick: kiwi.gateway.nick, channel: dest, msg: msg});
a3364605
JA
386 break;
387
388 case '/win':
389 if (parts[1] !== undefined) {
673a7c8f 390 kiwi.front.windowsShow(parseInt(parts[1], 10));
a3364605
JA
391 }
392 break;
393
394 case '/quit':
b575fa08 395 kiwi.gateway.quit(parts.slice(1).join(' '));
f4f7781b
JA
396 break;
397
45c03116 398 case '/topic':
a3364605
JA
399 if (parts[1] === undefined) {
400 t = $('.cur_topic');
401 if (t.createTextRange) {
402 pos = t.text().length();
403 textRange = t.createTextRange();
404 textRange.collapse(true);
405 textRange.moveEnd(pos);
406 textRange.moveStart(pos);
407 textRange.select();
408 } else if (t.setSelectionRange) {
409 t.setSelectionRange(pos, pos);
410 }
411 } else {
673a7c8f
D
412 kiwi.gateway.setTopic(kiwi.front.cur_channel.name, msg.split(' ', 2)[1]);
413 //kiwi.gateway.raw('TOPIC ' + kiwi.front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
aa191535 414 }
45c03116 415 break;
abb46d2d
D
416
417 case '/kiwi':
673a7c8f 418 kiwi.gateway.kiwi(kiwi.front.cur_channel.name, msg.substring(6));
abb46d2d
D
419 break;
420
a3c7edd4
JA
421 case '/ctcp':
422 parts = parts.slice(1);
423 dest = parts.shift();
424 msg = parts.join(' ');
425
426 kiwi.gateway.msg(dest, String.fromCharCode(1) + msg + String.fromCharCode(1));
427 kiwi.front.tabviews.server.addMsg(null, 'CTCP Request', '[to ' + dest + '] ' + msg, 'ctcp');
428 break;
a3364605 429 default:
673a7c8f 430 //kiwi.front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
8343584e 431 kiwi.gateway.raw(msg.substring(1));
a3364605
JA
432 }
433
434 } else {
435 //alert('Sending message: '+msg);
436 if (msg.trim() === '') {
bad1ea63
JA
437 return;
438 }
673a7c8f
D
439 if (kiwi.front.cur_channel.name !== 'server') {
440 kiwi.gateway.msg(kiwi.front.cur_channel.name, msg);
a3364605
JA
441 d = new Date();
442 d = d.getHours() + ":" + d.getMinutes();
673a7c8f
D
443 //kiwi.front.addMsg(d, kiwi.gateway.nick, msg);
444 kiwi.front.cur_channel.addMsg(null, kiwi.gateway.nick, msg);
a3364605
JA
445 }
446 }
447 },
8343584e
D
448
449
a3364605 450 onMsg: function (e, data) {
875d0c71 451 var destination, plugin_event;
a3364605 452 // Is this message from a user?
673a7c8f 453 if (data.channel === kiwi.gateway.nick) {
a3364605
JA
454 destination = data.nick.toLowerCase();
455 } else {
8343584e 456 destination = data.channel.toLowerCase();
a3364605 457 }
8343584e 458
c6ef62a3 459 plugin_event = {nick: data.nick, msg: data.msg, destination: destination};
673a7c8f 460 plugin_event = kiwi.plugs.run('msg_recieved', plugin_event);
c6ef62a3
JA
461 if (!plugin_event) {
462 return;
463 }
875d0c71 464
673a7c8f
D
465 if (!kiwi.front.tabviewExists(plugin_event.destination)) {
466 kiwi.front.tabviewAdd(plugin_event.destination);
bad1ea63 467 }
673a7c8f 468 kiwi.front.tabviews[plugin_event.destination].addMsg(null, plugin_event.nick, plugin_event.msg);
a3364605 469 },
8343584e 470
a3364605 471 onDebug: function (e, data) {
673a7c8f
D
472 if (!kiwi.front.tabviewExists('kiwi_debug')) {
473 kiwi.front.tabviewAdd('kiwi_debug');
bad1ea63 474 }
673a7c8f 475 kiwi.front.tabviews.kiwi_debug.addMsg(null, ' ', data.msg);
a3364605 476 },
8343584e 477
a3364605 478 onAction: function (e, data) {
bad1ea63 479 var destination;
a3364605 480 // Is this message from a user?
673a7c8f 481 if (data.channel === kiwi.gateway.nick) {
a3364605
JA
482 destination = data.nick;
483 } else {
8343584e 484 destination = data.channel;
a3364605 485 }
8343584e 486
673a7c8f
D
487 if (!kiwi.front.tabviewExists(destination)) {
488 kiwi.front.tabviewAdd(destination);
bad1ea63 489 }
673a7c8f 490 kiwi.front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* ' + data.nick + ' ' + data.msg, 'action', 'color:#555;');
a3364605 491 },
8343584e 492
a3364605 493 onTopic: function (e, data) {
673a7c8f 494 if (kiwi.front.tabviewExists(data.channel)) {
8343584e 495 kiwi.front.tabviews[data.channel.toLowerCase()].changeTopic(data.topic);
a3364605
JA
496 }
497 },
8343584e 498
a3364605 499 onNotice: function (e, data) {
c6ef62a3
JA
500 var nick = (data.nick === undefined) ? '' : data.nick,
501 enick = '[' + nick + ']';
c89b9fdf 502
673a7c8f
D
503 if (kiwi.front.tabviewExists(data.target)) {
504 kiwi.front.tabviews[data.target.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
505 } else if (kiwi.front.tabviewExists(nick)) {
506 kiwi.front.tabviews[nick.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
a3364605 507 } else {
673a7c8f 508 kiwi.front.tabviews.server.addMsg(null, enick, data.msg, 'notice');
a3364605
JA
509 }
510 },
8343584e 511
423a590e
JA
512 onCTCPRequest: function (e, data) {
513 var msg = data.msg.split(" ", 2);
514 switch (msg[0]) {
515 case 'PING':
a3364605
JA
516 if (typeof msg[1] === 'undefined') {
517 msg[1] = '';
518 }
673a7c8f 519 kiwi.gateway.notice(data.nick, String.fromCharCode(1) + 'PING ' + msg[1] + String.fromCharCode(1));
423a590e 520 break;
d9e91fed 521 case 'TIME':
673a7c8f 522 kiwi.gateway.notice(data.nick, String.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String.fromCharCode(1));
423a590e
JA
523 break;
524 }
a3c7edd4 525 kiwi.front.tabviews.server.addMsg(null, 'CTCP Request', '[from ' + data.nick + '] ' + data.msg, 'ctcp');
423a590e 526 },
8343584e 527
a3364605 528 onCTCPResponse: function (e, data) {
a3c7edd4 529 kiwi.front.tabviews.server.addMsg(null, 'CTCP Reply', '[from ' + data.nick + '] ' + data.msg, 'ctcp');
423a590e 530 },
abb46d2d
D
531
532 onKiwi: function (e, data) {
533 //console.log(data);
534 },
8343584e 535
a3364605 536 onConnect: function (e, data) {
8343584e 537 var err_box, channels;
8717f02d 538
a3364605 539 if (data.connected) {
8717f02d
D
540 // Did we disconnect?
541 err_box = $('.messages .msg.error.disconnect .text');
542 if (typeof err_box[0] !== 'undefined') {
543 err_box.text('Reconnected OK :)');
544 err_box.removeClass('disconnect');
545
546 // Rejoin channels
547 channels = '';
548 $.each(kiwi.front.tabviews, function (i, tabview) {
cd81360a
D
549 if (tabview.name === 'server') {
550 return;
551 }
8717f02d
D
552 channels += tabview.name + ',';
553 });
554 console.log('Rejoining: ' + channels);
555 kiwi.gateway.join(channels);
556 return;
557 }
558
673a7c8f
D
559 if (kiwi.gateway.nick !== data.nick) {
560 kiwi.gateway.nick = data.nick;
561 kiwi.front.doLayout();
337c9866
D
562 }
563
673a7c8f 564 kiwi.front.tabviews.server.addMsg(null, ' ', '=== Connected OK :)', 'status');
a3364605 565 if (typeof init_data.channel === "string") {
673a7c8f 566 kiwi.front.joinChannel(init_data.channel);
a3364605 567 }
673a7c8f 568 kiwi.plugs.run('connect', {success: true});
a3364605 569 } else {
673a7c8f
D
570 kiwi.front.tabviews.server.addMsg(null, ' ', '=== Failed to connect :(', 'status');
571 kiwi.plugs.run('connect', {success: false});
a3364605
JA
572 }
573 },
b7be4766
D
574 onConnectFail: function (e, data) {
575 var reason = (typeof data.reason === 'string') ? data.reason : '';
673a7c8f
D
576 kiwi.front.tabviews.server.addMsg(null, '', 'There\'s a problem connecting! (' + reason + ')', 'error');
577 kiwi.plugs.run('connect', {success: false});
b7be4766 578 },
a3364605
JA
579 onDisconnect: function (e, data) {
580 var tab;
673a7c8f 581 for (tab in kiwi.front.tabviews) {
8717f02d 582 kiwi.front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error disconnect');
a3364605 583 }
673a7c8f 584 kiwi.plugs.run('disconnect', {success: false});
a3364605 585 },
8717f02d
D
586 onReconnecting: function (e, data) {
587 var err_box, f, msg;
588
589 err_box = $('.messages .msg.error.disconnect .text');
cd81360a
D
590 if (!err_box) {
591 return;
592 }
8717f02d
D
593
594 f = function (num) {
595 switch (num) {
596 case 1: return 'First';
597 case 2: return 'Second';
598 case 3: return 'Third';
599 case 4: return 'Fourth';
600 case 5: return 'Fifth';
601 case 6: return 'Sixth';
602 case 7: return 'Seventh';
cd81360a 603 default: return 'Next';
8717f02d
D
604 }
605 };
606
607 // TODO: convert seconds to mins:secs
608 msg = f(data.attempts) + ' attempt at reconnecting in ' + (data.delay / 1000).toString() + ' seconds..';
609 err_box.text(msg);
610 },
a3364605 611 onOptions: function (e, data) {
673a7c8f
D
612 if (typeof kiwi.gateway.network_name === "string" && kiwi.gateway.network_name !== "") {
613 kiwi.front.tabviews.server.tab.text(kiwi.gateway.network_name);
a3364605
JA
614 }
615 },
616 onMOTD: function (e, data) {
673a7c8f 617 kiwi.front.tabviews.server.addMsg(null, data.server, data.msg, 'motd');
a3364605
JA
618 },
619 onWhois: function (e, data) {
0c5e2aaa
JA
620 var d;
621 if (data.msg) {
673a7c8f 622 kiwi.front.cur_channel.addMsg(null, data.nick, data.msg, 'whois');
0c5e2aaa
JA
623 } else if (data.logon) {
624 d = new Date();
625 d.setTime(data.logon * 1000);
626 d = d.toLocaleString();
673a7c8f 627 kiwi.front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' second' + ((data.idle !== 1) ? 's' : '') + ', signed on ' + d, 'whois');
0c5e2aaa 628 } else {
673a7c8f 629 kiwi.front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' seconds', 'whois');
0c5e2aaa 630 }
a3364605 631 },
abb46d2d 632 onMode: function (e, data) {
8343584e
D
633 var tab;
634 if ((typeof data.channel === 'string') && (typeof data.effected_nick === 'string')) {
635 tab = kiwi.front.tabviews[data.channel.toLowerCase()];
636 tab.addMsg(null, ' ', '[' + data.mode + '] ' + data.effected_nick + ' by ' + data.nick, 'mode', '');
637 if (tab.userlist.hasUser(data.effected_nick)) {
638 tab.userlist.changeUserMode(data.effected_nick, data.mode.substr(1), (data.mode[0] === '+'));
639 }
75681d88 640 }
8343584e
D
641
642 // TODO: Other mode changes that aren't +/- qaohv. - JA
abb46d2d 643 },
a3364605 644 onUserList: function (e, data) {
8343584e
D
645 var tab;
646
647 tab = kiwi.front.tabviews[data.channel.toLowerCase()];
648 if (typeof tab === 'undefined') {
bad1ea63 649 return;
a3364605 650 }
8343584e
D
651
652 if ((!kiwi.front.cache.userlist) || (!kiwi.front.cache.userlist.updating)) {
653 if (!kiwi.front.cache.userlist) {
654 kiwi.front.cache.userlist = {updating: true};
655 } else {
656 kiwi.front.cache.userlist.updating = true;
657 }
658 tab.userlist.empty();
a3364605 659 }
8343584e
D
660
661 tab.userlist.addUser(data.users);
662
a3364605
JA
663 },
664 onUserListEnd: function (e, data) {
8343584e
D
665 if (!kiwi.front.cache.userlist) {
666 kiwi.front.cache.userlist = {};
667 }
668 kiwi.front.cache.userlist.updating = false;
a3364605 669 },
8343584e 670
b10afa2d 671 onChannelListStart: function (e, data) {
8343584e 672 /*global Utilityview */
5c7ac96f 673 var tab, table;
8343584e 674
fde6ea95 675 tab = new Utilityview('Channel List');
5c7ac96f 676 tab.div.css('overflow-y', 'scroll');
3f954378 677 table = $('<table style="margin:1em 2em;"><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>');
fde6ea95 678 tab.div.append(table);
8343584e 679
5c7ac96f
JA
680 kiwi.front.cache.list = {chans: [], tab: tab, table: table,
681 update: function (newChans) {
682 var body = this.table.children('tbody:first').detach(),
683 chan,
684 html;
8343584e 685
5c7ac96f
JA
686 html = '';
687 for (chan in newChans) {
688 this.chans.push(newChans[chan]);
689 html += newChans[chan].html;
690 }
691 body.append(html);
692 this.table.append(body);
8343584e 693
5c7ac96f
JA
694 },
695 finalise: function () {
696 var body = this.table.children('tbody:first').detach(),
697 list,
698 chan;
8343584e 699
5c7ac96f 700 list = $.makeArray($(body).children());
8343584e 701
5c7ac96f
JA
702 for (chan in list) {
703 list[chan] = $(list[chan]).detach();
704 }
8343584e 705
5c7ac96f
JA
706 list = _.sortBy(list, function (channel) {
707 return parseInt(channel.children('.num_users').first().text(), 10);
708 }).reverse();
8343584e 709
5c7ac96f
JA
710 for (chan in list) {
711 body.append(list[chan]);
712 }
8343584e 713
5c7ac96f 714 this.table.append(body);
8343584e 715
5c7ac96f
JA
716 }};
717 },
718 onChannelList: function (e, data) {
719 var chans;
5c7ac96f
JA
720 data = data.chans;
721 //data = [data];
722 for (chans in data) {
723 data[chans] = {data: data[chans], html: '<tr><td><a class="chan">' + data[chans].channel + '</a></td><td class="num_users" style="text-align: center;">' + data[chans].num_users + '</td><td style="padding-left: 2em;">' + kiwi.front.format(data[chans].topic) + '</td></tr>'};
724 }
725 kiwi.front.cache.list.update(data);
726 },
727 onChannelListEnd: function (e, data) {
728 kiwi.front.cache.list.finalise();
729 kiwi.front.cache.list.tab.show();
b10afa2d
D
730 },
731
732
a3364605 733 onJoin: function (e, data) {
673a7c8f
D
734 if (!kiwi.front.tabviewExists(data.channel)) {
735 kiwi.front.tabviewAdd(data.channel.toLowerCase());
a3364605 736 }
8343584e 737
8717f02d
D
738 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action join', 'color:#009900;');
739
673a7c8f 740 if (data.nick === kiwi.gateway.nick) {
bad1ea63
JA
741 return; // Not needed as it's already in nicklist
742 }
8717f02d 743
8343584e 744 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.addUser({nick: data.nick, modes: []});
a3364605
JA
745 },
746 onPart: function (e, data) {
673a7c8f 747 if (kiwi.front.tabviewExists(data.channel)) {
a3364605 748 // If this is us, close the tabview
673a7c8f
D
749 if (data.nick === kiwi.gateway.nick) {
750 kiwi.front.tabviews[data.channel.toLowerCase()].close();
751 kiwi.front.tabviews.server.show();
a3364605
JA
752 return;
753 }
8343584e 754
673a7c8f 755 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.nick + ' has left (' + data.message + ')', 'action part', 'color:#990000;');
8343584e 756 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.removeUser(data.nick);
a3364605
JA
757 }
758 },
759 onKick: function (e, data) {
673a7c8f 760 if (kiwi.front.tabviewExists(data.channel)) {
a3364605 761 // If this is us, close the tabview
673a7c8f
D
762 if (data.kicked === kiwi.gateway.nick) {
763 //kiwi.front.tabviews[data.channel.toLowerCase()].close();
764 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data.channel + '. ' + data.message, 'status kick');
765 kiwi.front.tabviews[data.channel.toLowerCase()].safe_to_close = true;
8343584e 766 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.remove();
a3364605
JA
767 return;
768 }
8343584e 769
673a7c8f 770 kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.kicked + ' kicked by ' + data.nick + '(' + data.message + ')', 'action kick', 'color:#990000;');
8343584e 771 kiwi.front.tabviews[data.channel.toLowerCase()].userlist.removeUser(data.nick);
a3364605
JA
772 }
773 },
774 onNick: function (e, data) {
673a7c8f
D
775 if (data.nick === kiwi.gateway.nick) {
776 kiwi.gateway.nick = data.newnick;
777 kiwi.front.doLayout();
a3364605 778 }
8343584e 779
673a7c8f
D
780 $.each(kiwi.front.tabviews, function (i, item) {
781 $.each(kiwi.front.tabviews, function (i, item) {
8343584e
D
782 if (item.userlist.hasUser(data.nick)) {
783 item.userlist.renameUser(data.nick, data.newnick);
784 item.addMsg(null, ' ', '=== ' + data.nick + ' is now known as ' + data.newnick, 'action changenick');
785 }
a3364605
JA
786 });
787 });
788 },
789 onQuit: function (e, data) {
673a7c8f
D
790 $.each(kiwi.front.tabviews, function (i, item) {
791 $.each(kiwi.front.tabviews, function (i, item) {
8343584e
D
792 if (item.userlist.hasUser(data.nick)) {
793 item.userlist.removeUser(data.nick);
794 item.addMsg(null, ' ', '<-- ' + data.nick + ' has quit (' + data.message + ')', 'action quit', 'color:#990000;');
795 }
a3364605
JA
796 });
797 });
798 },
799 onChannelRedirect: function (e, data) {
673a7c8f
D
800 kiwi.front.tabviews[data.from.toLowerCase()].close();
801 kiwi.front.tabviewAdd(data.to.toLowerCase());
802 kiwi.front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data.from, 'action');
a3364605 803 },
8343584e 804
81122538 805 onIRCError: function (e, data) {
a3364605 806 var t_view;
673a7c8f 807 if (data.channel !== undefined && kiwi.front.tabviewExists(data.channel)) {
a3364605
JA
808 t_view = data.channel;
809 } else {
810 t_view = 'server';
811 }
812
813 switch (data.error) {
81122538 814 case 'banned_from_channel':
673a7c8f 815 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== You are banned from ' + data.channel + '. ' + data.reason, 'status');
a3364605 816 if (t_view !== 'server') {
673a7c8f 817 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 818 }
81122538
JA
819 break;
820 case 'bad_channel_key':
673a7c8f 821 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== Bad channel key for ' + data.channel, 'status');
a3364605 822 if (t_view !== 'server') {
673a7c8f 823 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 824 }
81122538
JA
825 break;
826 case 'invite_only_channel':
673a7c8f 827 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is invite only.', 'status');
a3364605 828 if (t_view !== 'server') {
673a7c8f 829 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 830 }
81122538
JA
831 break;
832 case 'channel_is_full':
673a7c8f 833 kiwi.front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is full.', 'status');
a3364605 834 if (t_view !== 'server') {
673a7c8f 835 kiwi.front.tabviews[t_view].safe_to_close = true;
a3364605 836 }
81122538 837 break;
818ef327 838 case 'chanop_privs_needed':
673a7c8f 839 kiwi.front.tabviews[data.channel].addMsg(null, ' ', '=== ' + data.reason, 'status');
818ef327
JA
840 break;
841 case 'no_such_nick':
8343584e 842 kiwi.front.tabviews.server.addMsg(null, ' ', '=== ' + data.nick + ': ' + data.reason, 'status');
818ef327 843 break;
337c9866 844 case 'nickname_in_use':
673a7c8f 845 kiwi.front.tabviews.server.addMsg(null, ' ', '=== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');
8717f02d 846 kiwi.front.showChangeNick('That nick is already taken');
337c9866 847 break;
81122538 848 default:
a3364605 849 // We don't know what data contains, so don't do anything with it.
673a7c8f 850 //kiwi.front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
81122538
JA
851 }
852 },
8343584e 853
a3364605 854 registerKeys: function () {
458d2808 855 var tabcomplete = {active: false, data: [], prefix: ''};
a3364605 856 $('#kiwi_msginput').bind('keydown', function (e) {
8343584e 857 var windows, meta, num, msg, data, self;
a3364605
JA
858 windows = $('#windows');
859 //var meta = e.altKey;
860 meta = e.ctrlKey;
8343584e 861
458d2808
JA
862 if (e.which !== 9) {
863 tabcomplete.active = false;
864 tabcomplete.data = [];
865 tabcomplete.prefix = '';
866 }
8343584e 867
a3364605
JA
868 switch (true) {
869 case (e.which >= 48) && (e.which <= 57):
870 if (meta) {
871 num = e.which - 48;
872 if (num === 0) {
873 num = 10;
874 }
875 num = num - 1;
673a7c8f 876 kiwi.front.windowsShow(num);
a3364605
JA
877 return false;
878 }
879 break;
880 case e.which === 27: // escape
881 return false;
882 case e.which === 13: // return
883 msg = $('#kiwi_msginput').val();
884 msg = msg.trim();
8343584e 885
673a7c8f
D
886 kiwi.front.buffer.push(msg);
887 kiwi.front.buffer_pos = kiwi.front.buffer.length;
8343584e 888
673a7c8f 889 kiwi.front.run(msg);
a3364605 890 $('#kiwi_msginput').val('');
8343584e 891
a3364605
JA
892 break;
893 case e.which === 33: // page up
894 console.log("page up");
895 windows[0].scrollTop = windows[0].scrollTop - windows.height();
896 return false;
897 case e.which === 34: // page down
898 windows[0].scrollTop = windows[0].scrollTop + windows.height();
899 return false;
900 case e.which === 37: // left
901 if (meta) {
673a7c8f 902 kiwi.front.windowsPrevious();
a3364605
JA
903 return false;
904 }
905 break;
906 case e.which === 38: // up
673a7c8f
D
907 if (kiwi.front.buffer_pos > 0) {
908 kiwi.front.buffer_pos--;
909 $('#kiwi_msginput').val(kiwi.front.buffer[kiwi.front.buffer_pos]);
a3364605
JA
910 }
911 break;
912 case e.which === 39: // right
913 if (meta) {
673a7c8f 914 kiwi.front.windowsNext();
a3364605
JA
915 return false;
916 }
917 break;
918 case e.which === 40: // down
673a7c8f
D
919 if (kiwi.front.buffer_pos < kiwi.front.buffer.length) {
920 kiwi.front.buffer_pos++;
921 $('#kiwi_msginput').val(kiwi.front.buffer[kiwi.front.buffer_pos]);
a3364605
JA
922 }
923 break;
8343584e 924
a3364605 925 case e.which === 9: // tab
458d2808
JA
926 tabcomplete.active = true;
927 if (_.isEqual(tabcomplete.data, [])) {
928 // Get possible autocompletions
929 data = [];
8343584e 930 kiwi.front.cur_channel.userlist.listUsers(false).each(function () {
458d2808
JA
931 var nick;
932 nick = kiwi.front.nickStripPrefix($('a.nick', this).text());
933 data.push(nick);
934 });
935 data = _.sortBy(data, function (nick) {
936 return nick;
937 });
938 tabcomplete.data = data;
939 }
8343584e 940
458d2808
JA
941 if (this.value[this.selectionStart - 1] === ' ') {
942 return false;
943 }
944 self = this;
945 (function () {
946 var tokens = self.value.substring(0, self.selectionStart).split(" "),
947 val,
948 p1,
949 newnick,
8343584e
D
950 range,
951 nick = tokens[tokens.length - 1];
458d2808
JA
952 if (tabcomplete.prefix === '') {
953 tabcomplete.prefix = nick;
a3364605 954 }
8343584e 955
458d2808
JA
956 tabcomplete.data = _.select(tabcomplete.data, function (n) {
957 return (n.toLowerCase().indexOf(tabcomplete.prefix.toLowerCase()) === 0);
958 });
8343584e 959
458d2808
JA
960 if (tabcomplete.data.length > 0) {
961 p1 = self.selectionStart - (nick.length);
962 val = self.value.substr(0, p1);
963 newnick = tabcomplete.data.shift();
964 tabcomplete.data.push(newnick);
965 val += newnick;
966 val += self.value.substr(self.selectionStart);
967 self.value = val;
968 if (self.setSelectionRange) {
969 self.setSelectionRange(p1 + newnick.length, p1 + newnick.length);
970 } else if (self.createTextRange) { // not sure if this bit is actually needed....
971 range = self.createTextRange();
972 range.collapse(true);
973 range.moveEnd('character', p1 + newnick.length);
974 range.moveStart('character', p1 + newnick.length);
975 range.select();
976 }
a3364605 977 }
458d2808 978 }());
a3364605
JA
979 return false;
980 }
981 });
8343584e
D
982
983
a3364605 984 $('#kiwi .control .msginput .nick').click(function () {
8343584e 985 kiwi.front.showChangeNick();
a3364605 986 });
8343584e
D
987
988
989
990
991
a3364605 992 $('#kiwi .plugins .load_plugin_file').click(function () {
673a7c8f 993 if (typeof kiwi.front.boxes.plugins !== "undefined") {
bad1ea63
JA
994 return;
995 }
8343584e 996
673a7c8f
D
997 kiwi.front.boxes.plugins = new Box("plugin_file");
998 $('#tmpl_plugins').tmpl({}).appendTo(kiwi.front.boxes.plugins.content);
999 kiwi.front.boxes.plugins.box.css('top', -(kiwi.front.boxes.plugins.height + 40));
8343584e 1000
a3364605 1001 // Populate the plugin list..
c6ef62a3
JA
1002 function enumPlugins() {
1003 var lst, j, txt;
4528138d
D
1004 lst = $('#plugin_list');
1005 lst.find('option').remove();
673a7c8f
D
1006 for (j in kiwi.plugs.loaded) {
1007 txt = kiwi.plugs.loaded[j].name;
4528138d
D
1008 lst.append('<option value="' + txt + '">' + txt + '</option>');
1009 }
a3364605 1010 }
4528138d 1011 enumPlugins();
8343584e 1012
a3364605
JA
1013 // Event bindings
1014 $('#kiwi .plugin_file').submit(function () {
c6ef62a3 1015 $('<div></div>').load($('.txtpluginfile').val(), function (e) {
4528138d 1016 enumPlugins();
a3364605
JA
1017 });
1018 return false;
1019 });
1020 $('#kiwi .cancelpluginfile').click(function () {
673a7c8f 1021 kiwi.front.boxes.plugins.destroy();
a3364605 1022 });
8343584e 1023
a3364605 1024 $('#kiwi #plugins_list_unload').click(function () {
4528138d 1025 var selected_plugin;
a3364605 1026 selected_plugin = $('#plugin_list').val();
673a7c8f 1027 kiwi.plugs.unloadPlugin(selected_plugin);
4528138d 1028 enumPlugins();
a3364605 1029 });
8343584e 1030
a3364605 1031 $('#kiwi .txtpluginfile').focus();
8343584e 1032
a3364605 1033 });
8343584e 1034
a3364605
JA
1035 $('#kiwi .plugins .reload_css').click(function () {
1036 var links = document.getElementsByTagName("link"),
1037 i;
1038 for (i = 0; i < links.length; i++) {
1039 if (links[i].rel === "stylesheet") {
1040 if (links[i].href.indexOf("?") === -1) {
1041 links[i].href += "?";
1042 }
1043 links[i].href += "x";
1044 }
1045 }
1046 });
1047
1048
1049 $('#kiwi .about .about_close').click(function () {
1050 $('#kiwi .about').css('display', 'none');
1051 });
8343584e
D
1052
1053
a3364605
JA
1054 $('#kiwi .poweredby').click(function () {
1055 $('#kiwi .about').css('display', 'block');
1056 });
8343584e 1057
a3364605 1058 },
8343584e
D
1059
1060
8717f02d
D
1061 showChangeNick: function (caption) {
1062 caption = (typeof caption !== 'undefined') ? caption : '';
1063
a3364605 1064 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
8343584e 1065
8717f02d
D
1066 $('#kiwi .newnick .caption').text(caption);
1067
a3364605 1068 $('#kiwi .form_newnick').submit(function () {
673a7c8f 1069 kiwi.front.run('/NICK ' + $('#kiwi .txtnewnick').val());
a3364605
JA
1070 $('#kiwi .newnick').remove();
1071 return false;
1072 });
8343584e 1073
a3364605
JA
1074 $('#kiwi .txtnewnick').keypress(function (ev) {
1075 if (!this.first_press) {
1076 this.first_press = true;
1077 return false;
1078 }
1079 });
1080
1081 $('#kiwi .txtnewnick').keydown(function (ev) {
1082 if (ev.which === 27) { // ESC
1083 $('#kiwi_msginput').focus();
1084 $('#kiwi .newnick').remove();
1085 }
1086 });
8343584e 1087
a3364605
JA
1088 $('#kiwi .cancelnewnick').click(function () {
1089 $('#kiwi .newnick').remove();
1090 });
8343584e 1091
a3364605
JA
1092 $('#kiwi .txtnewnick').focus();
1093 },
1094
1095
1096 tabviewExists: function (name) {
673a7c8f 1097 return (typeof kiwi.front.tabviews[name.toLowerCase()] !== 'undefined');
a3364605 1098 },
8343584e 1099
a3364605 1100 tabviewAdd: function (v_name) {
8343584e 1101 /*global Tabview, UserList */
875d0c71
D
1102 var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname, userlist_enabled = true;
1103
673a7c8f
D
1104 if (v_name.charAt(0) === kiwi.gateway.channel_prefix) {
1105 re = new RegExp(kiwi.gateway.channel_prefix, "g");
a3364605
JA
1106 htmlsafe_name = v_name.replace(re, 'pre');
1107 htmlsafe_name = "chan_" + htmlsafe_name;
1108 } else {
1109 htmlsafe_name = 'query_' + v_name;
875d0c71 1110 userlist_enabled = false;
a3364605 1111 }
8343584e 1112
a3364605
JA
1113 tmp_divname = 'kiwi_window_' + htmlsafe_name;
1114 tmp_userlistname = 'kiwi_userlist_' + htmlsafe_name;
1115 tmp_tabname = 'kiwi_tab_' + htmlsafe_name;
8343584e 1116
673a7c8f 1117 if (!kiwi.front.tabviewExists(v_name)) {
a3364605 1118 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>');
8343584e 1119 //$('#kiwi .userlist').append('<ul id="' + tmp_userlistname + '"></ul>');
673a7c8f 1120 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname + '" onclick="kiwi.front.tabviews[\'' + v_name.toLowerCase() + '\'].show();">' + v_name + '</li>');
a3364605 1121 }
673a7c8f
D
1122 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ kiwi.front.windowShow(v_name); });
1123 //kiwi.front.windowShow(v_name);
8343584e 1124
673a7c8f
D
1125 kiwi.front.tabviews[v_name.toLowerCase()] = new Tabview();
1126 kiwi.front.tabviews[v_name.toLowerCase()].name = v_name;
1127 kiwi.front.tabviews[v_name.toLowerCase()].div = $('#' + tmp_divname);
8343584e 1128 kiwi.front.tabviews[v_name.toLowerCase()].userlist = new UserList(htmlsafe_name);
673a7c8f 1129 kiwi.front.tabviews[v_name.toLowerCase()].tab = $('#' + tmp_tabname);
c6ef62a3 1130 if (!userlist_enabled) {
8343584e 1131 kiwi.front.tabviews[v_name.toLowerCase()].userlist.setWidth(0);
c6ef62a3 1132 }
673a7c8f 1133 kiwi.front.tabviews[v_name.toLowerCase()].show();
8343584e 1134
a3364605
JA
1135 if (typeof registerTouches === "function") {
1136 //alert("Registering touch interface");
1137 //registerTouches($('#'+tmp_divname));
1138 registerTouches(document.getElementById(tmp_divname));
1139 }
1140 /*
673a7c8f 1141 kiwi.front.tabviews[v_name.toLowerCase()].userlist.click(function(){
a3364605
JA
1142 alert($(this).attr('id'));
1143 });
1144 */
1145
673a7c8f 1146 kiwi.front.doLayoutSize();
a3364605 1147 },
ef885bff 1148
ef885bff 1149
a3364605 1150 sync: function () {
673a7c8f 1151 kiwi.gateway.sync();
a3364605 1152 },
8343584e 1153
a3364605
JA
1154 onSync: function (e, data) {
1155 // Set any settings
1156 if (data.nick !== undefined) {
673a7c8f 1157 kiwi.gateway.nick = data.nick;
a3364605 1158 }
8343584e 1159
a3364605
JA
1160 // Add the tabviews
1161 if (data.tabviews !== undefined) {
1162 $.each(data.tabviews, function (i, tab) {
673a7c8f
D
1163 if (!kiwi.front.tabviewExists(tab.name)) {
1164 kiwi.front.tabviewAdd(kiwi.gateway.channel_prefix + tab.name);
8343584e 1165
a3364605 1166 if (tab.userlist !== undefined) {
8343584e 1167 kiwi.front.onUserList({'channel': kiwi.gateway.channel_prefix + tab.name, 'users': tab.userlist.getUsers(false)});
a3364605
JA
1168 }
1169 }
1170 });
1171 }
8343584e 1172
673a7c8f 1173 kiwi.front.doLayout();
a3364605 1174 },
8343584e
D
1175
1176
a3364605 1177 setTopicText: function (new_topic) {
673a7c8f 1178 kiwi.front.original_topic = new_topic;
ec770c28 1179 $('#kiwi .cur_topic .topic').text(new_topic);
673a7c8f 1180 kiwi.front.doLayoutSize();
a3364605 1181 },
8343584e 1182
a3364605 1183 nickStripPrefix: function (nick) {
8343584e
D
1184 var tmp = nick, i, j, k;
1185 i = 0;
1186 for (j = 0; j < nick.length; j++) {
1187 for (k = 0; k < kiwi.gateway.user_prefixes.length; k++) {
1188 if (nick.charAt(j) === kiwi.gateway.user_prefixes[k].symbol) {
1189 i++;
1190 break;
1191 }
a3364605 1192 }
a3364605
JA
1193 }
1194
8343584e 1195 return tmp.substr(i);
a3364605 1196 },
8343584e 1197
a3364605 1198 nickGetPrefix: function (nick) {
8343584e
D
1199 var tmp = nick, i, j, k;
1200 i = 0;
1201 for (j = 0; j < nick.length; j++) {
1202 for (k = 0; k < kiwi.gateway.user_prefixes.length; k++) {
1203 if (nick.charAt(j) === kiwi.gateway.user_prefixes[k].symbol) {
1204 i++;
1205 break;
1206 }
a3364605
JA
1207 }
1208 }
1209
8343584e 1210 return tmp.substr(0, i);
a3364605 1211 },
8343584e 1212
a3364605 1213 isChannel: function (name) {
c6ef62a3 1214 var prefix, is_chan;
a3364605 1215 prefix = name.charAt(0);
673a7c8f 1216 if (kiwi.gateway.channel_prefix.indexOf(prefix) > -1) {
a3364605
JA
1217 is_chan = true;
1218 } else {
1219 is_chan = false;
1220 }
8343584e 1221
a3364605
JA
1222 return is_chan;
1223 },
8343584e 1224
a3364605
JA
1225 tabviewsNext: function () {
1226 var wl = $('#kiwi .windowlist ul'),
1227 next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) + 170;
1228 wl.css('text-indent', next_left);
1229 },
8343584e 1230
a3364605
JA
1231 tabviewsPrevious: function () {
1232 var wl = $('#kiwi .windowlist ul'),
1233 next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) - 170;
1234 wl.css('text-indent', next_left);
1235 },
1236
1237 windowsNext: function () {
1238 var tab, next;
1239 next = false;
673a7c8f 1240 for (tab in kiwi.front.tabviews) {
a3364605 1241 if (!next) {
673a7c8f 1242 if (kiwi.front.tabviews[tab] === kiwi.front.cur_channel) {
a3364605
JA
1243 next = true;
1244 continue;
1245 }
1246 } else {
673a7c8f 1247 kiwi.front.tabviews[tab].show();
a3364605
JA
1248 return;
1249 }
1250 }
1251 },
1252
1253 windowsPrevious: function () {
1254 var tab, prev_tab, next;
1255 next = false;
673a7c8f
D
1256 for (tab in kiwi.front.tabviews) {
1257 if (kiwi.front.tabviews[tab] === kiwi.front.cur_channel) {
a3364605
JA
1258 if (prev_tab) {
1259 prev_tab.show();
1260 }
1261 return;
1262 }
673a7c8f 1263 prev_tab = kiwi.front.tabviews[tab];
a3364605
JA
1264 }
1265 },
1266
1267 windowsShow: function (num) {
1268 num = parseInt(num, 10);
1269 console.log('Showing window ' + num.toString());
1270 var i = 0, tab;
673a7c8f 1271 for (tab in kiwi.front.tabviews) {
a3364605 1272 if (i === num) {
673a7c8f 1273 kiwi.front.tabviews[tab].show();
a3364605
JA
1274 return;
1275 }
1276 i++;
1277 }
c432172c
D
1278 },
1279
1280
1281
1282 barsShow: function () {
1283 $('#kiwi .toolbars').slideDown();
1284 $('#kiwi .control').slideDown();
1285 },
1286
1287 barsHide: function () {
1288 $('#kiwi .toolbars').slideUp();
1289 $('#kiwi .control').slideUp();
24552b4b 1290 },
8343584e 1291
24552b4b 1292 format: function (msg) {
8343584e
D
1293 var re, next;
1294
9d34a1ca
JA
1295 if ((!msg) || (typeof msg !== 'string')) {
1296 return;
1297 }
8343584e 1298
24552b4b
JA
1299 // bold
1300 if (msg.indexOf(String.fromCharCode(2)) !== -1) {
1301 next = '<b>';
1302 while (msg.indexOf(String.fromCharCode(2)) !== -1) {
1303 msg = msg.replace(String.fromCharCode(2), next);
1304 next = (next === '<b>') ? '</b>' : '<b>';
1305 }
1306 if (next === '</b>') {
1307 msg = msg + '</b>';
1308 }
1309 }
8343584e 1310
24552b4b
JA
1311 // underline
1312 if (msg.indexOf(String.fromCharCode(31)) !== -1) {
1313 next = '<u>';
1314 while (msg.indexOf(String.fromCharCode(31)) !== -1) {
1315 msg = msg.replace(String.fromCharCode(31), next);
1316 next = (next === '<u>') ? '</u>' : '<u>';
1317 }
1318 if (next === '</u>') {
1319 msg = msg + '</u>';
1320 }
1321 }
8343584e 1322
24552b4b
JA
1323 // colour
1324 re = /\x03([0-9][0-5]?)(,([0-9][0-5]?))?(.*?)\x03/g;
8343584e 1325
24552b4b
JA
1326 msg = msg.replace(re, function (str, p1, p2, p3, p4) {
1327 var fg, bg,
1328 col = function (num) {
1329 switch (parseInt(num, 10)) {
1330 case 0:
1331 return '#FFFFFF';
1332 case 1:
1333 return '#000000';
1334 case 2:
1335 return '#000080';
1336 case 3:
1337 return '#008000';
1338 case 4:
1339 return '#FF0000';
1340 case 5:
1341 return '#800040';
1342 case 6:
1343 return '#800080';
1344 case 7:
1345 return '#FF8040';
1346 case 8:
1347 return '#FFFF00';
1348 case 9:
1349 return '#80FF00';
1350 case 10:
1351 return '#008080';
1352 case 11:
1353 return '#00FFFF';
1354 case 12:
1355 return '#0000FF';
1356 case 13:
1357 return '#FF55FF';
1358 case 14:
1359 return '#808080';
1360 case 15:
1361 return '#C0C0C0';
1362 default:
1363 return null;
1364 }
1365 };
1366 fg = col(p1);
1367 bg = col(p3);
5c7ac96f 1368 return '<span style="' + ((fg !== null) ? 'color: ' + fg + '; ' : '') + ((bg !== null) ? 'background-color: ' + bg + ';' : '') + '">' + p4 + '</span>';
24552b4b
JA
1369 });
1370 return msg;
a3364605 1371 }
8343584e 1372
a3364605 1373};
54f4a22e
D
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
8343584e
D
1384var UserList = function (name) {
1385 var userlist, list_html, sortUsers, sortModes, getPrefix;
1386
1387 userlist = [];
1388
1389 $('#kiwi .userlist').append($('<ul id="kiwi_userlist_' + name + '"></ul>'));
1390 list_html = $('#kiwi_userlist_' + name);
1391 $('a.nick', list_html[0]).live('click', this.clickHandler);
1392
1393 sortUsers = function () {
1394 var parent;
1395 parent = list_html.parent();
1396 list_html = list_html.detach();
1397
1398 // Not sure this is needed.
1399 // It's O(n^2) as well, so need to test to see if it works without.
1400 // Alternative to test: list_html.children('li').detach();
1401 list_html.children().each(function (child) {
1402 var i, nick;
1403 child = $(child);
1404 nick = child.data('nick');
1405 for (i = 0; i < userlist.length; i++) {
1406 if (userlist[i].nick === nick) {
1407 userlist[i].html = child.detach();
1408 break;
1409 }
1410 }
1411 });
1412
1413 userlist.sort(function (a, b) {
1414 var i, a_idx, b_idx, a_nick, b_nick;
1415 // Try to sort by modes first
1416 if (a.modes.length > 0) {
1417 // a has modes, but b doesn't so a should appear first
1418 if (b.modes.length === 0) {
1419 return -1;
1420 }
1421 a_idx = b_idx = -1;
1422 // Compare the first (highest) mode
1423 for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) {
1424 if (kiwi.gateway.user_prefixes[i].mode === a.modes[0]) {
1425 a_idx = i;
1426 }
1427 }
1428 for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) {
1429 if (kiwi.gateway.user_prefixes[i].mode === b.modes[0]) {
1430 b_idx = i;
1431 }
1432 }
1433 if (a_idx < b_idx) {
1434 return -1;
1435 } else if (a_idx > b_idx) {
1436 return 1;
1437 }
1438 // If we get to here both a and b have the same highest mode so have to resort to lexicographical sorting
1439
1440 } else if (b.modes.length > 0) {
1441 // b has modes but a doesn't so b should appear first
1442 return 1;
1443 }
1444 a_nick = a.nick.toLocaleUpperCase();
1445 b_nick = b.nick.toLocaleUpperCase();
1446 // Lexicographical sorting
1447 if (a_nick < b_nick) {
1448 return -1;
1449 } else if (a_nick > b_nick) {
1450 return 1;
1451 } else {
1452 // This should never happen; both users have the same nick.
1453 console.log('Something\'s gone wrong somewhere - two users have the same nick!');
1454 return 0;
1455 }
1456 });
1457 _.each(userlist, function (user) {
1458 user.html = user.html.appendTo(list_html);
1459 });
54f4a22e 1460
8343584e
D
1461 list_html = list_html.appendTo(parent);
1462 };
1463
1464 sortModes = function (modes) {
1465 return modes.sort(function (a, b) {
1466 var a_idx, b_idx, i;
1467 for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) {
1468 if (kiwi.gateway.user_prefixes[i].mode === a) {
1469 a_idx = i;
1470 }
1471 }
1472 for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) {
1473 if (kiwi.gateway.user_prefixes[i].mode === b) {
1474 b_idx = i;
1475 }
1476 }
1477 if (a_idx < b_idx) {
1478 return -1;
1479 } else if (a_idx > b_idx) {
1480 return 1;
1481 } else {
1482 return 0;
1483 }
1484 });
1485 };
1486
1487 getPrefix = function (modes) {
1488 var prefix = '';
1489 if (typeof modes[0] !== 'undefined') {
1490 prefix = _.detect(kiwi.gateway.user_prefixes, function (prefix) {
1491 return prefix.mode === modes[0];
1492 });
1493 prefix = (prefix) ? prefix.symbol : '';
1494 }
1495 return prefix;
1496 };
1497
1498 this.addUser = function (users) {
1499 if (!_.isArray(users)) {
1500 users = [users];
1501 }
1502 _.each(users, function (user) {
1503 var html, prefix = '';
1504 user.nick = kiwi.front.nickStripPrefix(user.nick);
1505 user.modes = sortModes(user.modes);
1506 if (typeof user.modes[0] !== 'undefined') {
1507 prefix = _.detect(kiwi.gateway.user_prefixes, function (prefix) {
1508 return prefix.mode === user.modes[0];
1509 });
1510 prefix = (prefix) ? prefix.symbol : '';
1511 }
1512 html = $('<li><a class="nick">' + prefix + user.nick + '</a></li>');
1513 userlist.push({nick: user.nick, modes: user.modes, html: html});
1514 });
1515 sortUsers();
1516
1517 return this;
1518 };
1519
1520 this.removeUser = function (nicks) {
1521 var toRemove;
1522 if (!_.isArray(nicks)) {
1523 nicks = [nicks];
1524 }
1525 toRemove = _.select(userlist, function (user) {
1526 return _.any(nicks, function (n) {
1527 return n === user.nick;
1528 });
1529 });
1530
1531 _.each(toRemove, function (user) {
1532 user.html.remove();
1533 });
1534
1535 userlist = _.difference(userlist, toRemove);
1536
1537 return this;
1538 };
1539
1540 this.renameUser = function (oldNick, newNick) {
1541 var user = _.detect(userlist, function (u) {
1542 return u.nick === oldNick;
1543 });
1544 if (user) {
1545 user.nick = newNick;
1546 user.html.text(getPrefix(user.modes) + newNick);
1547 }
1548
1549 sortUsers();
1550
1551 return this;
1552 };
1553
1554 this.listUsers = function (modesort, modes) {
1555 var users = userlist;
1556 if (modes) {
1557 users = _.select(users, function (user) {
1558 return _.any(modes, function (m) {
1559 return _.any(user.modes, function (um) {
1560 return m === um;
1561 });
1562 });
1563 });
1564 }
1565 if ((modesort === true) || (typeof modesort === undefined)) {
1566 return users;
1567 } else {
1568 return _.sortBy(users, function (user) {
1569 return user.nick;
1570 });
1571 }
1572 };
1573
1574 this.remove = function () {
1575 list_html.remove();
1576 list_html = null;
1577 userlist = null;
1578 };
1579
1580 this.empty = function () {
1581 list_html.children().remove();
1582 userlist = [];
1583
1584 return this;
1585 };
1586
1587 this.hasUser = function (nick) {
1588 return _.any(userlist, function (user) {
1589 return user.nick === nick;
1590 });
1591 };
1592
1593 this.active = function (active) {
1594 if ((arguments.length === 0) || (active)) {
1595 list_html.addClass('active');
1596 list_html.show();
1597 } else {
1598 list_html.removeClass('active');
1599 list_html.hide();
1600 }
1601
1602 return this;
1603 };
1604
1605 this.changeUserMode = function (nick, mode, add) {
1606 var user;
1607 if (this.hasUser(nick)) {
1608 user = _.detect(userlist, function (u) {
1609 return u.nick === nick;
1610 });
1611
1612 if ((arguments.length < 3) || (add)) {
1613 user.modes.push(mode);
1614 } else {
1615 user.modes = _.reject(user.modes, function (m) {
1616 return m === mode;
1617 });
1618 }
1619 user.modes = sortModes(user.modes);
1620 user.html.children('a:first').text(getPrefix(user.modes) + user.nick);
1621 sortUsers();
1622 }
1623
1624 return this;
1625 };
1626};
1627UserList.prototype.width = null;
1628UserList.prototype.setWidth = function (newWidth) {
1629 var w, u;
1630 if (typeof newWidth === 'Number') {
1631 this.width = newWidth;
1632
1633 w = $('#windows');
1634 u = $('#kiwi .userlist');
1635
1636 // Set the window size accordingly
1637 if (this.width > 0) {
1638 u.width(this.width);
1639 w.css('right', u.outerWidth(true));
1640 } else {
1641 w.css('right', 0);
1642 }
1643 }
1644
1645 return this;
1646};
1647
1648UserList.prototype.clickHandler = function () {
1649 var li = $(this).parent();
1650
1651 // Remove any existing userboxes
1652 $('#kiwi .userbox').remove();
1653
1654 if ($(li).data('userbox') === this) {
1655 $(li).removeData('userbox');
1656 } else {
1657 $('#tmpl_user_box').tmpl({nick: kiwi.front.nickStripPrefix($(this).text())}).appendTo(li);
1658
1659 $('#kiwi .userbox .userbox_query').click(function (ev) {
1660 var nick = $('#kiwi .userbox_nick').val();
1661 kiwi.front.run('/query ' + nick);
1662 });
1663
1664 $('#kiwi .userbox .userbox_whois').click(function (ev) {
1665 var nick = $('#kiwi .userbox_nick').val();
1666 kiwi.front.run('/whois ' + nick);
1667 });
1668 $(li).data('userbox', this);
1669 }
1670};
54f4a22e
D
1671
1672
1673
1674
1675
1bb74900
D
1676/*
1677 * MISC VIEW
1678 */
1679
604c5174 1680var Utilityview = function (name) {
c6ef62a3
JA
1681 var rand_name = randomString(15),
1682 tmp_divname = 'kiwi_window_' + rand_name,
c6ef62a3 1683 tmp_tabname = 'kiwi_tab_' + rand_name;
8343584e 1684
604c5174
D
1685 this.name = rand_name;
1686 this.title = name;
1687 this.topic = ' ';
4e4ca8e6 1688 this.panel = $('#panel1');
1bb74900 1689
4e4ca8e6
D
1690 if (typeof $('.scroller', this.panel)[0] === 'undefined') {
1691 this.panel.append('<div id="' + tmp_divname + '" class="messages"></div>');
1692 } else {
1693 $('.scroller', this.panel).append('<div id="' + tmp_divname + '" class="messages"></div>');
1694 }
604c5174
D
1695
1696 this.tab = $('<li id="' + tmp_tabname + '">' + this.title + '</li>');
c6ef62a3 1697 this.tab.click(function () {
673a7c8f 1698 kiwi.front.utilityviews[rand_name.toLowerCase()].show();
604c5174
D
1699 });
1700 $('#kiwi .utilityviewlist ul').append(this.tab);
3b5ccd71 1701 kiwi.front.doLayoutSize();
8343584e 1702
1bb74900
D
1703 this.div = $('#' + tmp_divname);
1704 this.div.css('overflow', 'hidden');
1705
673a7c8f 1706 kiwi.front.utilityviews[rand_name.toLowerCase()] = this;
1bb74900
D
1707};
1708
1709Utilityview.prototype.name = null;
604c5174 1710Utilityview.prototype.title = null;
1bb74900
D
1711Utilityview.prototype.div = null;
1712Utilityview.prototype.tab = null;
ca7e63ea 1713Utilityview.prototype.topic = ' ';
4e4ca8e6 1714Utilityview.prototype.panel = null;
1bb74900 1715Utilityview.prototype.show = function () {
4e4ca8e6 1716 $('.messages', this.panel).removeClass("active");
1bb74900 1717 $('#kiwi .userlist ul').removeClass("active");
2b9dcc03 1718 $('#kiwi .toolbars ul li').removeClass("active");
1bb74900 1719
4e4ca8e6 1720 this.panel.css('overflow-y', 'hidden');
2b9dcc03 1721 $('#windows').css('right', 0);
1bb74900
D
1722 // Activate this tab!
1723 this.div.addClass('active');
1724 this.tab.addClass('active');
1725
1726 this.addPartImage();
1727
673a7c8f
D
1728 kiwi.front.setTopicText(this.topic);
1729 kiwi.front.cur_channel = this;
1bb74900
D
1730
1731 // If we're using fancy scrolling, refresh it
1732 if (touch_scroll) {
1733 touch_scroll.refresh();
1734 }
c6ef62a3 1735};
1bb74900 1736
4e4ca8e6
D
1737Utilityview.prototype.setPanel = function (new_panel) {
1738 this.div.detach();
1739 this.panel = new_panel;
1740 this.panel.append(this.div);
1741 this.show();
1742};
1743
1bb74900
D
1744Utilityview.prototype.close = function () {
1745 this.div.remove();
1746 this.tab.remove();
8343584e 1747
673a7c8f
D
1748 if (kiwi.front.cur_channel === this) {
1749 kiwi.front.tabviews.server.show();
1bb74900 1750 }
673a7c8f 1751 delete kiwi.front.utilityviews[this.name.toLowerCase()];
1bb74900
D
1752};
1753
1754Utilityview.prototype.addPartImage = function () {
1755 this.clearPartImage();
8343584e 1756
1bb74900
D
1757 // We can't close this tab, so don't have the close image
1758 if (this.name === 'server') {
1759 return;
1760 }
1761
8397d902 1762 var del_html = '<img src="/img/redcross.png" class="tab_part" />';
1bb74900 1763 this.tab.append(del_html);
8343584e 1764
1bb74900 1765 $('.tab_part', this.tab).click(function () {
673a7c8f
D
1766 if (kiwi.front.cur_channel.name !== 'server') {
1767 kiwi.front.cur_channel.close();
1bb74900
D
1768 }
1769 });
1770};
1771
1772Utilityview.prototype.clearPartImage = function () {
2b9dcc03 1773 $('#kiwi .toolbars .tab_part').remove();
1bb74900
D
1774};
1775
1776
1777
1778
54f4a22e
D
1779
1780/*
1781 *
1782 * TABVIEWS
1783 *
1784 */
1785
1786
4e4ca8e6
D
1787var Tabview = function () {
1788 this.panel = $('#panel1');
1789};
a3364605
JA
1790Tabview.prototype.name = null;
1791Tabview.prototype.div = null;
1792Tabview.prototype.userlist = null;
875d0c71 1793Tabview.prototype.userlist_width = 100; // 0 for disabled
a3364605
JA
1794Tabview.prototype.tab = null;
1795Tabview.prototype.topic = "";
1796Tabview.prototype.safe_to_close = false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
4e4ca8e6 1797Tabview.prototype.panel = null;
a3364605
JA
1798
1799Tabview.prototype.show = function () {
875d0c71
D
1800 var w, u;
1801
4e4ca8e6 1802 $('.messages', this.panel).removeClass("active");
a3364605 1803 $('#kiwi .userlist ul').removeClass("active");
2b9dcc03 1804 $('#kiwi .toolbars ul li').removeClass("active");
8343584e 1805
875d0c71
D
1806 w = $('#windows');
1807 u = $('#kiwi .userlist');
1808
8536a327 1809 this.panel.css('overflow-y', 'scroll');
875d0c71
D
1810
1811 // Set the window size accordingly
b10afa2d 1812 this.setUserlistWidth();
1bb74900 1813
a3364605
JA
1814 // Activate this tab!
1815 this.div.addClass('active');
c6ef62a3 1816 if (this.userlist_width > 0) {
8343584e 1817 this.userlist.active(true);
b10afa2d
D
1818 // Enable the userlist resizer
1819 $('#nicklist_resize').css('display', 'block');
1820 } else {
1821 // Disable the userlist resizer
1822 $('#nicklist_resize').css('display', 'none');
c6ef62a3 1823 }
a3364605 1824 this.tab.addClass('active');
8343584e 1825
a3364605
JA
1826 // Add the part image to the tab
1827 this.addPartImage();
8343584e 1828
a3364605 1829 this.clearHighlight();
673a7c8f
D
1830 kiwi.front.setTopicText(this.topic);
1831 kiwi.front.cur_channel = this;
8343584e 1832
a3364605
JA
1833 // If we're using fancy scrolling, refresh it
1834 if (touch_scroll) {
1835 touch_scroll.refresh();
1836 }
1837
1838 this.scrollBottom();
1839 if (!touchscreen) {
1840 $('#kiwi_msginput').focus();
1841 }
1842};
1843
1844Tabview.prototype.close = function () {
1845 this.div.remove();
1846 this.userlist.remove();
8343584e 1847 this.userlist = null;
a3364605 1848 this.tab.remove();
8343584e 1849
673a7c8f
D
1850 if (kiwi.front.cur_channel === this) {
1851 kiwi.front.tabviews.server.show();
a3364605 1852 }
673a7c8f 1853 delete kiwi.front.tabviews[this.name.toLowerCase()];
a3364605
JA
1854};
1855
b10afa2d 1856Tabview.prototype.setUserlistWidth = function (new_width) {
8343584e 1857 this.userlist.setWidth(new_width);
b10afa2d
D
1858};
1859
a3364605
JA
1860Tabview.prototype.addPartImage = function () {
1861 this.clearPartImage();
8343584e 1862
a3364605
JA
1863 // We can't close this tab, so don't have the close image
1864 if (this.name === 'server') {
1865 return;
1866 }
1867
8397d902 1868 var del_html = '<img src="/img/redcross.png" class="tab_part" />';
a3364605 1869 this.tab.append(del_html);
8343584e 1870
a3364605 1871 $('.tab_part', this.tab).click(function () {
673a7c8f
D
1872 if (kiwi.front.isChannel($(this).parent().text())) {
1873 kiwi.front.run("/part");
a3364605
JA
1874 } else {
1875 // Make sure we don't close the server tab
673a7c8f
D
1876 if (kiwi.front.cur_channel.name !== 'server') {
1877 kiwi.front.cur_channel.close();
a3364605
JA
1878 }
1879 }
1880 });
1881};
1882
1883Tabview.prototype.clearPartImage = function () {
2b9dcc03 1884 $('#kiwi .toolbars .tab_part').remove();
a3364605
JA
1885};
1886
1887Tabview.prototype.addMsg = function (time, nick, msg, type, style) {
8343584e
D
1888 var self, tmp, d, re, line_msg;
1889
a3364605 1890 self = this;
8343584e 1891
ca7e63ea 1892 tmp = {msg: msg, time: time, nick: nick, tabview: this.name};
673a7c8f 1893 tmp = kiwi.plugs.run('addmsg', tmp);
c6ef62a3
JA
1894 if (!tmp) {
1895 return;
1896 }
8343584e 1897
ca7e63ea
D
1898
1899 msg = tmp.msg;
1900 time = tmp.time;
1901 nick = tmp.nick;
1902
a3364605
JA
1903 if (time === null) {
1904 d = new Date();
1905 time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0");
1906 }
8343584e 1907
a3364605
JA
1908 // The CSS class (action, topic, notice, etc)
1909 if (typeof type !== "string") {
1910 type = '';
1911 }
8343584e 1912
a3364605
JA
1913 // Make sure we don't have NaN or something
1914 if (typeof msg !== "string") {
1915 msg = '';
1916 }
8343584e 1917
ca7e63ea 1918 // Make the channels clickable
673a7c8f 1919 re = new RegExp('\\B(' + kiwi.gateway.channel_prefix + '[^ ,.\\007]+)', 'g');
a3364605 1920 msg = msg.replace(re, function (match) {
ca7e63ea 1921 return '<a class="chan">' + match + '</a>';
9545f343
JA
1922 });
1923
9d34a1ca 1924 msg = kiwi.front.format(msg);
8343584e 1925
ca7e63ea
D
1926 // Build up and add the line
1927 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
1928 this.div.append(line_msg);
1929
1930 if (!touchscreen) {
1931 this.scrollBottom();
1932 } else {
1933 touch_scroll.refresh();
1934 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1935 this.scrollBottom();
1936 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1937 // touch_scroll.scrollTo(0, this.div.height());
1938 //}
1939 }
1940};
1941
1942Tabview.prototype.scrollBottom = function () {
4e4ca8e6 1943 var panel = this.panel;
4e4ca8e6 1944 panel[0].scrollTop = panel[0].scrollHeight;
a3364605
JA
1945};
1946
1947Tabview.prototype.changeNick = function (newNick, oldNick) {
8343584e
D
1948 var inChan = this.userlist.hasUser(oldNick);
1949 if (inChan) {
1950 this.userlist.renameUser(oldNick, newNick);
673a7c8f 1951 this.addMsg(null, ' ', '=== ' + oldNick + ' is now known as ' + newNick, 'action changenick');
a3364605
JA
1952 }
1953};
a3364605
JA
1954Tabview.prototype.highlight = function () {
1955 this.tab.addClass('highlight');
1956};
1957Tabview.prototype.activity = function () {
1958 this.tab.addClass('activity');
1959};
1960Tabview.prototype.clearHighlight = function () {
1961 this.tab.removeClass('highlight');
1962 this.tab.removeClass('activity');
1963};
1964Tabview.prototype.changeTopic = function (new_topic) {
1965 this.topic = new_topic;
1966 this.addMsg(null, ' ', '=== Topic for ' + this.name + ' is: ' + new_topic, 'topic');
673a7c8f
D
1967 if (kiwi.front.cur_channel.name === this.name) {
1968 kiwi.front.setTopicText(new_topic);
a3364605
JA
1969 }
1970};
1971
1972
1973
1974
1975
1976var Box = function (classname) {
1977 this.id = randomString(10);
1978 var tmp = $('<div id="' + this.id + '" class="box ' + classname + '"><div class="boxarea"></div></div>');
1979 $('#kiwi').append(tmp);
1980 this.box = $('#' + this.id);
1981 this.content = $('#' + this.id + ' .boxarea');
8343584e 1982
a3364605
JA
1983 this.box.draggable({ stack: ".box" });
1984 this.content.click(function () {});
1985 //this.box.click(function(){ $(this)..css });
1986};
1987Box.prototype.create = function (name, classname) {
8343584e 1988
a3364605
JA
1989};
1990Box.prototype.id = null;
1991Box.prototype.box = null;
1992Box.prototype.content = null;
1993Box.prototype.destroy = function () {
1994 var name;
1995 this.box.remove();
673a7c8f
D
1996 for (name in kiwi.front.boxes) {
1997 if (kiwi.front.boxes[name].id === this.id) {
1998 delete kiwi.front.boxes[name];
a3364605
JA
1999 }
2000 }
2001};
8343584e 2002Box.prototype.height = function () {
a3364605
JA
2003 return this.box.height();
2004};