mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	Instead of having logical expressions in templates, it's always preferred to calculating them in javascript and pass the results as a context. It also enhances the readability of templates and testing of such logic is easier in js over templates.
		
			
				
	
	
		
			157 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			157 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
const stream_notification_settings = [
 | 
						|
    "enable_stream_desktop_notifications",
 | 
						|
    "enable_stream_audible_notifications",
 | 
						|
    "enable_stream_push_notifications",
 | 
						|
    "enable_stream_email_notifications",
 | 
						|
    "wildcard_mentions_notify",
 | 
						|
];
 | 
						|
 | 
						|
const pm_mention_notification_settings = [
 | 
						|
    "enable_desktop_notifications",
 | 
						|
    "enable_sounds",
 | 
						|
    "enable_offline_push_notifications",
 | 
						|
    "enable_offline_email_notifications",
 | 
						|
];
 | 
						|
 | 
						|
const desktop_notification_settings = [
 | 
						|
    "pm_content_in_desktop_notifications",
 | 
						|
];
 | 
						|
 | 
						|
const mobile_notification_settings = [
 | 
						|
    "enable_online_push_notifications",
 | 
						|
];
 | 
						|
 | 
						|
const email_notification_settings = [
 | 
						|
    "enable_digest_emails",
 | 
						|
    "enable_login_emails",
 | 
						|
    "message_content_in_email_notifications",
 | 
						|
    "realm_name_in_notifications",
 | 
						|
];
 | 
						|
 | 
						|
const other_notification_settings = desktop_notification_settings.concat(
 | 
						|
    ["desktop_icon_count_display"],
 | 
						|
    mobile_notification_settings,
 | 
						|
    email_notification_settings,
 | 
						|
    ["notification_sound"]
 | 
						|
);
 | 
						|
 | 
						|
const notification_settings_status = [
 | 
						|
    {status_label: "pm-mention-notify-settings-status", settings: pm_mention_notification_settings},
 | 
						|
    {status_label: "other-notify-settings-status", settings: other_notification_settings},
 | 
						|
    {status_label: "stream-notify-settings-status", settings: stream_notification_settings},
 | 
						|
];
 | 
						|
 | 
						|
exports.all_notification_settings = other_notification_settings.concat(
 | 
						|
    pm_mention_notification_settings,
 | 
						|
    stream_notification_settings
 | 
						|
);
 | 
						|
 | 
						|
exports.all_notifications = {
 | 
						|
    settings: {
 | 
						|
        stream_notification_settings: stream_notification_settings,
 | 
						|
        pm_mention_notification_settings: pm_mention_notification_settings,
 | 
						|
        desktop_notification_settings: desktop_notification_settings,
 | 
						|
        mobile_notification_settings: mobile_notification_settings,
 | 
						|
        email_notification_settings: email_notification_settings,
 | 
						|
    },
 | 
						|
    show_push_notifications_tooltip: {
 | 
						|
        enable_stream_push_notifications: !page_params.realm_push_notifications_enabled,
 | 
						|
        enable_offline_push_notifications: !page_params.realm_push_notifications_enabled,
 | 
						|
        enable_online_push_notifications: !page_params.realm_push_notifications_enabled,
 | 
						|
    },
 | 
						|
};
 | 
						|
 | 
						|
exports.desktop_icon_count_display_values = {
 | 
						|
    messages: {
 | 
						|
        code: 1,
 | 
						|
        description: i18n.t("All unreads"),
 | 
						|
    },
 | 
						|
    notifiable: {
 | 
						|
        code: 2,
 | 
						|
        description: i18n.t("Private messages and mentions"),
 | 
						|
    },
 | 
						|
    none: {
 | 
						|
        code: 3,
 | 
						|
        description: i18n.t("None"),
 | 
						|
    },
 | 
						|
};
 | 
						|
 | 
						|
function change_notification_setting(setting, setting_data, status_element) {
 | 
						|
    const data = {};
 | 
						|
    data[setting] = JSON.stringify(setting_data);
 | 
						|
    settings_ui.do_settings_change(channel.patch, '/json/settings/notifications', data, status_element);
 | 
						|
}
 | 
						|
 | 
						|
function update_desktop_icon_count_display() {
 | 
						|
    $("#desktop_icon_count_display").val(page_params.desktop_icon_count_display);
 | 
						|
    const count = unread.get_notifiable_count();
 | 
						|
    notifications.update_title_count(count);
 | 
						|
}
 | 
						|
 | 
						|
exports.set_enable_digest_emails_visibility = function () {
 | 
						|
    if (page_params.realm_digest_emails_enabled) {
 | 
						|
        $('#enable_digest_emails_label').parent().show();
 | 
						|
    } else {
 | 
						|
        $('#enable_digest_emails_label').parent().hide();
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
exports.set_up = function () {
 | 
						|
    for (const setting of notification_settings_status) {
 | 
						|
        for (const sub_setting of setting.settings) {
 | 
						|
            $("#" + sub_setting).change(function () {
 | 
						|
                let value;
 | 
						|
 | 
						|
                // `notification_sound` and `desktop_icon_count_display` are not booleans.
 | 
						|
                if (sub_setting === "notification_sound") {
 | 
						|
                    value = $(this).val();
 | 
						|
                } else if (sub_setting === "desktop_icon_count_display") {
 | 
						|
                    value = parseInt($(this).val(), 10);
 | 
						|
                } else {
 | 
						|
                    value = $(this).prop('checked');
 | 
						|
                }
 | 
						|
                change_notification_setting(sub_setting, value,
 | 
						|
                                            "#" + setting.status_label);
 | 
						|
            });
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    update_desktop_icon_count_display();
 | 
						|
 | 
						|
    $("#play_notification_sound").click(function () {
 | 
						|
        $("#notifications-area").find("audio")[0].play();
 | 
						|
    });
 | 
						|
 | 
						|
    const notification_sound_dropdown = $("#notification_sound");
 | 
						|
    notification_sound_dropdown.val(page_params.notification_sound);
 | 
						|
 | 
						|
    $("#enable_sounds, #enable_stream_audible_notifications").change(function () {
 | 
						|
        if ($("#enable_stream_audible_notifications").prop("checked") || $("#enable_sounds").prop("checked")) {
 | 
						|
            notification_sound_dropdown.prop("disabled", false);
 | 
						|
            notification_sound_dropdown.parent().removeClass("control-label-disabled");
 | 
						|
        } else {
 | 
						|
            notification_sound_dropdown.prop("disabled", true);
 | 
						|
            notification_sound_dropdown.parent().addClass("control-label-disabled");
 | 
						|
        }
 | 
						|
    });
 | 
						|
    exports.set_enable_digest_emails_visibility();
 | 
						|
};
 | 
						|
 | 
						|
exports.update_page = function () {
 | 
						|
    for (const setting of exports.all_notification_settings) {
 | 
						|
        if (setting === 'enable_offline_push_notifications'
 | 
						|
            && !page_params.realm_push_notifications_enabled) {
 | 
						|
            // If push notifications are disabled at the realm level,
 | 
						|
            // we should just leave the checkbox always off.
 | 
						|
            continue;
 | 
						|
        } else if (setting === 'desktop_icon_count_display') {
 | 
						|
            update_desktop_icon_count_display();
 | 
						|
            continue;
 | 
						|
        }
 | 
						|
 | 
						|
        $("#" + setting).prop('checked', page_params[setting]);
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
window.settings_notifications = exports;
 |