summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/vcard-edit/custom.mjs
blob: bcdb1f653192302176912de4f5aab2511344e5f0 (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
/* 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 { vCardIdGen } from "./id-gen.mjs";

export class VCardCustomComponent extends HTMLElement {
  /** @type {VCardPropertyEntry[]} */
  vCardPropertyEntries = null;
  /** @type {HTMLInputElement[]} */
  inputEls = null;

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    let template = document.getElementById("template-vcard-edit-custom");
    let clonedTemplate = template.content.cloneNode(true);
    this.appendChild(clonedTemplate);

    this.inputEls = this.querySelectorAll("input");
    let labelEls = this.querySelectorAll("label");
    for (let i = 0; i < 4; i++) {
      let inputId = vCardIdGen.next().value;
      document.l10n.setAttributes(
        labelEls[i],
        `about-addressbook-entry-name-custom${i + 1}`
      );
      labelEls[i].htmlFor = inputId;
      this.inputEls[i].id = inputId;
    }
    this.fromVCardPropertyEntryToUI();
    this.querySelector(".remove-property-button").addEventListener(
      "click",
      () => {
        document.getElementById("vcard-add-custom").hidden = false;
        this.dispatchEvent(
          new CustomEvent("vcard-remove-property", { bubbles: true })
        );
        this.remove();
      }
    );
  }

  fromVCardPropertyEntryToUI() {
    for (let i = 0; i < 4; i++) {
      this.inputEls[i].value = this.vCardPropertyEntries[i].value;
    }
  }

  fromUIToVCardPropertyEntry() {
    for (let i = 0; i < 4; i++) {
      this.vCardPropertyEntries[i].value = this.inputEls[i].value;
    }
  }
}

customElements.define("vcard-custom", VCardCustomComponent);