xref: /webtrees/resources/views/individual-page-tabs.phtml (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Individual;
6use Fisharebest\Webtrees\Module\ModuleTabInterface;
7use Fisharebest\Webtrees\View;
8use Illuminate\Support\Collection;
9
10/**
11 * @var Individual                         $record
12 * @var Collection<int,ModuleTabInterface> $tabs
13 */
14?>
15
16<div class="wt-tabs-individual" id="individual-tabs">
17    <ul class="nav nav-tabs flex-wrap" role="tablist">
18        <?php foreach ($tabs as $tab) : ?>
19            <li class="nav-item" role="presentation">
20                <a class="nav-link<?= $tab->isGrayedOut($record) ? ' text-muted' : '' ?>" data-bs-toggle="tab" role="tab" data-wt-href="<?= e(route('module', ['module' => $tab->name(), 'action' => 'Tab', 'tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>" href="#<?= $tab->name() ?>">
21                    <?= $tab->tabTitle() ?>
22                </a>
23            </li>
24        <?php endforeach ?>
25    </ul>
26
27    <div class="tab-content">
28        <?php foreach ($tabs as $tab) : ?>
29            <div id="<?= $tab->name() ?>" class="tab-pane fade wt-ajax-load" role="tabpanel">
30                <?php if (!$tab->canLoadAjax()) : ?>
31                    <?= $tab->getTabContent($record) ?>
32                <?php endif ?>
33            </div>
34        <?php endforeach ?>
35    </div>
36</div>
37
38<?php View::push('javascript') ?>
39<script>
40  'use strict';
41
42  // Bootstrap tabs - load content dynamically using AJAX
43  $('a[data-bs-toggle="tab"][data-wt-href]').on('show.bs.tab', function () {
44    let target = $(this.hash + ':empty');
45    if (target.length > 0) {
46      // Start the download immediately...
47      let download = webtrees.httpGet(this.dataset.wtHref);
48
49      // ...but don't insert it until the tab is ready.
50      $(this).one('shown.bs.tab', () => {
51        download
52          .then(data => data.text())
53          .then(data => target.html(data));
54      });
55    }
56  });
57
58  // If the URL contains a fragment, then activate the corresponding tab.
59  // Use a prefix on the fragment, to prevent scrolling to the element.
60  let target = window.location.hash.replace("tab-", "");
61  let tab = document.querySelector("#individual-tabs .nav-link[href='" + target + "']");
62  // If not, then activate the first tab.
63  tab = tab ?? document.querySelector("#individual-tabs .nav-link");
64  tab.click();
65
66  // If the user selects a tab, update the URL to reflect this
67  $('#individual-tabs a[data-bs-toggle="tab"]').on('shown.bs.tab', function (e) {
68    window.location.hash = "tab-" + e.target.href.substring(e.target.href.indexOf('#') + 1);
69  });
70</script>
71<?php View::endpush() ?>
72