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
);
134 $('#kiwi .cur_topic').keydown(function (e
) {
135 if (e
.which
=== 13) {
139 $('#kiwi_msginput').focus();
140 } else if (e
.which
=== 27) {
143 $(this).text(front
.original_topic
);
144 $('#kiwi_msginput').focus();
147 /*$('.cur_topic').live('keypress', function(e) {
148 if (e.keyCode === 13) {
152 $('#kiwi_msginput').focus();
153 } else if (e.keyCode === 27) {
156 $(this).text(front.original_topic);
159 $('.cur_topic').live('change', function () {
161 text
= $(this).text();
162 if (text
!== front
.original_topic
) {
163 chan
= front
.cur_channel
.name
;
164 gateway
.setTopic(chan
, text
);
169 $('#windows a.chan').live('click', function () {
170 front
.joinChannel($(this).text());
177 plugs
.loadPlugin(plugins
[i
]);
182 doLayoutSize: function () {
183 var kiwi
, toolbars
, ul
, n_top
, n_bottom
;
186 if (kiwi
.width() < 330 && !kiwi
.hasClass('small_kiwi')) {
187 console
.log("switching to small kiwi");
188 kiwi
.removeClass('large_kiwi');
189 kiwi
.addClass('small_kiwi');
190 } else if (kiwi
.width() >= 330 && !kiwi
.hasClass('large_kiwi')) {
191 kiwi
.removeClass('small_kiwi');
192 kiwi
.addClass('large_kiwi');
195 toolbars
= $('#kiwi .cur_topic');
196 ul
= $('#kiwi .userlist');
198 n_top
= parseInt(toolbars
.offset().top
, 10) + parseInt(toolbars
.outerHeight(true), 10);
199 n_bottom
= $(document
).height() - parseInt($('#kiwi .control').offset().top
, 10);
201 $('#kiwi .windows').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
202 $('#kiwi .userlist').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
206 doLayout: function () {
207 $('#kiwi .msginput .nick a').text(gateway
.nick
);
208 $('#kiwi_msginput').val(' ');
209 $('#kiwi_msginput').focus();
213 joinChannel: function (chan_name
) {
214 var chans
= chan_name
.split(','),
219 if (front
.tabviews
[chan
.toLowerCase()] === undefined || (front
.tabviews
[chan
.toLowerCase()] !== undefined && front
.tabviews
[chan
.toLowerCase()].safe_to_close
=== true)) {
221 front
.tabviewAdd(chan
);
223 front
.tabviews
[chan
.toLowerCase()].show();
229 run: function (msg
) {
230 var parts
, dest
, t
, pos
, textRange
, d
, plugin_event
, msg_sliced
;
232 // Run through any plugins
233 plugin_event
= {command
: msg
};
234 plugin_event
= plugs
.run('command_run', plugin_event
);
235 if (!plugin_event
|| typeof plugin_event
.command
=== 'undefined') {
239 // Update msg if it's been changed by any plugins
240 msg
= plugin_event
.command
.toString();
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]);
308 if (typeof parts
[1] !== "undefined") {
309 msg_sliced
= msg
.split(' ').slice(2).join(' ');
310 gateway
.msg(parts
[1], msg_sliced
);
312 if (!front
.tabviewExists(parts
[1])) {
313 front
.tabviewAdd(parts
[1]);
315 front
.tabviews
[parts
[1].toLowerCase()].addMsg(null, gateway
.nick
, msg_sliced
);
321 gateway
.raw(msg
.replace(/^\/quote /i, ''));
325 gateway
.action(front
.cur_channel
.name
, msg
.substring(4));
326 //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
327 front
.cur_channel
.addMsg(null, ' ', '* ' + gateway
.nick
+ ' ' + msg
.substring(4), 'action', 'color:#555;');
332 msg
= parts
.slice(2).join(' ');
334 gateway
.notice(dest
, msg
);
335 this.onNotice({}, {nick
: gateway
.nick
, channel
: dest
, msg
: msg
});
339 if (parts
[1] !== undefined) {
340 front
.windowsShow(parseInt(parts
[1], 10));
345 gateway
.quit(msg
.split(" ", 2)[1]);
349 if (parts
[1] === undefined) {
351 if (t
.createTextRange
) {
352 pos
= t
.text().length();
353 textRange
= t
.createTextRange();
354 textRange
.collapse(true);
355 textRange
.moveEnd(pos
);
356 textRange
.moveStart(pos
);
358 } else if (t
.setSelectionRange
) {
359 t
.setSelectionRange(pos
, pos
);
362 gateway
.setTopic(front
.cur_channel
.name
, msg
.split(' ', 2)[1]);
363 //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
368 gateway
.kiwi(front
.cur_channel
.name
, msg
.substring(6));
372 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
373 gateway
.raw(msg
.substring(1));
377 //alert('Sending message: '+msg);
378 if (msg
.trim() === '') {
381 if (front
.cur_channel
.name
!== 'server') {
382 gateway
.msg(front
.cur_channel
.name
, msg
);
384 d
= d
.getHours() + ":" + d
.getMinutes();
385 //front.addMsg(d, gateway.nick, msg);
386 front
.cur_channel
.addMsg(null, gateway
.nick
, msg
);
392 onMsg: function (e
, data
) {
393 var destination
, plugin_event
;
394 // Is this message from a user?
395 if (data
.channel
=== gateway
.nick
) {
396 destination
= data
.nick
.toLowerCase();
398 destination
= data
.channel
.toLowerCase();
401 plugin_event
= {nick
: data
.nick
, msg
: data
.msg
, destination
: destination
};
402 plugin_event
= plugs
.run('msg_recieved', plugin_event
);
407 if (!front
.tabviewExists(plugin_event
.destination
)) {
408 front
.tabviewAdd(plugin_event
.destination
);
410 front
.tabviews
[plugin_event
.destination
].addMsg(null, plugin_event
.nick
, plugin_event
.msg
);
413 onDebug: function (e
, data
) {
414 if (!front
.tabviewExists('kiwi_debug')) {
415 front
.tabviewAdd('kiwi_debug');
417 front
.tabviews
.kiwi_debug
.addMsg(null, ' ', data
.msg
);
420 onAction: function (e
, data
) {
422 // Is this message from a user?
423 if (data
.channel
=== gateway
.nick
) {
424 destination
= data
.nick
;
426 destination
= data
.channel
;
429 if (!front
.tabviewExists(destination
)) {
430 front
.tabviewAdd(destination
);
432 front
.tabviews
[destination
.toLowerCase()].addMsg(null, ' ', '* ' + data
.nick
+ ' ' + data
.msg
, 'action', 'color:#555;');
435 onTopic: function (e
, data
) {
436 if (front
.tabviewExists(data
.channel
)) {
437 front
.tabviews
[data
.channel
.toLowerCase()].changeTopic(data
.topic
);
441 onNotice: function (e
, data
) {
442 var nick
= (data
.nick
=== undefined) ? '' : data
.nick
,
443 enick
= '[' + nick
+ ']';
445 if (front
.tabviewExists(data
.target
)) {
446 front
.tabviews
[data
.target
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
447 } else if (front
.tabviewExists(nick
)) {
448 front
.tabviews
[nick
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
450 front
.tabviews
.server
.addMsg(null, enick
, data
.msg
, 'notice');
454 onCTCPRequest: function (e
, data
) {
455 var msg
= data
.msg
.split(" ", 2);
458 if (typeof msg
[1] === 'undefined') {
461 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'PING ' + msg
[1] + String
.fromCharCode(1));
464 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String
.fromCharCode(1));
467 front
.tabviews
.server
.addMsg(null, 'CTCP [' + data
.nick
+ ']', data
.msg
, 'ctcp');
470 onCTCPResponse: function (e
, data
) {
473 onKiwi: function (e
, data
) {
477 onConnect: function (e
, data
) {
478 if (data
.connected
) {
479 if (gateway
.nick
!== data
.nick
) {
480 gateway
.nick
= data
.nick
;
484 front
.tabviews
.server
.addMsg(null, ' ', '=== Connected OK :)', 'status');
485 if (typeof init_data
.channel
=== "string") {
486 front
.joinChannel(init_data
.channel
);
488 plugs
.run('connect', {success
: true});
490 front
.tabviews
.server
.addMsg(null, ' ', '=== Failed to connect :(', 'status');
491 plugs
.run('connect', {success
: false});
494 onConnectFail: function (e
, data
) {
495 var reason
= (typeof data
.reason
=== 'string') ? data
.reason
: '';
496 front
.tabviews
.server
.addMsg(null, '', 'There\'s a problem connecting! (' + reason
+ ')', 'error');
497 plugs
.run('connect', {success
: false});
499 onDisconnect: function (e
, data
) {
501 for (tab
in front
.tabviews
) {
502 front
.tabviews
[tab
].addMsg(null, '', 'Disconnected from server!', 'error');
504 plugs
.run('disconnect', {success
: false});
506 onOptions: function (e
, data
) {
507 if (typeof gateway
.network_name
=== "string" && gateway
.network_name
!== "") {
508 front
.tabviews
.server
.tab
.text(gateway
.network_name
);
511 onMOTD: function (e
, data
) {
512 front
.tabviews
.server
.addMsg(null, data
.server
, data
.msg
, 'motd');
514 onWhois: function (e
, data
) {
517 front
.cur_channel
.addMsg(null, data
.nick
, data
.msg
, 'whois');
518 } else if (data
.logon
) {
520 d
.setTime(data
.logon
* 1000);
521 d
= d
.toLocaleString();
522 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' second' + ((data
.idle
!== 1) ? 's' : '') + ', signed on ' + d
, 'whois');
524 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' seconds', 'whois');
527 onMode: function (e
, data
) {
530 onUserList: function (e
, data
) {
532 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
535 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
537 if (!document
.userlist_updating
) {
538 document
.userlist_updating
= true;
542 $.each(data
.users
, function (i
, item
) {
543 nick
= i
; //i.match(/^.+!/g);
545 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
548 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
550 onUserListEnd: function (e
, data
) {
551 document
.userlist_updating
= false;
554 onJoin: function (e
, data
) {
555 if (!front
.tabviewExists(data
.channel
)) {
556 front
.tabviewAdd(data
.channel
.toLowerCase());
559 if (data
.nick
=== gateway
.nick
) {
560 return; // Not needed as it's already in nicklist
562 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '--> ' + data
.nick
+ ' has joined', 'action', 'color:#009900;');
563 $('<li><a class="nick" onclick="front.userClick(this);">' + data
.nick
+ '</a></li>').appendTo(front
.tabviews
[data
.channel
.toLowerCase()].userlist
);
564 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
566 onPart: function (e
, data
) {
567 if (front
.tabviewExists(data
.channel
)) {
568 // If this is us, close the tabview
569 if (data
.nick
=== gateway
.nick
) {
570 front
.tabviews
[data
.channel
.toLowerCase()].close();
571 front
.tabviews
.server
.show();
575 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.nick
+ ' has left (' + data
.message
+ ')', 'action', 'color:#990000;');
576 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
577 if ($(this).text() === data
.nick
) {
583 onKick: function (e
, data
) {
584 if (front
.tabviewExists(data
.channel
)) {
585 // If this is us, close the tabview
586 if (data
.kicked
=== gateway
.nick
) {
587 //front.tabviews[data.channel.toLowerCase()].close();
588 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data
.channel
+ '. ' + data
.message
, 'status');
589 front
.tabviews
[data
.channel
.toLowerCase()].safe_to_close
= true;
590 $('li', front
.tabviews
[data
.channel
.toLowerCase()].userlist
).remove();
594 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.kicked
+ ' kicked by ' + data
.nick
+ '(' + data
.message
+ ')', 'action', 'color:#990000;');
595 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
596 if ($(this).text() === data
.nick
) {
602 onNick: function (e
, data
) {
603 if (data
.nick
=== gateway
.nick
) {
604 gateway
.nick
= data
.newnick
;
608 $.each(front
.tabviews
, function (i
, item
) {
609 $.each(front
.tabviews
, function (i
, item
) {
610 item
.changeNick(data
.newnick
, data
.nick
);
614 onQuit: function (e
, data
) {
615 $.each(front
.tabviews
, function (i
, item
) {
616 $.each(front
.tabviews
, function (i
, item
) {
617 item
.userlist
.children().each(function () {
618 if ($(this).text() === data
.nick
) {
620 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
626 onChannelRedirect: function (e
, data
) {
627 front
.tabviews
[data
.from.toLowerCase()].close();
628 front
.tabviewAdd(data
.to
.toLowerCase());
629 front
.tabviews
[data
.to
.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data
.from, 'action');
632 onIRCError: function (e
, data
) {
634 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
635 t_view
= data
.channel
;
640 switch (data
.error
) {
641 case 'banned_from_channel':
642 front
.tabviews
[t_view
].addMsg(null, ' ', '=== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
643 if (t_view
!== 'server') {
644 front
.tabviews
[t_view
].safe_to_close
= true;
647 case 'bad_channel_key':
648 front
.tabviews
[t_view
].addMsg(null, ' ', '=== Bad channel key for ' + data
.channel
, 'status');
649 if (t_view
!== 'server') {
650 front
.tabviews
[t_view
].safe_to_close
= true;
653 case 'invite_only_channel':
654 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is invite only.', 'status');
655 if (t_view
!== 'server') {
656 front
.tabviews
[t_view
].safe_to_close
= true;
659 case 'channel_is_full':
660 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is full.', 'status');
661 if (t_view
!== 'server') {
662 front
.tabviews
[t_view
].safe_to_close
= true;
665 case 'chanop_privs_needed':
666 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
669 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
671 case 'nickname_in_use':
672 front
.tabviews
.server
.addMsg(null, ' ', '=== The nickname ' + data
.nick
+ ' is already in use. Please select a new nickname', 'status');
673 front
.showChangeNick();
676 // We don't know what data contains, so don't do anything with it.
677 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
681 registerKeys: function () {
682 $('#kiwi_msginput').bind('keydown', function (e
) {
683 var windows
, meta
, num
, msg
, data
, candidates
, word_pos
, word
, i
;
684 windows
= $('#windows');
685 //var meta = e.altKey;
689 case (e
.which
>= 48) && (e
.which
<= 57):
696 front
.windowsShow(num
);
700 case e
.which
=== 27: // escape
702 case e
.which
=== 13: // return
703 msg
= $('#kiwi_msginput').val();
706 front
.buffer
.push(msg
);
707 front
.buffer_pos
= front
.buffer
.length
;
710 $('#kiwi_msginput').val('');
713 case e
.which
=== 33: // page up
714 console
.log("page up");
715 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
717 case e
.which
=== 34: // page down
718 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
720 case e
.which
=== 37: // left
722 front
.windowsPrevious();
726 case e
.which
=== 38: // up
727 if (front
.buffer_pos
> 0) {
729 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
732 case e
.which
=== 39: // right
738 case e
.which
=== 40: // down
739 if (front
.buffer_pos
< front
.buffer
.length
) {
741 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
745 case e
.which
=== 9: // tab
746 // Get possible autocompletions
748 front
.cur_channel
.userlist
.children().each(function () {
750 nick
= front
.nickStripPrefix($('a.nick', this).text());
754 // Do the autocomplete
755 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
758 word_pos
= this.value
.lastIndexOf(' ');
760 if (word_pos
=== -1) {
763 word
= this.value
.substr(word_pos
);
767 // filter data to find only strings that start with existing value
768 for (i
= 0; i
< data
.length
; i
++) {
769 if (data
[i
].indexOf(word
) === 0 && data
[i
].length
> word
.length
) {
770 candidates
.push(data
[i
]);
774 if (candidates
.length
> 0) {
775 // some candidates for autocompletion are found
776 this.value
= this.value
.substring(0, word_pos
) + ' ' + candidates
[0] + ': ';
777 this.selectionStart
= this.value
.length
;
785 $('#kiwi .control .msginput .nick').click(function () {
786 front
.showChangeNick();
793 $('#kiwi .plugins .load_plugin_file').click(function () {
794 if (typeof front
.boxes
.plugins
!== "undefined") {
798 front
.boxes
.plugins
= new Box("plugin_file");
799 $('#tmpl_plugins').tmpl({}).appendTo(front
.boxes
.plugins
.content
);
800 front
.boxes
.plugins
.box
.css('top', -(front
.boxes
.plugins
.height
+ 40));
802 // Populate the plugin list..
803 function enumPlugins() {
805 lst
= $('#plugin_list');
806 lst
.find('option').remove();
807 for (j
in plugs
.loaded
) {
808 txt
= plugs
.loaded
[j
].name
;
809 lst
.append('<option value="' + txt
+ '">' + txt
+ '</option>');
815 $('#kiwi .plugin_file').submit(function () {
816 $('<div></div>').load($('.txtpluginfile').val(), function (e
) {
821 $('#kiwi .cancelpluginfile').click(function () {
822 front
.boxes
.plugins
.destroy();
825 $('#kiwi #plugins_list_unload').click(function () {
827 selected_plugin
= $('#plugin_list').val();
828 plugs
.unloadPlugin(selected_plugin
);
832 $('#kiwi .txtpluginfile').focus();
836 $('#kiwi .plugins .reload_css').click(function () {
837 var links
= document
.getElementsByTagName("link"),
839 for (i
= 0; i
< links
.length
; i
++) {
840 if (links
[i
].rel
=== "stylesheet") {
841 if (links
[i
].href
.indexOf("?") === -1) {
842 links
[i
].href
+= "?";
844 links
[i
].href
+= "x";
850 $('#kiwi .about .about_close').click(function () {
851 $('#kiwi .about').css('display', 'none');
855 $('#kiwi .poweredby').click(function () {
856 $('#kiwi .about').css('display', 'block');
862 showChangeNick: function () {
863 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
865 $('#kiwi .form_newnick').submit(function () {
866 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
867 $('#kiwi .newnick').remove();
871 $('#kiwi .txtnewnick').keypress(function (ev
) {
872 if (!this.first_press
) {
873 this.first_press
= true;
878 $('#kiwi .txtnewnick').keydown(function (ev
) {
879 if (ev
.which
=== 27) { // ESC
880 $('#kiwi_msginput').focus();
881 $('#kiwi .newnick').remove();
885 $('#kiwi .cancelnewnick').click(function () {
886 $('#kiwi .newnick').remove();
889 $('#kiwi .txtnewnick').focus();
893 tabviewExists: function (name
) {
894 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
897 tabviewAdd: function (v_name
) {
899 var re
, htmlsafe_name
, tmp_divname
, tmp_userlistname
, tmp_tabname
, userlist_enabled
= true;
901 if (v_name
.charAt(0) === gateway
.channel_prefix
) {
902 re
= new RegExp(gateway
.channel_prefix
, "g");
903 htmlsafe_name
= v_name
.replace(re
, 'pre');
904 htmlsafe_name
= "chan_" + htmlsafe_name
;
906 htmlsafe_name
= 'query_' + v_name
;
907 userlist_enabled
= false;
910 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
911 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
912 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
914 if (!front
.tabviewExists(v_name
)) {
915 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
916 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname
+ '"></ul>');
917 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + v_name
.toLowerCase() + '\'].show();">' + v_name
+ '</li>');
919 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
920 //front.windowShow(v_name);
922 front
.tabviews
[v_name
.toLowerCase()] = new Tabview();
923 front
.tabviews
[v_name
.toLowerCase()].name
= v_name
;
924 front
.tabviews
[v_name
.toLowerCase()].div
= $('#' + tmp_divname
);
925 front
.tabviews
[v_name
.toLowerCase()].userlist
= $('#' + tmp_userlistname
);
926 front
.tabviews
[v_name
.toLowerCase()].tab
= $('#' + tmp_tabname
);
927 if (!userlist_enabled
) {
928 front
.tabviews
[v_name
.toLowerCase()].userlist_width
= 0;
930 front
.tabviews
[v_name
.toLowerCase()].show();
932 if (typeof registerTouches
=== "function") {
933 //alert("Registering touch interface");
934 //registerTouches($('#'+tmp_divname));
935 registerTouches(document
.getElementById(tmp_divname
));
938 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
939 alert($(this).attr('id'));
943 front
.doLayoutSize();
947 userClick: function (item
) {
948 var li
= $(item
).parent();
950 // Remove any existing userboxes
951 $('#kiwi .userbox').remove();
953 if ($(li
).data('userbox') === item
) {
954 $(li
).removeData('userbox');
956 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
958 $('#kiwi .userbox .userbox_query').click(function (ev
) {
959 var nick
= $('#kiwi .userbox_nick').val();
960 front
.run('/query ' + nick
);
963 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
964 var nick
= $('#kiwi .userbox_nick').val();
965 front
.run('/whois ' + nick
);
967 $(li
).data('userbox', item
);
976 onSync: function (e
, data
) {
978 if (data
.nick
!== undefined) {
979 gateway
.nick
= data
.nick
;
983 if (data
.tabviews
!== undefined) {
984 $.each(data
.tabviews
, function (i
, tab
) {
985 if (!front
.tabviewExists(tab
.name
)) {
986 front
.tabviewAdd(gateway
.channel_prefix
+ tab
.name
);
988 if (tab
.userlist
!== undefined) {
989 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
999 setTopicText: function (new_topic
) {
1000 front
.original_topic
= new_topic
;
1001 $('#kiwi .cur_topic .topic').text(new_topic
);
1002 front
.doLayoutSize();
1011 nickStripPrefix: function (nick
) {
1012 var tmp
= nick
, i
, prefix
;
1014 prefix
= tmp
.charAt(0);
1015 for (i
in gateway
.user_prefixes
) {
1016 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
1017 return tmp
.substring(1);
1024 nickGetPrefix: function (nick
) {
1025 var tmp
= nick
, i
, prefix
;
1027 prefix
= tmp
.charAt(0);
1028 for (i
in gateway
.user_prefixes
) {
1029 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
1037 isChannel: function (name
) {
1038 var prefix
, is_chan
;
1039 prefix
= name
.charAt(0);
1040 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
1049 tabviewsNext: function () {
1050 var wl
= $('#kiwi .windowlist ul'),
1051 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) + 170;
1052 wl
.css('text-indent', next_left
);
1055 tabviewsPrevious: function () {
1056 var wl
= $('#kiwi .windowlist ul'),
1057 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) - 170;
1058 wl
.css('text-indent', next_left
);
1061 windowsNext: function () {
1064 for (tab
in front
.tabviews
) {
1066 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1071 front
.tabviews
[tab
].show();
1077 windowsPrevious: function () {
1078 var tab
, prev_tab
, next
;
1080 for (tab
in front
.tabviews
) {
1081 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1087 prev_tab
= front
.tabviews
[tab
];
1091 windowsShow: function (num
) {
1092 num
= parseInt(num
, 10);
1093 console
.log('Showing window ' + num
.toString());
1095 for (tab
in front
.tabviews
) {
1097 front
.tabviews
[tab
].show();
1106 barsShow: function () {
1107 $('#kiwi .toolbars').slideDown();
1108 $('#kiwi .control').slideDown();
1111 barsHide: function () {
1112 $('#kiwi .toolbars').slideUp();
1113 $('#kiwi .control').slideUp();
1136 var Utilityview = function (name
) {
1137 var rand_name
= randomString(15),
1138 tmp_divname
= 'kiwi_window_' + rand_name
,
1139 tmp_userlistname
= 'kiwi_userlist_' + rand_name
,
1140 tmp_tabname
= 'kiwi_tab_' + rand_name
;
1142 this.name
= rand_name
;
1146 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
1148 this.tab
= $('<li id="' + tmp_tabname
+ '">' + this.title
+ '</li>');
1149 this.tab
.click(function () {
1150 front
.utilityviews
[rand_name
.toLowerCase()].show();
1152 $('#kiwi .utilityviewlist ul').append(this.tab
);
1154 this.div
= $('#' + tmp_divname
);
1155 this.div
.css('overflow', 'hidden');
1157 front
.utilityviews
[rand_name
.toLowerCase()] = this;
1160 Utilityview
.prototype.name
= null;
1161 Utilityview
.prototype.title
= null;
1162 Utilityview
.prototype.div
= null;
1163 Utilityview
.prototype.tab
= null;
1164 Utilityview
.prototype.topic
= ' ';
1165 Utilityview
.prototype.show = function () {
1166 $('#kiwi .messages').removeClass("active");
1167 $('#kiwi .userlist ul').removeClass("active");
1168 $('#kiwi .toolbars ul li').removeClass("active");
1170 $('#windows').css('overflow-y', 'hidden');
1171 $('#windows').css('right', 0);
1172 // Activate this tab!
1173 this.div
.addClass('active');
1174 this.tab
.addClass('active');
1176 this.addPartImage();
1178 front
.setTopicText(this.topic
);
1179 front
.cur_channel
= this;
1181 // If we're using fancy scrolling, refresh it
1183 touch_scroll
.refresh();
1187 Utilityview
.prototype.close = function () {
1191 if (front
.cur_channel
=== this) {
1192 front
.tabviews
.server
.show();
1194 delete front
.utilityviews
[this.name
.toLowerCase()];
1197 Utilityview
.prototype.addPartImage = function () {
1198 this.clearPartImage();
1200 // We can't close this tab, so don't have the close image
1201 if (this.name
=== 'server') {
1205 var del_html
= '<img src="/img/redcross.png" class="tab_part" />';
1206 this.tab
.append(del_html
);
1208 $('.tab_part', this.tab
).click(function () {
1209 if (front
.cur_channel
.name
!== 'server') {
1210 front
.cur_channel
.close();
1215 Utilityview
.prototype.clearPartImage = function () {
1216 $('#kiwi .toolbars .tab_part').remove();
1230 var Tabview = function () {};
1231 Tabview
.prototype.name
= null;
1232 Tabview
.prototype.div
= null;
1233 Tabview
.prototype.userlist
= null;
1234 Tabview
.prototype.userlist_width
= 100; // 0 for disabled
1235 Tabview
.prototype.tab
= null;
1236 Tabview
.prototype.topic
= "";
1237 Tabview
.prototype.safe_to_close
= false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1239 Tabview
.prototype.show = function () {
1242 $('#kiwi .messages').removeClass("active");
1243 $('#kiwi .userlist ul').removeClass("active");
1244 $('#kiwi .toolbars ul li').removeClass("active");
1247 u
= $('#kiwi .userlist');
1249 w
.css('overflow-y', 'scroll');
1251 // Set the window size accordingly
1252 if (this.userlist_width
> 0) {
1253 u
.width(this.userlist_width
);
1254 w
.css('right', u
.outerWidth(true));
1259 // Activate this tab!
1260 this.div
.addClass('active');
1261 if (this.userlist_width
> 0) {
1262 this.userlist
.addClass('active');
1264 this.tab
.addClass('active');
1266 // Add the part image to the tab
1267 this.addPartImage();
1269 this.clearHighlight();
1270 front
.setTopicText(this.topic
);
1271 front
.cur_channel
= this;
1273 // If we're using fancy scrolling, refresh it
1275 touch_scroll
.refresh();
1278 this.scrollBottom();
1280 $('#kiwi_msginput').focus();
1284 Tabview
.prototype.close = function () {
1286 this.userlist
.remove();
1289 if (front
.cur_channel
=== this) {
1290 front
.tabviews
.server
.show();
1292 delete front
.tabviews
[this.name
.toLowerCase()];
1295 Tabview
.prototype.addPartImage = function () {
1296 this.clearPartImage();
1298 // We can't close this tab, so don't have the close image
1299 if (this.name
=== 'server') {
1303 var del_html
= '<img src="/img/redcross.png" class="tab_part" />';
1304 this.tab
.append(del_html
);
1306 $('.tab_part', this.tab
).click(function () {
1307 if (front
.isChannel($(this).parent().text())) {
1310 // Make sure we don't close the server tab
1311 if (front
.cur_channel
.name
!== 'server') {
1312 front
.cur_channel
.close();
1318 Tabview
.prototype.clearPartImage = function () {
1319 $('#kiwi .toolbars .tab_part').remove();
1322 Tabview
.prototype.addMsg = function (time
, nick
, msg
, type
, style
) {
1323 var self
, tmp
, plugin_ret
, i
, d
, re
, line_msg
, next
;
1327 tmp
= {msg
: msg
, time
: time
, nick
: nick
, tabview
: this.name
};
1328 tmp
= plugs
.run('addmsg', tmp
);
1338 if (time
=== null) {
1340 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1343 // The CSS class (action, topic, notice, etc)
1344 if (typeof type
!== "string") {
1348 // Make sure we don't have NaN or something
1349 if (typeof msg
!== "string") {
1355 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1357 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1358 msg
= msg
.replace(String
.fromCharCode(2), next
);
1359 next
= (next
=== '<b>') ? '</b>' : '<b>';
1361 if (next
=== '</b>') {
1366 // Wierd thing noticed by Dux0r on the irc.esper.net server
1367 if (typeof msg
!== "string") {
1372 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1374 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1375 msg
= msg
.replace(String
.fromCharCode(31), next
);
1376 next
= (next
=== '<u>') ? '</u>' : '<u>';
1378 if (next
=== '</u>') {
1383 // Make the channels clickable
1384 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1385 msg
= msg
.replace(re
, function (match
) {
1386 return '<a class="chan">' + match
+ '</a>';
1389 // Build up and add the line
1390 line_msg
= $('<div class="msg ' + type
+ '"><div class="time">' + time
+ '</div><div class="nick">' + nick
+ '</div><div class="text" style="' + style
+ '">' + msg
+ ' </div></div>');
1391 this.div
.append(line_msg
);
1394 this.scrollBottom();
1396 touch_scroll
.refresh();
1397 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1398 this.scrollBottom();
1399 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1400 // touch_scroll.scrollTo(0, this.div.height());
1405 Tabview
.prototype.scrollBottom = function () {
1406 var w
= $('#windows');
1407 w
[0].scrollTop
= w
[0].scrollHeight
;
1410 Tabview
.prototype.changeNick = function (newNick
, oldNick
) {
1411 this.userlist
.children().each(function () {
1412 var item
= $('a.nick', this);
1413 if (front
.nickStripPrefix(item
.text()) === oldNick
) {
1414 item
.text(front
.nickGetPrefix(item
.text()) + newNick
);
1415 document
.temp_chan
= 1;
1419 if (typeof document
.temp_chan
!== "undefined") {
1420 this.addMsg(null, ' ', '=== ' + oldNick
+ ' is now known as ' + newNick
, 'action');
1421 delete document
.temp_chan
;
1422 this.userlistSort();
1426 Tabview
.prototype.userlistSort = function () {
1427 var ul
= this.userlist
,
1428 listitems
= ul
.children('li').get(),
1430 listitems
.sort(function (a
, b
) {
1431 var compA
= $(a
).text().toUpperCase(),
1432 compB
= $(b
).text().toUpperCase(),
1435 // Sort by prefixes first
1436 for (i
in gateway
.user_prefixes
) {
1437 prefix
= gateway
.user_prefixes
[i
].symbol
;
1439 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1440 // Both have the same prefix, string compare time
1444 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1448 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1453 // No prefixes, compare by string
1454 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1456 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1459 Tabview
.prototype.highlight = function () {
1460 this.tab
.addClass('highlight');
1462 Tabview
.prototype.activity = function () {
1463 this.tab
.addClass('activity');
1465 Tabview
.prototype.clearHighlight = function () {
1466 this.tab
.removeClass('highlight');
1467 this.tab
.removeClass('activity');
1469 Tabview
.prototype.changeTopic = function (new_topic
) {
1470 this.topic
= new_topic
;
1471 this.addMsg(null, ' ', '=== Topic for ' + this.name
+ ' is: ' + new_topic
, 'topic');
1472 if (front
.cur_channel
.name
=== this.name
) {
1473 front
.setTopicText(new_topic
);
1481 var Box = function (classname
) {
1482 this.id
= randomString(10);
1483 var tmp
= $('<div id="' + this.id
+ '" class="box ' + classname
+ '"><div class="boxarea"></div></div>');
1484 $('#kiwi').append(tmp
);
1485 this.box
= $('#' + this.id
);
1486 this.content
= $('#' + this.id
+ ' .boxarea');
1488 this.box
.draggable({ stack
: ".box" });
1489 this.content
.click(function () {});
1490 //this.box.click(function(){ $(this)..css });
1492 Box
.prototype.create = function (name
, classname
) {
1495 Box
.prototype.id
= null;
1496 Box
.prototype.box
= null;
1497 Box
.prototype.content
= null;
1498 Box
.prototype.destroy = function () {
1501 for (name
in front
.boxes
) {
1502 if (front
.boxes
[name
].id
=== this.id
) {
1503 delete front
.boxes
[name
];
1507 Box
.prototype.height = function () {
1508 return this.box
.height();