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