Extract settings_notifications.js.

This commit is contained in:
Steve Howell 2017-04-06 07:01:07 -07:00 committed by Tim Abbott
parent fa143d4582
commit 1f38884b27
6 changed files with 166 additions and 147 deletions

View File

@ -34,6 +34,7 @@
"navigate": false, "navigate": false,
"settings_account": false, "settings_account": false,
"settings_display": false, "settings_display": false,
"settings_notifications": false,
"settings": false, "settings": false,
"resize": false, "resize": false,
"loading": false, "loading": false,

View File

@ -302,7 +302,7 @@ function dispatch_normal_event(event) {
notifications.handle_global_notification_updates(event.notification_name, notifications.handle_global_notification_updates(event.notification_name,
event.setting); event.setting);
if ($("#settings.tab-pane.active").length) { if ($("#settings.tab-pane.active").length) {
settings.update_page(); settings_notifications.update_page();
} }
break; break;

View File

@ -238,13 +238,13 @@ function _setup_page() {
}); });
$(".settings-box").html(settings_tab); $(".settings-box").html(settings_tab);
$("#notify-settings-status").hide();
$("#ui-settings-status").hide(); $("#ui-settings-status").hide();
alert_words_ui.set_up_alert_words(); alert_words_ui.set_up_alert_words();
attachments_ui.set_up_attachments(); attachments_ui.set_up_attachments();
settings_account.set_up(); settings_account.set_up();
settings_display.set_up(); settings_display.set_up();
settings_notifications.set_up();
$("#api_key_value").text(""); $("#api_key_value").text("");
$("#get_api_key_box").hide(); $("#get_api_key_box").hide();
@ -272,135 +272,6 @@ function _setup_page() {
$('.new-bot-ui').hide(); $('.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(); $("#get_api_key_box").hide();
$("#show_api_key_box").hide(); $("#show_api_key_box").hide();
$("#get_api_key_box form").ajaxForm({ $("#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 () { exports.setup_page = function () {
i18n.ensure_i18n(_setup_page); i18n.ensure_i18n(_setup_page);
}; };
exports.update_page = function () {
i18n.ensure_i18n(_update_page);
};
exports.launch_page = function (tab) { exports.launch_page = function (tab) {
var $active_tab = $("#settings_overlay_container li[data-section='" + tab + "']"); var $active_tab = $("#settings_overlay_container li[data-section='" + tab + "']");

View File

@ -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;
}

View File

@ -159,6 +159,7 @@ def find_edges_to_remove(graph, methods):
('message_list', 'message_edit'), ('message_list', 'message_edit'),
('message_edit', 'compose'), ('message_edit', 'compose'),
('message_store', 'compose'), ('message_store', 'compose'),
('settings_notifications', 'subs'),
('settings', 'muting_ui'), ('settings', 'muting_ui'),
('message_fetch', 'tutorial'), ('message_fetch', 'tutorial'),
('settings', 'subs'), ('settings', 'subs'),

View File

@ -907,6 +907,7 @@ JS_SPECS = {
'js/realm_icon.js', 'js/realm_icon.js',
'js/settings_account.js', 'js/settings_account.js',
'js/settings_display.js', 'js/settings_display.js',
'js/settings_notifications.js',
'js/settings.js', 'js/settings.js',
'js/admin.js', 'js/admin.js',
'js/tab_bar.js', 'js/tab_bar.js',