xref: /webtrees/resources/views/modals/create-source.phtml (revision 4d99955e5797e1e12688b8d0592d207a63b2c6c6)
1<?php use Fisharebest\Webtrees\I18N; ?>
2
3<form action="<?= e(route('create-source')) ?>" id="wt-modal-form" method="POST">
4    <?= csrf_field() ?>
5    <input type="hidden" name="ged" value="<?= e($tree->name()) ?>">
6
7    <?= view('modals/header', ['title' => I18N::translate('Create a source')]) ?>
8
9    <div class="modal-body">
10        <?= view('modals/source-fields', ['tree' => $tree]) ?>
11    </div>
12
13    <?= view('modals/footer-save-cancel') ?>
14</form>
15
16<script>
17    // Submit the modal form using AJAX
18    document.getElementById("wt-modal-form").addEventListener("submit", function (event) {
19        event.preventDefault();
20        let form          = event.target;
21        let modal_content = document.querySelector("#wt-ajax-modal .modal-content");
22        let select        = document.getElementById(modal_content.dataset.selectId);
23
24        $.ajax({
25            url:         form.action,
26            type:        form.method,
27            data:        new FormData(form),
28            async:       false,
29            cache:       false,
30            contentType: false,
31            processData: false,
32            success:     function (data) {
33                if (select) {
34                    // If this modal was activated by the "new" button in a select2
35                    // edit control, then insert the result and select it.
36                    let option       = new Option(data.text, data.id, true, true);
37                    option.innerHTML = option.innerText;
38
39                    $(select)
40                        .select2()
41                        .empty()
42                        .append(option)
43                        .trigger("change");
44
45                    $("#wt-ajax-modal").modal("hide");
46                } else {
47                    modal_content.innerHTML = data.html;
48                }
49            },
50            failure:     function (data) {
51                modal_content.innerHTML = data.html;
52            },
53        });
54    });
55</script>
56