/* 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 } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; /** * The widget for showing available options when users want to turn on * scheduled backups. */ export default class TurnOnScheduledBackups extends MozLitElement { #placeholderIconURL = "chrome://global/skin/icons/page-portrait.svg"; static properties = { defaultIconURL: { type: String, reflect: true }, defaultLabel: { type: String, reflect: true }, defaultPath: { type: String, reflect: true }, _newIconURL: { type: String }, _newLabel: { type: String }, _newPath: { type: String }, showPasswordOptions: { type: Boolean, reflect: true }, passwordsMatch: { type: Boolean, reflect: true }, }; static get queries() { return { cancelButtonEl: "#backup-turn-on-scheduled-cancel-button", confirmButtonEl: "#backup-turn-on-scheduled-confirm-button", filePathButtonEl: "#backup-location-filepicker-button", filePathInputCustomEl: "#backup-location-filepicker-input-custom", filePathInputDefaultEl: "#backup-location-filepicker-input-default", passwordOptionsCheckboxEl: "#sensitive-data-checkbox-input", passwordOptionsExpandedEl: "#passwords", inputNewPasswordEl: "#new-password-input", inputRepeatPasswordEl: "#repeat-password-input", }; } constructor() { super(); this.defaultIconURL = ""; this.defaultLabel = ""; this.defaultPath = ""; this._newIconURL = ""; this._newLabel = ""; this._newPath = ""; this.showPasswordOptions = false; this.passwordsMatch = false; } /** * Dispatches the BackupUI:InitWidget custom event upon being attached to the * DOM, which registers with BackupUIChild for BackupService state updates. */ connectedCallback() { super.connectedCallback(); this.dispatchEvent( new CustomEvent("BackupUI:InitWidget", { bubbles: true }) ); this.addEventListener("BackupUI:SelectNewFilepickerPath", this); } handleEvent(event) { if (event.type == "BackupUI:SelectNewFilepickerPath") { let { path, filename, iconURL } = event.detail; this._newPath = path; this._newLabel = filename; this._newIconURL = iconURL; } } async handleChooseLocation() { this.dispatchEvent( new CustomEvent("BackupUI:ShowFilepicker", { bubbles: true, detail: { win: window.browsingContext, }, }) ); } handleCancel() { this.dispatchEvent( new CustomEvent("dialogCancel", { bubbles: true, composed: true, }) ); this.resetChanges(); } handleConfirm() { /** * TODO: * Before confirmation, verify passwords match and FxA format rules (bug 1896772). */ let detail = { parentDirPath: this._newPath || this.defaultPath, }; if (this.showPasswordOptions && this.passwordsMatch) { detail.password = this.inputNewPasswordEl.value; } this.dispatchEvent( new CustomEvent("turnOnScheduledBackups", { bubbles: true, composed: true, detail, }) ); this.resetChanges(); } handleTogglePasswordOptions() { this.showPasswordOptions = this.passwordOptionsCheckboxEl?.checked; this.passwordsMatch = false; } handleChangeNewPassword() { this.updatePasswordValidity(); } handleChangeRepeatPassword() { this.updatePasswordValidity(); } updatePasswordValidity() { let isNewPasswordInputValid = this.inputNewPasswordEl?.checkValidity(); let isRepeatPasswordInputValid = this.inputRepeatPasswordEl?.checkValidity(); this.passwordsMatch = isNewPasswordInputValid && isRepeatPasswordInputValid && this.inputNewPasswordEl.value == this.inputRepeatPasswordEl.value; } resetChanges() { this._newPath = ""; this._newIconURL = ""; this._newLabel = ""; this.showPasswordOptions = false; this.passwordOptionsCheckboxEl.checked = false; this.passwordsMatch = false; } defaultFilePathInputTemplate() { let filename = this.defaultLabel; let iconURL = this.defaultIconURL || this.#placeholderIconURL; return html` `; } customFilePathInputTemplate() { let filename = this._newLabel; let iconURL = this._newIconURL || this.#placeholderIconURL; return html` `; } allOptionsTemplate() { return html`
`; } passwordOptionsTemplate() { return html`