/* 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,
repeat,
styleMap,
when,
} from "chrome://global/content/vendor/lit.all.mjs";
import { MozLitElement } from "chrome://global/content/lit-utils.mjs";
import { escapeRegExp } from "./search-helpers.mjs";
// eslint-disable-next-line import/no-unassigned-import
import "chrome://global/content/elements/moz-button.mjs";
const NOW_THRESHOLD_MS = 91000;
const FXVIEW_ROW_HEIGHT_PX = 32;
const lazy = {};
let XPCOMUtils;
if (!window.IS_STORYBOOK) {
XPCOMUtils = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
).XPCOMUtils;
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"virtualListEnabledPref",
"browser.firefox-view.virtual-list.enabled"
);
ChromeUtils.defineLazyGetter(lazy, "relativeTimeFormat", () => {
return new Services.intl.RelativeTimeFormat(undefined, {
style: "narrow",
});
});
ChromeUtils.defineESModuleGetters(lazy, {
BrowserUtils: "resource://gre/modules/BrowserUtils.sys.mjs",
});
}
/**
* A list of clickable tab items
*
* @property {boolean} compactRows - Whether to hide the URL and date/time for each tab.
* @property {string} dateTimeFormat - Expected format for date and/or time
* @property {string} hasPopup - The aria-haspopup attribute for the secondary action, if required
* @property {number} maxTabsLength - The max number of tabs for the list
* @property {Array} tabItems - Items to show in the tab list
* @property {string} searchQuery - The query string to highlight, if provided.
* @property {string} secondaryActionClass - The class used to style the secondary action element
* @property {string} tertiaryActionClass - The class used to style the tertiary action element
*/
export class FxviewTabListBase extends MozLitElement {
constructor() {
super();
window.MozXULElement.insertFTLIfNeeded("toolkit/branding/brandings.ftl");
window.MozXULElement.insertFTLIfNeeded("browser/fxviewTabList.ftl");
this.activeIndex = 0;
this.currentActiveElementId = "fxview-tab-row-main";
this.hasPopup = null;
this.dateTimeFormat = "relative";
this.maxTabsLength = 25;
this.tabItems = [];
this.compactRows = false;
this.updatesPaused = true;
this.#register();
}
static properties = {
activeIndex: { type: Number },
compactRows: { type: Boolean },
currentActiveElementId: { type: String },
dateTimeFormat: { type: String },
hasPopup: { type: String },
maxTabsLength: { type: Number },
tabItems: { type: Array },
updatesPaused: { type: Boolean },
searchQuery: { type: String },
secondaryActionClass: { type: String },
tertiaryActionClass: { type: String },
};
static queries = {
emptyState: "fxview-empty-state",
rowEls: {
all: "fxview-tab-row",
},
rootVirtualListEl: "virtual-list",
};
willUpdate(changes) {
this.activeIndex = Math.min(
Math.max(this.activeIndex, 0),
this.tabItems.length - 1
);
if (changes.has("dateTimeFormat") || changes.has("updatesPaused")) {
this.clearIntervalTimer();
if (
!this.updatesPaused &&
this.dateTimeFormat == "relative" &&
!window.IS_STORYBOOK
) {
this.startIntervalTimer();
this.onIntervalUpdate();
}
}
if (this.maxTabsLength > 0) {
this.tabItems = this.tabItems.slice(0, this.maxTabsLength);
}
}
startIntervalTimer() {
this.clearIntervalTimer();
this.intervalID = setInterval(
() => this.onIntervalUpdate(),
this.timeMsPref
);
}
clearIntervalTimer() {
if (this.intervalID) {
clearInterval(this.intervalID);
delete this.intervalID;
}
}
#register() {
if (!window.IS_STORYBOOK) {
XPCOMUtils.defineLazyPreferenceGetter(
this,
"timeMsPref",
"browser.tabs.firefox-view.updateTimeMs",
NOW_THRESHOLD_MS,
() => {
this.clearIntervalTimer();
if (!this.isConnected) {
return;
}
this.startIntervalTimer();
this.requestUpdate();
}
);
}
}
connectedCallback() {
super.connectedCallback();
if (
!this.updatesPaused &&
this.dateTimeFormat === "relative" &&
!window.IS_STORYBOOK
) {
this.startIntervalTimer();
}
}
disconnectedCallback() {
super.disconnectedCallback();
this.clearIntervalTimer();
}
async getUpdateComplete() {
await super.getUpdateComplete();
await Promise.all(Array.from(this.rowEls).map(item => item.updateComplete));
}
onIntervalUpdate() {
this.requestUpdate();
Array.from(this.rowEls).forEach(fxviewTabRow =>
fxviewTabRow.requestUpdate()
);
}
/**
* Focuses the expected element (either the link or button) within fxview-tab-row
* The currently focused/active element ID within a row is stored in this.currentActiveElementId
*/
handleFocusElementInRow(e) {
let fxviewTabRow = e.target;
if (e.code == "ArrowUp") {
// Focus either the link or button of the previous row based on this.currentActiveElementId
e.preventDefault();
this.focusPrevRow();
} else if (e.code == "ArrowDown") {
// Focus either the link or button of the next row based on this.currentActiveElementId
e.preventDefault();
this.focusNextRow();
} else if (e.code == "ArrowRight") {
// Focus either the link or the button in the current row and
// set this.currentActiveElementId to that element's ID
e.preventDefault();
if (document.dir == "rtl") {
fxviewTabRow.moveFocusLeft();
} else {
fxviewTabRow.moveFocusRight();
}
} else if (e.code == "ArrowLeft") {
// Focus either the link or the button in the current row and
// set this.currentActiveElementId to that element's ID
e.preventDefault();
if (document.dir == "rtl") {
fxviewTabRow.moveFocusRight();
} else {
fxviewTabRow.moveFocusLeft();
}
}
}
focusPrevRow() {
this.focusIndex(this.activeIndex - 1);
}
focusNextRow() {
this.focusIndex(this.activeIndex + 1);
}
async focusIndex(index) {
// Focus link or button of item
if (lazy.virtualListEnabledPref) {
let row = this.rootVirtualListEl.getItem(index);
if (!row) {
return;
}
let subList = this.rootVirtualListEl.getSubListForItem(index);
if (!subList) {
return;
}
this.activeIndex = index;
// In Bug 1866845, these manual updates to the sublists should be removed
// and scrollIntoView() should also be iterated on so that we aren't constantly
// moving the focused item to the center of the viewport
for (const sublist of Array.from(this.rootVirtualListEl.children)) {
await sublist.requestUpdate();
await sublist.updateComplete;
}
row.scrollIntoView({ block: "center" });
row.focus();
} else if (index >= 0 && index < this.rowEls?.length) {
this.rowEls[index].focus();
this.activeIndex = index;
}
}
shouldUpdate(changes) {
if (changes.has("updatesPaused")) {
if (this.updatesPaused) {
this.clearIntervalTimer();
}
}
return !this.updatesPaused;
}
itemTemplate = (tabItem, i) => {
let time;
if (tabItem.time || tabItem.closedAt) {
let stringTime = (tabItem.time || tabItem.closedAt).toString();
// Different APIs return time in different units, so we use
// the length to decide if it's milliseconds or nanoseconds.
if (stringTime.length === 16) {
time = (tabItem.time || tabItem.closedAt) / 1000;
} else {
time = tabItem.time || tabItem.closedAt;
}
}
return html`
`;
};
stylesheets() {
return html``;
}
render() {
if (this.searchQuery && !this.tabItems.length) {
return this.emptySearchResultsTemplate();
}
return html`
${this.stylesheets()}