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*/
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;
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
);
47 // Build the about box
48 front
.boxes
.about
= new Box("about");
49 about_info
= 'UI adapted for ' + agent
;
51 about_info
+= ' touchscreen ';
53 about_info
+= 'usage';
54 $('#tmpl_about_box').tmpl({
56 front_revision
: front
.revision
,
57 gateway_revision
: gateway
.revision
58 }).appendTo(front
.boxes
.about
.content
);
60 //$(window).bind("beforeunload", function(){ gateway.quit(); });
63 $('#kiwi').addClass('touchscreen');
65 // Single touch scrolling through scrollback for touchscreens
67 touch_scroll
= new iScroll('windows', scroll_opts
);
72 $('#kiwi .cur_topic').resize(front
.doLayoutSize
);
74 $('#kiwi .formconnectwindow').submit(function () {
75 var netsel
= $('#kiwi .formconnectwindow .network'),
76 nick
= $('#kiwi .formconnectwindow .nick'),
79 if (nick
.val() === '') {
80 nick
.val('Nick please!');
85 tmp
= nick
.val().split(' ');
86 gateway
.nick
= tmp
[0];
89 front
.run('/connect ' + netsel
.val());
94 $('#kiwi .connectwindow').slideUp();
95 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
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);
106 //front.windowAdd('server');
107 front
.tabviewAdd('server');
109 // Any pre-defined nick?
110 if (typeof init_data
.nick
=== "string") {
111 $('#kiwi .formconnectwindow .nick').val(init_data
.nick
);
114 //gateway.session_id = 'testses';
116 $('#kiwi .cur_topic').keydown(function (e
) {
117 if (e
.which
=== 13) {
121 $('#kiwi_msginput').focus();
122 } else if (e
.which
=== 27) {
125 $(this).text(front
.original_topic
);
126 $('#kiwi_msginput').focus();
129 /*$('.cur_topic').live('keypress', function(e) {
130 if (e.keyCode === 13) {
134 $('#kiwi_msginput').focus();
135 } else if (e.keyCode === 27) {
138 $(this).text(front.original_topic);
141 $('.cur_topic').live('change', function () {
143 text
= $(this).text();
144 if (text
!== front
.original_topic
) {
145 chan
= front
.cur_channel
.name
;
146 gateway
.setTopic(chan
, text
);
151 $('#windows a.chan').live('click', function() {
152 front
.joinChannel($(this).text());
156 $('#windows a.link_ext').live('mouseover', function () {
158 var tt
= $('.tt', a
);
160 if (tt
.text() === '') {
161 var tooltip
= $('<a class="link_ext_browser">Open in Kiwi..</a>');
165 tt
.css('top', -tt
.outerHeight()+'px');
166 tt
.css('left', (a
.outerWidth() / 2) - (tt
.outerWidth() / 2));
168 $('#windows a.link_ext').live('mouseout', function () {
170 var tt
= $('.tt', a
);
172 $('#windows a.link_ext').live('click', function (e
) {
175 switch (e
.target
.className
) {
179 case 'link_ext_browser':
180 var t
= new Utilityview('Browser', a
.attr('href'));
189 doLayoutSize: function () {
190 var kiwi
, ct
, ul
, n_top
, n_bottom
;
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');
201 ct
= $('#kiwi .cur_topic');
202 ul
= $('#kiwi .userlist');
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 :/
211 n_bottom
= $(document
).height() - parseInt($('#kiwi .control').offset().top
, 10);
213 $('#kiwi .windows').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
214 $('#kiwi .userlist').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
218 doLayout: function () {
219 $('#kiwi .msginput .nick a').text(gateway
.nick
);
220 $('#kiwi_msginput').val(' ');
221 $('#kiwi_msginput').focus();
225 joinChannel: function (chan_name
) {
226 var chans
= chan_name
.split(','),
230 if (front
.tabviews
[chan
.toLowerCase()] === undefined || (front
.tabviews
[chan
.toLowerCase()] !== undefined && front
.tabviews
[chan
.toLowerCase()].safe_to_close
=== true)) {
232 front
.tabviewAdd(chan
);
234 front
.tabviews
[chan
.toLowerCase()].show();
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()) {
248 front
.joinChannel(parts
[1]);
253 if (parts
[1] === undefined) {
254 alert('Usage: /connect servername [port]');
258 if (parts
[2] === undefined) {
261 front
.cur_channel
.addMsg(null, ' ', '=== Connecting to ' + parts
[1] + '...', 'status');
262 gateway
.connect(parts
[1], parts
[2], 0);
266 console
.log("/nick");
267 if (parts
[1] === undefined) {
268 console
.log("calling show nick");
269 front
.showChangeNick();
271 console
.log("sending raw");
272 gateway
.raw(msg
.substring(1));
277 if (typeof parts
[1] === "undefined") {
278 if (front
.cur_channel
.safe_to_close
) {
279 front
.cur_channel
.close();
281 gateway
.raw(msg
.substring(1) + ' ' + front
.cur_channel
.name
);
284 gateway
.raw(msg
.substring(1));
289 if (typeof parts
[1] !== "undefined") {
290 gateway
.raw(msg
.substring(1));
300 if (typeof parts
[1] !== "undefined") {
301 front
.tabviewAdd(parts
[1]);
306 gateway
.raw(msg
.replace(/^\/quote /i, ''));
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;');
317 msg
= parts
.slice(2).join(' ');
319 gateway
.notice(dest
, msg
);
320 this.onNotice({}, {nick
: gateway
.nick
, channel
: dest
, msg
: msg
});
324 if (parts
[1] !== undefined) {
325 front
.windowsShow(parseInt(parts
[1], 10));
330 gateway
.quit(msg
.split(" ", 2)[1]);
334 if (parts
[1] === undefined) {
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
);
343 } else if (t
.setSelectionRange
) {
344 t
.setSelectionRange(pos
, pos
);
347 gateway
.setTopic(front
.cur_channel
.name
, msg
.split(' ', 2)[1]);
348 //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
352 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
353 gateway
.raw(msg
.substring(1));
357 //alert('Sending message: '+msg);
358 if (msg
.trim() === '') {
361 if (front
.cur_channel
.name
!== 'server') {
362 gateway
.msg(front
.cur_channel
.name
, msg
);
364 d
= d
.getHours() + ":" + d
.getMinutes();
365 //front.addMsg(d, gateway.nick, msg);
366 front
.cur_channel
.addMsg(null, gateway
.nick
, msg
);
372 onMsg: function (e
, data
) {
374 // Is this message from a user?
375 if (data
.channel
=== gateway
.nick
) {
376 destination
= data
.nick
.toLowerCase();
378 destination
= data
.channel
.toLowerCase();
381 if (!front
.tabviewExists(destination
)) {
382 front
.tabviewAdd(destination
);
384 front
.tabviews
[destination
].addMsg(null, data
.nick
, data
.msg
);
387 onDebug: function (e
, data
) {
388 if (!front
.tabviewExists('kiwi_debug')) {
389 front
.tabviewAdd('kiwi_debug');
391 front
.tabviews
.kiwi_debug
.addMsg(null, ' ', data
.msg
);
394 onAction: function (e
, data
) {
396 // Is this message from a user?
397 if (data
.channel
=== gateway
.nick
) {
398 destination
= data
.nick
;
400 destination
= data
.channel
;
403 if (!front
.tabviewExists(destination
)) {
404 front
.tabviewAdd(destination
);
406 front
.tabviews
[destination
.toLowerCase()].addMsg(null, ' ', '* ' + data
.nick
+ ' ' + data
.msg
, 'action', 'color:#555;');
409 onTopic: function (e
, data
) {
410 if (front
.tabviewExists(data
.channel
)) {
411 front
.tabviews
[data
.channel
.toLowerCase()].changeTopic(data
.topic
);
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');
421 front
.tabviews
.server
.addMsg(null, nick
, data
.msg
, 'notice');
424 front
.tabviews
.server
.addMsg(null, nick
, data
.msg
, 'notice');
428 onCTCPRequest: function (e
, data
) {
429 var msg
= data
.msg
.split(" ", 2);
432 if (typeof msg
[1] === 'undefined') {
435 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'PING ' + msg
[1] + String
.fromCharCode(1));
438 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String
.fromCharCode(1));
441 front
.tabviews
.server
.addMsg(null, 'CTCP [' + data
.nick
+ ']', data
.msg
, 'ctcp');
444 onCTCPResponse: function (e
, data
) {
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
);
454 front
.tabviews
.server
.addMsg(null, ' ', '=== Failed to connect :(', 'status');
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');
461 onDisconnect: function (e
, data
) {
463 for (tab
in front
.tabviews
) {
464 front
.tabviews
[tab
].addMsg(null, '', 'Disconnected from server!', 'error');
467 onOptions: function (e
, data
) {
468 if (typeof gateway
.network_name
=== "string" && gateway
.network_name
!== "") {
469 front
.tabviews
.server
.tab
.text(gateway
.network_name
);
472 onMOTD: function (e
, data
) {
473 front
.tabviews
.server
.addMsg(null, data
.server
, data
.msg
, 'motd');
475 onWhois: function (e
, data
) {
478 front
.cur_channel
.addMsg(null, data
.nick
, data
.msg
, 'whois');
479 } else if (data
.logon
) {
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');
485 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' seconds', 'whois');
488 onUserList: function (e
, data
) {
490 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
493 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
495 if (!document
.userlist_updating
) {
496 document
.userlist_updating
= true;
500 $.each(data
.users
, function (i
, item
) {
501 nick
= i
; //i.match(/^.+!/g);
503 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
506 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
508 onUserListEnd: function (e
, data
) {
509 document
.userlist_updating
= false;
512 onJoin: function (e
, data
) {
513 if (!front
.tabviewExists(data
.channel
)) {
514 front
.tabviewAdd(data
.channel
.toLowerCase());
517 if (data
.nick
=== gateway
.nick
) {
518 return; // Not needed as it's already in nicklist
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();
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();
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
) {
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();
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
) {
560 onNick: function (e
, data
) {
561 if (data
.nick
=== gateway
.nick
) {
562 gateway
.nick
= data
.newnick
;
566 $.each(front
.tabviews
, function (i
, item
) {
567 $.each(front
.tabviews
, function (i
, item
) {
568 item
.changeNick(data
.newnick
, data
.nick
);
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
) {
578 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
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');
590 onIRCError: function (e
, data
) {
592 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
593 t_view
= data
.channel
;
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;
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;
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;
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;
623 case 'chanop_privs_needed':
624 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
627 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
630 // We don't know what data contains, so don't do anything with it.
631 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
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;
643 case (e
.which
>= 48) && (e
.which
<= 57):
650 front
.windowsShow(num
);
654 case e
.which
=== 27: // escape
656 case e
.which
=== 13: // return
657 msg
= $('#kiwi_msginput').val();
660 front
.buffer
.push(msg
);
661 front
.buffer_pos
= front
.buffer
.length
;
664 $('#kiwi_msginput').val('');
667 case e
.which
=== 33: // page up
668 console
.log("page up");
669 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
671 case e
.which
=== 34: // page down
672 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
674 case e
.which
=== 37: // left
676 front
.windowsPrevious();
680 case e
.which
=== 38: // up
681 if (front
.buffer_pos
> 0) {
683 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
686 case e
.which
=== 39: // right
692 case e
.which
=== 40: // down
693 if (front
.buffer_pos
< front
.buffer
.length
) {
695 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
699 case e
.which
=== 9: // tab
700 // Get possible autocompletions
702 front
.cur_channel
.userlist
.children().each(function () {
703 nick
= front
.nickStripPrefix($('a.nick', this).text());
707 // Do the autocomplete
708 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
711 word_pos
= this.value
.lastIndexOf(' ');
713 if (word_pos
=== -1) {
716 word
= this.value
.substr(word_pos
);
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
]);
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
;
738 $('#kiwi .control .msginput .nick').click(function () {
739 front
.showChangeNick();
746 $('#kiwi .plugins .load_plugin_file').click(function () {
748 if (typeof front
.boxes
.plugins
!== "undefined") {
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));
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>');
765 $('#kiwi .plugin_file').submit(function () {
766 $.getJSON($('.txtpluginfile').val(), function (data
) {
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
);
774 if (plg
.onload
instanceof Function
) {
780 $('#kiwi .cancelpluginfile').click(function () {
781 front
.boxes
.plugins
.destroy();
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();
793 delete plugins
.privmsg
[i
];
798 $('#kiwi .txtpluginfile').focus();
802 $('#kiwi .plugins .reload_css').click(function () {
803 var links
= document
.getElementsByTagName("link"),
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
+= "?";
810 links
[i
].href
+= "x";
816 $('#kiwi .about .about_close').click(function () {
817 $('#kiwi .about').css('display', 'none');
821 $('#kiwi .poweredby').click(function () {
822 $('#kiwi .about').css('display', 'block');
828 showChangeNick: function () {
829 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
831 $('#kiwi .form_newnick').submit(function () {
832 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
833 $('#kiwi .newnick').remove();
837 $('#kiwi .txtnewnick').keypress(function (ev
) {
838 if (!this.first_press
) {
839 this.first_press
= true;
844 $('#kiwi .txtnewnick').keydown(function (ev
) {
845 if (ev
.which
=== 27) { // ESC
846 $('#kiwi_msginput').focus();
847 $('#kiwi .newnick').remove();
851 $('#kiwi .cancelnewnick').click(function () {
852 $('#kiwi .newnick').remove();
855 $('#kiwi .txtnewnick').focus();
859 tabviewExists: function (name
) {
860 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
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
;
870 htmlsafe_name
= 'query_' + v_name
;
873 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
874 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
875 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
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>');
882 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
883 //front.windowShow(v_name);
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();
892 if (typeof registerTouches
=== "function") {
893 //alert("Registering touch interface");
894 //registerTouches($('#'+tmp_divname));
895 registerTouches(document
.getElementById(tmp_divname
));
898 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
899 alert($(this).attr('id'));
903 front
.doLayoutSize();
907 userClick: function (item
) {
908 // Remove any existing userboxes
909 $('#kiwi .userbox').remove();
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>\
918 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
920 $('#kiwi .userbox .userbox_query').click(function (ev
) {
921 var nick
= $('#kiwi .userbox_nick').val();
922 front
.run('/query ' + nick
);
925 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
926 var nick
= $('#kiwi .userbox_nick').val();
927 front
.run('/whois ' + nick
);
936 onSync: function (e
, data
) {
938 if (data
.nick
!== undefined) {
939 gateway
.nick
= data
.nick
;
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
);
948 if (tab
.userlist
!== undefined) {
949 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
959 setTopicText: function (new_topic
) {
960 front
.original_topic
= new_topic
;
961 $('#kiwi .cur_topic .topic').text(new_topic
);
962 front
.doLayoutSize();
971 nickStripPrefix: function (nick
) {
974 prefix
= tmp
.charAt(0);
975 for (i
in gateway
.user_prefixes
) {
976 if (gateway
.user_prefixes
[i
].symbol
!== prefix
) {
979 return tmp
.substring(1);
985 nickGetPrefix: function (nick
) {
988 prefix
= tmp
.charAt(0);
989 for (i
in gateway
.user_prefixes
) {
990 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
998 isChannel: function (name
) {
999 prefix
= name
.charAt(0);
1000 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
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
);
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
);
1021 windowsNext: function () {
1024 for (tab
in front
.tabviews
) {
1026 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1031 front
.tabviews
[tab
].show();
1037 windowsPrevious: function () {
1038 var tab
, prev_tab
, next
;
1040 for (tab
in front
.tabviews
) {
1041 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1047 prev_tab
= front
.tabviews
[tab
];
1051 windowsShow: function (num
) {
1052 num
= parseInt(num
, 10);
1053 console
.log('Showing window ' + num
.toString());
1055 for (tab
in front
.tabviews
) {
1057 front
.tabviews
[tab
].show();
1084 var Utilityview = function (name
, src
) {
1086 var tmp_divname
= 'kiwi_window_' + name
;
1087 var tmp_userlistname
= 'kiwi_userlist_' + name
;
1088 var tmp_tabname
= 'kiwi_tab_' + name
;
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>');
1097 this.div
= $('#' + tmp_divname
);
1098 this.div
.css('overflow', 'hidden');
1100 this.tab
= $('#' + tmp_tabname
);
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
);
1106 front
.tabviews
[name
.toLowerCase()] = this;
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");
1118 $('#windows').css('overflow-y', 'hidden');
1120 // Activate this tab!
1121 this.div
.addClass('active');
1122 this.tab
.addClass('active');
1124 this.addPartImage();
1126 front
.setTopicText(' ');
1127 front
.cur_channel
= this;
1129 // If we're using fancy scrolling, refresh it
1131 touch_scroll
.refresh();
1135 Utilityview
.prototype.close = function () {
1139 if (front
.cur_channel
=== this) {
1140 front
.tabviews
.server
.show();
1142 delete front
.tabviews
[this.name
.toLowerCase()];
1145 Utilityview
.prototype.addPartImage = function () {
1146 this.clearPartImage();
1148 // We can't close this tab, so don't have the close image
1149 if (this.name
=== 'server') {
1153 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1154 this.tab
.append(del_html
);
1156 $('.tab_part', this.tab
).click(function () {
1157 if (front
.cur_channel
.name
!== 'server') {
1158 front
.cur_channel
.close();
1163 Utilityview
.prototype.clearPartImage = function () {
1164 $('#kiwi .windowlist .tab_part').remove();
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
1186 Tabview
.prototype.show = function () {
1187 $('#kiwi .messages').removeClass("active");
1188 $('#kiwi .userlist ul').removeClass("active");
1189 $('#kiwi .windowlist ul li').removeClass("active");
1191 $('#windows').css('overflow-y', 'scroll');
1193 // Activate this tab!
1194 this.div
.addClass('active');
1195 this.userlist
.addClass('active');
1196 this.tab
.addClass('active');
1198 // Add the part image to the tab
1199 this.addPartImage();
1201 this.clearHighlight();
1202 front
.setTopicText(this.topic
);
1203 front
.cur_channel
= this;
1205 // If we're using fancy scrolling, refresh it
1207 touch_scroll
.refresh();
1210 this.scrollBottom();
1212 $('#kiwi_msginput').focus();
1216 Tabview
.prototype.close = function () {
1218 this.userlist
.remove();
1221 if (front
.cur_channel
=== this) {
1222 front
.tabviews
.server
.show();
1224 delete front
.tabviews
[this.name
.toLowerCase()];
1227 Tabview
.prototype.addPartImage = function () {
1228 this.clearPartImage();
1230 // We can't close this tab, so don't have the close image
1231 if (this.name
=== 'server') {
1235 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1236 this.tab
.append(del_html
);
1238 $('.tab_part', this.tab
).click(function () {
1239 if (front
.isChannel($(this).parent().text())) {
1242 // Make sure we don't close the server tab
1243 if (front
.cur_channel
.name
!== 'server') {
1244 front
.cur_channel
.close();
1250 Tabview
.prototype.clearPartImage = function () {
1251 $('#kiwi .windowlist .tab_part').remove();
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();
1262 for (i
in plugins
.privmsg
) {
1263 if ((plugins
.privmsg
[i
].onprivmsg
instanceof Function
)) {
1266 plugin_ret
= plugins
.privmsg
[i
].onprivmsg(tmp
, this.name
);
1268 // If this plugin has returned false, do not add this message
1269 if (plugin_ret
=== false) {
1275 // If we actually have a string from the plugin, use it
1276 if (typeof plugin_ret
=== "string") {
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) {
1286 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1289 // The CSS class (action, topic, notice, etc)
1290 if (typeof type
!== "string") {
1294 // Make sure we don't have NaN or something
1295 if (typeof msg
!== "string") {
1301 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1303 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1304 msg
= msg
.replace(String
.fromCharCode(2), next
);
1305 next
= (next
=== '<b>') ? '</b>' : '<b>';
1307 if (next
=== '</b>') {
1312 // Wierd thing noticed by Dux0r on the irc.esper.net server
1313 if (typeof msg
!== "string") {
1318 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1320 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1321 msg
= msg
.replace(String
.fromCharCode(31), next
);
1322 next
= (next
=== '<u>') ? '</u>' : '<u>';
1324 if (next
=== '</u>') {
1329 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1331 msg
= msg
.replace(re
, function (match
) {
1332 return '<a class="chan" href="#">' + match
+ '</a>';
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] });
1338 this.div
.append(line_msg
);
1341 this.scrollBottom();
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());
1352 Tabview
.prototype.scrollBottom = function () {
1353 var w
= $('#windows');
1354 w
[0].scrollTop
= w
[0].scrollHeight
;
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;
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();
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(),
1381 // Sort by prefixes first
1382 for (i
in gateway
.user_prefixes
) {
1383 prefix
= gateway
.user_prefixes
[i
].symbol
;
1385 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1386 // Both have the same prefix, string compare time
1390 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1394 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1399 // No prefixes, compare by string
1400 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1402 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1405 Tabview
.prototype.highlight = function () {
1406 this.tab
.addClass('highlight');
1408 Tabview
.prototype.activity = function () {
1409 this.tab
.addClass('activity');
1411 Tabview
.prototype.clearHighlight = function () {
1412 this.tab
.removeClass('highlight');
1413 this.tab
.removeClass('activity');
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
);
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');
1434 this.box
.draggable({ stack
: ".box" });
1435 this.content
.click(function () {});
1436 //this.box.click(function(){ $(this)..css });
1438 Box
.prototype.create = function (name
, classname
) {
1441 Box
.prototype.id
= null;
1442 Box
.prototype.box
= null;
1443 Box
.prototype.content
= null;
1444 Box
.prototype.destroy = function () {
1447 for (name
in front
.boxes
) {
1448 if (front
.boxes
[name
].id
=== this.id
) {
1449 delete front
.boxes
[name
];
1453 Box
.prototype.height = function () {
1454 return this.box
.height();