From cfd1e8cbc3adb03259bc4ee8b6e1a5840b011169 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 19 Mar 2017 08:00:35 -0700 Subject: [PATCH] Extract stream_muting.js. --- .eslintrc.json | 1 + static/js/stream_muting.js | 63 ++++++++++++++++++++++++++++++++++++++ static/js/subs.js | 54 ++------------------------------ zproject/settings.py | 1 + 4 files changed, 67 insertions(+), 52 deletions(-) create mode 100644 static/js/stream_muting.js diff --git a/.eslintrc.json b/.eslintrc.json index bfd8ca2d7a..2d6edcfc45 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -40,6 +40,7 @@ "compose_fade": false, "modals": false, "subs": false, + "stream_muting": false, "timerender": false, "message_live_update": false, "message_edit": false, diff --git a/static/js/stream_muting.js b/static/js/stream_muting.js new file mode 100644 index 0000000000..6e0144584f --- /dev/null +++ b/static/js/stream_muting.js @@ -0,0 +1,63 @@ +var stream_muting = (function () { + +var exports = {}; + +exports.update_in_home_view = function (sub, value) { + // value is true if we are in home view + // TODO: flip the semantics to be is_muting + sub.in_home_view = value; + + setTimeout(function () { + var msg_offset; + var saved_ypos; + // Save our current scroll position + if (ui_state.home_tab_obscured()) { + saved_ypos = message_viewport.scrollTop(); + } else if (home_msg_list === current_msg_list && + current_msg_list.selected_row().offset() !== null) { + msg_offset = current_msg_list.selected_row().offset().top; + } + + 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); + + // Ensure we're still at the same scroll position + if (ui_state.home_tab_obscured()) { + message_viewport.scrollTop(saved_ypos); + } else if (home_msg_list === current_msg_list) { + // We pass use_closest to handle the case where the + // currently selected message is being hidden from the + // home view + home_msg_list.select_id(home_msg_list.selected_id(), + {use_closest: true, empty_ok: true}); + if (current_msg_list.selected_id() !== -1) { + message_viewport.set_message_offset(msg_offset); + } + } + + // In case we added messages to what's visible in the home view, we need to re-scroll to + // make sure the pointer is still visible. We don't want the auto-scroll handler to move + // our pointer to the old scroll location before we have a chance to update it. + pointer.recenter_pointer_on_display = true; + pointer.suppress_scroll_pointer_update = true; + + if (! home_msg_list.empty()) { + message_store.do_unread_count_updates(home_msg_list.all_messages()); + } + }, 0); + + stream_list.set_in_home_view(sub.name, sub.in_home_view); + + var not_in_home_view_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_setting_not_in_home_view .sub_setting_control"); + not_in_home_view_checkbox.prop('checked', !value); +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = stream_muting; +} + diff --git a/static/js/subs.js b/static/js/subs.js index 1ce9059829..000b118ba6 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -125,58 +125,8 @@ function stream_home_view_clicked(e) { } } -function update_in_home_view(sub, value) { - sub.in_home_view = value; - - setTimeout(function () { - var msg_offset; - var saved_ypos; - // Save our current scroll position - if (ui_state.home_tab_obscured()) { - saved_ypos = message_viewport.scrollTop(); - } else if (home_msg_list === current_msg_list && - current_msg_list.selected_row().offset() !== null) { - msg_offset = current_msg_list.selected_row().offset().top; - } - - 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); - - // Ensure we're still at the same scroll position - if (ui_state.home_tab_obscured()) { - message_viewport.scrollTop(saved_ypos); - } else if (home_msg_list === current_msg_list) { - // We pass use_closest to handle the case where the - // currently selected message is being hidden from the - // home view - home_msg_list.select_id(home_msg_list.selected_id(), - {use_closest: true, empty_ok: true}); - if (current_msg_list.selected_id() !== -1) { - message_viewport.set_message_offset(msg_offset); - } - } - - // In case we added messages to what's visible in the home view, we need to re-scroll to - // make sure the pointer is still visible. We don't want the auto-scroll handler to move - // our pointer to the old scroll location before we have a chance to update it. - pointer.recenter_pointer_on_display = true; - pointer.suppress_scroll_pointer_update = true; - - if (! home_msg_list.empty()) { - message_store.do_unread_count_updates(home_msg_list.all_messages()); - } - }, 0); - - stream_list.set_in_home_view(sub.name, sub.in_home_view); - - var not_in_home_view_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_setting_not_in_home_view .sub_setting_control"); - not_in_home_view_checkbox.prop('checked', !value); -} - exports.toggle_home = function (sub) { - update_in_home_view(sub, ! sub.in_home_view); + stream_muting.update_in_home_view(sub, ! sub.in_home_view); set_stream_property(sub, 'in_home_view', sub.in_home_view); }; @@ -845,7 +795,7 @@ exports.update_subscription_properties = function (stream_id, property, value) { stream_color.update_stream_color(sub, value, {update_historical: true}); break; case 'in_home_view': - update_in_home_view(sub, value); + stream_muting.update_in_home_view(sub, value); break; case 'desktop_notifications': update_stream_desktop_notifications(sub, value); diff --git a/zproject/settings.py b/zproject/settings.py index b38c0b1f28..55638ad328 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -843,6 +843,7 @@ JS_SPECS = { 'js/compose.js', 'js/stream_color.js', 'js/stream_data.js', + 'js/stream_muting.js', 'js/subs.js', 'js/message_edit.js', 'js/condense.js',