xref: /webtrees/resources/views/modals/create-repository.phtml (revision edbd59d95d68a6a1309e3b3be4b7cd46060a3f9a)
1852ede8cSGreg Roach<?php
2dd6b2bfcSGreg Roach
3852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\CreateRepositoryAction;
4852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N;
5*edbd59d9SGreg Roachuse Fisharebest\Webtrees\Tree;
6*edbd59d9SGreg Roach
7*edbd59d9SGreg Roach/**
8*edbd59d9SGreg Roach * @var Tree $tree
9*edbd59d9SGreg Roach */
10852ede8cSGreg Roach
11852ede8cSGreg Roach?>
12852ede8cSGreg Roach
13852ede8cSGreg Roach<form method="post" action="<?= e(route(CreateRepositoryAction::class, ['tree' => $tree->name()])) ?>" id="wt-modal-form">
14dd6b2bfcSGreg Roach    <?= csrf_field() ?>
15dd6b2bfcSGreg Roach
16dd6b2bfcSGreg Roach    <?= view('modals/header', ['title' => I18N::translate('Create a repository')]) ?>
17dd6b2bfcSGreg Roach
18dd6b2bfcSGreg Roach    <div class="modal-body">
19dd6b2bfcSGreg Roach        <?= view('modals/repository-fields') ?>
20dd6b2bfcSGreg Roach    </div>
21dd6b2bfcSGreg Roach
22dd6b2bfcSGreg Roach    <?= view('modals/footer-save-cancel') ?>
23dd6b2bfcSGreg Roach</form>
24dd6b2bfcSGreg Roach
25dd6b2bfcSGreg Roach<script>
26dd6b2bfcSGreg Roach    // Submit the modal form using AJAX
272a12145eSGreg Roach    document.getElementById("wt-modal-form").addEventListener("submit", function (event) {
28dd6b2bfcSGreg Roach        event.preventDefault();
29dd6b2bfcSGreg Roach        let form          = event.target;
302a12145eSGreg Roach        let modal_content = document.querySelector("#wt-ajax-modal .modal-content");
31dd6b2bfcSGreg Roach        let select        = document.getElementById(modal_content.dataset.selectId);
32dd6b2bfcSGreg Roach
33dd6b2bfcSGreg Roach        $.ajax({
34dd6b2bfcSGreg Roach            url:         form.action,
35dd6b2bfcSGreg Roach            type:        form.method,
36dd6b2bfcSGreg Roach            data:        new FormData(form),
37dd6b2bfcSGreg Roach            async:       false,
38dd6b2bfcSGreg Roach            cache:       false,
39dd6b2bfcSGreg Roach            contentType: false,
40dd6b2bfcSGreg Roach            processData: false,
41dd6b2bfcSGreg Roach            success:     function (data) {
42dd6b2bfcSGreg Roach                if (select) {
43dd6b2bfcSGreg Roach                    // If this modal was activated by the "new" button in a select2
44dd6b2bfcSGreg Roach                    // edit control, then insert the result and select it.
452a12145eSGreg Roach                    let option       = new Option(data.text, data.id, true, true);
462a12145eSGreg Roach                    option.innerHTML = option.innerText;
472a12145eSGreg Roach
48dd6b2bfcSGreg Roach                    $(select)
49*edbd59d9SGreg Roach                        .select2({ escapeMarkup: (x) => x })
50dd6b2bfcSGreg Roach                        .empty()
512a12145eSGreg Roach                        .append(option)
522a12145eSGreg Roach                        .trigger("change");
53dd6b2bfcSGreg Roach
542a12145eSGreg Roach                    $("#wt-ajax-modal").modal("hide");
55dd6b2bfcSGreg Roach                } else {
56dd6b2bfcSGreg Roach                    modal_content.innerHTML = data.html;
57dd6b2bfcSGreg Roach                }
58dd6b2bfcSGreg Roach            },
59dd6b2bfcSGreg Roach            failure:     function (data) {
60dd6b2bfcSGreg Roach                modal_content.innerHTML = data.html;
612a12145eSGreg Roach            },
62dd6b2bfcSGreg Roach        });
63dd6b2bfcSGreg Roach    });
64dd6b2bfcSGreg Roach</script>
65