From fd856d728c7a8ed7795da86222fd83deb361db03 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 19 Mar 2017 14:43:38 -0700 Subject: [PATCH] Extract message_util.js --- .eslintrc.json | 1 + static/js/message_events.js | 10 +++++----- static/js/message_fetch.js | 6 +++--- static/js/message_store.js | 29 ---------------------------- static/js/message_util.js | 38 +++++++++++++++++++++++++++++++++++++ static/js/narrow.js | 2 +- static/js/stream_events.js | 2 +- static/js/stream_muting.js | 4 ++-- zproject/settings.py | 1 + 9 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 static/js/message_util.js diff --git a/.eslintrc.json b/.eslintrc.json index 7dac9689b5..24e43ec51a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -87,6 +87,7 @@ "unread": false, "alert_words_ui": false, "message_store": false, + "message_util": false, "message_events": false, "message_fetch": false, "favicon": false, diff --git a/static/js/message_events.js b/static/js/message_events.js index f56331aacd..63e7483a1d 100644 --- a/static/js/message_events.js +++ b/static/js/message_events.js @@ -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); - message_store.add_messages( + message_util.add_messages( new_messages, msg_list, {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 // calling unread.process_loaded_messages. - message_store.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, home_msg_list, {messages_are_new: true}); + message_util.add_messages(messages, message_list.all, {messages_are_new: true}); if (narrow.active()) { 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); } else { // 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); - message_store.do_unread_count_updates(messages); + message_util.do_unread_count_updates(messages); if (narrow.narrowed_by_reply()) { // If you send a message when narrowed to a recipient, move the diff --git a/static/js/message_fetch.js b/static/js/message_fetch.js index e87fae147d..1bcb265723 100644 --- a/static/js/message_fetch.js +++ b/static/js/message_fetch.js @@ -29,12 +29,12 @@ function process_result(messages, opts) { // the message_list.all as well, as the home_msg_list is reconstructed // from message_list.all. if (opts.msg_list === home_msg_list) { - message_store.do_unread_count_updates(messages); - message_store.add_messages(messages, message_list.all, {messages_are_new: false}); + message_util.do_unread_count_updates(messages); + message_util.add_messages(messages, message_list.all, {messages_are_new: false}); } 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); diff --git a/static/js/message_store.js b/static/js/message_store.js index c7c90c66ab..5843745acc 100644 --- a/static/js/message_store.js +++ b/static/js/message_store.js @@ -168,35 +168,6 @@ exports.add_message_metadata = function (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() { this.stored_messages = {}; }; diff --git a/static/js/message_util.js b/static/js/message_util.js new file mode 100644 index 0000000000..16c74fc7af --- /dev/null +++ b/static/js/message_util.js @@ -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; +} diff --git a/static/js/narrow.js b/static/js/narrow.js index 4352b3485b..3676f91ef4 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -315,7 +315,7 @@ exports.activate = function (raw_operators, opts) { // the message we want anyway or if the filter can't be applied // 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}); } diff --git a/static/js/stream_events.js b/static/js/stream_events.js index 364a589c22..bd4bc183c7 100644 --- a/static/js/stream_events.js +++ b/static/js/stream_events.js @@ -94,7 +94,7 @@ exports.mark_subscribed = function (sub, subscribers) { // Update unread counts as the new stream in sidebar might // 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})); }; diff --git a/static/js/stream_muting.js b/static/js/stream_muting.js index 6e0144584f..3cf5289357 100644 --- a/static/js/stream_muting.js +++ b/static/js/stream_muting.js @@ -21,7 +21,7 @@ exports.update_in_home_view = function (sub, value) { home_msg_list.clear({clear_selected_id: false}); // 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 if (ui_state.home_tab_obscured()) { @@ -44,7 +44,7 @@ exports.update_in_home_view = function (sub, value) { pointer.suppress_scroll_pointer_update = true; 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); diff --git a/zproject/settings.py b/zproject/settings.py index f5b0a95539..4039fd60c9 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -878,6 +878,7 @@ JS_SPECS = { 'js/alert_words_ui.js', 'js/attachments_ui.js', 'js/message_store.js', + 'js/message_util.js', 'js/message_events.js', 'js/message_fetch.js', 'js/server_events.js',