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