1 /*jslint devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */
2 /*global gateway, io, $, iScroll, agent, touchscreen, init_data, plugs, plugins, registerTouches, randomString */
18 /*global Box, touch_scroll:true */
19 var about_info
, supportsOrientationChange
, orientationEvent
, scroll_opts
;
20 gateway
.nick
= 'kiwi_' + Math
.ceil(100 * Math
.random()) + Math
.ceil(100 * Math
.random());
21 gateway
.session_id
= null;
23 $(gateway
).bind("onmsg", front
.onMsg
);
24 $(gateway
).bind("onnotice", front
.onNotice
);
25 $(gateway
).bind("onaction", front
.onAction
);
26 $(gateway
).bind("onmotd", front
.onMOTD
);
27 $(gateway
).bind("onoptions", front
.onOptions
);
28 $(gateway
).bind("onconnect", front
.onConnect
);
29 $(gateway
).bind("onconnect_fail", front
.onConnectFail
);
30 $(gateway
).bind("ondisconnect", front
.onDisconnect
);
31 $(gateway
).bind("onnick", front
.onNick
);
32 $(gateway
).bind("onuserlist", front
.onUserList
);
33 $(gateway
).bind("onuserlist_end", front
.onUserListEnd
);
34 $(gateway
).bind("onjoin", front
.onJoin
);
35 $(gateway
).bind("ontopic", front
.onTopic
);
36 $(gateway
).bind("onpart", front
.onPart
);
37 $(gateway
).bind("onkick", front
.onKick
);
38 $(gateway
).bind("onquit", front
.onQuit
);
39 $(gateway
).bind("onmode", front
.onMode
);
40 $(gateway
).bind("onwhois", front
.onWhois
);
41 $(gateway
).bind("onsync", front
.onSync
);
42 $(gateway
).bind("onchannel_redirect", front
.onChannelRedirect
);
43 $(gateway
).bind("ondebug", front
.onDebug
);
44 $(gateway
).bind("onctcp_request", front
.onCTCPRequest
);
45 $(gateway
).bind("onctcp_response", front
.onCTCPResponse
);
46 $(gateway
).bind("onirc_error", front
.onIRCError
);
47 $(gateway
).bind("onkiwi", front
.onKiwi
);
51 // Build the about box
52 front
.boxes
.about
= new Box("about");
53 about_info
= 'UI adapted for ' + agent
;
55 about_info
+= ' touchscreen ';
57 about_info
+= 'usage';
58 $('#tmpl_about_box').tmpl({
60 front_revision
: front
.revision
,
61 gateway_revision
: gateway
.revision
62 }).appendTo(front
.boxes
.about
.content
);
64 //$(window).bind("beforeunload", function(){ gateway.quit(); });
67 $('#kiwi').addClass('touchscreen');
69 // Single touch scrolling through scrollback for touchscreens
71 touch_scroll
= new iScroll('windows', scroll_opts
);
76 $('#kiwi .toolbars').resize(front
.doLayoutSize
);
78 $('#kiwi .formconnectwindow').submit(function () {
79 var netsel
= $('#kiwi .formconnectwindow .network'),
80 nick
= $('#kiwi .formconnectwindow .nick'),
83 if (nick
.val() === '') {
84 nick
.val('Nick please!');
89 tmp
= nick
.val().split(' ');
90 gateway
.nick
= tmp
[0];
92 init_data
.channel
= $('#channel').val();
96 front
.run('/connect ' + netsel
.val());
101 $('#kiwi .connectwindow').slideUp('', front
.barsShow
);
102 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
107 supportsOrientationChange
= (typeof window
.onorientationchange
!== undefined);
108 orientationEvent
= supportsOrientationChange
? "orientationchange" : "resize";
109 if (window
.addEventListener
) {
110 window
.addEventListener(orientationEvent
, front
.doLayoutSize
, false);
113 window
.attachEvent(orientationEvent
, front
.doLayoutSize
, false);
115 //$('#kiwi').bind("resize", front.doLayoutSize, false);
120 front
.tabviewAdd('server');
121 front
.tabviews
.server
.userlist_width
= 0; // Disable the userlist
123 // Any pre-defined nick?
124 if (typeof window
.init_data
.nick
=== "string") {
125 $('#kiwi .formconnectwindow .nick').val(init_data
.nick
);
128 // Any pre-defined channels?
129 if (typeof window
.init_data
.channel
=== 'string') {
130 $('#channel').val(init_data
.channel
);
133 // Fix for Opera inserting a spurious <br/>
134 $('#kiwi .cur_topic br').remove();
136 $('#kiwi .cur_topic').keydown(function (e
) {
137 if (e
.which
=== 13) {
141 $('#kiwi_msginput').focus();
142 } else if (e
.which
=== 27) {
145 $(this).text(front
.original_topic
);
146 $('#kiwi_msginput').focus();
149 /*$('.cur_topic').live('keypress', function(e) {
150 if (e.keyCode === 13) {
154 $('#kiwi_msginput').focus();
155 } else if (e.keyCode === 27) {
158 $(this).text(front.original_topic);
161 $('.cur_topic').live('change', function () {
163 text
= $(this).text();
164 if (text
!== front
.original_topic
) {
165 chan
= front
.cur_channel
.name
;
166 gateway
.setTopic(chan
, text
);
171 $('#windows a.chan').live('click', function () {
172 front
.joinChannel($(this).text());
179 plugs
.loadPlugin(plugins
[i
]);
184 doLayoutSize: function () {
185 var kiwi
, toolbars
, ul
, n_top
, n_bottom
;
188 if (kiwi
.width() < 330 && !kiwi
.hasClass('small_kiwi')) {
189 console
.log("switching to small kiwi");
190 kiwi
.removeClass('large_kiwi');
191 kiwi
.addClass('small_kiwi');
192 } else if (kiwi
.width() >= 330 && !kiwi
.hasClass('large_kiwi')) {
193 kiwi
.removeClass('small_kiwi');
194 kiwi
.addClass('large_kiwi');
197 toolbars
= $('#kiwi .cur_topic');
198 ul
= $('#kiwi .userlist');
200 n_top
= parseInt(toolbars
.offset().top
, 10) + parseInt(toolbars
.outerHeight(true), 10);
201 n_bottom
= $(document
).height() - parseInt($('#kiwi .control').offset().top
, 10);
203 $('#kiwi .windows').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
204 $('#kiwi .userlist').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
208 doLayout: function () {
209 $('#kiwi .msginput .nick a').text(gateway
.nick
);
210 $('#kiwi_msginput').val(' ');
211 $('#kiwi_msginput').focus();
215 joinChannel: function (chan_name
) {
216 var chans
= chan_name
.split(','),
221 if (front
.tabviews
[chan
.toLowerCase()] === undefined || (front
.tabviews
[chan
.toLowerCase()] !== undefined && front
.tabviews
[chan
.toLowerCase()].safe_to_close
=== true)) {
223 front
.tabviewAdd(chan
);
225 front
.tabviews
[chan
.toLowerCase()].show();
231 run: function (msg
) {
232 var parts
, dest
, t
, pos
, textRange
, d
, plugin_event
, msg_sliced
;
234 // Run through any plugins
235 plugin_event
= {command
: msg
};
236 plugin_event
= plugs
.run('command_run', plugin_event
);
237 if (!plugin_event
|| typeof plugin_event
.command
=== 'undefined') {
241 // Update msg if it's been changed by any plugins
242 msg
= plugin_event
.command
.toString();
244 console
.log("running " + msg
);
245 if (msg
.substring(0, 1) === '/') {
246 parts
= msg
.split(' ');
247 switch (parts
[0].toLowerCase()) {
250 front
.joinChannel(parts
[1]);
255 if (parts
[1] === undefined) {
256 alert('Usage: /connect servername [port]');
260 if (parts
[2] === undefined) {
263 front
.cur_channel
.addMsg(null, ' ', '=== Connecting to ' + parts
[1] + '...', 'status');
264 gateway
.connect(parts
[1], parts
[2], 0);
268 console
.log("/nick");
269 if (parts
[1] === undefined) {
270 console
.log("calling show nick");
271 front
.showChangeNick();
273 console
.log("sending raw");
274 gateway
.raw(msg
.substring(1));
279 if (typeof parts
[1] === "undefined") {
280 if (front
.cur_channel
.safe_to_close
) {
281 front
.cur_channel
.close();
283 gateway
.raw(msg
.substring(1) + ' ' + front
.cur_channel
.name
);
286 gateway
.raw(msg
.substring(1));
291 if (typeof parts
[1] !== "undefined") {
292 gateway
.raw(msg
.substring(1));
302 if (typeof parts
[1] !== "undefined") {
303 front
.tabviewAdd(parts
[1]);
310 if (typeof parts
[1] !== "undefined") {
311 msg_sliced
= msg
.split(' ').slice(2).join(' ');
312 gateway
.msg(parts
[1], msg_sliced
);
314 if (!front
.tabviewExists(parts
[1])) {
315 front
.tabviewAdd(parts
[1]);
317 front
.tabviews
[parts
[1].toLowerCase()].addMsg(null, gateway
.nick
, msg_sliced
);
323 gateway
.raw(msg
.replace(/^\/quote /i, ''));
327 gateway
.action(front
.cur_channel
.name
, msg
.substring(4));
328 //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
329 front
.cur_channel
.addMsg(null, ' ', '* ' + gateway
.nick
+ ' ' + msg
.substring(4), 'action', 'color:#555;');
334 msg
= parts
.slice(2).join(' ');
336 gateway
.notice(dest
, msg
);
337 this.onNotice({}, {nick
: gateway
.nick
, channel
: dest
, msg
: msg
});
341 if (parts
[1] !== undefined) {
342 front
.windowsShow(parseInt(parts
[1], 10));
347 gateway
.quit(msg
.split(" ", 2)[1]);
351 if (parts
[1] === undefined) {
353 if (t
.createTextRange
) {
354 pos
= t
.text().length();
355 textRange
= t
.createTextRange();
356 textRange
.collapse(true);
357 textRange
.moveEnd(pos
);
358 textRange
.moveStart(pos
);
360 } else if (t
.setSelectionRange
) {
361 t
.setSelectionRange(pos
, pos
);
364 gateway
.setTopic(front
.cur_channel
.name
, msg
.split(' ', 2)[1]);
365 //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
370 gateway
.kiwi(front
.cur_channel
.name
, msg
.substring(6));
374 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
375 gateway
.raw(msg
.substring(1));
379 //alert('Sending message: '+msg);
380 if (msg
.trim() === '') {
383 if (front
.cur_channel
.name
!== 'server') {
384 gateway
.msg(front
.cur_channel
.name
, msg
);
386 d
= d
.getHours() + ":" + d
.getMinutes();
387 //front.addMsg(d, gateway.nick, msg);
388 front
.cur_channel
.addMsg(null, gateway
.nick
, msg
);
394 onMsg: function (e
, data
) {
395 var destination
, plugin_event
;
396 // Is this message from a user?
397 if (data
.channel
=== gateway
.nick
) {
398 destination
= data
.nick
.toLowerCase();
400 destination
= data
.channel
.toLowerCase();
403 plugin_event
= {nick
: data
.nick
, msg
: data
.msg
, destination
: destination
};
404 plugin_event
= plugs
.run('msg_recieved', plugin_event
);
409 if (!front
.tabviewExists(plugin_event
.destination
)) {
410 front
.tabviewAdd(plugin_event
.destination
);
412 front
.tabviews
[plugin_event
.destination
].addMsg(null, plugin_event
.nick
, plugin_event
.msg
);
415 onDebug: function (e
, data
) {
416 if (!front
.tabviewExists('kiwi_debug')) {
417 front
.tabviewAdd('kiwi_debug');
419 front
.tabviews
.kiwi_debug
.addMsg(null, ' ', data
.msg
);
422 onAction: function (e
, data
) {
424 // Is this message from a user?
425 if (data
.channel
=== gateway
.nick
) {
426 destination
= data
.nick
;
428 destination
= data
.channel
;
431 if (!front
.tabviewExists(destination
)) {
432 front
.tabviewAdd(destination
);
434 front
.tabviews
[destination
.toLowerCase()].addMsg(null, ' ', '* ' + data
.nick
+ ' ' + data
.msg
, 'action', 'color:#555;');
437 onTopic: function (e
, data
) {
438 if (front
.tabviewExists(data
.channel
)) {
439 front
.tabviews
[data
.channel
.toLowerCase()].changeTopic(data
.topic
);
443 onNotice: function (e
, data
) {
444 var nick
= (data
.nick
=== undefined) ? '' : data
.nick
,
445 enick
= '[' + nick
+ ']';
447 if (front
.tabviewExists(data
.target
)) {
448 front
.tabviews
[data
.target
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
449 } else if (front
.tabviewExists(nick
)) {
450 front
.tabviews
[nick
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
452 front
.tabviews
.server
.addMsg(null, enick
, data
.msg
, 'notice');
456 onCTCPRequest: function (e
, data
) {
457 var msg
= data
.msg
.split(" ", 2);
460 if (typeof msg
[1] === 'undefined') {
463 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'PING ' + msg
[1] + String
.fromCharCode(1));
466 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String
.fromCharCode(1));
469 front
.tabviews
.server
.addMsg(null, 'CTCP [' + data
.nick
+ ']', data
.msg
, 'ctcp');
472 onCTCPResponse: function (e
, data
) {
475 onKiwi: function (e
, data
) {
479 onConnect: function (e
, data
) {
480 if (data
.connected
) {
481 if (gateway
.nick
!== data
.nick
) {
482 gateway
.nick
= data
.nick
;
486 front
.tabviews
.server
.addMsg(null, ' ', '=== Connected OK :)', 'status');
487 if (typeof init_data
.channel
=== "string") {
488 front
.joinChannel(init_data
.channel
);
490 plugs
.run('connect', {success
: true});
492 front
.tabviews
.server
.addMsg(null, ' ', '=== Failed to connect :(', 'status');
493 plugs
.run('connect', {success
: false});
496 onConnectFail: function (e
, data
) {
497 var reason
= (typeof data
.reason
=== 'string') ? data
.reason
: '';
498 front
.tabviews
.server
.addMsg(null, '', 'There\'s a problem connecting! (' + reason
+ ')', 'error');
499 plugs
.run('connect', {success
: false});
501 onDisconnect: function (e
, data
) {
503 for (tab
in front
.tabviews
) {
504 front
.tabviews
[tab
].addMsg(null, '', 'Disconnected from server!', 'error');
506 plugs
.run('disconnect', {success
: false});
508 onOptions: function (e
, data
) {
509 if (typeof gateway
.network_name
=== "string" && gateway
.network_name
!== "") {
510 front
.tabviews
.server
.tab
.text(gateway
.network_name
);
513 onMOTD: function (e
, data
) {
514 front
.tabviews
.server
.addMsg(null, data
.server
, data
.msg
, 'motd');
516 onWhois: function (e
, data
) {
519 front
.cur_channel
.addMsg(null, data
.nick
, data
.msg
, 'whois');
520 } else if (data
.logon
) {
522 d
.setTime(data
.logon
* 1000);
523 d
= d
.toLocaleString();
524 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' second' + ((data
.idle
!== 1) ? 's' : '') + ', signed on ' + d
, 'whois');
526 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' seconds', 'whois');
529 onMode: function (e
, data
) {
530 var i
, new_nick_text
;
532 // TODO: Store the modes in the elements data, then work out the current
533 // mode symbol from the highest mode. Eg. -h may leave +o from previous modes; It
534 // doesn't simply clear it! ~Darren
535 if (typeof data
.channel
=== 'string' && typeof data
.effected_nick
=== 'string') {
536 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '[' + data
.mode
+ '] ' + data
.effected_nick
+ ' by ' + data
.nick
, 'mode', '');
537 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
538 if (front
.nickStripPrefix($(this).text()) === data
.effected_nick
) {
540 if (data
.mode
.split('')[0] === '+') {
541 for (i
in gateway
.user_prefixes
) {
542 if (gateway
.user_prefixes
[i
].mode
== data
.mode
.split('')[1]) {
543 new_nick_text
= gateway
.user_prefixes
[i
].symbol
+ data
.effected_nick
;
547 } else if (data
.mode
.split('')[0] === '-') {
548 new_nick_text
= data
.effected_nick
;
551 if (new_nick_text
!== '') {
552 $(this).text(new_nick_text
);
560 onUserList: function (e
, data
) {
562 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
565 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
567 if (!document
.userlist_updating
) {
568 document
.userlist_updating
= true;
572 $.each(data
.users
, function (i
, item
) {
573 nick
= i
; //i.match(/^.+!/g);
575 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
578 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
580 onUserListEnd: function (e
, data
) {
581 document
.userlist_updating
= false;
584 onJoin: function (e
, data
) {
585 if (!front
.tabviewExists(data
.channel
)) {
586 front
.tabviewAdd(data
.channel
.toLowerCase());
589 if (data
.nick
=== gateway
.nick
) {
590 return; // Not needed as it's already in nicklist
592 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '--> ' + data
.nick
+ ' has joined', 'action', 'color:#009900;');
593 $('<li><a class="nick" onclick="front.userClick(this);">' + data
.nick
+ '</a></li>').appendTo(front
.tabviews
[data
.channel
.toLowerCase()].userlist
);
594 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
596 onPart: function (e
, data
) {
597 if (front
.tabviewExists(data
.channel
)) {
598 // If this is us, close the tabview
599 if (data
.nick
=== gateway
.nick
) {
600 front
.tabviews
[data
.channel
.toLowerCase()].close();
601 front
.tabviews
.server
.show();
605 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.nick
+ ' has left (' + data
.message
+ ')', 'action', 'color:#990000;');
606 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
607 if ($(this).text() === data
.nick
) {
613 onKick: function (e
, data
) {
614 if (front
.tabviewExists(data
.channel
)) {
615 // If this is us, close the tabview
616 if (data
.kicked
=== gateway
.nick
) {
617 //front.tabviews[data.channel.toLowerCase()].close();
618 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data
.channel
+ '. ' + data
.message
, 'status');
619 front
.tabviews
[data
.channel
.toLowerCase()].safe_to_close
= true;
620 $('li', front
.tabviews
[data
.channel
.toLowerCase()].userlist
).remove();
624 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.kicked
+ ' kicked by ' + data
.nick
+ '(' + data
.message
+ ')', 'action', 'color:#990000;');
625 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
626 if ($(this).text() === data
.nick
) {
632 onNick: function (e
, data
) {
633 if (data
.nick
=== gateway
.nick
) {
634 gateway
.nick
= data
.newnick
;
638 $.each(front
.tabviews
, function (i
, item
) {
639 $.each(front
.tabviews
, function (i
, item
) {
640 item
.changeNick(data
.newnick
, data
.nick
);
644 onQuit: function (e
, data
) {
645 $.each(front
.tabviews
, function (i
, item
) {
646 $.each(front
.tabviews
, function (i
, item
) {
647 item
.userlist
.children().each(function () {
648 if ($(this).text() === data
.nick
) {
650 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
656 onChannelRedirect: function (e
, data
) {
657 front
.tabviews
[data
.from.toLowerCase()].close();
658 front
.tabviewAdd(data
.to
.toLowerCase());
659 front
.tabviews
[data
.to
.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data
.from, 'action');
662 onIRCError: function (e
, data
) {
664 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
665 t_view
= data
.channel
;
670 switch (data
.error
) {
671 case 'banned_from_channel':
672 front
.tabviews
[t_view
].addMsg(null, ' ', '=== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
673 if (t_view
!== 'server') {
674 front
.tabviews
[t_view
].safe_to_close
= true;
677 case 'bad_channel_key':
678 front
.tabviews
[t_view
].addMsg(null, ' ', '=== Bad channel key for ' + data
.channel
, 'status');
679 if (t_view
!== 'server') {
680 front
.tabviews
[t_view
].safe_to_close
= true;
683 case 'invite_only_channel':
684 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is invite only.', 'status');
685 if (t_view
!== 'server') {
686 front
.tabviews
[t_view
].safe_to_close
= true;
689 case 'channel_is_full':
690 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is full.', 'status');
691 if (t_view
!== 'server') {
692 front
.tabviews
[t_view
].safe_to_close
= true;
695 case 'chanop_privs_needed':
696 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
699 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
701 case 'nickname_in_use':
702 front
.tabviews
.server
.addMsg(null, ' ', '=== The nickname ' + data
.nick
+ ' is already in use. Please select a new nickname', 'status');
703 front
.showChangeNick();
706 // We don't know what data contains, so don't do anything with it.
707 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
711 registerKeys: function () {
712 $('#kiwi_msginput').bind('keydown', function (e
) {
713 var windows
, meta
, num
, msg
, data
, candidates
, word_pos
, word
, i
;
714 windows
= $('#windows');
715 //var meta = e.altKey;
719 case (e
.which
>= 48) && (e
.which
<= 57):
726 front
.windowsShow(num
);
730 case e
.which
=== 27: // escape
732 case e
.which
=== 13: // return
733 msg
= $('#kiwi_msginput').val();
736 front
.buffer
.push(msg
);
737 front
.buffer_pos
= front
.buffer
.length
;
740 $('#kiwi_msginput').val('');
743 case e
.which
=== 33: // page up
744 console
.log("page up");
745 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
747 case e
.which
=== 34: // page down
748 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
750 case e
.which
=== 37: // left
752 front
.windowsPrevious();
756 case e
.which
=== 38: // up
757 if (front
.buffer_pos
> 0) {
759 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
762 case e
.which
=== 39: // right
768 case e
.which
=== 40: // down
769 if (front
.buffer_pos
< front
.buffer
.length
) {
771 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
775 case e
.which
=== 9: // tab
776 // Get possible autocompletions
778 front
.cur_channel
.userlist
.children().each(function () {
780 nick
= front
.nickStripPrefix($('a.nick', this).text());
784 // Do the autocomplete
785 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
788 word_pos
= this.value
.lastIndexOf(' ');
790 if (word_pos
=== -1) {
793 word
= this.value
.substr(word_pos
);
797 // filter data to find only strings that start with existing value
798 for (i
= 0; i
< data
.length
; i
++) {
799 if (data
[i
].indexOf(word
) === 0 && data
[i
].length
> word
.length
) {
800 candidates
.push(data
[i
]);
804 if (candidates
.length
> 0) {
805 // some candidates for autocompletion are found
806 this.value
= this.value
.substring(0, word_pos
) + ' ' + candidates
[0] + ': ';
807 this.selectionStart
= this.value
.length
;
815 $('#kiwi .control .msginput .nick').click(function () {
816 front
.showChangeNick();
823 $('#kiwi .plugins .load_plugin_file').click(function () {
824 if (typeof front
.boxes
.plugins
!== "undefined") {
828 front
.boxes
.plugins
= new Box("plugin_file");
829 $('#tmpl_plugins').tmpl({}).appendTo(front
.boxes
.plugins
.content
);
830 front
.boxes
.plugins
.box
.css('top', -(front
.boxes
.plugins
.height
+ 40));
832 // Populate the plugin list..
833 function enumPlugins() {
835 lst
= $('#plugin_list');
836 lst
.find('option').remove();
837 for (j
in plugs
.loaded
) {
838 txt
= plugs
.loaded
[j
].name
;
839 lst
.append('<option value="' + txt
+ '">' + txt
+ '</option>');
845 $('#kiwi .plugin_file').submit(function () {
846 $('<div></div>').load($('.txtpluginfile').val(), function (e
) {
851 $('#kiwi .cancelpluginfile').click(function () {
852 front
.boxes
.plugins
.destroy();
855 $('#kiwi #plugins_list_unload').click(function () {
857 selected_plugin
= $('#plugin_list').val();
858 plugs
.unloadPlugin(selected_plugin
);
862 $('#kiwi .txtpluginfile').focus();
866 $('#kiwi .plugins .reload_css').click(function () {
867 var links
= document
.getElementsByTagName("link"),
869 for (i
= 0; i
< links
.length
; i
++) {
870 if (links
[i
].rel
=== "stylesheet") {
871 if (links
[i
].href
.indexOf("?") === -1) {
872 links
[i
].href
+= "?";
874 links
[i
].href
+= "x";
880 $('#kiwi .about .about_close').click(function () {
881 $('#kiwi .about').css('display', 'none');
885 $('#kiwi .poweredby').click(function () {
886 $('#kiwi .about').css('display', 'block');
892 showChangeNick: function () {
893 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
895 $('#kiwi .form_newnick').submit(function () {
896 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
897 $('#kiwi .newnick').remove();
901 $('#kiwi .txtnewnick').keypress(function (ev
) {
902 if (!this.first_press
) {
903 this.first_press
= true;
908 $('#kiwi .txtnewnick').keydown(function (ev
) {
909 if (ev
.which
=== 27) { // ESC
910 $('#kiwi_msginput').focus();
911 $('#kiwi .newnick').remove();
915 $('#kiwi .cancelnewnick').click(function () {
916 $('#kiwi .newnick').remove();
919 $('#kiwi .txtnewnick').focus();
923 tabviewExists: function (name
) {
924 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
927 tabviewAdd: function (v_name
) {
929 var re
, htmlsafe_name
, tmp_divname
, tmp_userlistname
, tmp_tabname
, userlist_enabled
= true;
931 if (v_name
.charAt(0) === gateway
.channel_prefix
) {
932 re
= new RegExp(gateway
.channel_prefix
, "g");
933 htmlsafe_name
= v_name
.replace(re
, 'pre');
934 htmlsafe_name
= "chan_" + htmlsafe_name
;
936 htmlsafe_name
= 'query_' + v_name
;
937 userlist_enabled
= false;
940 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
941 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
942 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
944 if (!front
.tabviewExists(v_name
)) {
945 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
946 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname
+ '"></ul>');
947 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + v_name
.toLowerCase() + '\'].show();">' + v_name
+ '</li>');
949 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
950 //front.windowShow(v_name);
952 front
.tabviews
[v_name
.toLowerCase()] = new Tabview();
953 front
.tabviews
[v_name
.toLowerCase()].name
= v_name
;
954 front
.tabviews
[v_name
.toLowerCase()].div
= $('#' + tmp_divname
);
955 front
.tabviews
[v_name
.toLowerCase()].userlist
= $('#' + tmp_userlistname
);
956 front
.tabviews
[v_name
.toLowerCase()].tab
= $('#' + tmp_tabname
);
957 if (!userlist_enabled
) {
958 front
.tabviews
[v_name
.toLowerCase()].userlist_width
= 0;
960 front
.tabviews
[v_name
.toLowerCase()].show();
962 if (typeof registerTouches
=== "function") {
963 //alert("Registering touch interface");
964 //registerTouches($('#'+tmp_divname));
965 registerTouches(document
.getElementById(tmp_divname
));
968 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
969 alert($(this).attr('id'));
973 front
.doLayoutSize();
977 userClick: function (item
) {
978 var li
= $(item
).parent();
980 // Remove any existing userboxes
981 $('#kiwi .userbox').remove();
983 if ($(li
).data('userbox') === item
) {
984 $(li
).removeData('userbox');
986 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
988 $('#kiwi .userbox .userbox_query').click(function (ev
) {
989 var nick
= $('#kiwi .userbox_nick').val();
990 front
.run('/query ' + nick
);
993 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
994 var nick
= $('#kiwi .userbox_nick').val();
995 front
.run('/whois ' + nick
);
997 $(li
).data('userbox', item
);
1006 onSync: function (e
, data
) {
1008 if (data
.nick
!== undefined) {
1009 gateway
.nick
= data
.nick
;
1013 if (data
.tabviews
!== undefined) {
1014 $.each(data
.tabviews
, function (i
, tab
) {
1015 if (!front
.tabviewExists(tab
.name
)) {
1016 front
.tabviewAdd(gateway
.channel_prefix
+ tab
.name
);
1018 if (tab
.userlist
!== undefined) {
1019 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
1029 setTopicText: function (new_topic
) {
1030 front
.original_topic
= new_topic
;
1031 $('#kiwi .cur_topic .topic').text(new_topic
);
1032 front
.doLayoutSize();
1041 nickStripPrefix: function (nick
) {
1042 var tmp
= nick
, i
, prefix
;
1044 prefix
= tmp
.charAt(0);
1045 for (i
in gateway
.user_prefixes
) {
1046 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
1047 return tmp
.substring(1);
1054 nickGetPrefix: function (nick
) {
1055 var tmp
= nick
, i
, prefix
;
1057 prefix
= tmp
.charAt(0);
1058 for (i
in gateway
.user_prefixes
) {
1059 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
1067 isChannel: function (name
) {
1068 var prefix
, is_chan
;
1069 prefix
= name
.charAt(0);
1070 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
1079 tabviewsNext: function () {
1080 var wl
= $('#kiwi .windowlist ul'),
1081 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) + 170;
1082 wl
.css('text-indent', next_left
);
1085 tabviewsPrevious: function () {
1086 var wl
= $('#kiwi .windowlist ul'),
1087 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) - 170;
1088 wl
.css('text-indent', next_left
);
1091 windowsNext: function () {
1094 for (tab
in front
.tabviews
) {
1096 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1101 front
.tabviews
[tab
].show();
1107 windowsPrevious: function () {
1108 var tab
, prev_tab
, next
;
1110 for (tab
in front
.tabviews
) {
1111 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1117 prev_tab
= front
.tabviews
[tab
];
1121 windowsShow: function (num
) {
1122 num
= parseInt(num
, 10);
1123 console
.log('Showing window ' + num
.toString());
1125 for (tab
in front
.tabviews
) {
1127 front
.tabviews
[tab
].show();
1136 barsShow: function () {
1137 $('#kiwi .toolbars').slideDown();
1138 $('#kiwi .control').slideDown();
1141 barsHide: function () {
1142 $('#kiwi .toolbars').slideUp();
1143 $('#kiwi .control').slideUp();
1166 var Utilityview = function (name
) {
1167 var rand_name
= randomString(15),
1168 tmp_divname
= 'kiwi_window_' + rand_name
,
1169 tmp_userlistname
= 'kiwi_userlist_' + rand_name
,
1170 tmp_tabname
= 'kiwi_tab_' + rand_name
;
1172 this.name
= rand_name
;
1176 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
1178 this.tab
= $('<li id="' + tmp_tabname
+ '">' + this.title
+ '</li>');
1179 this.tab
.click(function () {
1180 front
.utilityviews
[rand_name
.toLowerCase()].show();
1182 $('#kiwi .utilityviewlist ul').append(this.tab
);
1184 this.div
= $('#' + tmp_divname
);
1185 this.div
.css('overflow', 'hidden');
1187 front
.utilityviews
[rand_name
.toLowerCase()] = this;
1190 Utilityview
.prototype.name
= null;
1191 Utilityview
.prototype.title
= null;
1192 Utilityview
.prototype.div
= null;
1193 Utilityview
.prototype.tab
= null;
1194 Utilityview
.prototype.topic
= ' ';
1195 Utilityview
.prototype.show = function () {
1196 $('#kiwi .messages').removeClass("active");
1197 $('#kiwi .userlist ul').removeClass("active");
1198 $('#kiwi .toolbars ul li').removeClass("active");
1200 $('#windows').css('overflow-y', 'hidden');
1201 $('#windows').css('right', 0);
1202 // Activate this tab!
1203 this.div
.addClass('active');
1204 this.tab
.addClass('active');
1206 this.addPartImage();
1208 front
.setTopicText(this.topic
);
1209 front
.cur_channel
= this;
1211 // If we're using fancy scrolling, refresh it
1213 touch_scroll
.refresh();
1217 Utilityview
.prototype.close = function () {
1221 if (front
.cur_channel
=== this) {
1222 front
.tabviews
.server
.show();
1224 delete front
.utilityviews
[this.name
.toLowerCase()];
1227 Utilityview
.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
.cur_channel
.name
!== 'server') {
1240 front
.cur_channel
.close();
1245 Utilityview
.prototype.clearPartImage = function () {
1246 $('#kiwi .toolbars .tab_part').remove();
1260 var Tabview = function () {};
1261 Tabview
.prototype.name
= null;
1262 Tabview
.prototype.div
= null;
1263 Tabview
.prototype.userlist
= null;
1264 Tabview
.prototype.userlist_width
= 100; // 0 for disabled
1265 Tabview
.prototype.tab
= null;
1266 Tabview
.prototype.topic
= "";
1267 Tabview
.prototype.safe_to_close
= false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1269 Tabview
.prototype.show = function () {
1272 $('#kiwi .messages').removeClass("active");
1273 $('#kiwi .userlist ul').removeClass("active");
1274 $('#kiwi .toolbars ul li').removeClass("active");
1277 u
= $('#kiwi .userlist');
1279 w
.css('overflow-y', 'scroll');
1281 // Set the window size accordingly
1282 if (this.userlist_width
> 0) {
1283 u
.width(this.userlist_width
);
1284 w
.css('right', u
.outerWidth(true));
1289 // Activate this tab!
1290 this.div
.addClass('active');
1291 if (this.userlist_width
> 0) {
1292 this.userlist
.addClass('active');
1294 this.tab
.addClass('active');
1296 // Add the part image to the tab
1297 this.addPartImage();
1299 this.clearHighlight();
1300 front
.setTopicText(this.topic
);
1301 front
.cur_channel
= this;
1303 // If we're using fancy scrolling, refresh it
1305 touch_scroll
.refresh();
1308 this.scrollBottom();
1310 $('#kiwi_msginput').focus();
1314 Tabview
.prototype.close = function () {
1316 this.userlist
.remove();
1319 if (front
.cur_channel
=== this) {
1320 front
.tabviews
.server
.show();
1322 delete front
.tabviews
[this.name
.toLowerCase()];
1325 Tabview
.prototype.addPartImage = function () {
1326 this.clearPartImage();
1328 // We can't close this tab, so don't have the close image
1329 if (this.name
=== 'server') {
1333 var del_html
= '<img src="/img/redcross.png" class="tab_part" />';
1334 this.tab
.append(del_html
);
1336 $('.tab_part', this.tab
).click(function () {
1337 if (front
.isChannel($(this).parent().text())) {
1340 // Make sure we don't close the server tab
1341 if (front
.cur_channel
.name
!== 'server') {
1342 front
.cur_channel
.close();
1348 Tabview
.prototype.clearPartImage = function () {
1349 $('#kiwi .toolbars .tab_part').remove();
1352 Tabview
.prototype.addMsg = function (time
, nick
, msg
, type
, style
) {
1353 var self
, tmp
, plugin_ret
, i
, d
, re
, line_msg
, next
;
1357 tmp
= {msg
: msg
, time
: time
, nick
: nick
, tabview
: this.name
};
1358 tmp
= plugs
.run('addmsg', tmp
);
1368 if (time
=== null) {
1370 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1373 // The CSS class (action, topic, notice, etc)
1374 if (typeof type
!== "string") {
1378 // Make sure we don't have NaN or something
1379 if (typeof msg
!== "string") {
1385 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1387 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1388 msg
= msg
.replace(String
.fromCharCode(2), next
);
1389 next
= (next
=== '<b>') ? '</b>' : '<b>';
1391 if (next
=== '</b>') {
1396 // Wierd thing noticed by Dux0r on the irc.esper.net server
1397 if (typeof msg
!== "string") {
1402 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1404 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1405 msg
= msg
.replace(String
.fromCharCode(31), next
);
1406 next
= (next
=== '<u>') ? '</u>' : '<u>';
1408 if (next
=== '</u>') {
1413 // Make the channels clickable
1414 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1415 msg
= msg
.replace(re
, function (match
) {
1416 return '<a class="chan">' + match
+ '</a>';
1419 // Build up and add the line
1420 line_msg
= $('<div class="msg ' + type
+ '"><div class="time">' + time
+ '</div><div class="nick">' + nick
+ '</div><div class="text" style="' + style
+ '">' + msg
+ ' </div></div>');
1421 this.div
.append(line_msg
);
1424 this.scrollBottom();
1426 touch_scroll
.refresh();
1427 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1428 this.scrollBottom();
1429 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1430 // touch_scroll.scrollTo(0, this.div.height());
1435 Tabview
.prototype.scrollBottom = function () {
1436 var w
= $('#windows');
1437 w
[0].scrollTop
= w
[0].scrollHeight
;
1440 Tabview
.prototype.changeNick = function (newNick
, oldNick
) {
1441 this.userlist
.children().each(function () {
1442 var item
= $('a.nick', this);
1443 if (front
.nickStripPrefix(item
.text()) === oldNick
) {
1444 item
.text(front
.nickGetPrefix(item
.text()) + newNick
);
1445 document
.temp_chan
= 1;
1449 if (typeof document
.temp_chan
!== "undefined") {
1450 this.addMsg(null, ' ', '=== ' + oldNick
+ ' is now known as ' + newNick
, 'action');
1451 delete document
.temp_chan
;
1452 this.userlistSort();
1456 Tabview
.prototype.userlistSort = function () {
1457 var ul
= this.userlist
,
1458 listitems
= ul
.children('li').get(),
1460 listitems
.sort(function (a
, b
) {
1461 var compA
= $(a
).text().toUpperCase(),
1462 compB
= $(b
).text().toUpperCase(),
1465 // Sort by prefixes first
1466 for (i
in gateway
.user_prefixes
) {
1467 prefix
= gateway
.user_prefixes
[i
].symbol
;
1469 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1470 // Both have the same prefix, string compare time
1474 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1478 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1483 // No prefixes, compare by string
1484 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1486 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1489 Tabview
.prototype.highlight = function () {
1490 this.tab
.addClass('highlight');
1492 Tabview
.prototype.activity = function () {
1493 this.tab
.addClass('activity');
1495 Tabview
.prototype.clearHighlight = function () {
1496 this.tab
.removeClass('highlight');
1497 this.tab
.removeClass('activity');
1499 Tabview
.prototype.changeTopic = function (new_topic
) {
1500 this.topic
= new_topic
;
1501 this.addMsg(null, ' ', '=== Topic for ' + this.name
+ ' is: ' + new_topic
, 'topic');
1502 if (front
.cur_channel
.name
=== this.name
) {
1503 front
.setTopicText(new_topic
);
1511 var Box = function (classname
) {
1512 this.id
= randomString(10);
1513 var tmp
= $('<div id="' + this.id
+ '" class="box ' + classname
+ '"><div class="boxarea"></div></div>');
1514 $('#kiwi').append(tmp
);
1515 this.box
= $('#' + this.id
);
1516 this.content
= $('#' + this.id
+ ' .boxarea');
1518 this.box
.draggable({ stack
: ".box" });
1519 this.content
.click(function () {});
1520 //this.box.click(function(){ $(this)..css });
1522 Box
.prototype.create = function (name
, classname
) {
1525 Box
.prototype.id
= null;
1526 Box
.prototype.box
= null;
1527 Box
.prototype.content
= null;
1528 Box
.prototype.destroy = function () {
1531 for (name
in front
.boxes
) {
1532 if (front
.boxes
[name
].id
=== this.id
) {
1533 delete front
.boxes
[name
];
1537 Box
.prototype.height = function () {
1538 return this.box
.height();