Server connection dialog extracted
[KiwiIRC.git] / client / assets / dev / view.js
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 */
2 /*global kiwi */
3
4 _kiwi.view.MemberList = Backbone.View.extend({
5 tagName: "ul",
6 events: {
7 "click .nick": "nickClick"
8 },
9 initialize: function (options) {
10 this.model.bind('all', this.render, this);
11 $(this.el).appendTo('#memberlists');
12 },
13 render: function () {
14 var $this = $(this.el);
15 $this.empty();
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>')
19 .appendTo($this)
20 .data('member', member);
21 });
22 },
23 nickClick: function (event) {
24 var $target = $(event.currentTarget).parent('li'),
25 member = $target.data('member'),
26 userbox;
27
28 event.stopPropagation();
29
30 // If the userbox already exists here, hide it
31 if ($target.find('.userbox').length > 0) {
32 $('.userbox', this.$el).remove();
33 return;
34 }
35
36 userbox = new _kiwi.view.UserBox();
37 userbox.member = member;
38 userbox.channel = this.model.channel;
39
40 if (!this.model.getByNick(_kiwi.app.connections.active_connection.get('nick')).get('is_op')) {
41 userbox.$el.children('.if_op').remove();
42 }
43
44 var menu = new _kiwi.view.MenuBox(member.get('nick') || 'User');
45 menu.addItem('userbox', userbox.$el);
46 menu.show();
47
48 // Position the userbox + menubox
49 (function() {
50 var t = event.pageY,
51 m_bottom = t + menu.$el.outerHeight(), // Where the bottom of menu will be
52 memberlist_bottom = this.$el.parent().offset().top + this.$el.parent().outerHeight();
53
54 // If the bottom of the userbox is going to be too low.. raise it
55 if (m_bottom > memberlist_bottom){
56 t = memberlist_bottom - menu.$el.outerHeight();
57 }
58
59 // Set the new positon
60 menu.$el.offset({
61 left: _kiwi.app.view.$el.width() - menu.$el.outerWidth() - 20,
62 top: t
63 });
64 }).call(this);
65 },
66 show: function () {
67 $('#memberlists').children().removeClass('active');
68 $(this.el).addClass('active');
69 }
70 });
71
72
73
74 _kiwi.view.UserBox = Backbone.View.extend({
75 events: {
76 'click .query': 'queryClick',
77 'click .info': 'infoClick',
78 'click .slap': 'slapClick',
79 'click .op': 'opClick',
80 'click .deop': 'deopClick',
81 'click .voice': 'voiceClick',
82 'click .devoice': 'devoiceClick',
83 'click .kick': 'kickClick',
84 'click .ban': 'banClick'
85 },
86
87 initialize: function () {
88 this.$el = $($('#tmpl_userbox').html());
89 },
90
91 queryClick: function (event) {
92 var panel = new _kiwi.model.Query({name: this.member.get('nick')});
93 _kiwi.app.connections.active_connection.panels.add(panel);
94 panel.view.show();
95 },
96
97 infoClick: function (event) {
98 _kiwi.app.controlbox.processInput('/whois ' + this.member.get('nick'));
99 },
100
101 slapClick: function (event) {
102 _kiwi.app.controlbox.processInput('/slap ' + this.member.get('nick'));
103 },
104
105 opClick: function (event) {
106 _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +o ' + this.member.get('nick'));
107 },
108
109 deopClick: function (event) {
110 _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' -o ' + this.member.get('nick'));
111 },
112
113 voiceClick: function (event) {
114 _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +v ' + this.member.get('nick'));
115 },
116
117 devoiceClick: function (event) {
118 _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' -v ' + this.member.get('nick'));
119 },
120
121 kickClick: function (event) {
122 // TODO: Enable the use of a custom kick message
123 _kiwi.app.controlbox.processInput('/kick ' + this.member.get('nick') + ' Bye!');
124 },
125
126 banClick: function (event) {
127 // TODO: Set ban on host, not just on nick
128 _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +b ' + this.member.get('nick') + '!*');
129 }
130 });
131
132 _kiwi.view.NickChangeBox = Backbone.View.extend({
133 events: {
134 'submit': 'changeNick',
135 'click .cancel': 'close'
136 },
137
138 initialize: function () {
139 this.$el = $($('#tmpl_nickchange').html());
140 },
141
142 render: function () {
143 // Add the UI component and give it focus
144 _kiwi.app.controlbox.$el.prepend(this.$el);
145 this.$el.find('input').focus();
146
147 this.$el.css('bottom', _kiwi.app.controlbox.$el.outerHeight(true));
148 },
149
150 close: function () {
151 this.$el.remove();
152
153 },
154
155 changeNick: function (event) {
156 var that = this;
157
158 event.preventDefault();
159
160 _kiwi.app.connections.active_connection.gateway.changeNick(this.$el.find('input').val(), function (err, val) {
161 that.close();
162 });
163 return false;
164 }
165 });
166
167 _kiwi.view.ServerSelect = function () {
168 // Are currently showing all the controlls or just a nick_change box?
169 var state = 'all';
170
171 var model = Backbone.View.extend({
172 events: {
173 'submit form': 'submitForm',
174 'click .show_more': 'showMore',
175 'change .have_pass input': 'showPass'
176 },
177
178 initialize: function () {
179 var that = this;
180
181 this.$el = $($('#tmpl_server_select').html());
182
183 // Remove the 'more' link if the server has disabled server changing
184 if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) {
185 if (!_kiwi.app.server_settings.connection.allow_change) {
186 this.$el.find('.show_more').remove();
187 this.$el.addClass('single_server');
188 }
189 }
190
191 _kiwi.gateway.bind('onconnect', this.networkConnected, this);
192 _kiwi.gateway.bind('connecting', this.networkConnecting, this);
193 _kiwi.gateway.bind('onirc_error', this.onIrcError, this);
194 },
195
196 dispose: function() {
197 _kiwi.gateway.off('onconnect', this.networkConnected, this);
198 _kiwi.gateway.off('connecting', this.networkConnecting, this);
199 _kiwi.gateway.off('onirc_error', this.onIrcError, this);
200
201 this.$el.remove();
202 },
203
204 submitForm: function (event) {
205 event.preventDefault();
206
207 // Make sure a nick is chosen
208 if (!$('input.nick', this.$el).val().trim()) {
209 this.setStatus('Select a nickname first!');
210 $('input.nick', this.$el).select();
211 return;
212 }
213
214 if (state === 'nick_change') {
215 this.submitNickChange(event);
216 } else {
217 this.submitLogin(event);
218 }
219
220 $('button', this.$el).attr('disabled', 1);
221 return;
222 },
223
224 submitLogin: function (event) {
225 // If submitting is disabled, don't do anything
226 if ($('button', this.$el).attr('disabled')) return;
227
228 var values = {
229 nick: $('input.nick', this.$el).val(),
230 server: $('input.server', this.$el).val(),
231 port: $('input.port', this.$el).val(),
232 ssl: $('input.ssl', this.$el).prop('checked'),
233 password: $('input.password', this.$el).val(),
234 channel: $('input.channel', this.$el).val(),
235 channel_key: $('input.channel_key', this.$el).val()
236 };
237
238 this.trigger('server_connect', values);
239 },
240
241 submitNickChange: function (event) {
242 _kiwi.gateway.changeNick($('input.nick', this.$el).val());
243 this.networkConnecting();
244 },
245
246 showPass: function (event) {
247 if (this.$el.find('tr.have_pass input').is(':checked')) {
248 this.$el.find('tr.pass').show().find('input').focus();
249 } else {
250 this.$el.find('tr.pass').hide().find('input').val('');
251 }
252 },
253
254 showMore: function (event) {
255 $('.more', this.$el).slideDown('fast');
256 $('input.server', this.$el).select();
257 },
258
259 populateFields: function (defaults) {
260 var nick, server, port, channel, channel_key, ssl, password;
261
262 defaults = defaults || {};
263
264 nick = defaults.nick || '';
265 server = defaults.server || '';
266 port = defaults.port || 6667;
267 ssl = defaults.ssl || 0;
268 password = defaults.password || '';
269 channel = defaults.channel || '';
270 channel_key = defaults.channel_key || '';
271
272 $('input.nick', this.$el).val(nick);
273 $('input.server', this.$el).val(server);
274 $('input.port', this.$el).val(port);
275 $('input.ssl', this.$el).prop('checked', ssl);
276 $('input.password', this.$el).val(password);
277 $('input.channel', this.$el).val(channel);
278 $('input.channel_key', this.$el).val(channel_key);
279 },
280
281 hide: function () {
282 this.$el.slideUp();
283 },
284
285 show: function (new_state) {
286 new_state = new_state || 'all';
287
288 this.$el.show();
289
290 if (new_state === 'all') {
291 $('.show_more', this.$el).show();
292
293 } else if (new_state === 'more') {
294 $('.more', this.$el).slideDown('fast');
295
296 } else if (new_state === 'nick_change') {
297 $('.more', this.$el).hide();
298 $('.show_more', this.$el).hide();
299 $('input.nick', this.$el).select();
300 }
301
302 state = new_state;
303 },
304
305 setStatus: function (text, class_name) {
306 $('.status', this.$el)
307 .text(text)
308 .attr('class', 'status')
309 .addClass(class_name||'')
310 .show();
311 },
312 clearStatus: function () {
313 $('.status', this.$el).hide();
314 },
315
316 networkConnected: function (event) {
317 this.setStatus('Connected :)', 'ok');
318 $('form', this.$el).hide();
319 },
320
321 networkConnecting: function (event) {
322 this.setStatus('Connecting..', 'ok');
323 },
324
325 onIrcError: function (data) {
326 $('button', this.$el).attr('disabled', null);
327
328 if (data.error == 'nickname_in_use') {
329 this.setStatus('Nickname already taken');
330 this.show('nick_change');
331 }
332
333 if (data.error == 'password_mismatch') {
334 this.setStatus('Incorrect Password');
335 this.show('nick_change');
336 that.$el.find('.password').select();
337 }
338 },
339
340 showError: function (event) {
341 this.setStatus('Error connecting', 'error');
342 $('button', this.$el).attr('disabled', null);
343 this.show();
344 }
345 });
346
347
348 return new model(arguments);
349 };
350
351
352 _kiwi.view.Panel = Backbone.View.extend({
353 tagName: "div",
354 className: "panel messages",
355
356 events: {
357 "click .chan": "chanClick",
358 'click .media .open': 'mediaClick',
359 'mouseenter .msg .nick': 'msgEnter',
360 'mouseleave .msg .nick': 'msgLeave'
361 },
362
363 initialize: function (options) {
364 this.initializePanel(options);
365 },
366
367 initializePanel: function (options) {
368 this.$el.css('display', 'none');
369 options = options || {};
370
371 // Containing element for this panel
372 if (options.container) {
373 this.$container = $(options.container);
374 } else {
375 this.$container = $('#panels .container1');
376 }
377
378 this.$el.appendTo(this.$container);
379
380 this.alert_level = 0;
381
382 this.model.bind('msg', this.newMsg, this);
383 this.msg_count = 0;
384
385 this.model.set({"view": this}, {"silent": true});
386 },
387
388 render: function () {
389 var that = this;
390
391 this.$el.empty();
392 _.each(this.model.get('scrollback'), function (msg) {
393 that.newMsg(msg);
394 });
395 },
396
397 newMsg: function (msg) {
398 var re, line_msg, $this = this.$el,
399 nick_colour_hex, nick_hex, is_highlight, msg_css_classes = '';
400
401 // Nick highlight detecting
402 if ((new RegExp('\\b' + _kiwi.app.connections.active_connection.get('nick') + '\\b', 'i')).test(msg.msg)) {
403 is_highlight = true;
404 msg_css_classes += ' highlight';
405 }
406
407 // Escape any HTML that may be in here
408 msg.msg = $('<div />').text(msg.msg).html();
409
410 // Make the channels clickable
411 re = new RegExp('(?:^|\\s)([' + _kiwi.gateway.get('channel_prefix') + '][^ ,.\\007]+)', 'g');
412 msg.msg = msg.msg.replace(re, function (match) {
413 return '<a class="chan" data-channel="' + match.trim() + '">' + match + '</a>';
414 });
415
416
417 // Parse any links found
418 msg.msg = msg.msg.replace(/(([A-Za-z0-9\-]+\:\/\/)|(www\.))([\w.\-]+)([a-zA-Z]{2,6})(:[0-9]+)?(\/[\w#!:.?$'()[\]*,;~+=&%@!\-\/]*)?/gi, function (url) {
419 var nice = url,
420 extra_html = '';
421
422 // Add the http if no protoocol was found
423 if (url.match(/^www\./)) {
424 url = 'http://' + url;
425 }
426
427 // Shorten the displayed URL if it's going to be too long
428 if (nice.length > 100) {
429 nice = nice.substr(0, 100) + '...';
430 }
431
432 // Get any media HTML if supported
433 extra_html = _kiwi.view.MediaMessage.buildHtml(url);
434
435 // Make the link clickable
436 return '<a class="link_ext" target="_blank" rel="nofollow" href="' + url + '">' + nice + '</a> ' + extra_html;
437 });
438
439
440 // Convert IRC formatting into HTML formatting
441 msg.msg = formatIRCMsg(msg.msg);
442
443
444 // Add some colours to the nick (Method based on IRSSIs nickcolor.pl)
445 nick_colour_hex = (function (nick) {
446 var nick_int = 0, rgb;
447
448 _.map(nick.split(''), function (i) { nick_int += i.charCodeAt(0); });
449 rgb = hsl2rgb(nick_int % 255, 70, 35);
450 rgb = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16);
451
452 return '#' + rgb.toString(16);
453 })(msg.nick);
454
455 msg.nick_style = 'color:' + nick_colour_hex + ';';
456
457 // Generate a hex string from the nick to be used as a CSS class name
458 nick_hex = msg.nick_css_class = '';
459 if (msg.nick) {
460 _.map(msg.nick.split(''), function (char) {
461 nick_hex += char.charCodeAt(0).toString(16);
462 });
463 msg_css_classes += ' nick_' + nick_hex;
464 }
465
466 // Build up and add the line
467 msg.msg_css_classes = msg_css_classes;
468 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>';
469 $this.append(_.template(line_msg, msg));
470
471 // Activity/alerts based on the type of new message
472 if (msg.type.match(/^action /)) {
473 this.alert('action');
474
475 } else if (is_highlight) {
476 _kiwi.app.view.alertWindow('* People are talking!');
477 _kiwi.app.view.playSound('highlight');
478 this.alert('highlight');
479
480 } else {
481 // If this is the active panel, send an alert out
482 if (this.model.isActive()) {
483 _kiwi.app.view.alertWindow('* People are talking!');
484 }
485 this.alert('activity');
486 }
487
488 if (this.model.isQuery() && !this.model.isActive()) {
489 _kiwi.app.view.alertWindow('* People are talking!');
490 _kiwi.app.view.playSound('highlight');
491 }
492
493 // Update the activity counters
494 (function () {
495 // Only inrement the counters if we're not the active panel
496 if (this.model.isActive()) return;
497
498 var $act = this.model.tab.find('.activity');
499 $act.text((parseInt($act.text(), 10) || 0) + 1);
500 if ($act.text() === '0') {
501 $act.addClass('zero');
502 } else {
503 $act.removeClass('zero');
504 }
505 }).apply(this);
506
507 this.scrollToBottom();
508
509 // Make sure our DOM isn't getting too large (Acts as scrollback)
510 this.msg_count++;
511 if (this.msg_count > (parseInt(_kiwi.global.settings.get('scrollback'), 10) || 250)) {
512 $('.msg:first', this.$el).remove();
513 this.msg_count--;
514 }
515 },
516 chanClick: function (event) {
517 if (event.target) {
518 _kiwi.gateway.join($(event.target).data('channel'));
519 } else {
520 // IE...
521 _kiwi.gateway.join($(event.srcElement).data('channel'));
522 }
523 },
524
525 mediaClick: function (event) {
526 var $media = $(event.target).parents('.media');
527 var media_message;
528
529 if ($media.data('media')) {
530 media_message = $media.data('media');
531 } else {
532 media_message = new _kiwi.view.MediaMessage({el: $media[0]});
533 $media.data('media', media_message);
534 }
535
536 $media.data('media', media_message);
537
538 media_message.open();
539 },
540
541 // Cursor hovers over a message
542 msgEnter: function (event) {
543 var nick_class;
544
545 // Find a valid class that this element has
546 _.each($(event.currentTarget).parent('.msg').attr('class').split(' '), function (css_class) {
547 if (css_class.match(/^nick_[a-z0-9]+/i)) {
548 nick_class = css_class;
549 }
550 });
551
552 // If no class was found..
553 if (!nick_class) return;
554
555 $('.'+nick_class).addClass('global_nick_highlight');
556 },
557
558 // Cursor leaves message
559 msgLeave: function (event) {
560 var nick_class;
561
562 // Find a valid class that this element has
563 _.each($(event.currentTarget).parent('.msg').attr('class').split(' '), function (css_class) {
564 if (css_class.match(/^nick_[a-z0-9]+/i)) {
565 nick_class = css_class;
566 }
567 });
568
569 // If no class was found..
570 if (!nick_class) return;
571
572 $('.'+nick_class).removeClass('global_nick_highlight');
573 },
574
575 show: function () {
576 var $this = this.$el;
577
578 // Hide all other panels and show this one
579 this.$container.children('.panel').css('display', 'none');
580 $this.css('display', 'block');
581
582 // Show this panels memberlist
583 var members = this.model.get("members");
584 if (members) {
585 $('#memberlists').removeClass('disabled');
586 members.view.show();
587 } else {
588 // Memberlist not found for this panel, hide any active ones
589 $('#memberlists').addClass('disabled').children().removeClass('active');
590 }
591
592 // Remove any alerts and activity counters for this panel
593 this.alert('none');
594 this.model.tab.find('.activity').text('0').addClass('zero');
595
596 _kiwi.app.panels.trigger('active', this.model, _kiwi.app.panels().active);
597 this.model.trigger('active', this.model);
598
599 _kiwi.app.view.doLayout();
600
601 this.scrollToBottom(true);
602 },
603
604
605 alert: function (level) {
606 // No need to highlight if this si the active panel
607 if (this.model == _kiwi.app.panels().active) return;
608
609 var types, type_idx;
610 types = ['none', 'action', 'activity', 'highlight'];
611
612 // Default alert level
613 level = level || 'none';
614
615 // If this alert level does not exist, assume clearing current level
616 type_idx = _.indexOf(types, level);
617 if (!type_idx) {
618 level = 'none';
619 type_idx = 0;
620 }
621
622 // Only 'upgrade' the alert. Never down (unless clearing)
623 if (type_idx !== 0 && type_idx <= this.alert_level) {
624 return;
625 }
626
627 // Clear any existing levels
628 this.model.tab.removeClass(function (i, css) {
629 return (css.match(/\balert_\S+/g) || []).join(' ');
630 });
631
632 // Add the new level if there is one
633 if (level !== 'none') {
634 this.model.tab.addClass('alert_' + level);
635 }
636
637 this.alert_level = type_idx;
638 },
639
640
641 // Scroll to the bottom of the panel
642 scrollToBottom: function (force_down) {
643 // If this isn't the active panel, don't scroll
644 if (this.model !== _kiwi.app.panels().active) return;
645
646 // Don't scroll down if we're scrolled up the panel a little
647 if (force_down || this.$container.scrollTop() + this.$container.height() > this.$el.outerHeight() - 150) {
648 this.$container[0].scrollTop = this.$container[0].scrollHeight;
649 }
650 }
651 });
652
653 _kiwi.view.Applet = _kiwi.view.Panel.extend({
654 className: 'applet',
655 initialize: function (options) {
656 this.initializePanel(options);
657 }
658 });
659
660 _kiwi.view.Channel = _kiwi.view.Panel.extend({
661 initialize: function (options) {
662 this.initializePanel(options);
663 this.model.bind('change:topic', this.topic, this);
664
665 // Only show the loader if this is a channel (ie. not a query)
666 if (this.model.isChannel()) {
667 this.$el.append('<div class="initial_loader" style="margin:1em;text-align:center;">Joining channel.. <span class="loader"></span></div>');
668 }
669 },
670
671 // Override the existing newMsg() method to remove the joining channel loader
672 newMsg: function () {
673 this.$el.find('.initial_loader').slideUp(function () {
674 $(this).remove();
675 });
676
677 return this.constructor.__super__.newMsg.apply(this, arguments);
678 },
679
680 topic: function (topic) {
681 if (typeof topic !== 'string' || !topic) {
682 topic = this.model.get("topic");
683 }
684
685 this.model.addMsg('', '== Topic for ' + this.model.get('name') + ' is: ' + topic, 'topic');
686
687 // If this is the active channel then update the topic bar
688 if (_kiwi.app.panels().active === this) {
689 _kiwi.app.topicbar.setCurrentTopic(this.model.get("topic"));
690 }
691 }
692 });
693
694
695
696 // Model for this = _kiwi.model.NetworkPanelList
697 _kiwi.view.NetworkTabs = Backbone.View.extend({
698 tagName: 'ul',
699 className: 'connections',
700
701 initialize: function() {
702 this.model.on('add', this.networkAdded, this);
703 this.model.on('remove', this.networkRemoved, this);
704
705 this.$el.appendTo($('#kiwi #tabs'));
706 },
707
708 networkAdded: function(network) {
709 $('<li class="connection"></li>')
710 .append(network.panels.view.$el)
711 .appendTo(this.$el);
712 },
713
714 networkRemoved: function(network) {
715 network.panels.view.remove();
716
717 _kiwi.app.view.doLayout();
718 }
719 });
720
721
722
723 // Model for this = _kiwi.model.PanelList
724 _kiwi.view.Tabs = Backbone.View.extend({
725 tagName: 'ul',
726 className: 'panellist',
727
728 events: {
729 'click li': 'tabClick',
730 'click li .part': 'partClick'
731 },
732
733 initialize: function () {
734 this.model.on("add", this.panelAdded, this);
735 this.model.on("remove", this.panelRemoved, this);
736 this.model.on("reset", this.render, this);
737
738 this.model.on('active', this.panelActive, this);
739
740 // Network tabs start with a server, so determine what we are now
741 this.is_network = false;
742
743 if (this.model.network) {
744 this.is_network = true;
745
746 this.model.network.on('change:name', function (network, new_val) {
747 $('span', this.model.server.tab).text(new_val);
748 }, this);
749 }
750 },
751
752 render: function () {
753 var that = this;
754
755 this.$el.empty();
756
757 if (this.is_network) {
758 // Add the server tab first
759 this.model.server.tab
760 .data('panel', this.model.server)
761 .data('connection_id', this.model.network.get('connection_id'))
762 .appendTo(this.$el);
763 }
764
765 // Go through each panel adding its tab
766 this.model.forEach(function (panel) {
767 // If this is the server panel, ignore as it's already added
768 if (this.is_network && panel == that.model.server)
769 return;
770
771 panel.tab.data('panel', panel);
772
773 if (this.is_network)
774 panel.tab.data('connection_id', this.model.network.get('connection_id'));
775
776 panel.tab.appendTo(that.$el);
777 });
778
779 _kiwi.app.view.doLayout();
780 },
781
782 updateTabTitle: function (panel, new_title) {
783 $('span', panel.tab).text(new_title);
784 },
785
786 panelAdded: function (panel) {
787 // Add a tab to the panel
788 panel.tab = $('<li><span>' + (panel.get('title') || panel.get('name')) + '</span><div class="activity"></div></li>');
789
790 if (panel.isServer()) {
791 panel.tab.addClass('server');
792 panel.tab.addClass('icon-nonexistant');
793 }
794
795 panel.tab.data('panel', panel);
796
797 if (this.is_network)
798 panel.tab.data('connection_id', this.model.network.get('connection_id'));
799
800 panel.tab.appendTo(this.$el);
801
802 panel.bind('change:title', this.updateTabTitle);
803 _kiwi.app.view.doLayout();
804 },
805 panelRemoved: function (panel) {
806 panel.tab.remove();
807 delete panel.tab;
808
809 _kiwi.app.view.doLayout();
810 },
811
812 panelActive: function (panel, previously_active_panel) {
813 // Remove any existing tabs or part images
814 _kiwi.app.view.$el.find('.panellist .part').remove();
815 _kiwi.app.view.$el.find('.panellist .active').removeClass('active');
816
817 panel.tab.addClass('active');
818
819 // Only show the part image on non-server tabs
820 if (!panel.isServer()) {
821 panel.tab.append('<span class="part icon-nonexistant"></span>');
822 }
823 },
824
825 tabClick: function (e) {
826 var tab = $(e.currentTarget);
827
828 var panel = tab.data('panel');
829 if (!panel) {
830 // A panel wasn't found for this tab... wadda fuck
831 return;
832 }
833
834 panel.view.show();
835 },
836
837 partClick: function (e) {
838 var tab = $(e.currentTarget).parent();
839 var panel = this.model.getByCid(tab.data('panel'));
840
841 // Only need to part if it's a channel
842 // If the nicklist is empty, we haven't joined the channel as yet
843 if (panel.isChannel() && panel.get('members').models.length > 0) {
844 this.model.network.gateway.part(panel.get('name'));
845 } else {
846 panel.close();
847 }
848 },
849
850 next: function () {
851 var next = this.$tab_container.find('.active').next();
852 if (!next.length) next = $('li:first', this.$tab_container);
853
854 next.click();
855 },
856 prev: function () {
857 var prev = this.$tab_container.find('.active').prev();
858 if (!prev.length) prev = $('li:last', this.$tab_container);
859
860 prev.click();
861 }
862 });
863
864
865
866 _kiwi.view.TopicBar = Backbone.View.extend({
867 events: {
868 'keydown div': 'process'
869 },
870
871 initialize: function () {
872 _kiwi.app.panels.bind('active', function (active_panel) {
873 // If it's a channel topic, update and make editable
874 if (active_panel.isChannel()) {
875 this.setCurrentTopic(active_panel.get('topic') || '');
876 this.$el.find('div').attr('contentEditable', true);
877
878 } else {
879 // Not a channel topic.. clear and make uneditable
880 this.$el.find('div').attr('contentEditable', false)
881 .text('');
882 }
883 }, this);
884 },
885
886 process: function (ev) {
887 var inp = $(ev.currentTarget),
888 inp_val = inp.text();
889
890 // Only allow topic editing if this is a channel panel
891 if (!_kiwi.app.panels().active.isChannel()) {
892 return false;
893 }
894
895 // If hit return key, update the current topic
896 if (ev.keyCode === 13) {
897 _kiwi.gateway.topic(_kiwi.app.panels().active.get('name'), inp_val);
898 return false;
899 }
900 },
901
902 setCurrentTopic: function (new_topic) {
903 new_topic = new_topic || '';
904
905 // We only want a plain text version
906 $('div', this.$el).html(formatIRCMsg(_.escape(new_topic)));
907 }
908 });
909
910
911
912 _kiwi.view.ControlBox = Backbone.View.extend({
913 events: {
914 'keydown .inp': 'process',
915 'click .nick': 'showNickChange'
916 },
917
918 initialize: function () {
919 var that = this;
920
921 this.buffer = []; // Stores previously run commands
922 this.buffer_pos = 0; // The current position in the buffer
923
924 this.preprocessor = new InputPreProcessor();
925 this.preprocessor.recursive_depth = 5;
926
927 // Hold tab autocomplete data
928 this.tabcomplete = {active: false, data: [], prefix: ''};
929
930 // Keep the nick view updated with nick changes
931 _kiwi.app.connections.on('change:nick', function(connection) {
932 // Only update the nick view if it's the active connection
933 if (connection !== _kiwi.app.connections.active_connection)
934 return;
935
936 $('.nick', that.$el).text(connection.get('nick'));
937 });
938
939 // Update our nick view as we flick between connections
940 _kiwi.app.connections.on('active', function(panel, connection) {
941 $('.nick', that.$el).text(connection.get('nick'));
942 });
943 },
944
945 showNickChange: function (ev) {
946 (new _kiwi.view.NickChangeBox()).render();
947 },
948
949 process: function (ev) {
950 var that = this,
951 inp = $(ev.currentTarget),
952 inp_val = inp.val(),
953 meta;
954
955 if (navigator.appVersion.indexOf("Mac") !== -1) {
956 meta = ev.metaKey;
957 } else {
958 meta = ev.altKey;
959 }
960
961 // If not a tab key, reset the tabcomplete data
962 if (this.tabcomplete.active && ev.keyCode !== 9) {
963 this.tabcomplete.active = false;
964 this.tabcomplete.data = [];
965 this.tabcomplete.prefix = '';
966 }
967
968 switch (true) {
969 case (ev.keyCode === 13): // return
970 inp_val = inp_val.trim();
971
972 if (inp_val) {
973 $.each(inp_val.split('\n'), function (idx, line) {
974 that.processInput(line);
975 });
976
977 this.buffer.push(inp_val);
978 this.buffer_pos = this.buffer.length;
979 }
980
981 inp.val('');
982 return false;
983
984 break;
985
986 case (ev.keyCode === 38): // up
987 if (this.buffer_pos > 0) {
988 this.buffer_pos--;
989 inp.val(this.buffer[this.buffer_pos]);
990 }
991 break;
992
993 case (ev.keyCode === 40): // down
994 if (this.buffer_pos < this.buffer.length) {
995 this.buffer_pos++;
996 inp.val(this.buffer[this.buffer_pos]);
997 }
998 break;
999
1000 case (ev.keyCode === 219 && meta): // [ + meta
1001 _kiwi.app.panels.view.prev();
1002 return false;
1003
1004 case (ev.keyCode === 221 && meta): // ] + meta
1005 _kiwi.app.panels.view.next();
1006 return false;
1007
1008 case (ev.keyCode === 9): // tab
1009 this.tabcomplete.active = true;
1010 if (_.isEqual(this.tabcomplete.data, [])) {
1011 // Get possible autocompletions
1012 var ac_data = [],
1013 members = _kiwi.app.panels().active.get('members');
1014
1015 // If we have a members list, get the models. Otherwise empty array
1016 members = members ? members.models : [];
1017
1018 $.each(members, function (i, member) {
1019 if (!member) return;
1020 ac_data.push(member.get('nick'));
1021 });
1022
1023 ac_data.push(_kiwi.app.panels().active.get('name'));
1024
1025 ac_data = _.sortBy(ac_data, function (nick) {
1026 return nick;
1027 });
1028 this.tabcomplete.data = ac_data;
1029 }
1030
1031 if (inp_val[inp[0].selectionStart - 1] === ' ') {
1032 return false;
1033 }
1034
1035 (function () {
1036 var tokens, // Words before the cursor position
1037 val, // New value being built up
1038 p1, // Position in the value just before the nick
1039 newnick, // New nick to be displayed (cycles through)
1040 range, // TextRange for setting new text cursor position
1041 nick, // Current nick in the value
1042 trailing = ': '; // Text to be inserted after a tabbed nick
1043
1044 tokens = inp_val.substring(0, inp[0].selectionStart).split(' ');
1045 if (tokens[tokens.length-1] == ':')
1046 tokens.pop();
1047
1048 nick = tokens[tokens.length - 1];
1049
1050 if (this.tabcomplete.prefix === '') {
1051 this.tabcomplete.prefix = nick;
1052 }
1053
1054 this.tabcomplete.data = _.select(this.tabcomplete.data, function (n) {
1055 return (n.toLowerCase().indexOf(that.tabcomplete.prefix.toLowerCase()) === 0);
1056 });
1057
1058 if (this.tabcomplete.data.length > 0) {
1059 // Get the current value before cursor position
1060 p1 = inp[0].selectionStart - (nick.length);
1061 val = inp_val.substr(0, p1);
1062
1063 // Include the current selected nick
1064 newnick = this.tabcomplete.data.shift();
1065 this.tabcomplete.data.push(newnick);
1066 val += newnick;
1067
1068 if (inp_val.substr(inp[0].selectionStart, 2) !== trailing)
1069 val += trailing;
1070
1071 // Now include the rest of the current value
1072 val += inp_val.substr(inp[0].selectionStart);
1073
1074 inp.val(val);
1075
1076 // Move the cursor position to the end of the nick
1077 if (inp[0].setSelectionRange) {
1078 inp[0].setSelectionRange(p1 + newnick.length + trailing.length, p1 + newnick.length + trailing.length);
1079 } else if (inp[0].createTextRange) { // not sure if this bit is actually needed....
1080 range = inp[0].createTextRange();
1081 range.collapse(true);
1082 range.moveEnd('character', p1 + newnick.length + trailing.length);
1083 range.moveStart('character', p1 + newnick.length + trailing.length);
1084 range.select();
1085 }
1086 }
1087 }).apply(this);
1088 return false;
1089 }
1090 },
1091
1092
1093 processInput: function (command_raw) {
1094 var command, params,
1095 pre_processed;
1096
1097 // The default command
1098 if (command_raw[0] !== '/' || command_raw.substr(0, 2) === '//') {
1099 // Remove any slash escaping at the start (ie. //)
1100 command_raw = command_raw.replace(/^\/\//, '/');
1101
1102 // Prepend the default command
1103 command_raw = '/msg ' + _kiwi.app.panels().active.get('name') + ' ' + command_raw;
1104 }
1105
1106 // Process the raw command for any aliases
1107 this.preprocessor.vars.server = _kiwi.app.connections.active_connection.get('name');
1108 this.preprocessor.vars.channel = _kiwi.app.panels().active.get('name');
1109 this.preprocessor.vars.destination = this.preprocessor.vars.channel;
1110 command_raw = this.preprocessor.process(command_raw);
1111
1112 // Extract the command and parameters
1113 params = command_raw.split(' ');
1114 if (params[0][0] === '/') {
1115 command = params[0].substr(1).toLowerCase();
1116 params = params.splice(1, params.length - 1);
1117 } else {
1118 // Default command
1119 command = 'msg';
1120 params.unshift(_kiwi.app.panels().active.get('name'));
1121 }
1122
1123 // Trigger the command events
1124 this.trigger('command', {command: command, params: params});
1125 this.trigger('command:' + command, {command: command, params: params});
1126
1127 // If we didn't have any listeners for this event, fire a special case
1128 // TODO: This feels dirty. Should this really be done..?
1129 if (!this._callbacks['command:' + command]) {
1130 this.trigger('unknown_command', {command: command, params: params});
1131 }
1132 },
1133
1134
1135 addPluginIcon: function ($icon) {
1136 var $tool = $('<div class="tool"></div>').append($icon);
1137 this.$el.find('.input_tools').append($tool);
1138 _kiwi.app.view.doLayout();
1139 }
1140 });
1141
1142
1143
1144
1145 _kiwi.view.StatusMessage = Backbone.View.extend({
1146 initialize: function () {
1147 this.$el.hide();
1148
1149 // Timer for hiding the message after X seconds
1150 this.tmr = null;
1151 },
1152
1153 text: function (text, opt) {
1154 // Defaults
1155 opt = opt || {};
1156 opt.type = opt.type || '';
1157 opt.timeout = opt.timeout || 5000;
1158
1159 this.$el.text(text).attr('class', opt.type);
1160 this.$el.slideDown($.proxy(_kiwi.app.view.doLayout, this));
1161
1162 if (opt.timeout) this.doTimeout(opt.timeout);
1163 },
1164
1165 html: function (html, opt) {
1166 // Defaults
1167 opt = opt || {};
1168 opt.type = opt.type || '';
1169 opt.timeout = opt.timeout || 5000;
1170
1171 this.$el.html(text).attr('class', opt.type);
1172 this.$el.slideDown(_kiwi.app.view.doLayout);
1173
1174 if (opt.timeout) this.doTimeout(opt.timeout);
1175 },
1176
1177 hide: function () {
1178 this.$el.slideUp($.proxy(_kiwi.app.view.doLayout, this));
1179 },
1180
1181 doTimeout: function (length) {
1182 if (this.tmr) clearTimeout(this.tmr);
1183 var that = this;
1184 this.tmr = setTimeout(function () { that.hide(); }, length);
1185 }
1186 });
1187
1188
1189
1190
1191 _kiwi.view.ResizeHandler = Backbone.View.extend({
1192 events: {
1193 'mousedown': 'startDrag',
1194 'mouseup': 'stopDrag'
1195 },
1196
1197 initialize: function () {
1198 this.dragging = false;
1199 this.starting_width = {};
1200
1201 $(window).on('mousemove', $.proxy(this.onDrag, this));
1202 },
1203
1204 startDrag: function (event) {
1205 this.dragging = true;
1206 },
1207
1208 stopDrag: function (event) {
1209 this.dragging = false;
1210 },
1211
1212 onDrag: function (event) {
1213 if (!this.dragging) return;
1214
1215 this.$el.css('left', event.clientX - (this.$el.outerWidth(true) / 2));
1216 $('#memberlists').css('width', this.$el.parent().width() - (this.$el.position().left + this.$el.outerWidth()));
1217 _kiwi.app.view.doLayout();
1218 }
1219 });
1220
1221
1222
1223 _kiwi.view.AppToolbar = Backbone.View.extend({
1224 events: {
1225 'click .settings': 'clickSettings'
1226 },
1227
1228 initialize: function () {
1229 },
1230
1231 clickSettings: function (event) {
1232 _kiwi.app.controlbox.processInput('/settings');
1233 }
1234 });
1235
1236
1237
1238 _kiwi.view.Application = Backbone.View.extend({
1239 initialize: function () {
1240 var that = this;
1241
1242 $(window).resize(function() { that.doLayout.apply(that); });
1243 $('#toolbar').resize(function() { that.doLayout.apply(that); });
1244 $('#controlbox').resize(function() { that.doLayout.apply(that); });
1245
1246 // Change the theme when the config is changed
1247 _kiwi.global.settings.on('change:theme', this.updateTheme, this);
1248 this.updateTheme(getQueryVariable('theme'));
1249
1250 _kiwi.global.settings.on('change:channel_list_style', this.setTabLayout, this);
1251 this.setTabLayout(_kiwi.global.settings.get('channel_list_style'));
1252
1253 _kiwi.global.settings.on('change:show_timestamps', this.displayTimestamps, this);
1254 this.displayTimestamps(_kiwi.global.settings.get('show_timestamps'));
1255
1256 this.doLayout();
1257
1258 $(document).keydown(this.setKeyFocus);
1259
1260 // Confirmation require to leave the page
1261 window.onbeforeunload = function () {
1262 if (_kiwi.gateway.isConnected()) {
1263 return 'This will close all KiwiIRC conversations. Are you sure you want to close this window?';
1264 }
1265 };
1266
1267 this.initSound();
1268 },
1269
1270
1271
1272 updateTheme: function (theme_name) {
1273 // If called by the settings callback, get the correct new_value
1274 if (theme_name === _kiwi.global.settings) {
1275 theme_name = arguments[1];
1276 }
1277
1278 // If we have no theme specified, get it from the settings
1279 if (!theme_name) theme_name = _kiwi.global.settings.get('theme');
1280
1281 // Clear any current theme
1282 this.$el.removeClass(function (i, css) {
1283 return (css.match(/\btheme_\S+/g) || []).join(' ');
1284 });
1285
1286 // Apply the new theme
1287 this.$el.addClass('theme_' + (theme_name || 'relaxed'));
1288 },
1289
1290
1291 setTabLayout: function (layout_style) {
1292 // If called by the settings callback, get the correct new_value
1293 if (layout_style === _kiwi.global.settings) {
1294 layout_style = arguments[1];
1295 }
1296
1297 if (layout_style == 'list') {
1298 this.$el.addClass('chanlist_treeview');
1299 } else {
1300 this.$el.removeClass('chanlist_treeview');
1301 }
1302
1303 this.doLayout();
1304 },
1305
1306
1307 displayTimestamps: function (show_timestamps) {
1308 // If called by the settings callback, get the correct new_value
1309 if (show_timestamps === _kiwi.global.settings) {
1310 show_timestamps = arguments[1];
1311 }
1312
1313 if (show_timestamps) {
1314 this.$el.addClass('timestamps');
1315 } else {
1316 this.$el.removeClass('timestamps');
1317 }
1318 },
1319
1320
1321 // Globally shift focus to the command input box on a keypress
1322 setKeyFocus: function (ev) {
1323 // If we're copying text, don't shift focus
1324 if (ev.ctrlKey || ev.altKey || ev.metaKey) {
1325 return;
1326 }
1327
1328 // If we're typing into an input box somewhere, ignore
1329 if ((ev.target.tagName.toLowerCase() === 'input') || (ev.target.tagName.toLowerCase() === 'textarea') || $(ev.target).attr('contenteditable')) {
1330 return;
1331 }
1332
1333 $('#controlbox .inp').focus();
1334 },
1335
1336
1337 doLayout: function () {
1338 var el_kiwi = this.$el;
1339 var el_panels = $('#kiwi #panels');
1340 var el_memberlists = $('#kiwi #memberlists');
1341 var el_toolbar = $('#kiwi #toolbar');
1342 var el_controlbox = $('#kiwi #controlbox');
1343 var el_resize_handle = $('#kiwi #memberlists_resize_handle');
1344
1345 var css_heights = {
1346 top: el_toolbar.outerHeight(true),
1347 bottom: el_controlbox.outerHeight(true)
1348 };
1349
1350
1351 // If any elements are not visible, full size the panals instead
1352 if (!el_toolbar.is(':visible')) {
1353 css_heights.top = 0;
1354 }
1355
1356 if (!el_controlbox.is(':visible')) {
1357 css_heights.bottom = 0;
1358 }
1359
1360 // Apply the CSS sizes
1361 el_panels.css(css_heights);
1362 el_memberlists.css(css_heights);
1363 el_resize_handle.css(css_heights);
1364
1365 // If we have channel tabs on the side, adjust the height
1366 if (el_kiwi.hasClass('chanlist_treeview')) {
1367 $('#tabs', el_kiwi).css(css_heights);
1368 }
1369
1370 // Determine if we have a narrow window (mobile/tablet/or even small desktop window)
1371 if (el_kiwi.outerWidth() < 400) {
1372 el_kiwi.addClass('narrow');
1373 } else {
1374 el_kiwi.removeClass('narrow');
1375 }
1376
1377 // Set the panels width depending on the memberlist visibility
1378 if (el_memberlists.css('display') != 'none') {
1379 // Panels to the side of the memberlist
1380 el_panels.css('right', el_memberlists.outerWidth(true));
1381 // The resize handle sits overlapping the panels and memberlist
1382 el_resize_handle.css('left', el_memberlists.position().left - (el_resize_handle.outerWidth(true) / 2));
1383 } else {
1384 // Memberlist is hidden so panels to the right edge
1385 el_panels.css('right', 0);
1386 // And move the handle just out of sight to the right
1387 el_resize_handle.css('left', el_panels.outerWidth(true));
1388 }
1389
1390 var input_wrap_width = parseInt($('#kiwi #controlbox .input_tools').outerWidth());
1391 el_controlbox.find('.input_wrap').css('right', input_wrap_width + 7);
1392 },
1393
1394
1395 alertWindow: function (title) {
1396 if (!this.alertWindowTimer) {
1397 this.alertWindowTimer = new (function () {
1398 var that = this;
1399 var tmr;
1400 var has_focus = true;
1401 var state = 0;
1402 var default_title = 'Kiwi IRC';
1403 var title = 'Kiwi IRC';
1404
1405 this.setTitle = function (new_title) {
1406 new_title = new_title || default_title;
1407 window.document.title = new_title;
1408 return new_title;
1409 };
1410
1411 this.start = function (new_title) {
1412 // Don't alert if we already have focus
1413 if (has_focus) return;
1414
1415 title = new_title;
1416 if (tmr) return;
1417 tmr = setInterval(this.update, 1000);
1418 };
1419
1420 this.stop = function () {
1421 // Stop the timer and clear the title
1422 if (tmr) clearInterval(tmr);
1423 tmr = null;
1424 this.setTitle();
1425
1426 // Some browsers don't always update the last title correctly
1427 // Wait a few seconds and then reset
1428 setTimeout(this.reset, 2000);
1429 };
1430
1431 this.reset = function () {
1432 if (tmr) return;
1433 that.setTitle();
1434 };
1435
1436
1437 this.update = function () {
1438 if (state === 0) {
1439 that.setTitle(title);
1440 state = 1;
1441 } else {
1442 that.setTitle();
1443 state = 0;
1444 }
1445 };
1446
1447 $(window).focus(function (event) {
1448 has_focus = true;
1449 that.stop();
1450
1451 // Some browsers don't always update the last title correctly
1452 // Wait a few seconds and then reset
1453 setTimeout(that.reset, 2000);
1454 });
1455
1456 $(window).blur(function (event) {
1457 has_focus = false;
1458 });
1459 })();
1460 }
1461
1462 this.alertWindowTimer.start(title);
1463 },
1464
1465
1466 barsHide: function (instant) {
1467 var that = this;
1468
1469 if (!instant) {
1470 $('#toolbar').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
1471 $('#controlbox').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
1472 } else {
1473 $('#toolbar').slideUp(0);
1474 $('#controlbox').slideUp(0);
1475 this.doLayout();
1476 }
1477 },
1478
1479 barsShow: function (instant) {
1480 var that = this;
1481
1482 if (!instant) {
1483 $('#toolbar').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
1484 $('#controlbox').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
1485 } else {
1486 $('#toolbar').slideDown(0);
1487 $('#controlbox').slideDown(0);
1488 this.doLayout();
1489 }
1490 },
1491
1492
1493 initSound: function () {
1494 var that = this,
1495 base_path = this.model.get('base_path');
1496
1497 $script(base_path + '/assets/libs/soundmanager2/soundmanager2-nodebug-jsmin.js', function() {
1498 if (typeof soundManager === 'undefined')
1499 return;
1500
1501 soundManager.setup({
1502 url: base_path + '/assets/libs/soundmanager2/',
1503 flashVersion: 9, // optional: shiny features (default = 8)// optional: ignore Flash where possible, use 100% HTML5 mode
1504 preferFlash: true,
1505
1506 onready: function() {
1507 that.sound_object = soundManager.createSound({
1508 id: 'highlight',
1509 url: base_path + '/assets/sound/highlight.mp3'
1510 });
1511 }
1512 });
1513 });
1514 },
1515
1516
1517 playSound: function (sound_id) {
1518 if (!this.sound_object) return;
1519
1520 if (_kiwi.global.settings.get('mute_sounds'))
1521 return;
1522
1523 soundManager.play(sound_id);
1524 }
1525 });
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535 _kiwi.view.MediaMessage = Backbone.View.extend({
1536 events: {
1537 'click .media_close': 'close'
1538 },
1539
1540 initialize: function () {
1541 // Get the URL from the data
1542 this.url = this.$el.data('url');
1543 },
1544
1545 // Close the media content and remove it from display
1546 close: function () {
1547 var that = this;
1548 this.$content.slideUp('fast', function () {
1549 that.$content.remove();
1550 });
1551 },
1552
1553 // Open the media content within its wrapper
1554 open: function () {
1555 // Create the content div if we haven't already
1556 if (!this.$content) {
1557 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>');
1558 this.$content.find('.content').append(this.mediaTypes[this.$el.data('type')].apply(this, []) || 'Not found :(');
1559 }
1560
1561 // Now show the content if not already
1562 if (!this.$content.is(':visible')) {
1563 // Hide it first so the slideDown always plays
1564 this.$content.hide();
1565
1566 // Add the media content and slide it into view
1567 this.$el.append(this.$content);
1568 this.$content.slideDown();
1569 }
1570 },
1571
1572
1573
1574 // Generate the media content for each recognised type
1575 mediaTypes: {
1576 twitter: function () {
1577 var tweet_id = this.$el.data('tweetid');
1578 var that = this;
1579
1580 $.getJSON('https://api.twitter.com/1/statuses/oembed.json?id=' + tweet_id + '&callback=?', function (data) {
1581 that.$content.find('.content').html(data.html);
1582 });
1583
1584 return $('<div>Loading tweet..</div>');
1585 },
1586
1587
1588 image: function () {
1589 return $('<a href="' + this.url + '" target="_blank"><img height="100" src="' + this.url + '" /></a>');
1590 },
1591
1592
1593 reddit: function () {
1594 var that = this;
1595 var matches = (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(this.url);
1596
1597 $.getJSON('http://www.' + matches[0] + '.json?jsonp=?', function (data) {
1598 console.log('Loaded reddit data', data);
1599 var post = data[0].data.children[0].data;
1600 var thumb = '';
1601
1602 // Show a thumbnail if there is one
1603 if (post.thumbnail) {
1604 //post.thumbnail = 'http://www.eurotunnel.com/uploadedImages/commercial/back-steps-icon-arrow.png';
1605
1606 // Hide the thumbnail if an over_18 image
1607 if (post.over_18) {
1608 thumb = '<span class="thumbnail_nsfw" onclick="$(this).find(\'p\').remove(); $(this).find(\'img\').css(\'visibility\', \'visible\');">';
1609 thumb += '<p style="font-size:0.9em;line-height:1.2em;cursor:pointer;">Show<br />NSFW</p>';
1610 thumb += '<img src="' + post.thumbnail + '" class="thumbnail" style="visibility:hidden;" />';
1611 thumb += '</span>';
1612 } else {
1613 thumb = '<img src="' + post.thumbnail + '" class="thumbnail" />';
1614 }
1615 }
1616
1617 // Build the template string up
1618 var tmpl = '<div>' + thumb + '<b><%- title %></b><br />Posted by <%- author %>. &nbsp;&nbsp; ';
1619 tmpl += '<i class="icon-arrow-up"></i> <%- ups %> &nbsp;&nbsp; <i class="icon-arrow-down"></i> <%- downs %><br />';
1620 tmpl += '<%- num_comments %> comments made. <a href="http://www.reddit.com<%- permalink %>">View post</a></div>';
1621
1622 that.$content.find('.content').html(_.template(tmpl, post));
1623 });
1624
1625 return $('<div>Loading Reddit thread..</div>');
1626 }
1627 }
1628
1629 }, {
1630
1631 // Build the closed media HTML from a URL
1632 buildHtml: function (url) {
1633 var html = '', matches;
1634
1635 // Is it an image?
1636 if (url.match(/(\.jpe?g|\.gif|\.bmp|\.png)\??$/i)) {
1637 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>';
1638 }
1639
1640 // Is it a tweet?
1641 matches = (/https?:\/\/twitter.com\/([a-zA-Z0-9_]+)\/status\/([0-9]+)/ig).exec(url);
1642 if (matches) {
1643 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>';
1644 }
1645
1646 // Is reddit?
1647 matches = (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(url);
1648 if (matches) {
1649 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>';
1650 }
1651
1652 return html;
1653 }
1654 });
1655
1656
1657
1658 _kiwi.view.MenuBox = Backbone.View.extend({
1659 events: {
1660 'click .ui_menu_foot .close': 'dispose'
1661 },
1662
1663 initialize: function(title) {
1664 var that = this;
1665
1666 this.$el = $('<div class="ui_menu"></div>');
1667
1668 this._title = title || '';
1669 this._items = {};
1670 this._display_footer = true;
1671 this._close_on_blur = true;
1672
1673 this._close_proxy = function(event) {
1674 that.onDocumentClick(event);
1675 };
1676 $(document).on('click', this._close_proxy);
1677 },
1678
1679
1680 render: function() {
1681 var that = this;
1682
1683 this.$el.find('*').remove();
1684
1685 if (this._title) {
1686 $('<div class="ui_menu_title"></div>')
1687 .text(this._title)
1688 .appendTo(this.$el);
1689 }
1690
1691
1692 _.each(this._items, function(item) {
1693 var $item = $('<div class="ui_menu_content hover"></div>')
1694 .append(item);
1695
1696 that.$el.append($item);
1697 });
1698
1699 if (this._display_footer)
1700 this.$el.append('<div class="ui_menu_foot"><a class="close" onclick="">Close <i class="icon-remove"></i></a></div>');
1701 },
1702
1703
1704 onDocumentClick: function(event) {
1705 var $target = $(event.target);
1706
1707 if (!this._close_on_blur)
1708 return;
1709
1710 // If this is not itself AND we don't contain this element, dispose $el
1711 if ($target[0] != this.$el[0] && this.$el.has($target).length === 0)
1712 this.dispose();
1713 },
1714
1715
1716 dispose: function() {
1717 _.each(this._items, function(item) {
1718 item.dispose && item.dispose();
1719 item.remove && item.remove();
1720 });
1721
1722 this._items = null;
1723 this.remove();
1724
1725 $(document).off('click', this._close_proxy);
1726 },
1727
1728
1729 addItem: function(item_name, $item) {
1730 $item = $($item);
1731 if ($item.is('a')) $item.addClass('icon-chevron-right');
1732 this._items[item_name] = $item;
1733 },
1734
1735
1736 removeItem: function(item_name) {
1737 delete this._items[item_name];
1738 },
1739
1740
1741 showFooter: function(show) {
1742 this._display_footer = show;
1743 },
1744
1745
1746 closeOnBlur: function(close_it) {
1747 this._close_on_blur = close_it;
1748 },
1749
1750
1751 show: function() {
1752 this.render();
1753 this.$el.appendTo(_kiwi.app.view.$el);
1754 }
1755 });