summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/vcard-edit/adr.mjs
blob: 2f395173f3e6972441fa3f3f2e5f35274c97a18d (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* 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 ADR
 */
export class VCardAdrComponent extends HTMLElement {
  /** @type {VCardPropertyEntry} */
  vCardPropertyEntry;

  static newVCardPropertyEntry() {
    return new lazy.VCardPropertyEntry("adr", {}, "text", [
      "",
      "",
      "",
      "",
      "",
      "",
      "",
    ]);
  }

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

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

    this.streetEl = this.querySelector('textarea[name="street"]');
    this.assignIds(this.streetEl, this.querySelector('label[for="street"]'));
    this.streetEl.addEventListener("input", () => {
      this.resizeStreetEl();
    });

    this.localityEl = this.querySelector('input[name="locality"]');
    this.assignIds(
      this.localityEl,
      this.querySelector('label[for="locality"]')
    );

    this.regionEl = this.querySelector('input[name="region"]');
    this.assignIds(this.regionEl, this.querySelector('label[for="region"]'));

    this.codeEl = this.querySelector('input[name="code"]');
    this.assignIds(this.regionEl, this.querySelector('label[for="code"]'));

    this.countryEl = this.querySelector('input[name="country"]');
    this.assignIds(this.countryEl, this.querySelector('label[for="country"]'));

    // Create the adr type selection.
    this.vCardType = this.querySelector("vcard-type");
    this.vCardType.createTypeSelection(this.vCardPropertyEntry, {
      createLabel: true,
    });

    this.fromVCardPropertyEntryToUI();

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

  fromVCardPropertyEntryToUI() {
    if (Array.isArray(this.vCardPropertyEntry.value[2])) {
      this.streetEl.value = this.vCardPropertyEntry.value[2].join("\n");
    } else {
      this.streetEl.value = this.vCardPropertyEntry.value[2] || "";
    }
    // Per RFC 6350, post office box and extended address SHOULD be empty.
    let pobox = this.vCardPropertyEntry.value[0] || "";
    let extendedAddr = this.vCardPropertyEntry.value[1] || "";
    if (extendedAddr) {
      this.streetEl.value = this.streetEl.value + "\n" + extendedAddr.trim();
      delete this.vCardPropertyEntry.value[1];
    }
    if (pobox) {
      this.streetEl.value = pobox.trim() + "\n" + this.streetEl.value;
      delete this.vCardPropertyEntry.value[0];
    }

    this.resizeStreetEl();
    this.localityEl.value = this.vCardPropertyEntry.value[3] || "";
    this.regionEl.value = this.vCardPropertyEntry.value[4] || "";
    this.codeEl.value = this.vCardPropertyEntry.value[5] || "";
    this.countryEl.value = this.vCardPropertyEntry.value[6] || "";
  }

  fromUIToVCardPropertyEntry() {
    let streetValue = this.streetEl.value || "";
    streetValue = streetValue.trim();
    if (streetValue.includes("\n")) {
      streetValue = streetValue.replaceAll("\r", "");
      streetValue = streetValue.split("\n");
    }

    this.vCardPropertyEntry.value = [
      "",
      "",
      streetValue,
      this.localityEl.value || "",
      this.regionEl.value || "",
      this.codeEl.value || "",
      this.countryEl.value || "",
    ];
  }

  valueIsEmpty() {
    return [
      this.streetEl,
      this.localityEl,
      this.regionEl,
      this.codeEl,
      this.countryEl,
    ].every(e => !e.value);
  }

  assignIds(inputEl, labelEl) {
    let labelInputId = vCardIdGen.next().value;
    inputEl.id = labelInputId;
    labelEl.htmlFor = labelInputId;
  }

  resizeStreetEl() {
    this.streetEl.rows = Math.max(1, this.streetEl.value.split("\n").length);
  }
}

customElements.define("vcard-adr", VCardAdrComponent);