summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/vcard-edit/edit.mjs
blob: 90463e33bb8a66f1b2f67a43c7212aa3e4840843 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
/* 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";
import { VCardAdrComponent } from "./adr.mjs";
import { VCardCustomComponent } from "./custom.mjs";
import { VCardEmailComponent } from "./email.mjs";
import { VCardIMPPComponent } from "./impp.mjs";
import { VCardNComponent } from "./n.mjs";
import { VCardFNComponent } from "./fn.mjs";
import { VCardNickNameComponent } from "./nickname.mjs";
import { VCardNoteComponent } from "./note.mjs";
import {
  VCardOrgComponent,
  VCardRoleComponent,
  VCardTitleComponent,
} from "./org.mjs";
import { VCardSpecialDateComponent } from "./special-date.mjs";
import { VCardTelComponent } from "./tel.mjs";
import { VCardTZComponent } from "./tz.mjs";
import { VCardURLComponent } from "./url.mjs";

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

class VCardEdit extends HTMLElement {
  constructor() {
    super();

    this.contactNameHeading = document.getElementById("editContactHeadingName");
    this.contactNickNameHeading = document.getElementById(
      "editContactHeadingNickName"
    );
    this.contactEmailHeading = document.getElementById(
      "editContactHeadingEmail"
    );
  }

  connectedCallback() {
    if (this.isConnected) {
      this.updateView();

      this.addEventListener("vcard-remove-property", e => {
        if (e.target.vCardPropertyEntries) {
          for (let entry of e.target.vCardPropertyEntries) {
            this.vCardProperties.removeEntry(entry);
          }
        } else {
          this.vCardProperties.removeEntry(e.target.vCardPropertyEntry);
        }

        // Move the focus to the first available valid element of the fieldset.
        let sibling =
          e.target.nextElementSibling || e.target.previousElementSibling;
        // If we got a button, focus it since it's the "add row" button.
        if (sibling?.type == "button") {
          sibling.focus();
          return;
        }

        // Otherwise we have a row field, so try to find a focusable element.
        if (sibling && this.moveFocusIntoElement(sibling)) {
          return;
        }

        // If we reach this point, the markup was unpredictable and we should
        // move the focus to a valid element to avoid focus lost.
        e.target
          .closest("fieldset")
          .querySelector(".add-property-button")
          .focus();
      });
    }
  }

  disconnectedCallback() {
    this.replaceChildren();
  }

  get vCardString() {
    return this._vCardProperties.toVCard();
  }

  set vCardString(value) {
    if (value) {
      try {
        this.vCardProperties = lazy.VCardProperties.fromVCard(value);
        return;
      } catch (ex) {
        console.error(ex);
      }
    }
    this.vCardProperties = new lazy.VCardProperties("4.0");
  }

  get vCardProperties() {
    return this._vCardProperties;
  }

  set vCardProperties(value) {
    this._vCardProperties = value;
    // If no n property is present set one.
    if (!this._vCardProperties.getFirstEntry("n")) {
      this._vCardProperties.addEntry(VCardNComponent.newVCardPropertyEntry());
    }
    // If no fn property is present set one.
    if (!this._vCardProperties.getFirstEntry("fn")) {
      this._vCardProperties.addEntry(VCardFNComponent.newVCardPropertyEntry());
    }
    // If no nickname property is present set one.
    if (!this._vCardProperties.getFirstEntry("nickname")) {
      this._vCardProperties.addEntry(
        VCardNickNameComponent.newVCardPropertyEntry()
      );
    }
    // If no email property is present set one.
    if (!this._vCardProperties.getFirstEntry("email")) {
      let emailEntry = VCardEmailComponent.newVCardPropertyEntry();
      emailEntry.params.pref = "1"; // Set as default email.
      this._vCardProperties.addEntry(emailEntry);
    }
    // If one of the organizational properties is present,
    // make sure they all are.
    let title = this._vCardProperties.getFirstEntry("title");
    let role = this._vCardProperties.getFirstEntry("role");
    let org = this._vCardProperties.getFirstEntry("org");
    if (title || role || org) {
      if (!title) {
        this._vCardProperties.addEntry(
          VCardTitleComponent.newVCardPropertyEntry()
        );
      }
      if (!role) {
        this._vCardProperties.addEntry(
          VCardRoleComponent.newVCardPropertyEntry()
        );
      }
      if (!org) {
        this._vCardProperties.addEntry(
          VCardOrgComponent.newVCardPropertyEntry()
        );
      }
    }

    for (let i = 1; i <= 4; i++) {
      if (!this._vCardProperties.getFirstEntry(`x-custom${i}`)) {
        this._vCardProperties.addEntry(
          new lazy.VCardPropertyEntry(`x-custom${i}`, {}, "text", "")
        );
      }
    }

    this.updateView();
  }

  updateView() {
    // Create new DOM and replacing other vCardProperties.
    let template = document.getElementById("template-addr-book-edit");
    let clonedTemplate = template.content.cloneNode(true);
    // Making the next two calls in one go causes a console error to be logged.
    this.replaceChildren();
    this.append(clonedTemplate);

    if (!this.vCardProperties) {
      return;
    }

    this.addFieldsetActions();

    // Insert the vCard property entries.
    for (let vCardPropertyEntry of this.vCardProperties.entries) {
      this.insertVCardElement(vCardPropertyEntry, false);
    }

    let customProperties = ["x-custom1", "x-custom2", "x-custom3", "x-custom4"];
    if (customProperties.some(key => this.vCardProperties.getFirstValue(key))) {
      // If one of these properties has a value, display all of them.
      let customFieldset = this.querySelector("#addr-book-edit-custom");
      let customEl =
        customFieldset.querySelector("vcard-custom") ||
        new VCardCustomComponent();
      customEl.vCardPropertyEntries = customProperties.map(key =>
        this._vCardProperties.getFirstEntry(key)
      );
      let addCustom = document.getElementById("vcard-add-custom");
      customFieldset.insertBefore(customEl, addCustom);
      addCustom.hidden = true;
    }

    let nameEl = this.querySelector("vcard-n");
    this.firstName = nameEl.firstNameEl.querySelector("input");
    this.lastName = nameEl.lastNameEl.querySelector("input");
    this.prefixName = nameEl.prefixEl.querySelector("input");
    this.middleName = nameEl.middleNameEl.querySelector("input");
    this.suffixName = nameEl.suffixEl.querySelector("input");
    this.displayName = this.querySelector("vcard-fn").displayEl;

    [
      this.firstName,
      this.lastName,
      this.prefixName,
      this.middleName,
      this.suffixName,
      this.displayName,
    ].forEach(element => {
      element.addEventListener("input", event =>
        this.generateContactName(event)
      );
    });

    // Only set the strings and define this selector if we're inside the
    // address book edit panel.
    if (document.getElementById("detailsPane")) {
      this.preferDisplayName = this.querySelector("vcard-fn").preferDisplayEl;
      document.l10n.setAttributes(
        this.preferDisplayName.closest(".vcard-checkbox").querySelector("span"),
        "about-addressbook-prefer-display-name"
      );
    }

    this.nickName = this.querySelector("vcard-nickname").nickNameEl;
    this.nickName.addEventListener("input", () => this.updateNickName());

    if (this.vCardProperties) {
      this.toggleDefaultEmailView();
      this.checkForBdayOccurrences();
    }

    this.updateNickName();
    this.updateEmailHeading();
    this.generateContactName();
  }

  /**
   * Update the contact name to reflect the users' choice.
   *
   * @param {?Event} event - The DOM event if we have one.
   */
  async generateContactName(event = null) {
    // Don't generate any preview if the contact name element is not available,
    // which it might happen since this component is used in other areas outside
    // the address book UI.
    if (!this.contactNameHeading) {
      return;
    }

    let bundle = Services.strings.createBundle(
      "chrome://messenger/locale/addressbook/addressBook.properties"
    );
    let result = "";
    let pref = Services.prefs.getIntPref("mail.addr_book.lastnamefirst");
    switch (pref) {
      case Ci.nsIAbCard.GENERATE_DISPLAY_NAME:
        result = this.buildDefaultName();
        break;

      case Ci.nsIAbCard.GENERATE_LAST_FIRST_ORDER:
        if (this.lastName.value) {
          result = bundle.formatStringFromName("lastFirstFormat", [
            this.lastName.value,
            [
              this.prefixName.value,
              this.firstName.value,
              this.middleName.value,
              this.suffixName.value,
            ]
              .filter(Boolean)
              .join(" "),
          ]);
        } else {
          // Get the generic name if we don't have a last name.
          result = this.buildDefaultName();
        }
        break;

      default:
        result = bundle.formatStringFromName("firstLastFormat", [
          [this.prefixName.value, this.firstName.value, this.middleName.value]
            .filter(Boolean)
            .join(" "),
          [this.lastName.value, this.suffixName.value]
            .filter(Boolean)
            .join(" "),
        ]);
        break;
    }

    if (result == "" || result == ", ") {
      // We don't have anything to show as a contact name, so let's find the
      // default email and show that, if we have it, otherwise pass an empty
      // string to remove any leftover data.
      let email = this.getDefaultEmail();
      result = email ? email.split("@", 1)[0] : "";
    }

    this.contactNameHeading.textContent = result;
    this.fillDisplayName(event);
  }

  /**
   * Returns the name to show for this contact if the display name is available
   * or it generates one from the available N data.
   *
   * @returns {string} - The name to show for this contact.
   */
  buildDefaultName() {
    return this.displayName.isDirty
      ? this.displayName.value
      : [
          this.prefixName.value,
          this.firstName.value,
          this.middleName.value,
          this.lastName.value,
          this.suffixName.value,
        ]
          .filter(Boolean)
          .join(" ");
  }

  /**
   * Update the nickname value of the contact header.
   */
  updateNickName() {
    // Don't generate any preview if the contact nickname element is not
    // available, which it might happen since this component is used in other
    // areas outside the address book UI.
    if (!this.contactNickNameHeading) {
      return;
    }

    let value = this.nickName.value.trim();
    this.contactNickNameHeading.hidden = !value;
    this.contactNickNameHeading.textContent = value;
  }

  /**
   * Update the email value of the contact header.
   *
   * @param {?string} email - The email value the user is currently typing.
   */
  updateEmailHeading(email = null) {
    // Don't generate any preview if the contact nickname email is not
    // available, which it might happen since this component is used in other
    // areas outside the address book UI.
    if (!this.contactEmailHeading) {
      return;
    }

    // If no email string was passed, it means this method was called when the
    // view or edit pane refreshes, therefore we need to fetch the correct
    // default email address.
    let value = email ?? this.getDefaultEmail();
    this.contactEmailHeading.hidden = !value;
    this.contactEmailHeading.textContent = value;
  }

  /**
   * Find the default email used for this contact.
   *
   * @returns {VCardEmailComponent}
   */
  getDefaultEmail() {
    let emails = document.getElementById("vcard-email").children;
    if (emails.length == 1) {
      return emails[0].emailEl.value;
    }

    let defaultEmail = [...emails].find(
      el => el.vCardPropertyEntry.params.pref === "1"
    );

    // If no email is marked as preferred, use the first one.
    if (!defaultEmail) {
      defaultEmail = emails[0];
    }

    return defaultEmail.emailEl.value;
  }

  /**
   * Auto fill the display name only if the pref is set, the user is not
   * editing the display name field, and the field was never edited.
   * The intention is to prefill while entering a new contact. Don't fill
   * if we don't have a proper default name to show, but only a placeholder.
   *
   * @param {?Event} event - The DOM event if we have one.
   */
  fillDisplayName(event = null) {
    if (
      Services.prefs.getBoolPref("mail.addr_book.displayName.autoGeneration") &&
      event?.originalTarget.id != "vCardDisplayName" &&
      !this.displayName.isDirty &&
      this.buildDefaultName()
    ) {
      this.displayName.value = this.contactNameHeading.textContent;
    }
  }

  /**
   * Inserts a custom element for a {VCardPropertyEntry}
   *
   *  - Assigns rich data (not bind to a html attribute) and therefore
   *    the reference.
   *  - Inserts the element in the form at the correct position.
   *
   * @param {VCardPropertyEntry} entry
   * @param {boolean} addEntry Adds the entry to the vCardProperties.
   * @returns {VCardPropertyEntryView | undefined}
   */
  insertVCardElement(entry, addEntry) {
    // Add the entry to the vCardProperty data.
    if (addEntry) {
      this.vCardProperties.addEntry(entry);
    }

    let fieldset;
    let addButton;
    switch (entry.name) {
      case "n":
        let n = new VCardNComponent();
        n.vCardPropertyEntry = entry;
        fieldset = document.getElementById("addr-book-edit-n");
        let displayNicknameContainer = this.querySelector(
          "#addr-book-edit-n .addr-book-edit-display-nickname"
        );
        fieldset.insertBefore(n, displayNicknameContainer);
        return n;
      case "fn":
        let fn = new VCardFNComponent();
        fn.vCardPropertyEntry = entry;
        fieldset = this.querySelector(
          "#addr-book-edit-n .addr-book-edit-display-nickname"
        );
        fieldset.insertBefore(fn, fieldset.firstElementChild);
        return fn;
      case "nickname":
        let nickname = new VCardNickNameComponent();
        nickname.vCardPropertyEntry = entry;
        fieldset = this.querySelector(
          "#addr-book-edit-n .addr-book-edit-display-nickname"
        );
        fieldset.insertBefore(
          nickname,
          fieldset.firstElementChild?.nextElementSibling
        );
        return nickname;
      case "email":
        let email = document.createElement("tr", { is: "vcard-email" });
        email.vCardPropertyEntry = entry;
        document.getElementById("vcard-email").appendChild(email);
        return email;
      case "url":
        let url = new VCardURLComponent();
        url.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-url");
        addButton = document.getElementById("vcard-add-url");
        fieldset.insertBefore(url, addButton);
        return url;
      case "tel":
        let tel = new VCardTelComponent();
        tel.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-tel");
        addButton = document.getElementById("vcard-add-tel");
        fieldset.insertBefore(tel, addButton);
        return tel;
      case "tz":
        let tz = new VCardTZComponent();
        tz.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-tz");
        addButton = document.getElementById("vcard-add-tz");
        fieldset.insertBefore(tz, addButton);
        addButton.hidden = true;
        return tz;
      case "impp":
        let impp = new VCardIMPPComponent();
        impp.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-impp");
        addButton = document.getElementById("vcard-add-impp");
        fieldset.insertBefore(impp, addButton);
        return impp;
      case "anniversary":
        let anniversary = new VCardSpecialDateComponent();
        anniversary.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-bday-anniversary");
        addButton = document.getElementById("vcard-add-bday-anniversary");
        fieldset.insertBefore(anniversary, addButton);
        return anniversary;
      case "bday":
        let bday = new VCardSpecialDateComponent();
        bday.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-bday-anniversary");
        addButton = document.getElementById("vcard-add-bday-anniversary");
        fieldset.insertBefore(bday, addButton);
        return bday;
      case "adr":
        let address = new VCardAdrComponent();
        address.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-address");
        addButton = document.getElementById("vcard-add-adr");
        fieldset.insertBefore(address, addButton);
        return address;
      case "note":
        let note = new VCardNoteComponent();
        note.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-note");
        addButton = document.getElementById("vcard-add-note");
        fieldset.insertBefore(note, addButton);
        // Only one note is allowed via UI.
        addButton.hidden = true;
        return note;
      case "title":
        let title = new VCardTitleComponent();
        title.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-org");
        addButton = document.getElementById("vcard-add-org");
        fieldset.insertBefore(
          title,
          fieldset.querySelector("vcard-role, vcard-org, #vcard-add-org")
        );
        this.querySelector(
          "#addr-book-edit-org .remove-property-button"
        ).hidden = false;
        // Only one title is allowed via UI.
        addButton.hidden = true;
        return title;
      case "role":
        let role = new VCardRoleComponent();
        role.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-org");
        addButton = document.getElementById("vcard-add-org");
        fieldset.insertBefore(
          role,
          fieldset.querySelector("vcard-org, #vcard-add-org")
        );
        this.querySelector(
          "#addr-book-edit-org .remove-property-button"
        ).hidden = false;
        // Only one role is allowed via UI.
        addButton.hidden = true;
        return role;
      case "org":
        let org = new VCardOrgComponent();
        org.vCardPropertyEntry = entry;
        fieldset = this.querySelector("#addr-book-edit-org");
        addButton = document.getElementById("vcard-add-org");
        fieldset.insertBefore(org, addButton);
        this.querySelector(
          "#addr-book-edit-org .remove-property-button"
        ).hidden = false;
        // Only one org is allowed via UI.
        addButton.hidden = true;
        return org;
      default:
        return undefined;
    }
  }

  /**
   * Creates a VCardPropertyEntry with a matching
   * name to the vCard spec.
   *
   * @param {string} entryName - A name which should be a vCard spec property.
   * @returns {VCardPropertyEntry | undefined}
   */
  static createVCardProperty(entryName) {
    switch (entryName) {
      case "n":
        return VCardNComponent.newVCardPropertyEntry();
      case "fn":
        return VCardFNComponent.newVCardPropertyEntry();
      case "nickname":
        return VCardNickNameComponent.newVCardPropertyEntry();
      case "email":
        return VCardEmailComponent.newVCardPropertyEntry();
      case "url":
        return VCardURLComponent.newVCardPropertyEntry();
      case "tel":
        return VCardTelComponent.newVCardPropertyEntry();
      case "tz":
        return VCardTZComponent.newVCardPropertyEntry();
      case "impp":
        return VCardIMPPComponent.newVCardPropertyEntry();
      case "bday":
        return VCardSpecialDateComponent.newBdayVCardPropertyEntry();
      case "anniversary":
        return VCardSpecialDateComponent.newAnniversaryVCardPropertyEntry();
      case "adr":
        return VCardAdrComponent.newVCardPropertyEntry();
      case "note":
        return VCardNoteComponent.newVCardPropertyEntry();
      case "title":
        return VCardTitleComponent.newVCardPropertyEntry();
      case "role":
        return VCardRoleComponent.newVCardPropertyEntry();
      case "org":
        return VCardOrgComponent.newVCardPropertyEntry();
      default:
        return undefined;
    }
  }

  /**
   * Mutates the referenced vCardPropertyEntry(s).
   * If the value of a VCardPropertyEntry is empty, the entry gets
   * removed from the vCardProperty.
   */
  saveVCard() {
    for (let node of [
      ...this.querySelectorAll("vcard-adr"),
      ...this.querySelectorAll("vcard-custom"),
      ...document.getElementById("vcard-email").children,
      ...this.querySelectorAll("vcard-fn"),
      ...this.querySelectorAll("vcard-impp"),
      ...this.querySelectorAll("vcard-n"),
      ...this.querySelectorAll("vcard-nickname"),
      ...this.querySelectorAll("vcard-note"),
      ...this.querySelectorAll("vcard-org"),
      ...this.querySelectorAll("vcard-role"),
      ...this.querySelectorAll("vcard-title"),
      ...this.querySelectorAll("vcard-special-date"),
      ...this.querySelectorAll("vcard-tel"),
      ...this.querySelectorAll("vcard-tz"),
      ...this.querySelectorAll("vcard-url"),
    ]) {
      if (typeof node.fromUIToVCardPropertyEntry === "function") {
        node.fromUIToVCardPropertyEntry();
      }

      // Filter out empty fields.
      if (typeof node.valueIsEmpty === "function" && node.valueIsEmpty()) {
        this.vCardProperties.removeEntry(node.vCardPropertyEntry);
      }
    }

    // If no email has a pref value of 1, set it to the first email.
    let emailEntries = this.vCardProperties.getAllEntries("email");
    if (
      emailEntries.length >= 1 &&
      emailEntries.every(entry => entry.params.pref !== "1")
    ) {
      emailEntries[0].params.pref = "1";
    }

    for (let i = 1; i <= 4; i++) {
      let entry = this._vCardProperties.getFirstEntry(`x-custom${i}`);
      if (entry && !entry.value) {
        this._vCardProperties.removeEntry(entry);
      }
    }
  }

  /**
   * Move focus into the form.
   */
  setFocus() {
    this.querySelector("vcard-n input:not([hidden])").focus();
  }

  /**
   * Move focus to the first visible form element below the given element.
   *
   * @param {Element} element - The element to move focus into.
   * @returns {boolean} - If the focus was moved into the element.
   */
  moveFocusIntoElement(element) {
    for (let child of element.querySelectorAll(
      "select,input,textarea,button"
    )) {
      // Make sure it is visible.
      if (child.clientWidth != 0 && child.clientHeight != 0) {
        child.focus();
        return true;
      }
    }
    return false;
  }

  /**
   * Add buttons and further actions of the groupings for vCard property
   * entries.
   */
  addFieldsetActions() {
    // Add email button.
    let addEmail = document.getElementById("vcard-add-email");
    this.registerAddButton(addEmail, "email", () => {
      this.toggleDefaultEmailView();
    });

    // Add listener to update the email written in the contact header.
    this.addEventListener("vcard-email-default-changed", event => {
      this.updateEmailHeading(
        event.target.querySelector('input[type="email"]').value
      );
    });

    // Add listener to be sure that only one checkbox from the emails is ticked.
    this.addEventListener("vcard-email-default-checkbox", event => {
      // Show the newly selected default email in the contact header.
      this.updateEmailHeading(
        event.target.querySelector('input[type="email"]').value
      );
      for (let vCardEmailComponent of document.getElementById("vcard-email")
        .children) {
        if (event.target !== vCardEmailComponent) {
          vCardEmailComponent.checkboxEl.checked = false;
        }
      }
    });

    // Handling the VCardPropertyEntry change with the select.
    let specialDatesFieldset = document.getElementById(
      "addr-book-edit-bday-anniversary"
    );
    specialDatesFieldset.addEventListener(
      "vcard-bday-anniversary-change",
      event => {
        let newVCardPropertyEntry = new lazy.VCardPropertyEntry(
          event.detail.name,
          event.target.vCardPropertyEntry.params,
          event.target.vCardPropertyEntry.type,
          event.target.vCardPropertyEntry.value
        );
        this.vCardProperties.removeEntry(event.target.vCardPropertyEntry);
        event.target.vCardPropertyEntry = newVCardPropertyEntry;
        this.vCardProperties.addEntry(newVCardPropertyEntry);
        this.checkForBdayOccurrences();
      }
    );

    // Add special date button.
    let addSpecialDate = document.getElementById("vcard-add-bday-anniversary");
    addSpecialDate.addEventListener("click", e => {
      let newVCardProperty;
      if (!this.vCardProperties.getFirstEntry("bday")) {
        newVCardProperty = VCardEdit.createVCardProperty("bday");
      } else {
        newVCardProperty = VCardEdit.createVCardProperty("anniversary");
      }
      let el = this.insertVCardElement(newVCardProperty, true);
      this.checkForBdayOccurrences();
      this.moveFocusIntoElement(el);
    });

    // Organizational Properties.
    let addOrg = document.getElementById("vcard-add-org");
    addOrg.addEventListener("click", event => {
      let title = VCardEdit.createVCardProperty("title");
      let role = VCardEdit.createVCardProperty("role");
      let org = VCardEdit.createVCardProperty("org");

      let titleEl = this.insertVCardElement(title, true);
      this.insertVCardElement(role, true);
      this.insertVCardElement(org, true);

      this.moveFocusIntoElement(titleEl);
      addOrg.hidden = true;
    });

    let addAddress = document.getElementById("vcard-add-adr");
    this.registerAddButton(addAddress, "adr");

    let addURL = document.getElementById("vcard-add-url");
    this.registerAddButton(addURL, "url");

    let addTel = document.getElementById("vcard-add-tel");
    this.registerAddButton(addTel, "tel");

    let addTZ = document.getElementById("vcard-add-tz");
    this.registerAddButton(addTZ, "tz", () => {
      addTZ.hidden = true;
    });

    let addIMPP = document.getElementById("vcard-add-impp");
    this.registerAddButton(addIMPP, "impp");

    let addNote = document.getElementById("vcard-add-note");
    this.registerAddButton(addNote, "note", () => {
      addNote.hidden = true;
    });

    let addCustom = document.getElementById("vcard-add-custom");
    addCustom.addEventListener("click", event => {
      let el = new VCardCustomComponent();

      // When the custom properties are deleted and added again ensure that
      // the properties are set.
      for (let i = 1; i <= 4; i++) {
        if (!this._vCardProperties.getFirstEntry(`x-custom${i}`)) {
          this._vCardProperties.addEntry(
            new lazy.VCardPropertyEntry(`x-custom${i}`, {}, "text", "")
          );
        }
      }

      el.vCardPropertyEntries = [
        this._vCardProperties.getFirstEntry("x-custom1"),
        this._vCardProperties.getFirstEntry("x-custom2"),
        this._vCardProperties.getFirstEntry("x-custom3"),
        this._vCardProperties.getFirstEntry("x-custom4"),
      ];
      addCustom.parentNode.insertBefore(el, addCustom);

      this.moveFocusIntoElement(el);
      addCustom.hidden = true;
    });

    // Delete button for Organization Properties. This property has multiple
    // fields, so we should dispatch the remove event only once after everything
    // has been removed.
    this.querySelector(
      "#addr-book-edit-org .remove-property-button"
    ).addEventListener("click", event => {
      this.querySelector("vcard-title").remove();
      this.querySelector("vcard-role").remove();
      let org = this.querySelector("vcard-org");
      // Reveal the "Add" button so we can focus it.
      document.getElementById("vcard-add-org").hidden = false;
      // Dispatch the event before removing the element so we can handle focus.
      org.dispatchEvent(
        new CustomEvent("vcard-remove-property", { bubbles: true })
      );
      org.remove();
      event.target.hidden = true;
    });
  }

  /**
   * Registers a click event for addButton which creates a new vCardProperty
   * and inserts it.
   *
   * @param {HTMLButtonElement} addButton
   * @param {string} VCardPropertyName RFC6350 vCard property name.
   * @param {(vCardElement) => {}} callback For further refinement.
   * Like different focus instead of an input field.
   */
  registerAddButton(addButton, VCardPropertyName, callback) {
    addButton.addEventListener("click", event => {
      let newVCardProperty = VCardEdit.createVCardProperty(VCardPropertyName);
      let el = this.insertVCardElement(newVCardProperty, true);

      this.moveFocusIntoElement(el);
      if (callback) {
        callback(el);
      }
    });
  }

  /**
   * If one BDAY vCardPropertyEntry is present disable
   * the option to change an Anniversary to a BDAY.
   *
   * @see VCardSpecialDateComponent
   */
  checkForBdayOccurrences() {
    let bdayOccurrence = this.vCardProperties.getFirstEntry("bday");
    this.querySelectorAll("vcard-special-date").forEach(specialDate => {
      specialDate.birthdayAvailability({ hasBday: !!bdayOccurrence });
    });
  }

  /**
   * Hide the default checkbox if we only have one email field.
   */
  toggleDefaultEmailView() {
    let hideDefault =
      document.getElementById("vcard-email").children.length <= 1;
    let defaultColumn = this.querySelector(".default-column");
    if (defaultColumn) {
      defaultColumn.hidden = hideDefault;
    }
    document.getElementById("addr-book-edit-email-default").hidden =
      hideDefault;

    // Add class to position legend absolute.
    document
      .getElementById("addr-book-edit-email")
      .classList.toggle("default-table-header", !hideDefault);
  }

  /**
   * Validate the form with the minimum required data to save or update a
   * contact. We can't use the built-in checkValidity() since our fields
   * are not handled properly by the form element.
   *
   * @returns {boolean} - If the form is valid or not.
   */
  checkMinimumRequirements() {
    let hasEmail = [...document.getElementById("vcard-email").children].find(
      s => {
        let field = s.querySelector(`input[type="email"]`);
        return field.value.trim() && field.checkValidity();
      }
    );
    let hasOrg = [...this.querySelectorAll("vcard-org")].find(n =>
      n.orgEl.value.trim()
    );

    return (
      this.firstName.value.trim() ||
      this.lastName.value.trim() ||
      this.displayName.value.trim() ||
      hasEmail ||
      hasOrg
    );
  }

  /**
   * Validate the special date fields making sure that we have a valid
   * DATE-AND-OR-TIME. See date, date-noreduc.
   * That is, valid if any of the fields are valid, but the combination of
   * only year and day is not valid.
   *
   * @returns {boolean} - True all created special date fields are valid.
   * @see https://datatracker.ietf.org/doc/html/rfc6350#section-4.3.4
   */
  validateDates() {
    for (let field of document.querySelectorAll("vcard-special-date")) {
      let y = field.querySelector(`input[type="number"][name="year"]`);
      let m = field.querySelector(`select[name="month"]`);
      let d = field.querySelector(`select[name="day"]`);
      if (!y.checkValidity()) {
        y.focus();
        return false;
      }
      if (y.value && d.value && !m.value) {
        m.required = true;
        m.focus();
        return false;
      }
    }
    return true;
  }
}
customElements.define("vcard-edit", VCardEdit);

