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 = {};
|
2014-03-17 19:38:35 +01:00
|
|
|
this.message_containers = {};
|
2013-08-16 17:10:22 +02:00
|
|
|
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 () {
|
|
|
|
|
2018-12-12 22:30:30 +01:00
|
|
|
function get_user_id_for_mention_button(elem) {
|
2017-01-20 17:43:45 +01:00
|
|
|
var user_id = $(elem).attr('data-user-id');
|
|
|
|
// Handle legacy markdown that was rendered before we cut
|
|
|
|
// over to using data-user-id.
|
|
|
|
var email = $(elem).attr('data-user-email');
|
2018-12-12 22:30:30 +01:00
|
|
|
|
|
|
|
if (user_id === "*" || email === "*") {
|
|
|
|
return "*";
|
2017-01-20 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 22:30:30 +01:00
|
|
|
if (user_id) {
|
|
|
|
return user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (email) {
|
|
|
|
// Will return undefined if there's no match
|
2018-12-16 23:35:39 +01:00
|
|
|
var user = people.get_by_email(email);
|
|
|
|
if (user) {
|
|
|
|
return user.user_id;
|
|
|
|
}
|
|
|
|
return;
|
2018-12-12 22:30:30 +01:00
|
|
|
}
|
|
|
|
return;
|
2017-01-20 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
function same_day(earlier_msg, later_msg) {
|
2014-03-30 03:42:48 +02:00
|
|
|
if (earlier_msg === undefined || later_msg === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-14 16:28:54 +01:00
|
|
|
var earlier_time = new XDate(earlier_msg.msg.timestamp * 1000);
|
|
|
|
var later_time = new XDate(later_msg.msg.timestamp * 1000);
|
2014-02-05 16:55:24 +01:00
|
|
|
|
|
|
|
return earlier_time.toDateString() === later_time.toDateString();
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:28:54 +01:00
|
|
|
function same_sender(a, b) {
|
|
|
|
if (a === undefined || b === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return util.same_sender(a.msg, b.msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function same_recipient(a, b) {
|
|
|
|
if (a === undefined || b === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return util.same_recipient(a.msg, b.msg);
|
|
|
|
}
|
|
|
|
|
2018-11-12 23:43:37 +01:00
|
|
|
function update_group_time_display(group, message_container, prev) {
|
2014-03-17 19:38:35 +01:00
|
|
|
var time = new XDate(message_container.msg.timestamp * 1000);
|
2017-05-18 21:18:11 +02:00
|
|
|
var today = new XDate();
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
if (prev !== undefined) {
|
2014-03-14 16:28:54 +01:00
|
|
|
var prev_time = new XDate(prev.msg.timestamp * 1000);
|
2013-08-16 17:10:22 +02:00
|
|
|
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.
|
2018-06-06 18:19:09 +02:00
|
|
|
group.show_date = timerender.render_date(time, prev_time, today)[0].outerHTML;
|
2017-05-16 23:19:57 +02:00
|
|
|
group.show_date_separator = true;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-16 23:19:57 +02:00
|
|
|
// Show the date in the recipient bar, but not a date separator bar.
|
|
|
|
group.show_date_separator = false;
|
2018-06-06 18:19:09 +02:00
|
|
|
group.show_date = timerender.render_date(time, undefined, today)[0].outerHTML;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2018-11-12 23:43:37 +01:00
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2018-11-12 23:43:37 +01:00
|
|
|
function update_timestr(message_container) {
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_container.timestr === undefined) {
|
2018-11-12 23:43:37 +01:00
|
|
|
var time = new XDate(message_container.msg.timestamp * 1000);
|
2017-12-25 21:43:06 +01:00
|
|
|
message_container.timestr = timerender.stringify_time(time);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
editing: Fix live update of ability to edit messages.
Previously, we didn't check the organization-level settings when
rendering a message list; instead, we only checked it when putting
messages into the message_store. That resulted in the state being
stale in the event that the setting controlling whether one can edit
messages was changed.
We remove some node tests, because revidving the node test for their
new home in message_list_view would be more work than we probably want
to do with an upcoming release. We basically need to be better about
exporting functions like populate_group_from_message_container and
set_topic_edit_properties, so we can do fine grained testing.
When we get around to the node tests, rather than exporting these
functions, it might make sense to create a new module with a name
like message_container.js, which would have all of these
last-second type of data manipulations on message objects. This
would be nice to split out of message_list_view.js. MLV is our
biggest module, and it's mostly cohesive, but it's real job
should be about assembling messages into a DOM list, which is
probably 80% of the code now. The 20% that I'd want to consider
splitting out is actually closer in spirit to message_store.js.
Thanks to Steve Howell for helping with the node tests.
2017-08-23 04:41:43 +02:00
|
|
|
function set_topic_edit_properties(group, message) {
|
2018-03-05 19:52:18 +01:00
|
|
|
group.realm_allow_message_editing = page_params.realm_allow_message_editing;
|
editing: Fix live update of ability to edit messages.
Previously, we didn't check the organization-level settings when
rendering a message list; instead, we only checked it when putting
messages into the message_store. That resulted in the state being
stale in the event that the setting controlling whether one can edit
messages was changed.
We remove some node tests, because revidving the node test for their
new home in message_list_view would be more work than we probably want
to do with an upcoming release. We basically need to be better about
exporting functions like populate_group_from_message_container and
set_topic_edit_properties, so we can do fine grained testing.
When we get around to the node tests, rather than exporting these
functions, it might make sense to create a new module with a name
like message_container.js, which would have all of these
last-second type of data manipulations on message objects. This
would be nice to split out of message_list_view.js. MLV is our
biggest module, and it's mostly cohesive, but it's real job
should be about assembling messages into a DOM list, which is
probably 80% of the code now. The 20% that I'd want to consider
splitting out is actually closer in spirit to message_store.js.
Thanks to Steve Howell for helping with the node tests.
2017-08-23 04:41:43 +02:00
|
|
|
group.always_visible_topic_edit = false;
|
|
|
|
group.on_hover_topic_edit = false;
|
|
|
|
|
|
|
|
// Messages with no topics should always have an edit icon visible
|
|
|
|
// to encourage updating them. Admins can also edit any topic.
|
|
|
|
if (message.subject === compose.empty_topic_placeholder()) {
|
|
|
|
group.always_visible_topic_edit = true;
|
2018-10-09 09:54:57 +02:00
|
|
|
} else if (message_edit.is_topic_editable(message)) {
|
editing: Fix live update of ability to edit messages.
Previously, we didn't check the organization-level settings when
rendering a message list; instead, we only checked it when putting
messages into the message_store. That resulted in the state being
stale in the event that the setting controlling whether one can edit
messages was changed.
We remove some node tests, because revidving the node test for their
new home in message_list_view would be more work than we probably want
to do with an upcoming release. We basically need to be better about
exporting functions like populate_group_from_message_container and
set_topic_edit_properties, so we can do fine grained testing.
When we get around to the node tests, rather than exporting these
functions, it might make sense to create a new module with a name
like message_container.js, which would have all of these
last-second type of data manipulations on message objects. This
would be nice to split out of message_list_view.js. MLV is our
biggest module, and it's mostly cohesive, but it's real job
should be about assembling messages into a DOM list, which is
probably 80% of the code now. The 20% that I'd want to consider
splitting out is actually closer in spirit to message_store.js.
Thanks to Steve Howell for helping with the node tests.
2017-08-23 04:41:43 +02:00
|
|
|
group.on_hover_topic_edit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
function populate_group_from_message_container(group, message_container) {
|
|
|
|
group.is_stream = message_container.msg.is_stream;
|
|
|
|
group.is_private = message_container.msg.is_private;
|
2014-03-03 22:00:42 +01:00
|
|
|
|
|
|
|
if (group.is_stream) {
|
2014-03-17 19:38:35 +01:00
|
|
|
group.background_color = stream_data.get_color(message_container.msg.stream);
|
2014-03-03 22:00:42 +01:00
|
|
|
group.color_class = stream_color.get_color_class(group.background_color);
|
2014-03-17 19:38:35 +01:00
|
|
|
group.invite_only = stream_data.get_invite_only(message_container.msg.stream);
|
2018-11-15 17:13:49 +01:00
|
|
|
group.topic = message_container.msg.subject;
|
2018-11-15 16:59:41 +01:00
|
|
|
group.match_topic = util.get_match_topic(message_container.msg);
|
2014-03-17 19:38:35 +01:00
|
|
|
group.stream_url = message_container.stream_url;
|
|
|
|
group.topic_url = message_container.topic_url;
|
2016-11-22 09:20:20 +01:00
|
|
|
var sub = stream_data.get_sub(message_container.msg.stream);
|
|
|
|
if (sub === undefined) {
|
|
|
|
// Hack to handle unusual cases like the tutorial where
|
|
|
|
// the streams used don't actually exist in the subs
|
|
|
|
// module. Ideally, we'd clean this up by making the
|
|
|
|
// tutorial populate subs.js "properly".
|
|
|
|
group.stream_id = -1;
|
|
|
|
} else {
|
|
|
|
group.stream_id = sub.stream_id;
|
|
|
|
}
|
2014-03-03 22:00:42 +01:00
|
|
|
} else if (group.is_private) {
|
2014-03-17 19:38:35 +01:00
|
|
|
group.pm_with_url = message_container.pm_with_url;
|
2017-01-25 02:16:33 +01:00
|
|
|
group.display_reply_to = message_store.get_pm_full_names(message_container.msg);
|
2014-03-03 22:00:42 +01:00
|
|
|
}
|
2014-03-17 19:38:35 +01:00
|
|
|
group.display_recipient = message_container.msg.display_recipient;
|
2018-11-15 16:41:21 +01:00
|
|
|
group.topic_links = util.get_topic_links(message_container.msg);
|
2016-08-18 22:18:33 +02:00
|
|
|
|
editing: Fix live update of ability to edit messages.
Previously, we didn't check the organization-level settings when
rendering a message list; instead, we only checked it when putting
messages into the message_store. That resulted in the state being
stale in the event that the setting controlling whether one can edit
messages was changed.
We remove some node tests, because revidving the node test for their
new home in message_list_view would be more work than we probably want
to do with an upcoming release. We basically need to be better about
exporting functions like populate_group_from_message_container and
set_topic_edit_properties, so we can do fine grained testing.
When we get around to the node tests, rather than exporting these
functions, it might make sense to create a new module with a name
like message_container.js, which would have all of these
last-second type of data manipulations on message objects. This
would be nice to split out of message_list_view.js. MLV is our
biggest module, and it's mostly cohesive, but it's real job
should be about assembling messages into a DOM list, which is
probably 80% of the code now. The 20% that I'd want to consider
splitting out is actually closer in spirit to message_store.js.
Thanks to Steve Howell for helping with the node tests.
2017-08-23 04:41:43 +02:00
|
|
|
set_topic_edit_properties(group, message_container.msg);
|
|
|
|
|
2016-08-18 22:18:33 +02:00
|
|
|
var time = new XDate(message_container.msg.timestamp * 1000);
|
2017-05-18 21:18:11 +02:00
|
|
|
var today = new XDate();
|
|
|
|
var date_element = timerender.render_date(time, undefined, today)[0];
|
2016-08-18 22:18:33 +02:00
|
|
|
|
|
|
|
group.date = date_element.outerHTML;
|
2014-03-03 22:00:42 +01:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_add_msg_timestring: function (message_container) {
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_container.msg.last_edit_timestamp !== undefined) {
|
2014-03-06 23:11:03 +01:00
|
|
|
// Add or update the last_edit_timestr
|
2014-03-17 19:38:35 +01:00
|
|
|
var last_edit_time = new XDate(message_container.msg.last_edit_timestamp * 1000);
|
2017-05-18 21:18:11 +02:00
|
|
|
var today = new XDate();
|
2014-03-17 19:38:35 +01:00
|
|
|
message_container.last_edit_timestr =
|
2018-06-06 18:19:09 +02:00
|
|
|
timerender.render_date(last_edit_time, undefined, today)[0].textContent
|
2017-12-25 21:43:06 +01:00
|
|
|
+ " at " + timerender.stringify_time(last_edit_time);
|
2014-03-06 23:11:03 +01:00
|
|
|
}
|
|
|
|
},
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2018-05-06 21:43:17 +02:00
|
|
|
add_subscription_marker: function (group, last_msg_container, first_msg_container) {
|
2017-12-22 12:37:26 +01:00
|
|
|
if (last_msg_container === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var last_subscribed = !last_msg_container.msg.historical;
|
|
|
|
var first_subscribed = !first_msg_container.msg.historical;
|
|
|
|
var stream = first_msg_container.msg.stream;
|
|
|
|
|
|
|
|
if (!last_subscribed && first_subscribed) {
|
|
|
|
group.bookend_top = true;
|
|
|
|
group.subscribed = stream;
|
|
|
|
group.bookend_content = this.list.subscribed_bookend_content(stream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_subscribed && !first_subscribed) {
|
2014-03-06 23:11:03 +01:00
|
|
|
group.bookend_top = true;
|
2017-12-22 12:37:26 +01:00
|
|
|
group.unsubscribed = stream;
|
|
|
|
group.bookend_content = this.list.unsubscribed_bookend_content(stream);
|
|
|
|
return;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2014-03-06 23:11:03 +01:00
|
|
|
},
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
build_message_groups: function (message_containers) {
|
2014-02-05 16:55:24 +01:00
|
|
|
function start_group() {
|
2014-03-08 08:59:38 +01:00
|
|
|
return {
|
2014-03-17 19:38:35 +01:00
|
|
|
message_containers: [],
|
2017-01-12 00:17:43 +01:00
|
|
|
message_group_id: _.uniqueId('message_group_'),
|
2014-03-08 08:59:38 +01:00
|
|
|
};
|
2014-02-05 16:55:24 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 23:11:03 +01:00
|
|
|
var self = this;
|
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 = [];
|
2014-03-06 23:11:03 +01:00
|
|
|
var prev;
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
function add_message_container_to_group(message_container) {
|
|
|
|
if (same_sender(prev, message_container)) {
|
2014-03-08 08:59:38 +01:00
|
|
|
prev.next_is_same_sender = true;
|
|
|
|
}
|
2014-03-17 19:38:35 +01:00
|
|
|
current_group.message_containers.push(message_container);
|
2014-02-05 16:55:24 +01:00
|
|
|
}
|
|
|
|
|
2013-08-23 00:14:11 +02:00
|
|
|
function finish_group() {
|
2014-03-17 19:38:35 +01:00
|
|
|
if (current_group.message_containers.length > 0) {
|
2016-12-02 15:16:33 +01:00
|
|
|
populate_group_from_message_container(current_group,
|
|
|
|
current_group.message_containers[0]);
|
|
|
|
current_group
|
2018-05-06 21:43:17 +02:00
|
|
|
.message_containers[current_group.message_containers.length - 1]
|
|
|
|
.include_footer = true;
|
2014-02-05 16:55:24 +01:00
|
|
|
new_message_groups.push(current_group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
_.each(message_containers, function (message_container) {
|
2016-12-02 13:23:23 +01:00
|
|
|
var message_reactions = reactions.get_message_reactions(message_container.msg);
|
|
|
|
message_container.msg.message_reactions = message_reactions;
|
2014-03-17 19:38:35 +01:00
|
|
|
message_container.include_recipient = false;
|
|
|
|
message_container.include_footer = false;
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2016-08-18 22:18:33 +02:00
|
|
|
if (same_recipient(prev, message_container) && self.collapse_messages &&
|
2016-12-02 15:16:33 +01:00
|
|
|
prev.msg.historical === message_container.msg.historical &&
|
|
|
|
same_day(prev, message_container)) {
|
2014-03-17 19:38:35 +01:00
|
|
|
add_message_container_to_group(message_container);
|
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();
|
2014-03-17 19:38:35 +01:00
|
|
|
add_message_container_to_group(message_container);
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
message_container.include_recipient = true;
|
|
|
|
message_container.subscribed = false;
|
|
|
|
message_container.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) {
|
2014-03-17 19:38:35 +01:00
|
|
|
self.add_subscription_marker(current_group, prev, message_container);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2013-10-03 23:05:46 +02:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_container.msg.stream) {
|
2016-12-02 15:16:33 +01:00
|
|
|
message_container.stream_url =
|
2018-12-14 19:18:24 +01:00
|
|
|
hash_util.by_stream_uri(message_container.msg.stream_id);
|
2016-12-02 15:16:33 +01:00
|
|
|
message_container.topic_url =
|
2018-11-13 16:34:19 +01:00
|
|
|
hash_util.by_stream_topic_uri(
|
2018-12-14 19:02:26 +01:00
|
|
|
message_container.msg.stream_id,
|
2018-08-04 16:19:38 +02:00
|
|
|
message_container.msg.subject);
|
2013-10-03 23:05:46 +02:00
|
|
|
} else {
|
2016-12-02 15:16:33 +01:00
|
|
|
message_container.pm_with_url =
|
2017-02-06 20:48:01 +01:00
|
|
|
message_container.msg.pm_with_url;
|
2013-10-03 23:05:46 +02:00
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 23:43:37 +01:00
|
|
|
update_group_time_display(current_group, message_container, prev);
|
|
|
|
update_timestr(message_container);
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
message_container.include_sender = true;
|
|
|
|
if (!message_container.include_recipient &&
|
2013-11-08 02:00:12 +01:00
|
|
|
!prev.status_message &&
|
2014-03-17 19:38:35 +01:00
|
|
|
same_sender(prev, message_container)) {
|
|
|
|
message_container.include_sender = false;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 16:45:06 +01:00
|
|
|
message_container.sender_is_bot = people.sender_is_bot(message_container.msg);
|
2018-10-31 18:15:29 +01:00
|
|
|
message_container.sender_is_guest = people.sender_is_guest(message_container.msg);
|
2017-01-11 16:45:06 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
self._add_msg_timestring(message_container);
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2017-01-21 20:29:39 +01:00
|
|
|
message_container.small_avatar_url = people.small_avatar_url(message_container.msg);
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_container.msg.stream !== undefined) {
|
2016-12-02 15:16:33 +01:00
|
|
|
message_container.background_color =
|
|
|
|
stream_data.get_color(message_container.msg.stream);
|
2014-02-24 19:18:38 +01:00
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2017-08-25 08:47:55 +02:00
|
|
|
message_container.contains_mention = message_container.msg.mentioned;
|
2016-05-31 14:24:58 +02:00
|
|
|
self._maybe_format_me_message(message_container);
|
2013-11-08 02:00:12 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
prev = message_container;
|
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-03-06 23:11:03 +01:00
|
|
|
return new_message_groups;
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
join_message_groups: function (first_group, second_group) {
|
2014-03-08 08:59:38 +01:00
|
|
|
// join_message_groups will combine groups if they have the
|
|
|
|
// same_recipient on the same_day and the view supports collapsing
|
|
|
|
// otherwise it may add a subscription_marker if required.
|
|
|
|
// It returns true if the two groups were joined in to one and
|
|
|
|
// the second_group should be ignored.
|
|
|
|
if (first_group === undefined || second_group === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-17 19:38:35 +01:00
|
|
|
var last_msg_container = _.last(first_group.message_containers);
|
|
|
|
var first_msg_container = _.first(second_group.message_containers);
|
2014-03-08 08:59:38 +01:00
|
|
|
|
|
|
|
// Join two groups into one.
|
2016-12-02 15:16:33 +01:00
|
|
|
if (this.collapse_messages && same_recipient(last_msg_container, first_msg_container) &&
|
|
|
|
same_day(last_msg_container, first_msg_container) &&
|
|
|
|
last_msg_container.msg.historical === first_msg_container.msg.historical) {
|
|
|
|
if (!last_msg_container.status_message && !first_msg_container.msg.is_me_message
|
|
|
|
&& same_sender(last_msg_container, first_msg_container)) {
|
2014-03-17 19:38:35 +01:00
|
|
|
first_msg_container.include_sender = false;
|
2014-03-08 08:59:38 +01:00
|
|
|
}
|
2014-03-17 19:38:35 +01:00
|
|
|
if (same_sender(last_msg_container, first_msg_container)) {
|
|
|
|
last_msg_container.next_is_same_sender = true;
|
2014-03-08 08:59:38 +01:00
|
|
|
}
|
2016-12-02 15:16:33 +01:00
|
|
|
first_group.message_containers =
|
|
|
|
first_group.message_containers.concat(second_group.message_containers);
|
2014-03-08 08:59:38 +01:00
|
|
|
return true;
|
|
|
|
// Add a subscription marker
|
2016-12-02 15:16:33 +01:00
|
|
|
} else if (this.list !== home_msg_list &&
|
|
|
|
last_msg_container.msg.historical !== first_msg_container.msg.historical) {
|
2017-06-06 01:43:26 +02:00
|
|
|
second_group.bookend_top = true;
|
|
|
|
this.add_subscription_marker(second_group, last_msg_container, first_msg_container);
|
2014-03-08 08:59:38 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
merge_message_groups: function (new_message_groups, where) {
|
2014-03-08 08:59:38 +01:00
|
|
|
// merge_message_groups takes a list of new messages groups to add to
|
|
|
|
// this._message_groups and a location where to merge them currently
|
|
|
|
// top or bottom. It returns an object of changes which needed to be
|
|
|
|
// rendered in to the page. The types of actions are append_group,
|
|
|
|
// prepend_group, rerender_group, append_message.
|
|
|
|
//
|
|
|
|
// append_groups are groups to add to the top of the rendered DOM
|
|
|
|
// prepend_groups are group to add to the bottom of the rendered DOM
|
|
|
|
// rerender_groups are group that should be updated in place in the DOM
|
|
|
|
// append_messages are messages which should be added to the last group in the DOM
|
|
|
|
// rerender_messages are messages which should be updated in place in the DOM
|
|
|
|
|
|
|
|
var message_actions = {
|
|
|
|
append_groups: [],
|
|
|
|
prepend_groups: [],
|
|
|
|
rerender_groups: [],
|
|
|
|
append_messages: [],
|
2017-01-12 00:17:43 +01:00
|
|
|
rerender_messages: [],
|
2014-03-08 08:59:38 +01:00
|
|
|
};
|
2016-12-02 17:09:31 +01:00
|
|
|
var first_group;
|
|
|
|
var second_group;
|
2018-11-12 23:43:37 +01:00
|
|
|
var curr_msg_container;
|
|
|
|
var prev_msg_container;
|
2014-03-08 08:59:38 +01:00
|
|
|
|
|
|
|
if (where === 'top') {
|
|
|
|
first_group = _.last(new_message_groups);
|
|
|
|
second_group = _.first(this._message_groups);
|
|
|
|
if (this.join_message_groups(first_group, second_group)) {
|
|
|
|
// join_message_groups moved the old message to the end of the
|
|
|
|
// new group. We need to replace the old rendered message
|
|
|
|
// group. So we will reuse its ID.
|
|
|
|
|
|
|
|
first_group.message_group_id = second_group.message_group_id;
|
|
|
|
message_actions.rerender_groups.push(first_group);
|
|
|
|
|
|
|
|
// Swap the new group in
|
|
|
|
this._message_groups.shift();
|
|
|
|
this._message_groups.unshift(first_group);
|
|
|
|
|
|
|
|
new_message_groups = _.initial(new_message_groups);
|
2016-12-02 15:16:33 +01:00
|
|
|
} else if (!same_day(second_group.message_containers[0],
|
2018-05-06 21:43:17 +02:00
|
|
|
first_group.message_containers[0])) {
|
2018-11-12 23:43:37 +01:00
|
|
|
prev_msg_container = _.last(first_group.message_containers);
|
|
|
|
curr_msg_container = _.first(second_group.message_containers);
|
|
|
|
|
2014-03-30 03:42:48 +02:00
|
|
|
// The groups did not merge, so we need up update the date row for the old group
|
2018-11-12 23:43:37 +01:00
|
|
|
update_group_time_display(second_group, curr_msg_container, prev_msg_container);
|
|
|
|
update_timestr(curr_msg_container);
|
2016-12-02 15:16:33 +01:00
|
|
|
// We could add an action to update the date row, but for now rerender the group.
|
2014-03-30 03:42:48 +02:00
|
|
|
message_actions.rerender_groups.push(second_group);
|
2014-03-08 08:59:38 +01:00
|
|
|
}
|
|
|
|
message_actions.prepend_groups = new_message_groups;
|
|
|
|
this._message_groups = new_message_groups.concat(this._message_groups);
|
|
|
|
} else {
|
|
|
|
first_group = _.last(this._message_groups);
|
|
|
|
second_group = _.first(new_message_groups);
|
|
|
|
if (this.join_message_groups(first_group, second_group)) {
|
|
|
|
// rerender the last message
|
|
|
|
message_actions.rerender_messages.push(
|
2016-12-02 15:16:33 +01:00
|
|
|
first_group.message_containers[
|
|
|
|
first_group.message_containers.length
|
|
|
|
- second_group.message_containers.length - 1]
|
2014-03-08 08:59:38 +01:00
|
|
|
);
|
2014-03-17 19:38:35 +01:00
|
|
|
message_actions.append_messages = _.first(new_message_groups).message_containers;
|
2014-03-08 08:59:38 +01:00
|
|
|
new_message_groups = _.rest(new_message_groups);
|
|
|
|
} else if (first_group !== undefined && second_group !== undefined) {
|
2018-11-12 23:43:37 +01:00
|
|
|
prev_msg_container = _.last(first_group.message_containers);
|
|
|
|
curr_msg_container = _.first(second_group.message_containers);
|
2017-01-06 01:44:34 +01:00
|
|
|
|
2018-11-12 23:43:37 +01:00
|
|
|
if (same_day(prev_msg_container, curr_msg_container)) {
|
2014-03-08 08:59:38 +01:00
|
|
|
// Clear the date if it is the same as the last group
|
2018-11-12 23:43:37 +01:00
|
|
|
second_group.show_date = false;
|
|
|
|
second_group.show_date_separator = undefined;
|
2017-06-05 18:13:02 +02:00
|
|
|
} else {
|
|
|
|
// If we just sent the first message on a new day
|
|
|
|
// in a narrow, make sure we render a date separator.
|
2018-11-12 23:43:37 +01:00
|
|
|
update_group_time_display(second_group, curr_msg_container, prev_msg_container);
|
|
|
|
update_timestr(curr_msg_container);
|
2014-03-08 08:59:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
message_actions.append_groups = new_message_groups;
|
|
|
|
this._message_groups = this._message_groups.concat(new_message_groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
return message_actions;
|
|
|
|
},
|
|
|
|
|
Use jQuery objects in MessageListView._rows.
The values of this dictionary used to be raw DOM elements,
but get_row() wraps them again, so there's not a huge
reason to store them as raw DOM elements internally. It
is slightly easier to reason about the code if everything
stays at the jQuery level.
To preserve the old behavior here, we have to do something
that is kind of ugly, but at least it's explicit now. In
the old code, our cache was DOM elements, and if an id
wasn't in the cache, we would sneakily return $(undefined)
with this code in get_row():
return $(this._rows[id]);
And it turns out that $(undefined) is basically just a
zero-element jQuery object. A lot of our code depends
on this behavior and just works around the zero-element
objects as needed with checks like this:
if (this.selected_row()).length === 0) {
// don't try to get offset
}
For now we just preserve this behavior. We could eventually
be more strict here, or at least have aggressive warnings
on cache misses, but we'd need to retrofit code to be
able to call something like `has_rendered_selection()`
and/or deal with `undefined` as the return value for the case
where the selection hasn't been rendered.
Here is some example code that would cause tracebacks if
we just returned `undefined` for cache misses:
rerender_preserving_scrolltop: function () {
// old_offset is the number of pixels between the top of the
// viewable window and the selected message
var old_offset;
var selected_row = this.selected_row();
var selected_in_view = selected_row.length > 0;
if (selected_in_view) {
old_offset = selected_row.offset().top;
}
return this.rerender_with_target_scrolltop(selected_row,
old_offset);
},
2018-07-12 23:11:59 +02:00
|
|
|
_put_row: function (row) {
|
|
|
|
// row is a jQuery object wrapping one message row
|
2018-07-12 18:29:29 +02:00
|
|
|
if (row.hasClass('message_row')) {
|
Use jQuery objects in MessageListView._rows.
The values of this dictionary used to be raw DOM elements,
but get_row() wraps them again, so there's not a huge
reason to store them as raw DOM elements internally. It
is slightly easier to reason about the code if everything
stays at the jQuery level.
To preserve the old behavior here, we have to do something
that is kind of ugly, but at least it's explicit now. In
the old code, our cache was DOM elements, and if an id
wasn't in the cache, we would sneakily return $(undefined)
with this code in get_row():
return $(this._rows[id]);
And it turns out that $(undefined) is basically just a
zero-element jQuery object. A lot of our code depends
on this behavior and just works around the zero-element
objects as needed with checks like this:
if (this.selected_row()).length === 0) {
// don't try to get offset
}
For now we just preserve this behavior. We could eventually
be more strict here, or at least have aggressive warnings
on cache misses, but we'd need to retrofit code to be
able to call something like `has_rendered_selection()`
and/or deal with `undefined` as the return value for the case
where the selection hasn't been rendered.
Here is some example code that would cause tracebacks if
we just returned `undefined` for cache misses:
rerender_preserving_scrolltop: function () {
// old_offset is the number of pixels between the top of the
// viewable window and the selected message
var old_offset;
var selected_row = this.selected_row();
var selected_in_view = selected_row.length > 0;
if (selected_in_view) {
old_offset = selected_row.offset().top;
}
return this.rerender_with_target_scrolltop(selected_row,
old_offset);
},
2018-07-12 23:11:59 +02:00
|
|
|
this._rows[row.attr('zid')] = row;
|
2018-07-12 18:29:29 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:41:05 +02:00
|
|
|
_post_process: function ($message_rows) {
|
|
|
|
// $message_rows wraps one or more message rows
|
|
|
|
|
|
|
|
if ($message_rows.constructor !== jQuery) {
|
|
|
|
// An assertion check that we're calling this properly
|
|
|
|
blueslip.error('programming error--pass in jQuery objects');
|
|
|
|
}
|
2014-03-08 08:59:38 +01:00
|
|
|
|
|
|
|
var self = this;
|
2018-07-12 17:41:05 +02:00
|
|
|
_.each($message_rows, function (dom_row) {
|
|
|
|
var row = $(dom_row);
|
Use jQuery objects in MessageListView._rows.
The values of this dictionary used to be raw DOM elements,
but get_row() wraps them again, so there's not a huge
reason to store them as raw DOM elements internally. It
is slightly easier to reason about the code if everything
stays at the jQuery level.
To preserve the old behavior here, we have to do something
that is kind of ugly, but at least it's explicit now. In
the old code, our cache was DOM elements, and if an id
wasn't in the cache, we would sneakily return $(undefined)
with this code in get_row():
return $(this._rows[id]);
And it turns out that $(undefined) is basically just a
zero-element jQuery object. A lot of our code depends
on this behavior and just works around the zero-element
objects as needed with checks like this:
if (this.selected_row()).length === 0) {
// don't try to get offset
}
For now we just preserve this behavior. We could eventually
be more strict here, or at least have aggressive warnings
on cache misses, but we'd need to retrofit code to be
able to call something like `has_rendered_selection()`
and/or deal with `undefined` as the return value for the case
where the selection hasn't been rendered.
Here is some example code that would cause tracebacks if
we just returned `undefined` for cache misses:
rerender_preserving_scrolltop: function () {
// old_offset is the number of pixels between the top of the
// viewable window and the selected message
var old_offset;
var selected_row = this.selected_row();
var selected_in_view = selected_row.length > 0;
if (selected_in_view) {
old_offset = selected_row.offset().top;
}
return this.rerender_with_target_scrolltop(selected_row,
old_offset);
},
2018-07-12 23:11:59 +02:00
|
|
|
self._put_row(row);
|
2018-07-12 18:42:27 +02:00
|
|
|
self._post_process_single_row(row);
|
|
|
|
});
|
|
|
|
},
|
2018-07-05 12:17:20 +02:00
|
|
|
|
2018-07-12 18:42:27 +02:00
|
|
|
_post_process_single_row: function (row) {
|
|
|
|
// For message formatting that requires some post-processing
|
|
|
|
// (and is not possible to handle solely via CSS), this is
|
|
|
|
// where we modify the content. It is a goal to minimize how
|
|
|
|
// much logic is present in this function; wherever possible,
|
|
|
|
// we should implement features with the markdown processor,
|
|
|
|
// HTML and CSS.
|
2014-03-08 08:59:38 +01:00
|
|
|
|
2018-07-12 18:42:27 +02:00
|
|
|
if (row.length !== 1) {
|
|
|
|
blueslip.error('programming error--expected single element');
|
|
|
|
}
|
2014-03-08 08:59:38 +01:00
|
|
|
|
2018-07-12 18:42:27 +02:00
|
|
|
var content = row.find('.message_content');
|
2017-10-23 13:54:20 +02:00
|
|
|
|
2018-07-12 18:42:27 +02:00
|
|
|
// Set the rtl class if the text has an rtl direction
|
|
|
|
if (rtl.get_direction(content.text()) === 'rtl') {
|
|
|
|
content.addClass('rtl');
|
|
|
|
}
|
2014-03-08 08:59:38 +01:00
|
|
|
|
2018-07-12 18:42:27 +02:00
|
|
|
if (row.hasClass('mention')) {
|
|
|
|
row.find('.user-mention').each(function () {
|
2018-12-12 22:30:30 +01:00
|
|
|
var user_id = get_user_id_for_mention_button(this);
|
2018-07-12 18:42:27 +02:00
|
|
|
// We give special highlights to the mention buttons
|
|
|
|
// that refer to the current user.
|
2018-12-12 22:30:30 +01:00
|
|
|
if (user_id === "*" || people.is_my_user_id(user_id)) {
|
|
|
|
// Either a wildcard mention or us, so mark it.
|
2018-07-12 18:42:27 +02:00
|
|
|
$(this).addClass('user-mention-me');
|
2018-12-12 22:30:30 +01:00
|
|
|
// TODO: We should probably also mark user groups
|
|
|
|
// you're with the user-mention-me tag.
|
|
|
|
}
|
|
|
|
if (user_id && user_id !== "*" && !$(this).find(".highlight")) {
|
|
|
|
// If it's a mention of a specific user, edit the
|
|
|
|
// mention text to show the user's current name,
|
|
|
|
// assuming that you're not searching for text
|
|
|
|
// inside the highlight.
|
|
|
|
$(this).text("@" + people.get_person_from_user_id(user_id).full_name);
|
2018-07-12 18:42:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display emoji (including realm emoji) as text if
|
|
|
|
// page_params.emojiset is 'text'.
|
|
|
|
if (page_params.emojiset === 'text') {
|
|
|
|
row.find(".emoji").replaceWith(function () {
|
|
|
|
var text = $(this).attr("title");
|
|
|
|
return ":" + text + ":";
|
2018-02-12 15:12:40 +01:00
|
|
|
});
|
2018-07-12 18:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var id = rows.id(row);
|
|
|
|
message_edit.maybe_show_edit(row, id);
|
|
|
|
|
|
|
|
submessage.process_submessages({
|
|
|
|
row: row,
|
|
|
|
message_id: id,
|
2014-03-08 08:59:38 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_get_message_template: function (message_container) {
|
2017-02-02 22:19:39 +01:00
|
|
|
var msg_reactions = reactions.get_message_reactions(message_container.msg);
|
|
|
|
message_container.msg.message_reactions = msg_reactions;
|
|
|
|
var msg_to_render = _.extend(message_container, {
|
|
|
|
table_name: this.table_name,
|
|
|
|
});
|
|
|
|
return templates.render('single_message', msg_to_render);
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
render: function (messages, where, messages_are_new) {
|
2014-03-06 23:11:03 +01:00
|
|
|
// 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 list = this.list; // for convenience
|
|
|
|
var table_name = this.table_name;
|
|
|
|
var table = rows.get_table(table_name);
|
2016-12-02 17:09:31 +01:00
|
|
|
var orig_scrolltop_offset;
|
|
|
|
var message_containers;
|
2014-03-06 23:11:03 +01:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2014-03-14 16:28:54 +01:00
|
|
|
// The messages we are being asked to render are shared with between
|
|
|
|
// all messages lists. To prevent having both list views overwriting
|
|
|
|
// each others data we will make a new message object to add data to
|
|
|
|
// for rendering.
|
2017-06-29 08:33:10 +02:00
|
|
|
message_containers = _.map(messages, function (message) {
|
|
|
|
if (message.starred) {
|
|
|
|
message.starred_status = i18n.t("Unstar");
|
|
|
|
} else {
|
|
|
|
message.starred_status = i18n.t("Star");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {msg: message};
|
|
|
|
});
|
2014-03-14 16:28:54 +01:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
function save_scroll_position() {
|
|
|
|
if (orig_scrolltop_offset === undefined && self.selected_row().length > 0) {
|
|
|
|
orig_scrolltop_offset = self.selected_row().offset().top;
|
2014-03-06 23:11:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
function restore_scroll_position() {
|
|
|
|
if (list === current_msg_list && orig_scrolltop_offset !== undefined) {
|
2018-04-14 01:10:22 +02:00
|
|
|
list.view.set_message_offset(orig_scrolltop_offset);
|
2014-03-08 08:59:38 +01:00
|
|
|
list.reselect_selected_id();
|
|
|
|
}
|
2014-03-06 23:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
// This function processes messages into chunks with separators between them,
|
|
|
|
// and templates them to be inserted as table rows into the DOM.
|
2014-03-06 23:11:03 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_containers.length === 0 || this.table_name === undefined) {
|
2013-08-20 22:05:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
var new_message_groups = this.build_message_groups(message_containers, this.table_name);
|
2014-03-08 08:59:38 +01:00
|
|
|
var message_actions = this.merge_message_groups(new_message_groups, where);
|
|
|
|
var new_dom_elements = [];
|
2016-12-02 17:09:31 +01:00
|
|
|
var rendered_groups;
|
|
|
|
var dom_messages;
|
|
|
|
var last_message_row;
|
|
|
|
var last_group_row;
|
2014-03-08 08:59:38 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
_.each(message_containers, function (message_container) {
|
|
|
|
self.message_containers[message_container.msg.id] = message_container;
|
|
|
|
});
|
2014-03-14 16:28:54 +01:00
|
|
|
|
2014-03-30 03:42:48 +02:00
|
|
|
// Render new message groups on the top
|
|
|
|
if (message_actions.prepend_groups.length > 0) {
|
|
|
|
save_scroll_position();
|
|
|
|
|
|
|
|
rendered_groups = $(templates.render('message_group', {
|
|
|
|
message_groups: message_actions.prepend_groups,
|
2018-05-04 18:51:09 +02:00
|
|
|
use_match_properties: self.list.is_search(),
|
2017-01-12 00:17:43 +01:00
|
|
|
table_name: self.table_name,
|
2014-03-30 03:42:48 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
dom_messages = rendered_groups.find('.message_row');
|
|
|
|
new_dom_elements = new_dom_elements.concat(rendered_groups);
|
|
|
|
|
2018-07-12 17:41:05 +02:00
|
|
|
self._post_process(dom_messages);
|
2014-03-30 03:42:48 +02:00
|
|
|
|
|
|
|
// The date row will be included in the message groups or will be
|
|
|
|
// added in a rerenderd in the group below
|
|
|
|
table.find('.recipient_row').first().prev('.date_row').remove();
|
|
|
|
table.prepend(rendered_groups);
|
|
|
|
condense.condense_and_collapse(dom_messages);
|
|
|
|
}
|
2014-03-14 16:28:54 +01:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
// Rerender message groups
|
|
|
|
if (message_actions.rerender_groups.length > 0) {
|
|
|
|
save_scroll_position();
|
|
|
|
|
|
|
|
_.each(message_actions.rerender_groups, function (message_group) {
|
|
|
|
var old_message_group = $('#' + message_group.message_group_id);
|
|
|
|
// Remove the top date_row, we'll re-add it after rendering
|
|
|
|
old_message_group.prev('.date_row').remove();
|
|
|
|
|
|
|
|
rendered_groups = $(templates.render('message_group', {
|
|
|
|
message_groups: [message_group],
|
2018-05-04 18:51:09 +02:00
|
|
|
use_match_properties: self.list.is_search(),
|
2017-01-12 00:17:43 +01:00
|
|
|
table_name: self.table_name,
|
2014-03-08 08:59:38 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
dom_messages = rendered_groups.find('.message_row');
|
|
|
|
// Not adding to new_dom_elements it is only used for autoscroll
|
|
|
|
|
2018-07-12 17:41:05 +02:00
|
|
|
self._post_process(dom_messages);
|
2014-03-08 08:59:38 +01:00
|
|
|
old_message_group.replaceWith(rendered_groups);
|
|
|
|
condense.condense_and_collapse(dom_messages);
|
|
|
|
});
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
// Rerender message rows
|
|
|
|
if (message_actions.rerender_messages.length > 0) {
|
2014-03-17 19:38:35 +01:00
|
|
|
_.each(message_actions.rerender_messages, function (message_container) {
|
|
|
|
var old_row = self.get_row(message_container.msg.id);
|
2017-02-02 22:19:39 +01:00
|
|
|
var row = $(self._get_message_template(message_container));
|
2018-07-12 17:41:05 +02:00
|
|
|
self._post_process(row);
|
2014-03-08 08:59:38 +01:00
|
|
|
old_row.replaceWith(row);
|
|
|
|
condense.condense_and_collapse(row);
|
|
|
|
list.reselect_selected_id();
|
|
|
|
});
|
|
|
|
}
|
2014-01-17 20:11:54 +01:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
// Insert new messages in to the last message group
|
|
|
|
if (message_actions.append_messages.length > 0) {
|
2014-03-18 18:20:29 +01:00
|
|
|
last_message_row = table.find('.message_row:last').expectOne();
|
2014-03-08 08:59:38 +01:00
|
|
|
last_group_row = rows.get_message_recipient_row(last_message_row);
|
2014-03-17 19:38:35 +01:00
|
|
|
dom_messages = $(_.map(message_actions.append_messages, function (message_container) {
|
2017-02-02 22:19:39 +01:00
|
|
|
return self._get_message_template(message_container);
|
2014-03-18 15:05:20 +01:00
|
|
|
}).join('')).filter('.message_row');
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2018-07-12 17:41:05 +02:00
|
|
|
self._post_process(dom_messages);
|
2014-03-08 08:59:38 +01:00
|
|
|
last_group_row.append(dom_messages);
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2017-03-19 22:28:45 +01:00
|
|
|
condense.condense_and_collapse(dom_messages);
|
2014-03-08 08:59:38 +01:00
|
|
|
new_dom_elements = new_dom_elements.concat(dom_messages);
|
2014-02-05 16:55:24 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
// Add new message groups to the end
|
|
|
|
if (message_actions.append_groups.length > 0) {
|
|
|
|
// Remove the trailing bookend; it'll be re-added after we do our rendering
|
|
|
|
self.clear_trailing_bookend();
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
rendered_groups = $(templates.render('message_group', {
|
|
|
|
message_groups: message_actions.append_groups,
|
2018-05-04 18:51:09 +02:00
|
|
|
use_match_properties: self.list.is_search(),
|
2017-01-12 00:17:43 +01:00
|
|
|
table_name: self.table_name,
|
2014-03-08 08:59:38 +01:00
|
|
|
}));
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
dom_messages = rendered_groups.find('.message_row');
|
|
|
|
new_dom_elements = new_dom_elements.concat(rendered_groups);
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2018-07-12 17:41:05 +02:00
|
|
|
self._post_process(dom_messages);
|
2018-03-21 23:50:47 +01:00
|
|
|
|
|
|
|
// This next line is a workaround for a weird scrolling
|
|
|
|
// bug on Chrome. Basically, in Chrome 64, we had a
|
|
|
|
// highly reproducible bug where if you hit the "End" key
|
|
|
|
// 5 times in a row in a `near:1` narrow (or any other
|
|
|
|
// narrow with enough content below to try this), the 5th
|
|
|
|
// time (because RENDER_WINDOW_SIZE / batch_size = 4,
|
|
|
|
// i.e. the first time we need to rerender to show the
|
|
|
|
// message "End" jumps to) would trigger an unexpected
|
|
|
|
// scroll, resulting in some chaotic scrolling and
|
|
|
|
// additional fetches (from bottom_whitespace ending up in
|
|
|
|
// the view). During debugging, we found that this adding
|
|
|
|
// this next line seems to prevent the Chrome bug from firing.
|
|
|
|
message_viewport.scrollTop();
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
table.append(rendered_groups);
|
2014-03-08 08:59:38 +01:00
|
|
|
condense.condense_and_collapse(dom_messages);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
restore_scroll_position();
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2014-03-08 08:59:38 +01:00
|
|
|
var last_message_group = _.last(self._message_groups);
|
|
|
|
if (last_message_group !== undefined) {
|
2016-12-02 15:16:33 +01:00
|
|
|
list.last_message_historical =
|
|
|
|
_.last(last_message_group.message_containers).msg.historical;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2016-03-22 16:50:09 +01:00
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
var stream_name = narrow_state.stream();
|
2016-03-22 16:50:09 +01:00
|
|
|
if (stream_name !== undefined) {
|
|
|
|
// If user narrows to a stream, doesn't update
|
|
|
|
// trailing bookend if user is subscribed.
|
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2016-09-20 07:09:15 +02:00
|
|
|
if (sub === undefined || !sub.subscribed) {
|
2016-03-22 16:50:09 +01:00
|
|
|
list.update_trailing_bookend();
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 17:10:22 +02:00
|
|
|
|
|
|
|
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-03-17 19:38:35 +01:00
|
|
|
var message_row = self.get_row(message_group.message_containers[0].msg.id);
|
2014-02-21 21:10:09 +01:00
|
|
|
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) {
|
2017-12-27 13:17:58 +01:00
|
|
|
self._maybe_autoscroll(new_dom_elements);
|
2014-02-05 16:55:24 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_maybe_autoscroll: function (rendered_elems) {
|
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 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.
|
2014-03-08 08:59:38 +01:00
|
|
|
if (elem.is("div")) {
|
|
|
|
new_messages_height += elem.height();
|
2013-12-05 22:45:38 +01:00
|
|
|
// starting from the last message, ignore message heights that weren't sent by me.
|
2016-06-09 22:47:12 +02:00
|
|
|
if (id_of_last_message_sent_by_us > -1) {
|
2013-12-13 21:46:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 08:59:38 +01:00
|
|
|
var row_id = rows.id(elem);
|
2013-12-13 21:46:16 +01:00
|
|
|
// check for `row_id` NaN in case we're looking at a date row or bookend row
|
|
|
|
if (row_id > -1 &&
|
2017-01-19 20:18:03 +01:00
|
|
|
people.is_current_user(this.get_message(row_id).sender_email)) {
|
2014-03-08 08:59:38 +01:00
|
|
|
id_of_last_message_sent_by_us = rows.id(elem);
|
2013-12-05 22:45:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
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)
|
2018-06-06 18:50:09 +02:00
|
|
|
if (!(selected_row && selected_row.length > 0 && last_visible)) {
|
2013-08-16 17:10:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selected_row_offset = selected_row.offset().top;
|
2017-03-10 23:48:51 +01:00
|
|
|
var info = message_viewport.message_viewport_info();
|
2013-08-16 17:10:22 +02:00
|
|
|
var available_space_for_scroll = selected_row_offset - info.visible_top;
|
|
|
|
|
|
|
|
// Don't scroll if we can't move the pointer up.
|
|
|
|
if (available_space_for_scroll <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_messages_height <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
message_list: Limit potential damage of auto-scrolling.
We've been getting reports for a few months of folks coming back to
their Zulip window after a night's sleep and finding it scrolled to
the bottom, past dozens or hundreds of messages that they haven't
read. Oddly, the pointer is actually still located where it should be
(verifiable by hitting the Up key), but it's too late: everything
below gets marked as read because bottom_whitespace is in view.
There's only a few places in the zulip codebase where we scroll the
page down, and this is the main one of them. My best theory for what
could be happening is that the browser is, in its overnight
power-saving mode, not granting the Zulip window the resources to
actually repaint the early scrolls. This, in turn, would cause
scrolling down to happen that is not limited by the need to keep the
pointer in view.
I don't think that this fully closes the issue; ideally, we'd have a
reproducer and much more precise detection logic for this situation,
but it should mostly resolve the problem with likely no user-facing
visible harm.
2017-10-04 22:38:31 +02:00
|
|
|
if (!activity.has_focus) {
|
|
|
|
// Don't autoscroll if the window hasn't had focus
|
|
|
|
// recently. This in intended to help protect us from
|
|
|
|
// auto-scrolling downwards when the window is in the
|
|
|
|
// background and might be having some functionality
|
|
|
|
// throttled by modern Chrome's aggressive power-saving
|
|
|
|
// features.
|
|
|
|
blueslip.log("Suppressing scrolldown due to inactivity");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:20:48 +01:00
|
|
|
// do not scroll if there are any active popovers.
|
|
|
|
if (popovers.any_active()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
// 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.
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.system_initiated_animate_scroll(scroll_amount);
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
clear_rendering_state: function (clear_table) {
|
2013-08-16 17:10:22 +02:00
|
|
|
if (clear_table) {
|
|
|
|
this.clear_table();
|
|
|
|
}
|
|
|
|
this.list.last_message_historical = false;
|
|
|
|
|
|
|
|
this._render_win_start = 0;
|
|
|
|
this._render_win_end = 0;
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
update_render_window: function (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;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
maybe_rerender: function () {
|
2013-08-16 17:10:22 +02:00
|
|
|
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
|
2018-06-06 18:50:09 +02:00
|
|
|
if (!(selected_idx - this._render_win_start < this._RENDER_THRESHOLD
|
|
|
|
&& this._render_win_start !== 0 ||
|
|
|
|
this._render_win_end - selected_idx <= this._RENDER_THRESHOLD
|
|
|
|
&& this._render_win_end !== this.list.num_items())) {
|
2013-08-16 17:10:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.update_render_window(selected_idx, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rerender_preserving_scrolltop();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
rerender_preserving_scrolltop: function () {
|
2018-03-11 19:09:17 +01:00
|
|
|
// old_offset is the number of pixels between the top of the
|
2015-01-16 00:26:25 +01:00
|
|
|
// viewable window and the selected message
|
2018-03-11 19:09:17 +01:00
|
|
|
var old_offset;
|
2013-08-16 17:10:22 +02:00
|
|
|
var selected_row = this.selected_row();
|
2018-06-06 18:19:09 +02:00
|
|
|
var selected_in_view = selected_row.length > 0;
|
2013-08-16 17:10:22 +02:00
|
|
|
if (selected_in_view) {
|
2018-03-11 19:09:17 +01:00
|
|
|
old_offset = selected_row.offset().top;
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
2018-04-14 00:47:02 +02:00
|
|
|
return this.rerender_with_target_scrolltop(selected_row, old_offset);
|
|
|
|
},
|
2013-08-16 17:10:22 +02:00
|
|
|
|
2018-04-14 01:10:22 +02:00
|
|
|
set_message_offset: function (offset) {
|
|
|
|
var msg = this.selected_row();
|
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + msg.offset().top - offset);
|
|
|
|
},
|
|
|
|
|
2018-04-14 00:47:02 +02:00
|
|
|
rerender_with_target_scrolltop: function (selected_row, target_offset) {
|
|
|
|
// target_offset is the target number of pixels between the top of the
|
|
|
|
// viewable window and the selected message
|
2013-08-16 17:10:22 +02:00
|
|
|
this.clear_table();
|
2018-04-14 00:47:02 +02:00
|
|
|
this.render(this.list.all_messages().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.
|
2018-04-14 00:47:02 +02:00
|
|
|
if (target_offset !== undefined) {
|
2014-03-13 20:07:33 +01:00
|
|
|
if (this.selected_row().length === 0 && this.list.selected_id() > -1) {
|
|
|
|
this.list.select_id(this.list.selected_id(), {use_closest: true});
|
|
|
|
}
|
2018-03-11 19:09:17 +01:00
|
|
|
|
2018-04-14 01:10:22 +02:00
|
|
|
this.set_message_offset(target_offset);
|
2013-08-16 17:10:22 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_rerender_header: function (message_containers) {
|
2014-03-03 22:00:42 +01:00
|
|
|
// Given a list of messages that are in the **same** message group,
|
|
|
|
// rerender the header / recipient bar of the messages
|
2014-03-17 19:38:35 +01:00
|
|
|
if (message_containers.length === 0) {
|
2014-03-03 22:00:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
var first_row = this.get_row(message_containers[0].msg.id);
|
2014-03-04 23:09:56 +01:00
|
|
|
|
|
|
|
// We may not have the row if the stream or topic was muted
|
|
|
|
if (first_row.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:00:42 +01:00
|
|
|
var recipient_row = rows.get_message_recipient_row(first_row);
|
|
|
|
var header = recipient_row.find('.message_header');
|
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
var group = {message_containers: message_containers};
|
|
|
|
populate_group_from_message_container(group, message_containers[0]);
|
2014-03-03 22:00:42 +01:00
|
|
|
|
|
|
|
var rendered_recipient_row = $(templates.render('recipient_row', group));
|
|
|
|
|
|
|
|
header.replaceWith(rendered_recipient_row);
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_rerender_message: function (message_container, message_content_edited) {
|
2014-03-17 19:38:35 +01:00
|
|
|
var row = this.get_row(message_container.msg.id);
|
|
|
|
var was_selected = this.list.selected_message() === message_container.msg;
|
2014-02-11 16:19:42 +01:00
|
|
|
|
|
|
|
// Re-render just this one message
|
2014-03-17 19:38:35 +01:00
|
|
|
this._add_msg_timestring(message_container);
|
2016-05-31 14:24:58 +02:00
|
|
|
this._maybe_format_me_message(message_container);
|
2014-03-13 03:39:11 +01:00
|
|
|
|
2017-10-21 01:36:08 +02:00
|
|
|
// Make sure the right thing happens if the message was edited to mention us.
|
|
|
|
message_container.contains_mention = message_container.msg.mentioned;
|
|
|
|
|
2017-02-02 22:19:39 +01:00
|
|
|
var rendered_msg = $(this._get_message_template(message_container));
|
2017-04-21 20:27:45 +02:00
|
|
|
if (message_content_edited) {
|
|
|
|
rendered_msg.addClass("fade-in-message");
|
|
|
|
}
|
2018-07-12 17:41:05 +02:00
|
|
|
this._post_process(rendered_msg);
|
2014-03-14 16:28:54 +01:00
|
|
|
row.replaceWith(rendered_msg);
|
2014-03-13 03:39:11 +01:00
|
|
|
|
2014-02-21 16:24:21 +01:00
|
|
|
if (was_selected) {
|
2014-03-17 19:38:35 +01:00
|
|
|
this.list.select_id(message_container.msg.id);
|
2014-02-21 16:24:21 +01:00
|
|
|
}
|
2014-02-11 16:19:42 +01:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
rerender_messages: function (messages, message_content_edited) {
|
2014-02-11 16:19:42 +01:00
|
|
|
var self = this;
|
2014-03-04 16:38:12 +01:00
|
|
|
|
2014-03-14 16:28:54 +01:00
|
|
|
// Convert messages to list messages
|
2014-03-17 19:38:35 +01:00
|
|
|
var message_containers = _.map(messages, function (message) {
|
|
|
|
return self.message_containers[message.id];
|
2014-03-14 16:28:54 +01:00
|
|
|
});
|
2014-03-18 20:46:32 +01:00
|
|
|
// We may not have the message_container if the stream or topic was muted
|
|
|
|
message_containers = _.reject(message_containers, function (message_container) {
|
|
|
|
return message_container === undefined;
|
|
|
|
});
|
2014-03-04 16:38:12 +01:00
|
|
|
|
2014-03-03 22:00:42 +01:00
|
|
|
var message_groups = [];
|
|
|
|
var current_group = [];
|
2014-03-17 19:38:35 +01:00
|
|
|
_.each(message_containers, function (message_container) {
|
2016-12-02 15:16:33 +01:00
|
|
|
if (current_group.length === 0 ||
|
|
|
|
same_recipient(current_group[current_group.length - 1], message_container)) {
|
2014-03-17 19:38:35 +01:00
|
|
|
current_group.push(message_container);
|
2014-03-03 22:00:42 +01:00
|
|
|
} else {
|
|
|
|
message_groups.push(current_group);
|
|
|
|
current_group = [];
|
|
|
|
}
|
2017-04-21 20:27:45 +02:00
|
|
|
self._rerender_message(message_container, message_content_edited);
|
2014-02-11 16:19:42 +01:00
|
|
|
});
|
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) {
|
2017-04-21 20:27:45 +02:00
|
|
|
self._rerender_header(messages_in_group, message_content_edited);
|
2014-03-03 22:00:42 +01:00
|
|
|
});
|
2014-02-11 16:19:42 +01:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
append: function (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
|
2017-01-12 05:27:56 +01:00
|
|
|
// newly received message should trigger a rerender so that
|
2013-08-16 17:10:22 +02:00
|
|
|
// the new message, which will appear in the viewable area,
|
|
|
|
// is rendered.
|
|
|
|
this.maybe_rerender();
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
prepend: function (messages) {
|
2013-08-16 17:10:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-09-29 21:33:12 +02:00
|
|
|
|
|
|
|
// See comment for maybe_rerender call in the append code path
|
|
|
|
this.maybe_rerender();
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
rerender_the_whole_thing: function () {
|
2013-08-16 17:10:22 +02:00
|
|
|
// 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);
|
2016-04-23 00:56:44 +02:00
|
|
|
this.render(this.list.all_messages().slice(this._render_win_start, this._render_win_end), 'bottom');
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
clear_table: function () {
|
2013-08-16 17:10:22 +02:00
|
|
|
// 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 = {};
|
2014-03-18 18:20:29 +01:00
|
|
|
this._message_groups = [];
|
2014-03-17 19:38:35 +01:00
|
|
|
this.message_containers = {};
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
get_row: function (id) {
|
Use jQuery objects in MessageListView._rows.
The values of this dictionary used to be raw DOM elements,
but get_row() wraps them again, so there's not a huge
reason to store them as raw DOM elements internally. It
is slightly easier to reason about the code if everything
stays at the jQuery level.
To preserve the old behavior here, we have to do something
that is kind of ugly, but at least it's explicit now. In
the old code, our cache was DOM elements, and if an id
wasn't in the cache, we would sneakily return $(undefined)
with this code in get_row():
return $(this._rows[id]);
And it turns out that $(undefined) is basically just a
zero-element jQuery object. A lot of our code depends
on this behavior and just works around the zero-element
objects as needed with checks like this:
if (this.selected_row()).length === 0) {
// don't try to get offset
}
For now we just preserve this behavior. We could eventually
be more strict here, or at least have aggressive warnings
on cache misses, but we'd need to retrofit code to be
able to call something like `has_rendered_selection()`
and/or deal with `undefined` as the return value for the case
where the selection hasn't been rendered.
Here is some example code that would cause tracebacks if
we just returned `undefined` for cache misses:
rerender_preserving_scrolltop: function () {
// old_offset is the number of pixels between the top of the
// viewable window and the selected message
var old_offset;
var selected_row = this.selected_row();
var selected_in_view = selected_row.length > 0;
if (selected_in_view) {
old_offset = selected_row.offset().top;
}
return this.rerender_with_target_scrolltop(selected_row,
old_offset);
},
2018-07-12 23:11:59 +02:00
|
|
|
var row = this._rows[id];
|
|
|
|
|
|
|
|
if (row === undefined) {
|
|
|
|
// For legacy reasons we need to return an empty
|
|
|
|
// jQuery object here.
|
|
|
|
return $(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2013-08-16 17:10:22 +02:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
clear_trailing_bookend: function () {
|
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();
|
|
|
|
},
|
|
|
|
|
2018-05-06 21:43:17 +02:00
|
|
|
render_trailing_bookend: function (trailing_bookend_content, subscribed, show_button) {
|
2014-02-05 16:55:24 +01:00
|
|
|
var rendered_trailing_bookend = $(templates.render('bookend', {
|
|
|
|
bookend_content: trailing_bookend_content,
|
2017-06-21 02:20:33 +02:00
|
|
|
trailing: show_button,
|
2017-01-12 00:17:43 +01:00
|
|
|
subscribed: subscribed,
|
2013-08-16 17:10:22 +02:00
|
|
|
}));
|
|
|
|
rows.get_table(this.table_name).append(rendered_trailing_bookend);
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
selected_row: function () {
|
2013-08-16 17:10:22 +02:00
|
|
|
return this.get_row(this.list.selected_id());
|
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
get_message: function (id) {
|
2013-08-16 17:10:22 +02:00
|
|
|
return this.list.get(id);
|
2013-12-19 17:03:08 +01:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
change_message_id: function (old_id, new_id) {
|
2013-12-19 17:03:08 +01:00
|
|
|
if (this._rows[old_id] !== undefined) {
|
|
|
|
var row = this._rows[old_id];
|
|
|
|
delete this._rows[old_id];
|
|
|
|
|
Use jQuery objects in MessageListView._rows.
The values of this dictionary used to be raw DOM elements,
but get_row() wraps them again, so there's not a huge
reason to store them as raw DOM elements internally. It
is slightly easier to reason about the code if everything
stays at the jQuery level.
To preserve the old behavior here, we have to do something
that is kind of ugly, but at least it's explicit now. In
the old code, our cache was DOM elements, and if an id
wasn't in the cache, we would sneakily return $(undefined)
with this code in get_row():
return $(this._rows[id]);
And it turns out that $(undefined) is basically just a
zero-element jQuery object. A lot of our code depends
on this behavior and just works around the zero-element
objects as needed with checks like this:
if (this.selected_row()).length === 0) {
// don't try to get offset
}
For now we just preserve this behavior. We could eventually
be more strict here, or at least have aggressive warnings
on cache misses, but we'd need to retrofit code to be
able to call something like `has_rendered_selection()`
and/or deal with `undefined` as the return value for the case
where the selection hasn't been rendered.
Here is some example code that would cause tracebacks if
we just returned `undefined` for cache misses:
rerender_preserving_scrolltop: function () {
// old_offset is the number of pixels between the top of the
// viewable window and the selected message
var old_offset;
var selected_row = this.selected_row();
var selected_in_view = selected_row.length > 0;
if (selected_in_view) {
old_offset = selected_row.offset().top;
}
return this.rerender_with_target_scrolltop(selected_row,
old_offset);
},
2018-07-12 23:11:59 +02:00
|
|
|
row.attr('zid', new_id);
|
|
|
|
row.attr('id', this.table_name + new_id);
|
|
|
|
row.removeClass('local');
|
2013-12-19 17:03:08 +01:00
|
|
|
this._rows[new_id] = row;
|
|
|
|
}
|
2014-03-14 16:28:54 +01:00
|
|
|
|
2014-03-17 19:38:35 +01:00
|
|
|
if (this.message_containers[old_id] !== undefined) {
|
|
|
|
var message_container = this.message_containers[old_id];
|
|
|
|
delete this.message_containers[old_id];
|
|
|
|
this.message_containers[new_id] = message_container;
|
2014-03-14 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2016-05-31 14:24:58 +02:00
|
|
|
},
|
|
|
|
|
2018-07-12 17:01:51 +02:00
|
|
|
_maybe_format_me_message: function (message_container) {
|
2016-05-31 14:24:58 +02:00
|
|
|
if (message_container.msg.is_me_message) {
|
|
|
|
// Slice the '<p>/me ' off the front, and '</p>' off the end
|
|
|
|
message_container.status_message = message_container.msg.content.slice(4 + 3, -4);
|
|
|
|
message_container.include_sender = true;
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2016-05-31 14:24:58 +02:00
|
|
|
message_container.status_message = false;
|
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-08-16 17:10:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = MessageListView;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.MessageListView = MessageListView;
|