eslint: Fix unicorn/prefer-switch.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-05-09 14:36:58 -07:00 committed by Tim Abbott
parent 04569bc17e
commit 5ca763fc24
12 changed files with 512 additions and 396 deletions

View File

@ -102,7 +102,6 @@
"unicorn/prefer-module": "off", "unicorn/prefer-module": "off",
"unicorn/prefer-node-protocol": "off", "unicorn/prefer-node-protocol": "off",
"unicorn/prefer-spread": "off", "unicorn/prefer-spread": "off",
"unicorn/prefer-switch": "off",
"unicorn/prefer-ternary": "off", "unicorn/prefer-ternary": "off",
"unicorn/prevent-abbreviations": "off", "unicorn/prevent-abbreviations": "off",
"valid-typeof": ["error", {"requireStringLiterals": true}], "valid-typeof": ["error", {"requireStringLiterals": true}],

View File

@ -38,19 +38,23 @@ run_test("initialize", (override) => {
let create_ajax_request_form_call_count = 0; let create_ajax_request_form_call_count = 0;
helpers.__Rewire__("create_ajax_request", (url, form_name, stripe_token, redirect_to) => { helpers.__Rewire__("create_ajax_request", (url, form_name, stripe_token, redirect_to) => {
create_ajax_request_form_call_count += 1; create_ajax_request_form_call_count += 1;
if (form_name === "autopay") { switch (form_name) {
case "autopay":
assert.equal(url, "/json/billing/upgrade"); assert.equal(url, "/json/billing/upgrade");
assert.equal(stripe_token, "stripe_add_card_token"); assert.equal(stripe_token, "stripe_add_card_token");
assert.equal(redirect_to, undefined); assert.equal(redirect_to, undefined);
} else if (form_name === "invoice") { break;
case "invoice":
assert.equal(url, "/json/billing/upgrade"); assert.equal(url, "/json/billing/upgrade");
assert.equal(stripe_token, undefined); assert.equal(stripe_token, undefined);
assert.equal(redirect_to, undefined); assert.equal(redirect_to, undefined);
} else if (form_name === "sponsorship") { break;
case "sponsorship":
assert.equal(url, "/json/billing/sponsorship"); assert.equal(url, "/json/billing/sponsorship");
assert.equal(stripe_token, undefined); assert.equal(stripe_token, undefined);
assert.equal(redirect_to, "/"); assert.equal(redirect_to, "/");
} else { break;
default:
throw new Error("Unhandled case"); throw new Error("Unhandled case");
} }
}); });

View File

@ -441,7 +441,6 @@ export function navigate(event_name, e) {
const selected_emoji = get_rendered_emoji(current_section, current_index); const selected_emoji = get_rendered_emoji(current_section, current_index);
const is_filter_focused = $(".emoji-popover-filter").is(":focus"); const is_filter_focused = $(".emoji-popover-filter").is(":focus");
let next_section = 0;
// special cases // special cases
if (is_filter_focused) { if (is_filter_focused) {
// Move down into emoji map. // Move down into emoji map.
@ -481,42 +480,32 @@ export function navigate(event_name, e) {
reset_emoji_showcase(); reset_emoji_showcase();
return true; return true;
} }
} else if (event_name === "tab") { return false;
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;
} }
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); return maybe_change_focused_emoji($emoji_map, next_coord.section, next_coord.index);
} }
default:
return false; return false;
}
} }
function process_keypress(e) { function process_keypress(e) {

View File

@ -69,35 +69,42 @@ function message_in_home(message) {
function message_matches_search_term(message, operator, operand) { function message_matches_search_term(message, operator, operand) {
switch (operator) { switch (operator) {
case "has": case "has":
if (operand === "image") { switch (operand) {
case "image":
return message_parser.message_has_image(message); return message_parser.message_has_image(message);
} else if (operand === "link") { case "link":
return message_parser.message_has_link(message); return message_parser.message_has_link(message);
} else if (operand === "attachment") { case "attachment":
return message_parser.message_has_attachment(message); 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);
} }
case "is":
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": case "in":
if (operand === "home") { switch (operand) {
case "home":
return message_in_home(message); return message_in_home(message);
} else if (operand === "all") { case "all":
return true; return true;
} default:
return false; // in:whatever returns false return false; // in:whatever returns false
}
case "near": case "near":
// this is all handled server side // this is all handled server side

View File

@ -680,23 +680,31 @@ export function process_hotkey(e, hotkey) {
) { ) {
compose_actions.cancel(); compose_actions.cancel();
// don't return, as we still want it to be picked up by the code below // don't return, as we still want it to be picked up by the code below
} else if (event_name === "page_up") { } else {
switch (event_name) {
case "page_up":
$(":focus").caret(0).animate({scrollTop: 0}, "fast"); $(":focus").caret(0).animate({scrollTop: 0}, "fast");
return true; return true;
} else if (event_name === "page_down") { case "page_down": {
// so that it always goes to the end of the text box. // so that it always goes to the end of the text box.
const height = $(":focus")[0].scrollHeight; const height = $(":focus")[0].scrollHeight;
$(":focus").caret(Number.POSITIVE_INFINITY).animate({scrollTop: height}, "fast"); $(":focus")
.caret(Number.POSITIVE_INFINITY)
.animate({scrollTop: height}, "fast");
return true; return true;
} else if (event_name === "search_with_k") { }
case "search_with_k":
// Do nothing; this allows one to use Ctrl+K inside compose. // Do nothing; this allows one to use Ctrl+K inside compose.
} else if (event_name === "star_message") { break;
case "star_message":
// Do nothing; this allows one to use Ctrl+S inside compose. // Do nothing; this allows one to use Ctrl+S inside compose.
} else { break;
default:
// Let the browser handle the key normally. // Let the browser handle the key normally.
return false; return false;
} }
} }
}
if (event_name === "left_arrow") { if (event_name === "left_arrow") {
if (overlays.lightbox_open()) { if (overlays.lightbox_open()) {

View File

@ -316,11 +316,14 @@ export function create(opts) {
const $pill = store.$parent.find(".pill:focus"); const $pill = store.$parent.find(".pill:focus");
if (char === KEY.LEFT_ARROW) { switch (char) {
case KEY.LEFT_ARROW:
$pill.prev().trigger("focus"); $pill.prev().trigger("focus");
} else if (char === KEY.RIGHT_ARROW) { break;
case KEY.RIGHT_ARROW:
$pill.next().trigger("focus"); $pill.next().trigger("focus");
} else if (char === KEY.BACKSPACE) { break;
case KEY.BACKSPACE: {
const $next = $pill.next(); const $next = $pill.next();
const id = $pill.data("id"); const id = $pill.data("id");
funcs.removePill(id); funcs.removePill(id);
@ -328,6 +331,8 @@ export function create(opts) {
// the "Backspace" key in Firefox will go back a page if you do // the "Backspace" key in Firefox will go back a page if you do
// not prevent it. // not prevent it.
e.preventDefault(); e.preventDefault();
break;
}
} }
}); });

View File

@ -79,11 +79,14 @@ function display_video(payload) {
).hide(); ).hide();
let source; let source;
if (payload.type === "youtube-video") { switch (payload.type) {
case "youtube-video":
source = "https://www.youtube.com/embed/" + payload.source; source = "https://www.youtube.com/embed/" + payload.source;
} else if (payload.type === "vimeo-video") { break;
case "vimeo-video":
source = "https://player.vimeo.com/video/" + payload.source; source = "https://player.vimeo.com/video/" + payload.source;
} else if (payload.type === "embed-video") { break;
case "embed-video":
// Use data: to load the player in a unique origin for security. // Use data: to load the player in a unique origin for security.
source = source =
"data:text/html," + "data:text/html," +
@ -91,6 +94,7 @@ function display_video(payload) {
"<!DOCTYPE html><style>iframe{position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box}</style>" + "<!DOCTYPE html><style>iframe{position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box}</style>" +
payload.source, payload.source,
); );
break;
} }
const iframe = $("<iframe></iframe>"); const iframe = $("<iframe></iframe>");

View File

@ -101,14 +101,20 @@ const funcs = {
if (!overlays.lightbox_open()) { if (!overlays.lightbox_open()) {
return; return;
} }
if (e.key === "Z" || e.key === "+") { switch (e.key) {
case "Z":
case "+":
funcs.setZoom(meta, "+"); funcs.setZoom(meta, "+");
funcs.displayImage(canvas, context, meta); funcs.displayImage(canvas, context, meta);
} else if (e.key === "z" || e.key === "-") { break;
case "z":
case "-":
funcs.setZoom(meta, "-"); funcs.setZoom(meta, "-");
funcs.displayImage(canvas, context, meta); funcs.displayImage(canvas, context, meta);
} else if (e.key === "v") { break;
case "v":
overlays.close_overlay("lightbox"); overlays.close_overlay("lightbox");
break;
} }
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();

View File

@ -401,11 +401,13 @@ function edit_message(row, raw_content) {
stream_bar.decorate(stream_name, stream_header_colorblock, false); stream_bar.decorate(stream_name, stream_header_colorblock, false);
}); });
if (editability === editability_types.NO) { switch (editability) {
case editability_types.NO:
message_edit_content.attr("readonly", "readonly"); message_edit_content.attr("readonly", "readonly");
message_edit_topic.attr("readonly", "readonly"); message_edit_topic.attr("readonly", "readonly");
create_copy_to_clipboard_handler(copy_message[0], message.id); create_copy_to_clipboard_handler(copy_message[0], message.id);
} else if (editability === editability_types.NO_LONGER) { break;
case editability_types.NO_LONGER:
// You can currently only reach this state in non-streams. If that // You can currently only reach this state in non-streams. If that
// changes (e.g. if we stop allowing topics to be modified forever // changes (e.g. if we stop allowing topics to be modified forever
// in streams), then we'll need to disable // in streams), then we'll need to disable
@ -413,12 +415,14 @@ function edit_message(row, raw_content) {
message_edit_content.attr("readonly", "readonly"); message_edit_content.attr("readonly", "readonly");
message_edit_countdown_timer.text($t({defaultMessage: "View source"})); message_edit_countdown_timer.text($t({defaultMessage: "View source"}));
create_copy_to_clipboard_handler(copy_message[0], message.id); create_copy_to_clipboard_handler(copy_message[0], message.id);
} else if (editability === editability_types.TOPIC_ONLY) { break;
case editability_types.TOPIC_ONLY:
message_edit_content.attr("readonly", "readonly"); message_edit_content.attr("readonly", "readonly");
// Hint why you can edit the topic but not the message content // Hint why you can edit the topic but not the message content
message_edit_countdown_timer.text($t({defaultMessage: "Topic editing only"})); message_edit_countdown_timer.text($t({defaultMessage: "Topic editing only"}));
create_copy_to_clipboard_handler(copy_message[0], message.id); create_copy_to_clipboard_handler(copy_message[0], message.id);
} else if (editability === editability_types.FULL) { break;
case editability_types.FULL: {
copy_message.remove(); copy_message.remove();
const edit_id = `#edit_form_${CSS.escape(rows.id(row))} .message_edit_content`; const edit_id = `#edit_form_${CSS.escape(rows.id(row))} .message_edit_content`;
const listeners = resize.watch_manual_resize(edit_id); const listeners = resize.watch_manual_resize(edit_id);
@ -433,6 +437,8 @@ function edit_message(row, raw_content) {
$(edit_id).on("keyup", function (event) { $(edit_id).on("keyup", function (event) {
compose.handle_keyup(event, $(this).expectOne()); compose.handle_keyup(event, $(this).expectOne());
}); });
break;
}
} }
// Add tooltip // Add tooltip

View File

@ -328,16 +328,20 @@ export function activate(raw_operators, opts) {
// particular message ID (which could be max_int), or we're // particular message ID (which could be max_int), or we're
// asking the server to figure out for us what the first // asking the server to figure out for us what the first
// unread message is, and center the narrow around that. // unread message is, and center the narrow around that.
if (id_info.final_select_id === undefined) { switch (id_info.final_select_id) {
case undefined:
anchor = "first_unread"; anchor = "first_unread";
} else if (id_info.final_select_id === -1) { break;
case -1:
// This case should never happen in this code path; it's // This case should never happen in this code path; it's
// here in case we choose to extract this as an // here in case we choose to extract this as an
// independent reusable function. // independent reusable function.
anchor = "oldest"; anchor = "oldest";
} else if (id_info.final_select_id === LARGER_THAN_MAX_MESSAGE_ID) { break;
case LARGER_THAN_MAX_MESSAGE_ID:
anchor = "newest"; anchor = "newest";
} else { break;
default:
anchor = id_info.final_select_id; anchor = id_info.final_select_id;
} }

View File

@ -130,21 +130,28 @@ export function get_direction(str) {
} }
const bidi_class = get_bidi_class(ch); const bidi_class = get_bidi_class(ch);
if (bidi_class === "I") { switch (bidi_class) {
case "I":
// LRI, RLI, FSI // LRI, RLI, FSI
isolations += 1; isolations += 1;
} else if (bidi_class === "PDI") { break;
case "PDI":
if (isolations > 0) { if (isolations > 0) {
isolations -= 1; isolations -= 1;
} }
} else if (bidi_class === "R") { break;
case "R":
// R, AL // R, AL
if (isolations === 0) { if (isolations === 0) {
return "rtl"; return "rtl";
} }
} else if (bidi_class === "L" && isolations === 0) { break;
case "L":
if (isolations === 0) {
return "ltr"; return "ltr";
} }
break;
}
} }
return "ltr"; return "ltr";
} }

