From 5ca763fc2451b91ffd77f38b8142b8c8e2ea6224 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 9 May 2021 14:36:58 -0700 Subject: [PATCH] eslint: Fix unicorn/prefer-switch. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/upgrade.js | 32 +- static/js/emoji_picker.js | 61 ++-- static/js/filter.js | 53 +-- static/js/hotkey.js | 36 +- static/js/input_pill.js | 29 +- static/js/lightbox.js | 28 +- static/js/lightbox_canvas.js | 22 +- static/js/message_edit.js | 68 ++-- static/js/narrow.js | 26 +- static/js/rtl.js | 35 +- static/js/settings_org.js | 517 +++++++++++++++------------ 12 files changed, 512 insertions(+), 396 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 94ac048e2d..de92a723d4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -102,7 +102,6 @@ "unicorn/prefer-module": "off", "unicorn/prefer-node-protocol": "off", "unicorn/prefer-spread": "off", - "unicorn/prefer-switch": "off", "unicorn/prefer-ternary": "off", "unicorn/prevent-abbreviations": "off", "valid-typeof": ["error", {"requireStringLiterals": true}], diff --git a/frontend_tests/node_tests/upgrade.js b/frontend_tests/node_tests/upgrade.js index 9f4284776e..33118677a3 100644 --- a/frontend_tests/node_tests/upgrade.js +++ b/frontend_tests/node_tests/upgrade.js @@ -38,20 +38,24 @@ run_test("initialize", (override) => { let create_ajax_request_form_call_count = 0; helpers.__Rewire__("create_ajax_request", (url, form_name, stripe_token, redirect_to) => { create_ajax_request_form_call_count += 1; - if (form_name === "autopay") { - assert.equal(url, "/json/billing/upgrade"); - assert.equal(stripe_token, "stripe_add_card_token"); - assert.equal(redirect_to, undefined); - } else if (form_name === "invoice") { - assert.equal(url, "/json/billing/upgrade"); - assert.equal(stripe_token, undefined); - assert.equal(redirect_to, undefined); - } else if (form_name === "sponsorship") { - assert.equal(url, "/json/billing/sponsorship"); - assert.equal(stripe_token, undefined); - assert.equal(redirect_to, "/"); - } else { - throw new Error("Unhandled case"); + switch (form_name) { + case "autopay": + assert.equal(url, "/json/billing/upgrade"); + assert.equal(stripe_token, "stripe_add_card_token"); + assert.equal(redirect_to, undefined); + break; + case "invoice": + assert.equal(url, "/json/billing/upgrade"); + assert.equal(stripe_token, undefined); + assert.equal(redirect_to, undefined); + break; + case "sponsorship": + assert.equal(url, "/json/billing/sponsorship"); + assert.equal(stripe_token, undefined); + assert.equal(redirect_to, "/"); + break; + default: + throw new Error("Unhandled case"); } }); diff --git a/static/js/emoji_picker.js b/static/js/emoji_picker.js index cfd40bf151..112b2d6411 100644 --- a/static/js/emoji_picker.js +++ b/static/js/emoji_picker.js @@ -441,7 +441,6 @@ export function navigate(event_name, e) { const selected_emoji = get_rendered_emoji(current_section, current_index); const is_filter_focused = $(".emoji-popover-filter").is(":focus"); - let next_section = 0; // special cases if (is_filter_focused) { // Move down into emoji map. @@ -481,42 +480,32 @@ export function navigate(event_name, e) { reset_emoji_showcase(); return true; } - } else if (event_name === "tab") { - change_focus_to_filter(); - return true; - } else if (event_name === "shift_tab") { - if (!is_filter_focused) { - change_focus_to_filter(); - } - return true; - } else if (event_name === "page_up") { - next_section = current_section - 1; - maybe_change_active_section(next_section); - return true; - } else if (event_name === "page_down") { - next_section = current_section + 1; - maybe_change_active_section(next_section); - return true; - } else if (!is_filter_focused) { - let next_coord = {}; - switch (event_name) { - case "down_arrow": - next_coord = get_next_emoji_coordinates(6); - break; - case "up_arrow": - next_coord = get_next_emoji_coordinates(-6); - break; - case "left_arrow": - next_coord = get_next_emoji_coordinates(-1); - break; - case "right_arrow": - next_coord = get_next_emoji_coordinates(1); - break; - } - - return maybe_change_focused_emoji($emoji_map, next_coord.section, next_coord.index); + return false; + } + + switch (event_name) { + case "tab": + case "shift_tab": + change_focus_to_filter(); + return true; + case "page_up": + maybe_change_active_section(current_section - 1); + return true; + case "page_down": + maybe_change_active_section(current_section + 1); + return true; + case "down_arrow": + case "up_arrow": + case "left_arrow": + case "right_arrow": { + const next_coord = get_next_emoji_coordinates( + {down_arrow: 6, up_arrow: -6, left_arrow: -1, right_arrow: 1}[event_name], + ); + return maybe_change_focused_emoji($emoji_map, next_coord.section, next_coord.index); + } + default: + return false; } - return false; } function process_keypress(e) { diff --git a/static/js/filter.js b/static/js/filter.js index 8077ce7498..4fbdf8b265 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -69,35 +69,42 @@ function message_in_home(message) { function message_matches_search_term(message, operator, operand) { switch (operator) { case "has": - if (operand === "image") { - return message_parser.message_has_image(message); - } else if (operand === "link") { - return message_parser.message_has_link(message); - } else if (operand === "attachment") { - return message_parser.message_has_attachment(message); + switch (operand) { + case "image": + return message_parser.message_has_image(message); + case "link": + return message_parser.message_has_link(message); + case "attachment": + return message_parser.message_has_attachment(message); + default: + return false; // has:something_else returns false } - return false; // has:something_else returns false + case "is": - if (operand === "private") { - return message.type === "private"; - } else if (operand === "starred") { - return message.starred; - } else if (operand === "mentioned") { - return message.mentioned; - } else if (operand === "alerted") { - return message.alerted; - } else if (operand === "unread") { - return unread.message_unread(message); + switch (operand) { + case "private": + return message.type === "private"; + case "starred": + return message.starred; + case "mentioned": + return message.mentioned; + case "alerted": + return message.alerted; + case "unread": + return unread.message_unread(message); + default: + return false; // is:whatever returns false } - return false; // is:whatever returns false case "in": - if (operand === "home") { - return message_in_home(message); - } else if (operand === "all") { - return true; + switch (operand) { + case "home": + return message_in_home(message); + case "all": + return true; + default: + return false; // in:whatever returns false } - return false; // in:whatever returns false case "near": // this is all handled server side diff --git a/static/js/hotkey.js b/static/js/hotkey.js index 2e33e1acb7..af8079ddde 100644 --- a/static/js/hotkey.js +++ b/static/js/hotkey.js @@ -680,21 +680,29 @@ export function process_hotkey(e, hotkey) { ) { compose_actions.cancel(); // don't return, as we still want it to be picked up by the code below - } else if (event_name === "page_up") { - $(":focus").caret(0).animate({scrollTop: 0}, "fast"); - return true; - } else if (event_name === "page_down") { - // so that it always goes to the end of the text box. - const height = $(":focus")[0].scrollHeight; - $(":focus").caret(Number.POSITIVE_INFINITY).animate({scrollTop: height}, "fast"); - return true; - } else if (event_name === "search_with_k") { - // Do nothing; this allows one to use Ctrl+K inside compose. - } else if (event_name === "star_message") { - // Do nothing; this allows one to use Ctrl+S inside compose. } else { - // Let the browser handle the key normally. - return false; + switch (event_name) { + case "page_up": + $(":focus").caret(0).animate({scrollTop: 0}, "fast"); + return true; + case "page_down": { + // so that it always goes to the end of the text box. + const height = $(":focus")[0].scrollHeight; + $(":focus") + .caret(Number.POSITIVE_INFINITY) + .animate({scrollTop: height}, "fast"); + return true; + } + case "search_with_k": + // Do nothing; this allows one to use Ctrl+K inside compose. + break; + case "star_message": + // Do nothing; this allows one to use Ctrl+S inside compose. + break; + default: + // Let the browser handle the key normally. + return false; + } } } diff --git a/static/js/input_pill.js b/static/js/input_pill.js index 4d04e6ce0f..2a6b6f374c 100644 --- a/static/js/input_pill.js +++ b/static/js/input_pill.js @@ -316,18 +316,23 @@ export function create(opts) { const $pill = store.$parent.find(".pill:focus"); - if (char === KEY.LEFT_ARROW) { - $pill.prev().trigger("focus"); - } else if (char === KEY.RIGHT_ARROW) { - $pill.next().trigger("focus"); - } else if (char === KEY.BACKSPACE) { - const $next = $pill.next(); - const id = $pill.data("id"); - funcs.removePill(id); - $next.trigger("focus"); - // the "Backspace" key in Firefox will go back a page if you do - // not prevent it. - e.preventDefault(); + switch (char) { + case KEY.LEFT_ARROW: + $pill.prev().trigger("focus"); + break; + case KEY.RIGHT_ARROW: + $pill.next().trigger("focus"); + break; + case KEY.BACKSPACE: { + const $next = $pill.next(); + const id = $pill.data("id"); + funcs.removePill(id); + $next.trigger("focus"); + // the "Backspace" key in Firefox will go back a page if you do + // not prevent it. + e.preventDefault(); + break; + } } }); diff --git a/static/js/lightbox.js b/static/js/lightbox.js index 2036e3cced..4e357ff481 100644 --- a/static/js/lightbox.js +++ b/static/js/lightbox.js @@ -79,18 +79,22 @@ function display_video(payload) { ).hide(); let source; - if (payload.type === "youtube-video") { - source = "https://www.youtube.com/embed/" + payload.source; - } else if (payload.type === "vimeo-video") { - source = "https://player.vimeo.com/video/" + payload.source; - } else if (payload.type === "embed-video") { - // Use data: to load the player in a unique origin for security. - source = - "data:text/html," + - window.encodeURIComponent( - "" + - payload.source, - ); + switch (payload.type) { + case "youtube-video": + source = "https://www.youtube.com/embed/" + payload.source; + break; + case "vimeo-video": + source = "https://player.vimeo.com/video/" + payload.source; + break; + case "embed-video": + // Use data: to load the player in a unique origin for security. + source = + "data:text/html," + + window.encodeURIComponent( + "" + + payload.source, + ); + break; } const iframe = $(""); diff --git a/static/js/lightbox_canvas.js b/static/js/lightbox_canvas.js index 49ac7f707d..9bb157a2fc 100644 --- a/static/js/lightbox_canvas.js +++ b/static/js/lightbox_canvas.js @@ -101,14 +101,20 @@ const funcs = { if (!overlays.lightbox_open()) { return; } - if (e.key === "Z" || e.key === "+") { - funcs.setZoom(meta, "+"); - funcs.displayImage(canvas, context, meta); - } else if (e.key === "z" || e.key === "-") { - funcs.setZoom(meta, "-"); - funcs.displayImage(canvas, context, meta); - } else if (e.key === "v") { - overlays.close_overlay("lightbox"); + switch (e.key) { + case "Z": + case "+": + funcs.setZoom(meta, "+"); + funcs.displayImage(canvas, context, meta); + break; + case "z": + case "-": + funcs.setZoom(meta, "-"); + funcs.displayImage(canvas, context, meta); + break; + case "v": + overlays.close_overlay("lightbox"); + break; } e.preventDefault(); e.stopPropagation(); diff --git a/static/js/message_edit.js b/static/js/message_edit.js index c7c2b6f3fe..df5acb0816 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -401,38 +401,44 @@ function edit_message(row, raw_content) { stream_bar.decorate(stream_name, stream_header_colorblock, false); }); - if (editability === editability_types.NO) { - message_edit_content.attr("readonly", "readonly"); - message_edit_topic.attr("readonly", "readonly"); - create_copy_to_clipboard_handler(copy_message[0], message.id); - } else if (editability === editability_types.NO_LONGER) { - // You can currently only reach this state in non-streams. If that - // changes (e.g. if we stop allowing topics to be modified forever - // in streams), then we'll need to disable - // row.find('input.message_edit_topic') as well. - message_edit_content.attr("readonly", "readonly"); - message_edit_countdown_timer.text($t({defaultMessage: "View source"})); - create_copy_to_clipboard_handler(copy_message[0], message.id); - } else if (editability === editability_types.TOPIC_ONLY) { - message_edit_content.attr("readonly", "readonly"); - // Hint why you can edit the topic but not the message content - message_edit_countdown_timer.text($t({defaultMessage: "Topic editing only"})); - create_copy_to_clipboard_handler(copy_message[0], message.id); - } else if (editability === editability_types.FULL) { - copy_message.remove(); - const edit_id = `#edit_form_${CSS.escape(rows.id(row))} .message_edit_content`; - const listeners = resize.watch_manual_resize(edit_id); - if (listeners) { - currently_editing_messages.get(rows.id(row)).listeners = listeners; + switch (editability) { + case editability_types.NO: + message_edit_content.attr("readonly", "readonly"); + message_edit_topic.attr("readonly", "readonly"); + create_copy_to_clipboard_handler(copy_message[0], message.id); + break; + case editability_types.NO_LONGER: + // You can currently only reach this state in non-streams. If that + // changes (e.g. if we stop allowing topics to be modified forever + // in streams), then we'll need to disable + // row.find('input.message_edit_topic') as well. + message_edit_content.attr("readonly", "readonly"); + message_edit_countdown_timer.text($t({defaultMessage: "View source"})); + create_copy_to_clipboard_handler(copy_message[0], message.id); + break; + case editability_types.TOPIC_ONLY: + message_edit_content.attr("readonly", "readonly"); + // Hint why you can edit the topic but not the message content + message_edit_countdown_timer.text($t({defaultMessage: "Topic editing only"})); + create_copy_to_clipboard_handler(copy_message[0], message.id); + break; + case editability_types.FULL: { + copy_message.remove(); + const edit_id = `#edit_form_${CSS.escape(rows.id(row))} .message_edit_content`; + const listeners = resize.watch_manual_resize(edit_id); + if (listeners) { + currently_editing_messages.get(rows.id(row)).listeners = listeners; + } + composebox_typeahead.initialize_compose_typeahead(edit_id); + compose.handle_keyup(null, $(edit_id).expectOne()); + $(edit_id).on("keydown", function (event) { + compose.handle_keydown(event, $(this).expectOne()); + }); + $(edit_id).on("keyup", function (event) { + compose.handle_keyup(event, $(this).expectOne()); + }); + break; } - composebox_typeahead.initialize_compose_typeahead(edit_id); - compose.handle_keyup(null, $(edit_id).expectOne()); - $(edit_id).on("keydown", function (event) { - compose.handle_keydown(event, $(this).expectOne()); - }); - $(edit_id).on("keyup", function (event) { - compose.handle_keyup(event, $(this).expectOne()); - }); } // Add tooltip diff --git a/static/js/narrow.js b/static/js/narrow.js index 45ba5b364d..9e4411114d 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -328,17 +328,21 @@ export function activate(raw_operators, opts) { // particular message ID (which could be max_int), or we're // asking the server to figure out for us what the first // unread message is, and center the narrow around that. - if (id_info.final_select_id === undefined) { - anchor = "first_unread"; - } else if (id_info.final_select_id === -1) { - // This case should never happen in this code path; it's - // here in case we choose to extract this as an - // independent reusable function. - anchor = "oldest"; - } else if (id_info.final_select_id === LARGER_THAN_MAX_MESSAGE_ID) { - anchor = "newest"; - } else { - anchor = id_info.final_select_id; + switch (id_info.final_select_id) { + case undefined: + anchor = "first_unread"; + break; + case -1: + // This case should never happen in this code path; it's + // here in case we choose to extract this as an + // independent reusable function. + anchor = "oldest"; + break; + case LARGER_THAN_MAX_MESSAGE_ID: + anchor = "newest"; + break; + default: + anchor = id_info.final_select_id; } message_fetch.load_messages_for_narrow({ diff --git a/static/js/rtl.js b/static/js/rtl.js index 35759398ff..18728cc80d 100644 --- a/static/js/rtl.js +++ b/static/js/rtl.js @@ -130,20 +130,27 @@ export function get_direction(str) { } const bidi_class = get_bidi_class(ch); - if (bidi_class === "I") { - // LRI, RLI, FSI - isolations += 1; - } else if (bidi_class === "PDI") { - if (isolations > 0) { - isolations -= 1; - } - } else if (bidi_class === "R") { - // R, AL - if (isolations === 0) { - return "rtl"; - } - } else if (bidi_class === "L" && isolations === 0) { - return "ltr"; + switch (bidi_class) { + case "I": + // LRI, RLI, FSI + isolations += 1; + break; + case "PDI": + if (isolations > 0) { + isolations -= 1; + } + break; + case "R": + // R, AL + if (isolations === 0) { + return "rtl"; + } + break; + case "L": + if (isolations === 0) { + return "ltr"; + } + break; } } return "ltr"; diff --git a/static/js/settings_org.js b/static/js/settings_org.js index d0dc1b795a..b2724fdf9d 100644 --- a/static/js/settings_org.js +++ b/static/js/settings_org.js @@ -399,31 +399,37 @@ export function populate_auth_methods(auth_methods) { function update_dependent_subsettings(property_name) { if (simple_dropdown_properties.includes(property_name)) { set_property_dropdown_value(property_name); - } else if (property_name === "realm_waiting_period_threshold") { - set_realm_waiting_period_dropdown(); - } else if ( - property_name === "realm_video_chat_provider" - ) { - set_video_chat_provider_dropdown(); - } else if ( - property_name === "realm_msg_edit_limit_setting" || - property_name === "realm_message_content_edit_limit_minutes" - ) { - set_msg_edit_limit_dropdown(); - } else if (property_name === "realm_message_retention_days") { - set_message_retention_setting_dropdown(); - } else if ( - property_name === "realm_msg_delete_limit_setting" || - property_name === "realm_message_content_delete_limit_minutes" - ) { - set_msg_delete_limit_dropdown(); - } else if (property_name === "realm_org_join_restrictions") { - set_org_join_restrictions_dropdown(); - } else if (property_name === "realm_message_content_allowed_in_email_notifications") { - set_message_content_in_email_notifications_visiblity(); - } else if (property_name === "realm_digest_emails_enabled") { - settings_notifications.set_enable_digest_emails_visibility(); - set_digest_emails_weekday_visibility(); + return; + } + + switch (property_name) { + case "realm_waiting_period_threshold": + set_realm_waiting_period_dropdown(); + break; + case "realm_video_chat_provider": + set_video_chat_provider_dropdown(); + break; + case "realm_msg_edit_limit_setting": + case "realm_message_content_edit_limit_minutes": + set_msg_edit_limit_dropdown(); + break; + case "realm_message_retention_days": + set_message_retention_setting_dropdown(); + break; + case "realm_msg_delete_limit_setting": + case "realm_message_content_delete_limit_minutes": + set_msg_delete_limit_dropdown(); + break; + case "realm_org_join_restrictions": + set_org_join_restrictions_dropdown(); + break; + case "realm_message_content_allowed_in_email_notifications": + set_message_content_in_email_notifications_visiblity(); + break; + case "realm_digest_emails_enabled": + settings_notifications.set_enable_digest_emails_visibility(); + set_digest_emails_weekday_visibility(); + break; } } @@ -436,18 +442,25 @@ function discard_property_element_changes(elem) { const property_name = extract_property_name(elem); const property_value = get_property_value(property_name); - if (property_name === "realm_authentication_methods") { - populate_auth_methods(property_value); - } else if (property_name === "realm_notifications_stream_id") { - notifications_stream_widget.render(property_value); - } else if (property_name === "realm_signup_notifications_stream_id") { - signup_notifications_stream_widget.render(property_value); - } else if (property_name === "realm_default_code_block_language") { - default_code_language_widget.render(property_value); - } else if (property_value !== undefined) { - set_input_element_value(elem, property_value); - } else { - blueslip.error("Element refers to unknown property " + property_name); + switch (property_name) { + case "realm_authentication_methods": + populate_auth_methods(property_value); + break; + case "realm_notifications_stream_id": + notifications_stream_widget.render(property_value); + break; + case "realm_signup_notifications_stream_id": + signup_notifications_stream_widget.render(property_value); + break; + case "realm_default_code_block_language": + default_code_language_widget.render(property_value); + break; + default: + if (property_value !== undefined) { + set_input_element_value(elem, property_value); + } else { + blueslip.error("Element refers to unknown property " + property_name); + } } update_dependent_subsettings(property_name); @@ -459,29 +472,39 @@ export function sync_realm_settings(property) { } const value = page_params[`realm_${property}`]; - if (property === "notifications_stream_id") { - notifications_stream_widget.render(value); - } else if (property === "signup_notifications_stream_id") { - signup_notifications_stream_widget.render(value); - } else if (property === "default_code_block_language") { - default_code_language_widget.render(value); + switch (property) { + case "notifications_stream_id": + notifications_stream_widget.render(value); + break; + case "signup_notifications_stream_id": + signup_notifications_stream_widget.render(value); + break; + case "default_code_block_language": + default_code_language_widget.render(value); + break; } - if (property === "message_content_edit_limit_seconds") { - property = "message_content_edit_limit_minutes"; - } else if (property === "allow_message_editing") { - property = "msg_edit_limit_setting"; - } else if ( - property === "emails_restricted_to_domains" || - property === "disallow_disposable_email_addresses" - ) { - property = "org_join_restrictions"; - } else if (property === "message_content_delete_limit_seconds") { - property = "message_content_delete_limit_minutes"; - } else if (property === "allow_message_deleting") { - property = "msg_delete_limit_setting"; - } else if (property === "invite_required" || property === "invite_to_realm_policy") { - property = "user_invite_restriction"; + switch (property) { + case "message_content_edit_limit_seconds": + property = "message_content_edit_limit_minutes"; + break; + case "allow_message_editing": + property = "msg_edit_limit_setting"; + break; + case "emails_restricted_to_domains": + case "disallow_disposable_email_addresses": + property = "org_join_restrictions"; + break; + case "message_content_delete_limit_seconds": + property = "message_content_delete_limit_minutes"; + break; + case "allow_message_deleting": + property = "msg_delete_limit_setting"; + break; + case "invite_required": + case "invite_to_realm_policy": + property = "user_invite_restriction"; + break; } const element = $(`#id_realm_${CSS.escape(property)}`); if (element.length) { @@ -515,31 +538,37 @@ export function change_save_button_state($element, state) { let button_text; let data_status; let is_show; - if (state === "unsaved") { - button_text = $t({defaultMessage: "Save changes"}); - data_status = "unsaved"; - is_show = true; + switch (state) { + case "unsaved": + button_text = $t({defaultMessage: "Save changes"}); + data_status = "unsaved"; + is_show = true; - $element.find(".discard-button").show(); - } else if (state === "saved") { - button_text = $t({defaultMessage: "Save changes"}); - data_status = ""; - is_show = false; - } else if (state === "saving") { - button_text = $t({defaultMessage: "Saving"}); - data_status = "saving"; - is_show = true; + $element.find(".discard-button").show(); + break; + case "saved": + button_text = $t({defaultMessage: "Save changes"}); + data_status = ""; + is_show = false; + break; + case "saving": + button_text = $t({defaultMessage: "Saving"}); + data_status = "saving"; + is_show = true; - $element.find(".discard-button").hide(); - $saveBtn.addClass("saving"); - } else if (state === "failed") { - button_text = $t({defaultMessage: "Save changes"}); - data_status = "failed"; - is_show = true; - } else if (state === "succeeded") { - button_text = $t({defaultMessage: "Saved"}); - data_status = "saved"; - is_show = false; + $element.find(".discard-button").hide(); + $saveBtn.addClass("saving"); + break; + case "failed": + button_text = $t({defaultMessage: "Save changes"}); + data_status = "failed"; + is_show = true; + break; + case "succeeded": + button_text = $t({defaultMessage: "Saved"}); + data_status = "saved"; + is_show = false; + break; } $textEl.text(button_text); @@ -557,18 +586,16 @@ function get_input_type(input_elem, input_type) { export function get_input_element_value(input_elem, input_type) { input_elem = $(input_elem); input_type = get_input_type(input_elem, input_type); - if (input_type) { - if (input_type === "boolean") { + switch (input_type) { + case "boolean": return input_elem.prop("checked"); - } - if (input_type === "string") { + case "string": return input_elem.val().trim(); - } - if (input_type === "number") { + case "number": return Number.parseInt(input_elem.val().trim(), 10); - } + default: + return undefined; } - return undefined; } export function set_input_element_value(input_elem, value) { @@ -576,8 +603,7 @@ export function set_input_element_value(input_elem, value) { if (input_type) { if (input_type === "boolean") { return input_elem.prop("checked", value); - } - if (input_type === "string" || input_type === "number") { + } else if (input_type === "string" || input_type === "number") { return input_elem.val(value); } } @@ -609,21 +635,28 @@ function check_property_changed(elem) { let current_val = get_property_value(property_name); let changed_val; - if (property_name === "realm_authentication_methods") { - current_val = sort_object_by_key(current_val); - current_val = JSON.stringify(current_val); - changed_val = get_auth_method_table_data(); - changed_val = JSON.stringify(changed_val); - } else if (property_name === "realm_notifications_stream_id") { - changed_val = Number.parseInt(notifications_stream_widget.value(), 10); - } else if (property_name === "realm_signup_notifications_stream_id") { - changed_val = Number.parseInt(signup_notifications_stream_widget.value(), 10); - } else if (property_name === "realm_default_code_block_language") { - changed_val = default_code_language_widget.value(); - } else if (current_val !== undefined) { - changed_val = get_input_element_value(elem, typeof current_val); - } else { - blueslip.error("Element refers to unknown property " + property_name); + switch (property_name) { + case "realm_authentication_methods": + current_val = sort_object_by_key(current_val); + current_val = JSON.stringify(current_val); + changed_val = get_auth_method_table_data(); + changed_val = JSON.stringify(changed_val); + break; + case "realm_notifications_stream_id": + changed_val = Number.parseInt(notifications_stream_widget.value(), 10); + break; + case "realm_signup_notifications_stream_id": + changed_val = Number.parseInt(signup_notifications_stream_widget.value(), 10); + break; + case "realm_default_code_block_language": + changed_val = default_code_language_widget.value(); + break; + default: + if (current_val !== undefined) { + changed_val = get_input_element_value(elem, typeof current_val); + } else { + blueslip.error("Element refers to unknown property " + property_name); + } } return current_val !== changed_val; } @@ -770,128 +803,172 @@ export function build_page() { function get_complete_data_for_subsection(subsection) { let data = {}; - if (subsection === "msg_editing") { - const edit_limit_setting_value = $("#id_realm_msg_edit_limit_setting").val(); - if (edit_limit_setting_value === "never") { - data.allow_message_editing = false; - } else if (edit_limit_setting_value === "custom_limit") { - data.message_content_edit_limit_seconds = parse_time_limit( - $("#id_realm_message_content_edit_limit_minutes"), + switch (subsection) { + case "msg_editing": { + const edit_limit_setting_value = $("#id_realm_msg_edit_limit_setting").val(); + if (edit_limit_setting_value === "never") { + data.allow_message_editing = false; + } else if (edit_limit_setting_value === "custom_limit") { + data.message_content_edit_limit_seconds = parse_time_limit( + $("#id_realm_message_content_edit_limit_minutes"), + ); + // Disable editing if the parsed time limit is 0 seconds + data.allow_message_editing = Boolean(data.message_content_edit_limit_seconds); + } else { + data.allow_message_editing = true; + data.message_content_edit_limit_seconds = + settings_config.msg_edit_limit_dropdown_values.get( + edit_limit_setting_value, + ).seconds; + } + const delete_limit_setting_value = $("#id_realm_msg_delete_limit_setting").val(); + if (delete_limit_setting_value === "never") { + data.allow_message_deleting = false; + } else if (delete_limit_setting_value === "custom_limit") { + data.message_content_delete_limit_seconds = parse_time_limit( + $("#id_realm_message_content_delete_limit_minutes"), + ); + // Disable deleting if the parsed time limit is 0 seconds + data.allow_message_deleting = Boolean( + data.message_content_delete_limit_seconds, + ); + } else { + data.allow_message_deleting = true; + data.message_content_delete_limit_seconds = + settings_config.msg_delete_limit_dropdown_values.get( + delete_limit_setting_value, + ).seconds; + } + break; + } + case "notifications": + data.notifications_stream_id = Number.parseInt( + notifications_stream_widget.value(), + 10, ); - // Disable editing if the parsed time limit is 0 seconds - data.allow_message_editing = Boolean(data.message_content_edit_limit_seconds); - } else { - data.allow_message_editing = true; - data.message_content_edit_limit_seconds = - settings_config.msg_edit_limit_dropdown_values.get( - edit_limit_setting_value, - ).seconds; - } - const delete_limit_setting_value = $("#id_realm_msg_delete_limit_setting").val(); - if (delete_limit_setting_value === "never") { - data.allow_message_deleting = false; - } else if (delete_limit_setting_value === "custom_limit") { - data.message_content_delete_limit_seconds = parse_time_limit( - $("#id_realm_message_content_delete_limit_minutes"), + data.signup_notifications_stream_id = Number.parseInt( + signup_notifications_stream_widget.value(), + 10, ); - // Disable deleting if the parsed time limit is 0 seconds - data.allow_message_deleting = Boolean(data.message_content_delete_limit_seconds); - } else { - data.allow_message_deleting = true; - data.message_content_delete_limit_seconds = - settings_config.msg_delete_limit_dropdown_values.get( - delete_limit_setting_value, - ).seconds; + break; + case "message_retention": { + const message_retention_setting_value = $( + "#id_realm_message_retention_setting", + ).val(); + if (message_retention_setting_value === "retain_forever") { + data.message_retention_days = JSON.stringify("forever"); + } else { + data.message_retention_days = JSON.stringify( + get_input_element_value($("#id_realm_message_retention_days")), + ); + } + break; } - } else if (subsection === "notifications") { - data.notifications_stream_id = Number.parseInt(notifications_stream_widget.value(), 10); - data.signup_notifications_stream_id = Number.parseInt( - signup_notifications_stream_widget.value(), - 10, - ); - } else if (subsection === "message_retention") { - const message_retention_setting_value = $("#id_realm_message_retention_setting").val(); - if (message_retention_setting_value === "retain_forever") { - data.message_retention_days = JSON.stringify("forever"); - } else { - data.message_retention_days = JSON.stringify( - get_input_element_value($("#id_realm_message_retention_days")), - ); + case "other_settings": { + const code_block_language_value = default_code_language_widget.value(); + // No need to JSON-encode, since this value is already a string. + data.default_code_block_language = code_block_language_value; + break; } - } else if (subsection === "other_settings") { - const code_block_language_value = default_code_language_widget.value(); - // No need to JSON-encode, since this value is already a string. - data.default_code_block_language = code_block_language_value; - } else if (subsection === "other_permissions") { - const add_emoji_permission = $("#id_realm_add_emoji_by_admins_only").val(); + case "other_permissions": { + const add_emoji_permission = $("#id_realm_add_emoji_by_admins_only").val(); + switch (add_emoji_permission) { + case "by_admins_only": + data.add_emoji_by_admins_only = true; + break; + case "by_anyone": + data.add_emoji_by_admins_only = false; + break; + } + break; + } + case "org_join": { + const org_join_restrictions = $("#id_realm_org_join_restrictions").val(); + switch (org_join_restrictions) { + case "only_selected_domain": + data.emails_restricted_to_domains = true; + data.disallow_disposable_email_addresses = false; + break; + case "no_disposable_email": + data.emails_restricted_to_domains = false; + data.disallow_disposable_email_addresses = true; + break; + case "no_restriction": + data.disallow_disposable_email_addresses = false; + data.emails_restricted_to_domains = false; + break; + } - if (add_emoji_permission === "by_admins_only") { - data.add_emoji_by_admins_only = true; - } else if (add_emoji_permission === "by_anyone") { - data.add_emoji_by_admins_only = false; - } - } else if (subsection === "org_join") { - const org_join_restrictions = $("#id_realm_org_join_restrictions").val(); - if (org_join_restrictions === "only_selected_domain") { - data.emails_restricted_to_domains = true; - data.disallow_disposable_email_addresses = false; - } else if (org_join_restrictions === "no_disposable_email") { - data.emails_restricted_to_domains = false; - data.disallow_disposable_email_addresses = true; - } else if (org_join_restrictions === "no_restriction") { - data.disallow_disposable_email_addresses = false; - data.emails_restricted_to_domains = false; - } + const user_invite_restriction = $("#id_realm_user_invite_restriction").val(); + switch (user_invite_restriction) { + case "no_invite_required": + data.invite_required = false; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_members.code; + break; + case "no_invite_required_by_admins_only": + data.invite_required = false; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_admins_only.code; + break; + case "no_invite_required_by_moderators_only": + data.invite_required = false; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_moderators_only.code; + break; + case "no_invite_required_by_full_members": + data.invite_required = false; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_full_members.code; + break; + case "by_admins_only": + data.invite_required = true; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_admins_only.code; + break; + case "by_moderators_only": + data.invite_required = true; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_moderators_only.code; + break; + case "by_full_members": + data.invite_required = true; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_full_members.code; + break; + default: + data.invite_required = true; + data.invite_to_realm_policy = + settings_config.common_policy_values.by_members.code; + } - const user_invite_restriction = $("#id_realm_user_invite_restriction").val(); - if (user_invite_restriction === "no_invite_required") { - data.invite_required = false; - data.invite_to_realm_policy = settings_config.common_policy_values.by_members.code; - } else if (user_invite_restriction === "no_invite_required_by_admins_only") { - data.invite_required = false; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_admins_only.code; - } else if (user_invite_restriction === "no_invite_required_by_moderators_only") { - data.invite_required = false; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_moderators_only.code; - } else if (user_invite_restriction === "no_invite_required_by_full_members") { - data.invite_required = false; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_full_members.code; - } else if (user_invite_restriction === "by_admins_only") { - data.invite_required = true; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_admins_only.code; - } else if (user_invite_restriction === "by_moderators_only") { - data.invite_required = true; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_moderators_only.code; - } else if (user_invite_restriction === "by_full_members") { - data.invite_required = true; - data.invite_to_realm_policy = - settings_config.common_policy_values.by_full_members.code; - } else { - data.invite_required = true; - data.invite_to_realm_policy = settings_config.common_policy_values.by_members.code; + const waiting_period_threshold = $("#id_realm_waiting_period_setting").val(); + switch (waiting_period_threshold) { + case "none": + data.waiting_period_threshold = 0; + break; + case "three_days": + data.waiting_period_threshold = 3; + break; + case "custom_days": + data.waiting_period_threshold = $( + "#id_realm_waiting_period_threshold", + ).val(); + break; + } + break; } - - const waiting_period_threshold = $("#id_realm_waiting_period_setting").val(); - if (waiting_period_threshold === "none") { - data.waiting_period_threshold = 0; - } else if (waiting_period_threshold === "three_days") { - data.waiting_period_threshold = 3; - } else if (waiting_period_threshold === "custom_days") { - data.waiting_period_threshold = $("#id_realm_waiting_period_threshold").val(); + case "auth_settings": + data = {}; + data.authentication_methods = JSON.stringify(get_auth_method_table_data()); + break; + case "user_defaults": { + const realm_default_twenty_four_hour_time = $( + "#id_realm_default_twenty_four_hour_time", + ).val(); + data.default_twenty_four_hour_time = realm_default_twenty_four_hour_time; + break; } - } else if (subsection === "auth_settings") { - data = {}; - data.authentication_methods = JSON.stringify(get_auth_method_table_data()); - } else if (subsection === "user_defaults") { - const realm_default_twenty_four_hour_time = $( - "#id_realm_default_twenty_four_hour_time", - ).val(); - data.default_twenty_four_hour_time = realm_default_twenty_four_hour_time; } return data; }