xref: /webtrees/resources/views/edit/input-addon-edit-name.phtml (revision 2978efea0c934a7f830579d20c3e8661e837b748)
1a45ad69dSGreg Roach<?php
2a45ad69dSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
5a45ad69dSGreg Roachuse Fisharebest\Webtrees\I18N;
6a45ad69dSGreg Roach
77c2c99faSGreg Roach/**
87c2c99faSGreg Roach * @var string $id
97c2c99faSGreg Roach */
107c2c99faSGreg Roach
11a45ad69dSGreg Roach?>
12a45ad69dSGreg Roach
13a45ad69dSGreg Roach<span class="input-group-text">
14a45ad69dSGreg Roach    <a id="<?= e($id) ?>-edit" href="#" title="<?= I18N::translate('Edit the name') ?>">
15a45ad69dSGreg Roach        <?= view('icons/edit') ?>
16315eb316SGreg Roach        <span class="visually-hidden">
17a45ad69dSGreg Roach            <?= I18N::translate('Edit the name') ?>
18a45ad69dSGreg Roach        </span>
19a45ad69dSGreg Roach    </a>
20a45ad69dSGreg Roach</span>
21315eb316SGreg Roach
22a45ad69dSGreg Roach<script>
23a45ad69dSGreg Roach  document.getElementById('<?= e($id) ?>-edit').addEventListener('click', function (event) {
24a45ad69dSGreg Roach    event.preventDefault();
25*2978efeaSJonathan Jaubart    // Toggle the visibility of the full name inputs
26*2978efeaSJonathan Jaubart    // Hide the disable input - does not send any value when posting the form
27*2978efeaSJonathan Jaubart    let disabledElement = document.getElementById('<?= e($id) ?>-disabled');
28*2978efeaSJonathan Jaubart    disabledElement.classList.add('d-none');
29*2978efeaSJonathan Jaubart    // Show the editable input - send value when posting the form
30*2978efeaSJonathan Jaubart    let editElement = document.getElementById('<?= e($id) ?>');
31*2978efeaSJonathan Jaubart    editElement.classList.remove('d-none');
32*2978efeaSJonathan Jaubart    editElement.focus();
33a45ad69dSGreg Roach
3462cb0f58SGreg Roach    this.parentNode.removeChild(this);
35a45ad69dSGreg Roach  });
36a45ad69dSGreg Roach  document.addEventListener('DOMContentLoaded', function () {
379b35f881SGreg Roach    let NAME = document.getElementById('<?= e($id) ?>');
38*2978efeaSJonathan Jaubart    let NAME_DISABLED = document.getElementById('<?= e($id) ?>-disabled');
399b35f881SGreg Roach    let container = NAME.parentNode.parentNode.parentNode;
409b35f881SGreg Roach
41*2978efeaSJonathan Jaubart    let setNameValue = function(name_value) {
42*2978efeaSJonathan Jaubart      NAME.value = name_value;
43*2978efeaSJonathan Jaubart      NAME_DISABLED.value = name_value;
44*2978efeaSJonathan Jaubart    }
45*2978efeaSJonathan Jaubart
4682a264c2SGreg Roach    if (NAME.id.endsWith('-INDI-NAME')) {
479b35f881SGreg Roach      // NAME has children at the same level.
489b35f881SGreg Roach      container = container.parentNode;
499b35f881SGreg Roach    } else {
509b35f881SGreg Roach      // ROMN/FONE have children in a collapsable panel
519b35f881SGreg Roach      container = container.nextSibling.nextSibling;
529b35f881SGreg Roach    }
53fca40842SGreg Roach    let NPFX = container.querySelector('[id$="-NPFX"]');
54fca40842SGreg Roach    let GIVN = container.querySelector('[id$="-GIVN"]');
55fca40842SGreg Roach    let SPFX = container.querySelector('[id$="-SPFX"]');
56fca40842SGreg Roach    let SURN = container.querySelector('[id$="-SURN"]');
57fca40842SGreg Roach    let NSFX = container.querySelector('[id$="-NSFX"]');
58a45ad69dSGreg Roach
5913adff1fSGreg Roach    let generated_name = webtrees.buildNameFromParts(
60a45ad69dSGreg Roach      NPFX ? NPFX.value : '',
61a45ad69dSGreg Roach      GIVN ? GIVN.value : '',
62a45ad69dSGreg Roach      SPFX ? SPFX.value : '',
63a45ad69dSGreg Roach      SURN ? SURN.value : '',
64a45ad69dSGreg Roach      NSFX ? NSFX.value : '',
659b35f881SGreg Roach      'U',
6613adff1fSGreg Roach    );
6713adff1fSGreg Roach
6813adff1fSGreg Roach    if (NAME.value === '') {
69*2978efeaSJonathan Jaubart      setNameValue(generated_name);
7013adff1fSGreg Roach    }
7113adff1fSGreg Roach    if (NAME.value !== generated_name) {
72a45ad69dSGreg Roach      document.getElementById('<?= e($id) ?>-edit').click();
73a45ad69dSGreg Roach    } else {
74a45ad69dSGreg Roach      let fn = function () {
75*2978efeaSJonathan Jaubart        if (NAME.classList.contains("d-none") === true) {
76*2978efeaSJonathan Jaubart          setNameValue(webtrees.buildNameFromParts(
77a45ad69dSGreg Roach            NPFX ? NPFX.value : '',
78a45ad69dSGreg Roach            GIVN ? GIVN.value : '',
79a45ad69dSGreg Roach            SPFX ? SPFX.value : '',
80a45ad69dSGreg Roach            SURN ? SURN.value : '',
81a45ad69dSGreg Roach            NSFX ? NSFX.value : '',
829b35f881SGreg Roach            'U',
83*2978efeaSJonathan Jaubart          ));
84a45ad69dSGreg Roach        }
859b35f881SGreg Roach      };
86f49d2236SGreg Roach      NPFX && NPFX.addEventListener('input', fn);
87f49d2236SGreg Roach      GIVN && GIVN.addEventListener('input', fn);
88f49d2236SGreg Roach      SPFX && SPFX.addEventListener('input', fn);
89f49d2236SGreg Roach      SURN && SURN.addEventListener('input', fn);
90f49d2236SGreg Roach      SURN && SURN.addEventListener('blur', fn); // For autocompleted entries
91f49d2236SGreg Roach      NSFX && NSFX.addEventListener('input', fn);
92a45ad69dSGreg Roach    }
93a45ad69dSGreg Roach  });
94a45ad69dSGreg Roach</script>
95