xref: /webtrees/resources/views/individual-page-tabs.phtml (revision 4e54fb47ea2fc584c07c0c135853d49a85735c1a)
10f5fd22fSGreg Roach<?php
20f5fd22fSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
50f5fd22fSGreg Roachuse Fisharebest\Webtrees\Individual;
60f5fd22fSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
70f5fd22fSGreg Roachuse Fisharebest\Webtrees\View;
80f5fd22fSGreg Roachuse Illuminate\Support\Collection;
90f5fd22fSGreg Roach
100f5fd22fSGreg Roach/**
110f5fd22fSGreg Roach * @var Individual                         $record
1236779af1SGreg Roach * @var Collection<int,ModuleTabInterface> $tabs
130f5fd22fSGreg Roach */
140f5fd22fSGreg Roach?>
150f5fd22fSGreg Roach
160f5fd22fSGreg Roach<div class="wt-tabs-individual" id="individual-tabs">
170f5fd22fSGreg Roach    <ul class="nav nav-tabs flex-wrap" role="tablist">
180f5fd22fSGreg Roach        <?php foreach ($tabs as $tab) : ?>
190f5fd22fSGreg Roach            <li class="nav-item" role="presentation">
20d4786c66SGreg 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() ?>">
210f5fd22fSGreg Roach                    <?= $tab->tabTitle() ?>
220f5fd22fSGreg Roach                </a>
230f5fd22fSGreg Roach            </li>
240f5fd22fSGreg Roach        <?php endforeach ?>
250f5fd22fSGreg Roach    </ul>
260f5fd22fSGreg Roach
270f5fd22fSGreg Roach    <div class="tab-content">
280f5fd22fSGreg Roach        <?php foreach ($tabs as $tab) : ?>
29*4e54fb47SGreg Roach            <div id="<?= $tab->name() ?>" class="tab-pane mt-2 fade wt-ajax-load" role="tabpanel"><?php if (!$tab->canLoadAjax()) : ?><?= $tab->getTabContent($record) ?><?php endif ?></div>
300f5fd22fSGreg Roach        <?php endforeach ?>
310f5fd22fSGreg Roach    </div>
320f5fd22fSGreg Roach</div>
330f5fd22fSGreg Roach
340f5fd22fSGreg Roach<?php View::push('javascript') ?>
350f5fd22fSGreg Roach<script>
36ffb44950SGreg 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...
43313cf418SGreg Roach      let download = webtrees.httpGet(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