/**
 * Responsible for the type selection of a vCard property.
 *
 * Couples the given vCardPropertyEntry with a <select> element.
 * This is safe because contact editing always creates a new contact, even
 * when an existing contact is selected for editing.
 *
 * @see RFC6350 TYPE
 */
class VCardTypeSelectionComponent extends HTMLElement {
  /**
   * The select element created by this custom element.
   *
   * @type {HTMLSelectElement}
   */
  selectEl;

  /**
   * Initializes the type selector elements to control the given
   * vCardPropertyEntry.
   *
   * @param {VCardPropertyEntry} vCardPropertyEntry - The VCardPropertyEntry
   *   this element should control.
   * @param {boolean} [options.createLabel] - Whether a Type label should be
   *   created for the selectEl element. If this is not `true`, then the label
   *   for the selectEl should be provided through some other means, such as the
   *   labelledBy property.
   * @param {string} [options.labelledBy] - Optional `id` of the element that
   *   should label the selectEl element (through aria-labelledby).
   * @param {string} [options.propertyType] - Specifies the set of types that
   *   should be available and shown for the corresponding property. Set as
   *   "tel" to use the set of telephone types. Otherwise defaults to only using
   *   the `home`, `work` and `(None)` types.
   */
  createTypeSelection(vCardPropertyEntry, options) {
    let template;
    let types;
    switch (options.propertyType) {
      case "tel":
        types = ["work", "home", "cell", "fax", "pager"];
        template = document.getElementById("template-vcard-edit-type-tel");
        break;
      default:
        types = ["work", "home"];
        template = document.getElementById("template-vcard-edit-type");
        break;
    }

    let clonedTemplate = template.content.cloneNode(true);
    this.replaceChildren(clonedTemplate);

    this.selectEl = this.querySelector("select");
    let selectId = vCardIdGen.next().value;
    this.selectEl.id = selectId;

    // Just abandon any values we don't have UI for. We don't have any way to
    // know whether to keep them or not, and they're very rarely used.
    let paramsType = vCardPropertyEntry.params.type;
    // toLowerCase is called because other vCard sources are saving the type
    // in upper case. E.g. from Google.
    if (Array.isArray(paramsType)) {
      let lowerCaseTypes = paramsType.map(type => type.toLowerCase());
      this.selectEl.value = lowerCaseTypes.find(t => types.includes(t)) || "";
    } else if (paramsType && types.includes(paramsType.toLowerCase())) {
      this.selectEl.value = paramsType.toLowerCase();
    }

    // Change the value on the vCardPropertyEntry.
    this.selectEl.addEventListener("change", e => {
      if (this.selectEl.value) {
        vCardPropertyEntry.params.type = this.selectEl.value;
      } else {
        delete vCardPropertyEntry.params.type;
      }
    });

    // Set an aria-labelledyby on the select.
    if (options.labelledBy) {
      if (!document.getElementById(options.labelledBy)) {
        throw new Error(`No such label element with id ${options.labelledBy}`);
      }
      this.querySelector("select").setAttribute(
        "aria-labelledby",
        options.labelledBy
      );
    }

    // Create a label element for the select.
    if (options.createLabel) {
      let labelEl = document.createElement("label");
      labelEl.htmlFor = selectId;
      labelEl.setAttribute("data-l10n-id", "vcard-entry-type-label");
      labelEl.classList.add("screen-reader-only");
      this.insertBefore(labelEl, this.selectEl);
    }
  }
}

