summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/vcard-edit/impp.mjs
blob: 232925942e3732b1b8ca2194804317bdefc90f28 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* 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";

const lazy = {};
ChromeUtils.defineModuleGetter(
  lazy,
  "VCardPropertyEntry",
  "resource:///modules/VCardUtils.jsm"
);

/**
 * @implements {VCardPropertyEntryView}
 * @see RFC6350 IMPP
 */
export class VCardIMPPComponent extends HTMLElement {
  /** @type {VCardPropertyEntry} */
  vCardPropertyEntry;

  /** @type {HTMLInputElement} */
  imppEl;
  /** @type {HTMLSelectElement} */
  protocolEl;

  static newVCardPropertyEntry() {
    return new lazy.VCardPropertyEntry("impp", {}, "uri", "");
  }

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

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

    this.imppEl = this.querySelector('input[name="impp"]');
    document.l10n
      .formatValue("vcard-impp-input-title")
      .then(t => (this.imppEl.title = t));

    this.protocolEl = this.querySelector('select[name="protocol"]');
    this.protocolEl.id = vCardIdGen.next().value;

    let protocolLabel = this.querySelector('label[for="protocol"]');
    protocolLabel.htmlFor = this.protocolEl.id;

    this.protocolEl.addEventListener("change", event => {
      let entered = this.imppEl.value.split(":", 1)[0]?.toLowerCase();
      if (entered) {
        this.protocolEl.value =
          [...this.protocolEl.options].find(o => o.value.startsWith(entered))
            ?.value || "";
      }
      this.imppEl.placeholder = this.protocolEl.value;
      this.imppEl.pattern = this.protocolEl.selectedOptions[0].dataset.pattern;
    });

    this.imppEl.id = vCardIdGen.next().value;
    let imppLabel = this.querySelector('label[for="impp"]');
    imppLabel.htmlFor = this.imppEl.id;
    document.l10n.setAttributes(imppLabel, "vcard-impp-label");
    this.imppEl.addEventListener("change", event => {
      this.protocolEl.dispatchEvent(new CustomEvent("change"));
    });

    this.querySelector(".remove-property-button").addEventListener(
      "click",
      () => {
        this.dispatchEvent(
          new CustomEvent("vcard-remove-property", { bubbles: true })
        );
        this.remove();
      }
    );

    this.fromVCardPropertyEntryToUI();
    this.imppEl.dispatchEvent(new CustomEvent("change"));
  }

  fromVCardPropertyEntryToUI() {
    this.imppEl.value = this.vCardPropertyEntry.value;
  }

  fromUIToVCardPropertyEntry() {
    this.vCardPropertyEntry.value = this.imppEl.value;
  }

  valueIsEmpty() {
    return this.vCardPropertyEntry.value === "";
  }
}

customElements.define("vcard-impp", VCardIMPPComponent);