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