message_list: Convert MessageList to an ES6 class.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-07-22 15:25:12 -07:00
committed by Tim Abbott
parent 49db62c240
commit 7249103ab4

View File

@@ -5,7 +5,8 @@ exports.set_narrowed = function (value) {
exports.narrowed = value; exports.narrowed = value;
}; };
exports.MessageList = function (opts) { class MessageList {
constructor(opts) {
if (opts.data) { if (opts.data) {
this.muting_enabled = opts.data.muting_enabled; this.muting_enabled = opts.data.muting_enabled;
this.data = opts.data; this.data = opts.data;
@@ -29,12 +30,9 @@ exports.MessageList = function (opts) {
this.num_appends = 0; this.num_appends = 0;
return this; return this;
}; }
exports.MessageList.prototype = {
add_messages: function MessageList_add_messages(messages, opts) {
const self = this;
add_messages(messages, opts) {
// This adds all messages to our data, but only returns // This adds all messages to our data, but only returns
// the currently viewable ones. // the currently viewable ones.
const info = this.data.add_messages(messages); const info = this.data.add_messages(messages);
@@ -50,77 +48,77 @@ exports.MessageList.prototype = {
let render_info; let render_info;
if (interior_messages.length > 0) { if (interior_messages.length > 0) {
self.view.rerender_preserving_scrolltop(true); this.view.rerender_preserving_scrolltop(true);
return true; return true;
} }
if (top_messages.length > 0) { if (top_messages.length > 0) {
self.view.prepend(top_messages); this.view.prepend(top_messages);
} }
if (bottom_messages.length > 0) { if (bottom_messages.length > 0) {
render_info = self.append_to_view(bottom_messages, opts); render_info = this.append_to_view(bottom_messages, opts);
} }
if (self === exports.narrowed && !self.empty()) { if (this === exports.narrowed && !this.empty()) {
// If adding some new messages to the message tables caused // If adding some new messages to the message tables caused
// our current narrow to no longer be empty, hide the empty // our current narrow to no longer be empty, hide the empty
// feed placeholder text. // feed placeholder text.
narrow.hide_empty_narrow_message(); narrow.hide_empty_narrow_message();
} }
if (self === exports.narrowed && !self.empty() && self.selected_id() === -1) { if (this === exports.narrowed && !this.empty() && this.selected_id() === -1) {
// And also select the newly arrived message. // And also select the newly arrived message.
self.select_id(self.selected_id(), {then_scroll: true, use_closest: true}); this.select_id(this.selected_id(), {then_scroll: true, use_closest: true});
} }
return render_info; return render_info;
}, }
get(id) { get(id) {
return this.data.get(id); return this.data.get(id);
}, }
num_items() { num_items() {
return this.data.num_items(); return this.data.num_items();
}, }
empty() { empty() {
return this.data.empty(); return this.data.empty();
}, }
first() { first() {
return this.data.first(); return this.data.first();
}, }
last() { last() {
return this.data.last(); return this.data.last();
}, }
prev() { prev() {
return this.data.prev(); return this.data.prev();
}, }
next() { next() {
return this.data.next(); return this.data.next();
}, }
is_at_end() { is_at_end() {
return this.data.is_at_end(); return this.data.is_at_end();
}, }
nth_most_recent_id(n) { nth_most_recent_id(n) {
return this.data.nth_most_recent_id(n); return this.data.nth_most_recent_id(n);
}, }
is_search() { is_search() {
return this.data.is_search(); return this.data.is_search();
}, }
can_mark_messages_read() { can_mark_messages_read() {
return this.data.can_mark_messages_read(); return this.data.can_mark_messages_read();
}, }
clear: function MessageList_clear(opts) { clear(opts) {
opts = {clear_selected_id: true, ...opts}; opts = {clear_selected_id: true, ...opts};
this.data.clear(); this.data.clear();
@@ -129,13 +127,13 @@ exports.MessageList.prototype = {
if (opts.clear_selected_id) { if (opts.clear_selected_id) {
this.data.clear_selected_id(); this.data.clear_selected_id();
} }
}, }
selected_id() { selected_id() {
return this.data.selected_id(); return this.data.selected_id();
}, }
select_id: function MessageList_select_id(id, opts) { select_id(id, opts) {
opts = { opts = {
then_scroll: false, then_scroll: false,
target_scroll_offset: undefined, target_scroll_offset: undefined,
@@ -149,13 +147,13 @@ exports.MessageList.prototype = {
previously_selected: this.data.selected_id(), previously_selected: this.data.selected_id(),
}; };
function convert_id(str_id) { const convert_id = (str_id) => {
const id = parseFloat(str_id); const id = parseFloat(str_id);
if (isNaN(id)) { if (isNaN(id)) {
blueslip.fatal("Bad message id " + str_id); blueslip.fatal("Bad message id " + str_id);
} }
return id; return id;
} };
id = convert_id(id); id = convert_id(id);
@@ -198,48 +196,48 @@ exports.MessageList.prototype = {
} }
$(document).trigger($.Event("message_selected.zulip", opts)); $(document).trigger($.Event("message_selected.zulip", opts));
}, }
reselect_selected_id: function MessageList_select_closest_id() { reselect_selected_id() {
this.select_id(this.data.selected_id(), {from_rendering: true}); this.select_id(this.data.selected_id(), {from_rendering: true});
}, }
selected_message: function MessageList_selected_message() { selected_message() {
return this.get(this.data.selected_id()); return this.get(this.data.selected_id());
}, }
selected_row: function MessageList_selected_row() { selected_row() {
return this.get_row(this.data.selected_id()); return this.get_row(this.data.selected_id());
}, }
closest_id(id) { closest_id(id) {
return this.data.closest_id(id); return this.data.closest_id(id);
}, }
advance_past_messages(msg_ids) { advance_past_messages(msg_ids) {
return this.data.advance_past_messages(msg_ids); return this.data.advance_past_messages(msg_ids);
}, }
selected_idx() { selected_idx() {
return this.data.selected_idx(); return this.data.selected_idx();
}, }
subscribed_bookend_content(stream_name) { subscribed_bookend_content(stream_name) {
return i18n.t("You subscribed to stream __stream__", {stream: stream_name}); return i18n.t("You subscribed to stream __stream__", {stream: stream_name});
}, }
unsubscribed_bookend_content(stream_name) { unsubscribed_bookend_content(stream_name) {
return i18n.t("You unsubscribed from stream __stream__", {stream: stream_name}); return i18n.t("You unsubscribed from stream __stream__", {stream: stream_name});
}, }
not_subscribed_bookend_content(stream_name) { not_subscribed_bookend_content(stream_name) {
return i18n.t("You are not subscribed to stream __stream__", {stream: stream_name}); return i18n.t("You are not subscribed to stream __stream__", {stream: stream_name});
}, }
// Maintains a trailing bookend element explaining any changes in // Maintains a trailing bookend element explaining any changes in
// your subscribed/unsubscribed status at the bottom of the // your subscribed/unsubscribed status at the bottom of the
// message list. // message list.
update_trailing_bookend: function MessageList_update_trailing_bookend() { update_trailing_bookend() {
this.view.clear_trailing_bookend(); this.view.clear_trailing_bookend();
if (!this.narrowed) { if (!this.narrowed) {
return; return;
@@ -273,16 +271,16 @@ exports.MessageList.prototype = {
if (trailing_bookend_content !== undefined) { if (trailing_bookend_content !== undefined) {
this.view.render_trailing_bookend(trailing_bookend_content, subscribed, show_button); this.view.render_trailing_bookend(trailing_bookend_content, subscribed, show_button);
} }
}, }
unmuted_messages(messages) { unmuted_messages(messages) {
return this.data.unmuted_messages(messages); return this.data.unmuted_messages(messages);
}, }
append: function MessageList_append(messages, opts) { append(messages, opts) {
const viewable_messages = this.data.append(messages); const viewable_messages = this.data.append(messages);
this.append_to_view(viewable_messages, opts); this.append_to_view(viewable_messages, opts);
}, }
append_to_view(messages, opts) { append_to_view(messages, opts) {
opts = {messages_are_new: false, ...opts}; opts = {messages_are_new: false, ...opts};
@@ -290,14 +288,14 @@ exports.MessageList.prototype = {
this.num_appends += 1; this.num_appends += 1;
const render_info = this.view.append(messages, opts.messages_are_new); const render_info = this.view.append(messages, opts.messages_are_new);
return render_info; return render_info;
}, }
remove_and_rerender: function MessageList_remove_and_rerender(messages) { remove_and_rerender(messages) {
this.data.remove(messages); this.data.remove(messages);
this.rerender(); this.rerender();
}, }
show_edit_message: function MessageList_show_edit_message(row, edit_obj) { show_edit_message(row, edit_obj) {
if (row.find(".message_edit_form form").length !== 0) { if (row.find(".message_edit_form form").length !== 0) {
return; return;
} }
@@ -305,14 +303,14 @@ exports.MessageList.prototype = {
row.find(".message_content, .status-message, .message_controls").hide(); row.find(".message_content, .status-message, .message_controls").hide();
row.find(".message_edit").css("display", "block"); row.find(".message_edit").css("display", "block");
autosize(row.find(".message_edit_content")); autosize(row.find(".message_edit_content"));
}, }
hide_edit_message: function MessageList_hide_edit_message(row) { hide_edit_message(row) {
row.find(".message_content, .status-message, .message_controls").show(); row.find(".message_content, .status-message, .message_controls").show();
row.find(".message_edit_form").empty(); row.find(".message_edit_form").empty();
row.find(".message_edit").hide(); row.find(".message_edit").hide();
row.trigger("mouseleave"); row.trigger("mouseleave");
}, }
show_edit_topic_on_recipient_row(recipient_row, form) { show_edit_topic_on_recipient_row(recipient_row, form) {
recipient_row.find(".topic_edit_form").append(form); recipient_row.find(".topic_edit_form").append(form);
@@ -320,7 +318,7 @@ exports.MessageList.prototype = {
recipient_row.find(".edit_content_button").hide(); recipient_row.find(".edit_content_button").hide();
recipient_row.find(".stream_topic").hide(); recipient_row.find(".stream_topic").hide();
recipient_row.find(".topic_edit").show(); recipient_row.find(".topic_edit").show();
}, }
hide_edit_topic_on_recipient_row(recipient_row) { hide_edit_topic_on_recipient_row(recipient_row) {
recipient_row.find(".stream_topic").show(); recipient_row.find(".stream_topic").show();
@@ -328,7 +326,7 @@ exports.MessageList.prototype = {
recipient_row.find(".edit_content_button").show(); recipient_row.find(".edit_content_button").show();
recipient_row.find(".topic_edit_form").empty(); recipient_row.find(".topic_edit_form").empty();
recipient_row.find(".topic_edit").hide(); recipient_row.find(".topic_edit").hide();
}, }
show_message_as_read(message, options) { show_message_as_read(message, options) {
const row = this.get_row(message.id); const row = this.get_row(message.id);
@@ -338,9 +336,9 @@ exports.MessageList.prototype = {
row.find(".unread_marker").addClass("slow_fade"); row.find(".unread_marker").addClass("slow_fade");
} }
row.removeClass("unread"); row.removeClass("unread");
}, }
rerender: function MessageList_rerender() { rerender() {
// We need to clear the rendering state, rather than just // We need to clear the rendering state, rather than just
// doing clear_table, since we want to potentially recollapse // doing clear_table, since we want to potentially recollapse
// things. // things.
@@ -358,7 +356,7 @@ exports.MessageList.prototype = {
this.view.rerender_preserving_scrolltop(); this.view.rerender_preserving_scrolltop();
this.redo_selection(); this.redo_selection();
}, }
redo_selection() { redo_selection() {
const selected_id = this.data.selected_id(); const selected_id = this.data.selected_id();
@@ -366,73 +364,71 @@ exports.MessageList.prototype = {
if (selected_id !== -1) { if (selected_id !== -1) {
this.select_id(selected_id); this.select_id(selected_id);
} }
}, }
update_muting_and_rerender: function MessageList_update_muting_and_rerender() { update_muting_and_rerender() {
if (!this.muting_enabled) { if (!this.muting_enabled) {
return; return;
} }
this.data.update_items_for_muting(); this.data.update_items_for_muting();
this.rerender(); this.rerender();
}, }
all_messages: function MessageList_all_messages() { all_messages() {
return this.data.all_messages(); return this.data.all_messages();
}, }
first_unread_message_id() { first_unread_message_id() {
return this.data.first_unread_message_id(); return this.data.first_unread_message_id();
}, }
message_range(start, end) { message_range(start, end) {
return this.data.message_range(start, end); return this.data.message_range(start, end);
}, }
get_row(id) { get_row(id) {
return this.view.get_row(id); return this.view.get_row(id);
}, }
update_user_full_name(user_id, full_name) { update_user_full_name(user_id, full_name) {
this.data.update_user_full_name(user_id, full_name); this.data.update_user_full_name(user_id, full_name);
if (this.table_name !== undefined) { if (this.table_name !== undefined) {
this.view.rerender_preserving_scrolltop(); this.view.rerender_preserving_scrolltop();
} }
}, }
update_user_avatar(user_id, avatar_url) { update_user_avatar(user_id, avatar_url) {
this.data.update_user_avatar(user_id, avatar_url); this.data.update_user_avatar(user_id, avatar_url);
if (this.table_name !== undefined) { if (this.table_name !== undefined) {
this.view.rerender_preserving_scrolltop(); this.view.rerender_preserving_scrolltop();
} }
}, }
update_stream_name(stream_id, new_stream_name) { update_stream_name(stream_id, new_stream_name) {
this.data.update_stream_name(stream_id, new_stream_name); this.data.update_stream_name(stream_id, new_stream_name);
if (this.table_name !== undefined) { if (this.table_name !== undefined) {
this.view.rerender_preserving_scrolltop(); this.view.rerender_preserving_scrolltop();
} }
}, }
change_message_id: function MessageList_change_message_id(old_id, new_id) { change_message_id(old_id, new_id) {
const self = this;
const opts = { const opts = {
is_current_list() { is_current_list: () => current_msg_list === this,
return current_msg_list === self; re_render: () => {
}, this.view.rerender_preserving_scrolltop();
re_render() { this.redo_selection();
self.view.rerender_preserving_scrolltop();
self.redo_selection();
}, },
}; };
this.data.change_message_id(old_id, new_id, opts); this.data.change_message_id(old_id, new_id, opts);
}, }
get_last_message_sent_by_me() { get_last_message_sent_by_me() {
return this.data.get_last_message_sent_by_me(); return this.data.get_last_message_sent_by_me();
}, }
}; }
exports.MessageList = MessageList;
exports.all = new exports.MessageList({ exports.all = new MessageList({
muting_enabled: false, muting_enabled: false,
}); });