xref: /webtrees/resources/views/edit/input-addon-edit-name.phtml (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
1a45ad69dSGreg Roach<?php
2a45ad69dSGreg Roach
3*10e06497SGreg Roachdeclare(strict_types=1);
4*10e06497SGreg 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;
309042baeaSGreg Roach        input_addon.removeChild(input_addon);
31a45ad69dSGreg Roach    });
32a45ad69dSGreg Roach    document.addEventListener('DOMContentLoaded', function () {
33a45ad69dSGreg Roach        let container = document.getElementById('<?= e($id) ?>').parentNode.parentNode.parentNode.parentNode;
34a45ad69dSGreg Roach        let NAME = container.querySelector('[id$="INDI:NAME"]');
35a45ad69dSGreg Roach        let NPFX = container.querySelector('[id$="INDI:NAME:NPFX"]');
36a45ad69dSGreg Roach        let GIVN = container.querySelector('[id$="INDI:NAME:GIVN"]');
37a45ad69dSGreg Roach        let SPFX = container.querySelector('[id$="INDI:NAME:SPFX"]');
38a45ad69dSGreg Roach        let SURN = container.querySelector('[id$="INDI:NAME:SURN"]');
39a45ad69dSGreg Roach        let NSFX = container.querySelector('[id$="INDI:NAME:NSFX"]');
40a45ad69dSGreg Roach
41a45ad69dSGreg Roach        if (NAME.value !== webtrees.buildNameFromParts(
42a45ad69dSGreg Roach            NPFX ? NPFX.value : '',
43a45ad69dSGreg Roach            GIVN ? GIVN.value : '',
44a45ad69dSGreg Roach            SPFX ? SPFX.value : '',
45a45ad69dSGreg Roach            SURN ? SURN.value : '',
46a45ad69dSGreg Roach            NSFX ? NSFX.value : '',
47a45ad69dSGreg Roach            'U'
48a45ad69dSGreg Roach        )) {
49a45ad69dSGreg Roach            document.getElementById('<?= e($id) ?>-edit').click();
50a45ad69dSGreg Roach        } else {
51a45ad69dSGreg Roach            let fn = function () {
52a45ad69dSGreg Roach                if (NAME.readOnly === true) {
53a45ad69dSGreg Roach                    NAME.value = webtrees.buildNameFromParts(
54a45ad69dSGreg Roach                        NPFX ? NPFX.value : '',
55a45ad69dSGreg Roach                        GIVN ? GIVN.value : '',
56a45ad69dSGreg Roach                        SPFX ? SPFX.value : '',
57a45ad69dSGreg Roach                        SURN ? SURN.value : '',
58a45ad69dSGreg Roach                        NSFX ? NSFX.value : '',
59a45ad69dSGreg Roach                        'U'
60a45ad69dSGreg Roach                    );
61a45ad69dSGreg Roach                }
62a45ad69dSGreg Roach            }
63f49d2236SGreg Roach            NPFX && NPFX.addEventListener('input', fn);
64f49d2236SGreg Roach            GIVN && GIVN.addEventListener('input', fn);
65f49d2236SGreg Roach            SPFX && SPFX.addEventListener('input', fn);
66f49d2236SGreg Roach            SURN && SURN.addEventListener('input', fn);
67f49d2236SGreg Roach            SURN && SURN.addEventListener('blur', fn); // For autocompleted entries
68f49d2236SGreg Roach            NSFX && NSFX.addEventListener('input', fn);
69a45ad69dSGreg Roach        }
70a45ad69dSGreg Roach    });
71a45ad69dSGreg Roach</script>
72