From 1d7d6869c9587d1e934faf25e3261b13078720eb Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 19 Mar 2017 08:03:07 -0700 Subject: [PATCH] Extract stream_events.js --- .eslintrc.json | 1 + frontend_tests/node_tests/dispatch.js | 4 +- frontend_tests/node_tests/server_events.js | 4 +- static/js/server_events.js | 4 +- static/js/stream_events.js | 70 ++++++++++++++++++++++ static/js/subs.js | 68 ++------------------- zproject/settings.py | 1 + 7 files changed, 82 insertions(+), 70 deletions(-) create mode 100644 static/js/stream_events.js diff --git a/.eslintrc.json b/.eslintrc.json index 2d6edcfc45..e50cbe5cc4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -41,6 +41,7 @@ "modals": false, "subs": false, "stream_muting": false, + "stream_events": false, "timerender": false, "message_live_update": false, "message_edit": false, diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 9218c694f1..3f86c3819b 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -633,7 +633,7 @@ with_overrides(function (override) { var event = event_fixtures.stream; global.with_stub(function (stub) { - override('subs.update_subscription_properties', stub.f); + override('stream_events.update_property', stub.f); override('admin.update_default_streams_table', noop); dispatch(event); var args = stub.get_args('stream_id', 'property', 'value'); @@ -700,7 +700,7 @@ with_overrides(function (override) { event = event_fixtures.subscription__update; global.with_stub(function (stub) { - override('subs.update_subscription_properties', stub.f); + override('stream_events.update_property', stub.f); dispatch(event); var args = stub.get_args('stream_id', 'property', 'value'); assert_same(args.stream_id, event.stream_id); diff --git a/frontend_tests/node_tests/server_events.js b/frontend_tests/node_tests/server_events.js index 736485b5f6..d944949a4c 100644 --- a/frontend_tests/node_tests/server_events.js +++ b/frontend_tests/node_tests/server_events.js @@ -46,8 +46,8 @@ var setup = function (results) { throw Error('update error'); }, }); - set_global('subs', { - update_subscription_properties: function () { + set_global('stream_events', { + update_property: function () { throw Error('subs update error'); }, }); diff --git a/static/js/server_events.js b/static/js/server_events.js index ecd4a9d3f5..6ef5838802 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -161,7 +161,7 @@ function dispatch_normal_event(event) { case 'stream': if (event.op === 'update') { // Legacy: Stream properties are still managed by subs.js on the client side. - subs.update_subscription_properties( + stream_events.update_property( event.stream_id, event.property, event.value @@ -229,7 +229,7 @@ function dispatch_normal_event(event) { subs.mark_sub_unsubscribed(sub); }); } else if (event.op === 'update') { - subs.update_subscription_properties( + stream_events.update_property( event.stream_id, event.property, event.value diff --git a/static/js/stream_events.js b/static/js/stream_events.js new file mode 100644 index 0000000000..0805c4ee06 --- /dev/null +++ b/static/js/stream_events.js @@ -0,0 +1,70 @@ +var stream_events = (function () { + +var exports = {}; + +function update_stream_desktop_notifications(sub, value) { + var desktop_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_desktop_notifications_setting .sub_setting_control"); + desktop_notifications_checkbox.prop('checked', value); + sub.desktop_notifications = value; +} + +function update_stream_audible_notifications(sub, value) { + var audible_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_audible_notifications_setting .sub_setting_control"); + audible_notifications_checkbox.prop('checked', value); + sub.audible_notifications = value; +} + +function update_stream_pin(sub, value) { + var pin_checkbox = $('#pinstream-' + sub.stream_id); + pin_checkbox.prop('checked', value); + sub.pin_to_top = value; +} + +exports.update_property = function (stream_id, property, value) { + var sub = stream_data.get_sub_by_id(stream_id); + if (sub === undefined) { + // This isn't a stream we know about, so ignore it. + blueslip.warn("Update for an unknown subscription", {stream_id: stream_id, + property: property, + value: value}); + return; + } + + switch (property) { + case 'color': + stream_color.update_stream_color(sub, value, {update_historical: true}); + break; + case 'in_home_view': + stream_muting.update_in_home_view(sub, value); + break; + case 'desktop_notifications': + update_stream_desktop_notifications(sub, value); + break; + case 'audible_notifications': + update_stream_audible_notifications(sub, value); + break; + case 'name': + subs.update_stream_name(sub, value); + break; + case 'description': + subs.update_stream_description(sub, value); + break; + case 'email_address': + sub.email_address = value; + break; + case 'pin_to_top': + update_stream_pin(sub, value); + stream_list.refresh_pinned_or_unpinned_stream(sub); + break; + default: + blueslip.warn("Unexpected subscription property type", {property: property, + value: value}); + } +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = stream_events; +} diff --git a/static/js/subs.js b/static/js/subs.js index 000b118ba6..aea87b2fc6 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -134,25 +134,7 @@ exports.toggle_pin_to_top_stream = function (sub) { set_stream_property(sub, 'pin_to_top', !sub.pin_to_top); }; -function update_stream_desktop_notifications(sub, value) { - var desktop_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_desktop_notifications_setting .sub_setting_control"); - desktop_notifications_checkbox.prop('checked', value); - sub.desktop_notifications = value; -} - -function update_stream_audible_notifications(sub, value) { - var audible_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_audible_notifications_setting .sub_setting_control"); - audible_notifications_checkbox.prop('checked', value); - sub.audible_notifications = value; -} - -function update_stream_pin(sub, value) { - var pin_checkbox = $('#pinstream-' + sub.stream_id); - pin_checkbox.prop('checked', value); - sub.pin_to_top = value; -} - -function update_stream_name(sub, new_name) { +exports.update_stream_name = function (sub, new_name) { // Rename the stream internally. stream_data.rename_sub(sub, new_name); var stream_id = sub.stream_id; @@ -171,9 +153,9 @@ function update_stream_name(sub, new_name) { // Update the message feed. message_live_update.update_stream_name(stream_id, new_name); -} +}; -function update_stream_description(sub, description) { +exports.update_stream_description = function (sub, description) { sub.description = description; // Update stream row @@ -184,7 +166,7 @@ function update_stream_description(sub, description) { var stream_settings = settings_for_sub(sub); stream_settings.find('input.description').val(description); stream_settings.find('.stream-description-editable').text(description); -} +}; function stream_desktop_notifications_clicked(e) { var sub = get_sub_for_target(e.target); @@ -780,48 +762,6 @@ exports.close = function () { subs.remove_miscategorized_streams(); }; -exports.update_subscription_properties = function (stream_id, property, value) { - var sub = stream_data.get_sub_by_id(stream_id); - if (sub === undefined) { - // This isn't a stream we know about, so ignore it. - blueslip.warn("Update for an unknown subscription", {stream_id: stream_id, - property: property, - value: value}); - return; - } - - switch (property) { - case 'color': - stream_color.update_stream_color(sub, value, {update_historical: true}); - break; - case 'in_home_view': - stream_muting.update_in_home_view(sub, value); - break; - case 'desktop_notifications': - update_stream_desktop_notifications(sub, value); - break; - case 'audible_notifications': - update_stream_audible_notifications(sub, value); - break; - case 'name': - update_stream_name(sub, value); - break; - case 'description': - update_stream_description(sub, value); - break; - case 'email_address': - sub.email_address = value; - break; - case 'pin_to_top': - update_stream_pin(sub, value); - stream_list.refresh_pinned_or_unpinned_stream(sub); - break; - default: - blueslip.warn("Unexpected subscription property type", {property: property, - value: value}); - } -}; - function ajaxSubscribe(stream) { // Subscribe yourself to a single stream. var true_stream_name; diff --git a/zproject/settings.py b/zproject/settings.py index 55638ad328..c72e4cab6c 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -844,6 +844,7 @@ JS_SPECS = { 'js/stream_color.js', 'js/stream_data.js', 'js/stream_muting.js', + 'js/stream_events.js', 'js/subs.js', 'js/message_edit.js', 'js/condense.js',