2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2023-10-09 22:53:32 +02:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
2024-05-30 11:28:49 +02:00
|
|
|
import * as components from "./components";
|
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:01:14 +01:00
|
|
|
import * as keydown_util from "./keydown_util";
|
2021-02-28 01:03:09 +01:00
|
|
|
import * as popovers from "./popovers";
|
2023-04-25 18:01:02 +02:00
|
|
|
import * as scroll_util from "./scroll_util";
|
2021-02-28 01:19:36 +01:00
|
|
|
import * as settings_sections from "./settings_sections";
|
2024-06-12 14:01:52 +02:00
|
|
|
import {redraw_active_users_list, redraw_deactivated_users_list} from "./settings_users";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export let normal_settings;
|
|
|
|
export let org_settings;
|
2021-02-28 00:36:50 +01:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export function mobile_deactivate_section() {
|
2020-06-25 00:51:20 +02:00
|
|
|
const $settings_overlay_container = $("#settings_overlay_container");
|
|
|
|
$settings_overlay_container.find(".right").removeClass("show");
|
|
|
|
$settings_overlay_container.find(".settings-header.mobile").removeClass("slide-left");
|
2021-02-28 01:01:14 +01:00
|
|
|
}
|
2020-06-25 00:51:20 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
function two_column_mode() {
|
|
|
|
return $("#settings_overlay_container").css("--single-column") === undefined;
|
|
|
|
}
|
|
|
|
|
2023-10-09 22:53:32 +02:00
|
|
|
function set_settings_header(key) {
|
|
|
|
const selected_tab_key = $("#settings_page .tab-switcher .selected").data("tab-key");
|
|
|
|
let header_prefix = $t_html({defaultMessage: "Personal settings"});
|
|
|
|
if (selected_tab_key === "organization") {
|
|
|
|
header_prefix = $t_html({defaultMessage: "Organization settings"});
|
|
|
|
}
|
|
|
|
$(".settings-header h1 .header-prefix").text(header_prefix);
|
|
|
|
|
|
|
|
const header_text = $(
|
|
|
|
`#settings_page .sidebar-list [data-section='${CSS.escape(key)}'] .text`,
|
|
|
|
).text();
|
|
|
|
if (header_text) {
|
|
|
|
$(".settings-header h1 .section").text(" / " + header_text);
|
|
|
|
} else {
|
|
|
|
blueslip.warn(
|
|
|
|
"Error: the key '" +
|
|
|
|
key +
|
|
|
|
"' does not exist in the settings" +
|
|
|
|
" sidebar list. Please add it.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export class SettingsPanelMenu {
|
2020-07-23 02:34:41 +02:00
|
|
|
constructor(opts) {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$main_elem = opts.$main_elem;
|
2020-07-23 02:34:41 +02:00
|
|
|
this.hash_prefix = opts.hash_prefix;
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$curr_li = this.$main_elem.children("li").eq(0);
|
2024-05-30 11:16:13 +02:00
|
|
|
this.current_tab = this.$curr_li.data("section");
|
2024-05-30 11:28:49 +02:00
|
|
|
this.current_user_settings_tab = "active";
|
|
|
|
this.org_user_settings_toggler = components.toggle({
|
|
|
|
html_class: "org-user-settings-switcher",
|
|
|
|
child_wants_focus: true,
|
|
|
|
values: [
|
|
|
|
{label: $t({defaultMessage: "Users"}), key: "active"},
|
|
|
|
{
|
|
|
|
label: $t({defaultMessage: "Deactivated users"}),
|
|
|
|
key: "deactivated",
|
|
|
|
},
|
|
|
|
{label: $t({defaultMessage: "Invitations"}), key: "invitations"},
|
|
|
|
],
|
|
|
|
callback: (_name, key) => {
|
|
|
|
browser_history.update(`#organization/users/${key}`);
|
|
|
|
this.set_user_settings_tab(key);
|
|
|
|
$(".user-settings-section").hide();
|
2024-06-12 14:01:52 +02:00
|
|
|
if (key === "active") {
|
|
|
|
redraw_active_users_list();
|
|
|
|
} else if (key === "deactivated") {
|
|
|
|
redraw_deactivated_users_list();
|
|
|
|
}
|
2024-05-30 11:28:49 +02:00
|
|
|
$(`[data-user-settings-section="${CSS.escape(key)}"]`).show();
|
|
|
|
},
|
|
|
|
});
|
2020-07-23 02:34:41 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$main_elem.on("click", "li[data-section]", (e) => {
|
2020-07-23 02:34:41 +02:00
|
|
|
const section = $(e.currentTarget).attr("data-section");
|
|
|
|
|
2024-05-30 11:28:49 +02:00
|
|
|
this.activate_section_or_default(section, this.current_user_settings_tab);
|
2020-07-23 02:34:41 +02:00
|
|
|
// You generally want to add logic to activate_section,
|
|
|
|
// not to this click handler.
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2020-06-25 00:51:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
show() {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$main_elem.show();
|
2024-05-30 11:16:13 +02:00
|
|
|
const section = this.current_tab;
|
2024-05-30 11:28:49 +02:00
|
|
|
const user_settings_tab = this.current_user_settings_tab;
|
|
|
|
|
2020-06-25 00:51:20 +02:00
|
|
|
if (two_column_mode()) {
|
2020-08-11 01:47:44 +02:00
|
|
|
// In one column mode want to show the settings list, not the first settings section.
|
2024-05-30 11:28:49 +02:00
|
|
|
this.activate_section_or_default(section, user_settings_tab);
|
2020-06-24 07:46:43 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$curr_li.trigger("focus");
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2024-05-30 11:28:49 +02:00
|
|
|
show_org_user_settings_toggler() {
|
|
|
|
if ($("#admin-user-list").find(".tab-switcher").length === 0) {
|
|
|
|
const toggler_html = this.org_user_settings_toggler.get();
|
|
|
|
$("#admin-user-list .tab-container").html(toggler_html);
|
|
|
|
|
|
|
|
// We need to re-register these handlers since they are
|
|
|
|
// destroyed once the settings modal closes.
|
|
|
|
this.org_user_settings_toggler.register_event_handlers();
|
2024-06-13 10:48:30 +02:00
|
|
|
this.set_key_handlers(this.org_user_settings_toggler, $("#admin-user-list"));
|
2024-05-30 11:28:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
hide() {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$main_elem.hide();
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
li_for_section(section) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $li = $(`#settings_overlay_container li[data-section='${CSS.escape(section)}']`);
|
|
|
|
return $li;
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-12-02 21:00:53 +01:00
|
|
|
|
2024-06-13 10:48:30 +02:00
|
|
|
set_key_handlers(toggler, $elem = this.$main_elem) {
|
2021-05-26 19:51:07 +02:00
|
|
|
const {vim_left, vim_right, vim_up, vim_down} = keydown_util;
|
2018-06-03 12:42:43 +02:00
|
|
|
keydown_util.handle({
|
2024-06-13 10:48:30 +02:00
|
|
|
$elem,
|
2018-06-03 12:42:43 +02:00
|
|
|
handlers: {
|
2021-05-26 19:51:07 +02:00
|
|
|
ArrowLeft: toggler.maybe_go_left,
|
|
|
|
ArrowRight: toggler.maybe_go_right,
|
|
|
|
Enter: () => this.enter_panel(),
|
|
|
|
ArrowUp: () => this.prev(),
|
|
|
|
ArrowDown: () => this.next(),
|
2021-05-10 17:52:46 +02:00
|
|
|
|
|
|
|
// Binding vim keys as well
|
2021-05-26 19:51:07 +02:00
|
|
|
[vim_left]: toggler.maybe_go_left,
|
|
|
|
[vim_right]: toggler.maybe_go_right,
|
|
|
|
[vim_up]: () => this.prev(),
|
|
|
|
[vim_down]: () => this.next(),
|
2018-06-03 12:42:43 +02:00
|
|
|
},
|
|
|
|
});
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-03 12:42:43 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
prev() {
|
2022-03-16 22:29:41 +01:00
|
|
|
this.$curr_li.prevAll(":visible").first().trigger("focus").trigger("click");
|
2018-06-03 12:42:43 +02:00
|
|
|
return true;
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-03 12:42:43 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
next() {
|
2022-03-16 22:29:41 +01:00
|
|
|
this.$curr_li.nextAll(":visible").first().trigger("focus").trigger("click");
|
2018-06-03 12:42:43 +02:00
|
|
|
return true;
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-03 12:42:43 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
enter_panel() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $panel = this.get_panel();
|
|
|
|
const $panel_elem = $panel.find("input:visible,button:visible,select:visible").first();
|
2018-06-04 12:52:42 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$panel_elem.trigger("focus");
|
2018-06-04 12:52:42 +02:00
|
|
|
return true;
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-04 12:52:42 +02:00
|
|
|
|
2024-05-30 11:16:13 +02:00
|
|
|
set_current_tab(tab) {
|
|
|
|
this.current_tab = tab;
|
|
|
|
}
|
|
|
|
|
2024-05-30 11:28:49 +02:00
|
|
|
set_user_settings_tab(tab) {
|
|
|
|
this.current_user_settings_tab = tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
activate_section_or_default(section, user_settings_tab) {
|
2020-07-03 22:20:57 +02:00
|
|
|
popovers.hide_all();
|
2020-06-25 00:51:20 +02:00
|
|
|
if (!section) {
|
|
|
|
// No section is given so we display the default.
|
|
|
|
|
|
|
|
if (two_column_mode()) {
|
|
|
|
// In two column mode we resume to the last active section.
|
2024-05-30 11:16:13 +02:00
|
|
|
section = this.current_tab;
|
2020-06-25 00:51:20 +02:00
|
|
|
} else {
|
|
|
|
// In single column mode we close the active section
|
|
|
|
// so that you always start at the settings list.
|
2021-02-28 01:01:14 +01:00
|
|
|
mobile_deactivate_section();
|
2020-06-25 00:51:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:04:17 +02:00
|
|
|
const $li_for_section = this.li_for_section(section);
|
|
|
|
if ($li_for_section.length === 0) {
|
|
|
|
// This happens when there is no such section or the user does not have
|
|
|
|
// permission to view that section.
|
2024-05-30 11:16:13 +02:00
|
|
|
section = this.current_tab;
|
2022-10-10 13:04:17 +02:00
|
|
|
} else {
|
|
|
|
this.$curr_li = $li_for_section;
|
|
|
|
}
|
2018-06-03 19:12:11 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
this.$main_elem.children("li").removeClass("active");
|
|
|
|
this.$curr_li.addClass("active");
|
2024-05-30 11:16:13 +02:00
|
|
|
this.set_current_tab(section);
|
2018-12-02 17:34:48 +01:00
|
|
|
|
2024-05-30 11:28:49 +02:00
|
|
|
if (section !== "users") {
|
|
|
|
const settings_section_hash = "#" + this.hash_prefix + section;
|
2021-03-16 07:22:28 +01:00
|
|
|
|
2024-05-30 11:28:49 +02:00
|
|
|
// It could be that the hash has already been set.
|
|
|
|
browser_history.update_hash_internally_if_required(settings_section_hash);
|
|
|
|
}
|
|
|
|
if (section === "users" && this.org_user_settings_toggler !== undefined) {
|
|
|
|
this.show_org_user_settings_toggler();
|
|
|
|
this.org_user_settings_toggler.goto(user_settings_tab);
|
|
|
|
}
|
2018-06-03 17:59:33 +02:00
|
|
|
|
2020-07-03 18:49:26 +02:00
|
|
|
$(".settings-section").removeClass("show");
|
2018-06-03 17:59:33 +02:00
|
|
|
|
2018-12-08 19:28:26 +01:00
|
|
|
settings_sections.load_settings_section(section);
|
2018-06-03 17:59:33 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
this.get_panel().addClass("show");
|
2018-12-02 21:10:32 +01:00
|
|
|
|
2023-04-25 18:01:02 +02:00
|
|
|
scroll_util.reset_scrollbar($("#settings_content"));
|
2019-03-01 01:40:05 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const $settings_overlay_container = $("#settings_overlay_container");
|
2018-12-02 21:10:32 +01:00
|
|
|
$settings_overlay_container.find(".right").addClass("show");
|
|
|
|
$settings_overlay_container.find(".settings-header.mobile").addClass("slide-left");
|
|
|
|
|
2023-10-09 22:53:32 +02:00
|
|
|
set_settings_header(section);
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
2018-06-04 12:52:42 +02:00
|
|
|
|
2020-07-23 02:34:41 +02:00
|
|
|
get_panel() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const section = this.$curr_li.data("section");
|
2021-02-03 23:23:32 +01:00
|
|
|
const sel = `[data-name='${CSS.escape(section)}']`;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $panel = $(".settings-section" + sel);
|
|
|
|
return $panel;
|
2020-07-23 02:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export function initialize() {
|
|
|
|
normal_settings = new SettingsPanelMenu({
|
2022-01-25 11:36:19 +01:00
|
|
|
$main_elem: $(".normal-settings-list"),
|
2018-06-03 17:59:33 +02:00
|
|
|
hash_prefix: "settings/",
|
2018-06-03 17:29:11 +02:00
|
|
|
});
|
2021-02-28 01:01:14 +01:00
|
|
|
org_settings = new SettingsPanelMenu({
|
2022-01-25 11:36:19 +01:00
|
|
|
$main_elem: $(".org-settings-list"),
|
2018-06-03 17:59:33 +02:00
|
|
|
hash_prefix: "organization/",
|
2018-06-03 17:29:11 +02:00
|
|
|
});
|
2021-02-28 01:01:14 +01:00
|
|
|
}
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export function show_normal_settings() {
|
|
|
|
org_settings.hide();
|
|
|
|
normal_settings.show();
|
|
|
|
}
|
2018-06-03 17:29:11 +02:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export function show_org_settings() {
|
|
|
|
normal_settings.hide();
|
|
|
|
org_settings.show();
|
|
|
|
}
|
2018-06-03 12:42:43 +02:00
|
|
|
|
2021-02-28 01:01:14 +01:00
|
|
|
export function set_key_handlers(toggler) {
|
|
|
|
normal_settings.set_key_handlers(toggler);
|
|
|
|
org_settings.set_key_handlers(toggler);
|
|
|
|
}
|