/* 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 {
classMap,
html,
ifDefined,
when,
} from "chrome://global/content/vendor/lit.all.mjs";
import {
FxviewTabListBase,
FxviewTabRowBase,
} from "chrome://browser/content/firefoxview/fxview-tab-list.mjs";
export class SidebarTabList extends FxviewTabListBase {
constructor() {
super();
// Panel is open, assume we always want to react to updates.
this.updatesPaused = false;
this.multiSelect = true;
this.inTree = false;
this.level = 1;
}
static queries = {
...FxviewTabListBase.queries,
rowEls: {
all: "sidebar-tab-row",
},
};
static properties = {
inTree: { type: Boolean },
level: { type: Number },
mediumView: { type: Boolean, reflect: true, attribute: "medium-view" },
inactiveWindow: {
type: Boolean,
reflect: true,
attribute: "inactive-window",
},
rowsDraggable: { type: Boolean },
};
/**
* The tree view controller that owns selection state for the page this list
* belongs to.
*
* @returns {SidebarTreeView}
*/
get treeView() {
let host = this.getRootNode()?.host;
while (host) {
if (host.treeView) {
return host.treeView;
}
host = host.getRootNode()?.host;
}
return null;
}
#dispatchFocusRowEvent = event => {
const [row] = event.composedPath();
if (row.localName !== "sidebar-tab-row") {
return;
}
this.dispatchEvent(
new CustomEvent("focus-row", {
bubbles: true,
composed: true,
detail: { guid: row.guid },
})
);
};
connectedCallback() {
super.connectedCallback();
this.addEventListener("focusin", this.#dispatchFocusRowEvent);
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener("focusin", this.#dispatchFocusRowEvent);
}
willUpdate(changedProperties) {
if (changedProperties.has("tabItems") && Array.isArray(this.tabItems)) {
for (const item of this.tabItems) {
item.guid ??= Services.uuid.generateUUID().toString();
}
}
}
handleFocusElementInRow(e) {
if (!this.treeView) {
super.handleFocusElementInRow(e);
return;
}
this.treeView.handleKeydown(e);
if (e.defaultPrevented) {
e.stopPropagation();
}
}
toggleRowSelection(guid) {
this.treeView?.toggleSelection(this, guid);
}
clearSelection() {
this.treeView?.resetSelection();
}
selectAll() {
this.treeView?.selectAllInList(this);
}
itemTemplate = (tabItem, i) => {
const tabIndex = this.treeView?.isActiveNode(this, tabItem.guid) ? 0 : -1;
let time;
if (tabItem.time) {
// Some APIs report the timestamp in microseconds (16 digits); the row
// expects milliseconds.
const stringTime = tabItem.time.toString();
time = stringTime.length === 16 ? tabItem.time / 1000 : tabItem.time;
}
return html`
e.currentTarget.primaryActionHandler(e)}
>
`;
};
isTabItemSelected(tabItem) {
return !!this.treeView?.isSelected(this, tabItem.guid);
}
stylesheets() {
return [
super.stylesheets(),
html``,
];
}
render() {
if (!this.inTree || (this.searchQuery && !this.tabItems.length)) {
return super.render();
}
return html`
${this.stylesheets()}
1 ? "group" : "tree"}
@keydown=${this.handleFocusElementInRow}
>
`;
}
}
customElements.define("sidebar-tab-list", SidebarTabList);
/**
* A sidebar-specific tab row.
*
* Three Boolean states coexist on this row and they each mean something
* different:
* - `active` (inherited from FxviewTabRowBase): the row currently has
* keyboard focus via the parent list's activeIndex.
* - `selected`: the row is selected through the SidebarTreeView (user
* click or multi-select inside the panel).
* - `current`: the row's tabElement is gBrowser.selectedTab. Tracked
* live via a MutationObserver on the tab's [selected]
* attribute.
*/
export class SidebarTabRow extends FxviewTabRowBase {
static properties = {
containerObj: { type: Object },
guid: { type: String, reflect: true, attribute: "data-guid" },
selected: { type: Boolean, reflect: true },
current: { type: Boolean, reflect: true },
indicators: { type: Array },
mediumView: { type: Boolean, reflect: true, attribute: "medium-view" },
inactiveWindow: {
type: Boolean,
reflect: true,
attribute: "inactive-window",
},
};
static queries = {
...FxviewTabRowBase.queries,
domainEl: "#sidebar-tab-row-domain",
timeEl: "#fxview-tab-row-time",
};
#tabSelectObserver = null;
willUpdate(changedProperties) {
super.willUpdate?.(changedProperties);
if (changedProperties.has("tabElement")) {
this.#tabSelectObserver?.disconnect();
this.#tabSelectObserver = null;
if (this.tabElement) {
this.current = this.tabElement.selected;
this.#tabSelectObserver = new MutationObserver(() => {
this.current = this.tabElement?.selected ?? false;
});
this.#tabSelectObserver.observe(this.tabElement, {
attributes: true,
attributeFilter: ["selected"],
});
} else {
this.current = false;
}
}
}
disconnectedCallback() {
super.disconnectedCallback();
this.#tabSelectObserver?.disconnect();
this.#tabSelectObserver = null;
}
get tooltipText() {
if (!this.url) {
return null;
}
if (!this.title || this.title == this.url) {
return this.url;
}
return `${this.title}\n${this.url}`;
}
/**
* Fallback to the native implementation in sidebar. We want to focus the
* entire row instead of delegating it to link or hover buttons.
*/
focus(options) {
HTMLElement.prototype.focus.call(this, options);
}
#getContainerClasses() {
let containerClasses = ["fxview-tab-row-container-indicator", "icon"];
if (this.containerObj) {
let { icon, color } = this.containerObj;
containerClasses.push(`identity-icon-${icon}`);
containerClasses.push(`identity-color-${color}`);
}
return containerClasses;
}
#containerIndicatorTemplate() {
let tabList = this.getRootNode().host;
let tabsToCheck = tabList.tabItems;
return html`${when(
tabsToCheck.some(tab => tab.containerObj),
() => html``
)}`;
}
#getDomain() {
if (!this.url) {
return "";
}
try {
return Services.eTLD.getBaseDomain(Services.io.newURI(this.url));
} catch (e) {
// No base domain (about:, file:, IP hosts, etc.); show a friendly label
// the way Firefox View does.
return this.formatURIForDisplay(this.url);
}
}
#domainTemplate() {
return html`
${this.#getDomain()}
`;
}
secondaryButtonTemplate() {
return html`${when(
this.secondaryL10nId && this.secondaryActionClass,
() =>
html``
)}`;
}
render() {
return html`
${this.stylesheets()}
${when(
this.containerObj,
() => html`
`
)}
${this.faviconTemplate()} ${this.titleTemplate()}
${when(
this.mediumView,
() => html`${this.#domainTemplate()} ${this.timeTemplate()}`
)}
${this.#containerIndicatorTemplate()} ${this.secondaryButtonTemplate()}
`;
}
}
customElements.define("sidebar-tab-row", SidebarTabRow);