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