2013-08-16 17:10:22 +02:00
|
|
|
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 () {
|
|
|
|
|
2013-08-23 19:55:23 +02:00
|
|
|
function stringify_time(time) {
|
|
|
|
if (feature_flags.twenty_four_hour_time) {
|
|
|
|
return time.toString('HH:mm');
|
|
|
|
}
|
2013-11-21 12:11:50 +01:00
|
|
|
return time.toString('h:mm TT');
|
2013-08-23 19:55:23 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
function same_day(earlier_msg, later_msg) {
|
|
|
|
var earlier_time = new XDate(earlier_msg.timestamp * 1000);
|
|
|
|
var later_time = new XDate(later_msg.timestamp * 1000);
|
|
|
|
|
|
|
|
return earlier_time.toDateString() === later_time.toDateString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_display_time(group, message, prev) {
|
2013-08-16 17:10:22 +02:00
|
|
|
var time = new XDate(message.timestamp * 1000);
|
|
|
|
|
|
|
|
if (prev !== undefined) {
|
|
|
|
var prev_time = new XDate(prev.timestamp * 1000);
|
|
|
|
if (time.toDateString() !== prev_time.toDateString()) {
|
2013-08-29 01:34:10 +02:00
|
|
|
// NB: show_date is HTML, inserted into the document without escaping.
|
2014-02-05 16:55:24 +01:00
|
|
|
group.show_date = (timerender.render_date(time, prev_time))[0].outerHTML;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-05 16:55:24 +01:00
|
|
|
group.show_date = (timerender.render_date(time))[0].outerHTML;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 19:55:23 +02:00
|
|
|
if (message.timestr === undefined) {
|
|
|
|
message.timestr = stringify_time(time);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:00:42 +01:00
|
|
|
function populate_group_from_message(group, message) {
|
|
|
|
group.is_stream = message.is_stream;
|
|
|
|
group.is_private = message.is_private;
|
|
|
|
|
|
|
|
if (group.is_stream) {
|
|
|
|
group.background_color = stream_data.get_color(message.stream);
|
|
|
|
group.color_class = stream_color.get_color_class(group.background_color);
|
|
|
|
group.invite_only = stream_data.get_invite_only(message.stream);
|
|
|
|
group.subject = message.subject;
|
|
|
|
group.match_subject = message.match_subject;
|
|
|
|
group.stream_url = message.stream_url;
|
|
|
|
group.topic_url = message.topic_url;
|
|
|
|
} else if (group.is_private) {
|
|
|
|
group.pm_with_url = message.pm_with_url;
|
|
|
|
group.display_reply_to = message.display_reply_to;
|
|
|
|
}
|
|
|
|
group.display_recipient = message.display_recipient;
|
|
|
|
group.always_visible_topic_edit = message.always_visible_topic_edit;
|
|
|
|
group.on_hover_topic_edit = message.on_hover_topic_edit;
|
|
|
|
group.subject_links = message.subject_links;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
function start_group() {
|
|
|
|
return {messages: []};
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
var table_name = this.table_name;
|
|
|
|
var table = rows.get_table(table_name);
|
2013-12-05 22:45:38 +01:00
|
|
|
// we we record if last_message_was_selected before updating the table
|
|
|
|
var last_message_was_selected = rows.id(rows.last_visible()) === list.selected_id();
|
2013-08-16 17:10:22 +02:00
|
|
|
var ids_where_next_is_same_sender = {};
|
|
|
|
var prev;
|
|
|
|
var orig_scrolltop_offset, last_message_id;
|
2014-02-05 16:55:24 +01:00
|
|
|
var combined_messages, first_msg, last_msg;
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
var current_group = start_group();
|
2013-08-16 17:10:22 +02:00
|
|
|
var new_message_groups = [];
|
|
|
|
|
|
|
|
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) {
|
2014-01-28 20:29:18 +01:00
|
|
|
orig_scrolltop_offset = self.selected_row().offset().top;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (where === 'top' && self.collapse_messages && this._message_groups.length > 0) {
|
2014-02-05 16:55:24 +01:00
|
|
|
// Remove the top date_row, we'll re-add it after rendering
|
|
|
|
$('.date_row:first', table).remove();
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
// Delete the current top message group, and add it back in with these
|
|
|
|
// messages, in order to collapse properly.
|
|
|
|
//
|
2014-02-05 16:55:24 +01:00
|
|
|
// This means we redraw the entire view on each backfill-update when narrowed by
|
2013-08-16 17:10:22 +02:00
|
|
|
// 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];
|
2014-02-28 16:11:11 +01:00
|
|
|
var top_row = this.get_row(top_group.messages[0].id);
|
|
|
|
if (top_row.length !== 0) {
|
|
|
|
rows.get_message_recipient_row(top_row).remove();
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
messages = messages.concat(top_group.messages);
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
if (where !== 'top') {
|
2013-12-09 20:15:33 +01:00
|
|
|
var last_row = table.find('div[zid]:last');
|
2013-08-16 17:10:22 +02:00
|
|
|
last_message_id = rows.id(last_row);
|
|
|
|
prev = self.get_message(last_message_id);
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
function add_message_to_group(message) {
|
|
|
|
current_group.messages.push(message);
|
|
|
|
}
|
|
|
|
|
2013-08-23 00:14:11 +02:00
|
|
|
function finish_group() {
|
2014-02-05 16:55:24 +01:00
|
|
|
if (current_group.messages.length > 0) {
|
2014-03-03 22:00:42 +01:00
|
|
|
populate_group_from_message(current_group, current_group.messages[0]);
|
2014-02-05 16:55:24 +01:00
|
|
|
current_group.messages[current_group.messages.length - 1].include_footer = true;
|
|
|
|
new_message_groups.push(current_group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_subscription_marker(group, last_msg, first_msg) {
|
|
|
|
if (last_msg !== undefined &&
|
|
|
|
first_msg.historical !== last_msg.historical) {
|
|
|
|
group.bookend_top = true;
|
|
|
|
if (first_msg.historical) {
|
|
|
|
group.unsubscribed = first_msg.stream;
|
|
|
|
group.bookend_content = self.list.unsubscribed_bookend_content(first_msg.stream);
|
|
|
|
} else {
|
|
|
|
group.subscribed = first_msg.stream;
|
|
|
|
group.bookend_content = self.list.subscribed_bookend_content(first_msg.stream);
|
|
|
|
}
|
2013-08-23 00:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
_.each(messages, function (message) {
|
|
|
|
message.include_recipient = false;
|
|
|
|
message.include_footer = false;
|
|
|
|
|
|
|
|
if (util.same_recipient(prev, message) && self.collapse_messages &&
|
2014-02-05 16:55:24 +01:00
|
|
|
prev.historical === message.historical && same_day(prev, message)) {
|
|
|
|
add_message_to_group(message);
|
2013-08-16 17:10:22 +02:00
|
|
|
} else {
|
2013-08-23 00:14:11 +02:00
|
|
|
finish_group();
|
2014-02-05 16:55:24 +01:00
|
|
|
current_group = start_group();
|
|
|
|
add_message_to_group(message);
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
message.include_recipient = true;
|
|
|
|
message.subscribed = false;
|
|
|
|
message.unsubscribed = false;
|
2014-02-05 16:55:24 +01:00
|
|
|
|
|
|
|
// This home_msg_list condition can be removed
|
|
|
|
// once we filter historical messages from the
|
|
|
|
// home view on the server side (which requires
|
|
|
|
// having an index on UserMessage.flags)
|
|
|
|
if (self.list !== home_msg_list) {
|
|
|
|
add_subscription_marker(current_group, prev, message);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2013-10-03 23:05:46 +02:00
|
|
|
|
|
|
|
if (message.stream) {
|
|
|
|
message.stream_url = narrow.by_stream_uri(message.stream);
|
|
|
|
message.topic_url = narrow.by_stream_subject_uri(message.stream, message.subject);
|
|
|
|
} else {
|
|
|
|
message.pm_with_url = narrow.pm_with_uri(message.reply_to);
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
add_display_time(current_group, message, prev);
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
message.include_sender = true;
|
|
|
|
if (!message.include_recipient &&
|
2013-11-08 02:00:12 +01:00
|
|
|
!prev.status_message &&
|
2013-08-16 17:10:22 +02:00
|
|
|
util.same_sender(prev, message)) {
|
|
|
|
message.include_sender = false;
|
|
|
|
ids_where_next_is_same_sender[prev.id] = true;
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
self._add_msg_timestring(message);
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
message.dom_id = table_name + message.id;
|
|
|
|
message.small_avatar_url = ui.small_avatar_url(message);
|
2014-02-24 19:18:38 +01:00
|
|
|
if (message.stream !== undefined) {
|
|
|
|
message.background_color = stream_data.get_color(message.stream);
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
message.contains_mention = notifications.speaking_at_me(message);
|
|
|
|
message.unread = unread.message_unread(message);
|
|
|
|
|
2014-01-27 16:56:01 +01:00
|
|
|
if (message.is_me_message) {
|
|
|
|
// Slice the '<p>/me ' off the front, and '</p>' off the end
|
|
|
|
message.status_message = message.content.slice(4 + 3, -4);
|
2013-11-08 02:00:12 +01:00
|
|
|
message.include_sender = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
message.status_message = false;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
prev = message;
|
2013-07-19 17:38:55 +02:00
|
|
|
// This home_msg_list condition can be removed
|
|
|
|
// once we filter historical messages from the
|
|
|
|
// home view on the server side (which requires
|
|
|
|
// having an index on UserMessage.flags)
|
|
|
|
if (self.list !== home_msg_list) {
|
|
|
|
list.last_message_historical = message.historical;
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
});
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
finish_group();
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
if (new_message_groups.length === 0 ||
|
|
|
|
new_message_groups[new_message_groups.length - 1].messages.length === 0) {
|
2013-08-20 22:05:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
if (where === 'top') {
|
2014-02-05 16:55:24 +01:00
|
|
|
// If we prepended messages that are historical, show the subscription message
|
|
|
|
if (self._message_groups.length === 0 ||
|
|
|
|
(prev.historical !== self._message_groups[0].messages[0].historical)) {
|
|
|
|
if (self.list !== home_msg_list) {
|
|
|
|
current_group.bookend_bottom = true;
|
|
|
|
add_subscription_marker(current_group, prev, self._message_groups[0].messages[0]);
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
var rendered_groups = $(templates.render('message_group', {
|
2014-02-26 00:19:20 +01:00
|
|
|
message_groups: new_message_groups,
|
|
|
|
use_match_properties: self.list.filter.is_search()
|
2013-08-16 17:10:22 +02:00
|
|
|
}));
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
var rendered_messages = [];
|
|
|
|
_.each(rendered_groups, function (group) {
|
|
|
|
_.each($('div.message_row', group), function (message_row) {
|
|
|
|
var row = $(message_row);
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
// Save DOM elements by id into self._rows for O(1) lookup
|
|
|
|
if (row.hasClass('message_row')) {
|
|
|
|
self._rows[row.attr('zid')] = message_row;
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
rendered_messages.push(message_row);
|
|
|
|
});
|
2013-08-16 17:10:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
_.each(rendered_messages, function (elem){
|
2014-01-17 20:11:54 +01:00
|
|
|
var e = $.Event('message_rendered.zulip', {target: elem});
|
|
|
|
$(document).trigger(e);
|
|
|
|
});
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
|
|
|
|
function first_group_message(groups) {
|
|
|
|
var first_group = groups[0];
|
|
|
|
return first_group.messages[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function last_group_message(groups) {
|
|
|
|
var last_group = groups[groups.length - 1];
|
|
|
|
return last_group.messages[last_group.messages.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function combine_adjacent_groups(before_list, after_list) {
|
|
|
|
// Given two lists of message groups,
|
|
|
|
// returns: one list that has the abutting groups' message list messages
|
|
|
|
var combined_messages = before_list[before_list.length - 1].messages.concat(after_list[0].messages);
|
|
|
|
|
|
|
|
before_list[before_list.length - 1].messages = combined_messages;
|
|
|
|
|
|
|
|
return before_list.concat(after_list.slice(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self._message_groups.length === 0) {
|
|
|
|
self._message_groups = new_message_groups;
|
|
|
|
table.append(rendered_groups);
|
2013-08-16 17:10:22 +02:00
|
|
|
} else {
|
2014-02-05 16:55:24 +01:00
|
|
|
if (where === 'top') {
|
|
|
|
self._message_groups = new_message_groups.concat(self._message_groups);
|
|
|
|
table.prepend(rendered_groups);
|
|
|
|
} else {
|
|
|
|
// When appending messages, since we're not re-rendering the whole existing last block of messages,
|
|
|
|
// we may have to insert the messages in the existing block. We do this if the messages would normally
|
|
|
|
// have been in the same group originally
|
|
|
|
last_msg = last_group_message(self._message_groups);
|
|
|
|
first_msg = first_group_message(new_message_groups);
|
|
|
|
|
|
|
|
if (self.collapse_messages && util.same_recipient(last_msg, first_msg) && same_day(last_msg, first_msg)) {
|
|
|
|
self._message_groups = combine_adjacent_groups(self._message_groups, new_message_groups);
|
|
|
|
|
|
|
|
// Pluck the merged messages out of our rendered group list, and insert them
|
|
|
|
// into the existing group div
|
|
|
|
var last_group = rows.get_message_recipient_row(self._rows[last_msg.id]);
|
2014-02-21 22:22:04 +01:00
|
|
|
last_group.find('.last_message').removeClass('last_message');
|
2014-02-05 16:55:24 +01:00
|
|
|
last_group.append($('.message_row', rendered_groups[0]).remove());
|
|
|
|
rendered_groups.splice(0, 1);
|
|
|
|
} else {
|
|
|
|
self._message_groups = self._message_groups.concat(new_message_groups);
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
// append the rest of the groups
|
|
|
|
table.append(rendered_groups);
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
list.update_trailing_bookend();
|
|
|
|
|
|
|
|
_.each(rendered_messages, function (elem) {
|
2013-08-16 17:10:22 +02:00
|
|
|
var row = $(elem);
|
|
|
|
var id = rows.id(row);
|
|
|
|
message_edit.maybe_show_edit(row, id);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Must happen after the elements are inserted into the document for
|
|
|
|
// getBoundingClientRect to work.
|
2013-12-09 19:20:55 +01:00
|
|
|
// Also, the list must actually be visible.
|
|
|
|
if (list === current_msg_list) {
|
2014-02-05 16:55:24 +01:00
|
|
|
ui.condense_and_collapse(rendered_messages);
|
2013-12-09 19:20:55 +01:00
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
// Must happen after anything that changes the height of messages has
|
|
|
|
// taken effect.
|
|
|
|
if (where === 'top' && list === current_msg_list && orig_scrolltop_offset !== undefined) {
|
|
|
|
// Restore the selected row to its original position in
|
|
|
|
// relation to the top of the window
|
2014-01-28 20:29:18 +01:00
|
|
|
viewport.set_message_offset(orig_scrolltop_offset);
|
2013-08-16 17:10:22 +02:00
|
|
|
list.reselect_selected_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list === current_msg_list) {
|
|
|
|
// Update the fade.
|
|
|
|
|
2014-02-21 21:10:09 +01:00
|
|
|
var get_element = function (message_group) {
|
|
|
|
// We don't have a MessageGroup class, but we can at least hide the messy details
|
2013-08-16 17:10:22 +02:00
|
|
|
// of rows.js from compose_fade. We provide a callback function to be lazy--
|
|
|
|
// compose_fade may not actually need the elements depending on its internal
|
|
|
|
// state.
|
2014-02-21 21:10:09 +01:00
|
|
|
var message_row = self.get_row(message_group.messages[0].id);
|
|
|
|
return rows.get_message_recipient_row(message_row);
|
2013-08-16 17:10:22 +02:00
|
|
|
};
|
|
|
|
|
2014-02-21 21:10:09 +01:00
|
|
|
compose_fade.update_rendered_message_groups(new_message_groups, get_element);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (list === current_msg_list && messages_are_new) {
|
2014-02-05 16:55:24 +01:00
|
|
|
self._maybe_autoscroll(rendered_messages, last_message_was_selected);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_add_msg_timestring: function MessageListView__add_msg_timestring(message) {
|
|
|
|
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].innerText
|
|
|
|
+ " at " + stringify_time(last_edit_time);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-05 22:45:38 +01:00
|
|
|
_maybe_autoscroll: function MessageListView__maybe_autoscroll(rendered_elems, last_message_was_selected) {
|
2013-08-16 17:10:22 +02:00
|
|
|
// If we are near the bottom of our feed (the bottom is visible) and can
|
|
|
|
// scroll up without moving the pointer out of the viewport, do so, by
|
|
|
|
// up to the amount taken up by the new message.
|
2013-12-05 22:45:38 +01:00
|
|
|
var new_messages_height = 0;
|
|
|
|
var distance_to_last_message_sent_by_me = 0;
|
|
|
|
var id_of_last_message_sent_by_us = -1;
|
|
|
|
|
|
|
|
// C++ iterators would have made this less painful
|
2014-02-05 16:55:24 +01:00
|
|
|
_.each(rendered_elems.reverse(), function (elem) {
|
2013-12-05 22:45:38 +01:00
|
|
|
// Sometimes there are non-DOM elements in rendered_elems; only
|
|
|
|
// try to get the heights of actual trs.
|
2013-12-09 20:15:33 +01:00
|
|
|
if ($(elem).is("div")) {
|
2013-12-05 22:45:38 +01:00
|
|
|
new_messages_height += elem.offsetHeight;
|
|
|
|
// starting from the last message, ignore message heights that weren't sent by me.
|
|
|
|
if(id_of_last_message_sent_by_us > -1) {
|
|
|
|
distance_to_last_message_sent_by_me += elem.offsetHeight;
|
2013-12-13 21:46:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var row_id = rows.id($(elem));
|
|
|
|
// check for `row_id` NaN in case we're looking at a date row or bookend row
|
|
|
|
if (row_id > -1 &&
|
|
|
|
this.get_message(row_id).sender_email === page_params.email)
|
2013-12-05 22:45:38 +01:00
|
|
|
{
|
|
|
|
distance_to_last_message_sent_by_me += elem.offsetHeight;
|
|
|
|
id_of_last_message_sent_by_us = rows.id($(elem));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// autoscroll_forever: if we're on the last message, keep us on the last message
|
|
|
|
if (last_message_was_selected && page_params.autoscroll_forever) {
|
|
|
|
this.list.select_id(this.list.last().id, {from_rendering: true});
|
|
|
|
scroll_to_selected();
|
|
|
|
this.list.reselect_selected_id();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
var selected_row = this.selected_row();
|
|
|
|
var last_visible = rows.last_visible();
|
|
|
|
|
|
|
|
// Make sure we have a selected row and last visible row. (defensive)
|
|
|
|
if (!(selected_row && (selected_row.length > 0) && last_visible)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selected_row_offset = selected_row.offset().top;
|
|
|
|
var info = viewport.message_viewport_info();
|
|
|
|
var available_space_for_scroll = selected_row_offset - info.visible_top;
|
|
|
|
|
2013-12-05 22:45:38 +01:00
|
|
|
// autoscroll_forever: if we've sent a message, move pointer at least that far.
|
|
|
|
if (page_params.autoscroll_forever && id_of_last_message_sent_by_us > -1 && (rows.last_visible().offset().top - this.list.selected_row().offset().top) < (viewport.height())) {
|
|
|
|
this.list.select_id(id_of_last_message_sent_by_us, {from_rendering: true});
|
|
|
|
scroll_to_selected();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
// Don't scroll if we can't move the pointer up.
|
|
|
|
if (available_space_for_scroll <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_messages_height <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This next decision is fairly debatable. For a big message that
|
|
|
|
// would push the pointer off the screen, we do a partial autoscroll,
|
|
|
|
// which has the following implications:
|
|
|
|
// a) user sees scrolling (good)
|
|
|
|
// b) user's pointer stays on screen (good)
|
|
|
|
// c) scroll amount isn't really tied to size of new messages (bad)
|
|
|
|
// d) all the bad things about scrolling for users who want messages
|
|
|
|
// to stay on the screen
|
|
|
|
var scroll_amount = new_messages_height;
|
|
|
|
|
|
|
|
if (scroll_amount > available_space_for_scroll) {
|
|
|
|
scroll_amount = available_space_for_scroll;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's work our way back to whether the user was already dealing
|
|
|
|
// with messages off the screen, in which case we shouldn't autoscroll.
|
|
|
|
var bottom_last_visible = last_visible.offset().top + last_visible.height();
|
|
|
|
var bottom_old_last_visible = bottom_last_visible - new_messages_height;
|
|
|
|
var bottom_viewport = info.visible_top + info.visible_height;
|
|
|
|
|
|
|
|
// Exit if the user was already past the bottom.
|
|
|
|
if (bottom_old_last_visible > bottom_viewport) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, we are finally ready to actually scroll.
|
|
|
|
viewport.system_initiated_animate_scroll(scroll_amount);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
clear_rendering_state: function MessageListView__clear_rendering_state(clear_table) {
|
|
|
|
this._message_groups = [];
|
|
|
|
if (clear_table) {
|
|
|
|
this.clear_table();
|
|
|
|
}
|
|
|
|
this.list.last_message_historical = false;
|
|
|
|
|
|
|
|
this._render_win_start = 0;
|
|
|
|
this._render_win_end = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
update_render_window: function MessageListView__update_render_window(selected_idx, check_for_changed) {
|
2013-11-07 16:40:57 +01:00
|
|
|
var new_start = Math.max(selected_idx - this._RENDER_WINDOW_SIZE / 2, 0);
|
2013-08-16 17:10:22 +02:00
|
|
|
if (check_for_changed && new_start === this._render_win_start) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._render_win_start = new_start;
|
2013-11-07 16:40:57 +01:00
|
|
|
this._render_win_end = Math.min(this._render_win_start + this._RENDER_WINDOW_SIZE,
|
|
|
|
this.list.num_items());
|
2013-08-16 17:10:22 +02:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
maybe_rerender: function MessageListView__maybe_rerender() {
|
|
|
|
if (this.table_name === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selected_idx = this.list.selected_idx();
|
|
|
|
|
|
|
|
// We rerender under the following conditions:
|
2013-11-07 16:40:57 +01:00
|
|
|
// * The selected message is within this._RENDER_THRESHOLD messages
|
|
|
|
// of the top of the currently rendered window and the top
|
|
|
|
// of the window does not abut the beginning of the message
|
|
|
|
// list
|
|
|
|
// * The selected message is within this._RENDER_THRESHOLD messages
|
|
|
|
// of the bottom of the currently rendered window and the
|
|
|
|
// bottom of the window does not abut the end of the
|
|
|
|
// message list
|
|
|
|
if (! (((selected_idx - this._render_win_start < this._RENDER_THRESHOLD)
|
2013-08-16 17:10:22 +02:00
|
|
|
&& (this._render_win_start !== 0)) ||
|
2013-11-07 16:40:57 +01:00
|
|
|
((this._render_win_end - selected_idx <= this._RENDER_THRESHOLD)
|
2013-08-16 17:10:22 +02:00
|
|
|
&& (this._render_win_end !== this.list.num_items()))))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.update_render_window(selected_idx, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rerender_preserving_scrolltop();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
rerender_preserving_scrolltop: function MessageListView__rerender_preserving_scrolltop() {
|
|
|
|
// scrolltop_offset is the number of pixels between the top of the
|
|
|
|
// viewable window and the newly selected message
|
|
|
|
var scrolltop_offset;
|
|
|
|
var selected_row = this.selected_row();
|
|
|
|
var selected_in_view = (selected_row.length > 0);
|
|
|
|
if (selected_in_view) {
|
|
|
|
scrolltop_offset = viewport.scrollTop() - selected_row.offset().top;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clear_table();
|
2013-11-26 17:07:40 +01:00
|
|
|
this.render(this.list.all().slice(this._render_win_start,
|
|
|
|
this._render_win_end), 'bottom');
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
// If we could see the newly selected message, scroll the
|
|
|
|
// window such that the newly selected message is at the
|
|
|
|
// same location as it would have been before we
|
|
|
|
// re-rendered.
|
|
|
|
if (selected_in_view) {
|
|
|
|
// Must get this.list.selected_row() again since it is now a new DOM element
|
|
|
|
viewport.scrollTop(this.selected_row().offset().top + scrolltop_offset);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-03 22:00:42 +01:00
|
|
|
rerender_header: function MessageListView__maybe_rerender_header(messages) {
|
|
|
|
// Given a list of messages that are in the **same** message group,
|
|
|
|
// rerender the header / recipient bar of the messages
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var first_row = this.get_row(messages[0].id);
|
|
|
|
var recipient_row = rows.get_message_recipient_row(first_row);
|
|
|
|
var header = recipient_row.find('.message_header');
|
|
|
|
|
|
|
|
var group = {messages: messages};
|
|
|
|
populate_group_from_message(group, messages[0]);
|
|
|
|
|
|
|
|
var rendered_recipient_row = $(templates.render('recipient_row', group));
|
|
|
|
|
|
|
|
header.replaceWith(rendered_recipient_row);
|
|
|
|
},
|
|
|
|
|
2014-02-11 16:19:42 +01:00
|
|
|
rerender_message: function MessageListView__rerender_message(message) {
|
|
|
|
var row = this.get_row(message.id);
|
2014-02-21 16:24:21 +01:00
|
|
|
var was_selected = this.list.selected_message() === message;
|
2014-02-11 16:19:42 +01:00
|
|
|
|
|
|
|
if (row === undefined) {
|
|
|
|
blueslip.error("Cannot rerender a message that's not in this list!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-render just this one message
|
2014-02-21 16:35:22 +01:00
|
|
|
this._add_msg_timestring(message);
|
2014-02-11 16:19:42 +01:00
|
|
|
var rendered_msg = $(templates.render('single_message', message));
|
|
|
|
this._rows[message.id] = rendered_msg;
|
|
|
|
row.replaceWith(rendered_msg);
|
2014-02-21 16:24:21 +01:00
|
|
|
if (was_selected) {
|
|
|
|
this.list.select_id(message.id);
|
|
|
|
}
|
2014-02-11 16:19:42 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
rerender_messages: function MessageListView__rerender_messages(messages) {
|
|
|
|
var self = this;
|
2014-03-03 22:00:42 +01:00
|
|
|
var message_groups = [];
|
|
|
|
var current_group = [];
|
2014-02-11 16:19:42 +01:00
|
|
|
_.each(messages, function (message) {
|
2014-03-03 22:00:42 +01:00
|
|
|
if (current_group.length === 0 || util.same_recipient(current_group[current_group.length - 1], message)) {
|
|
|
|
current_group.push(message);
|
|
|
|
} else {
|
|
|
|
message_groups.push(current_group);
|
|
|
|
current_group = [];
|
|
|
|
}
|
2014-02-11 16:19:42 +01:00
|
|
|
self.rerender_message(message);
|
|
|
|
});
|
2014-03-03 22:00:42 +01:00
|
|
|
if (current_group.length !== 0) {
|
|
|
|
message_groups.push(current_group);
|
|
|
|
}
|
|
|
|
_.each(message_groups, function (messages_in_group) {
|
|
|
|
self.rerender_header(messages_in_group);
|
|
|
|
});
|
2014-02-11 16:19:42 +01:00
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
append: function MessageListView__append(messages, messages_are_new) {
|
2013-11-07 16:40:57 +01:00
|
|
|
var cur_window_size = this._render_win_end - this._render_win_start;
|
2013-08-16 17:10:22 +02:00
|
|
|
if (cur_window_size < this._RENDER_WINDOW_SIZE) {
|
|
|
|
var slice_to_render = messages.slice(0, this._RENDER_WINDOW_SIZE - cur_window_size);
|
|
|
|
this.render(slice_to_render, 'bottom', messages_are_new);
|
|
|
|
this._render_win_end += slice_to_render.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pointer is high on the page such that there is a
|
|
|
|
// lot of empty space below and the render window is full, a
|
|
|
|
// newly recieved message should trigger a rerender so that
|
|
|
|
// the new message, which will appear in the viewable area,
|
|
|
|
// is rendered.
|
|
|
|
this.maybe_rerender();
|
|
|
|
},
|
|
|
|
|
|
|
|
prepend: function MessageListView__prepend(messages) {
|
|
|
|
this._render_win_start += messages.length;
|
|
|
|
this._render_win_end += messages.length;
|
|
|
|
|
2013-11-07 16:44:17 +01:00
|
|
|
var cur_window_size = this._render_win_end - this._render_win_start;
|
|
|
|
if (cur_window_size < this._RENDER_WINDOW_SIZE) {
|
2014-02-05 16:55:24 +01:00
|
|
|
var msgs_to_render_count = this._RENDER_WINDOW_SIZE - cur_window_size;
|
|
|
|
var slice_to_render = messages.slice(messages.length - msgs_to_render_count);
|
2013-11-07 16:44:17 +01:00
|
|
|
this.render(slice_to_render, 'top', false);
|
|
|
|
this._render_win_start -= slice_to_render.length;
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
rerender_the_whole_thing: function MessageListView__rerender_the_whole_thing(messages) {
|
|
|
|
// TODO: Figure out if we can unify this with this.list.rerender().
|
|
|
|
|
|
|
|
this.clear_rendering_state(true);
|
|
|
|
|
|
|
|
this.update_render_window(this.list.selected_idx(), false);
|
|
|
|
|
2013-11-26 17:07:40 +01:00
|
|
|
this.render(this.list.all().slice(this._render_win_start,
|
|
|
|
this._render_win_end), 'bottom');
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
clear_table: function MessageListView_clear_table() {
|
|
|
|
// We do not want to call .empty() because that also clears
|
|
|
|
// jQuery data. This does mean, however, that we need to be
|
|
|
|
// mindful of memory leaks.
|
|
|
|
rows.get_table(this.table_name).children().detach();
|
|
|
|
this._rows = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
get_row: function MessageListView_get_row(id) {
|
|
|
|
return $(this._rows[id]);
|
|
|
|
},
|
|
|
|
|
|
|
|
clear_trailing_bookend: function MessageListView_clear_trailing_bookend() {
|
2014-02-05 16:55:24 +01:00
|
|
|
var trailing_bookend = rows.get_table(this.table_name).find('.trailing_bookend');
|
2013-08-16 17:10:22 +02:00
|
|
|
trailing_bookend.remove();
|
|
|
|
},
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
render_trailing_bookend: function MessageListView_render_trailing_bookend(trailing_bookend_content) {
|
|
|
|
var rendered_trailing_bookend = $(templates.render('bookend', {
|
|
|
|
bookend_content: trailing_bookend_content,
|
|
|
|
trailing: true
|
2013-08-16 17:10:22 +02:00
|
|
|
}));
|
|
|
|
rows.get_table(this.table_name).append(rendered_trailing_bookend);
|
|
|
|
},
|
|
|
|
|
|
|
|
selected_row: function MessageListView_selected_row() {
|
|
|
|
return this.get_row(this.list.selected_id());
|
|
|
|
},
|
|
|
|
|
|
|
|
get_message: function MessageListView_get_message(id) {
|
|
|
|
return this.list.get(id);
|
2013-12-19 17:03:08 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
change_message_id: function MessageListView_change_message_id(old_id, new_id) {
|
|
|
|
if (this._rows[old_id] !== undefined) {
|
|
|
|
var row = this._rows[old_id];
|
|
|
|
delete this._rows[old_id];
|
|
|
|
|
|
|
|
row.setAttribute('zid', new_id);
|
|
|
|
row.setAttribute('id', this.table_name + new_id);
|
2014-01-24 18:01:12 +01:00
|
|
|
$(row).removeClass('local');
|
2013-12-19 17:03:08 +01:00
|
|
|
this._rows[new_id] = row;
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = MessageListView;
|
|
|
|
}
|