1 /*jslint white:true, regexp: true, nomen: true, devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */
4 _kiwi
.view
.MemberList
= Backbone
.View
.extend({
7 "click .nick": "nickClick"
9 initialize: function (options
) {
10 this.model
.bind('all', this.render
, this);
11 $(this.el
).appendTo('#memberlists');
14 var $this = $(this.el
);
16 this.model
.forEach(function (member
) {
17 var prefix_css_class
= (member
.get('modes') || []).join(' ');
18 $('<li class="mode ' + prefix_css_class
+ '"><a class="nick"><span class="prefix">' + member
.get("prefix") + '</span>' + member
.get("nick") + '</a></li>')
20 .data('member', member
);
23 nickClick: function (x
) {
24 var $target
= $(x
.currentTarget
).parent('li'),
25 member
= $target
.data('member'),
28 // If the userbox already exists here, hide it
29 if ($target
.find('.userbox').length
> 0) {
30 $('.userbox', this.$el
).remove();
34 userbox
= new _kiwi
.view
.UserBox();
35 userbox
.member
= member
;
37 // Remove any existing userboxes
38 $('.userbox', this.$el
).remove();
40 $target
.append(userbox
.$el
);
43 $('#memberlists').children().removeClass('active');
44 $(this.el
).addClass('active');
50 _kiwi
.view
.UserBox
= Backbone
.View
.extend({
52 'click .query': 'queryClick',
53 'click .info': 'infoClick',
54 'click .slap': 'slapClick'
57 initialize: function () {
58 this.$el
= $($('#tmpl_userbox').html());
61 queryClick: function (event
) {
62 var panel
= new _kiwi
.model
.Query({name
: this.member
.get('nick')});
63 _kiwi
.app
.panels
.add(panel
);
67 infoClick: function (event
) {
68 _kiwi
.app
.controlbox
.processInput('/whois ' + this.member
.get('nick'));
71 slapClick: function (event
) {
72 _kiwi
.app
.controlbox
.processInput('/slap ' + this.member
.get('nick'));
76 _kiwi
.view
.NickChangeBox
= Backbone
.View
.extend({
78 'submit': 'changeNick',
79 'click .cancel': 'close'
82 initialize: function () {
83 this.$el
= $($('#tmpl_nickchange').html());
87 // Add the UI component and give it focus
88 _kiwi
.app
.controlbox
.$el
.prepend(this.$el
);
89 this.$el
.find('input').focus();
91 this.$el
.css('bottom', _kiwi
.app
.controlbox
.$el
.outerHeight(true));
99 changeNick: function (event
) {
101 _kiwi
.gateway
.changeNick(this.$el
.find('input').val(), function (err
, val
) {
108 _kiwi
.view
.ServerSelect = function () {
109 // Are currently showing all the controlls or just a nick_change box?
112 var model
= Backbone
.View
.extend({
114 'submit form': 'submitForm',
115 'click .show_more': 'showMore',
116 'change .have_pass input': 'showPass'
119 initialize: function () {
122 this.$el
= $($('#tmpl_server_select').html());
124 // Remove the 'more' link if the server has disabled server changing
125 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
126 if (!_kiwi
.app
.server_settings
.connection
.allow_change
) {
127 this.$el
.find('.show_more').remove();
128 this.$el
.addClass('single_server');
133 _kiwi
.gateway
.bind('onconnect', this.networkConnected
, this);
134 _kiwi
.gateway
.bind('connecting', this.networkConnecting
, this);
136 _kiwi
.gateway
.bind('onirc_error', function (data
) {
137 $('button', this.$el
).attr('disabled', null);
139 if (data
.error
== 'nickname_in_use') {
140 this.setStatus('Nickname already taken');
141 this.show('nick_change');
144 if (data
.error
== 'password_mismatch') {
145 this.setStatus('Incorrect Password');
146 this.show('nick_change');
147 that
.$el
.find('.password').select();
152 submitForm: function (event
) {
153 if (state
=== 'nick_change') {
154 this.submitNickChange(event
);
156 this.submitLogin(event
);
159 $('button', this.$el
).attr('disabled', 1);
163 submitLogin: function (event
) {
164 // If submitting is disabled, don't do anything
165 if ($('button', this.$el
).attr('disabled')) return;
168 nick
: $('input.nick', this.$el
).val(),
169 server
: $('input.server', this.$el
).val(),
170 port
: $('input.port', this.$el
).val(),
171 ssl
: $('input.ssl', this.$el
).prop('checked'),
172 password
: $('input.password', this.$el
).val(),
173 channel
: $('input.channel', this.$el
).val(),
174 channel_key
: $('input.channel_key', this.$el
).val()
177 this.trigger('server_connect', values
);
180 submitNickChange: function (event
) {
181 _kiwi
.gateway
.changeNick($('input.nick', this.$el
).val());
182 this.networkConnecting();
185 showPass: function (event
) {
186 if (this.$el
.find('tr.have_pass input').is(':checked')) {
187 this.$el
.find('tr.pass').show().find('input').focus();
189 this.$el
.find('tr.pass').hide().find('input').val('');
193 showMore: function (event
) {
194 $('.more', this.$el
).slideDown('fast');
195 $('input.server', this.$el
).select();
198 populateFields: function (defaults
) {
199 var nick
, server
, port
, channel
, channel_key
, ssl
, password
;
201 defaults
= defaults
|| {};
203 nick
= defaults
.nick
|| '';
204 server
= defaults
.server
|| '';
205 port
= defaults
.port
|| 6667;
206 ssl
= defaults
.ssl
|| 0;
207 password
= defaults
.password
|| '';
208 channel
= defaults
.channel
|| '';
209 channel_key
= defaults
.channel_key
|| '';
211 $('input.nick', this.$el
).val(nick
);
212 $('input.server', this.$el
).val(server
);
213 $('input.port', this.$el
).val(port
);
214 $('input.ssl', this.$el
).prop('checked', ssl
);
215 $('input.password', this.$el
).val(password
);
216 $('input.channel', this.$el
).val(channel
);
217 $('input.channel_key', this.$el
).val(channel_key
);
224 show: function (new_state
) {
225 new_state
= new_state
|| 'all';
229 if (new_state
=== 'all') {
230 $('.show_more', this.$el
).show();
232 } else if (new_state
=== 'more') {
233 $('.more', this.$el
).slideDown('fast');
235 } else if (new_state
=== 'nick_change') {
236 $('.more', this.$el
).hide();
237 $('.show_more', this.$el
).hide();
238 $('input.nick', this.$el
).select();
244 setStatus: function (text
, class_name
) {
245 $('.status', this.$el
)
247 .attr('class', 'status')
248 .addClass(class_name
)
251 clearStatus: function () {
252 $('.status', this.$el
).hide();
255 networkConnected: function (event
) {
256 this.setStatus('Connected :)', 'ok');
257 $('form', this.$el
).hide();
260 networkConnecting: function (event
) {
261 this.setStatus('Connecting..', 'ok');
264 showError: function (event
) {
265 this.setStatus('Error connecting', 'error');
266 $('button', this.$el
).attr('disabled', null);
272 return new model(arguments
);
276 _kiwi
.view
.Panel
= Backbone
.View
.extend({
278 className
: "messages",
280 "click .chan": "chanClick",
281 'click .media .open': 'mediaClick',
282 'mouseenter .msg .nick': 'msgEnter',
283 'mouseleave .msg .nick': 'msgLeave'
286 initialize: function (options
) {
287 this.initializePanel(options
);
290 initializePanel: function (options
) {
291 this.$el
.css('display', 'none');
292 options
= options
|| {};
294 // Containing element for this panel
295 if (options
.container
) {
296 this.$container
= $(options
.container
);
298 this.$container
= $('#panels .container1');
301 this.$el
.appendTo(this.$container
);
303 this.alert_level
= 0;
305 this.model
.bind('msg', this.newMsg
, this);
308 this.model
.set({"view": this}, {"silent": true});
311 render: function () {
315 _
.each(this.model
.get('scrollback'), function (msg
) {
320 newMsg: function (msg
) {
321 var re
, line_msg
, $this = this.$el
,
322 nick_colour_hex
, nick_hex
, is_highlight
, msg_css_classes
= '';
324 // Nick highlight detecting
325 if ((new RegExp('\\b' + _kiwi
.gateway
.get('nick') + '\\b', 'i')).test(msg
.msg
)) {
327 msg_css_classes
+= ' highlight';
330 // Escape any HTML that may be in here
331 msg
.msg
= $('<div />').text(msg
.msg
).html();
333 // Make the channels clickable
334 re
= new RegExp('(?:^|\\s)([' + _kiwi
.gateway
.get('channel_prefix') + '][^ ,.\\007]+)', 'g');
335 msg
.msg
= msg
.msg
.replace(re
, function (match
) {
336 return '<a class="chan" data-channel="' + match
.trim() + '">' + match
+ '</a>';
340 // Parse any links found
341 msg
.msg
= msg
.msg
.replace(/(([A-Za-z0-9\-]+\:\/\/)|(www\.))([\w.\-]+)([a-zA-Z]{2,6})(:[0-9]+)?(\/[\w#!:.?$'()[\]*,;~+=&%@!\-\/]*)?/gi, function (url
) {
345 // Add the http if no protoocol was found
346 if (url
.match(/^www\./)) {
347 url
= 'http://' + url
;
350 // Shorten the displayed URL if it's going to be too long
351 if (nice
.length
> 100) {
352 nice
= nice
.substr(0, 100) + '...';
355 // Get any media HTML if supported
356 extra_html
= _kiwi
.view
.MediaMessage
.buildHtml(url
);
358 // Make the link clickable
359 return '<a class="link_ext" target="_blank" rel="nofollow" href="' + url
+ '">' + nice
+ '</a> ' + extra_html
;
363 // Convert IRC formatting into HTML formatting
364 msg
.msg
= formatIRCMsg(msg
.msg
);
367 // Add some colours to the nick (Method based on IRSSIs nickcolor.pl)
368 nick_colour_hex
= (function (nick
) {
369 var nick_int
= 0, rgb
;
371 _
.map(nick
.split(''), function (i
) { nick_int
+= i
.charCodeAt(0); });
372 rgb
= hsl2rgb(nick_int
% 255, 70, 35);
373 rgb
= rgb
[2] | (rgb
[1] << 8) | (rgb
[0] << 16);
375 return '#' + rgb
.toString(16);
378 msg
.nick_style
= 'color:' + nick_colour_hex
+ ';';
380 // Generate a hex string from the nick to be used as a CSS class name
381 nick_hex
= msg
.nick_css_class
= '';
383 _
.map(msg
.nick
.split(''), function (char) {
384 nick_hex
+= char.charCodeAt(0).toString(16);
386 msg_css_classes
+= ' nick_' + nick_hex
;
389 // Build up and add the line
390 msg
.msg_css_classes
= msg_css_classes
;
391 line_msg
= '<div class="msg <%= type %> <%= msg_css_classes %>"><div class="time"><%- time %></div><div class="nick" style="<%= nick_style %>"><%- nick %></div><div class="text" style="<%= style %>"><%= msg %> </div></div>';
392 $this.append(_
.template(line_msg
, msg
));
394 // Activity/alerts based on the type of new message
395 if (msg
.type
.match(/^action /)) {
396 this.alert('action');
398 } else if (is_highlight
) {
399 _kiwi
.app
.view
.alertWindow('* People are talking!');
400 this.alert('highlight');
403 // If this is the active panel, send an alert out
404 if (this.model
.isActive()) {
405 _kiwi
.app
.view
.alertWindow('* People are talking!');
407 this.alert('activity');
410 // Update the activity counters
412 // Only inrement the counters if we're not the active panel
413 if (this.model
.isActive()) return;
415 var $act
= this.model
.tab
.find('.activity');
416 $act
.text((parseInt($act
.text(), 10) || 0) + 1);
417 if ($act
.text() === '0') {
418 $act
.addClass('zero');
420 $act
.removeClass('zero');
424 this.scrollToBottom();
426 // Make sure our DOM isn't getting too large (Acts as scrollback)
428 if (this.msg_count
> (parseInt(_kiwi
.global
.settings
.get('scrollback'), 10) || 250)) {
429 $('.msg:first', this.$el
).remove();
433 chanClick: function (event
) {
435 _kiwi
.gateway
.join($(event
.target
).data('channel'));
438 _kiwi
.gateway
.join($(event
.srcElement
).data('channel'));
442 mediaClick: function (event
) {
443 var $media
= $(event
.target
).parents('.media');
446 if ($media
.data('media')) {
447 media_message
= $media
.data('media');
449 media_message
= new _kiwi
.view
.MediaMessage({el
: $media
[0]});
450 $media
.data('media', media_message
);
453 $media
.data('media', media_message
);
455 media_message
.open();
458 // Cursor hovers over a message
459 msgEnter: function (event
) {
462 // Find a valid class that this element has
463 _
.each($(event
.currentTarget
).parent('.msg').attr('class').split(' '), function (css_class
) {
464 if (css_class
.match(/^nick_[a-z0-9]+/i)) {
465 nick_class
= css_class
;
469 // If no class was found..
470 if (!nick_class
) return;
472 $('.'+nick_class
).addClass('global_nick_highlight');
475 // Cursor leaves message
476 msgLeave: function (event
) {
479 // Find a valid class that this element has
480 _
.each($(event
.currentTarget
).parent('.msg').attr('class').split(' '), function (css_class
) {
481 if (css_class
.match(/^nick_[a-z0-9]+/i)) {
482 nick_class
= css_class
;
486 // If no class was found..
487 if (!nick_class
) return;
489 $('.'+nick_class
).removeClass('global_nick_highlight');
493 var $this = this.$el
;
495 // Hide all other panels and show this one
496 this.$container
.children().css('display', 'none');
497 $this.css('display', 'block');
499 // Show this panels memberlist
500 var members
= this.model
.get("members");
502 $('#memberlists').removeClass('disabled');
505 // Memberlist not found for this panel, hide any active ones
506 $('#memberlists').addClass('disabled').children().removeClass('active');
509 _kiwi
.app
.view
.doLayout();
511 // Remove any alerts and activity counters for this panel
513 this.model
.tab
.find('.activity').text('0').addClass('zero');
515 this.trigger('active', this.model
);
516 _kiwi
.app
.panels
.trigger('active', this.model
, _kiwi
.app
.panels
.active
);
518 this.scrollToBottom(true);
522 alert: function (level
) {
523 // No need to highlight if this si the active panel
524 if (this.model
== _kiwi
.app
.panels
.active
) return;
527 types
= ['none', 'action', 'activity', 'highlight'];
529 // Default alert level
530 level
= level
|| 'none';
532 // If this alert level does not exist, assume clearing current level
533 type_idx
= _
.indexOf(types
, level
);
539 // Only 'upgrade' the alert. Never down (unless clearing)
540 if (type_idx
!== 0 && type_idx
<= this.alert_level
) {
544 // Clear any existing levels
545 this.model
.tab
.removeClass(function (i
, css
) {
546 return (css
.match(/\balert_\S+/g) || []).join(' ');
549 // Add the new level if there is one
550 if (level
!== 'none') {
551 this.model
.tab
.addClass('alert_' + level
);
554 this.alert_level
= type_idx
;
558 // Scroll to the bottom of the panel
559 scrollToBottom: function (force_down
) {
560 // If this isn't the active panel, don't scroll
561 if (this.model
!== _kiwi
.app
.panels
.active
) return;
563 // Don't scroll down if we're scrolled up the panel a little
564 if (force_down
|| this.$container
.scrollTop() + this.$container
.height() > this.$el
.outerHeight() - 150) {
565 this.$container
[0].scrollTop
= this.$container
[0].scrollHeight
;
570 _kiwi
.view
.Applet
= _kiwi
.view
.Panel
.extend({
572 initialize: function (options
) {
573 this.initializePanel(options
);
577 _kiwi
.view
.Channel
= _kiwi
.view
.Panel
.extend({
578 initialize: function (options
) {
579 this.initializePanel(options
);
580 this.model
.bind('change:topic', this.topic
, this);
582 // Only show the loader if this is a channel (ie. not a query)
583 if (this.model
.isChannel()) {
584 this.$el
.append('<div class="initial_loader" style="margin:1em;text-align:center;">Joining channel.. <span class="loader"></span></div>');
588 // Override the existing newMsg() method to remove the joining channel loader
589 newMsg: function () {
590 this.$el
.find('.initial_loader').slideUp(function () {
594 return this.constructor.__super__
.newMsg
.apply(this, arguments
);
597 topic: function (topic
) {
598 if (typeof topic
!== 'string' || !topic
) {
599 topic
= this.model
.get("topic");
602 this.model
.addMsg('', '== Topic for ' + this.model
.get('name') + ' is: ' + topic
, 'topic');
604 // If this is the active channel then update the topic bar
605 if (_kiwi
.app
.panels
.active
=== this) {
606 _kiwi
.app
.topicbar
.setCurrentTopic(this.model
.get("topic"));
611 // Model for this = _kiwi.model.PanelList
612 _kiwi
.view
.Tabs
= Backbone
.View
.extend({
614 'click li': 'tabClick',
615 'click li .part': 'partClick'
618 initialize: function () {
619 this.model
.on("add", this.panelAdded
, this);
620 this.model
.on("remove", this.panelRemoved
, this);
621 this.model
.on("reset", this.render
, this);
623 this.model
.on('active', this.panelActive
, this);
625 this.tabs_applets
= $('ul.applets', this.$el
);
626 this.tabs_msg
= $('ul.channels', this.$el
);
628 _kiwi
.gateway
.on('change:name', function (gateway
, new_val
) {
629 $('span', this.model
.server
.tab
).text(new_val
);
632 render: function () {
635 this.tabs_msg
.empty();
637 // Add the server tab first
638 this.model
.server
.tab
639 .data('panel_id', this.model
.server
.cid
)
640 .appendTo(this.tabs_msg
);
642 // Go through each panel adding its tab
643 this.model
.forEach(function (panel
) {
644 // If this is the server panel, ignore as it's already added
645 if (panel
== that
.model
.server
) return;
648 .data('panel_id', panel
.cid
)
649 .appendTo(panel
.isApplet() ? this.tabs_applets
: this.tabs_msg
);
652 _kiwi
.app
.view
.doLayout();
655 updateTabTitle: function (panel
, new_title
) {
656 $('span', panel
.tab
).text(new_title
);
659 panelAdded: function (panel
) {
660 // Add a tab to the panel
661 panel
.tab
= $('<li><span>' + (panel
.get('title') || panel
.get('name')) + '</span><div class="activity"></div></li>');
663 if (panel
.isServer()) {
664 panel
.tab
.addClass('server');
667 panel
.tab
.data('panel_id', panel
.cid
)
668 .appendTo(panel
.isApplet() ? this.tabs_applets
: this.tabs_msg
);
670 panel
.bind('change:title', this.updateTabTitle
);
671 _kiwi
.app
.view
.doLayout();
673 panelRemoved: function (panel
) {
677 _kiwi
.app
.view
.doLayout();
680 panelActive: function (panel
, previously_active_panel
) {
681 // Remove any existing tabs or part images
682 $('.part', this.$el
).remove();
683 this.tabs_applets
.children().removeClass('active');
684 this.tabs_msg
.children().removeClass('active');
686 panel
.tab
.addClass('active');
688 // Only show the part image on non-server tabs
689 if (!panel
.isServer()) {
690 panel
.tab
.append('<span class="part"></span>');
694 tabClick: function (e
) {
695 var tab
= $(e
.currentTarget
);
697 var panel
= this.model
.getByCid(tab
.data('panel_id'));
699 // A panel wasn't found for this tab... wadda fuck
706 partClick: function (e
) {
707 var tab
= $(e
.currentTarget
).parent();
708 var panel
= this.model
.getByCid(tab
.data('panel_id'));
710 // Only need to part if it's a channel
711 // If the nicklist is empty, we haven't joined the channel as yet
712 if (panel
.isChannel() && panel
.get('members').models
.length
> 0) {
713 _kiwi
.gateway
.part(panel
.get('name'));
720 var next
= _kiwi
.app
.panels
.active
.tab
.next();
721 if (!next
.length
) next
= $('li:first', this.tabs_msgs
);
726 var prev
= _kiwi
.app
.panels
.active
.tab
.prev();
727 if (!prev
.length
) prev
= $('li:last', this.tabs_msgs
);
735 _kiwi
.view
.TopicBar
= Backbone
.View
.extend({
737 'keydown div': 'process'
740 initialize: function () {
741 _kiwi
.app
.panels
.bind('active', function (active_panel
) {
742 // If it's a channel topic, update and make editable
743 if (active_panel
.isChannel()) {
744 this.setCurrentTopic(active_panel
.get('topic') || '');
745 this.$el
.find('div').attr('contentEditable', true);
748 // Not a channel topic.. clear and make uneditable
749 this.$el
.find('div').attr('contentEditable', false)
755 process: function (ev
) {
756 var inp
= $(ev
.currentTarget
),
757 inp_val
= inp
.text();
759 // Only allow topic editing if this is a channel panel
760 if (!_kiwi
.app
.panels
.active
.isChannel()) {
764 // If hit return key, update the current topic
765 if (ev
.keyCode
=== 13) {
766 _kiwi
.gateway
.topic(_kiwi
.app
.panels
.active
.get('name'), inp_val
);
771 setCurrentTopic: function (new_topic
) {
772 new_topic
= new_topic
|| '';
774 // We only want a plain text version
775 $('div', this.$el
).html(formatIRCMsg(_
.escape(new_topic
)));
781 _kiwi
.view
.ControlBox
= Backbone
.View
.extend({
783 'keydown .inp': 'process',
784 'click .nick': 'showNickChange'
787 initialize: function () {
790 this.buffer
= []; // Stores previously run commands
791 this.buffer_pos
= 0; // The current position in the buffer
793 this.preprocessor
= new InputPreProcessor();
794 this.preprocessor
.recursive_depth
= 5;
796 // Hold tab autocomplete data
797 this.tabcomplete
= {active
: false, data
: [], prefix
: ''};
799 _kiwi
.gateway
.bind('change:nick', function () {
800 $('.nick', that
.$el
).text(this.get('nick'));
804 showNickChange: function (ev
) {
805 (new _kiwi
.view
.NickChangeBox()).render();
808 process: function (ev
) {
810 inp
= $(ev
.currentTarget
),
814 if (navigator
.appVersion
.indexOf("Mac") !== -1) {
820 // If not a tab key, reset the tabcomplete data
821 if (this.tabcomplete
.active
&& ev
.keyCode
!== 9) {
822 this.tabcomplete
.active
= false;
823 this.tabcomplete
.data
= [];
824 this.tabcomplete
.prefix
= '';
828 case (ev
.keyCode
=== 13): // return
829 inp_val
= inp_val
.trim();
832 $.each(inp_val
.split('\n'), function (idx
, line
) {
833 that
.processInput(line
);
836 this.buffer
.push(inp_val
);
837 this.buffer_pos
= this.buffer
.length
;
845 case (ev
.keyCode
=== 38): // up
846 if (this.buffer_pos
> 0) {
848 inp
.val(this.buffer
[this.buffer_pos
]);
852 case (ev
.keyCode
=== 40): // down
853 if (this.buffer_pos
< this.buffer
.length
) {
855 inp
.val(this.buffer
[this.buffer_pos
]);
859 case (ev
.keyCode
=== 219 && meta
): // [ + meta
860 _kiwi
.app
.panels
.view
.prev();
863 case (ev
.keyCode
=== 221 && meta
): // ] + meta
864 _kiwi
.app
.panels
.view
.next();
867 case (ev
.keyCode
=== 9): // tab
868 this.tabcomplete
.active
= true;
869 if (_
.isEqual(this.tabcomplete
.data
, [])) {
870 // Get possible autocompletions
872 $.each(_kiwi
.app
.panels
.active
.get('members').models
, function (i
, member
) {
874 ac_data
.push(member
.get('nick'));
876 ac_data
= _
.sortBy(ac_data
, function (nick
) {
879 this.tabcomplete
.data
= ac_data
;
882 if (inp_val
[inp
[0].selectionStart
- 1] === ' ') {
887 var tokens
= inp_val
.substring(0, inp
[0].selectionStart
).split(' '),
892 nick
= tokens
[tokens
.length
- 1];
893 if (this.tabcomplete
.prefix
=== '') {
894 this.tabcomplete
.prefix
= nick
;
897 this.tabcomplete
.data
= _
.select(this.tabcomplete
.data
, function (n
) {
898 return (n
.toLowerCase().indexOf(that
.tabcomplete
.prefix
.toLowerCase()) === 0);
901 if (this.tabcomplete
.data
.length
> 0) {
902 p1
= inp
[0].selectionStart
- (nick
.length
);
903 val
= inp_val
.substr(0, p1
);
904 newnick
= this.tabcomplete
.data
.shift();
905 this.tabcomplete
.data
.push(newnick
);
907 val
+= inp_val
.substr(inp
[0].selectionStart
);
910 if (inp
[0].setSelectionRange
) {
911 inp
[0].setSelectionRange(p1
+ newnick
.length
, p1
+ newnick
.length
);
912 } else if (inp
[0].createTextRange
) { // not sure if this bit is actually needed....
913 range
= inp
[0].createTextRange();
914 range
.collapse(true);
915 range
.moveEnd('character', p1
+ newnick
.length
);
916 range
.moveStart('character', p1
+ newnick
.length
);
926 processInput: function (command_raw
) {
930 // The default command
931 if (command_raw
[0] !== '/' || command_raw
.substr(0, 2) === '//') {
932 // Remove any slash escaping at the start (ie. //)
933 command_raw
= command_raw
.replace(/^\/\//, '/');
935 // Prepend the default command
936 command_raw
= '/msg ' + _kiwi
.app
.panels
.active
.get('name') + ' ' + command_raw
;
939 // Process the raw command for any aliases
940 this.preprocessor
.vars
.server
= _kiwi
.gateway
.get('name');
941 this.preprocessor
.vars
.channel
= _kiwi
.app
.panels
.active
.get('name');
942 this.preprocessor
.vars
.destination
= this.preprocessor
.vars
.channel
;
943 command_raw
= this.preprocessor
.process(command_raw
);
945 // Extract the command and parameters
946 params
= command_raw
.split(' ');
947 if (params
[0][0] === '/') {
948 command
= params
[0].substr(1).toLowerCase();
949 params
= params
.splice(1, params
.length
- 1);
953 params
.unshift(_kiwi
.app
.panels
.active
.get('name'));
956 // Trigger the command events
957 this.trigger('command', {command
: command
, params
: params
});
958 this.trigger('command:' + command
, {command
: command
, params
: params
});
960 // If we didn't have any listeners for this event, fire a special case
961 // TODO: This feels dirty. Should this really be done..?
962 if (!this._callbacks
['command:' + command
]) {
963 this.trigger('unknown_command', {command
: command
, params
: params
});
971 _kiwi
.view
.StatusMessage
= Backbone
.View
.extend({
972 initialize: function () {
975 // Timer for hiding the message after X seconds
979 text: function (text
, opt
) {
982 opt
.type
= opt
.type
|| '';
983 opt
.timeout
= opt
.timeout
|| 5000;
985 this.$el
.text(text
).attr('class', opt
.type
);
986 this.$el
.slideDown(_kiwi
.app
.view
.doLayout
);
988 if (opt
.timeout
) this.doTimeout(opt
.timeout
);
991 html: function (html
, opt
) {
994 opt
.type
= opt
.type
|| '';
995 opt
.timeout
= opt
.timeout
|| 5000;
997 this.$el
.html(text
).attr('class', opt
.type
);
998 this.$el
.slideDown(_kiwi
.app
.view
.doLayout
);
1000 if (opt
.timeout
) this.doTimeout(opt
.timeout
);
1004 this.$el
.slideUp(_kiwi
.app
.view
.doLayout
);
1007 doTimeout: function (length
) {
1008 if (this.tmr
) clearTimeout(this.tmr
);
1010 this.tmr
= setTimeout(function () { that
.hide(); }, length
);
1017 _kiwi
.view
.ResizeHandler
= Backbone
.View
.extend({
1019 'mousedown': 'startDrag',
1020 'mouseup': 'stopDrag'
1023 initialize: function () {
1024 this.dragging
= false;
1025 this.starting_width
= {};
1027 $(window
).on('mousemove', $.proxy(this.onDrag
, this));
1030 startDrag: function (event
) {
1031 this.dragging
= true;
1034 stopDrag: function (event
) {
1035 this.dragging
= false;
1038 onDrag: function (event
) {
1039 if (!this.dragging
) return;
1041 this.$el
.css('left', event
.clientX
- (this.$el
.outerWidth(true) / 2));
1042 $('#memberlists').css('width', this.$el
.parent().width() - (this.$el
.position().left
+ this.$el
.outerWidth()));
1043 _kiwi
.app
.view
.doLayout();
1049 _kiwi
.view
.AppToolbar
= Backbone
.View
.extend({
1051 'click .settings': 'clickSettings'
1054 initialize: function () {
1057 clickSettings: function (event
) {
1058 _kiwi
.app
.controlbox
.processInput('/settings');
1064 _kiwi
.view
.Application
= Backbone
.View
.extend({
1065 initialize: function () {
1066 $(window
).resize(this.doLayout
);
1067 $('#toolbar').resize(this.doLayout
);
1068 $('#controlbox').resize(this.doLayout
);
1070 // Change the theme when the config is changed
1071 _kiwi
.global
.settings
.on('change:theme', this.updateTheme
, this);
1072 this.updateTheme(getQueryVariable('theme'));
1074 _kiwi
.global
.settings
.on('change:channel_list_style', this.setTabLayout
, this);
1075 this.setTabLayout(_kiwi
.global
.settings
.get('channel_list_style'));
1079 $(document
).keydown(this.setKeyFocus
);
1081 // Confirmation require to leave the page
1082 window
.onbeforeunload = function () {
1083 if (_kiwi
.gateway
.isConnected()) {
1084 return 'This will close all KiwiIRC conversations. Are you sure you want to close this window?';
1091 updateTheme: function (theme_name
) {
1092 // If called by the settings callback, get the correct new_value
1093 if (theme_name
=== _kiwi
.global
.settings
) {
1094 theme_name
= arguments
[1];
1097 // If we have no theme specified, get it from the settings
1098 if (!theme_name
) theme_name
= _kiwi
.global
.settings
.get('theme');
1100 // Clear any current theme
1101 this.$el
.removeClass(function (i
, css
) {
1102 return (css
.match(/\btheme_\S+/g) || []).join(' ');
1105 // Apply the new theme
1106 this.$el
.addClass('theme_' + (theme_name
|| 'relaxed'));
1110 setTabLayout: function (layout_style
) {
1111 // If called by the settings callback, get the correct new_value
1112 if (layout_style
=== _kiwi
.global
.settings
) {
1113 layout_style
= arguments
[1];
1116 if (layout_style
== 'list') {
1117 this.$el
.addClass('chanlist_treeview');
1119 this.$el
.removeClass('chanlist_treeview');
1126 // Globally shift focus to the command input box on a keypress
1127 setKeyFocus: function (ev
) {
1128 // If we're copying text, don't shift focus
1129 if (ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
1133 // If we're typing into an input box somewhere, ignore
1134 if ((ev
.target
.tagName
.toLowerCase() === 'input') || $(ev
.target
).attr('contenteditable')) {
1138 $('#controlbox .inp').focus();
1142 doLayout: function () {
1143 var el_kiwi
= $('#kiwi');
1144 var el_panels
= $('#panels');
1145 var el_memberlists
= $('#memberlists');
1146 var el_toolbar
= $('#toolbar');
1147 var el_controlbox
= $('#controlbox');
1148 var el_resize_handle
= $('#memberlists_resize_handle');
1151 top
: el_toolbar
.outerHeight(true),
1152 bottom
: el_controlbox
.outerHeight(true)
1156 // If any elements are not visible, full size the panals instead
1157 if (!el_toolbar
.is(':visible')) {
1158 css_heights
.top
= 0;
1161 if (!el_controlbox
.is(':visible')) {
1162 css_heights
.bottom
= 0;
1165 // Apply the CSS sizes
1166 el_panels
.css(css_heights
);
1167 el_memberlists
.css(css_heights
);
1168 el_resize_handle
.css(css_heights
);
1170 // If we have channel tabs on the side, adjust the height
1171 if (el_kiwi
.hasClass('chanlist_treeview')) {
1172 $('#kiwi #tabs').css(css_heights
);
1175 // Determine if we have a narrow window (mobile/tablet/or even small desktop window)
1176 if (el_kiwi
.outerWidth() < 400) {
1177 el_kiwi
.addClass('narrow');
1179 el_kiwi
.removeClass('narrow');
1182 // Set the panels width depending on the memberlist visibility
1183 if (el_memberlists
.css('display') != 'none') {
1184 // Panels to the side of the memberlist
1185 el_panels
.css('right', el_memberlists
.outerWidth(true));
1186 // The resize handle sits overlapping the panels and memberlist
1187 el_resize_handle
.css('left', el_memberlists
.position().left
- (el_resize_handle
.outerWidth(true) / 2));
1189 // Memberlist is hidden so panels to the right edge
1190 el_panels
.css('right', 0);
1191 // And move the handle just out of sight to the right
1192 el_resize_handle
.css('left', el_panels
.outerWidth(true));
1197 alertWindow: function (title
) {
1198 if (!this.alertWindowTimer
) {
1199 this.alertWindowTimer
= new (function () {
1202 var has_focus
= true;
1204 var default_title
= 'Kiwi IRC';
1205 var title
= 'Kiwi IRC';
1207 this.setTitle = function (new_title
) {
1208 new_title
= new_title
|| default_title
;
1209 window
.document
.title
= new_title
;
1213 this.start = function (new_title
) {
1214 // Don't alert if we already have focus
1215 if (has_focus
) return;
1219 tmr
= setInterval(this.update
, 1000);
1222 this.stop = function () {
1223 // Stop the timer and clear the title
1224 if (tmr
) clearInterval(tmr
);
1228 // Some browsers don't always update the last title correctly
1229 // Wait a few seconds and then reset
1230 setTimeout(this.reset
, 2000);
1233 this.reset = function () {
1239 this.update = function () {
1241 that
.setTitle(title
);
1249 $(window
).focus(function (event
) {
1253 // Some browsers don't always update the last title correctly
1254 // Wait a few seconds and then reset
1255 setTimeout(that
.reset
, 2000);
1258 $(window
).blur(function (event
) {
1264 this.alertWindowTimer
.start(title
);
1268 barsHide: function (instant
) {
1272 $('#toolbar').slideUp({queue
: false, duration
: 400, step
: this.doLayout
});
1273 $('#controlbox').slideUp({queue
: false, duration
: 400, step
: this.doLayout
});
1275 $('#toolbar').slideUp(0);
1276 $('#controlbox').slideUp(0);
1281 barsShow: function (instant
) {
1285 $('#toolbar').slideDown({queue
: false, duration
: 400, step
: this.doLayout
});
1286 $('#controlbox').slideDown({queue
: false, duration
: 400, step
: this.doLayout
});
1288 $('#toolbar').slideDown(0);
1289 $('#controlbox').slideDown(0);
1303 _kiwi
.view
.MediaMessage
= Backbone
.View
.extend({
1305 'click .media_close': 'close'
1308 initialize: function () {
1309 // Get the URL from the data
1310 this.url
= this.$el
.data('url');
1313 // Close the media content and remove it from display
1314 close: function () {
1316 this.$content
.slideUp('fast', function () {
1317 that
.$content
.remove();
1321 // Open the media content within its wrapper
1323 // Create the content div if we haven't already
1324 if (!this.$content
) {
1325 this.$content
= $('<div class="media_content"><a class="media_close"><i class="icon-chevron-up"></i> Close media</a><br /><div class="content"></div></div>');
1326 this.$content
.find('.content').append(this.mediaTypes
[this.$el
.data('type')].apply(this, []) || 'Not found :(');
1329 // Now show the content if not already
1330 if (!this.$content
.is(':visible')) {
1331 // Hide it first so the slideDown always plays
1332 this.$content
.hide();
1334 // Add the media content and slide it into view
1335 this.$el
.append(this.$content
);
1336 this.$content
.slideDown();
1342 // Generate the media content for each recognised type
1344 twitter: function () {
1345 var tweet_id
= this.$el
.data('tweetid');
1348 $.getJSON('https://api.twitter.com/1/statuses/oembed.json?id=' + tweet_id
+ '&callback=?', function (data
) {
1349 that
.$content
.find('.content').html(data
.html
);
1352 return $('<div>Loading tweet..</div>');
1356 image: function () {
1357 return $('<a href="' + this.url
+ '" target="_blank"><img height="100" src="' + this.url
+ '" /></a>');
1361 reddit: function () {
1363 var matches
= (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(this.url
);
1365 $.getJSON('http://www.' + matches
[0] + '.json?jsonp=?', function (data
) {
1366 console
.log('Loaded reddit data', data
);
1367 var post
= data
[0].data
.children
[0].data
;
1370 // Show a thumbnail if there is one
1371 if (post
.thumbnail
) {
1372 //post.thumbnail = 'http://www.eurotunnel.com/uploadedImages/commercial/back-steps-icon-arrow.png';
1374 // Hide the thumbnail if an over_18 image
1376 thumb
= '<span class="thumbnail_nsfw" onclick="$(this).find(\'p\').remove(); $(this).find(\'img\').css(\'visibility\', \'visible\');">';
1377 thumb
+= '<p style="font-size:0.9em;line-height:1.2em;cursor:pointer;">Show<br />NSFW</p>';
1378 thumb
+= '<img src="' + post
.thumbnail
+ '" class="thumbnail" style="visibility:hidden;" />';
1381 thumb
= '<img src="' + post
.thumbnail
+ '" class="thumbnail" />';
1385 // Build the template string up
1386 var tmpl
= '<div>' + thumb
+ '<b><%- title %></b><br />Posted by <%- author %>. ';
1387 tmpl
+= '<i class="icon-arrow-up"></i> <%- ups %> <i class="icon-arrow-down"></i> <%- downs %><br />';
1388 tmpl
+= '<%- num_comments %> comments made. <a href="http://www.reddit.com<%- permalink %>">View post</a></div>';
1390 that
.$content
.find('.content').html(_
.template(tmpl
, post
));
1393 return $('<div>Loading Reddit thread..</div>');
1399 // Build the closed media HTML from a URL
1400 buildHtml: function (url
) {
1401 var html
= '', matches
;
1404 if (url
.match(/(\.jpe?g|\.gif|\.bmp|\.png)\??$/i)) {
1405 html
+= '<span class="media image" data-type="image" data-url="' + url
+ '" title="Open Image"><a class="open"><i class="icon-chevron-right"></i></a></span>';
1409 matches
= (/https?:\/\/twitter.com\/([a-zA-Z0-9_]+)\/status\/([0-9]+)/ig).exec(url
);
1411 html
+= '<span class="media twitter" data-type="twitter" data-url="' + url
+ '" data-tweetid="' + matches
[2] + '" title="Show tweet information"><a class="open"><i class="icon-chevron-right"></i></a></span>';
1415 matches
= (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(url
);
1417 html
+= '<span class="media reddit" data-type="reddit" data-url="' + url
+ '" title="Reddit thread"><a class="open"><i class="icon-chevron-right"></i></a></span>';