From 1d43a4cccb557728c6632cf1190553a18f6b206a Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 9 Oct 2023 18:44:51 -0700 Subject: [PATCH] stream_edit: Extract toggler to new stream_edit_toggler module. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 1 + web/src/stream_edit.js | 22 +++------------------- web/src/stream_edit_toggler.js | 23 +++++++++++++++++++++++ web/src/stream_ui_updates.js | 22 +++++++++++----------- 4 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 web/src/stream_edit_toggler.js diff --git a/tools/test-js-with-node b/tools/test-js-with-node index d668d39fa2..1848796b2b 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -221,6 +221,7 @@ EXEMPT_FILES = make_set( "web/src/stream_create_subscribers.js", "web/src/stream_edit.js", "web/src/stream_edit_subscribers.js", + "web/src/stream_edit_toggler.js", "web/src/stream_list.js", "web/src/stream_muting.js", "web/src/stream_popover.js", diff --git a/web/src/stream_edit.js b/web/src/stream_edit.js index 07ba724816..c511657742 100644 --- a/web/src/stream_edit.js +++ b/web/src/stream_edit.js @@ -11,7 +11,6 @@ import render_stream_settings from "../templates/stream_settings/stream_settings import * as blueslip from "./blueslip"; import * as browser_history from "./browser_history"; import * as channel from "./channel"; -import * as components from "./components"; import * as confirm_dialog from "./confirm_dialog"; import {show_copied_confirmation} from "./copied_tooltip"; import * as dialog_widget from "./dialog_widget"; @@ -28,6 +27,7 @@ import * as settings_org from "./settings_org"; import * as stream_color from "./stream_color"; import * as stream_data from "./stream_data"; import * as stream_edit_subscribers from "./stream_edit_subscribers"; +import * as stream_edit_toggler from "./stream_edit_toggler"; import * as stream_settings_api from "./stream_settings_api"; import * as stream_settings_containers from "./stream_settings_containers"; import * as stream_settings_data from "./stream_settings_data"; @@ -39,9 +39,6 @@ import * as user_groups from "./user_groups"; import {user_settings} from "./user_settings"; import * as util from "./util"; -export let toggler; -export let select_tab = "personal_settings"; - function setup_subscriptions_stream_hash(sub) { const hash = hash_util.stream_edit_url(sub); browser_history.update(hash); @@ -261,7 +258,7 @@ export function show_settings_for(node) { }); scroll_util.get_content_element($("#stream_settings")).html(html); - $("#stream_settings .tab-container").prepend(toggler.get()); + $("#stream_settings .tab-container").prepend(stream_edit_toggler.toggler.get()); stream_ui_updates.update_toggler_for_sub(sub); const $edit_container = stream_settings_containers.get_edit_container(sub); @@ -278,20 +275,7 @@ export function show_settings_for(node) { } export function setup_stream_settings(node) { - toggler = components.toggle({ - child_wants_focus: true, - values: [ - {label: $t({defaultMessage: "General"}), key: "general_settings"}, - {label: $t({defaultMessage: "Personal"}), key: "personal_settings"}, - {label: $t({defaultMessage: "Subscribers"}), key: "subscriber_settings"}, - ], - callback(_name, key) { - $(".stream_section").hide(); - $(`.${CSS.escape(key)}`).show(); - select_tab = key; - }, - }); - + stream_edit_toggler.setup_toggler(); show_settings_for(node); } diff --git a/web/src/stream_edit_toggler.js b/web/src/stream_edit_toggler.js new file mode 100644 index 0000000000..264b3abdb2 --- /dev/null +++ b/web/src/stream_edit_toggler.js @@ -0,0 +1,23 @@ +import $ from "jquery"; + +import * as components from "./components"; +import {$t} from "./i18n"; + +export let toggler; +export let select_tab = "personal_settings"; + +export function setup_toggler() { + toggler = components.toggle({ + child_wants_focus: true, + values: [ + {label: $t({defaultMessage: "General"}), key: "general_settings"}, + {label: $t({defaultMessage: "Personal"}), key: "personal_settings"}, + {label: $t({defaultMessage: "Subscribers"}), key: "subscriber_settings"}, + ], + callback(_name, key) { + $(".stream_section").hide(); + $(`.${CSS.escape(key)}`).show(); + select_tab = key; + }, + }); +} diff --git a/web/src/stream_ui_updates.js b/web/src/stream_ui_updates.js index 247c28c4c9..939385831c 100644 --- a/web/src/stream_ui_updates.js +++ b/web/src/stream_ui_updates.js @@ -11,7 +11,7 @@ import {page_params} from "./page_params"; import * as settings_data from "./settings_data"; import * as settings_org from "./settings_org"; import * as stream_data from "./stream_data"; -import * as stream_edit from "./stream_edit"; +import * as stream_edit_toggler from "./stream_edit_toggler"; import * as stream_settings_containers from "./stream_settings_containers"; import * as stream_settings_ui from "./stream_settings_ui"; @@ -37,18 +37,18 @@ export function update_toggler_for_sub(sub) { return; } if (sub.subscribed) { - stream_edit.toggler.enable_tab("personal_settings"); - stream_edit.toggler.goto(stream_edit.select_tab); + stream_edit_toggler.toggler.enable_tab("personal_settings"); + stream_edit_toggler.toggler.goto(stream_edit_toggler.select_tab); } else { - if (stream_edit.select_tab === "personal_settings") { + if (stream_edit_toggler.select_tab === "personal_settings") { // Go to the general settings tab, if the user is not // subscribed. Also preserve the previous selected tab, // to render next time a stream row is selected. - stream_edit.toggler.goto("general_settings"); + stream_edit_toggler.toggler.goto("general_settings"); } else { - stream_edit.toggler.goto(stream_edit.select_tab); + stream_edit_toggler.toggler.goto(stream_edit_toggler.select_tab); } - stream_edit.toggler.disable_tab("personal_settings"); + stream_edit_toggler.toggler.disable_tab("personal_settings"); } enable_or_disable_subscribers_tab(sub); } @@ -59,14 +59,14 @@ export function enable_or_disable_subscribers_tab(sub) { } if (!stream_data.can_view_subscribers(sub)) { - stream_edit.toggler.disable_tab("subscriber_settings"); - if (stream_edit.select_tab === "subscriber_settings") { - stream_edit.toggler.goto("general_settings"); + stream_edit_toggler.toggler.disable_tab("subscriber_settings"); + if (stream_edit_toggler.select_tab === "subscriber_settings") { + stream_edit_toggler.toggler.goto("general_settings"); } return; } - stream_edit.toggler.enable_tab("subscriber_settings"); + stream_edit_toggler.toggler.enable_tab("subscriber_settings"); } export function update_settings_button_for_sub(sub) {