From 1f38884b27ed3f999bbb1e59cb4b06eee6ee15d6 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 6 Apr 2017 07:01:07 -0700 Subject: [PATCH] Extract settings_notifications.js. --- .eslintrc.json | 1 + static/js/server_events.js | 2 +- static/js/settings.js | 147 +------------------------ static/js/settings_notifications.js | 161 ++++++++++++++++++++++++++++ tools/js-dep-visualizer.py | 1 + zproject/settings.py | 1 + 6 files changed, 166 insertions(+), 147 deletions(-) create mode 100644 static/js/settings_notifications.js diff --git a/.eslintrc.json b/.eslintrc.json index 2c0c5525c0..cee41ce181 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -34,6 +34,7 @@ "navigate": false, "settings_account": false, "settings_display": false, + "settings_notifications": false, "settings": false, "resize": false, "loading": false, diff --git a/static/js/server_events.js b/static/js/server_events.js index ed895e4e5f..5cd982ad9c 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -302,7 +302,7 @@ function dispatch_normal_event(event) { notifications.handle_global_notification_updates(event.notification_name, event.setting); if ($("#settings.tab-pane.active").length) { - settings.update_page(); + settings_notifications.update_page(); } break; diff --git a/static/js/settings.js b/static/js/settings.js index de1cd01fd7..467a23d760 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -238,13 +238,13 @@ function _setup_page() { }); $(".settings-box").html(settings_tab); - $("#notify-settings-status").hide(); $("#ui-settings-status").hide(); alert_words_ui.set_up_alert_words(); attachments_ui.set_up_attachments(); settings_account.set_up(); settings_display.set_up(); + settings_notifications.set_up(); $("#api_key_value").text(""); $("#get_api_key_box").hide(); @@ -272,135 +272,6 @@ function _setup_page() { $('.new-bot-ui').hide(); } - function update_notification_settings_success(resp, statusText, xhr) { - var result = JSON.parse(xhr.responseText); - var notify_settings_status = $('#notify-settings-status').expectOne(); - - // Stream notification settings. - - if (result.enable_stream_desktop_notifications !== undefined) { - page_params.stream_desktop_notifications_enabled = - result.enable_stream_desktop_notifications; - } - if (result.enable_stream_sounds !== undefined) { - page_params.stream_sounds_enabled = result.enable_stream_sounds; - } - - // PM and @-mention notification settings. - - if (result.enable_desktop_notifications !== undefined) { - page_params.desktop_notifications_enabled = result.enable_desktop_notifications; - } - if (result.enable_sounds !== undefined) { - page_params.sounds_enabled = result.enable_sounds; - } - - if (result.enable_offline_email_notifications !== undefined) { - page_params.enable_offline_email_notifications = - result.enable_offline_email_notifications; - } - - if (result.enable_offline_push_notifications !== undefined) { - page_params.enable_offline_push_notifications = - result.enable_offline_push_notifications; - } - - if (result.enable_online_push_notifications !== undefined) { - page_params.enable_online_push_notifications = result.enable_online_push_notifications; - } - - if (result.pm_content_in_desktop_notifications !== undefined) { - page_params.pm_content_in_desktop_notifications - = result.pm_content_in_desktop_notifications; - } - // Other notification settings. - - if (result.enable_digest_emails !== undefined) { - page_params.enable_digest_emails = result.enable_digest_emails; - } - - ui_report.success(i18n.t("Updated notification settings!"), notify_settings_status); - } - - function update_notification_settings_error(xhr) { - ui_report.error(i18n.t("Error changing settings"), xhr, $('#notify-settings-status').expectOne()); - } - - function post_notify_settings_changes(notification_changes, success_func, - error_func) { - return channel.patch({ - url: "/json/settings/notifications", - data: notification_changes, - success: success_func, - error: error_func, - }); - } - - $("#change_notification_settings").on("click", function (e) { - e.preventDefault(); - - var updated_settings = {}; - _.each(["enable_stream_desktop_notifications", "enable_stream_sounds", - "enable_desktop_notifications", "pm_content_in_desktop_notifications", "enable_sounds", - "enable_offline_email_notifications", - "enable_offline_push_notifications", "enable_online_push_notifications", - "enable_digest_emails"], - function (setting) { - updated_settings[setting] = $("#" + setting).is(":checked"); - }); - post_notify_settings_changes(updated_settings, - update_notification_settings_success, - update_notification_settings_error); - }); - - function update_global_stream_setting(notification_type, new_setting) { - var data = {}; - data[notification_type] = new_setting; - channel.patch({ - url: "/json/settings/notifications", - 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 () { - propagate_setting_function(checkbox_status); - control_group.find(".propagate_stream_notifications_change").empty(); - }); - control_group.find(".no_propagate_notifications").on("click", function () { - control_group.find(".propagate_stream_notifications_change").empty(); - }); - } - - $("#enable_stream_desktop_notifications").on("click", function () { - 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 () { - var notification_checkbox = $("#enable_stream_sounds"); - maybe_bulk_update_stream_notification_setting(notification_checkbox, - update_audible_notification_setting); - }); - $("#get_api_key_box").hide(); $("#show_api_key_box").hide(); $("#get_api_key_box form").ajaxForm({ @@ -718,26 +589,10 @@ function _setup_page() { }); } -function _update_page() { - $("#enable_stream_desktop_notifications").prop('checked', page_params.stream_desktop_notifications_enabled); - $("#enable_stream_sounds").prop('checked', page_params.stream_sounds_enabled); - $("#enable_desktop_notifications").prop('checked', page_params.desktop_notifications_enabled); - $("#enable_sounds").prop('checked', page_params.sounds_enabled); - $("#enable_offline_email_notifications").prop('checked', page_params.enable_offline_email_notifications); - $("#enable_offline_push_notifications").prop('checked', page_params.enable_offline_push_notifications); - $("#enable_online_push_notifications").prop('checked', page_params.enable_online_push_notifications); - $("#pm_content_in_desktop_notifications").prop('checked', page_params.pm_content_in_desktop_notifications); - $("#enable_digest_emails").prop('checked', page_params.enable_digest_emails); -} - exports.setup_page = function () { i18n.ensure_i18n(_setup_page); }; -exports.update_page = function () { - i18n.ensure_i18n(_update_page); -}; - exports.launch_page = function (tab) { var $active_tab = $("#settings_overlay_container li[data-section='" + tab + "']"); diff --git a/static/js/settings_notifications.js b/static/js/settings_notifications.js new file mode 100644 index 0000000000..b0f1a341fa --- /dev/null +++ b/static/js/settings_notifications.js @@ -0,0 +1,161 @@ +var settings_notifications = (function () { + +var exports = {}; + +exports.set_up = function () { + $("#notify-settings-status").hide(); + + function update_notification_settings_success(resp, statusText, xhr) { + var result = JSON.parse(xhr.responseText); + var notify_settings_status = $('#notify-settings-status').expectOne(); + + // Stream notification settings. + + if (result.enable_stream_desktop_notifications !== undefined) { + page_params.stream_desktop_notifications_enabled = + result.enable_stream_desktop_notifications; + } + if (result.enable_stream_sounds !== undefined) { + page_params.stream_sounds_enabled = result.enable_stream_sounds; + } + + // PM and @-mention notification settings. + + if (result.enable_desktop_notifications !== undefined) { + page_params.desktop_notifications_enabled = result.enable_desktop_notifications; + } + if (result.enable_sounds !== undefined) { + page_params.sounds_enabled = result.enable_sounds; + } + + if (result.enable_offline_email_notifications !== undefined) { + page_params.enable_offline_email_notifications = + result.enable_offline_email_notifications; + } + + if (result.enable_offline_push_notifications !== undefined) { + page_params.enable_offline_push_notifications = + result.enable_offline_push_notifications; + } + + if (result.enable_online_push_notifications !== undefined) { + page_params.enable_online_push_notifications = result.enable_online_push_notifications; + } + + if (result.pm_content_in_desktop_notifications !== undefined) { + page_params.pm_content_in_desktop_notifications + = result.pm_content_in_desktop_notifications; + } + // Other notification settings. + + if (result.enable_digest_emails !== undefined) { + page_params.enable_digest_emails = result.enable_digest_emails; + } + + ui_report.success(i18n.t("Updated notification settings!"), notify_settings_status); + } + + function update_notification_settings_error(xhr) { + ui_report.error(i18n.t("Error changing settings"), xhr, $('#notify-settings-status').expectOne()); + } + + function post_notify_settings_changes(notification_changes, success_func, + error_func) { + return channel.patch({ + url: "/json/settings/notifications", + data: notification_changes, + success: success_func, + error: error_func, + }); + } + + $("#change_notification_settings").on("click", function (e) { + e.preventDefault(); + + var updated_settings = {}; + _.each(["enable_stream_desktop_notifications", "enable_stream_sounds", + "enable_desktop_notifications", "pm_content_in_desktop_notifications", "enable_sounds", + "enable_offline_email_notifications", + "enable_offline_push_notifications", "enable_online_push_notifications", + "enable_digest_emails"], + function (setting) { + updated_settings[setting] = $("#" + setting).is(":checked"); + }); + post_notify_settings_changes(updated_settings, + update_notification_settings_success, + update_notification_settings_error); + }); + + function update_global_stream_setting(notification_type, new_setting) { + var data = {}; + data[notification_type] = new_setting; + channel.patch({ + url: "/json/settings/notifications", + 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"); + // TODO: This seems broken!!! + 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 () { + propagate_setting_function(checkbox_status); + control_group.find(".propagate_stream_notifications_change").empty(); + }); + control_group.find(".no_propagate_notifications").on("click", function () { + control_group.find(".propagate_stream_notifications_change").empty(); + }); + } + + $("#enable_stream_desktop_notifications").on("click", function () { + 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 () { + var notification_checkbox = $("#enable_stream_sounds"); + maybe_bulk_update_stream_notification_setting(notification_checkbox, + update_audible_notification_setting); + }); + +}; + +function _update_page() { + $("#enable_stream_desktop_notifications").prop('checked', page_params.stream_desktop_notifications_enabled); + $("#enable_stream_sounds").prop('checked', page_params.stream_sounds_enabled); + $("#enable_desktop_notifications").prop('checked', page_params.desktop_notifications_enabled); + $("#enable_sounds").prop('checked', page_params.sounds_enabled); + $("#enable_offline_email_notifications").prop('checked', page_params.enable_offline_email_notifications); + $("#enable_offline_push_notifications").prop('checked', page_params.enable_offline_push_notifications); + $("#enable_online_push_notifications").prop('checked', page_params.enable_online_push_notifications); + $("#pm_content_in_desktop_notifications").prop('checked', page_params.pm_content_in_desktop_notifications); + $("#enable_digest_emails").prop('checked', page_params.enable_digest_emails); +} + +exports.update_page = function () { + i18n.ensure_i18n(_update_page); +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = settings_notifications; +} diff --git a/tools/js-dep-visualizer.py b/tools/js-dep-visualizer.py index dab8e325e7..27e50361cb 100644 --- a/tools/js-dep-visualizer.py +++ b/tools/js-dep-visualizer.py @@ -159,6 +159,7 @@ def find_edges_to_remove(graph, methods): ('message_list', 'message_edit'), ('message_edit', 'compose'), ('message_store', 'compose'), + ('settings_notifications', 'subs'), ('settings', 'muting_ui'), ('message_fetch', 'tutorial'), ('settings', 'subs'), diff --git a/zproject/settings.py b/zproject/settings.py index eb812bb423..65915b789f 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -907,6 +907,7 @@ JS_SPECS = { 'js/realm_icon.js', 'js/settings_account.js', 'js/settings_display.js', + 'js/settings_notifications.js', 'js/settings.js', 'js/admin.js', 'js/tab_bar.js',