/* 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 { MozLitElement } from "chrome://global/content/lit-utils.mjs"; import { html } from "chrome://global/content/vendor/lit.all.mjs"; /** * A custom element that manages the display and editing of a Device name * in Firefox Sync settings section. * * @tagname sync-device-name * @property {string} value - The current value of the device name. * @property {string} defaultValue - Default device name shown in the input field when empty. * @property {boolean} disabled - The disabled state of the device name component. * @property {boolean} _isInEditMode - Whether the component is currently in edit mode. */ class SyncDeviceName extends MozLitElement { static properties = { value: { type: String }, defaultValue: { type: String }, disabled: { type: Boolean }, _isInEditMode: { type: Boolean, state: true }, }; static queries = { inputTextEl: "#fxaSyncComputerName", changeBtnEl: "#fxaChangeDeviceName", }; constructor() { super(); /** @type {string} */ this.value = ""; /** @type {string} */ this.defaultValue = ""; /** @type {boolean} */ this.disabled = false; /** @type {boolean} */ this._isInEditMode = false; } setFocus() { this.updateComplete.then(() => { const targetEl = this._isInEditMode ? this.inputTextEl : this.changeBtnEl; targetEl?.focus(); }); } onDeviceNameChange() { this._isInEditMode = true; this.setFocus(); } onDeviceNameCancel() { this._isInEditMode = false; this.setFocus(); } onDeviceNameSave() { const inputVal = this.inputTextEl.value?.trim(); this.value = inputVal === "" ? this.defaultValue : inputVal; this._isInEditMode = false; this.setFocus(); } /** * Handles key presses in the device name input. * Pressing Enter saves the name, pressing Escape cancels editing. * * @param {KeyboardEvent} event */ onDeviceNameKeyDown(event) { switch (event.key) { case "Enter": event.preventDefault(); this.onDeviceNameSave(); break; case "Escape": event.preventDefault(); this.onDeviceNameCancel(); break; } } displayDeviceNameTemplate() { return html``; } editDeviceNameTemplate() { return html` `; } render() { let label = ""; if (!this._isInEditMode) { label = this.value == "" ? this.defaultValue : this.value; } return html` ${this._isInEditMode ? this.editDeviceNameTemplate() : this.displayDeviceNameTemplate()} `; } } customElements.define("sync-device-name", SyncDeviceName);