Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings_panel_menu: Convert make_menu to an ES6 class SettingsPanelMenu.
Anders Kaseorg0b4483c8d272
diff --git a/static/js/settings_panel_menu.js b/static/js/settings_panel_menu.jsindex d6ebcf0a151..eff6eaf0912 100644--- a/static/js/settings_panel_menu.js+++ b/static/js/settings_panel_menu.js@@ -4,79 +4,93 @@ exports.mobile_deactivate_section = function () {$settings_overlay_container.find(".settings-header.mobile").removeClass("slide-left");};-exports.make_menu = function (opts) {- const main_elem = opts.main_elem;- const hash_prefix = opts.hash_prefix;- let curr_li = main_elem.children("li").eq(0);+function two_column_mode() {+ return $("#settings_overlay_container").css("--single-column") === undefined;+}++class SettingsPanelMenu {+ constructor(opts) {+ this.main_elem = opts.main_elem;+ this.hash_prefix = opts.hash_prefix;+ this.curr_li = this.main_elem.children("li").eq(0);+ if (this.curr_li === undefined) {+ throw new Error("CURR_LI IS UNDEFINED RHJRWKTDSF");+ }++ this.main_elem.on("click", "li[data-section]", (e) => {+ const section = $(e.currentTarget).attr("data-section");++ this.activate_section_or_default(section);- const self = {};+ // You generally want to add logic to activate_section,+ // not to this click handler.- function two_column_mode() {- return $("#settings_overlay_container").css("--single-column") === undefined;+ e.stopPropagation();+ });}- self.show = function () {- main_elem.show();- const section = self.current_tab();+ show() {+ this.main_elem.show();+ const section = this.current_tab();if (two_column_mode()) {// In one colum mode want to show the settings list, not the first settings section.- self.activate_section_or_default(section);+ this.activate_section_or_default(section);}- curr_li.trigger("focus");- };+ this.curr_li.trigger("focus");+ }- self.hide = function () {- main_elem.hide();- };+ hide() {+ this.main_elem.hide();+ }- self.current_tab = function () {- return curr_li.data("section");- };+ current_tab() {+ return this.curr_li.data("section");+ }- self.li_for_section = function (section) {+ li_for_section(section) {const li = $("#settings_overlay_container li[data-section='" + section + "']");return li;- };+ }- self.set_key_handlers = function (toggler) {+ set_key_handlers(toggler) {keydown_util.handle({- elem: main_elem,+ elem: this.main_elem,handlers: {left_arrow: toggler.maybe_go_left,right_arrow: toggler.maybe_go_right,- enter_key: self.enter_panel,- up_arrow: self.prev,- down_arrow: self.next,+ enter_key: () => this.enter_panel(),+ up_arrow: () => this.prev(),+ down_arrow: () => this.next(),},});- };+ }- self.prev = function () {- curr_li.prevAll(":visible:first").trigger("focus").trigger("click");+ prev() {+ this.curr_li.prevAll(":visible:first").trigger("focus").trigger("click");return true;- };+ }- self.next = function () {- curr_li.nextAll(":visible:first").trigger("focus").trigger("click");+ next() {+ this.curr_li.nextAll(":visible:first").trigger("focus").trigger("click");return true;- };+ }- self.enter_panel = function () {- const panel = self.get_panel();+ enter_panel() {+ const panel = this.get_panel();const sel = "input:visible,button:visible,select:visible";const panel_elem = panel.find(sel).first();panel_elem.trigger("focus");return true;- };+ }- self.activate_section_or_default = function (section) {+ activate_section_or_default(section) {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.- section = self.current_tab();+ section = this.current_tab();} else {// In single column mode we close the active section// so that you always start at the settings list.@@ -85,19 +99,22 @@ exports.make_menu = function (opts) {}}- curr_li = self.li_for_section(section);+ this.curr_li = this.li_for_section(section);+ if (this.curr_li === undefined) {+ throw new Error("CURR_LI IS UNDEFINED WEJRGWKJ");+ }- main_elem.children("li").removeClass("active");- curr_li.addClass("active");+ this.main_elem.children("li").removeClass("active");+ this.curr_li.addClass("active");- const settings_section_hash = "#" + hash_prefix + section;+ const settings_section_hash = "#" + this.hash_prefix + section;hashchange.update_browser_history(settings_section_hash);$(".settings-section").removeClass("show");settings_sections.load_settings_section(section);- self.get_panel().addClass("show");+ this.get_panel().addClass("show");ui.reset_scrollbar($("#settings_content"));@@ -106,35 +123,23 @@ exports.make_menu = function (opts) {$settings_overlay_container.find(".settings-header.mobile").addClass("slide-left");settings.set_settings_header(section);- };+ }- self.get_panel = function () {- const section = curr_li.data("section");+ get_panel() {+ const section = this.curr_li.data("section");const sel = "[data-name='" + section + "']";const panel = $(".settings-section" + sel);return panel;- };-- main_elem.on("click", "li[data-section]", function (e) {- const section = $(this).attr("data-section");-- self.activate_section_or_default(section);-- // You generally want to add logic to activate_section,- // not to this click handler.-- e.stopPropagation();- });-- return self;-};+ }+}+exports.SettingsPanelMenu = SettingsPanelMenu;exports.initialize = function () {- exports.normal_settings = exports.make_menu({+ exports.normal_settings = new SettingsPanelMenu({main_elem: $(".normal-settings-list"),hash_prefix: "settings/",});- exports.org_settings = exports.make_menu({+ exports.org_settings = new SettingsPanelMenu({main_elem: $(".org-settings-list"),hash_prefix: "organization/",});