xref: /webtrees/resources/views/edit/input-addon-edit-name.phtml (revision 13adff1f29a9f4cc132345652c8d37d9d4d0c269)
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();
25a45ad69dSGreg Roach    let element = document.getElementById('<?= e($id) ?>');
26a45ad69dSGreg Roach    element.readOnly = false;
27a45ad69dSGreg Roach    element.focus();
28a45ad69dSGreg Roach
299042baeaSGreg Roach    let input_addon = this.parentNode;
309b35f881SGreg Roach    input_addon.removeChild(input_addon);
31a45ad69dSGreg Roach  });
32a45ad69dSGreg Roach  document.addEventListener('DOMContentLoaded', function () {
339b35f881SGreg Roach    let NAME = document.getElementById('<?= e($id) ?>');
349b35f881SGreg Roach    let container = NAME.parentNode.parentNode.parentNode;
359b35f881SGreg Roach
369b35f881SGreg Roach    if (NAME.id.endsWith('-INDI:NAME')) {
379b35f881SGreg Roach      // NAME has children at the same level.
389b35f881SGreg Roach      container = container.parentNode;
399b35f881SGreg Roach    } else {
409b35f881SGreg Roach      // ROMN/FONE have children in a collapsable panel
419b35f881SGreg Roach      container = container.nextSibling.nextSibling;
429b35f881SGreg Roach    }
439b35f881SGreg Roach    let NPFX = container.querySelector('[id$=":NPFX"]');
449b35f881SGreg Roach    let GIVN = container.querySelector('[id$=":GIVN"]');
459b35f881SGreg Roach    let SPFX = container.querySelector('[id$=":SPFX"]');
469b35f881SGreg Roach    let SURN = container.querySelector('[id$=":SURN"]');
479b35f881SGreg Roach    let NSFX = container.querySelector('[id$=":NSFX"]');
48a45ad69dSGreg Roach
49*13adff1fSGreg Roach    let generated_name = webtrees.buildNameFromParts(
50a45ad69dSGreg Roach      NPFX ? NPFX.value : '',
51a45ad69dSGreg Roach      GIVN ? GIVN.value : '',
52a45ad69dSGreg Roach      SPFX ? SPFX.value : '',
53a45ad69dSGreg Roach      SURN ? SURN.value : '',
54a45ad69dSGreg Roach      NSFX ? NSFX.value : '',
559b35f881SGreg Roach      'U',
56*13adff1fSGreg Roach    );
57*13adff1fSGreg Roach
58*13adff1fSGreg Roach    if (NAME.value === '') {
59*13adff1fSGreg Roach      NAME.value = generated_name;
60*13adff1fSGreg Roach    }
61*13adff1fSGreg Roach    if (NAME.value !== generated_name) {
62a45ad69dSGreg Roach      document.getElementById('<?= e($id) ?>-edit').click();
63a45ad69dSGreg Roach    } else {
64a45ad69dSGreg Roach      let fn = function () {
65a45ad69dSGreg Roach        if (NAME.readOnly === true) {
66a45ad69dSGreg Roach          NAME.value = webtrees.buildNameFromParts(
67a45ad69dSGreg Roach            NPFX ? NPFX.value : '',
68a45ad69dSGreg Roach            GIVN ? GIVN.value : '',
69a45ad69dSGreg Roach            SPFX ? SPFX.value : '',
70a45ad69dSGreg Roach            SURN ? SURN.value : '',
71a45ad69dSGreg Roach            NSFX ? NSFX.value : '',
729b35f881SGreg Roach            'U',
73a45ad69dSGreg Roach          );
74a45ad69dSGreg Roach        }
759b35f881SGreg Roach      };
76f49d2236SGreg Roach      NPFX && NPFX.addEventListener('input', fn);
77f49d2236SGreg Roach      GIVN && GIVN.addEventListener('input', fn);
78f49d2236SGreg Roach      SPFX && SPFX.addEventListener('input', fn);
79f49d2236SGreg Roach      SURN && SURN.addEventListener('input', fn);
80f49d2236SGreg Roach      SURN && SURN.addEventListener('blur', fn); // For autocompleted entries
81f49d2236SGreg Roach      NSFX && NSFX.addEventListener('input', fn);
82a45ad69dSGreg Roach    }
83a45ad69dSGreg Roach  });
84a45ad69dSGreg Roach</script>
85