xref: /webtrees/resources/views/individual-page-tabs.phtml (revision ffb4495015ffc2f565789df543f2759f552aee96)
10f5fd22fSGreg Roach<?php
20f5fd22fSGreg Roach
30f5fd22fSGreg Roachuse Fisharebest\Webtrees\Individual;
40f5fd22fSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
50f5fd22fSGreg Roachuse Fisharebest\Webtrees\View;
60f5fd22fSGreg Roachuse Illuminate\Support\Collection;
70f5fd22fSGreg Roach
80f5fd22fSGreg Roach/**
90f5fd22fSGreg Roach * @var Individual                     $record
100f5fd22fSGreg Roach * @var Collection<ModuleTabInterface> $tabs
110f5fd22fSGreg Roach */
120f5fd22fSGreg Roach?>
130f5fd22fSGreg Roach
140f5fd22fSGreg Roach<div class="wt-tabs-individual" id="individual-tabs">
150f5fd22fSGreg Roach    <ul class="nav nav-tabs flex-wrap" role="tablist">
160f5fd22fSGreg Roach        <?php foreach ($tabs as $tab) : ?>
170f5fd22fSGreg Roach            <li class="nav-item" role="presentation">
18d4786c66SGreg Roach                <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() ?>">
190f5fd22fSGreg Roach                    <?= $tab->tabTitle() ?>
200f5fd22fSGreg Roach                </a>
210f5fd22fSGreg Roach            </li>
220f5fd22fSGreg Roach        <?php endforeach ?>
230f5fd22fSGreg Roach    </ul>
240f5fd22fSGreg Roach
250f5fd22fSGreg Roach    <div class="tab-content">
260f5fd22fSGreg Roach        <?php foreach ($tabs as $tab) : ?>
270f5fd22fSGreg Roach            <div id="<?= $tab->name() ?>" class="tab-pane fade wt-ajax-load" role="tabpanel"><?php if (!$tab->canLoadAjax()) :
280f5fd22fSGreg Roach                    ?><?= $tab->getTabContent($record) ?><?php
290f5fd22fSGreg Roach                endif ?></div>
300f5fd22fSGreg Roach        <?php endforeach ?>
310f5fd22fSGreg Roach    </div>
320f5fd22fSGreg Roach</div>
330f5fd22fSGreg Roach
340f5fd22fSGreg Roach<?php View::push('javascript') ?>
350f5fd22fSGreg Roach<script>
36*ffb44950SGreg Roach  'use strict';
370f5fd22fSGreg Roach
380f5fd22fSGreg Roach  // Bootstrap tabs - load content dynamically using AJAX
39d4786c66SGreg Roach  $('a[data-bs-toggle="tab"][data-wt-href]').on('show.bs.tab', function () {
400f5fd22fSGreg Roach    let target = $(this.hash + ':empty');
410f5fd22fSGreg Roach    if (target.length > 0) {
420f5fd22fSGreg Roach      // Start the download immediately...
43d4786c66SGreg Roach      let download = fetch(this.dataset.wtHref);
440f5fd22fSGreg Roach
450f5fd22fSGreg Roach      // ...but don't insert it until the tab is ready.
460f5fd22fSGreg Roach      $(this).one('shown.bs.tab', () => {
470f5fd22fSGreg Roach        download
480f5fd22fSGreg Roach          .then(data => data.text())
490f5fd22fSGreg Roach          .then(data => target.html(data));
500f5fd22fSGreg Roach      });
510f5fd22fSGreg Roach    }
520f5fd22fSGreg Roach  });
530f5fd22fSGreg Roach
540f5fd22fSGreg Roach  // If the URL contains a fragment, then activate the corresponding tab.
550f5fd22fSGreg Roach  // Use a prefix on the fragment, to prevent scrolling to the element.
560f5fd22fSGreg Roach  let target = window.location.hash.replace("tab-", "");
57315eb316SGreg Roach  let tab = document.querySelector("#individual-tabs .nav-link[href='" + target + "']");
580f5fd22fSGreg Roach  // If not, then activate the first tab.
59315eb316SGreg Roach  tab = tab ?? document.querySelector("#individual-tabs .nav-link");
60315eb316SGreg Roach  tab.click();
610f5fd22fSGreg Roach
620f5fd22fSGreg Roach  // If the user selects a tab, update the URL to reflect this
63315eb316SGreg Roach  $('#individual-tabs a[data-bs-toggle="tab"]').on('shown.bs.tab', function (e) {
640f5fd22fSGreg Roach    window.location.hash = "tab-" + e.target.href.substring(e.target.href.indexOf('#') + 1);
650f5fd22fSGreg Roach  });
660f5fd22fSGreg Roach</script>
670f5fd22fSGreg Roach<?php View::endpush() ?>
68