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];
88 init_data
.channel
= $('#channel').val();
92 front
.run('/connect ' + netsel
.val());
97 $('#kiwi .connectwindow').slideUp();
98 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
103 supportsOrientationChange
= (typeof window
.onorientationchange
!== undefined);
104 orientationEvent
= supportsOrientationChange
? "orientationchange" : "resize";
105 if (window
.addEventListener
) {
106 window
.addEventListener(orientationEvent
, front
.doLayoutSize
, false);
109 window
.attachEvent(orientationEvent
, front
.doLayoutSize
, false);
111 //$('#kiwi').bind("resize", front.doLayoutSize, false);
115 front
.tabviewAdd('server');
116 front
.tabviews
.server
.userlist_width
= 0; // Disable the userlist
118 // Any pre-defined nick?
119 if (typeof window
.init_data
.nick
=== "string") {
120 $('#kiwi .formconnectwindow .nick').val(init_data
.nick
);
123 // Any pre-defined channels?
124 if (typeof window
.init_data
.channel
=== 'string') {
125 $('#channel').val(init_data
.channel
);
129 $('#kiwi .cur_topic').keydown(function (e
) {
130 if (e
.which
=== 13) {
134 $('#kiwi_msginput').focus();
135 } else if (e
.which
=== 27) {
138 $(this).text(front
.original_topic
);
139 $('#kiwi_msginput').focus();
142 /*$('.cur_topic').live('keypress', function(e) {
143 if (e.keyCode === 13) {
147 $('#kiwi_msginput').focus();
148 } else if (e.keyCode === 27) {
151 $(this).text(front.original_topic);
154 $('.cur_topic').live('change', function () {
156 text
= $(this).text();
157 if (text
!== front
.original_topic
) {
158 chan
= front
.cur_channel
.name
;
159 gateway
.setTopic(chan
, text
);
164 $('#windows a.chan').live('click', function() {
165 front
.joinChannel($(this).text());
169 $('#windows a.link_ext').live('mouseover', function () {
171 var tt
= $('.tt', a
);
173 if (tt
.text() === '') {
174 var tooltip
= $('<a class="link_ext_browser">Open in Kiwi..</a>');
178 tt
.css('top', -tt
.outerHeight()+'px');
179 tt
.css('left', (a
.outerWidth() / 2) - (tt
.outerWidth() / 2));
181 $('#windows a.link_ext').live('mouseout', function () {
183 var tt
= $('.tt', a
);
185 $('#windows a.link_ext').live('click', function (e
) {
188 switch (e
.target
.className
) {
193 case 'link_ext_browser':
194 var t
= new Utilityview('Browser', a
.attr('href'));
203 doLayoutSize: function () {
204 var kiwi
, toolbars
, ul
, n_top
, n_bottom
;
207 if (kiwi
.width() < 330 && !kiwi
.hasClass('small_kiwi')) {
208 console
.log("switching to small kiwi");
209 kiwi
.removeClass('large_kiwi');
210 kiwi
.addClass('small_kiwi');
211 } else if (kiwi
.width() >= 330 && !kiwi
.hasClass('large_kiwi')) {
212 kiwi
.removeClass('small_kiwi');
213 kiwi
.addClass('large_kiwi');
216 toolbars
= $('#kiwi .cur_topic');
217 ul
= $('#kiwi .userlist');
219 n_top
= parseInt(toolbars
.offset().top
, 10) + parseInt(toolbars
.outerHeight(true), 10);
220 n_bottom
= $(document
).height() - parseInt($('#kiwi .control').offset().top
, 10);
222 $('#kiwi .windows').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
223 $('#kiwi .userlist').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
227 doLayout: function () {
228 $('#kiwi .msginput .nick a').text(gateway
.nick
);
229 $('#kiwi_msginput').val(' ');
230 $('#kiwi_msginput').focus();
234 joinChannel: function (chan_name
) {
235 var chans
= chan_name
.split(','),
239 if (front
.tabviews
[chan
.toLowerCase()] === undefined || (front
.tabviews
[chan
.toLowerCase()] !== undefined && front
.tabviews
[chan
.toLowerCase()].safe_to_close
=== true)) {
241 front
.tabviewAdd(chan
);
243 front
.tabviews
[chan
.toLowerCase()].show();
249 run: function (msg
) {
250 var parts
, dest
, t
, pos
, textRange
, d
, plugin_event
;
252 // Run through any plugins
253 plugin_event
= {command
: msg
};
254 plugin_event
= plugs
.run('command_run', plugin_event
);
255 if (!plugin_event
|| typeof plugin_event
.command
=== 'undefined') return;
257 // Update msg if it's been changed by any plugins
258 msg
= plugin_event
.command
.toString();
260 console
.log("running " + msg
);
261 if (msg
.substring(0, 1) === '/') {
262 parts
= msg
.split(' ');
263 switch (parts
[0].toLowerCase()) {
266 front
.joinChannel(parts
[1]);
271 if (parts
[1] === undefined) {
272 alert('Usage: /connect servername [port]');
276 if (parts
[2] === undefined) {
279 front
.cur_channel
.addMsg(null, ' ', '=== Connecting to ' + parts
[1] + '...', 'status');
280 gateway
.connect(parts
[1], parts
[2], 0);
284 console
.log("/nick");
285 if (parts
[1] === undefined) {
286 console
.log("calling show nick");
287 front
.showChangeNick();
289 console
.log("sending raw");
290 gateway
.raw(msg
.substring(1));
295 if (typeof parts
[1] === "undefined") {
296 if (front
.cur_channel
.safe_to_close
) {
297 front
.cur_channel
.close();
299 gateway
.raw(msg
.substring(1) + ' ' + front
.cur_channel
.name
);
302 gateway
.raw(msg
.substring(1));
307 if (typeof parts
[1] !== "undefined") {
308 gateway
.raw(msg
.substring(1));
318 if (typeof parts
[1] !== "undefined") {
319 front
.tabviewAdd(parts
[1]);
326 if (typeof parts
[1] !== "undefined") {
327 var msg_sliced
= msg
.split(' ').slice(2).join(' ');
328 gateway
.msg(parts
[1], msg_sliced
);
330 if (!front
.tabviewExists(parts
[1])) {
331 front
.tabviewAdd(parts
[1]);
333 front
.tabviews
[parts
[1].toLowerCase()].addMsg(null, gateway
.nick
, msg_sliced
);
339 gateway
.raw(msg
.replace(/^\/quote /i, ''));
343 gateway
.action(front
.cur_channel
.name
, msg
.substring(4));
344 //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
345 front
.cur_channel
.addMsg(null, ' ', '* ' + gateway
.nick
+ ' ' + msg
.substring(4), 'action', 'color:#555;');
350 msg
= parts
.slice(2).join(' ');
352 gateway
.notice(dest
, msg
);
353 this.onNotice({}, {nick
: gateway
.nick
, channel
: dest
, msg
: msg
});
357 if (parts
[1] !== undefined) {
358 front
.windowsShow(parseInt(parts
[1], 10));
363 gateway
.quit(msg
.split(" ", 2)[1]);
367 if (parts
[1] === undefined) {
369 if (t
.createTextRange
) {
370 pos
= t
.text().length();
371 textRange
= t
.createTextRange();
372 textRange
.collapse(true);
373 textRange
.moveEnd(pos
);
374 textRange
.moveStart(pos
);
376 } else if (t
.setSelectionRange
) {
377 t
.setSelectionRange(pos
, pos
);
380 gateway
.setTopic(front
.cur_channel
.name
, msg
.split(' ', 2)[1]);
381 //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
385 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
386 gateway
.raw(msg
.substring(1));
390 //alert('Sending message: '+msg);
391 if (msg
.trim() === '') {
394 if (front
.cur_channel
.name
!== 'server') {
395 gateway
.msg(front
.cur_channel
.name
, msg
);
397 d
= d
.getHours() + ":" + d
.getMinutes();
398 //front.addMsg(d, gateway.nick, msg);
399 front
.cur_channel
.addMsg(null, gateway
.nick
, msg
);
405 onMsg: function (e
, data
) {
406 var destination
, plugin_event
;
407 // Is this message from a user?
408 if (data
.channel
=== gateway
.nick
) {
409 destination
= data
.nick
.toLowerCase();
411 destination
= data
.channel
.toLowerCase();
414 plugin_event
= {nick
: data
.nick
, msg
:data
.msg
, destination
: destination
};
415 plugin_event
= plugs
.run('msg_recieved', plugin_event
);
416 if (!plugin_event
) return;
418 if (!front
.tabviewExists(plugin_event
.destination
)) {
419 front
.tabviewAdd(plugin_event
.destination
);
421 front
.tabviews
[plugin_event
.destination
].addMsg(null, plugin_event
.nick
, plugin_event
.msg
);
424 onDebug: function (e
, data
) {
425 if (!front
.tabviewExists('kiwi_debug')) {
426 front
.tabviewAdd('kiwi_debug');
428 front
.tabviews
.kiwi_debug
.addMsg(null, ' ', data
.msg
);
431 onAction: function (e
, data
) {
433 // Is this message from a user?
434 if (data
.channel
=== gateway
.nick
) {
435 destination
= data
.nick
;
437 destination
= data
.channel
;
440 if (!front
.tabviewExists(destination
)) {
441 front
.tabviewAdd(destination
);
443 front
.tabviews
[destination
.toLowerCase()].addMsg(null, ' ', '* ' + data
.nick
+ ' ' + data
.msg
, 'action', 'color:#555;');
446 onTopic: function (e
, data
) {
447 if (front
.tabviewExists(data
.channel
)) {
448 front
.tabviews
[data
.channel
.toLowerCase()].changeTopic(data
.topic
);
452 onNotice: function (e
, data
) {
453 var nick
= (data
.nick
=== undefined) ? '' : data
.nick
;
454 var enick
= '[' + nick
+ ']';
456 if (front
.tabviewExists(data
.target
)) {
457 front
.tabviews
[data
.target
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
458 } else if (front
.tabviewExists(nick
)) {
459 front
.tabviews
[nick
.toLowerCase()].addMsg(null, enick
, data
.msg
, 'notice');
461 front
.tabviews
.server
.addMsg(null, enick
, data
.msg
, 'notice');
465 onCTCPRequest: function (e
, data
) {
466 var msg
= data
.msg
.split(" ", 2);
469 if (typeof msg
[1] === 'undefined') {
472 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'PING ' + msg
[1] + String
.fromCharCode(1));
475 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String
.fromCharCode(1));
478 front
.tabviews
.server
.addMsg(null, 'CTCP [' + data
.nick
+ ']', data
.msg
, 'ctcp');
481 onCTCPResponse: function (e
, data
) {
484 onConnect: function (e
, data
) {
485 if (data
.connected
) {
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 onUserList: function (e
, data
) {
531 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
534 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
536 if (!document
.userlist_updating
) {
537 document
.userlist_updating
= true;
541 $.each(data
.users
, function (i
, item
) {
542 nick
= i
; //i.match(/^.+!/g);
544 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
547 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
549 onUserListEnd: function (e
, data
) {
550 document
.userlist_updating
= false;
553 onJoin: function (e
, data
) {
554 if (!front
.tabviewExists(data
.channel
)) {
555 front
.tabviewAdd(data
.channel
.toLowerCase());
558 if (data
.nick
=== gateway
.nick
) {
559 return; // Not needed as it's already in nicklist
561 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '--> ' + data
.nick
+ ' has joined', 'action', 'color:#009900;');
562 $('<li><a class="nick" onclick="front.userClick(this);">' + data
.nick
+ '</a></li>').appendTo(front
.tabviews
[data
.channel
.toLowerCase()].userlist
);
563 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
565 onPart: function (e
, data
) {
566 if (front
.tabviewExists(data
.channel
)) {
567 // If this is us, close the tabview
568 if (data
.nick
=== gateway
.nick
) {
569 front
.tabviews
[data
.channel
.toLowerCase()].close();
570 front
.tabviews
.server
.show();
574 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.nick
+ ' has left (' + data
.message
+ ')', 'action', 'color:#990000;');
575 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
576 if ($(this).text() === data
.nick
) {
582 onKick: function (e
, data
) {
583 if (front
.tabviewExists(data
.channel
)) {
584 // If this is us, close the tabview
585 if (data
.kicked
=== gateway
.nick
) {
586 //front.tabviews[data.channel.toLowerCase()].close();
587 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data
.channel
+ '. ' + data
.message
, 'status');
588 front
.tabviews
[data
.channel
.toLowerCase()].safe_to_close
= true;
589 $('li', front
.tabviews
[data
.channel
.toLowerCase()].userlist
).remove();
593 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.kicked
+ ' kicked by ' + data
.nick
+ '(' + data
.message
+ ')', 'action', 'color:#990000;');
594 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
595 if ($(this).text() === data
.nick
) {
601 onNick: function (e
, data
) {
602 if (data
.nick
=== gateway
.nick
) {
603 gateway
.nick
= data
.newnick
;
607 $.each(front
.tabviews
, function (i
, item
) {
608 $.each(front
.tabviews
, function (i
, item
) {
609 item
.changeNick(data
.newnick
, data
.nick
);
613 onQuit: function (e
, data
) {
614 $.each(front
.tabviews
, function (i
, item
) {
615 $.each(front
.tabviews
, function (i
, item
) {
616 item
.userlist
.children().each(function () {
617 if ($(this).text() === data
.nick
) {
619 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
625 onChannelRedirect: function (e
, data
) {
626 front
.tabviews
[data
.from.toLowerCase()].close();
627 front
.tabviewAdd(data
.to
.toLowerCase());
628 front
.tabviews
[data
.to
.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data
.from, 'action');
631 onIRCError: function (e
, data
) {
633 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
634 t_view
= data
.channel
;
639 switch (data
.error
) {
640 case 'banned_from_channel':
641 front
.tabviews
[t_view
].addMsg(null, ' ', '=== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
642 if (t_view
!== 'server') {
643 front
.tabviews
[t_view
].safe_to_close
= true;
646 case 'bad_channel_key':
647 front
.tabviews
[t_view
].addMsg(null, ' ', '=== Bad channel key for ' + data
.channel
, 'status');
648 if (t_view
!== 'server') {
649 front
.tabviews
[t_view
].safe_to_close
= true;
652 case 'invite_only_channel':
653 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is invite only.', 'status');
654 if (t_view
!== 'server') {
655 front
.tabviews
[t_view
].safe_to_close
= true;
658 case 'channel_is_full':
659 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is full.', 'status');
660 if (t_view
!== 'server') {
661 front
.tabviews
[t_view
].safe_to_close
= true;
664 case 'chanop_privs_needed':
665 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
668 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
671 // We don't know what data contains, so don't do anything with it.
672 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
676 registerKeys: function () {
677 $('#kiwi_msginput').bind('keydown', function (e
) {
678 var windows
, meta
, num
, msg
, data
, candidates
, word_pos
, word
;
679 windows
= $('#windows');
680 //var meta = e.altKey;
684 case (e
.which
>= 48) && (e
.which
<= 57):
691 front
.windowsShow(num
);
695 case e
.which
=== 27: // escape
697 case e
.which
=== 13: // return
698 msg
= $('#kiwi_msginput').val();
701 front
.buffer
.push(msg
);
702 front
.buffer_pos
= front
.buffer
.length
;
705 $('#kiwi_msginput').val('');
708 case e
.which
=== 33: // page up
709 console
.log("page up");
710 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
712 case e
.which
=== 34: // page down
713 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
715 case e
.which
=== 37: // left
717 front
.windowsPrevious();
721 case e
.which
=== 38: // up
722 if (front
.buffer_pos
> 0) {
724 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
727 case e
.which
=== 39: // right
733 case e
.which
=== 40: // down
734 if (front
.buffer_pos
< front
.buffer
.length
) {
736 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
740 case e
.which
=== 9: // tab
741 // Get possible autocompletions
743 front
.cur_channel
.userlist
.children().each(function () {
744 nick
= front
.nickStripPrefix($('a.nick', this).text());
748 // Do the autocomplete
749 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
752 word_pos
= this.value
.lastIndexOf(' ');
754 if (word_pos
=== -1) {
757 word
= this.value
.substr(word_pos
);
761 // filter data to find only strings that start with existing value
762 for (i
= 0; i
< data
.length
; i
++) {
763 if (data
[i
].indexOf(word
) === 0 && data
[i
].length
> word
.length
) {
764 candidates
.push(data
[i
]);
768 if (candidates
.length
> 0) {
769 // some candidates for autocompletion are found
770 this.value
= this.value
.substring(0, word_pos
) + ' ' + candidates
[0] + ': ';
771 this.selectionStart
= this.value
.length
;
779 $('#kiwi .control .msginput .nick').click(function () {
780 front
.showChangeNick();
787 $('#kiwi .plugins .load_plugin_file').click(function () {
789 if (typeof front
.boxes
.plugins
!== "undefined") {
793 front
.boxes
.plugins
= new Box("plugin_file");
794 $('#tmpl_plugins').tmpl({}).appendTo(front
.boxes
.plugins
.content
);
795 front
.boxes
.plugins
.box
.css('top', -(front
.boxes
.plugins
.height
+ 40));
797 // Populate the plugin list..
798 lst
= $('#plugin_list');
799 lst
.find('option').remove();
800 for (j
in plugins
.privmsg
) {
801 txt
= plugins
.privmsg
[j
].name
;
802 lst
.append('<option value="' + txt
+ '">' + txt
+ '</option>');
806 $('#kiwi .plugin_file').submit(function () {
807 $.getJSON($('.txtpluginfile').val(), function (data
) {
809 plg
.name
= data
.name
;
810 eval("plg.onprivmsg = " + data
.onprivmsg
);
811 eval("plg.onload = " + data
.onload
);
812 eval("plg.onunload = " + data
.onunload
);
813 plugins
.privmsg
.push(plg
);
815 if (plg
.onload
instanceof Function
) {
821 $('#kiwi .cancelpluginfile').click(function () {
822 front
.boxes
.plugins
.destroy();
825 $('#kiwi #plugins_list_unload').click(function () {
826 var selected_plugin
, i
;
827 selected_plugin
= $('#plugin_list').val();
828 console
.log("removing plugin: " + selected_plugin
);
829 for (i
in plugins
.privmsg
) {
830 if (plugins
.privmsg
[i
].name
=== selected_plugin
) {
831 if (plugins
.privmsg
[i
].onunload
instanceof Function
) {
832 plugins
.privmsg
[i
].onunload();
834 delete plugins
.privmsg
[i
];
839 $('#kiwi .txtpluginfile').focus();
843 $('#kiwi .plugins .reload_css').click(function () {
844 var links
= document
.getElementsByTagName("link"),
846 for (i
= 0; i
< links
.length
; i
++) {
847 if (links
[i
].rel
=== "stylesheet") {
848 if (links
[i
].href
.indexOf("?") === -1) {
849 links
[i
].href
+= "?";
851 links
[i
].href
+= "x";
857 $('#kiwi .about .about_close').click(function () {
858 $('#kiwi .about').css('display', 'none');
862 $('#kiwi .poweredby').click(function () {
863 $('#kiwi .about').css('display', 'block');
869 showChangeNick: function () {
870 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
872 $('#kiwi .form_newnick').submit(function () {
873 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
874 $('#kiwi .newnick').remove();
878 $('#kiwi .txtnewnick').keypress(function (ev
) {
879 if (!this.first_press
) {
880 this.first_press
= true;
885 $('#kiwi .txtnewnick').keydown(function (ev
) {
886 if (ev
.which
=== 27) { // ESC
887 $('#kiwi_msginput').focus();
888 $('#kiwi .newnick').remove();
892 $('#kiwi .cancelnewnick').click(function () {
893 $('#kiwi .newnick').remove();
896 $('#kiwi .txtnewnick').focus();
900 tabviewExists: function (name
) {
901 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
904 tabviewAdd: function (v_name
) {
905 var re
, htmlsafe_name
, tmp_divname
, tmp_userlistname
, tmp_tabname
, userlist_enabled
= true;
907 if (v_name
.charAt(0) === gateway
.channel_prefix
) {
908 re
= new RegExp(gateway
.channel_prefix
, "g");
909 htmlsafe_name
= v_name
.replace(re
, 'pre');
910 htmlsafe_name
= "chan_" + htmlsafe_name
;
912 htmlsafe_name
= 'query_' + v_name
;
913 userlist_enabled
= false;
916 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
917 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
918 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
920 if (!front
.tabviewExists(v_name
)) {
921 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
922 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname
+ '"></ul>');
923 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + v_name
.toLowerCase() + '\'].show();">' + v_name
+ '</li>');
925 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
926 //front.windowShow(v_name);
928 front
.tabviews
[v_name
.toLowerCase()] = new Tabview();
929 front
.tabviews
[v_name
.toLowerCase()].name
= v_name
;
930 front
.tabviews
[v_name
.toLowerCase()].div
= $('#' + tmp_divname
);
931 front
.tabviews
[v_name
.toLowerCase()].userlist
= $('#' + tmp_userlistname
);
932 front
.tabviews
[v_name
.toLowerCase()].tab
= $('#' + tmp_tabname
);
933 if (!userlist_enabled
) front
.tabviews
[v_name
.toLowerCase()].userlist_width
= 0;
934 front
.tabviews
[v_name
.toLowerCase()].show();
936 if (typeof registerTouches
=== "function") {
937 //alert("Registering touch interface");
938 //registerTouches($('#'+tmp_divname));
939 registerTouches(document
.getElementById(tmp_divname
));
942 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
943 alert($(this).attr('id'));
947 front
.doLayoutSize();
951 userClick: function (item
) {
952 // Remove any existing userboxes
953 $('#kiwi .userbox').remove();
955 var li
= $(item
).parent();
956 /*var html = '<div class="userbox">\
957 <input type="hidden" class="userbox_nick" value="' + front.nickStripPrefix($(item).text()) + '" />\
958 <a href="#" class="userbox_query">Message</a>\
959 <a href="#" class="userbox_whois">Info</a>\
962 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
964 $('#kiwi .userbox .userbox_query').click(function (ev
) {
965 var nick
= $('#kiwi .userbox_nick').val();
966 front
.run('/query ' + nick
);
969 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
970 var nick
= $('#kiwi .userbox_nick').val();
971 front
.run('/whois ' + nick
);
980 onSync: function (e
, data
) {
982 if (data
.nick
!== undefined) {
983 gateway
.nick
= data
.nick
;
987 if (data
.tabviews
!== undefined) {
988 $.each(data
.tabviews
, function (i
, tab
) {
989 if (!front
.tabviewExists(tab
.name
)) {
990 front
.tabviewAdd(gateway
.channel_prefix
+ tab
.name
);
992 if (tab
.userlist
!== undefined) {
993 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
1003 setTopicText: function (new_topic
) {
1004 front
.original_topic
= new_topic
;
1005 $('#kiwi .cur_topic .topic').text(new_topic
);
1006 front
.doLayoutSize();
1015 nickStripPrefix: function (nick
) {
1018 prefix
= tmp
.charAt(0);
1019 for (i
in gateway
.user_prefixes
) {
1020 if (gateway
.user_prefixes
[i
].symbol
!== prefix
) {
1023 return tmp
.substring(1);
1029 nickGetPrefix: function (nick
) {
1032 prefix
= tmp
.charAt(0);
1033 for (i
in gateway
.user_prefixes
) {
1034 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
1042 isChannel: function (name
) {
1043 prefix
= name
.charAt(0);
1044 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
1053 tabviewsNext: function () {
1054 var wl
= $('#kiwi .windowlist ul'),
1055 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) + 170;
1056 wl
.css('text-indent', next_left
);
1059 tabviewsPrevious: function () {
1060 var wl
= $('#kiwi .windowlist ul'),
1061 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) - 170;
1062 wl
.css('text-indent', next_left
);
1065 windowsNext: function () {
1068 for (tab
in front
.tabviews
) {
1070 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1075 front
.tabviews
[tab
].show();
1081 windowsPrevious: function () {
1082 var tab
, prev_tab
, next
;
1084 for (tab
in front
.tabviews
) {
1085 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1091 prev_tab
= front
.tabviews
[tab
];
1095 windowsShow: function (num
) {
1096 num
= parseInt(num
, 10);
1097 console
.log('Showing window ' + num
.toString());
1099 for (tab
in front
.tabviews
) {
1101 front
.tabviews
[tab
].show();
1128 var Utilityview = function (name
, src
) {
1130 var tmp_divname
= 'kiwi_window_' + name
;
1131 var tmp_userlistname
= 'kiwi_userlist_' + name
;
1132 var tmp_tabname
= 'kiwi_tab_' + name
;
1137 if (!front
.tabviewExists(name
)) {
1138 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
1139 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + name
.toLowerCase() + '\'].show();">' + name
+ '</li>');
1142 this.div
= $('#' + tmp_divname
);
1143 this.div
.css('overflow', 'hidden');
1145 this.tab
= $('#' + tmp_tabname
);
1147 this.iframe
= $('<iframe border="0" class="utility_view" src="" style="width:100%;height:100%;border:none;"></iframe>');
1148 if(src
) this.iframe
.attr('src', src
);
1149 this.div
.append(this.iframe
);
1151 front
.tabviews
[name
.toLowerCase()] = this;
1154 Utilityview
.prototype.name
= null;
1155 Utilityview
.prototype.div
= null;
1156 Utilityview
.prototype.tab
= null;
1157 Utilityview
.prototype.iframe
= null;
1158 Utilityview
.prototype.topic
= ' ';
1159 Utilityview
.prototype.show = function () {
1160 $('#kiwi .messages').removeClass("active");
1161 $('#kiwi .userlist ul').removeClass("active");
1162 $('#kiwi .windowlist ul li').removeClass("active");
1164 $('#windows').css('overflow-y', 'hidden');
1166 // Activate this tab!
1167 this.div
.addClass('active');
1168 this.tab
.addClass('active');
1170 this.addPartImage();
1172 front
.setTopicText(this.topic
);
1173 front
.cur_channel
= this;
1175 // If we're using fancy scrolling, refresh it
1177 touch_scroll
.refresh();
1181 Utilityview
.prototype.close = function () {
1185 if (front
.cur_channel
=== this) {
1186 front
.tabviews
.server
.show();
1188 delete front
.tabviews
[this.name
.toLowerCase()];
1191 Utilityview
.prototype.addPartImage = function () {
1192 this.clearPartImage();
1194 // We can't close this tab, so don't have the close image
1195 if (this.name
=== 'server') {
1199 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1200 this.tab
.append(del_html
);
1202 $('.tab_part', this.tab
).click(function () {
1203 if (front
.cur_channel
.name
!== 'server') {
1204 front
.cur_channel
.close();
1209 Utilityview
.prototype.clearPartImage = function () {
1210 $('#kiwi .windowlist .tab_part').remove();
1224 var Tabview = function () {};
1225 Tabview
.prototype.name
= null;
1226 Tabview
.prototype.div
= null;
1227 Tabview
.prototype.userlist
= null;
1228 Tabview
.prototype.userlist_width
= 100; // 0 for disabled
1229 Tabview
.prototype.tab
= null;
1230 Tabview
.prototype.topic
= "";
1231 Tabview
.prototype.safe_to_close
= false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1233 Tabview
.prototype.show = function () {
1236 $('#kiwi .messages').removeClass("active");
1237 $('#kiwi .userlist ul').removeClass("active");
1238 $('#kiwi .windowlist ul li').removeClass("active");
1241 u
= $('#kiwi .userlist');
1243 w
.css('overflow-y', 'scroll');
1245 // Set the window size accordingly
1246 if (this.userlist_width
> 0) {
1247 u
.width(this.userlist_width
);
1248 w
.css('right', u
.outerWidth(true));
1253 // Activate this tab!
1254 this.div
.addClass('active');
1255 if (this.userlist_width
> 0) this.userlist
.addClass('active');
1256 this.tab
.addClass('active');
1258 // Add the part image to the tab
1259 this.addPartImage();
1261 this.clearHighlight();
1262 front
.setTopicText(this.topic
);
1263 front
.cur_channel
= this;
1265 // If we're using fancy scrolling, refresh it
1267 touch_scroll
.refresh();
1270 this.scrollBottom();
1272 $('#kiwi_msginput').focus();
1276 Tabview
.prototype.close = function () {
1278 this.userlist
.remove();
1281 if (front
.cur_channel
=== this) {
1282 front
.tabviews
.server
.show();
1284 delete front
.tabviews
[this.name
.toLowerCase()];
1287 Tabview
.prototype.addPartImage = function () {
1288 this.clearPartImage();
1290 // We can't close this tab, so don't have the close image
1291 if (this.name
=== 'server') {
1295 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1296 this.tab
.append(del_html
);
1298 $('.tab_part', this.tab
).click(function () {
1299 if (front
.isChannel($(this).parent().text())) {
1302 // Make sure we don't close the server tab
1303 if (front
.cur_channel
.name
!== 'server') {
1304 front
.cur_channel
.close();
1310 Tabview
.prototype.clearPartImage = function () {
1311 $('#kiwi .windowlist .tab_part').remove();
1314 Tabview
.prototype.addMsg = function (time
, nick
, msg
, type
, style
) {
1315 var self
, tmp
, plugin_ret
, i
, d
, re
, line_msg
;
1319 tmp
= {msg
: msg
, time
: time
, nick
: nick
, tabview
: this.name
};
1320 tmp
= plugs
.run('addmsg', tmp
);
1328 if (time
=== null) {
1330 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1333 // The CSS class (action, topic, notice, etc)
1334 if (typeof type
!== "string") {
1338 // Make sure we don't have NaN or something
1339 if (typeof msg
!== "string") {
1345 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1347 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1348 msg
= msg
.replace(String
.fromCharCode(2), next
);
1349 next
= (next
=== '<b>') ? '</b>' : '<b>';
1351 if (next
=== '</b>') {
1356 // Wierd thing noticed by Dux0r on the irc.esper.net server
1357 if (typeof msg
!== "string") {
1362 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1364 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1365 msg
= msg
.replace(String
.fromCharCode(31), next
);
1366 next
= (next
=== '<u>') ? '</u>' : '<u>';
1368 if (next
=== '</u>') {
1373 // Make the channels clickable
1374 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1375 msg
= msg
.replace(re
, function (match
) {
1376 return '<a class="chan">' + match
+ '</a>';
1379 // Build up and add the line
1380 line_msg
= $('<div class="msg ' + type
+ '"><div class="time">' + time
+ '</div><div class="nick">' + nick
+ '</div><div class="text" style="' + style
+ '">' + msg
+ ' </div></div>');
1381 this.div
.append(line_msg
);
1384 this.scrollBottom();
1386 touch_scroll
.refresh();
1387 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1388 this.scrollBottom();
1389 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1390 // touch_scroll.scrollTo(0, this.div.height());
1395 Tabview
.prototype.scrollBottom = function () {
1396 var w
= $('#windows');
1397 w
[0].scrollTop
= w
[0].scrollHeight
;
1400 Tabview
.prototype.changeNick = function (newNick
, oldNick
) {
1401 this.userlist
.children().each(function () {
1402 var item
= $('a.nick', this);
1403 if (front
.nickStripPrefix(item
.text()) === oldNick
) {
1404 item
.text(front
.nickGetPrefix(item
.text()) + newNick
);
1405 document
.temp_chan
= 1;
1409 if (typeof document
.temp_chan
!== "undefined") {
1410 this.addMsg(null, ' ', '=== ' + oldNick
+ ' is now known as ' + newNick
, 'action');
1411 delete document
.temp_chan
;
1412 this.userlistSort();
1416 Tabview
.prototype.userlistSort = function () {
1417 var ul
= this.userlist
,
1418 listitems
= ul
.children('li').get();
1419 listitems
.sort(function (a
, b
) {
1420 var compA
= $(a
).text().toUpperCase(),
1421 compB
= $(b
).text().toUpperCase(),
1424 // Sort by prefixes first
1425 for (i
in gateway
.user_prefixes
) {
1426 prefix
= gateway
.user_prefixes
[i
].symbol
;
1428 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1429 // Both have the same prefix, string compare time
1433 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1437 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1442 // No prefixes, compare by string
1443 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1445 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1448 Tabview
.prototype.highlight = function () {
1449 this.tab
.addClass('highlight');
1451 Tabview
.prototype.activity = function () {
1452 this.tab
.addClass('activity');
1454 Tabview
.prototype.clearHighlight = function () {
1455 this.tab
.removeClass('highlight');
1456 this.tab
.removeClass('activity');
1458 Tabview
.prototype.changeTopic = function (new_topic
) {
1459 this.topic
= new_topic
;
1460 this.addMsg(null, ' ', '=== Topic for ' + this.name
+ ' is: ' + new_topic
, 'topic');
1461 if (front
.cur_channel
.name
=== this.name
) {
1462 front
.setTopicText(new_topic
);
1470 var Box = function (classname
) {
1471 this.id
= randomString(10);
1472 var tmp
= $('<div id="' + this.id
+ '" class="box ' + classname
+ '"><div class="boxarea"></div></div>');
1473 $('#kiwi').append(tmp
);
1474 this.box
= $('#' + this.id
);
1475 this.content
= $('#' + this.id
+ ' .boxarea');
1477 this.box
.draggable({ stack
: ".box" });
1478 this.content
.click(function () {});
1479 //this.box.click(function(){ $(this)..css });
1481 Box
.prototype.create = function (name
, classname
) {
1484 Box
.prototype.id
= null;
1485 Box
.prototype.box
= null;
1486 Box
.prototype.content
= null;
1487 Box
.prototype.destroy = function () {
1490 for (name
in front
.boxes
) {
1491 if (front
.boxes
[name
].id
=== this.id
) {
1492 delete front
.boxes
[name
];
1496 Box
.prototype.height = function () {
1497 return this.box
.height();