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