/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { html, when } from "chrome://global/content/vendor/lit.all.mjs"; import { SidebarPage } from "./sidebar-page.mjs"; // eslint-disable-next-line import/no-unassigned-import import "chrome://global/content/elements/moz-radio-group.mjs"; const l10nMap = new Map([ ["viewGenaiChatSidebar", "sidebar-menu-genai-chat-label"], ["viewReviewCheckerSidebar", "sidebar-menu-review-checker-label"], ["viewHistorySidebar", "sidebar-menu-history-label"], ["viewTabsSidebar", "sidebar-menu-synced-tabs-label"], ["viewBookmarksSidebar", "sidebar-menu-bookmarks-label"], ]); const VISIBILITY_SETTING_PREF = "sidebar.visibility"; const TAB_DIRECTION_SETTING_PREF = "sidebar.verticalTabs"; export class SidebarCustomize extends SidebarPage { constructor() { super(); this.activeExtIndex = 0; this.visibility = Services.prefs.getStringPref( VISIBILITY_SETTING_PREF, "always-show" ); this.boundObserve = (...args) => this.observe(...args); } static properties = { activeExtIndex: { type: Number }, visibility: { type: String }, }; static queries = { toolInputs: { all: ".customize-group moz-checkbox" }, extensionLinks: { all: ".extension-link" }, positionInputs: { all: ".position-setting" }, visibilityInputs: { all: ".visibility-setting" }, verticalTabsInputs: { all: ".vertical-tabs-setting" }, }; connectedCallback() { super.connectedCallback(); this.getWindow().addEventListener("SidebarItemAdded", this); this.getWindow().addEventListener("SidebarItemChanged", this); this.getWindow().addEventListener("SidebarItemRemoved", this); Services.prefs.addObserver(VISIBILITY_SETTING_PREF, this.boundObserve); } disconnectedCallback() { super.disconnectedCallback(); this.getWindow().removeEventListener("SidebarItemAdded", this); this.getWindow().removeEventListener("SidebarItemChanged", this); this.getWindow().removeEventListener("SidebarItemRemoved", this); Services.prefs.removeObserver(VISIBILITY_SETTING_PREF, this.boundObserve); } observe(subject, topic, prefName) { switch (topic) { case "nsPref:changed": switch (prefName) { case VISIBILITY_SETTING_PREF: this.visibility = Services.prefs.getStringPref( VISIBILITY_SETTING_PREF, "always-show" ); break; } break; } } get sidebarLauncher() { return this.getWindow().document.querySelector("sidebar-launcher"); } getWindow() { return window.browsingContext.embedderWindowGlobal.browsingContext.window; } handleEvent(e) { switch (e.type) { case "SidebarItemAdded": case "SidebarItemChanged": case "SidebarItemRemoved": this.requestUpdate(); break; } } async onToggleInput(e) { e.preventDefault(); this.getWindow().SidebarController.toggleTool(e.target.id); switch (e.target.id) { case "viewGenaiChatSidebar": Glean.sidebarCustomize.chatbotEnabled.record({ checked: e.target.checked, }); break; case "viewTabsSidebar": Glean.sidebarCustomize.syncedTabsEnabled.record({ checked: e.target.checked, }); break; case "viewHistorySidebar": Glean.sidebarCustomize.historyEnabled.record({ checked: e.target.checked, }); break; case "viewBookmarksSidebar": Glean.sidebarCustomize.bookmarksEnabled.record({ checked: e.target.checked, }); break; case "viewReviewCheckerSidebar": Glean.sidebarCustomize.shoppingReviewCheckerEnabled.record({ checked: e.target.checked, }); break; } } getInputL10nId(view) { return l10nMap.get(view); } openFirefoxSettings(e) { if (e.type == "click" || (e.type == "keydown" && e.code == "Enter")) { e.preventDefault(); this.getWindow().openPreferences(); Glean.sidebarCustomize.firefoxSettingsClicked.record(); } } inputTemplate(tool) { if (tool.hidden) { return null; } return html` `; } async manageAddon(extensionId) { await this.getWindow().BrowserAddonUI.manageAddon( extensionId, "unifiedExtensions" ); Glean.sidebarCustomize.extensionsClicked.record(); } handleKeydown(e) { if (e.code == "ArrowUp") { if (this.activeExtIndex > 0) { this.focusIndex(this.activeExtIndex - 1); } } else if (e.code == "ArrowDown") { if (this.activeExtIndex < this.extensionLinks.length - 1) { this.focusIndex(this.activeExtIndex + 1); } } else if ( (e.type == "keydown" && e.code == "Enter") || (e.type == "keydown" && e.code == "Space") ) { this.manageAddon(e.target.getAttribute("extensionId")); } } focusIndex(index) { let extLinkList = Array.from( this.shadowRoot.querySelectorAll(".extension-link") ); extLinkList[index].focus(); this.activeExtIndex = index; } reversePosition() { const { SidebarController } = this.getWindow(); SidebarController.reversePosition(); Glean.sidebarCustomize.sidebarPosition.record({ position: SidebarController._positionStart !== this.getWindow().RTL_UI ? "left" : "right", }); } extensionTemplate(extension, index) { return html`
this.manageAddon(extension.extensionId)} @keydown=${this.handleKeydown} > e.preventDefault()} >${extension.tooltiptext}
`; } render() { let extensions = this.getWindow().SidebarController.getExtensions(); return html` ${this.stylesheet()} `; } #handleVisibilityChange({ target: { value } }) { this.visibility = value; Services.prefs.setStringPref(VISIBILITY_SETTING_PREF, value); Glean.sidebarCustomize.sidebarDisplay.record({ preference: value === "always-show" ? "always" : "hide", }); } #handleTabDirectionChange({ target: { value } }) { const verticalTabsEnabled = value === "true"; Services.prefs.setBoolPref(TAB_DIRECTION_SETTING_PREF, verticalTabsEnabled); Glean.sidebarCustomize.tabsLayout.record({ orientation: verticalTabsEnabled ? "vertical" : "horizontal", }); } } customElements.define("sidebar-customize", SidebarCustomize);