Passes jslint with regexp: true, confusion: true, undef: false, node: true, sloppy...
[KiwiIRC.git] / js / front.js
CommitLineData
54f4a22e
D
1var front = {
2 revision: 38,
3
4 cur_channel: '',
5 windows: {},
6 tabviews: {},
7 boxes: {},
8
9 buffer: [],
10 buffer_pos: 0,
11
12 init: function(){
13 gateway.nick = 'kiwi_'+Math.ceil(100*Math.random())+Math.ceil(100*Math.random());
14 gateway.session_id = null;
15
16 $(gateway).bind("onmsg", front.onMsg);
17 $(gateway).bind("onnotice", front.onNotice);
18 $(gateway).bind("onaction", front.onAction);
19 $(gateway).bind("onmotd", front.onMOTD);
20 $(gateway).bind("onoptions", front.onOptions);
21 $(gateway).bind("onconnect", front.onConnect);
22 $(gateway).bind("onnick", front.onNick);
23 $(gateway).bind("onuserlist", front.onUserList);
24 $(gateway).bind("onuserlist_end", front.onUserListEnd);
25 $(gateway).bind("onjoin", front.onJoin);
26 $(gateway).bind("ontopic", front.onTopic);
27 $(gateway).bind("onpart", front.onPart);
28 $(gateway).bind("onkick", front.onKick);
29 $(gateway).bind("onquit", front.onQuit);
30 $(gateway).bind("onwhois", front.onWhois);
31 $(gateway).bind("onsync", front.onSync);
32 $(gateway).bind("onchannel_redirect", front.onChannelRedirect);
33 $(gateway).bind("ondebug", front.onDebug);
34
35 this.buffer = new Array();
36
37 // Build the about box
38 front.boxes.about = new box("about");
39 front.boxes.about.content.html('<h2>Kiwi IRC</h2>');
40 front.boxes.about.content.append("<p>An alternative to downloading an irc client. Kiwi IRC is the best web app you'll use for the next couple years.</p>");
41 front.boxes.about.content.append('<button class="about_close">Close</button>');
42
43 var about_info = 'UI adapted for '+agent;
44 if(touchscreen) about_info += ' touchscreen ';
45 about_info += 'usage';
46
47 front.boxes.about.content.append('<p class="info">'+about_info+'</p>');
48 front.boxes.about.content.append('<p class="revisions">Front: '+front.revision+'<br />Gateway: '+gateway.revision+'</p>');
49
50 //$(window).bind("beforeunload", function(){ gateway.quit(); });
51
52 front.registerKeys();
53
54
55 $('#kiwi .formconnectwindow').submit(function(){
56 var netsel = $('#kiwi .formconnectwindow .network');
57 var nick = $('#kiwi .formconnectwindow .nick');
58
59 if(nick.val() == ''){
60 nick.val('Nick please!');
61 nick.focus();
62 return false;
63 }
64
65 var tmp = nick.val().split(' ');
66 gateway.nick = tmp[0];
67 front.doLayout();
68 try {
69 front.run('/connect '+netsel.val());
70 } catch(e){
71 alert(e);
72 }
73
74 $('#kiwi .connectwindow').slideUp();
75 return false;
76 });
77
78 var supportsOrientationChange = "onorientationchange" in window,
79 orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
80 window.addEventListener(orientationEvent, front.doLayoutSize, false);
81 //$('#kiwi').bind("resize", front.doLayoutSize, false);
82
83 front.doLayout();
84 //front.windowAdd('server');
85 front.tabviewAdd('server');
86
87 // Any pre-defined nick?
88 if(typeof init_data.nick == "string") $('#kiwi .formconnectwindow .nick').val(init_data.nick);
89
90 //gateway.session_id = 'testses';
91
92 gateway.start();
93 //front.sync();
94 },
95
96 doLayoutSize: function() {
97 var kiwi = $('#kiwi');
98 if(kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')){
99 console.log("switching to small kiwi");
100 kiwi.removeClass('large_kiwi');
101 kiwi.addClass('small_kiwi');
102 } else if(kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) {
103 console.log("switching to large kiwi");
104 kiwi.removeClass('small_kiwi');
105 kiwi.addClass('large_kiwi');
106 }
107
108 var ct = $('#kiwi .cur_topic');
109 var ul = $('#kiwi .userlist');
110 var top = ul.css('margin-top').replace('px', '') + ct.css('border-bottom-width').replace('px', '');
111 //top = parseInt(ct.offset().top) + parseInt(ct.height()) + parseInt(top);
112 top = parseInt(ct.height()) + parseInt(top);
113
114 $('#kiwi .messages').css('top', top+"px");
115 $('#kiwi .userlist').css('top', top+"px");
116 },
117
118
119 doLayout: function(){
120 $('#kiwi .msginput .nick a').text(gateway.nick);
121 $('#kiwi_msginput').val(' ');
122 $('#kiwi_msginput').focus();
123 },
124
125
126 joinChannel: function(chan_name){
127 var chans = chan_name.split(',');
128 for(var i in chans){
129 chan = chans[i];
130 if(front.tabviews[chan.toLowerCase()] == undefined){
131 gateway.join(chan);
132 front.tabviewAdd(chan);
133 } else {
134 front.tabviews[chan.toLowerCase()].show();
135 }
136 }
137 },
138
139
140 run: function(msg){
141 if(msg.substring(0,1) == '/'){
142 var parts = msg.split(' ');
143 switch(parts[0]){
144 case '/j':
145 case '/join':
146 front.joinChannel(parts[1]);
147 break;
148
149 case '/connect':
150 case '/server':
151 if(parts[1] == undefined){
152 alert('Usage: /connect servername [port]');
153 break;
154 }
155
156 if(parts[2] == undefined) parts[2] = 6667;
157 front.cur_channel.addMsg(null, ' ', '=== Connecting to '+parts[1]+'...', 'status');
158 gateway.connect(parts[1], parts[2], 0);
159 break;
160
161 case '/part':
162 if(typeof parts[1] == "undefined"){
163 gateway.raw(msg.substring(1)+' '+front.cur_channel.name);
164 } else {
165 gateway.raw(msg.substring(1));
166 }
167 break;
168
169 case '/names':
170 if(typeof parts[1] != "undefined"){
171 gateway.raw(msg.substring(1));
172 }
173 break;
174
175 case '/debug':
176 gateway.debug();
177 break;
178
179 case '/q':
180 case '/query':
181 if(typeof parts[1] != "undefined"){
182 front.tabviewAdd(parts[1]);
183 }
184 break;
185
186 case '/quote':
187 gateway.raw(msg.replace(/^\/quote /i,''));
188 break;
189
190 case '/me':
191 gateway.action(front.cur_channel.name, msg.substring(4));
192 //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
193 front.cur_channel.addMsg(null, ' ', '* '+gateway.nick+' '+msg.substring(4), 'action', 'color:#555;');
194 break;
195
196 default:
197 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
198 gateway.raw(msg.substring(1));
199 }
200
201 } else {
202 //alert('Sending message: '+msg);
203 if(msg.trim() == '') return;
204 gateway.msg(front.cur_channel.name, msg);
205 var d = new Date();
206 var d = d.getHours() + ":" + d.getMinutes();
207 //front.addMsg(d, gateway.nick, msg);
208 front.cur_channel.addMsg(null, gateway.nick, msg);
209 }
210 },
211
212
213 onMsg: function(e, data){
214 // Is this message from a user?
215 if(data.channel == gateway.nick){
216 var destination = data.nick.toLowerCase();
217 } else {
218 var destination = data.channel.toLowerCase();
219 }
220
221 if(!front.tabviewExists(destination)) front.tabviewAdd(destination);
222 front.tabviews[destination].addMsg(null, data.nick, data.msg);
223 },
224
225 onDebug: function(e, data){
226 if(!front.tabviewExists('kiwi_debug')) front.tabviewAdd('kiwi_debug');
227 front.tabviews['kiwi_debug'].addMsg(null, ' ', data.msg);
228 },
229
230 onAction: function(e, data){
231 // Is this message from a user?
232 if(data.channel == gateway.nick){
233 var destination = data.nick;
234 } else {
235 var destination = data.channel;
236 }
237
238 if(!front.tabviewExists(destination)) front.tabviewAdd(destination);
239 front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'action', 'color:#555;');
240 },
241
242 onTopic: function(e, data){
243 if(front.tabviewExists(data.channel)){
244 front.tabviews[data.channel.toLowerCase()].changeTopic(data.topic);
245 }
246 },
247
248 onNotice: function(e, data){
249 var nick = (data.nick=="") ? "" : '['+data.nick+']';
250 if(data.channel != undefined){
251 //alert('notice for '+data.channel);
252 if(front.tabviewExists(data.channel)){
253 front.tabviews[data.channel.toLowerCase()].addMsg(null, nick, data.msg, 'notice');
254 }
255 } else {
256 //alert('direct notice');
257 front.tabviews['server'].addMsg(null, nick, data.msg, 'notice');
258 }
259 },
260 onConnect: function(e, data){
261 if(data.connected){
262 front.tabviews['server'].addMsg(null, ' ', '=== Connected OK :)', 'status');
263 if(typeof init_data.channel == "string"){
264 front.joinChannel(init_data.channel);
265 }
266 } else {
267 front.tabviews['server'].addMsg(null, ' ', '=== Failed to connect :(', 'status');
268 }
269 },
270 onOptions: function(e, data){
271 if(typeof gateway.network_name == "string" && gateway.network_name != ""){
272 front.tabviews['server'].tab.text(gateway.network_name);
273 }
274 },
275 onMOTD: function(e, data){
276 front.tabviews['server'].addMsg(null, data.server, data.msg, 'motd');
277 },
278 onWhois: function(e, data){
279 front.cur_channel.addMsg(null, data.nick, data.msg, 'whois');
280 },
281 onUserList: function(e, data){
282 if(front.tabviews[data.channel.toLowerCase()] == undefined) return;
283
284 var ul = front.tabviews[data.channel.toLowerCase()].userlist;
285
286 if(!document.userlist_updating){
287 document.userlist_updating = true;
288 ul.empty();
289 }
290
291 $.each(data.users, function(i,item){
292 var nick = i; //i.match(/^.+!/g);
293 var mode = item;
294 $('<li><a class="nick" onclick="front.userClick(this);">'+mode+nick+'</a></li>').appendTo(ul);
295 });
296
297 front.tabviews[data.channel.toLowerCase()].userlistSort();
298 },
299 onUserListEnd: function(e, data){
300 document.userlist_updating = false;
301 },
302
303 onJoin: function(e, data){
304 if(!front.tabviewExists(data.channel)){
305 front.tabviewAdd(data.channel.toLowerCase());
306 }
307
308 if(data.nick == gateway.nick) return; // Not needed as it's already in nicklist
309 front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> '+data.nick+' has joined', 'action', 'color:#009900;');
310 $('<li><a class="nick" onclick="front.userClick(this);">'+data.nick+'</a></li>').appendTo(front.tabviews[data.channel.toLowerCase()].userlist);
311 front.tabviews[data.channel.toLowerCase()].userlistSort();
312 },
313 onPart: function(e, data){
314 if(front.tabviewExists(data.channel)){
315 // If this is us, close the tabview
316 if(data.nick == gateway.nick){
317 front.tabviews[data.channel.toLowerCase()].close();
318 front.tabviews['server'].show();
319 return;
320 }
321
322 front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- '+data.nick+' has left ('+data.message+')', 'action', 'color:#990000;');
323 front.tabviews[data.channel.toLowerCase()].userlist.children().each(function(){
324 if($(this).text() == data.nick){
325 $(this).remove();
326 }
327 });
328 }
329 },
330 onKick: function(e, data){
331 if(front.tabviewExists(data.channel)){
332 // If this is us, close the tabview
333 if(data.kicked == gateway.nick){
334 front.tabviews[data.channel.toLowerCase()].close();
335 return;
336 }
337
338 front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- '+data.kicked+' kicked by '+data.nick+'('+data.message+')', 'action', 'color:#990000;');
339 front.tabviews[data.channel.toLowerCase()].userlist.children().each(function(){
340 if($(this).text() == data.nick){
341 $(this).remove();
342 }
343 });
344 }
345 },
346 onNick: function(e, data){
347 if(data.nick == gateway.nick){
348 gateway.nick = data.newnick;
349 front.doLayout();
350 }
351
352 $.each(front.tabviews, function(i,item){
353 $.each(front.tabviews, function(i,item){
354 item.changeNick(data.newnick, data.nick);
355 });
356 });
357 },
358 onQuit: function(e, data){
359 $.each(front.tabviews, function(i,item){
360 $.each(front.tabviews, function(i,item){
361 item.userlist.children().each(function(){
362 if($(this).text() == data.nick){
363 $(this).remove();
364 item.addMsg(null, ' ', '<-- '+data.nick+' has quit ('+data.message+')', 'action', 'color:#990000;');
365 }
366 });
367 });
368 });
369 },
370 onChannelRedirect: function(e, data){
371 front.tabviews[data.from.toLowerCase()].close();
372 front.tabviewAdd(data.to.toLowerCase());
373 front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from '+data.from, 'action');
374 },
375
376 registerKeys: function(){
377 $('#kiwi_msginput').bind('keydown', function(e){
378 //$('input').keypress(function(e){
379 switch(e.which){
380 case 27: // escape
381 return false;
382 break;
383 case 13: // return
384 var msg = $('#kiwi_msginput').val();
385 msg = msg.trim();
386
387 front.buffer.push(msg);
388 front.buffer_pos = front.buffer.length;
389
390 front.run(msg);
391 $('#kiwi_msginput').val('');
392
393 break;
394
395 case 38: // up
396 if(front.buffer_pos > 0){
397 front.buffer_pos--;
398 $('#kiwi_msginput').val(front.buffer[front.buffer_pos]);
399 }
400 break;
401 case 40: // down
402 if(front.buffer_pos < front.buffer.length){
403 front.buffer_pos++;
404 $('#kiwi_msginput').val(front.buffer[front.buffer_pos]);
405 }
406 break;
407
408 case 9: // tab
409 // Get possible autocompletions
410 var data = [];
411 front.cur_channel.userlist.children().each(function(){
412 nick = front.nickStripPrefix($('a.nick', this).text());
413 data.push(nick);
414 });
415
416 // Do the autocomplete
417 if (this.value.length == this.selectionStart && this.value.length == this.selectionEnd) {
418 var candidates = [];
419
420 var word_pos = this.value.lastIndexOf(' ');
421 var word = "";
422 if(word_pos == -1){
423 word = this.value;
424 } else {
425 word = this.value.substr(word_pos);
426 }
427 word = word.trim();
428
429 // filter data to find only strings that start with existing value
430 for (var i=0; i < data.length; i++) {
431 if (data[i].indexOf(word) == 0 && data[i].length > word.length)
432 candidates.push(data[i]);
433 }
434
435 if (candidates.length > 0) {
436 // some candidates for autocompletion are found
437 this.value = this.value.substring(0, word_pos) + ' ' + candidates[0]+': ';
438 this.selectionStart = this.value.length;
439 }
440 }
441 return false;
442
443 break;
444 }
445 });
446
447
448 $('#kiwi .control .msginput .nick').click(function(){
449 var html = '<div class="newnick box">\
450 Your new nick:<br />\
451 <form class="form_newnick">\
452 <input type="text" class="txtnewnick" /><br />\
453 <button class="butnewnick" type="submit">Change</button> <a class="link cancelnewnick">Cancel</a>\
454 </form>\
455</div>';
456 $('#kiwi').append(html);
457 $('#kiwi .form_newnick').submit(function(){
458 front.run('/NICK '+$('#kiwi .txtnewnick').val());
459 $('#kiwi .newnick').remove();
460 return false;
461 });
462 $('#kiwi .cancelnewnick').click(function(){
463 $('#kiwi .newnick').remove();
464 });
465
466 $('#kiwi .txtnewnick').focus();
467
468 });
469
470
471
472
473
474 $('#kiwi .plugins .load_plugin_file').click(function(){
475 if(typeof front.boxes.plugins != "undefined") return;
476
477 var html = '<div class="list">\
478 <h2>Kiwi plugins</h2>\
479 <p>\
480 <select multiple="multiple" id="plugin_list">\
481 </select>\
482 <button id="plugins_list_unload">Unload</button>\
483 </p>\
484 </div>\
485 <div class="load">\
486 Plugin file URL:<br />\
487 <form>\
488 <input type="text" class="txtpluginfile" /><br />\
489 <button class="butnewnick" type="submit">Load..</button> <a class="link cancelpluginfile">Cancel</a>\
490 </form>\
491 </div>';
492 front.boxes.plugins = new box("plugin_file");
493 front.boxes.plugins.content.html(html);
494 front.boxes.plugins.box.css('top', -(front.boxes.plugins.height+40));
495
496 // Populate the plugin list..
497 var lst = $('#plugin_list');
498 lst.find('option').remove();
499 for(var i in plugins.privmsg){
500 var txt = plugins.privmsg[i].name;
501 lst.append('<option value="'+txt+'">'+txt+'</option>');
502 }
503
504 // Event bindings
505 $('#kiwi .plugin_file').submit(function(){
506 $.getJSON($('.txtpluginfile').val(), function(data) {
507 var plg = {};
508 plg.name = data.name;
509 eval("plg.onprivmsg = "+data.onprivmsg);
510 eval("plg.onload = "+data.onload);
511 eval("plg.onunload = "+data.onunload);
512 plugins.privmsg.push(plg);
513
514 if(plg.onload instanceof Function) plg.onload();
515 });
516 return false;
517 });
518 $('#kiwi .cancelpluginfile').click(function(){
519 front.boxes.plugins.destroy();
520 });
521
522 $('#kiwi #plugins_list_unload').click(function(){
523 var selected_plugin = $('#plugin_list').val();
524 console.log("removing plugin: "+selected_plugin);
525 for(var i in plugins.privmsg){
526 if(plugins.privmsg[i].name == selected_plugin){
527 if(plugins.privmsg[i].onunload instanceof Function)
528 plugins.privmsg[i].onunload();
529
530 delete plugins.privmsg[i];
531 }
532 }
533 });
534
535 $('#kiwi .txtpluginfile').focus();
536
537 });
538
539 $('#kiwi .plugins .reload_css').click(function(){
540 var links = document.getElementsByTagName("link");
541 for (var i=0; i < links.length; i++){
542 if(links[i].rel === "stylesheet"){
543 if(links[i].href.indexOf("?")===-1){
544 links[i].href += "?";
545 }
546 links[i].href += "x";
547 }
548 }
549 });
550
551
552 $('#kiwi .about .about_close').click(function(){
553 $('#kiwi .about').css('display', 'none');
554 });
555
556
557 $('#kiwi .poweredby').click(function(){
558 $('#kiwi .about').css('display', 'block');
559 });
560
561 },
562
563
564 tabviewExists: function(name){
565 return !(front.tabviews[name.toLowerCase()] == undefined);
566 },
567
568 tabviewAdd: function(v_name){
569 if(v_name.charAt(0) == gateway.channel_prefix){
570 var re = new RegExp(gateway.channel_prefix,"g");
571 var htmlsafe_name = v_name.replace(re, 'pre');
572 htmlsafe_name = "chan_"+htmlsafe_name;
573 } else {
574 var htmlsafe_name = 'query_'+v_name;
575 }
576
577 var tmp_divname = 'kiwi_window_'+htmlsafe_name;
578 var tmp_userlistname = 'kiwi_userlist_'+htmlsafe_name;
579 var tmp_tabname = 'kiwi_tab_'+htmlsafe_name
580
581 $('#kiwi').append('<div id="'+tmp_divname+'" class="messages"></div>');
582 $('#kiwi .userlist').append('<ul id="'+tmp_userlistname+'"></ul>');
583 $('#kiwi .windowlist ul').append('<li id="'+tmp_tabname+'" onclick="front.tabviews[\''+v_name.toLowerCase()+'\'].show();">'+v_name+'</li>');
584 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
585 //front.windowShow(v_name);
586
587 front.tabviews[v_name.toLowerCase()] = new tabview();
588 front.tabviews[v_name.toLowerCase()].name = v_name;
589 front.tabviews[v_name.toLowerCase()].div = $('#'+tmp_divname);
590 front.tabviews[v_name.toLowerCase()].userlist = $('#'+tmp_userlistname);
591 front.tabviews[v_name.toLowerCase()].tab = $('#'+tmp_tabname);
592 front.tabviews[v_name.toLowerCase()].show();
593
594 if(typeof registerTouches == "function"){
595 //alert("Registering touch interface");
596 //registerTouches($('#'+tmp_divname));
597 registerTouches(document.getElementById(tmp_divname));
598 }
599 /*
600 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
601 alert($(this).attr('id'));
602 });
603 */
604
605 front.doLayoutSize();
606 },
607
608
609 userClick: function(item){
610 // Remove any existing userboxes
611 $('#kiwi .userbox').remove();
612
613 var li = $(item).parent();
614 var html = '<div class="userbox">\
615 <input type="hidden" class="userbox_nick" value="'+front.nickStripPrefix($(item).text())+'" />\
616 <a href="#" class="userbox_query">Message</a>\
617 <a href="#" class="userbox_whois">Info</a>\
618</div>';
619 li.append(html);
620
621 $('#kiwi .userbox .userbox_query').click(function(ev){
622 var nick = $('#kiwi .userbox_nick').val();
623 front.run('/query '+nick);
624 });
625
626 $('#kiwi .userbox .userbox_whois').click(function(ev){
627 var nick = $('#kiwi .userbox_nick').val();
628 front.run('/whois '+nick);
629 });
630 },
631
632
633 sync: function(){
634 gateway.sync();
635 },
636
637 onSync: function(e, data){
638 // Set any settings
639 if(data.nick != undefined) gateway.nick = data.nick;
640
641 // Add the tabviews
642 if(data.tabviews != undefined){
643 $.each(data.tabviews, function(i,tab){
644 if(!front.tabviewExists(tab.name)){
645 front.tabviewAdd(gateway.channel_prefix+tab.name);
646
647 if(tab.userlist != undefined)
648 front.onUserList({'channel':gateway.channel_prefix+tab.name, 'users':tab.userlist});
649 }
650 });
651 }
652
653 front.doLayout();
654 },
655
656
657 setTopicText: function(new_topic){
658 $('#kiwi .cur_topic').text(new_topic);
659 },
660
661
662
663
664
665
666
667 nickStripPrefix: function(nick){
668 var tmp = nick;
669
670 prefix = tmp.charAt(0);
671 if(typeof gateway.user_prefixes[prefix] != "undefined") tmp = tmp.substring(1);
672
673 return tmp;
674 },
675
676 nickGetPrefix: function(nick){
677 var tmp = nick;
678
679 prefix = tmp.charAt(0);
680 if(typeof gateway.user_prefixes[prefix] == "undefined"){
681 prefix = "";
682 }
683
684 return prefix;
685 },
686
687 isChannel: function(name){
688 prefix = name.charAt(0);
689 if(gateway.channel_prefix.indexOf(prefix) > -1){
690 is_chan = true;
691 } else {
692 is_chan = false;
693 }
694
695 return is_chan;
696 },
697
698 tabviewsNext: function(){
699 var wl = $('#kiwi .windowlist ul');
700 var next_left = parseInt(wl.css('text-indent').replace('px', ''))+170;
701 wl.css('text-indent', next_left);
702 },
703
704 tabviewsPrevious: function(){
705 var wl = $('#kiwi .windowlist ul');
706 var next_left = parseInt(wl.css('text-indent').replace('px', ''))-170;
707 wl.css('text-indent', next_left);
708 }
709}
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727/*
728 *
729 * TABVIEWS
730 *
731 */
732
733
734var tabview = function(){}
735tabview.prototype.name = null;
736tabview.prototype.div = null;
737tabview.prototype.userlist = null;
738tabview.prototype.tab = null;
739tabview.prototype.topic = "";
740
741tabview.prototype.show = function(){
742 $('#kiwi .messages').removeClass("active");
743 $('#kiwi .userlist ul').removeClass("active");
744 $('#kiwi .windowlist ul li').removeClass("active");
745
746 // Activate this tab!
747 this.div.addClass('active');
748 this.userlist.addClass('active');
749 this.tab.addClass('active');
750
751 document.tmp = this.div;
752 // Add the part image to the tab
753 this.addPartImage();
754
755 this.clearHighlight();
756 front.setTopicText(this.topic);
757 front.cur_channel = this;
758
759 this.scrollBottom();
760 $('#kiwi_msginput').focus();
761}
762
763tabview.prototype.close = function(){
764 this.div.remove();
765 this.userlist.remove();
766 this.tab.remove();
767
768 front.tabviews['server'].show();
769 delete front.tabviews[this.name.toLowerCase()];
770}
771
772tabview.prototype.addPartImage = function(){
773 this.clearPartImage();
774
775 var del_html = '<img src="img/redcross.png" class="tab_part" />';
776 this.tab.append(del_html);
777
778 $('.tab_part', this.tab).click(function(){
779 if(front.isChannel($(this).parent().text())){
780 front.run("/part");
781 } else {
782 // Make sure we don't close the server tab
783 if(front.cur_channel.name != "server") front.cur_channel.close();
784 }
785 });
786}
787
788tabview.prototype.clearPartImage = function(){
789 $('#kiwi .windowlist .tab_part').remove();
790}
791
792tabview.prototype.addMsg = function(time, nick, msg, type, style){
793 //if(nick.charAt(0) != "[" && nick != ""){
794 // var html_nick = $('<div/>').text('<'+nick+'>').html();
795 //} else {
796 var html_nick = $('<div/>').text(nick).html();
797 //}
798
799 var self = this;
800
801 var tmp = msg;
802 var plugin_ret = '';
803 for(var i in plugins.privmsg){
804 if ((plugins.privmsg[i].onprivmsg instanceof Function)) {
805 plugin_ret = '';
806 try {
807 plugin_ret = plugins.privmsg[i].onprivmsg(tmp, this.name);
808
809 // If this plugin has returned false, do not add this message
810 if(plugin_ret === false) return;
811 } catch (e){}
812
813 // If we actually have a string from the plugin, use it
814 if(typeof plugin_ret == "string") tmp = plugin_ret;
815 }
816 }
817 msg = tmp;
818
819 //var html_msg = $('<div/>').text(msg).html()+' '; // Add the space so the styling always has at least 1 character to go from
820 if(time == null){
821 var d = new Date();
822 time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0");
823 }
824
825 // The CSS class (action, topic, notice, etc)
826 if(typeof type != "string") type = '';
827
828 // Make sure we don't have NaN or something
829 if(typeof msg != "string") msg = '';
830
831 // Text formatting
832 // bold
833 if(msg.indexOf(String.fromCharCode(2))){
834 next = '<b>';
835 while(msg.indexOf(String.fromCharCode(2)) != -1){
836 msg = msg.replace(String.fromCharCode(2), next);
837 next = (next=='<b>') ? '</b>' : '<b>';
838 }
839 if(next == '</b>') msg =+ '</b>';
840 }
841
842 // Wierd thing noticed by Dux0r on the irc.esper.net server
843 if(typeof msg != "string") msg = '';
844
845 // underline
846 if(msg.indexOf(String.fromCharCode(31))){
847 next = '<u>';
848 while(msg.indexOf(String.fromCharCode(31)) != -1){
849 msg = msg.replace(String.fromCharCode(31), next);
850 next = (next=='<u>') ? '</u>' : '<u>';
851 }
852 if(next == '</u>') msg =+ '</u>';
853 }
854
855
856 var 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>';
857 this.div.append(line_msg);
858 this.scrollBottom();
859}
860
861tabview.prototype.scrollBottom = function(){
862 this.div.attr({ scrollTop: this.div.attr("scrollHeight") });
863}
864
865tabview.prototype.changeNick = function(newNick, oldNick){
866 this.userlist.children().each(function(){
867 var item = $('a.nick', this);
868 if(front.nickStripPrefix(item.text()) == oldNick){
869 item.text(front.nickGetPrefix(item.text())+newNick);
870 document.temp_chan = 1;
871 }
872 });
873
874 if(typeof document.temp_chan != "undefined"){
875 this.addMsg(null, ' ', '=== '+oldNick+' is now known as '+newNick, 'action');
876 delete document.temp_chan;
877 this.userlistSort();
878 }
879}
880
881tabview.prototype.userlistSort = function(){
882 var ul = this.userlist;
883 var listitems = ul.children('li').get();
884 listitems.sort(function(a, b) {
885 var compA = $(a).text().toUpperCase();
886 var compB = $(b).text().toUpperCase();
887
888 // Sort by prefixes first
889 for (var prefix in gateway.user_prefixes) {
890 mode = gateway.user_prefixes[prefix];
891
892 if(compA.charAt(0) == prefix && compB.charAt(0) == prefix){
893 // Both have the same prefix, string compare time
894 return 0;
895 }
896
897 if(compA.charAt(0) == prefix && compB.charAt(0) != prefix){
898 return -1;
899 }
900
901 if(compA.charAt(0) != prefix && compB.charAt(0) == prefix){
902 return 1;
903 }
904 }
905
906 // No prefixes, compare by string
907 return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
908 })
909 $.each(listitems, function(idx, itm) { ul.append(itm); });
910}
911
912tabview.prototype.highlight = function(){
913 this.tab.addClass('highlight');
914}
915tabview.prototype.activity = function(){
916 this.tab.addClass('activity');
917}
918tabview.prototype.clearHighlight = function(){
919 this.tab.removeClass('highlight');
920 this.tab.removeClass('activity');
921}
922tabview.prototype.changeTopic = function(new_topic){
923 this.topic = new_topic;
924 this.addMsg(null, ' ', '=== Topic for '+this.name+' is: '+new_topic, 'topic');
925 if(front.cur_channel.name == this.name) front.setTopicText(new_topic);
926}
927
928
929
930
931
932var box = function(classname){
933 this.id = randomString(10);
934 var tmp = $('<div id="'+this.id+'" class="box '+classname+'"><div class="boxarea"></div></div>');
935 $('#kiwi').append(tmp);
936 this.box = $('#'+this.id);
937 this.content = $('#'+this.id+' .boxarea');
938
939 this.box.draggable({ stack: ".box" });
940 this.content.click(function(){});
941 //this.box.click(function(){ $(this)..css });
942}
943box.prototype.create = function(name, classname){
944
945}
946box.prototype.id = null;
947box.prototype.box = null;
948box.prototype.content = null;
949box.prototype.destroy = function(){
950 this.box.remove();
951 for (var name in front.boxes) if(front.boxes[name].id = this.id) delete front.boxes[name];
952}
953box.prototype.height = function(){ return this.box.height(); }