/* 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 { MozLitElement } from "chrome://global/content/lit-utils.mjs"; // eslint-disable-next-line import/no-unassigned-import import "chrome://global/content/elements/panel-list.js"; class AutocompleteRowItem extends MozLitElement { static properties = { label: { type: String, fluent: true }, description: { type: String, fluent: true }, value: { type: String }, icon: { type: String }, actions: { type: Object }, }; #openActionsMenu(anchor, actions) { const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; const menupopup = document.createElementNS(XUL_NS, "menupopup"); for (const { label, action } of actions) { const menuitem = document.createElementNS(XUL_NS, "menuitem"); menuitem.setAttribute("label", label); menuitem.addEventListener("command", () => action()); menupopup.appendChild(menuitem); } const panel = this.closest("panel"); panel?.setAttribute("noautohide", "true"); menupopup.addEventListener("popuphiding", () => { panel?.removeAttribute("noautohide"); menupopup.remove(); }); document.documentElement.appendChild(menupopup); menupopup.openPopup(anchor, "after_start"); } getSecondaryActionItemIcon(type) { switch (type) { case "edit": return "chrome://global/skin/icons/edit.svg"; case "menupopup": return "chrome://global/skin/icons/more.svg"; default: return "chrome://global/skin/icons/settings.svg"; } } renderSecondaryActionButton() { const { type, action, actions } = this.actions.secondary; const stopMouseEvents = e => e.stopPropagation(); // We're expecting a single action if (action) { return html` { e.stopPropagation(); action(); }} type="icon ghost" .iconSrc=${this.getSecondaryActionItemIcon(type)} class="secondary-action" >`; } // We're expecting multiple actions for this item if (actions) { return html` { e.stopPropagation(); this.#openActionsMenu(e.currentTarget, actions); }} type="icon ghost" type="icon ghost" .iconSrc=${this.getSecondaryActionItemIcon(type)} class="secondary-action" menuId="secondary-action-menu" >`; } return ""; } render() { return html`
${when( this.icon, () => html`` )}
${this.label} ${when( this.description, () => html`${this.description}` )}
${when(this.actions?.secondary, () => this.renderSecondaryActionButton() )}
`; } } customElements.define("autocomplete-row-item", AutocompleteRowItem);