Keeping the .last_seen message marker in place until window blurs again
[KiwiIRC.git] / client / src / views / channel.js
CommitLineData
50ac472f 1_kiwi.view.Channel = _kiwi.view.Panel.extend({
9b807765 2 events: function(){
c794b877 3 var parent_events = this.constructor.__super__.events;
3499d625 4
9b807765
D
5 if(_.isFunction(parent_events)){
6 parent_events = parent_events();
7 }
8 return _.extend({}, parent_events, {
3499d625 9 'click .msg .nick' : 'nickClick',
88528078 10 'click .msg .inline-nick' : 'nickClick',
3499d625
D
11 "click .chan": "chanClick",
12 'click .media .open': 'mediaClick',
13 'mouseenter .msg .nick': 'msgEnter',
14 'mouseleave .msg .nick': 'msgLeave'
9b807765 15 });
dfb5209c
JA
16 },
17
50ac472f
D
18 initialize: function (options) {
19 this.initializePanel(options);
c794b877
D
20
21 // Container for all the messages
22 this.$messages = $('<div class="messages"></div>');
23 this.$el.append(this.$messages);
24
50ac472f 25 this.model.bind('change:topic', this.topic, this);
3aa7b8cc 26 this.model.bind('change:topic_set_by', this.topicSetBy, this);
50ac472f 27
7d2a2771 28 if (this.model.get('members')) {
5a0eb997 29 // When we join the memberlist, we have officially joined the channel
7d2a2771
D
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);
5a0eb997
D
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);
7d2a2771 46 }
660e1427 47
50ac472f
D
48 // Only show the loader if this is a channel (ie. not a query)
49 if (this.model.isChannel()) {
247dd7ac 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>');
50ac472f 51 }
c794b877
D
52
53 this.model.bind('msg', this.newMsg, this);
54 this.msg_count = 0;
50ac472f
D
55 },
56
c794b877 57
41a9c836
D
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
72a325ec 68 newMsg: function(msg) {
c794b877 69
72a325ec
D
70 // Parse the msg object into properties fit for displaying
71 msg = this.generateMessageDisplayObj(msg);
c794b877 72
c1c51f22 73 _kiwi.global.events.emit('message:display', {panel: this.model, message: msg})
060391b1 74 .then(_.bind(function() {
72a325ec
D
75 var line_msg;
76
e8885df9
D
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
72a325ec 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>';
e8885df9 82 this.$messages.append($(_.template(line_msg, display_obj)).data('message', msg));
c794b877 83
c1c51f22
D
84 // Activity/alerts based on the type of new message
85 if (msg.type.match(/^action /)) {
86 this.alert('action');
c794b877 87
72a325ec 88 } else if (msg.is_highlight) {
c794b877 89 _kiwi.app.view.alertWindow('* ' + _kiwi.global.i18n.translate('client_views_panel_activity').fetch());
c1c51f22
D
90 _kiwi.app.view.favicon.newHighlight();
91 _kiwi.app.view.playSound('highlight');
ddf30757 92 _kiwi.app.view.showNotification(this.model.get('name'), msg.unparsed_msg);
c1c51f22
D
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');
c794b877 101 }
c794b877 102
c1c51f22
D
103 if (this.model.isQuery() && !this.model.isActive()) {
104 _kiwi.app.view.alertWindow('* ' + _kiwi.global.i18n.translate('client_views_panel_activity').fetch());
ee2f0962 105
c1c51f22 106 // Highlights have already been dealt with above
72a325ec 107 if (!msg.is_highlight) {
c1c51f22
D
108 _kiwi.app.view.favicon.newHighlight();
109 }
ee2f0962 110
ddf30757 111 _kiwi.app.view.showNotification(this.model.get('name'), msg.unparsed_msg);
c1c51f22
D
112 _kiwi.app.view.playSound('highlight');
113 }
c794b877 114
c1c51f22
D
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;
c794b877 119
af03387c 120 var count_all_activity = _kiwi.global.settings.get('count_all_activity'),
31fe7a44 121 exclude_message_types, new_count;
7ba064d9 122
c1c51f22
D
123 // Set the default config value
124 if (typeof count_all_activity === 'undefined') {
125 count_all_activity = false;
126 }
223d53e5 127
c1c51f22
D
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) {
af03387c 139 new_count = this.model.get('activity_counter') || 0;
31fe7a44 140 new_count++;
af03387c 141 this.model.set('activity_counter', new_count);
c1c51f22 142 }
223d53e5 143
c1c51f22 144 }).apply(this);
c794b877 145
c1c51f22 146 if(this.model.isActive()) this.scrollToBottom();
c794b877 147
c1c51f22
D
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));
50ac472f
D
155 },
156
c794b877 157
88528078 158 // Let nicks be clickable + colourise within messages
a32b3a30
D
159 parseMessageNicks: function(word, colourise) {
160 var members, member, colour = '';
88528078
D
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
a32b3a30
D
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 }
88528078 177
a32b3a30 178 return _.template('<span class="inline-nick" style="<%- colour %>;cursor:pointer;" data-nick="<%- nick %>"><%- nick %></span>', {
88528078
D
179 nick: word,
180 colour: colour
181 });
182
183 },
184
185
72a325ec
D
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
8768fa0c 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) {
72a325ec
D
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
8768fa0c 239 return '<a class="link_ext" target="_blank" rel="nofollow" href="' + url.replace(/"/g, '%22') + '">' + _.escape(nice) + '</a>' + extra_html;
72a325ec
D
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
a32b3a30 296 parsed_word = this.parseMessageNicks(word, (msg.type === 'privmsg'));
88528078
D
297 if (typeof parsed_word === 'string') return parsed_word;
298
72a325ec
D
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
ddf30757 309 msg.unparsed_msg = msg.msg;
72a325ec
D
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
50ac472f
D
357 topic: function (topic) {
358 if (typeof topic !== 'string' || !topic) {
359 topic = this.model.get("topic");
360 }
361
9a10cede 362 this.model.addMsg('', styleText('channel_topic', {text: topic, channel: this.model.get('name')}), 'topic');
50ac472f
D
363
364 // If this is the active channel then update the topic bar
3aa7b8cc
D
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);
50ac472f 374 }
dfb5209c
JA
375 },
376
377 // Click on a nickname
378 nickClick: function (event) {
b43f8c8a
D
379 var $target = $(event.currentTarget),
380 nick,
dfb5209c 381 members = this.model.get('members'),
b43f8c8a 382 member;
dfb5209c 383
88528078
D
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)
b43f8c8a 388 nick = $target.data('nick');
88528078 389 if (!nick) {
b43f8c8a 390 nick = $target.parent('.msg').data('message').nick;
88528078
D
391 }
392
b43f8c8a
D
393 // Make sure this nick is still in the channel
394 member = members ? members.getByNick(nick) : null;
395 if (!member) {
396 return;
dfb5209c 397 }
b43f8c8a
D
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
63b21ebe 404 updateLastSeenMarker: function() {
8859068c 405 if (this.model.isActive()) {
63b21ebe 406 // Remove the previous last seen classes
8859068c 407 this.$(".last_seen").removeClass("last_seen");
63b21ebe
EH
408
409 // Mark the last message the user saw
8859068c 410 this.$messages.children().last().addClass("last_seen");
63b21ebe 411 }
b43f8c8a
D
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
8dfd6407 428 _kiwi.global.events.emit('usermenu:created', {menu: menubox, userbox: userbox, user: member})
b43f8c8a
D
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));
3499d625
D
455 },
456
457
458 chanClick: function (event) {
425efe7a
JA
459 var target = (event.target) ? $(event.target).data('channel') : $(event.srcElement).data('channel');
460
461 _kiwi.app.connections.active_connection.gateway.join(target);
3499d625
D
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');
3aa7b8cc 515 }
dfb5209c 516});