/* 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";
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const l10nMap = new Map([
["viewGenaiChatSidebar", "sidebar-menu-genai-chat-label"],
["viewHistorySidebar", "sidebar-menu-history-label"],
["viewTabsSidebar", "sidebar-menu-synced-tabs-label"],
["viewBookmarksSidebar", "sidebar-menu-bookmarks-label"],
["viewCPMSidebar", "sidebar-menu-contextual-password-manager-label"],
]);
const VISIBILITY_SETTING_PREF = "sidebar.visibility";
const EXPAND_ON_HOVER_PREF = "sidebar.expandOnHover";
const POSITION_SETTING_PREF = "sidebar.position_start";
const TAB_DIRECTION_SETTING_PREF = "sidebar.verticalTabs";
export class SidebarCustomize extends SidebarPage {
constructor() {
super();
this.activeExtIndex = 0;
XPCOMUtils.defineLazyPreferenceGetter(
this.#prefValues,
"visibility",
VISIBILITY_SETTING_PREF,
"always-show",
(_aPreference, _previousValue, newValue) => {
this.visibility = newValue;
}
);
XPCOMUtils.defineLazyPreferenceGetter(
this.#prefValues,
"isPositionStart",
POSITION_SETTING_PREF,
true,
(_aPreference, _previousValue, newValue) => {
this.isPositionStart = newValue;
}
);
XPCOMUtils.defineLazyPreferenceGetter(
this.#prefValues,
"verticalTabsEnabled",
TAB_DIRECTION_SETTING_PREF,
false,
(_aPreference, _previousValue, newValue) => {
this.verticalTabsEnabled = newValue;
}
);
XPCOMUtils.defineLazyPreferenceGetter(
this.#prefValues,
"expandOnHoverEnabled",
EXPAND_ON_HOVER_PREF,
false,
(_aPreference, _previousValue, newValue) => {
this.expandOnHoverEnabled = newValue;
}
);
this.visibility = this.#prefValues.visibility;
this.isPositionStart = this.#prefValues.isPositionStart;
this.verticalTabsEnabled = this.#prefValues.verticalTabsEnabled;
this.expandOnHoverEnabled = this.#prefValues.expandOnHoverEnabled;
this.boundObserve = (...args) => this.observe(...args);
}
#prefValues = {};
static properties = {
activeExtIndex: { type: Number },
visibility: { type: String },
isPositionStart: { type: Boolean },
verticalTabsEnabled: { type: Boolean },
expandOnHoverEnabled: { type: Boolean },
};
static queries = {
toolInputs: { all: ".tool" },
extensionLinks: { all: ".extension-link" },
positionInput: "#position",
visibilityInput: "#hide-sidebar",
verticalTabsInput: "#vertical-tabs",
expandOnHoverInput: "#expand-on-hover",
};
connectedCallback() {
super.connectedCallback();
this.getWindow().addEventListener("SidebarItemAdded", this);
this.getWindow().addEventListener("SidebarItemChanged", this);
this.getWindow().addEventListener("SidebarItemRemoved", this);
}
disconnectedCallback() {
super.disconnectedCallback();
this.getWindow().removeEventListener("SidebarItemAdded", this);
this.getWindow().removeEventListener("SidebarItemChanged", this);
this.getWindow().removeEventListener("SidebarItemRemoved", this);
}
get fluentStrings() {
if (!this._fluentStrings) {
this._fluentStrings = new Localization(["browser/sidebar.ftl"], true);
}
return this._fluentStrings;
}
getWindow() {
return window.browsingContext.embedderWindowGlobal.browsingContext.window;
}
handleEvent(e) {
switch (e.type) {
case "SidebarItemAdded":
case "SidebarItemChanged":
case "SidebarItemRemoved":
this.requestUpdate();
break;
}
}
async onToggleToolInput(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 "viewCPMSidebar":
Glean.contextualManager.passwordsEnabled.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();
}
}
toolInputTemplate(tool) {
if (tool.hidden) {
return null;
}
return html`