function MessageListView(list, table_name, collapse_messages) { this.list = list; this.collapse_messages = collapse_messages; this._rows = {}; this.table_name = table_name; if (this.table_name) { this.clear_table(); } this._message_groups = []; // Half-open interval of the indices that define the current render window this._render_win_start = 0; this._render_win_end = 0; } (function () { function stringify_time(time) { if (feature_flags.twenty_four_hour_time) { return time.toString('HH:mm'); } return time.toString('h:mmtt'); } function add_display_time(message, prev) { var time = new XDate(message.timestamp * 1000); if (prev !== undefined) { var prev_time = new XDate(prev.timestamp * 1000); if (time.toDateString() !== prev_time.toDateString()) { // NB: show_date is HTML, inserted into the document without escaping. message.show_date = (timerender.render_date(time, prev_time))[0].outerHTML; } else { // This is run on re-render, and must remove the date if a message // from the same day is added above this one when scrolling up. message.show_date = undefined; } } else { message.show_date = (timerender.render_date(time))[0].outerHTML; } if (message.timestr === undefined) { message.timestr = stringify_time(time); } } MessageListView.prototype = { // Number of messages to render at a time _RENDER_WINDOW_SIZE: 400, // Number of messages away from edge of render window at which we // trigger a re-render _RENDER_THRESHOLD: 50, render: function MessageListView__render(messages, where, messages_are_new) { var list = this.list; // for convenience // This function processes messages into chunks with separators between them, // and templates them to be inserted as table rows into the DOM. if (messages.length === 0 || this.table_name === undefined) { return; } var table_name = this.table_name; var table = rows.get_table(table_name); var messages_to_render = []; var ids_where_next_is_same_sender = {}; var prev; var orig_scrolltop_offset, last_message_id; var current_group = []; var new_message_groups = []; var summary_group = {}; var has_summary = false; var summary_start_id = 0; var self = this; if (where === "bottom") { // Remove the trailing bookend; it'll be re-added after we do our rendering self.clear_trailing_bookend(); } else if (self.selected_row().length > 0) { orig_scrolltop_offset = self.selected_row().offset().top - viewport.scrollTop(); } if (where === 'top' && self.collapse_messages && this._message_groups.length > 0) { // Delete the current top message group, and add it back in with these // messages, in order to collapse properly. // // This means we redraw the entire view on each update when narrowed by // subject, which could be a problem down the line. For now we hope // that subject views will not be very big. var top_group = self._message_groups[0]; var top_messages = []; _.each(top_group, function (id) { self.get_row(id).remove(); // Remove any date row headers for these messages $('.date_row[data-zid=' + id + ']').remove(); top_messages.push(self.get_message(id)); }); messages = messages.concat(top_messages); // Delete the leftover recipient label. table.find('.recipient_row:first').remove(); } else { var last_row = table.find('tr[zid]:last'); last_message_id = rows.id(last_row); prev = self.get_message(last_message_id); if (last_row.is('.summary_row')) { // Don't group with a summary, but don't put separators before the new message prev = _.pick(prev, 'timestamp', 'historical'); } } function set_template_properties(message) { if (message.is_stream) { message.background_color = stream_data.get_color(message.stream); message.color_class = stream_color.get_color_class(message.background_color); message.invite_only = stream_data.get_invite_only(message.stream); } } function finish_summary() { var first = true; _.each(summary_group, function (summary_row) { summary_row.count = summary_row.messages.length; summary_row.message_ids = _.pluck(summary_row.messages, 'id').join(' '); if (first) { summary_row.include_bookend = true; first = false; } set_template_properties(summary_row); messages_to_render.push(summary_row); prev = summary_row; }); prev.include_footer = true; has_summary = false; summary_group = {}; prev = _.pick(prev, 'timestamp', 'historical'); } function finish_group() { if (current_group.length > 0) { var message_ids = _.pluck(current_group, 'id'); current_group[0].message_ids = message_ids; current_group[0].collapsible = feature_flags.collapsible; new_message_groups.push(message_ids); } } _.each(messages, function (message) { message.include_recipient = false; message.include_bookend = false; message.include_footer = false; add_display_time(message, prev); if (has_summary && message.show_date) { finish_summary(); } var summary_adjective; if (_.contains(message.flags, 'force_collapse')) { summary_adjective = 'collapsed'; } else if (!_.contains(message.flags, 'force_expand')) { if (muting.is_topic_muted(message.stream, message.subject)) { summary_adjective = 'muted'; } else if (list.is_summarized_message(message)) { summary_adjective = 'read'; } } if (summary_adjective) { if (prev) { prev.include_footer = true; } var key = util.recipient_key(message); if (summary_group[key] === undefined) { // Start building a new summary row for messages from this recipient. // // Ugly: handlebars renderer only takes messages. We don't want to modify // the original message, so we make a fake message based on the real one // that will trigger the right part of the handlebars template and won't // show the content, date, etc. from the real message. summary_group[key] = _.extend(_.pick(message, 'timestamp', 'show_date', 'historical', 'stream', 'is_stream', 'subject', 'display_recipient', 'display_reply_to'), { is_summary: true, include_footer: false, include_bookend: false, first_message_id: message.id, summary_adjective: summary_adjective, messages: [message] }); } else { summary_group[key].messages.push(message); } has_summary = true; prev = message; return; } else if (has_summary) { finish_summary(); } if (util.same_recipient(prev, message) && self.collapse_messages && prev.historical === message.historical && !message.show_date) { current_group.push(message); // prev is no longer the last element in this block prev.include_footer = false; } else { finish_group(); current_group = [message]; // Add a space to the table, but not for the first element. message.include_recipient = true; message.include_bookend = (prev !== undefined); if (prev !== undefined) { prev.include_footer = true; } message.subscribed = false; message.unsubscribed = false; if (message.include_bookend && message.historical !== prev.historical) { if (message.historical) { message.unsubscribed = message.stream; } else { message.subscribed = message.stream; } } } message.include_sender = true; if (!message.include_recipient && util.same_sender(prev, message)) { message.include_sender = false; ids_where_next_is_same_sender[prev.id] = true; } if (message.last_edit_timestamp !== undefined) { // Add or update the last_edit_timestr var last_edit_time = new XDate(message.last_edit_timestamp * 1000); message.last_edit_timestr = (timerender.render_date(last_edit_time))[0].outerHTML + " " + stringify_time(last_edit_time); } message.dom_id = table_name + message.id; message.small_avatar_url = ui.small_avatar_url(message); set_template_properties(message); message.contains_mention = notifications.speaking_at_me(message); message.unread = unread.message_unread(message); messages_to_render.push(message); prev = message; list.last_message_historical = message.historical; }); if (prev) { prev.include_footer = true; } if (has_summary) { finish_summary(); } if (messages_to_render.length === 0) { return; } finish_group(); if (where === 'top') { self._message_groups = new_message_groups.concat(self._message_groups); } else { self._message_groups = self._message_groups.concat(new_message_groups); } var rendered_elems = $(templates.render('message', { messages: messages_to_render, include_layout_row: (table.find('tr:first').length === 0), use_match_properties: list.filter.is_search() })); _.each(rendered_elems, function (elem) { var row = $(elem); // Save DOM elements by id into self._rows for O(1) lookup if (row.hasClass('summary_row')) { _.each(row.attr('data-messages').split(' '), function (id) { self._rows[id] = elem; }); } else if (row.hasClass('message_row')) { self._rows[row.attr('zid')] = elem; } if (! row.hasClass('message_row')) { return; } var id = rows.id(row); if (ids_where_next_is_same_sender[id]) { row.find('.messagebox').addClass("next_is_same_sender"); } if (row.hasClass('mention')) { row.find('.user-mention').each(function () { var email = $(this).attr('data-user-email'); if (email === '*' || email === page_params.email) { $(this).addClass('user-mention-me'); } }); } }); // The message that was last before this batch came in has to be // handled specially because we didn't just render it and // therefore have to lookup its associated element // If the previous message was part of the same block but // had a footer, we need to remove it. if (last_message_id !== undefined) { var row = self.get_row(last_message_id); if (ids_where_next_is_same_sender[last_message_id]) { row.find('.messagebox').addClass("next_is_same_sender"); } // We didn't actually rerender the original last message, // but we might have set .include_footer=false for it in // the above loop since it was the previous message for // messages[0]. If so, we need to update the DOM. if (self.get_message(last_message_id) && ! self.get_message(last_message_id).include_footer) { row.removeClass('last_message'); } } if (where === 'top' && table.find('.ztable_layout_row').length > 0) { // If we have a totally empty narrow, there may not // be a .ztable_layout_row. table.find('.ztable_layout_row').after(rendered_elems); } else { table.append(rendered_elems); list.update_trailing_bookend(); // XXX: This is absolutely awful. There is a firefox bug // where when table rows as DOM elements are appended (as // opposed to as a string) a border is sometimes added to the // row. This border goes away if we add a dummy row to the // top of the table (it doesn't go away on any reflow, // though, as resizing the window doesn't make them go away). // So, we add an empty row and then garbage collect them // later when the user is idle. var dummy = $("