summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/content/components/fxaccounts-button.mjs
blob: d39969d726aae1569382810ea44a382ed708cf8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* 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/. */

export default class FxAccountsButton extends HTMLElement {
  connectedCallback() {
    if (this.shadowRoot) {
      return;
    }
    let template = document.querySelector("#fxaccounts-button-template");
    let shadowRoot = this.attachShadow({ mode: "open" });
    document.l10n.connectRoot(shadowRoot);
    shadowRoot.appendChild(template.content.cloneNode(true));

    this._avatarButton = shadowRoot.querySelector(".fxaccounts-avatar-button");
    this._extraText = shadowRoot.querySelector(".fxaccounts-extra-text");
    this._enableButton = shadowRoot.querySelector(".fxaccounts-enable-button");
    this._loggedOutView = shadowRoot.querySelector(".logged-out-view");
    this._loggedInView = shadowRoot.querySelector(".logged-in-view");
    this._emailText = shadowRoot.querySelector(".fxaccount-email");

    this._avatarButton.addEventListener("click", this);
    this._enableButton.addEventListener("click", this);

    this.render();
  }

  handleEvent(event) {
    if (event.currentTarget == this._avatarButton) {
      document.dispatchEvent(
        new CustomEvent("AboutLoginsSyncOptions", {
          bubbles: true,
        })
      );
      return;
    }
    if (event.target == this._enableButton) {
      document.dispatchEvent(
        new CustomEvent("AboutLoginsSyncEnable", {
          bubbles: true,
        })
      );
    }
  }

  render() {
    this._loggedOutView.hidden = !!this._loggedIn;
    this._loggedInView.hidden = !this._loggedIn;
    this._emailText.textContent = this._email;
    if (this._avatarURL) {
      this._avatarButton.style.setProperty(
        "--avatar-url",
        `url(${this._avatarURL})`
      );
    } else {
      let defaultAvatar = "chrome://browser/skin/fxa/avatar-color.svg";
      this._avatarButton.style.setProperty(
        "--avatar-url",
        `url(${defaultAvatar})`
      );
    }
  }

  /**
   *
   * @param {object} state
   *                   loggedIn: {Boolean} FxAccount authentication
   *                             status.
   *                   email: {String} Email address used with FxAccount. Must
   *                          be empty if `loggedIn` is false.
   *                   avatarURL: {String} URL of account avatar. Must
   *                              be empty if `loggedIn` is false.
   */
  updateState(state) {
    this.hidden = !state.fxAccountsEnabled;
    this._loggedIn = state.loggedIn;
    this._email = state.email;
    this._avatarURL = state.avatarURL;

    this.render();
  }
}
customElements.define("fxaccounts-button", FxAccountsButton);