settings_notifications.js: Refactor using notification_settings variable.

Create an array of all user notification settings and loop through it
to update notification settings and display results.

Formatting tweaked by tabbott.
This commit is contained in:
Sarah
2017-06-04 10:43:10 -07:00
committed by Tim Abbott
parent 0be5178759
commit 381ec1f5d4

View File

@@ -2,6 +2,18 @@ var settings_notifications = (function () {
var exports = {};
var notification_settings = [
"enable_desktop_notifications",
"enable_digest_emails",
"enable_offline_email_notifications",
"enable_offline_push_notifications",
"enable_online_push_notifications",
"enable_sounds",
"enable_stream_desktop_notifications",
"enable_stream_sounds",
"pm_content_in_desktop_notifications",
];
exports.set_up = function () {
$("#notify-settings-status").hide();
@@ -15,47 +27,11 @@ exports.set_up = function () {
// Stream notification settings.
if (result.enable_stream_desktop_notifications !== undefined) {
page_params.enable_stream_desktop_notifications =
result.enable_stream_desktop_notifications;
}
if (result.enable_stream_sounds !== undefined) {
page_params.enable_stream_sounds = result.enable_stream_sounds;
}
// PM and @-mention notification settings.
if (result.enable_desktop_notifications !== undefined) {
page_params.enable_desktop_notifications = result.enable_desktop_notifications;
}
if (result.enable_sounds !== undefined) {
page_params.enable_sounds = 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;
}
_.each(result, function (v, k) {
if (_.has(notification_settings, k) && result[k] !== undefined) {
page_params[k] = result[k];
}
});
ui_report.success(i18n.t("Updated notification settings!"), notify_settings_status);
}
@@ -77,14 +53,9 @@ exports.set_up = function () {
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");
});
_.each(notification_settings, function (setting) {
updated_settings[setting] = $("#" + setting).is(":checked");
});
post_notify_settings_changes(updated_settings,
update_notification_settings_success,
update_notification_settings_error);
@@ -142,15 +113,9 @@ exports.set_up = function () {
};
function _update_page() {
$("#enable_stream_desktop_notifications").prop('checked', page_params.enable_stream_desktop_notifications);
$("#enable_stream_sounds").prop('checked', page_params.enable_stream_sounds);
$("#enable_desktop_notifications").prop('checked', page_params.enable_desktop_notifications);
$("#enable_sounds").prop('checked', page_params.enable_sounds);
$("#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);
_.each(notification_settings, function (setting) {
$("#" + setting).prop('checked', page_params[setting]);
});
}
exports.update_page = function () {