1 /*jslint undef: true, browser: true, continue: true, sloppy: true, evil: true, forin: true, newcap: false, plusplus: true, maxerr: 50, indent: 4 */
2 /*global gateway, io, $, iScroll, agent, touchscreen*/
17 var about_info
, supportsOrientationChange
, orientationEvent
;
18 gateway
.nick
= 'kiwi_' + Math
.ceil(100 * Math
.random()) + Math
.ceil(100 * Math
.random());
19 gateway
.session_id
= null;
21 $(gateway
).bind("onmsg", front
.onMsg
);
22 $(gateway
).bind("onnotice", front
.onNotice
);
23 $(gateway
).bind("onaction", front
.onAction
);
24 $(gateway
).bind("onmotd", front
.onMOTD
);
25 $(gateway
).bind("onoptions", front
.onOptions
);
26 $(gateway
).bind("onconnect", front
.onConnect
);
27 $(gateway
).bind("onconnect_fail", front
.onConnectFail
);
28 $(gateway
).bind("ondisconnect", front
.onDisconnect
);
29 $(gateway
).bind("onnick", front
.onNick
);
30 $(gateway
).bind("onuserlist", front
.onUserList
);
31 $(gateway
).bind("onuserlist_end", front
.onUserListEnd
);
32 $(gateway
).bind("onjoin", front
.onJoin
);
33 $(gateway
).bind("ontopic", front
.onTopic
);
34 $(gateway
).bind("onpart", front
.onPart
);
35 $(gateway
).bind("onkick", front
.onKick
);
36 $(gateway
).bind("onquit", front
.onQuit
);
37 $(gateway
).bind("onwhois", front
.onWhois
);
38 $(gateway
).bind("onsync", front
.onSync
);
39 $(gateway
).bind("onchannel_redirect", front
.onChannelRedirect
);
40 $(gateway
).bind("ondebug", front
.onDebug
);
41 $(gateway
).bind("onctcp_request", front
.onCTCPRequest
);
42 $(gateway
).bind("onctcp_response", front
.onCTCPResponse
);
43 $(gateway
).bind("onirc_error", front
.onIRCError
);
47 // Build the about box
48 front
.boxes
.about
= new Box("about");
49 about_info
= 'UI adapted for ' + agent
;
51 about_info
+= ' touchscreen ';
53 about_info
+= 'usage';
54 $('#tmpl_about_box').tmpl({
56 front_revision
: front
.revision
,
57 gateway_revision
: gateway
.revision
58 }).appendTo(front
.boxes
.about
.content
);
60 //$(window).bind("beforeunload", function(){ gateway.quit(); });
63 $('#kiwi').addClass('touchscreen');
65 // Single touch scrolling through scrollback for touchscreens
67 touch_scroll
= new iScroll('windows', scroll_opts
);
72 $('#kiwi .cur_topic').resize(front
.doLayoutSize
);
74 $('#kiwi .formconnectwindow').submit(function () {
75 var netsel
= $('#kiwi .formconnectwindow .network'),
76 nick
= $('#kiwi .formconnectwindow .nick'),
79 if (nick
.val() === '') {
80 nick
.val('Nick please!');
85 tmp
= nick
.val().split(' ');
86 gateway
.nick
= tmp
[0];
89 front
.run('/connect ' + netsel
.val());
94 $('#kiwi .connectwindow').slideUp();
95 $('#windows').click(function () { $('#kiwi_msginput').focus(); });
100 supportsOrientationChange
= (typeof window
.onorientationchange
!== undefined);
101 orientationEvent
= supportsOrientationChange
? "orientationchange" : "resize";
102 window
.addEventListener(orientationEvent
, front
.doLayoutSize
, false);
103 //$('#kiwi').bind("resize", front.doLayoutSize, false);
106 //front.windowAdd('server');
107 front
.tabviewAdd('server');
109 // Any pre-defined nick?
110 if (typeof init_data
.nick
=== "string") {
111 $('#kiwi .formconnectwindow .nick').val(init_data
.nick
);
114 //gateway.session_id = 'testses';
116 $('#kiwi .cur_topic').keydown(function (e
) {
117 if (e
.which
=== 13) {
121 $('#kiwi_msginput').focus();
122 } else if (e
.which
=== 27) {
125 $(this).text(front
.original_topic
);
126 $('#kiwi_msginput').focus();
129 /*$('.cur_topic').live('keypress', function(e) {
130 if (e.keyCode === 13) {
134 $('#kiwi_msginput').focus();
135 } else if (e.keyCode === 27) {
138 $(this).text(front.original_topic);
141 $('.cur_topic').live('change', function () {
143 text
= $(this).text();
144 if (text
!== front
.original_topic
) {
145 chan
= front
.cur_channel
.name
;
146 gateway
.setTopic(chan
, text
);
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 onConnectFail: function (e
, data
) {
423 var reason
= (typeof data
.reason
=== 'string') ? data
.reason
: '';
424 front
.tabviews
.server
.addMsg(null, '', 'There\'s a problem connecting! (' + reason
+ ')', 'error');
426 onDisconnect: function (e
, data
) {
428 for (tab
in front
.tabviews
) {
429 front
.tabviews
[tab
].addMsg(null, '', 'Disconnected from server!', 'error');
432 onOptions: function (e
, data
) {
433 if (typeof gateway
.network_name
=== "string" && gateway
.network_name
!== "") {
434 front
.tabviews
.server
.tab
.text(gateway
.network_name
);
437 onMOTD: function (e
, data
) {
438 front
.tabviews
.server
.addMsg(null, data
.server
, data
.msg
, 'motd');
440 onWhois: function (e
, data
) {
443 front
.cur_channel
.addMsg(null, data
.nick
, data
.msg
, 'whois');
444 } else if (data
.logon
) {
446 d
.setTime(data
.logon
* 1000);
447 d
= d
.toLocaleString();
448 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' second' + ((data
.idle
!== 1) ? 's' : '') + ', signed on ' + d
, 'whois');
450 front
.cur_channel
.addMsg(null, data
.nick
, 'idle for ' + data
.idle
+ ' seconds', 'whois');
453 onUserList: function (e
, data
) {
455 if (front
.tabviews
[data
.channel
.toLowerCase()] === undefined) {
458 ul
= front
.tabviews
[data
.channel
.toLowerCase()].userlist
;
460 if (!document
.userlist_updating
) {
461 document
.userlist_updating
= true;
465 $.each(data
.users
, function (i
, item
) {
466 nick
= i
; //i.match(/^.+!/g);
468 $('<li><a class="nick" onclick="front.userClick(this);">' + mode
+ nick
+ '</a></li>').appendTo(ul
);
471 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
473 onUserListEnd: function (e
, data
) {
474 document
.userlist_updating
= false;
477 onJoin: function (e
, data
) {
478 if (!front
.tabviewExists(data
.channel
)) {
479 front
.tabviewAdd(data
.channel
.toLowerCase());
482 if (data
.nick
=== gateway
.nick
) {
483 return; // Not needed as it's already in nicklist
485 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '--> ' + data
.nick
+ ' has joined', 'action', 'color:#009900;');
486 $('<li><a class="nick" onclick="front.userClick(this);">' + data
.nick
+ '</a></li>').appendTo(front
.tabviews
[data
.channel
.toLowerCase()].userlist
);
487 front
.tabviews
[data
.channel
.toLowerCase()].userlistSort();
489 onPart: function (e
, data
) {
490 if (front
.tabviewExists(data
.channel
)) {
491 // If this is us, close the tabview
492 if (data
.nick
=== gateway
.nick
) {
493 front
.tabviews
[data
.channel
.toLowerCase()].close();
494 front
.tabviews
.server
.show();
498 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.nick
+ ' has left (' + data
.message
+ ')', 'action', 'color:#990000;');
499 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
500 if ($(this).text() === data
.nick
) {
506 onKick: function (e
, data
) {
507 if (front
.tabviewExists(data
.channel
)) {
508 // If this is us, close the tabview
509 if (data
.kicked
=== gateway
.nick
) {
510 //front.tabviews[data.channel.toLowerCase()].close();
511 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data
.channel
+ '. ' + data
.message
, 'status');
512 front
.tabviews
[data
.channel
.toLowerCase()].safe_to_close
= true;
513 $('li', front
.tabviews
[data
.channel
.toLowerCase()].userlist
).remove();
517 front
.tabviews
[data
.channel
.toLowerCase()].addMsg(null, ' ', '<-- ' + data
.kicked
+ ' kicked by ' + data
.nick
+ '(' + data
.message
+ ')', 'action', 'color:#990000;');
518 front
.tabviews
[data
.channel
.toLowerCase()].userlist
.children().each(function () {
519 if ($(this).text() === data
.nick
) {
525 onNick: function (e
, data
) {
526 if (data
.nick
=== gateway
.nick
) {
527 gateway
.nick
= data
.newnick
;
531 $.each(front
.tabviews
, function (i
, item
) {
532 $.each(front
.tabviews
, function (i
, item
) {
533 item
.changeNick(data
.newnick
, data
.nick
);
537 onQuit: function (e
, data
) {
538 $.each(front
.tabviews
, function (i
, item
) {
539 $.each(front
.tabviews
, function (i
, item
) {
540 item
.userlist
.children().each(function () {
541 if ($(this).text() === data
.nick
) {
543 item
.addMsg(null, ' ', '<-- ' + data
.nick
+ ' has quit (' + data
.message
+ ')', 'action', 'color:#990000;');
549 onChannelRedirect: function (e
, data
) {
550 front
.tabviews
[data
.from.toLowerCase()].close();
551 front
.tabviewAdd(data
.to
.toLowerCase());
552 front
.tabviews
[data
.to
.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data
.from, 'action');
555 onIRCError: function (e
, data
) {
557 if (data
.channel
!== undefined && front
.tabviewExists(data
.channel
)) {
558 t_view
= data
.channel
;
563 switch (data
.error
) {
564 case 'banned_from_channel':
565 front
.tabviews
[t_view
].addMsg(null, ' ', '=== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
566 if (t_view
!== 'server') {
567 front
.tabviews
[t_view
].safe_to_close
= true;
570 case 'bad_channel_key':
571 front
.tabviews
[t_view
].addMsg(null, ' ', '=== Bad channel key for ' + data
.channel
, 'status');
572 if (t_view
!== 'server') {
573 front
.tabviews
[t_view
].safe_to_close
= true;
576 case 'invite_only_channel':
577 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is invite only.', 'status');
578 if (t_view
!== 'server') {
579 front
.tabviews
[t_view
].safe_to_close
= true;
582 case 'channel_is_full':
583 front
.tabviews
[t_view
].addMsg(null, ' ', '=== ' + data
.channel
+ ' is full.', 'status');
584 if (t_view
!== 'server') {
585 front
.tabviews
[t_view
].safe_to_close
= true;
588 case 'chanop_privs_needed':
589 front
.tabviews
[data
.channel
].addMsg(null, ' ', '=== ' + data
.reason
, 'status');
592 front
.tabviews
.server
.addMsg(null, ' ', '=== ' + data
.nick
+ ': ' + data
.reason
, 'status');
595 // We don't know what data contains, so don't do anything with it.
596 //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
600 registerKeys: function () {
601 $('#kiwi_msginput').bind('keydown', function (e
) {
602 var windows
, meta
, num
, msg
, data
, candidates
, word_pos
, word
;
603 windows
= $('#windows');
604 //var meta = e.altKey;
608 case (e
.which
>= 48) && (e
.which
<= 57):
615 front
.windowsShow(num
);
619 case e
.which
=== 27: // escape
621 case e
.which
=== 13: // return
622 msg
= $('#kiwi_msginput').val();
625 front
.buffer
.push(msg
);
626 front
.buffer_pos
= front
.buffer
.length
;
629 $('#kiwi_msginput').val('');
632 case e
.which
=== 33: // page up
633 console
.log("page up");
634 windows
[0].scrollTop
= windows
[0].scrollTop
- windows
.height();
636 case e
.which
=== 34: // page down
637 windows
[0].scrollTop
= windows
[0].scrollTop
+ windows
.height();
639 case e
.which
=== 37: // left
641 front
.windowsPrevious();
645 case e
.which
=== 38: // up
646 if (front
.buffer_pos
> 0) {
648 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
651 case e
.which
=== 39: // right
657 case e
.which
=== 40: // down
658 if (front
.buffer_pos
< front
.buffer
.length
) {
660 $('#kiwi_msginput').val(front
.buffer
[front
.buffer_pos
]);
664 case e
.which
=== 9: // tab
665 // Get possible autocompletions
667 front
.cur_channel
.userlist
.children().each(function () {
668 nick
= front
.nickStripPrefix($('a.nick', this).text());
672 // Do the autocomplete
673 if (this.value
.length
=== this.selectionStart
&& this.value
.length
=== this.selectionEnd
) {
676 word_pos
= this.value
.lastIndexOf(' ');
678 if (word_pos
=== -1) {
681 word
= this.value
.substr(word_pos
);
685 // filter data to find only strings that start with existing value
686 for (i
= 0; i
< data
.length
; i
++) {
687 if (data
[i
].indexOf(word
) === 0 && data
[i
].length
> word
.length
) {
688 candidates
.push(data
[i
]);
692 if (candidates
.length
> 0) {
693 // some candidates for autocompletion are found
694 this.value
= this.value
.substring(0, word_pos
) + ' ' + candidates
[0] + ': ';
695 this.selectionStart
= this.value
.length
;
703 $('#kiwi .control .msginput .nick').click(function () {
704 front
.showChangeNick();
711 $('#kiwi .plugins .load_plugin_file').click(function () {
713 if (typeof front
.boxes
.plugins
!== "undefined") {
717 front
.boxes
.plugins
= new Box("plugin_file");
718 $('#tmpl_plugins').tmpl({}).appendTo(front
.boxes
.plugins
.content
);
719 front
.boxes
.plugins
.box
.css('top', -(front
.boxes
.plugins
.height
+ 40));
721 // Populate the plugin list..
722 lst
= $('#plugin_list');
723 lst
.find('option').remove();
724 for (j
in plugins
.privmsg
) {
725 txt
= plugins
.privmsg
[j
].name
;
726 lst
.append('<option value="' + txt
+ '">' + txt
+ '</option>');
730 $('#kiwi .plugin_file').submit(function () {
731 $.getJSON($('.txtpluginfile').val(), function (data
) {
733 plg
.name
= data
.name
;
734 eval("plg.onprivmsg = " + data
.onprivmsg
);
735 eval("plg.onload = " + data
.onload
);
736 eval("plg.onunload = " + data
.onunload
);
737 plugins
.privmsg
.push(plg
);
739 if (plg
.onload
instanceof Function
) {
745 $('#kiwi .cancelpluginfile').click(function () {
746 front
.boxes
.plugins
.destroy();
749 $('#kiwi #plugins_list_unload').click(function () {
750 var selected_plugin
, i
;
751 selected_plugin
= $('#plugin_list').val();
752 console
.log("removing plugin: " + selected_plugin
);
753 for (i
in plugins
.privmsg
) {
754 if (plugins
.privmsg
[i
].name
=== selected_plugin
) {
755 if (plugins
.privmsg
[i
].onunload
instanceof Function
) {
756 plugins
.privmsg
[i
].onunload();
758 delete plugins
.privmsg
[i
];
763 $('#kiwi .txtpluginfile').focus();
767 $('#kiwi .plugins .reload_css').click(function () {
768 var links
= document
.getElementsByTagName("link"),
770 for (i
= 0; i
< links
.length
; i
++) {
771 if (links
[i
].rel
=== "stylesheet") {
772 if (links
[i
].href
.indexOf("?") === -1) {
773 links
[i
].href
+= "?";
775 links
[i
].href
+= "x";
781 $('#kiwi .about .about_close').click(function () {
782 $('#kiwi .about').css('display', 'none');
786 $('#kiwi .poweredby').click(function () {
787 $('#kiwi .about').css('display', 'block');
793 showChangeNick: function () {
794 $('#kiwi').append($('#tmpl_change_nick').tmpl({}));
796 $('#kiwi .form_newnick').submit(function () {
797 front
.run('/NICK ' + $('#kiwi .txtnewnick').val());
798 $('#kiwi .newnick').remove();
802 $('#kiwi .txtnewnick').keypress(function (ev
) {
803 if (!this.first_press
) {
804 this.first_press
= true;
809 $('#kiwi .txtnewnick').keydown(function (ev
) {
810 if (ev
.which
=== 27) { // ESC
811 $('#kiwi_msginput').focus();
812 $('#kiwi .newnick').remove();
816 $('#kiwi .cancelnewnick').click(function () {
817 $('#kiwi .newnick').remove();
820 $('#kiwi .txtnewnick').focus();
824 tabviewExists: function (name
) {
825 return (typeof front
.tabviews
[name
.toLowerCase()] !== 'undefined');
828 tabviewAdd: function (v_name
) {
829 var re
, htmlsafe_name
, tmp_divname
, tmp_userlistname
, tmp_tabname
;
830 if (v_name
.charAt(0) === gateway
.channel_prefix
) {
831 re
= new RegExp(gateway
.channel_prefix
, "g");
832 htmlsafe_name
= v_name
.replace(re
, 'pre');
833 htmlsafe_name
= "chan_" + htmlsafe_name
;
835 htmlsafe_name
= 'query_' + v_name
;
838 tmp_divname
= 'kiwi_window_' + htmlsafe_name
;
839 tmp_userlistname
= 'kiwi_userlist_' + htmlsafe_name
;
840 tmp_tabname
= 'kiwi_tab_' + htmlsafe_name
;
842 if (!front
.tabviewExists(v_name
)) {
843 $('#kiwi .windows .scroller').append('<div id="' + tmp_divname
+ '" class="messages"></div>');
844 $('#kiwi .userlist').append('<ul id="' + tmp_userlistname
+ '"></ul>');
845 $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname
+ '" onclick="front.tabviews[\'' + v_name
.toLowerCase() + '\'].show();">' + v_name
+ '</li>');
847 //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); });
848 //front.windowShow(v_name);
850 front
.tabviews
[v_name
.toLowerCase()] = new Tabview();
851 front
.tabviews
[v_name
.toLowerCase()].name
= v_name
;
852 front
.tabviews
[v_name
.toLowerCase()].div
= $('#' + tmp_divname
);
853 front
.tabviews
[v_name
.toLowerCase()].userlist
= $('#' + tmp_userlistname
);
854 front
.tabviews
[v_name
.toLowerCase()].tab
= $('#' + tmp_tabname
);
855 front
.tabviews
[v_name
.toLowerCase()].show();
857 if (typeof registerTouches
=== "function") {
858 //alert("Registering touch interface");
859 //registerTouches($('#'+tmp_divname));
860 registerTouches(document
.getElementById(tmp_divname
));
863 front.tabviews[v_name.toLowerCase()].userlist.click(function(){
864 alert($(this).attr('id'));
868 front
.doLayoutSize();
872 userClick: function (item
) {
873 // Remove any existing userboxes
874 $('#kiwi .userbox').remove();
876 var li
= $(item
).parent();
877 /*var html = '<div class="userbox">\
878 <input type="hidden" class="userbox_nick" value="' + front.nickStripPrefix($(item).text()) + '" />\
879 <a href="#" class="userbox_query">Message</a>\
880 <a href="#" class="userbox_whois">Info</a>\
883 $('#tmpl_user_box').tmpl({nick
: front
.nickStripPrefix($(item
).text())}).appendTo(li
);
885 $('#kiwi .userbox .userbox_query').click(function (ev
) {
886 var nick
= $('#kiwi .userbox_nick').val();
887 front
.run('/query ' + nick
);
890 $('#kiwi .userbox .userbox_whois').click(function (ev
) {
891 var nick
= $('#kiwi .userbox_nick').val();
892 front
.run('/whois ' + nick
);
901 onSync: function (e
, data
) {
903 if (data
.nick
!== undefined) {
904 gateway
.nick
= data
.nick
;
908 if (data
.tabviews
!== undefined) {
909 $.each(data
.tabviews
, function (i
, tab
) {
910 if (!front
.tabviewExists(tab
.name
)) {
911 front
.tabviewAdd(gateway
.channel_prefix
+ tab
.name
);
913 if (tab
.userlist
!== undefined) {
914 front
.onUserList({'channel': gateway
.channel_prefix
+ tab
.name
, 'users': tab
.userlist
});
924 setTopicText: function (new_topic
) {
925 front
.original_topic
= new_topic
;
926 $('#kiwi .cur_topic .topic').text(new_topic
);
927 front
.doLayoutSize();
936 nickStripPrefix: function (nick
) {
939 prefix
= tmp
.charAt(0);
940 for (i
in gateway
.user_prefixes
) {
941 if (gateway
.user_prefixes
[i
].symbol
!== prefix
) {
944 return tmp
.substring(1);
950 nickGetPrefix: function (nick
) {
953 prefix
= tmp
.charAt(0);
954 for (i
in gateway
.user_prefixes
) {
955 if (gateway
.user_prefixes
[i
].symbol
=== prefix
) {
963 isChannel: function (name
) {
964 prefix
= name
.charAt(0);
965 if (gateway
.channel_prefix
.indexOf(prefix
) > -1) {
974 tabviewsNext: function () {
975 var wl
= $('#kiwi .windowlist ul'),
976 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) + 170;
977 wl
.css('text-indent', next_left
);
980 tabviewsPrevious: function () {
981 var wl
= $('#kiwi .windowlist ul'),
982 next_left
= parseInt(wl
.css('text-indent').replace('px', ''), 10) - 170;
983 wl
.css('text-indent', next_left
);
986 windowsNext: function () {
989 for (tab
in front
.tabviews
) {
991 if (front
.tabviews
[tab
] === front
.cur_channel
) {
996 front
.tabviews
[tab
].show();
1002 windowsPrevious: function () {
1003 var tab
, prev_tab
, next
;
1005 for (tab
in front
.tabviews
) {
1006 if (front
.tabviews
[tab
] === front
.cur_channel
) {
1012 prev_tab
= front
.tabviews
[tab
];
1016 windowsShow: function (num
) {
1017 num
= parseInt(num
, 10);
1018 console
.log('Showing window ' + num
.toString());
1020 for (tab
in front
.tabviews
) {
1022 front
.tabviews
[tab
].show();
1053 var Tabview = function () {};
1054 Tabview
.prototype.name
= null;
1055 Tabview
.prototype.div
= null;
1056 Tabview
.prototype.userlist
= null;
1057 Tabview
.prototype.tab
= null;
1058 Tabview
.prototype.topic
= "";
1059 Tabview
.prototype.safe_to_close
= false; // If we have been kicked/banned/etc from this channel, don't wait for a part message
1061 Tabview
.prototype.show = function () {
1062 $('#kiwi .messages').removeClass("active");
1063 $('#kiwi .userlist ul').removeClass("active");
1064 $('#kiwi .windowlist ul li').removeClass("active");
1066 // Activate this tab!
1067 this.div
.addClass('active');
1068 this.userlist
.addClass('active');
1069 this.tab
.addClass('active');
1071 document
.tmp
= this.div
;
1072 // Add the part image to the tab
1073 this.addPartImage();
1075 this.clearHighlight();
1076 front
.setTopicText(this.topic
);
1077 front
.cur_channel
= this;
1079 // If we're using fancy scrolling, refresh it
1081 touch_scroll
.refresh();
1084 this.scrollBottom();
1086 $('#kiwi_msginput').focus();
1090 Tabview
.prototype.close = function () {
1092 this.userlist
.remove();
1095 if (front
.cur_channel
=== this) {
1096 front
.tabviews
.server
.show();
1098 delete front
.tabviews
[this.name
.toLowerCase()];
1101 Tabview
.prototype.addPartImage = function () {
1102 this.clearPartImage();
1104 // We can't close this tab, so don't have the close image
1105 if (this.name
=== 'server') {
1109 var del_html
= '<img src="img/redcross.png" class="tab_part" />';
1110 this.tab
.append(del_html
);
1112 $('.tab_part', this.tab
).click(function () {
1113 if (front
.isChannel($(this).parent().text())) {
1116 // Make sure we don't close the server tab
1117 if (front
.cur_channel
.name
!== 'server') {
1118 front
.cur_channel
.close();
1124 Tabview
.prototype.clearPartImage = function () {
1125 $('#kiwi .windowlist .tab_part').remove();
1128 Tabview
.prototype.addMsg = function (time
, nick
, msg
, type
, style
) {
1129 var html_nick
, self
, tmp
, plugin_ret
, i
, d
, re
, line_msg
;
1130 html_nick
= $('<div/>').text(nick
).html();
1136 for (i
in plugins
.privmsg
) {
1137 if ((plugins
.privmsg
[i
].onprivmsg
instanceof Function
)) {
1140 plugin_ret
= plugins
.privmsg
[i
].onprivmsg(tmp
, this.name
);
1142 // If this plugin has returned false, do not add this message
1143 if (plugin_ret
=== false) {
1149 // If we actually have a string from the plugin, use it
1150 if (typeof plugin_ret
=== "string") {
1157 //var html_msg = $('<div/>').text(msg).html()+' '; // Add the space so the styling always has at least 1 character to go from
1158 if (time
=== null) {
1160 time
= d
.getHours().toString().lpad(2, "0") + ":" + d
.getMinutes().toString().lpad(2, "0") + ":" + d
.getSeconds().toString().lpad(2, "0");
1163 // The CSS class (action, topic, notice, etc)
1164 if (typeof type
!== "string") {
1168 // Make sure we don't have NaN or something
1169 if (typeof msg
!== "string") {
1175 if (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1177 while (msg
.indexOf(String
.fromCharCode(2)) !== -1) {
1178 msg
= msg
.replace(String
.fromCharCode(2), next
);
1179 next
= (next
=== '<b>') ? '</b>' : '<b>';
1181 if (next
=== '</b>') {
1186 // Wierd thing noticed by Dux0r on the irc.esper.net server
1187 if (typeof msg
!== "string") {
1192 if (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1194 while (msg
.indexOf(String
.fromCharCode(31)) !== -1) {
1195 msg
= msg
.replace(String
.fromCharCode(31), next
);
1196 next
= (next
=== '<u>') ? '</u>' : '<u>';
1198 if (next
=== '</u>') {
1203 re
= new RegExp('\\B(' + gateway
.channel_prefix
+ '[^ ,.\\007]+)', 'g');
1205 msg
= msg
.replace(re
, function (match
) {
1206 return '<a class="chan" href="#">' + match
+ '</a>';
1209 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>');
1210 this.div
.append(line_msg
);
1213 this.scrollBottom();
1215 touch_scroll
.refresh();
1216 //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height());
1217 this.scrollBottom();
1218 //if(this.div.attr("scrollHeight") > $('#windows').height()){
1219 // touch_scroll.scrollTo(0, this.div.height());
1224 Tabview
.prototype.scrollBottom = function () {
1225 var w
= $('#windows');
1226 w
[0].scrollTop
= w
[0].scrollHeight
;
1229 Tabview
.prototype.changeNick = function (newNick
, oldNick
) {
1230 this.userlist
.children().each(function () {
1231 var item
= $('a.nick', this);
1232 if (front
.nickStripPrefix(item
.text()) === oldNick
) {
1233 item
.text(front
.nickGetPrefix(item
.text()) + newNick
);
1234 document
.temp_chan
= 1;
1238 if (typeof document
.temp_chan
!== "undefined") {
1239 this.addMsg(null, ' ', '=== ' + oldNick
+ ' is now known as ' + newNick
, 'action');
1240 delete document
.temp_chan
;
1241 this.userlistSort();
1245 Tabview
.prototype.userlistSort = function () {
1246 var ul
= this.userlist
,
1247 listitems
= ul
.children('li').get();
1248 listitems
.sort(function (a
, b
) {
1249 var compA
= $(a
).text().toUpperCase(),
1250 compB
= $(b
).text().toUpperCase(),
1253 // Sort by prefixes first
1254 for (i
in gateway
.user_prefixes
) {
1255 prefix
= gateway
.user_prefixes
[i
].symbol
;
1257 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) === prefix
) {
1258 // Both have the same prefix, string compare time
1262 if (compA
.charAt(0) === prefix
&& compB
.charAt(0) !== prefix
) {
1266 if (compA
.charAt(0) !== prefix
&& compB
.charAt(0) === prefix
) {
1271 // No prefixes, compare by string
1272 return (compA
< compB
) ? -1 : (compA
> compB
) ? 1 : 0;
1274 $.each(listitems
, function (idx
, itm
) { ul
.append(itm
); });
1277 Tabview
.prototype.highlight = function () {
1278 this.tab
.addClass('highlight');
1280 Tabview
.prototype.activity = function () {
1281 this.tab
.addClass('activity');
1283 Tabview
.prototype.clearHighlight = function () {
1284 this.tab
.removeClass('highlight');
1285 this.tab
.removeClass('activity');
1287 Tabview
.prototype.changeTopic = function (new_topic
) {
1288 this.topic
= new_topic
;
1289 this.addMsg(null, ' ', '=== Topic for ' + this.name
+ ' is: ' + new_topic
, 'topic');
1290 if (front
.cur_channel
.name
=== this.name
) {
1291 front
.setTopicText(new_topic
);
1299 var Box = function (classname
) {
1300 this.id
= randomString(10);
1301 var tmp
= $('<div id="' + this.id
+ '" class="box ' + classname
+ '"><div class="boxarea"></div></div>');
1302 $('#kiwi').append(tmp
);
1303 this.box
= $('#' + this.id
);
1304 this.content
= $('#' + this.id
+ ' .boxarea');
1306 this.box
.draggable({ stack
: ".box" });
1307 this.content
.click(function () {});
1308 //this.box.click(function(){ $(this)..css });
1310 Box
.prototype.create = function (name
, classname
) {
1313 Box
.prototype.id
= null;
1314 Box
.prototype.box
= null;
1315 Box
.prototype.content
= null;
1316 Box
.prototype.destroy = function () {
1319 for (name
in front
.boxes
) {
1320 if (front
.boxes
[name
].id
=== this.id
) {
1321 delete front
.boxes
[name
];
1325 Box
.prototype.height = function () {
1326 return this.box
.height();