View File

@ -399,31 +399,37 @@ export function populate_auth_methods(auth_methods) {
function update_dependent_subsettings(property_name) { function update_dependent_subsettings(property_name) {
if (simple_dropdown_properties.includes(property_name)) { if (simple_dropdown_properties.includes(property_name)) {
set_property_dropdown_value(property_name); set_property_dropdown_value(property_name);
} else if (property_name === "realm_waiting_period_threshold") { return;
}
switch (property_name) {
case "realm_waiting_period_threshold":
set_realm_waiting_period_dropdown(); set_realm_waiting_period_dropdown();
} else if ( break;
property_name === "realm_video_chat_provider" case "realm_video_chat_provider":
) {
set_video_chat_provider_dropdown(); set_video_chat_provider_dropdown();
} else if ( break;
property_name === "realm_msg_edit_limit_setting" || case "realm_msg_edit_limit_setting":
property_name === "realm_message_content_edit_limit_minutes" case "realm_message_content_edit_limit_minutes":
) {
set_msg_edit_limit_dropdown(); set_msg_edit_limit_dropdown();
} else if (property_name === "realm_message_retention_days") { break;
case "realm_message_retention_days":
set_message_retention_setting_dropdown(); set_message_retention_setting_dropdown();
} else if ( break;
property_name === "realm_msg_delete_limit_setting" || case "realm_msg_delete_limit_setting":
property_name === "realm_message_content_delete_limit_minutes" case "realm_message_content_delete_limit_minutes":
) {
set_msg_delete_limit_dropdown(); set_msg_delete_limit_dropdown();
} else if (property_name === "realm_org_join_restrictions") { break;
case "realm_org_join_restrictions":
set_org_join_restrictions_dropdown(); set_org_join_restrictions_dropdown();
} else if (property_name === "realm_message_content_allowed_in_email_notifications") { break;
case "realm_message_content_allowed_in_email_notifications":
set_message_content_in_email_notifications_visiblity(); set_message_content_in_email_notifications_visiblity();
} else if (property_name === "realm_digest_emails_enabled") { break;
case "realm_digest_emails_enabled":
settings_notifications.set_enable_digest_emails_visibility(); settings_notifications.set_enable_digest_emails_visibility();
set_digest_emails_weekday_visibility(); set_digest_emails_weekday_visibility();
break;
} }
} }
@ -436,19 +442,26 @@ function discard_property_element_changes(elem) {
const property_name = extract_property_name(elem); const property_name = extract_property_name(elem);
const property_value = get_property_value(property_name); const property_value = get_property_value(property_name);
if (property_name === "realm_authentication_methods") { switch (property_name) {
case "realm_authentication_methods":
populate_auth_methods(property_value); populate_auth_methods(property_value);
} else if (property_name === "realm_notifications_stream_id") { break;
case "realm_notifications_stream_id":
notifications_stream_widget.render(property_value); notifications_stream_widget.render(property_value);
} else if (property_name === "realm_signup_notifications_stream_id") { break;
case "realm_signup_notifications_stream_id":
signup_notifications_stream_widget.render(property_value); signup_notifications_stream_widget.render(property_value);
} else if (property_name === "realm_default_code_block_language") { break;
case "realm_default_code_block_language":
default_code_language_widget.render(property_value); default_code_language_widget.render(property_value);
} else if (property_value !== undefined) { break;
default:
if (property_value !== undefined) {
set_input_element_value(elem, property_value); set_input_element_value(elem, property_value);
} else { } else {
blueslip.error("Element refers to unknown property " + property_name); blueslip.error("Element refers to unknown property " + property_name);
} }
}
update_dependent_subsettings(property_name); update_dependent_subsettings(property_name);
} }
@ -459,29 +472,39 @@ export function sync_realm_settings(property) {
} }
const value = page_params[`realm_${property}`]; const value = page_params[`realm_${property}`];
if (property === "notifications_stream_id") { switch (property) {
case "notifications_stream_id":
notifications_stream_widget.render(value); notifications_stream_widget.render(value);
} else if (property === "signup_notifications_stream_id") { break;
case "signup_notifications_stream_id":
signup_notifications_stream_widget.render(value); signup_notifications_stream_widget.render(value);
} else if (property === "default_code_block_language") { break;
case "default_code_block_language":
default_code_language_widget.render(value); default_code_language_widget.render(value);
break;
} }
if (property === "message_content_edit_limit_seconds") { switch (property) {
case "message_content_edit_limit_seconds":
property = "message_content_edit_limit_minutes"; property = "message_content_edit_limit_minutes";
} else if (property === "allow_message_editing") { break;
case "allow_message_editing":
property = "msg_edit_limit_setting"; property = "msg_edit_limit_setting";
} else if ( break;
property === "emails_restricted_to_domains" || case "emails_restricted_to_domains":
property === "disallow_disposable_email_addresses" case "disallow_disposable_email_addresses":
) {
property = "org_join_restrictions"; property = "org_join_restrictions";
} else if (property === "message_content_delete_limit_seconds") { break;
case "message_content_delete_limit_seconds":
property = "message_content_delete_limit_minutes"; property = "message_content_delete_limit_minutes";
} else if (property === "allow_message_deleting") { break;
case "allow_message_deleting":
property = "msg_delete_limit_setting"; property = "msg_delete_limit_setting";
} else if (property === "invite_required" || property === "invite_to_realm_policy") { break;
case "invite_required":
case "invite_to_realm_policy":
property = "user_invite_restriction"; property = "user_invite_restriction";
break;
} }
const element = $(`#id_realm_${CSS.escape(property)}`); const element = $(`#id_realm_${CSS.escape(property)}`);
if (element.length) { if (element.length) {
@ -515,31 +538,37 @@ export function change_save_button_state($element, state) {
let button_text; let button_text;
let data_status; let data_status;
let is_show; let is_show;
if (state === "unsaved") { switch (state) {
case "unsaved":
button_text = $t({defaultMessage: "Save changes"}); button_text = $t({defaultMessage: "Save changes"});
data_status = "unsaved"; data_status = "unsaved";
is_show = true; is_show = true;
$element.find(".discard-button").show(); $element.find(".discard-button").show();
} else if (state === "saved") { break;
case "saved":
button_text = $t({defaultMessage: "Save changes"}); button_text = $t({defaultMessage: "Save changes"});
data_status = ""; data_status = "";
is_show = false; is_show = false;
} else if (state === "saving") { break;
case "saving":
button_text = $t({defaultMessage: "Saving"}); button_text = $t({defaultMessage: "Saving"});
data_status = "saving"; data_status = "saving";
is_show = true; is_show = true;
$element.find(".discard-button").hide(); $element.find(".discard-button").hide();
$saveBtn.addClass("saving"); $saveBtn.addClass("saving");
} else if (state === "failed") { break;
case "failed":
button_text = $t({defaultMessage: "Save changes"}); button_text = $t({defaultMessage: "Save changes"});
data_status = "failed"; data_status = "failed";
is_show = true; is_show = true;
} else if (state === "succeeded") { break;
case "succeeded":
button_text = $t({defaultMessage: "Saved"}); button_text = $t({defaultMessage: "Saved"});
data_status = "saved"; data_status = "saved";
is_show = false; is_show = false;
break;
} }
$textEl.text(button_text); $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) { export function get_input_element_value(input_elem, input_type) {
input_elem = $(input_elem); input_elem = $(input_elem);
input_type = get_input_type(input_elem, input_type); input_type = get_input_type(input_elem, input_type);
if (input_type) { switch (input_type) {
if (input_type === "boolean") { case "boolean":
return input_elem.prop("checked"); return input_elem.prop("checked");
} case "string":
if (input_type === "string") {
return input_elem.val().trim(); return input_elem.val().trim();
} case "number":
if (input_type === "number") {
return Number.parseInt(input_elem.val().trim(), 10); return Number.parseInt(input_elem.val().trim(), 10);
} default:
}
return undefined; return undefined;
}
} }
export function set_input_element_value(input_elem, value) { 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) {
if (input_type === "boolean") { if (input_type === "boolean") {
return input_elem.prop("checked", value); return input_elem.prop("checked", value);
} } else if (input_type === "string" || input_type === "number") {
if (input_type === "string" || input_type === "number") {
return input_elem.val(value); return input_elem.val(value);
} }
} }
@ -609,22 +635,29 @@ function check_property_changed(elem) {
let current_val = get_property_value(property_name); let current_val = get_property_value(property_name);
let changed_val; let changed_val;
if (property_name === "realm_authentication_methods") { switch (property_name) {
case "realm_authentication_methods":
current_val = sort_object_by_key(current_val); current_val = sort_object_by_key(current_val);
current_val = JSON.stringify(current_val); current_val = JSON.stringify(current_val);
changed_val = get_auth_method_table_data(); changed_val = get_auth_method_table_data();
changed_val = JSON.stringify(changed_val); changed_val = JSON.stringify(changed_val);
} else if (property_name === "realm_notifications_stream_id") { break;
case "realm_notifications_stream_id":
changed_val = Number.parseInt(notifications_stream_widget.value(), 10); changed_val = Number.parseInt(notifications_stream_widget.value(), 10);
} else if (property_name === "realm_signup_notifications_stream_id") { break;
case "realm_signup_notifications_stream_id":
changed_val = Number.parseInt(signup_notifications_stream_widget.value(), 10); changed_val = Number.parseInt(signup_notifications_stream_widget.value(), 10);
} else if (property_name === "realm_default_code_block_language") { break;
case "realm_default_code_block_language":
changed_val = default_code_language_widget.value(); changed_val = default_code_language_widget.value();
} else if (current_val !== undefined) { break;
default:
if (current_val !== undefined) {
changed_val = get_input_element_value(elem, typeof current_val); changed_val = get_input_element_value(elem, typeof current_val);
} else { } else {
blueslip.error("Element refers to unknown property " + property_name); blueslip.error("Element refers to unknown property " + property_name);
} }
}
return current_val !== changed_val; return current_val !== changed_val;
} }
@ -770,7 +803,8 @@ export function build_page() {
function get_complete_data_for_subsection(subsection) { function get_complete_data_for_subsection(subsection) {
let data = {}; let data = {};
if (subsection === "msg_editing") { switch (subsection) {
case "msg_editing": {
const edit_limit_setting_value = $("#id_realm_msg_edit_limit_setting").val(); const edit_limit_setting_value = $("#id_realm_msg_edit_limit_setting").val();
if (edit_limit_setting_value === "never") { if (edit_limit_setting_value === "never") {
data.allow_message_editing = false; data.allow_message_editing = false;
@ -795,7 +829,9 @@ export function build_page() {
$("#id_realm_message_content_delete_limit_minutes"), $("#id_realm_message_content_delete_limit_minutes"),
); );
// Disable deleting if the parsed time limit is 0 seconds // Disable deleting if the parsed time limit is 0 seconds
data.allow_message_deleting = Boolean(data.message_content_delete_limit_seconds); data.allow_message_deleting = Boolean(
data.message_content_delete_limit_seconds,
);
} else { } else {
data.allow_message_deleting = true; data.allow_message_deleting = true;
data.message_content_delete_limit_seconds = data.message_content_delete_limit_seconds =
@ -803,14 +839,22 @@ export function build_page() {
delete_limit_setting_value, delete_limit_setting_value,
).seconds; ).seconds;
} }
} else if (subsection === "notifications") { break;
data.notifications_stream_id = Number.parseInt(notifications_stream_widget.value(), 10); }
case "notifications":
data.notifications_stream_id = Number.parseInt(
notifications_stream_widget.value(),
10,
);
data.signup_notifications_stream_id = Number.parseInt( data.signup_notifications_stream_id = Number.parseInt(
signup_notifications_stream_widget.value(), signup_notifications_stream_widget.value(),
10, 10,
); );
} else if (subsection === "message_retention") { break;
const message_retention_setting_value = $("#id_realm_message_retention_setting").val(); case "message_retention": {
const message_retention_setting_value = $(
"#id_realm_message_retention_setting",
).val();
if (message_retention_setting_value === "retain_forever") { if (message_retention_setting_value === "retain_forever") {
data.message_retention_days = JSON.stringify("forever"); data.message_retention_days = JSON.stringify("forever");
} else { } else {
@ -818,80 +862,113 @@ export function build_page() {
get_input_element_value($("#id_realm_message_retention_days")), get_input_element_value($("#id_realm_message_retention_days")),
); );
} }
} else if (subsection === "other_settings") { break;
}
case "other_settings": {
const code_block_language_value = default_code_language_widget.value(); const code_block_language_value = default_code_language_widget.value();
// No need to JSON-encode, since this value is already a string. // No need to JSON-encode, since this value is already a string.
data.default_code_block_language = code_block_language_value; data.default_code_block_language = code_block_language_value;
} else if (subsection === "other_permissions") { break;
const add_emoji_permission = $("#id_realm_add_emoji_by_admins_only").val();
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") { 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(); const org_join_restrictions = $("#id_realm_org_join_restrictions").val();
if (org_join_restrictions === "only_selected_domain") { switch (org_join_restrictions) {
case "only_selected_domain":
data.emails_restricted_to_domains = true; data.emails_restricted_to_domains = true;
data.disallow_disposable_email_addresses = false; data.disallow_disposable_email_addresses = false;
} else if (org_join_restrictions === "no_disposable_email") { break;
case "no_disposable_email":
data.emails_restricted_to_domains = false; data.emails_restricted_to_domains = false;
data.disallow_disposable_email_addresses = true; data.disallow_disposable_email_addresses = true;
} else if (org_join_restrictions === "no_restriction") { break;
case "no_restriction":
data.disallow_disposable_email_addresses = false; data.disallow_disposable_email_addresses = false;
data.emails_restricted_to_domains = false; data.emails_restricted_to_domains = false;
break;
} }
const user_invite_restriction = $("#id_realm_user_invite_restriction").val(); const user_invite_restriction = $("#id_realm_user_invite_restriction").val();
if (user_invite_restriction === "no_invite_required") { switch (user_invite_restriction) {
case "no_invite_required":
data.invite_required = false; data.invite_required = false;
data.invite_to_realm_policy = settings_config.common_policy_values.by_members.code; data.invite_to_realm_policy =
} else if (user_invite_restriction === "no_invite_required_by_admins_only") { settings_config.common_policy_values.by_members.code;
break;
case "no_invite_required_by_admins_only":
data.invite_required = false; data.invite_required = false;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_admins_only.code; settings_config.common_policy_values.by_admins_only.code;
} else if (user_invite_restriction === "no_invite_required_by_moderators_only") { break;
case "no_invite_required_by_moderators_only":
data.invite_required = false; data.invite_required = false;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_moderators_only.code; settings_config.common_policy_values.by_moderators_only.code;
} else if (user_invite_restriction === "no_invite_required_by_full_members") { break;
case "no_invite_required_by_full_members":
data.invite_required = false; data.invite_required = false;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_full_members.code; settings_config.common_policy_values.by_full_members.code;
} else if (user_invite_restriction === "by_admins_only") { break;
case "by_admins_only":
data.invite_required = true; data.invite_required = true;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_admins_only.code; settings_config.common_policy_values.by_admins_only.code;
} else if (user_invite_restriction === "by_moderators_only") { break;
case "by_moderators_only":
data.invite_required = true; data.invite_required = true;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_moderators_only.code; settings_config.common_policy_values.by_moderators_only.code;
} else if (user_invite_restriction === "by_full_members") { break;
case "by_full_members":
data.invite_required = true; data.invite_required = true;
data.invite_to_realm_policy = data.invite_to_realm_policy =
settings_config.common_policy_values.by_full_members.code; settings_config.common_policy_values.by_full_members.code;
} else { break;
default:
data.invite_required = true; data.invite_required = true;
data.invite_to_realm_policy = settings_config.common_policy_values.by_members.code; data.invite_to_realm_policy =
settings_config.common_policy_values.by_members.code;
} }
const waiting_period_threshold = $("#id_realm_waiting_period_setting").val(); const waiting_period_threshold = $("#id_realm_waiting_period_setting").val();
if (waiting_period_threshold === "none") { switch (waiting_period_threshold) {
case "none":
data.waiting_period_threshold = 0; data.waiting_period_threshold = 0;
} else if (waiting_period_threshold === "three_days") { break;
case "three_days":
data.waiting_period_threshold = 3; data.waiting_period_threshold = 3;
} else if (waiting_period_threshold === "custom_days") { break;
data.waiting_period_threshold = $("#id_realm_waiting_period_threshold").val(); case "custom_days":
data.waiting_period_threshold = $(
"#id_realm_waiting_period_threshold",
).val();
break;
} }
} else if (subsection === "auth_settings") { break;
}
case "auth_settings":
data = {}; data = {};
data.authentication_methods = JSON.stringify(get_auth_method_table_data()); data.authentication_methods = JSON.stringify(get_auth_method_table_data());
} else if (subsection === "user_defaults") { break;
case "user_defaults": {
const realm_default_twenty_four_hour_time = $( const realm_default_twenty_four_hour_time = $(
"#id_realm_default_twenty_four_hour_time", "#id_realm_default_twenty_four_hour_time",
).val(); ).val();
data.default_twenty_four_hour_time = realm_default_twenty_four_hour_time; data.default_twenty_four_hour_time = realm_default_twenty_four_hour_time;
break;
}
} }
return data; return data;
} }