Extract message_util.js

This commit is contained in:
Steve Howell 2017-03-19 14:43:38 -07:00 committed by Tim Abbott
parent 8d3d70984d
commit fd856d728c
9 changed files with 52 additions and 41 deletions

View File

@ -87,6 +87,7 @@
"unread": false, "unread": false,
"alert_words_ui": false, "alert_words_ui": false,
"message_store": false, "message_store": false,
"message_util": false,
"message_events": false, "message_events": false,
"message_fetch": false, "message_fetch": false,
"favicon": false, "favicon": false,

View File

@ -33,7 +33,7 @@ function maybe_add_narrowed_messages(messages, msg_list, messages_are_new, local
}); });
new_messages = _.map(new_messages, message_store.add_message_metadata); new_messages = _.map(new_messages, message_store.add_message_metadata);
message_store.add_messages( message_util.add_messages(
new_messages, new_messages,
msg_list, msg_list,
{messages_are_new: messages_are_new} {messages_are_new: messages_are_new}
@ -60,12 +60,12 @@ exports.insert_new_messages = function insert_new_messages(messages, local_id) {
// You must add add messages to home_msg_list BEFORE // You must add add messages to home_msg_list BEFORE
// calling unread.process_loaded_messages. // calling unread.process_loaded_messages.
message_store.add_messages(messages, home_msg_list, {messages_are_new: true}); message_util.add_messages(messages, home_msg_list, {messages_are_new: true});
message_store.add_messages(messages, message_list.all, {messages_are_new: true}); message_util.add_messages(messages, message_list.all, {messages_are_new: true});
if (narrow.active()) { if (narrow.active()) {
if (narrow.filter().can_apply_locally()) { if (narrow.filter().can_apply_locally()) {
message_store.add_messages(messages, message_list.narrowed, {messages_are_new: true}); message_util.add_messages(messages, message_list.narrowed, {messages_are_new: true});
notifications.possibly_notify_new_messages_outside_viewport(messages, local_id); notifications.possibly_notify_new_messages_outside_viewport(messages, local_id);
} else { } else {
// if we cannot apply locally, we have to wait for this callback to happen to notify // if we cannot apply locally, we have to wait for this callback to happen to notify
@ -76,7 +76,7 @@ exports.insert_new_messages = function insert_new_messages(messages, local_id) {
} }
activity.process_loaded_messages(messages); activity.process_loaded_messages(messages);
message_store.do_unread_count_updates(messages); message_util.do_unread_count_updates(messages);
if (narrow.narrowed_by_reply()) { if (narrow.narrowed_by_reply()) {
// If you send a message when narrowed to a recipient, move the // If you send a message when narrowed to a recipient, move the

View File

@ -29,12 +29,12 @@ function process_result(messages, opts) {
// the message_list.all as well, as the home_msg_list is reconstructed // the message_list.all as well, as the home_msg_list is reconstructed
// from message_list.all. // from message_list.all.
if (opts.msg_list === home_msg_list) { if (opts.msg_list === home_msg_list) {
message_store.do_unread_count_updates(messages); message_util.do_unread_count_updates(messages);
message_store.add_messages(messages, message_list.all, {messages_are_new: false}); message_util.add_messages(messages, message_list.all, {messages_are_new: false});
} }
if (messages.length !== 0 && !opts.cont_will_add_messages) { if (messages.length !== 0 && !opts.cont_will_add_messages) {
message_store.add_messages(messages, opts.msg_list, {messages_are_new: false}); message_util.add_messages(messages, opts.msg_list, {messages_are_new: false});
} }
activity.process_loaded_messages(messages); activity.process_loaded_messages(messages);

View File

@ -168,35 +168,6 @@ exports.add_message_metadata = function (message) {
return message; return message;
}; };
exports.add_messages = function add_messages(messages, msg_list, opts) {
if (!messages) {
return;
}
opts = _.extend({messages_are_new: false, delay_render: false}, opts);
loading.destroy_indicator($('#page_loading_indicator'));
$('#first_run_message').remove();
msg_list.add_messages(messages, opts);
if (msg_list === home_msg_list && opts.messages_are_new) {
_.each(messages, function (message) {
if (message.local_id === undefined) {
compose.report_as_received(message);
}
});
}
};
// This function could probably benefit from some refactoring
exports.do_unread_count_updates = function do_unread_count_updates(messages) {
unread.process_loaded_messages(messages);
unread_ui.update_unread_counts();
resize.resize_page_components();
};
exports.clear = function clear() { exports.clear = function clear() {
this.stored_messages = {}; this.stored_messages = {};
}; };

38
static/js/message_util.js Normal file
View File

@ -0,0 +1,38 @@
var message_util = (function () {
var exports = {};
exports.do_unread_count_updates = function do_unread_count_updates(messages) {
unread.process_loaded_messages(messages);
unread_ui.update_unread_counts();
resize.resize_page_components();
};
exports.add_messages = function add_messages(messages, msg_list, opts) {
if (!messages) {
return;
}
opts = _.extend({messages_are_new: false, delay_render: false}, opts);
loading.destroy_indicator($('#page_loading_indicator'));
$('#first_run_message').remove();
msg_list.add_messages(messages, opts);
if (msg_list === home_msg_list && opts.messages_are_new) {
_.each(messages, function (message) {
if (message.local_id === undefined) {
compose.report_as_received(message);
}
});
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = message_util;
}

View File

@ -315,7 +315,7 @@ exports.activate = function (raw_operators, opts) {
// the message we want anyway or if the filter can't be applied // the message we want anyway or if the filter can't be applied
// locally. // locally.
if (message_list.all.get(then_select_id) !== undefined && current_filter.can_apply_locally()) { if (message_list.all.get(then_select_id) !== undefined && current_filter.can_apply_locally()) {
message_store.add_messages(message_list.all.all_messages(), message_list.narrowed, message_util.add_messages(message_list.all.all_messages(), message_list.narrowed,
{delay_render: true}); {delay_render: true});
} }

View File

@ -94,7 +94,7 @@ exports.mark_subscribed = function (sub, subscribers) {
// Update unread counts as the new stream in sidebar might // Update unread counts as the new stream in sidebar might
// need its unread counts re-calculated // need its unread counts re-calculated
message_store.do_unread_count_updates(message_list.all.all_messages()); message_util.do_unread_count_updates(message_list.all.all_messages());
$(document).trigger($.Event('subscription_add_done.zulip', {sub: sub})); $(document).trigger($.Event('subscription_add_done.zulip', {sub: sub}));
}; };

View File

@ -21,7 +21,7 @@ exports.update_in_home_view = function (sub, value) {
home_msg_list.clear({clear_selected_id: false}); home_msg_list.clear({clear_selected_id: false});
// Recreate the home_msg_list with the newly filtered message_list.all // Recreate the home_msg_list with the newly filtered message_list.all
message_store.add_messages(message_list.all.all_messages(), home_msg_list); message_util.add_messages(message_list.all.all_messages(), home_msg_list);
// Ensure we're still at the same scroll position // Ensure we're still at the same scroll position
if (ui_state.home_tab_obscured()) { if (ui_state.home_tab_obscured()) {
@ -44,7 +44,7 @@ exports.update_in_home_view = function (sub, value) {
pointer.suppress_scroll_pointer_update = true; pointer.suppress_scroll_pointer_update = true;
if (! home_msg_list.empty()) { if (! home_msg_list.empty()) {
message_store.do_unread_count_updates(home_msg_list.all_messages()); message_util.do_unread_count_updates(home_msg_list.all_messages());
} }
}, 0); }, 0);

View File

@ -878,6 +878,7 @@ JS_SPECS = {
'js/alert_words_ui.js', 'js/alert_words_ui.js',
'js/attachments_ui.js', 'js/attachments_ui.js',
'js/message_store.js', 'js/message_store.js',
'js/message_util.js',
'js/message_events.js', 'js/message_events.js',
'js/message_fetch.js', 'js/message_fetch.js',
'js/server_events.js', 'js/server_events.js',