|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { |
| 6 | + html, |
| 7 | + ifDefined, |
| 8 | + styleMap, |
| 9 | +} from "chrome://global/content/vendor/lit.all.mjs"; |
| 10 | +import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; |
| 11 | + |
| 12 | +// eslint-disable-next-line import/no-unassigned-import |
| 13 | +import "chrome://global/content/elements/moz-button.mjs"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Vertical strip attached to the launcher that provides an entry point |
| 17 | + * to various sidebar panels. |
| 18 | + * |
| 19 | + */ |
| 20 | +export default class SidebarLauncher extends MozLitElement { |
| 21 | + static properties = { |
| 22 | + topActions: { type: Array }, |
| 23 | + bottomActions: { type: Array }, |
| 24 | + selectedView: { type: String }, |
| 25 | + open: { type: Boolean }, |
| 26 | + }; |
| 27 | + |
| 28 | + constructor() { |
| 29 | + super(); |
| 30 | + this.topActions = [ |
| 31 | + { |
| 32 | + icon: `url(chrome://browser/skin/insights.svg)`, |
| 33 | + view: null, |
| 34 | + l10nId: "sidebar-launcher-insights", |
| 35 | + }, |
| 36 | + ]; |
| 37 | + |
| 38 | + this.bottomActions = [ |
| 39 | + { |
| 40 | + l10nId: "sidebar-menu-history", |
| 41 | + icon: `url("chrome://browser/content/firefoxview/view-history.svg")`, |
| 42 | + view: "viewHistorySidebar", |
| 43 | + }, |
| 44 | + { |
| 45 | + l10nId: "sidebar-menu-bookmarks", |
| 46 | + icon: `url("chrome://browser/skin/bookmark-hollow.svg")`, |
| 47 | + view: "viewBookmarksSidebar", |
| 48 | + }, |
| 49 | + { |
| 50 | + l10nId: "sidebar-menu-synced-tabs", |
| 51 | + icon: `url("chrome://browser/skin/device-phone.svg")`, |
| 52 | + view: "viewTabsSidebar", |
| 53 | + }, |
| 54 | + ]; |
| 55 | + |
| 56 | + this.selectedView = window.SidebarUI.currentID; |
| 57 | + this.open = window.SidebarUI.isOpen; |
| 58 | + this.menuMutationObserver = new MutationObserver(() => |
| 59 | + this.#setExtensionItems() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + connectedCallback() { |
| 64 | + super.connectedCallback(); |
| 65 | + this._sidebarBox = document.getElementById("sidebar-box"); |
| 66 | + this._sidebarBox.addEventListener("sidebar-show", this); |
| 67 | + this._sidebarBox.addEventListener("sidebar-hide", this); |
| 68 | + this._sidebarMenu = document.getElementById("viewSidebarMenu"); |
| 69 | + |
| 70 | + this.menuMutationObserver.observe(this._sidebarMenu, { |
| 71 | + childList: true, |
| 72 | + subtree: true, |
| 73 | + }); |
| 74 | + this.#setExtensionItems(); |
| 75 | + } |
| 76 | + |
| 77 | + disconnectedCallback() { |
| 78 | + super.disconnectedCallback(); |
| 79 | + this._sidebarBox.removeEventListener("sidebar-show", this); |
| 80 | + this._sidebarBox.removeEventListener("sidebar-hide", this); |
| 81 | + this.menuMutationObserver.disconnect(); |
| 82 | + } |
| 83 | + |
| 84 | + getImageUrl(icon, targetURI) { |
| 85 | + if (window.IS_STORYBOOK) { |
| 86 | + return `chrome://global/skin/icons/defaultFavicon.svg`; |
| 87 | + } |
| 88 | + if (!icon) { |
| 89 | + if (targetURI?.startsWith("moz-extension")) { |
| 90 | + return "chrome://mozapps/skin/extensions/extension.svg"; |
| 91 | + } |
| 92 | + return `chrome://global/skin/icons/defaultFavicon.svg`; |
| 93 | + } |
| 94 | + // If the icon is not for website (doesn't begin with http), we |
| 95 | + // display it directly. Otherwise we go through the page-icon |
| 96 | + // protocol to try to get a cached version. We don't load |
| 97 | + // favicons directly. |
| 98 | + if (icon.startsWith("http")) { |
| 99 | + return `page-icon:${targetURI}`; |
| 100 | + } |
| 101 | + return icon; |
| 102 | + } |
| 103 | + |
| 104 | + #setExtensionItems() { |
| 105 | + for (let item of this._sidebarMenu.children) { |
| 106 | + if (item.id.endsWith("-sidebar-action")) { |
| 107 | + this.topActions.push({ |
| 108 | + tooltiptext: item.label, |
| 109 | + icon: item.style.getPropertyValue("--webextension-menuitem-image"), |
| 110 | + view: item.id.slice("menubar_menu_".length), |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + handleEvent(e) { |
| 117 | + switch (e.type) { |
| 118 | + case "sidebar-show": |
| 119 | + this.selectedView = e.detail.viewId; |
| 120 | + this.open = true; |
| 121 | + break; |
| 122 | + case "sidebar-hide": |
| 123 | + this.open = false; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + showView(e) { |
| 129 | + let view = e.target.getAttribute("view"); |
| 130 | + window.SidebarUI.toggle(view); |
| 131 | + } |
| 132 | + |
| 133 | + buttonType(action) { |
| 134 | + return this.open && action.view == this.selectedView |
| 135 | + ? "icon" |
| 136 | + : "icon ghost"; |
| 137 | + } |
| 138 | + |
| 139 | + entrypointTemplate(action) { |
| 140 | + return html`<moz-button |
| 141 | + class="icon-button" |
| 142 | + type=${this.buttonType(action)} |
| 143 | + view=${action.view} |
| 144 | + @click=${action.view ? this.showView : null} |
| 145 | + title=${ifDefined(action.tooltiptext)} |
| 146 | + data-l10n-id=${ifDefined(action.l10nId)} |
| 147 | + style=${styleMap({ "--action-icon": action.icon })} |
| 148 | + > |
| 149 | + </moz-button>`; |
| 150 | + } |
| 151 | + |
| 152 | + render() { |
| 153 | + return html` |
| 154 | + <link |
| 155 | + rel="stylesheet" |
| 156 | + href="chrome://browser/content/sidebar/sidebar-launcher.css" |
| 157 | + /> |
| 158 | + <div class="wrapper"> |
| 159 | + <div class="top-actions actions-list"> |
| 160 | + ${this.topActions.map(action => this.entrypointTemplate(action))} |
| 161 | + </div> |
| 162 | + <div class="bottom-actions actions-list"> |
| 163 | + ${this.bottomActions.map(action => this.entrypointTemplate(action))} |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + `; |
| 167 | + } |
| 168 | +} |
| 169 | +customElements.define("sidebar-launcher", SidebarLauncher); |
0 commit comments