summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/vcard-edit/n.mjs
blob: ae5d386d934aac4d7cabf79fae8504373752bc04 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 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/. */

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

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

  /** @type {HTMLElement} */
  prefixEl;
  /** @type {HTMLElement} */
  firstNameEl;
  /** @type {HTMLElement} */
  middleNameEl;
  /** @type {HTMLElement} */
  lastNameEl;
  /** @type {HTMLElement} */
  suffixEl;

  constructor() {
    super();
    let template = document.getElementById("template-vcard-edit-n");
    let clonedTemplate = template.content.cloneNode(true);
    this.appendChild(clonedTemplate);
  }

  connectedCallback() {
    if (this.isConnected) {
      this.registerListComponents();
      this.fromVCardPropertyEntryToUI();
      this.sortAsOrder();
    }
  }

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

  /**
   * Assigns the vCardPropertyEntry values to the individual
   * NListComponentText elements.
   *
   * @TODO sort-as param should be used for the order.
   * The use-case is that not every language has the order of
   * prefix, firstName, middleName, lastName, suffix.
   * Aswell that the user is able to change the sorting as he like
   * on a per contact base.
   */
  sortAsOrder() {
    if (!this.vCardPropertyEntry.params["sort-as"]) {
      // eslint-disable-next-line no-useless-return
      return;
    }
    /**
     * @TODO
     * The sort-as DOM Mutation
     */
  }

  fromVCardPropertyEntryToUI() {
    let prefixVal = this.vCardPropertyEntry.value[3] || "";
    let prefixInput = this.prefixEl.querySelector("input");
    prefixInput.value = prefixVal;
    if (prefixVal) {
      this.prefixEl.querySelector("button").hidden = true;
    } else {
      this.prefixEl.classList.add("hasButton");
      this.prefixEl.querySelector("label").hidden = true;
      prefixInput.hidden = true;
    }

    // First Name is always shown.
    this.firstNameEl.querySelector("input").value =
      this.vCardPropertyEntry.value[1] || "";

    let middleNameVal = this.vCardPropertyEntry.value[2] || "";
    let middleNameInput = this.middleNameEl.querySelector("input");
    middleNameInput.value = middleNameVal;
    if (middleNameVal) {
      this.middleNameEl.querySelector("button").hidden = true;
    } else {
      this.middleNameEl.classList.add("hasButton");
      this.middleNameEl.querySelector("label").hidden = true;
      middleNameInput.hidden = true;
    }

    // Last Name is always shown.
    this.lastNameEl.querySelector("input").value =
      this.vCardPropertyEntry.value[0] || "";

    let suffixVal = this.vCardPropertyEntry.value[4] || "";
    let suffixInput = this.suffixEl.querySelector("input");
    suffixInput.value = suffixVal;
    if (suffixVal) {
      this.suffixEl.querySelector("button").hidden = true;
    } else {
      this.suffixEl.classList.add("hasButton");
      this.suffixEl.querySelector("label").hidden = true;
      suffixInput.hidden = true;
    }
  }

  fromUIToVCardPropertyEntry() {
    this.vCardPropertyEntry.value = [
      this.lastNameEl.querySelector("input").value,
      this.firstNameEl.querySelector("input").value,
      this.middleNameEl.querySelector("input").value,
      this.prefixEl.querySelector("input").value,
      this.suffixEl.querySelector("input").value,
    ];
  }

  valueIsEmpty() {
    let noEmptyStrings = [
      this.prefixEl,
      this.firstNameEl,
      this.middleNameEl,
      this.lastNameEl,
      this.suffixEl,
    ].filter(node => {
      return node.querySelector("input").value !== "";
    });
    return noEmptyStrings.length === 0;
  }

  registerListComponents() {
    this.prefixEl = this.querySelector("#n-list-component-prefix");
    let prefixInput = this.prefixEl.querySelector("input");
    let prefixButton = this.prefixEl.querySelector("button");
    prefixButton.addEventListener("click", e => {
      this.prefixEl.querySelector("label").hidden = false;
      prefixInput.hidden = false;
      prefixButton.hidden = true;
      this.prefixEl.classList.remove("hasButton");
      prefixInput.focus();
    });

    this.firstNameEl = this.querySelector("#n-list-component-firstname");

    this.middleNameEl = this.querySelector("#n-list-component-middlename");
    let middleNameInput = this.middleNameEl.querySelector("input");
    let middleNameButton = this.middleNameEl.querySelector("button");
    middleNameButton.addEventListener("click", e => {
      this.middleNameEl.querySelector("label").hidden = false;
      middleNameInput.hidden = false;
      middleNameButton.hidden = true;
      this.middleNameEl.classList.remove("hasButton");
      middleNameInput.focus();
    });

    this.lastNameEl = this.querySelector("#n-list-component-lastname");

    this.suffixEl = this.querySelector("#n-list-component-suffix");
    let suffixInput = this.suffixEl.querySelector("input");
    let suffixButton = this.suffixEl.querySelector("button");
    suffixButton.addEventListener("click", e => {
      this.suffixEl.querySelector("label").hidden = false;
      suffixInput.hidden = false;
      suffixButton.hidden = true;
      this.suffixEl.classList.remove("hasButton");
      suffixInput.focus();
    });
  }

  disconnectedCallback() {
    if (!this.isConnected) {
      this.prefixEl = null;
      this.firstNameEl = null;
      this.middleNameEl = null;
      this.lastNameEl = null;
      this.suffixEl = null;
    }
  }
}
customElements.define("vcard-n", VCardNComponent);