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