customElements.define("vcard-type", VCardTypeSelectionComponent);

/**
 * Interface for vCard Fields in the edit view.
 *
 * @interface VCardPropertyEntryView
 */

/**
 * Getter/Setter for rich data do not use HTMLAttributes for this.
 *  Keep the reference intact through vCardProperties for proper saving.
 *
 * @property
 * @name VCardPropertyEntryView#vCardPropertyEntry
 */

/**
 * fromUIToVCardPropertyEntry should directly change data with the reference
 *  through vCardPropertyEntry.
 * It's there for an action to read the user input values into the
 *  vCardPropertyEntry.
 *
 * @function
 * @name VCardPropertyEntryView#fromUIToVCardPropertyEntry
 * @returns {void}
 */

/**
 * Updates the UI accordingly to the vCardPropertyEntry.
 *
 * @function
 * @name VCardPropertyEntryView#fromVCardPropertyEntryToUI
 * @returns {void}
 */

/**
 * Checks if the value of VCardPropertyEntry is empty.
 *
 * @function
 * @name VCardPropertyEntryView#valueIsEmpty
 * @returns {boolean}
 */

/**
 * Creates a new VCardPropertyEntry for usage in the a new Field.
 *
 * @function
 * @name VCardPropertyEntryView#newVCardPropertyEntry
 * @static
 * @returns {VCardPropertyEntry}
 */