diff --git a/static/js/subs.js b/static/js/subs.js index 04ecd02b06..165db5c341 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -58,6 +58,22 @@ function set_stream_property(stream_name, property, value) { }); } +function set_notification_setting_for_all_streams(notification_type, new_setting) { + _.each(stream_data.subscribed_subs(), function (sub) { + if (sub[notification_type] !== new_setting) { + set_stream_property(sub.name, notification_type, new_setting); + } + }); +} + +exports.set_all_stream_desktop_notifications_to = function (new_setting) { + set_notification_setting_for_all_streams("desktop_notifications", new_setting); +}; + +exports.set_all_stream_audible_notifications_to = function (new_setting) { + set_notification_setting_for_all_streams("audible_notifications", new_setting); +}; + function stream_home_view_clicked(e) { var sub_row = $(e.target).closest('.subscription_row'); var stream = sub_row.find('.subscription_name').text(); diff --git a/static/js/ui.js b/static/js/ui.js index f6f85c2bf3..9ada8b29e4 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -1155,6 +1155,54 @@ $(function () { update_notification_settings_error); }); + function update_global_stream_setting(notification_type, new_setting) { + var data = {}; + data[notification_type] = new_setting; + channel.post({ + url: "/json/notify_settings/change", + data: data, + success: update_notification_settings_success, + error: update_notification_settings_error + }); + } + + function update_desktop_notification_setting(new_setting) { + update_global_stream_setting("enable_stream_desktop_notifications", new_setting); + subs.set_all_stream_desktop_notifications_to(new_setting); + } + + function update_audible_notification_setting(new_setting) { + update_global_stream_setting("enable_stream_sounds", new_setting); + subs.set_all_stream_audible_notifications_to(new_setting); + } + + function maybe_bulk_update_stream_notification_setting(notification_checkbox, + propagate_setting_function) { + var html = templates.render("propagate_notification_change"); + var control_group = notification_checkbox.closest(".control-group"); + var checkbox_status = notification_checkbox.is(":checked"); + control_group.find(".propagate_stream_notifications_change").html(html); + control_group.find(".yes_propagate_notifications").on("click", function (e) { + propagate_setting_function(checkbox_status); + control_group.find(".propagate_stream_notifications_change").empty(); + }); + control_group.find(".no_propagate_notifications").on("click", function (e) { + control_group.find(".propagate_stream_notifications_change").empty(); + }); + } + + $("#enable_stream_desktop_notifications").on("click", function (e) { + var notification_checkbox = $("#enable_stream_desktop_notifications"); + maybe_bulk_update_stream_notification_setting(notification_checkbox, + update_desktop_notification_setting); + }); + + $("#enable_stream_sounds").on("click", function (e) { + var notification_checkbox = $("#enable_stream_sounds"); + maybe_bulk_update_stream_notification_setting(notification_checkbox, + update_audible_notification_setting); + }); + if (feature_flags.show_autoscroll_forever_option) { $("form.ui-settings").expectOne().ajaxForm({ dataType: 'json', diff --git a/static/templates/propagate_notification_change.handlebars b/static/templates/propagate_notification_change.handlebars new file mode 100644 index 0000000000..ab00b59122 --- /dev/null +++ b/static/templates/propagate_notification_change.handlebars @@ -0,0 +1,12 @@ +
Apply this change to all stream subscriptions?
+ +Change notification settings for diff --git a/zerver/tests/frontend/node/templates.js b/zerver/tests/frontend/node/templates.js index 17ad89740b..8bb24094d2 100644 --- a/zerver/tests/frontend/node/templates.js +++ b/zerver/tests/frontend/node/templates.js @@ -663,6 +663,15 @@ function render(template_name, args) { assert.equal(a.text().trim(), 'Narrow to private messages with Hamlet'); }()); +(function notification_docs() { + var html = render('propagate_notification_change'); + global.write_test_output("propagate_notification_change.handlebars", html); + + var button_area = $(html).find(".propagate-notifications-controls"); + assert.equal(button_area.find(".yes_propagate_notifications").text().trim(), 'Yes'); + assert.equal(button_area.find(".no_propagate_notifications").text().trim(), 'No'); +}()); + // By the end of this test, we should have compiled all our templates. Ideally, // we will also have exercised them to some degree, but that's a little trickier // to enforce.