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("ondisconnect", front
.onDisconnect
);
28 $(gateway
).bind("onnick", front
.onNick
);
29 $(gateway
).bind("onuserlist", front
.onUserList
);
30 $(gateway
).bind("onuserlist_end", front
.onUserListEnd
);
31 $(gateway
).bind("onjoin", front
.onJoin
);
32 $(gateway
).bind("ontopic", front
.onTopic
);
33 $(gateway
).bind("onpart", front
.onPart
);
34 $(gateway
).bind("onkick", front
.onKick
);
35 $(gateway
).bind("onquit", front
.onQuit
);
36 $(gateway
).bind("onwhois", front
.onWhois
);
37 $(gateway
).bind("onsync", front
.onSync
);
38 $(gateway
).bind("onchannel_redirect", front
.onChannelRedirect
);
39 $(gateway
).bind("ondebug", front
.onDebug
);
40 $(gateway
).bind("onctcp_request", front
.onCTCPRequest
);
41 $(gateway
).bind("onctcp_response", front
.onCTCPResponse
);
42 $(gateway
).bind("onirc_error", front
.onIRCError
);
46 // Build the about box
47 front
.boxes
.about
= new Box("about");
48 about_info
= 'UI adapted for ' + agent
;
50 about_info
+= ' touchscreen ';
52 about_info
+= 'usage';
53 $('#tmpl_about_box').tmpl({
55 front_revision
: front
.revision
,
56 gateway_revision
: gateway
.revision
57 }).appendTo(front
.boxes
.about
.content
);
59 //$(window).bind("beforeunload", function(){ gateway.quit(); });
62 $('#kiwi').addClass('touchscreen');
64 // Single touch scrolling through scrollback for touchscreens
66 touch_scroll
= new iScroll('windows', scroll_opts
);
71 $('#kiwi .cur_topic').resize(front
.doLayoutSize
);
73 $('#kiwi .formconnectwindow').submit(function () {
74 var netsel
= $('#kiwi .formconnectwindow .network'),
75 nick
= $('#kiwi .formconnectwindow .nick'),
78 if (nick
.val() === '') {
79 nick
.val('Nick please!');
84 tmp
= nick
.val().split(' ');
85 gateway
.nick
= tmp
[0];
88 front
.run('/connect ' + netsel
.val());
93 $('#kiwi .connectwindow').slideUp();
94 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
99 supportsOrientationChange
= (typeof window
.onorientationchange
!== undefined);
100 orientationEvent
= supportsOrientationChange
? "orientationchange" : "resize";
101 window
.addEventListener(orientationEvent
, front
.doLayoutSize
, false);
102 //$('#kiwi').bind("resize", front.doLayoutSize, false);
105 //front.windowAdd('server');
106 front
.tabviewAdd('server');
108 // Any pre-defined nick?
109 if (typeof init_data
.nick
=== "string") {
110 $('#kiwi .formconnectwindow .nick').val(init_data
.nick
);
113 //gateway.session_id = 'testses';
115 $('#kiwi .cur_topic').keydown(function (e
) {
116 if (e
.which
=== 13) {
120 $('#kiwi_msginput').focus();
121 } else if (e
.which
=== 27) {
124 $(this).text(front
.original_topic
);
125 $('#kiwi_msginput').focus();
128 /*$('.cur_topic').live('keypress', function(e) {
129 if (e.keyCode === 13) {
133 $('#kiwi_msginput').focus();
134 } else if (e.keyCode === 27) {
137 $(this).text(front.original_topic);
140 $('.cur_topic').live('change', function () {
142 text
= $(this).text();
143 if (text
!== front
.original_topic
) {
144 chan
= front
.cur_channel
.name
;
145 gateway
.setTopic(chan
, text
);
154 doLayoutSize: function () {
155 var kiwi
, ct
, ul
, n_top
, n_bottom
;
157 if (kiwi
.width() < 330 && !kiwi
.hasClass('small_kiwi')) {
158 console
.log("switching to small kiwi");
159 kiwi
.removeClass('large_kiwi');
160 kiwi
.addClass('small_kiwi');
161 } else if (kiwi
.width() >= 330 && !kiwi
.hasClass('large_kiwi')) {
162 kiwi
.removeClass('small_kiwi');
163 kiwi
.addClass('large_kiwi');
166 ct
= $('#kiwi .cur_topic');
167 ul
= $('#kiwi .userlist');
169 n_top
= parseInt(ct
.offset().top
, 10) + parseInt(ct
.height(), 10);
170 n_top
= n_top
+ parseInt(ct
.css('border-top-width').replace('px', ''), 10);
171 n_top
= n_top
+ parseInt(ct
.css('border-bottom-width').replace('px', ''), 10);
172 n_top
= n_top
+ parseInt(ct
.css('padding-top').replace('px', ''), 10);
173 n_top
= n_top
+ parseInt(ct
.css('padding-bottom').replace('px', ''), 10);
174 n_top
+= 1; // Dunno why this is needed.. but it's always 1 px out :/
176 n_bottom
= $(document
).height() - parseInt($('#kiwi .control').offset().top
, 10);
178 $('#kiwi .windows').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
179 $('#kiwi .userlist').css({top
: n_top
+ 'px', bottom
: n_bottom
+ 'px'});
183 doLayout: function () {
184 $('#kiwi .msginput .nick a').text(gateway
.nick
);
185 $('#kiwi_msginput').val(' ');
186 $('#kiwi_msginput').focus();
190 joinChannel: function (chan_name
) {
191 var chans
= chan_name
.split(','),
195 if (front
.tabviews
[chan
.toLowerCase()] === undefined || (front
.tabviews
[chan
.toLowerCase()] !== undefined && front
.tabviews
[chan
.toLowerCase()].safe_to_close
=== true)) {
197 front
.tabviewAdd(chan
);
199 front
.tabviews
[chan
.toLowerCase()].show();
205 run: function (msg
) {
206 var parts
, dest
, t
, pos
, textRange
, d
;
207 console
.log("running " + msg
);
208 if (msg
.substring(0, 1) === '/') {
209 parts
= msg
.split(' ');
210 switch (parts
[0].toLowerCase()) {
213 front
.joinChannel(parts
[1]);
218 if (parts
[1] === undefined) {
219 alert('Usage: /connect servername [port]');
223 if (parts
[2] === undefined) {
226 front
.cur_channel
.addMsg(null, ' ', '=== Connecting to ' + parts
[1] + '...', 'status');
227 gateway
.connect(parts
[1], parts
[2], 0);
231 console
.log("/nick");
232 if (parts
[1] === undefined) {
233 console
.log("calling show nick");
234 front
.showChangeNick();
236 console
.log("sending raw");
237 gateway
.raw(msg
.substring(1));
242 if (typeof parts
[1] === "undefined") {
243 if (front
.cur_channel
.safe_to_close
) {
244 front
.cur_channel
.close();
246 gateway
.raw(msg
.substring(1) + ' ' + front
.cur_channel
.name
);
249 gateway
.raw(msg
.substring(1));
254 if (typeof parts
[1] !== "undefined") {
255 gateway
.raw(msg
.substring(1));
265 if (typeof parts
[1] !== "undefined") {
266 front
.tabviewAdd(parts
[1]);
271 gateway
.raw(msg
.replace(/^\/quote /i, ''));
275 gateway
.action(front
.cur_channel
.name
, msg
.substring(4));
276 //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;');
277 front
.cur_channel
.addMsg(null, ' ', '* ' + gateway
.nick
+ ' ' + msg
.substring(4), 'action', 'color:#555;');
282 msg
= parts
.slice(2).join(' ');
284 gateway
.notice(dest
, msg
);
285 this.onNotice({}, {nick
: gateway
.nick
, channel
: dest
, msg
: msg
});
289 if (parts
[1] !== undefined) {
290 front
.windowsShow(parseInt(parts
[1], 10));
295 gateway
.quit(msg
.split(" ", 2)[1]);
299 if (parts
[1] === undefined) {
301 if (t
.createTextRange
) {
302 pos
= t
.text().length();
303 textRange
= t
.createTextRange();
304 textRange
.collapse(true);
305 textRange
.moveEnd(pos
);
306 textRange
.moveStart(pos
);
308 } else if (t
.setSelectionRange
) {
309 t
.setSelectionRange(pos
, pos
);
312 gateway
.setTopic(front
.cur_channel
.name
, msg
.split(' ', 2)[1]);
313 //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]);
317 //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1));
318 gateway
.raw(msg
.substring(1));
322 //alert('Sending message: '+msg);
323 if (msg
.trim() === '') {
326 if (front
.cur_channel
.name
!== 'server') {
327 gateway
.msg(front
.cur_channel
.name
, msg
);
329 d
= d
.getHours() + ":" + d
.getMinutes();
330 //front.addMsg(d, gateway.nick, msg);
331 front
.cur_channel
.addMsg(null, gateway
.nick
, msg
);
337 onMsg: function (e
, data
) {
339 // Is this message from a user?
340 if (data
.channel
=== gateway
.nick
) {
341 destination
= data
.nick
.toLowerCase();
343 destination
= data
.channel
.toLowerCase();
346 if (!front
.tabviewExists(destination
)) {
347 front
.tabviewAdd(destination
);
349 front
.tabviews
[destination
].addMsg(null, data
.nick
, data
.msg
);
352 onDebug: function (e
, data
) {
353 if (!front
.tabviewExists('kiwi_debug')) {
354 front
.tabviewAdd('kiwi_debug');
356 front
.tabviews
.kiwi_debug
.addMsg(null, ' ', data
.msg
);
359 onAction: function (e
, data
) {
361 // Is this message from a user?
362 if (data
.channel
=== gateway
.nick
) {
363 destination
= data
.nick
;
365 destination
= data
.channel
;
368 if (!front
.tabviewExists(destination
)) {
369 front
.tabviewAdd(destination
);
371 front
.tabviews
[destination
.toLowerCase()].addMsg(null, ' ', '* ' + data
.nick
+ ' ' + data
.msg
, 'action', 'color:#555;');
374 onTopic: function (e
, data
) {
375 if (front
.tabviewExists(data
.channel
)) {
376 front
.tabviews
[data
.channel
.toLowerCase()].changeTopic(data
.topic
);
380 onNotice: function (e
, data
) {
381 var nick
= (data
.nick
=== undefined || data
.nick
=== '') ? '' : '[' + data
.nick
+ ']';
382 if (data
.channel
!== undefined) {
383 if (front
.tabviewExists(data
.channel
)) {
384 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, nick
, data
.msg
, 'notice');
386 front
.tabviews
.server
.addMsg(null, nick
, data
.msg
, 'notice');
389 front
.tabviews
.server
.addMsg(null, nick
, data
.msg
, 'notice');
393 onCTCPRequest: function (e
, data
) {
394 var msg
= data
.msg
.split(" ", 2);
397 if (typeof msg
[1] === 'undefined') {
400 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'PING ' + msg
[1] + String
.fromCharCode(1));
403 gateway
.notice(data
.nick
, String
.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String
.fromCharCode(1));
406 front
.tabviews
.server
.addMsg(null, 'CTCP [' + data
.nick
+ ']', data
.msg
, 'ctcp');
409 onCTCPResponse: function (e
, data
) {
412 onConnect: function (e
, data
) {
413 if (data
.connected
) {
414 front
.tabviews
.server
.addMsg(null, ' ', '=== Connected OK :)', 'status');
415 if (typeof init_data
.channel
=== "string") {
416 front
.joinChannel(init_data
.channel
);
419 front
.tabviews
.server
.addMsg(null, ' ', '=== Failed to connect :(', 'status');
422 onDisconnect: function (e
, data
) {
424 for (tab
in front
.tabviews
) {
425 front
.tabviews
[tab
].addMsg(null, '', 'Disconnected from server!', 'error');
428 onOptions: function (e
, data
) {
429 if (typeof gateway
.network_name
=== "string" && gateway
.network_name
!== "") {
430 front
.tabviews
.server
.tab
.text(gateway
.network_name
);
433 onMOTD: function (e
, data
) {
434 front
.tabviews
.server
.addMsg(null, data
.server
, data
.msg
, 'motd');
436 onWhois: function (e
, data
) {
439 front
.cur_channel
.addMsg(null, data
.nick
, data
.msg
, 'whois');
440 } else if (data
.logon
) {
442 d
.setTime(data
.logon
* 1000);
443 d
= d
.toLocaleString();
444 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' second' + ((data
.idle
!== 1) ? 's' : '') + ', signed on ' + d
, 'whois');
446 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' seconds', 'whois');
449 onUserList: function (e
, data
) {
451 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
454 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
456 if (!document
.userlist_updating
) {
457 document
.userlist_updating
= true;
461 $.each(data
.users
, function (i
, item
) {
462 nick
= i
; //i.match(/^.+!/g);
464 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
467 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
469 onUserListEnd: function (e
, data
) {
470 document
.userlist_updating
= false;
473 onJoin: function (e
, data
) {
474 if (!front
.tabviewExists(data
.channel
)) {
475 front
.tabviewAdd(data
.channel
.toLowerCase());
478 if (data
.nick
=== gateway
.nick
) {
479 return; // Not needed as it's already in nicklist
481 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '--> ' + data
.nick
+ ' has joined', 'action', 'color:#009900;');
482 $('<li><a class="nick" onclick="front.userClick(this);">' + data
.nick
+ '</a></li>').appendTo(front
.tabviews
[data
.channel
.toLowerCase()].userlist
);
483 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
485 onPart: function (e
, data
) {
486 if (front
.tabviewExists(data
.channel
)) {
487 // If this is us, close the tabview
488 if (data
.nick
=== gateway
.nick
) {
489 front
.tabviews
[data
.channel
.toLowerCase()].close();
490 front
.tabviews
.server
.show();
494 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.nick
+ ' has left (' + data
.message
+ ')', 'action', 'color:#990000;');
495 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
496 if ($(this).text() === data
.nick
) {
502 onKick: function (e
, data
) {
503 if (front
.tabviewExists(data
.channel
)) {
504 // If this is us, close the tabview
505 if (data
.kicked
=== gateway
.nick
) {
506 //front.tabviews[data.channel.toLowerCase()].close();
507 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data
.channel
+ '. ' + data
.message
, 'status');
508 front
.tabviews
[data
.channel
.toLowerCase()].safe_to_close
= true;
509 $('li', front
.tabviews
[data
.channel
.toLowerCase()].userlist
).remove();
513 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.kicked
+ ' kicked by ' + data
.nick
+ '(' + data
.message
+ ')', 'action', 'color:#990000;');
514 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
515 if ($(this).text() === data
.nick
) {
521 onNick: function (e
, data
) {
522 if (data
.nick
=== gateway
.nick
) {
523 gateway
.nick
= data
.newnick
;
527 $.each(front
.tabviews
, function (i
, item
) {
528 $.each(front
.tabviews
, function (i
, item
) {
529 item
.changeNick(data
.newnick
, data
.nick
);
533 onQuit: function (e
, data
) {
534 $.each(front
.tabviews
, function (i
, item
) {
535 $.each(front
.tabviews
, function (i
, item
) {
536 item
.userlist
.children().each(function () {
537 if ($(this).text() === data
.nick
) {
539 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
545 onChannelRedirect: function (e
, data
) {
546 front
.tabviews
[data
.from.toLowerCase()].close();
547 front
.tabviewAdd(data
.to
.toLowerCase());
548 front
.tabviews
[data
.to
.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data
.from, 'action');
551 onIRCError: function (e
, data
) {
553 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
554 t_view
= data
.channel
;
559 switch (data
.error
) {
560 case 'banned_from_channel':
561 front
.tabviews
[t_view
].addMsg(null, ' ', '=== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
562 if (t_view
!== 'server') {
563 front
.tabviews
[t_view
].safe_to_close
= true;
566 case 'bad_channel_key':
567 front
.tabviews
[t_view
].addMsg(null, ' ', '=== Bad channel key for ' + data
.channel
, 'status');
568 if (t_view
!== 'server') {
569 front
.tabviews
[t_view
].safe_to_close
= true;
572 case 'invite_only_channel':
573 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is invite only.', 'status');
574 if (t_view
!== 'server') {
575 front
.tabviews
[t_view
].safe_to_close
= true;
578 case 'channel_is_full':
579 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is full.', 'status');
580 if (t_view
!== 'server') {
581 front
.tabviews
[t_view
].safe_to_close
= true;
584 case 'chanop_privs_needed':
585 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
588 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
591 // We don't know what data contains, so don't do anything with it.
592 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
596 registerKeys: function () {
597 $('#kiwi_msginput').bind('keydown', function (e
) {
598 var windows
, meta
, num
, msg
, data
, candidates
, word_pos
, word
;
599 windows
= $('#windows');
600 //var meta = e.altKey;
604 case (e
.which
>= 48) && (e
.which
<= 57):
611 front
.windowsShow(num
);
615 case e
.which
=== 27: // escape
617 case e
.which
=== 13: // return
618 msg
= $('#kiwi_msginput').val();
621 front
.buffer
.push(msg
);
622 front
.buffer_pos
= front
.buffer
.length
;
625 $('#kiwi_msginput').val('');
628 case e
.which
=== 33: // page up
629 console
.log("page up");
630 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
632 case e
.which
=== 34: // page down
633 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
635 case e
.which
=== 37: // left
637 front
.windowsPrevious();
641 case e
.which
=== 38: // up
642 if (front
.buffer_pos
> 0) {
644 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
647 case e
.which
=== 39: // right
653 case e
.which
=== 40: // down
654 if (front
.buffer_pos
< front
.buffer
.length
) {
656 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
660 case e
.which
=== 9: // tab
661 // Get possible autocompletions
663 front
.cur_channel
.userlist
.children().each(function () {
664 nick
= front
.nickStripPrefix($('a.nick', this).text());
668 // Do the autocomplete
669 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
672 word_pos
= this.value
.lastIndexOf(' ');
674 if (word_pos
=== -1) {
677 word
= this.value
.substr(word_pos
);
681 // filter data to find only strings that start with existing value
682 for (i
= 0; i
< data
.length
; i
++) {
683 if (data
[i
].indexOf(word
) === 0 && data
[i
].length
> word
.length
) {
684 candidates
.push(data
[i
]);
688 if (candidates
.length
> 0) {
689 // some candidates for autocompletion are found
690 this.value
= this.value
.substring(0, word_pos
) + ' ' + candidates
[0] + ': ';
691 this.selectionStart
= this.value
.length
;
699 $('#kiwi .control .msginput .nick').click(function () {
700 front
.showChangeNick();
707 $('#kiwi .plugins .load_plugin_file').click(function () {
709 if (typeof front
.boxes
.plugins
!== "undefined") {
713 front
.boxes
.plugins
= new Box("plugin_file");
714 $('#tmpl_plugins').tmpl({}).appendTo(front
.boxes
.plugins
.content
);
715 front
.boxes
.plugins
.box
.css('top', -(front
.boxes
.plugins
.height
+ 40));
717 // Populate the plugin list..
718 lst
= $('#plugin_list');
719 lst
.find('option').remove();
720 for (j
in plugins
.privmsg
) {
721 txt
= plugins
.privmsg
[j
].name
;
722 lst
.append('<option value="' + txt
+ '">' + txt
+ '</option>');
726 $('#kiwi .plugin_file').submit(function () {
727 $.getJSON($('.txtpluginfile').val(), function (data
) {
729 plg
.name
= data
.name
;
730 eval("plg.onprivmsg = " + data
.onprivmsg
);
731 eval("plg.onload = " + data
.onload
);
732 eval("plg.onunload = " + data
.onunload
);
733 plugins
.privmsg
.push(plg
);
735 if (plg
.onload
instanceof Function
) {
741 $('#kiwi .cancelpluginfile').click(function () {
742 front
.boxes
.plugins
.destroy();
745 $('#kiwi #plugins_list_unload').click(function () {
746 var selected_plugin
, i
;
747 selected_plugin
= $('#plugin_list').val();
748 console
.log("removing plugin: " + selected_plugin
);
749 for (i
in plugins
.privmsg
) {
750 if (plugins
.privmsg
[i
].name
=== selected_plugin
) {
751 if (plugins
.privmsg
[i
].onunload
instanceof Function
) {
752 plugins
.privmsg
[i
].onunload();
754 delete plugins
.privmsg
[i
];
759 $('#kiwi .txtpluginfile').focus();
763 $('#kiwi .plugins .reload_css').click(function () {
764 var links
= document
.getElementsByTagName("link"),
766 for (i
= 0; i
< links
.length
; i
++) {
767 if (links
[i
].rel
=== "stylesheet") {
768 if (links
[i
].href
.indexOf("?") === -1) {
769 links
[i
].href
+= "?";
771 links
[i
].href
+= "x";
777 $('#kiwi .about .about_close').click(function () {
778 $('#kiwi .about').css('display', 'none');
782 $('#kiwi .poweredby').click(function () {
783 $('#kiwi .about').css('display', 'block');
789 showChangeNick: function () {
790 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
792 $('#kiwi .form_newnick').submit(function () {
793 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
794 $('#kiwi .newnick').remove();
798 $('#kiwi .txtnewnick').keypress(function (ev
) {
799 if (!this.first_press
) {
800 this.first_press
= true;
805 $('#kiwi .txtnewnick').keydown(function (ev
) {
806 if (ev
.which
=== 27) { // ESC
807 $('#kiwi_msginput').focus();
808 $('#kiwi .newnick').remove();
812 $('#kiwi .cancelnewnick').click(function () {
813 $('#kiwi .newnick').remove();
816 $('#kiwi .txtnewnick').focus();
820 tabviewExists: function (name
) {
821 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
824 tabviewAdd: function (v_name
) {
825 var re
, htmlsafe_name
, tmp_divname
, tmp_userlistname
, tmp_tabname
;
826 if (v_name
.charAt(0) === gateway
.channel_prefix
) {
827 re
= new RegExp(gateway
.channel_prefix
, "g");
828 htmlsafe_name
= v_name
.replace(re
, 'pre');
829 htmlsafe_name
= "chan_" + htmlsafe_name
;
831 htmlsafe_name
= 'query_' + v_name
;
834 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
835 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
836 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
838 if (!front
.tabviewExists(v_name
)) {
839 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
840 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname
+ '"></ul>');
841 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + v_name
.toLowerCase() + '\'].show();">' + v_name
+ '</li>');
843 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
844 //front.windowShow(v_name);
846 front
.tabviews
[v_name
.toLowerCase()] = new Tabview();
847 front
.tabviews
[v_name
.toLowerCase()].name
= v_name
;
848 front
.tabviews
[v_name
.toLowerCase()].div
= $('#' + tmp_divname
);
849 front
.tabviews
[v_name
.toLowerCase()].userlist
= $('#' + tmp_userlistname
);
850 front
.tabviews
[v_name
.toLowerCase()].tab
= $('#' + tmp_tabname
);
851 front
.tabviews
[v_name
.toLowerCase()].show();
853 if (typeof registerTouches
=== "function") {
854 //alert("Registering touch interface");
855 //registerTouches($('#'+tmp_divname));
856 registerTouches(document
.getElementById(tmp_divname
));
859 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
860 alert($(this).attr('id'));
864 front
.doLayoutSize();
868 userClick: function (item
) {
869 // Remove any existing userboxes
870 $('#kiwi .userbox').remove();
872 var li
= $(item
).parent();
873 /*var html = '<div class="userbox">\
874 <input type="hidden" class="userbox_nick" value="' + front.nickStripPrefix($(item).text()) + '" />\
875 <a href="#" class="userbox_query">Message</a>\
876 <a href="#" class="userbox_whois">Info</a>\
879 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
881 $('#kiwi .userbox .userbox_query').click(function (ev
) {
882 var nick
= $('#kiwi .userbox_nick').val();
883 front
.run('/query ' + nick
);
886 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
887 var nick
= $('#kiwi .userbox_nick').val();
888 front
.run('/whois ' + nick
);
897 onSync: function (e
, data
) {
899 if (data
.nick
!== undefined) {
900 gateway
.nick
= data
.nick
;
904 if (data
.tabviews
!== undefined) {
905 $.each(data
.tabviews
, function (i
, tab
) {
906 if (!front
.tabviewExists(tab
.name
)) {
907 front
.tabviewAdd(gateway
.channel_prefix
+ tab
.name
);
909 if (tab
.userlist
!== undefined) {
910 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
920 setTopicText: function (new_topic
) {
921 front
.original_topic
= new_topic
;
922 $('#kiwi .cur_topic .topic').text(new_topic
);
923 front
.doLayoutSize();
932 nickStripPrefix: function (nick
) {
935 prefix
= tmp
.charAt(0);
936 for (i
in gateway
.user_prefixes
) {
937 if (gateway
.user_prefixes
[i
].symbol
!== prefix
) {
940 return tmp
.substring(1);
946 nickGetPrefix: function (nick
) {
949 prefix
= tmp
.charAt(0);
950 for (i
in gateway
.user_prefixes
) {
951 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
959 isChannel: function (name
) {
960 prefix
= name
.charAt(0);
961 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
970 tabviewsNext: function () {
971 var wl
= $('#kiwi .windowlist ul'),
972 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) + 170;
973 wl
.css('text-indent', next_left
);
976 tabviewsPrevious: function () {
977 var wl
= $('#kiwi .windowlist ul'),
978 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) - 170;
979 wl
.css('text-indent', next_left
);
982 windowsNext: function () {
985 for (tab
in front
.tabviews
) {
987 if (front
.tabviews
[tab
] === front
.cur_channel
) {
992 front
.tabviews
[tab
].show();
998 windowsPrevious: function () {
999 var tab
, prev_tab
, next
;
1001 for (tab
in front
.tabviews
) {
1002 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1008 prev_tab
= front
.tabviews
[tab
];
1012 windowsShow: function (num
) {
1013 num
= parseInt(num
, 10);
1014 console
.log('Showing window ' + num
.toString());
1016 for (tab
in front
.tabviews
) {
1018 front
.tabviews
[tab
].show();
1049 var Tabview = function () {};
1050 Tabview
.prototype.name
= null;
1051 Tabview
.prototype.div
= null;
1052 Tabview
.prototype.userlist
= null;
1053 Tabview
.prototype.tab
= null;
1054 Tabview
.prototype.topic
= "";
1055 Tabview
.prototype.safe_to_close
= false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1057 Tabview
.prototype.show = function () {
1058 $('#kiwi .messages').removeClass("active");
1059 $('#kiwi .userlist ul').removeClass("active");
1060 $('#kiwi .windowlist ul li').removeClass("active");
1062 // Activate this tab!
1063 this.div
.addClass('active');
1064 this.userlist
.addClass('active');
1065 this.tab
.addClass('active');
1067 document
.tmp
= this.div
;
1068 // Add the part image to the tab
1069 this.addPartImage();
1071 this.clearHighlight();
1072 front
.setTopicText(this.topic
);
1073 front
.cur_channel
= this;
1075 // If we're using fancy scrolling, refresh it
1077 touch_scroll
.refresh();
1080 this.scrollBottom();
1082 $('#kiwi_msginput').focus();
1086 Tabview
.prototype.close = function () {
1088 this.userlist
.remove();
1091 if (front
.cur_channel
=== this) {
1092 front
.tabviews
.server
.show();
1094 delete front
.tabviews
[this.name
.toLowerCase()];
1097 Tabview
.prototype.addPartImage = function () {
1098 this.clearPartImage();
1100 // We can't close this tab, so don't have the close image
1101 if (this.name
=== 'server') {
1105 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1106 this.tab
.append(del_html
);
1108 $('.tab_part', this.tab
).click(function () {
1109 if (front
.isChannel($(this).parent().text())) {
1112 // Make sure we don't close the server tab
1113 if (front
.cur_channel
.name
!== 'server') {
1114 front
.cur_channel
.close();
1120 Tabview
.prototype.clearPartImage = function () {
1121 $('#kiwi .windowlist .tab_part').remove();
1124 Tabview
.prototype.addMsg = function (time
, nick
, msg
, type
, style
) {
1125 var html_nick
, self
, tmp
, plugin_ret
, i
, d
, re
, line_msg
;
1126 html_nick
= $('<div/>').text(nick
).html();
1132 for (i
in plugins
.privmsg
) {
1133 if ((plugins
.privmsg
[i
].onprivmsg
instanceof Function
)) {
1136 plugin_ret
= plugins
.privmsg
[i
].onprivmsg(tmp
, this.name
);
1138 // If this plugin has returned false, do not add this message
1139 if (plugin_ret
=== false) {
1145 // If we actually have a string from the plugin, use it
1146 if (typeof plugin_ret
=== "string") {
1153 //var html_msg = $('<div/>').text(msg).html()+' '; // Add the space so the styling always has at least 1 character to go from
1154 if (time
=== null) {
1156 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1159 // The CSS class (action, topic, notice, etc)
1160 if (typeof type
!== "string") {
1164 // Make sure we don't have NaN or something
1165 if (typeof msg
!== "string") {
1171 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1173 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1174 msg
= msg
.replace(String
.fromCharCode(2), next
);
1175 next
= (next
=== '<b>') ? '</b>' : '<b>';
1177 if (next
=== '</b>') {
1182 // Wierd thing noticed by Dux0r on the irc.esper.net server
1183 if (typeof msg
!== "string") {
1188 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1190 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1191 msg
= msg
.replace(String
.fromCharCode(31), next
);
1192 next
= (next
=== '<u>') ? '</u>' : '<u>';
1194 if (next
=== '</u>') {
1199 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1201 msg
= msg
.replace(re
, function (match
) {
1202 return '<a class="chan" href="#">' + match
+ '</a>';
1205 line_msg
= $('<div class="msg ' + type
+ '"><div class="time">' + time
+ '</div><div class="nick">' + html_nick
+ '</div><div class="text" style="' + style
+ '">' + msg
+ ' </div></div>');
1206 this.div
.append(line_msg
);
1209 this.scrollBottom();
1211 touch_scroll
.refresh();
1212 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1213 this.scrollBottom();
1214 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1215 // touch_scroll.scrollTo(0, this.div.height());
1220 Tabview
.prototype.scrollBottom = function () {
1221 var w
= $('#windows');
1222 w
[0].scrollTop
= w
[0].scrollHeight
;
1225 Tabview
.prototype.changeNick = function (newNick
, oldNick
) {
1226 this.userlist
.children().each(function () {
1227 var item
= $('a.nick', this);
1228 if (front
.nickStripPrefix(item
.text()) === oldNick
) {
1229 item
.text(front
.nickGetPrefix(item
.text()) + newNick
);
1230 document
.temp_chan
= 1;
1234 if (typeof document
.temp_chan
!== "undefined") {
1235 this.addMsg(null, ' ', '=== ' + oldNick
+ ' is now known as ' + newNick
, 'action');
1236 delete document
.temp_chan
;
1237 this.userlistSort();
1241 Tabview
.prototype.userlistSort = function () {
1242 var ul
= this.userlist
,
1243 listitems
= ul
.children('li').get();
1244 listitems
.sort(function (a
, b
) {
1245 var compA
= $(a
).text().toUpperCase(),
1246 compB
= $(b
).text().toUpperCase(),
1249 // Sort by prefixes first
1250 for (i
in gateway
.user_prefixes
) {
1251 prefix
= gateway
.user_prefixes
[i
].symbol
;
1253 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1254 // Both have the same prefix, string compare time
1258 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1262 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1267 // No prefixes, compare by string
1268 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1270 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1273 Tabview
.prototype.highlight = function () {
1274 this.tab
.addClass('highlight');
1276 Tabview
.prototype.activity = function () {
1277 this.tab
.addClass('activity');
1279 Tabview
.prototype.clearHighlight = function () {
1280 this.tab
.removeClass('highlight');
1281 this.tab
.removeClass('activity');
1283 Tabview
.prototype.changeTopic = function (new_topic
) {
1284 this.topic
= new_topic
;
1285 this.addMsg(null, ' ', '=== Topic for ' + this.name
+ ' is: ' + new_topic
, 'topic');
1286 if (front
.cur_channel
.name
=== this.name
) {
1287 front
.setTopicText(new_topic
);
1295 var Box = function (classname
) {
1296 this.id
= randomString(10);
1297 var tmp
= $('<div id="' + this.id
+ '" class="box ' + classname
+ '"><div class="boxarea"></div></div>');
1298 $('#kiwi').append(tmp
);
1299 this.box
= $('#' + this.id
);
1300 this.content
= $('#' + this.id
+ ' .boxarea');
1302 this.box
.draggable({ stack
: ".box" });
1303 this.content
.click(function () {});
1304 //this.box.click(function(){ $(this)..css });
1306 Box
.prototype.create = function (name
, classname
) {
1309 Box
.prototype.id
= null;
1310 Box
.prototype.box
= null;
1311 Box
.prototype.content
= null;
1312 Box
.prototype.destroy = function () {
1315 for (name
in front
.boxes
) {
1316 if (front
.boxes
[name
].id
=== this.id
) {
1317 delete front
.boxes
[name
];
1321 Box
.prototype.height = function () {
1322 return this.box
.height();