| 1 | _kiwi.view.Channel = _kiwi.view.Panel.extend({ |
| 2 | events: function(){ |
| 3 | var parent_events = this.constructor.__super__.events; |
| 4 | |
| 5 | if(_.isFunction(parent_events)){ |
| 6 | parent_events = parent_events(); |
| 7 | } |
| 8 | return _.extend({}, parent_events, { |
| 9 | 'click .msg .nick' : 'nickClick', |
| 10 | 'click .msg .inline-nick' : 'nickClick', |
| 11 | "click .chan": "chanClick", |
| 12 | 'click .media .open': 'mediaClick', |
| 13 | 'mouseenter .msg .nick': 'msgEnter', |
| 14 | 'mouseleave .msg .nick': 'msgLeave' |
| 15 | }); |
| 16 | }, |
| 17 | |
| 18 | initialize: function (options) { |
| 19 | this.initializePanel(options); |
| 20 | |
| 21 | // Container for all the messages |
| 22 | this.$messages = $('<div class="messages"></div>'); |
| 23 | this.$el.append(this.$messages); |
| 24 | |
| 25 | this.model.bind('change:topic', this.topic, this); |
| 26 | this.model.bind('change:topic_set_by', this.topicSetBy, this); |
| 27 | |
| 28 | if (this.model.get('members')) { |
| 29 | // When we join the memberlist, we have officially joined the channel |
| 30 | this.model.get('members').bind('add', function (member) { |
| 31 | if (member.get('nick') === this.model.collection.network.get('nick')) { |
| 32 | this.$el.find('.initial_loader').slideUp(function () { |
| 33 | $(this).remove(); |
| 34 | }); |
| 35 | } |
| 36 | }, this); |
| 37 | |
| 38 | // Memberlist reset with a new nicklist? Consider we have joined |
| 39 | this.model.get('members').bind('reset', function(members) { |
| 40 | if (members.getByNick(this.model.collection.network.get('nick'))) { |
| 41 | this.$el.find('.initial_loader').slideUp(function () { |
| 42 | $(this).remove(); |
| 43 | }); |
| 44 | } |
| 45 | }, this); |
| 46 | } |
| 47 | |
| 48 | // Only show the loader if this is a channel (ie. not a query) |
| 49 | if (this.model.isChannel()) { |
| 50 | this.$el.append('<div class="initial_loader" style="margin:1em;text-align:center;"> ' + _kiwi.global.i18n.translate('client_views_channel_joining').fetch() + ' <span class="loader"></span></div>'); |
| 51 | } |
| 52 | |
| 53 | this.model.bind('msg', this.newMsg, this); |
| 54 | this.msg_count = 0; |
| 55 | }, |
| 56 | |
| 57 | |
| 58 | render: function () { |
| 59 | var that = this; |
| 60 | |
| 61 | this.$messages.empty(); |
| 62 | _.each(this.model.get('scrollback'), function (msg) { |
| 63 | that.newMsg(msg); |
| 64 | }); |
| 65 | }, |
| 66 | |
| 67 | |
| 68 | newMsg: function(msg) { |
| 69 | |
| 70 | // Parse the msg object into properties fit for displaying |
| 71 | msg = this.generateMessageDisplayObj(msg); |
| 72 | |
| 73 | _kiwi.global.events.emit('message:display', {panel: this.model, message: msg}) |
| 74 | .then(_.bind(function() { |
| 75 | var line_msg; |
| 76 | |
| 77 | // Format the nick to the config defined format |
| 78 | var display_obj = _.clone(msg); |
| 79 | display_obj.nick = styleText('message_nick', {nick: msg.nick, prefix: msg.nick_prefix || ''}); |
| 80 | |
| 81 | line_msg = '<div class="msg <%= type %> <%= css_classes %>"><div class="time"><%- time_string %></div><div class="nick" style="<%= nick_style %>"><%- nick %></div><div class="text" style="<%= style %>"><%= msg %> </div></div>'; |
| 82 | this.$messages.append($(_.template(line_msg, display_obj)).data('message', msg)); |
| 83 | |
| 84 | // Activity/alerts based on the type of new message |
| 85 | if (msg.type.match(/^action /)) { |
| 86 | this.alert('action'); |
| 87 | |
| 88 | } else if (msg.is_highlight) { |
| 89 | _kiwi.app.view.alertWindow('* ' + _kiwi.global.i18n.translate('client_views_panel_activity').fetch()); |
| 90 | _kiwi.app.view.favicon.newHighlight(); |
| 91 | _kiwi.app.view.playSound('highlight'); |
| 92 | _kiwi.app.view.showNotification(this.model.get('name'), msg.unparsed_msg); |
| 93 | this.alert('highlight'); |
| 94 | |
| 95 | } else { |
| 96 | // If this is the active panel, send an alert out |
| 97 | if (this.model.isActive()) { |
| 98 | _kiwi.app.view.alertWindow('* ' + _kiwi.global.i18n.translate('client_views_panel_activity').fetch()); |
| 99 | } |
| 100 | this.alert('activity'); |
| 101 | } |
| 102 | |
| 103 | if (this.model.isQuery() && !this.model.isActive()) { |
| 104 | _kiwi.app.view.alertWindow('* ' + _kiwi.global.i18n.translate('client_views_panel_activity').fetch()); |
| 105 | |
| 106 | // Highlights have already been dealt with above |
| 107 | if (!msg.is_highlight) { |
| 108 | _kiwi.app.view.favicon.newHighlight(); |
| 109 | } |
| 110 | |
| 111 | _kiwi.app.view.showNotification(this.model.get('name'), msg.unparsed_msg); |
| 112 | _kiwi.app.view.playSound('highlight'); |
| 113 | } |
| 114 | |
| 115 | // Update the activity counters |
| 116 | (function () { |
| 117 | // Only inrement the counters if we're not the active panel |
| 118 | if (this.model.isActive()) return; |
| 119 | |
| 120 | var count_all_activity = _kiwi.global.settings.get('count_all_activity'), |
| 121 | exclude_message_types, new_count; |
| 122 | |
| 123 | // Set the default config value |
| 124 | if (typeof count_all_activity === 'undefined') { |
| 125 | count_all_activity = false; |
| 126 | } |
| 127 | |
| 128 | // Do not increment the counter for these message types |
| 129 | exclude_message_types = [ |
| 130 | 'action join', |
| 131 | 'action quit', |
| 132 | 'action part', |
| 133 | 'action kick', |
| 134 | 'action nick', |
| 135 | 'action mode' |
| 136 | ]; |
| 137 | |
| 138 | if (count_all_activity || _.indexOf(exclude_message_types, msg.type) === -1) { |
| 139 | new_count = this.model.get('activity_counter') || 0; |
| 140 | new_count++; |
| 141 | this.model.set('activity_counter', new_count); |
| 142 | } |
| 143 | |
| 144 | }).apply(this); |
| 145 | |
| 146 | if(this.model.isActive()) this.scrollToBottom(); |
| 147 | |
| 148 | // Make sure our DOM isn't getting too large (Acts as scrollback) |
| 149 | this.msg_count++; |
| 150 | if (this.msg_count > (parseInt(_kiwi.global.settings.get('scrollback'), 10) || 250)) { |
| 151 | $('.msg:first', this.$messages).remove(); |
| 152 | this.msg_count--; |
| 153 | } |
| 154 | }, this)); |
| 155 | }, |
| 156 | |
| 157 | |
| 158 | // Let nicks be clickable + colourise within messages |
| 159 | parseMessageNicks: function(word, colourise) { |
| 160 | var members, member, colour = ''; |
| 161 | |
| 162 | members = this.model.get('members'); |
| 163 | if (!members) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | member = members.getByNick(word); |
| 168 | if (!member) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (colourise !== false) { |
| 173 | // Use the nick from the member object so the colour matches the letter casing |
| 174 | colour = this.getNickColour(member.get('nick')); |
| 175 | colour = 'color:' + colour; |
| 176 | } |
| 177 | |
| 178 | return _.template('<span class="inline-nick" style="<%- colour %>;cursor:pointer;" data-nick="<%- nick %>"><%- nick %></span>', { |
| 179 | nick: word, |
| 180 | colour: colour |
| 181 | }); |
| 182 | |
| 183 | }, |
| 184 | |
| 185 | |
| 186 | // Make channels clickable |
| 187 | parseMessageChannels: function(word) { |
| 188 | var re, |
| 189 | parsed = false, |
| 190 | network = this.model.get('network'); |
| 191 | |
| 192 | if (!network) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | re = new RegExp('(^|\\s)([' + escapeRegex(network.get('channel_prefix')) + '][^ ,\\007]+)', 'g'); |
| 197 | |
| 198 | if (!word.match(re)) { |
| 199 | return parsed; |
| 200 | } |
| 201 | |
| 202 | parsed = word.replace(re, function (m1, m2) { |
| 203 | return m2 + '<a class="chan" data-channel="' + _.escape(m1.trim()) + '">' + _.escape(m1.trim()) + '</a>'; |
| 204 | }); |
| 205 | |
| 206 | return parsed; |
| 207 | }, |
| 208 | |
| 209 | |
| 210 | parseMessageUrls: function(word) { |
| 211 | var found_a_url = false, |
| 212 | parsed_url; |
| 213 | |
| 214 | parsed_url = word.replace(/^(([A-Za-z][A-Za-z0-9\-]*\:\/\/)|(www\.))([\w.\-]+)([a-zA-Z]{2,6})(:[0-9]+)?(\/[\w!:.?$'()[\]*,;~+=&%@!\-\/]*)?(#.*)?$/gi, function (url) { |
| 215 | var nice = url, |
| 216 | extra_html = ''; |
| 217 | |
| 218 | // Don't allow javascript execution |
| 219 | if (url.match(/^javascript:/)) { |
| 220 | return url; |
| 221 | } |
| 222 | |
| 223 | found_a_url = true; |
| 224 | |
| 225 | // Add the http if no protoocol was found |
| 226 | if (url.match(/^www\./)) { |
| 227 | url = 'http://' + url; |
| 228 | } |
| 229 | |
| 230 | // Shorten the displayed URL if it's going to be too long |
| 231 | if (nice.length > 100) { |
| 232 | nice = nice.substr(0, 100) + '...'; |
| 233 | } |
| 234 | |
| 235 | // Get any media HTML if supported |
| 236 | extra_html = _kiwi.view.MediaMessage.buildHtml(url); |
| 237 | |
| 238 | // Make the link clickable |
| 239 | return '<a class="link_ext" target="_blank" rel="nofollow" href="' + url.replace(/"/g, '%22') + '">' + _.escape(nice) + '</a>' + extra_html; |
| 240 | }); |
| 241 | |
| 242 | return found_a_url ? parsed_url : false; |
| 243 | }, |
| 244 | |
| 245 | |
| 246 | // Get a colour from a nick (Method based on IRSSIs nickcolor.pl) |
| 247 | getNickColour: function(nick) { |
| 248 | var nick_int = 0, rgb; |
| 249 | |
| 250 | _.map(nick.split(''), function (i) { nick_int += i.charCodeAt(0); }); |
| 251 | rgb = hsl2rgb(nick_int % 255, 70, 35); |
| 252 | rgb = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16); |
| 253 | |
| 254 | return '#' + rgb.toString(16); |
| 255 | }, |
| 256 | |
| 257 | |
| 258 | // Takes an IRC message object and parses it for displaying |
| 259 | generateMessageDisplayObj: function(msg) { |
| 260 | var nick_hex, time_difference, |
| 261 | message_words, |
| 262 | sb = this.model.get('scrollback'), |
| 263 | prev_msg = sb[sb.length-2], |
| 264 | hour, pm, am_pm_locale_key; |
| 265 | |
| 266 | // Clone the msg object so we dont modify the original |
| 267 | msg = _.clone(msg); |
| 268 | |
| 269 | // Defaults |
| 270 | msg.css_classes = ''; |
| 271 | msg.nick_style = ''; |
| 272 | msg.is_highlight = false; |
| 273 | msg.time_string = ''; |
| 274 | |
| 275 | |
| 276 | // Nick highlight detecting |
| 277 | var nick = _kiwi.app.connections.active_connection.get('nick'); |
| 278 | if ((new RegExp('(^|\\W)(' + escapeRegex(nick) + ')(\\W|$)', 'i')).test(msg.msg)) { |
| 279 | // Do not highlight the user's own input |
| 280 | if (msg.nick.localeCompare(nick) !== 0) { |
| 281 | msg.is_highlight = true; |
| 282 | msg.css_classes += ' highlight'; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | message_words = msg.msg.split(' '); |
| 287 | message_words = _.map(message_words, function(word) { |
| 288 | var parsed_word; |
| 289 | |
| 290 | parsed_word = this.parseMessageUrls(word); |
| 291 | if (typeof parsed_word === 'string') return parsed_word; |
| 292 | |
| 293 | parsed_word = this.parseMessageChannels(word); |
| 294 | if (typeof parsed_word === 'string') return parsed_word; |
| 295 | |
| 296 | parsed_word = this.parseMessageNicks(word, (msg.type === 'privmsg')); |
| 297 | if (typeof parsed_word === 'string') return parsed_word; |
| 298 | |
| 299 | parsed_word = _.escape(word); |
| 300 | |
| 301 | // Replace text emoticons with images |
| 302 | if (_kiwi.global.settings.get('show_emoticons')) { |
| 303 | parsed_word = emoticonFromText(parsed_word); |
| 304 | } |
| 305 | |
| 306 | return parsed_word; |
| 307 | }, this); |
| 308 | |
| 309 | msg.unparsed_msg = msg.msg; |
| 310 | msg.msg = message_words.join(' '); |
| 311 | |
| 312 | // Convert IRC formatting into HTML formatting |
| 313 | msg.msg = formatIRCMsg(msg.msg); |
| 314 | |
| 315 | // Add some colours to the nick |
| 316 | msg.nick_style = 'color:' + this.getNickColour(msg.nick) + ';'; |
| 317 | |
| 318 | // Generate a hex string from the nick to be used as a CSS class name |
| 319 | nick_hex = ''; |
| 320 | if (msg.nick) { |
| 321 | _.map(msg.nick.split(''), function (char) { |
| 322 | nick_hex += char.charCodeAt(0).toString(16); |
| 323 | }); |
| 324 | msg.css_classes += ' nick_' + nick_hex; |
| 325 | } |
| 326 | |
| 327 | if (prev_msg) { |
| 328 | // Time difference between this message and the last (in minutes) |
| 329 | time_difference = (msg.time.getTime() - prev_msg.time.getTime())/1000/60; |
| 330 | if (prev_msg.nick === msg.nick && time_difference < 1) { |
| 331 | msg.css_classes += ' repeated_nick'; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Build up and add the line |
| 336 | if (_kiwi.global.settings.get('use_24_hour_timestamps')) { |
| 337 | msg.time_string = msg.time.getHours().toString().lpad(2, "0") + ":" + msg.time.getMinutes().toString().lpad(2, "0") + ":" + msg.time.getSeconds().toString().lpad(2, "0"); |
| 338 | } else { |
| 339 | hour = msg.time.getHours(); |
| 340 | pm = hour > 11; |
| 341 | |
| 342 | hour = hour % 12; |
| 343 | if (hour === 0) |
| 344 | hour = 12; |
| 345 | |
| 346 | am_pm_locale_key = pm ? |
| 347 | 'client_views_panel_timestamp_pm' : |
| 348 | 'client_views_panel_timestamp_am'; |
| 349 | |
| 350 | msg.time_string = translateText(am_pm_locale_key, hour + ":" + msg.time.getMinutes().toString().lpad(2, "0") + ":" + msg.time.getSeconds().toString().lpad(2, "0")); |
| 351 | } |
| 352 | |
| 353 | return msg; |
| 354 | }, |
| 355 | |
| 356 | |
| 357 | topic: function (topic) { |
| 358 | if (typeof topic !== 'string' || !topic) { |
| 359 | topic = this.model.get("topic"); |
| 360 | } |
| 361 | |
| 362 | this.model.addMsg('', styleText('channel_topic', {text: topic, channel: this.model.get('name')}), 'topic'); |
| 363 | |
| 364 | // If this is the active channel then update the topic bar |
| 365 | if (_kiwi.app.panels().active === this.model) { |
| 366 | _kiwi.app.topicbar.setCurrentTopicFromChannel(this.model); |
| 367 | } |
| 368 | }, |
| 369 | |
| 370 | topicSetBy: function (topic) { |
| 371 | // If this is the active channel then update the topic bar |
| 372 | if (_kiwi.app.panels().active === this.model) { |
| 373 | _kiwi.app.topicbar.setCurrentTopicFromChannel(this.model); |
| 374 | } |
| 375 | }, |
| 376 | |
| 377 | // Click on a nickname |
| 378 | nickClick: function (event) { |
| 379 | var $target = $(event.currentTarget), |
| 380 | nick, |
| 381 | members = this.model.get('members'), |
| 382 | member; |
| 383 | |
| 384 | event.stopPropagation(); |
| 385 | |
| 386 | // Check this current element for a nick before resorting to the main message |
| 387 | // (eg. inline nicks has the nick on its own element within the message) |
| 388 | nick = $target.data('nick'); |
| 389 | if (!nick) { |
| 390 | nick = $target.parent('.msg').data('message').nick; |
| 391 | } |
| 392 | |
| 393 | // Make sure this nick is still in the channel |
| 394 | member = members ? members.getByNick(nick) : null; |
| 395 | if (!member) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | _kiwi.global.events.emit('nick:select', {target: $target, member: member, source: 'message'}) |
| 400 | .then(_.bind(this.openUserMenuForNick, this, $target, member)); |
| 401 | }, |
| 402 | |
| 403 | |
| 404 | updateLastSeenMarker: function() { |
| 405 | if (this.model.isActive()) { |
| 406 | // Remove the previous last seen classes |
| 407 | this.$(".last_seen").removeClass("last_seen"); |
| 408 | |
| 409 | // Mark the last message the user saw |
| 410 | this.$messages.children().last().addClass("last_seen"); |
| 411 | } |
| 412 | }, |
| 413 | |
| 414 | |
| 415 | openUserMenuForNick: function ($target, member) { |
| 416 | var members = this.model.get('members'), |
| 417 | are_we_an_op = !!members.getByNick(_kiwi.app.connections.active_connection.get('nick')).get('is_op'), |
| 418 | userbox, menubox; |
| 419 | |
| 420 | userbox = new _kiwi.view.UserBox(); |
| 421 | userbox.setTargets(member, this.model); |
| 422 | userbox.displayOpItems(are_we_an_op); |
| 423 | |
| 424 | menubox = new _kiwi.view.MenuBox(member.get('nick') || 'User'); |
| 425 | menubox.addItem('userbox', userbox.$el); |
| 426 | menubox.showFooter(false); |
| 427 | |
| 428 | _kiwi.global.events.emit('usermenu:created', {menu: menubox, userbox: userbox, user: member}) |
| 429 | .then(_.bind(function() { |
| 430 | menubox.show(); |
| 431 | |
| 432 | // Position the userbox + menubox |
| 433 | var target_offset = $target.offset(), |
| 434 | t = target_offset.top, |
| 435 | m_bottom = t + menubox.$el.outerHeight(), // Where the bottom of menu will be |
| 436 | memberlist_bottom = this.$el.parent().offset().top + this.$el.parent().outerHeight(); |
| 437 | |
| 438 | // If the bottom of the userbox is going to be too low.. raise it |
| 439 | if (m_bottom > memberlist_bottom){ |
| 440 | t = memberlist_bottom - menubox.$el.outerHeight(); |
| 441 | } |
| 442 | |
| 443 | // Set the new positon |
| 444 | menubox.$el.offset({ |
| 445 | left: target_offset.left, |
| 446 | top: t |
| 447 | }); |
| 448 | }, this)) |
| 449 | .catch(_.bind(function() { |
| 450 | userbox = null; |
| 451 | |
| 452 | menu.dispose(); |
| 453 | menu = null; |
| 454 | }, this)); |
| 455 | }, |
| 456 | |
| 457 | |
| 458 | chanClick: function (event) { |
| 459 | var target = (event.target) ? $(event.target).data('channel') : $(event.srcElement).data('channel'); |
| 460 | |
| 461 | _kiwi.app.connections.active_connection.gateway.join(target); |
| 462 | }, |
| 463 | |
| 464 | |
| 465 | mediaClick: function (event) { |
| 466 | var $media = $(event.target).parents('.media'); |
| 467 | var media_message; |
| 468 | |
| 469 | if ($media.data('media')) { |
| 470 | media_message = $media.data('media'); |
| 471 | } else { |
| 472 | media_message = new _kiwi.view.MediaMessage({el: $media[0]}); |
| 473 | |
| 474 | // Cache this MediaMessage instance for when it's opened again |
| 475 | $media.data('media', media_message); |
| 476 | } |
| 477 | |
| 478 | media_message.toggle(); |
| 479 | }, |
| 480 | |
| 481 | |
| 482 | // Cursor hovers over a message |
| 483 | msgEnter: function (event) { |
| 484 | var nick_class; |
| 485 | |
| 486 | // Find a valid class that this element has |
| 487 | _.each($(event.currentTarget).parent('.msg').attr('class').split(' '), function (css_class) { |
| 488 | if (css_class.match(/^nick_[a-z0-9]+/i)) { |
| 489 | nick_class = css_class; |
| 490 | } |
| 491 | }); |
| 492 | |
| 493 | // If no class was found.. |
| 494 | if (!nick_class) return; |
| 495 | |
| 496 | $('.'+nick_class).addClass('global_nick_highlight'); |
| 497 | }, |
| 498 | |
| 499 | |
| 500 | // Cursor leaves message |
| 501 | msgLeave: function (event) { |
| 502 | var nick_class; |
| 503 | |
| 504 | // Find a valid class that this element has |
| 505 | _.each($(event.currentTarget).parent('.msg').attr('class').split(' '), function (css_class) { |
| 506 | if (css_class.match(/^nick_[a-z0-9]+/i)) { |
| 507 | nick_class = css_class; |
| 508 | } |
| 509 | }); |
| 510 | |
| 511 | // If no class was found.. |
| 512 | if (!nick_class) return; |
| 513 | |
| 514 | $('.'+nick_class).removeClass('global_nick_highlight'); |
| 515 | } |
| 516 | }); |