Skip to content

Commit

Permalink
Combined jump links: configurable linking modes
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 8, 2025
1 parent c8537e5 commit df1ea7a
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
5 changes: 5 additions & 0 deletions config/vufind/combined.ini
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ columns = 3
stack_placement = distributed
; Jump links appear above the search results and link to each result module
;jump_links = true
; If jump_links is true, this setting can control what types of links are used:
; 'anchor' will create anchor links that jump to the matching column on the page
; 'link' will link directly to the expanded search results
; The default is 'anchor'
;jump_links_mode = anchor

[Solr]
label = Catalog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,34 @@ public function testCombinedSearchResultsMixedAjax(): void
$this->assertResultsForDefaultQuery($page);
}

/**
* Data provider for testJumpMenu()
*
* @return array[]
*/
public static function jumpMenuProvider(): array
{
return [
'anchor mode' => ['anchor'],
'link mode' => ['link'],
];
}

/**
* Test that the jump menu can be enabled.
*
* @param string $linkMode Linking mode to activate
*
* @return void
*
* @dataProvider jumpMenuProvider
*/
public function testJumpMenu(): void
public function testJumpMenu(string $linkMode): void
{
$config = $this->getCombinedIniOverrides();
$config['Solr:one']['ajax'] = true; // use mixed AJAX mode for more thorough test
$config['Layout']['jump_links'] = true;
$config['Layout']['jump_links_mode'] = $linkMode;
$this->changeConfigs(
['combined' => $config],
['combined']
Expand All @@ -262,6 +280,18 @@ public function testJumpMenu(): void
$expectedContent,
$this->findCssAndGetText($page, '.combined-jump-links')
);
$firstLink = $this->findCss($page, '.combined-jump-links a')->getAttribute('href');
$secondLink = $this->findCss($page, '.combined-jump-links a', index: 1)->getAttribute('href');
$expectedFirstLink = $linkMode === 'anchor'
? '#combined_Solr____one'
: '/Search/Results?hiddenFilters%5B%5D=building%3A%22journals.mrc%22&lookfor=id%3A%22testsample1%22'
. '+OR+id%3A%22theplus%2Bandtheminus-%22&type=AllFields';
$expectedSecondLink = $linkMode === 'anchor'
? '#combined_Solr____two'
: '/Search/Results?hiddenFilters%5B%5D=building%3A%22weird_ids.mrc%22&lookfor=id%3A%22testsample1%22'
. '+OR+id%3A%22theplus%2Bandtheminus-%22&type=AllFields';
$this->assertStringEndsWith($expectedFirstLink, $firstLink);
$this->assertStringEndsWith($expectedSecondLink, $secondLink);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion themes/bootstrap3/templates/combined/jump-links.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
<span><?=$this->translate('combined_jump_links_intro')?></span>
<ul>
<?php foreach ($combinedResults as $section) : ?>
<?php
// If we're in link mode, try to directly link to results, but fail back to the anchor if we don't
// have access to results, or if the results object is "combined" (indicating we're in AJAX mode
// and don't have results yet):
if (
($mode ?? 'anchor') === 'link'
&& ($results = $section['view']->results ?? null)
&& !($results instanceof \VuFind\Search\Combined\Results)
) {
$params = $results->getParams();
$lookfor = $results->getUrlQuery()->isQuerySuppressed() ? '' : $params->getDisplayQuery();
$href = $this->url($params->getOptions()->getSearchAction()) . $results->getUrlQuery()->setPage(1)->setLimit($params->getOptions()->getDefaultLimit());
} else {
$href = '#' . $section['domId'];
}
?>
<li class="<?= $this->escapeHtmlAttr($section['domId']) ?>">
<a href="#<?= $this->escapeHtmlAttr($section['domId']) ?>"><?= $this->transEsc($section['label']) ?></a>
<a href="<?= $this->escapeHtmlAttr($href) ?>" data-link-mode="<?= $this->escapeHtmlAttr($mode) ?>"><?= $this->transEsc($section['label']) ?></a>
</li>
<?php endforeach; ?>
</ul>
Expand Down
13 changes: 9 additions & 4 deletions themes/bootstrap3/templates/combined/results-list.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,24 @@

// Update result counts in jump links
$classId = $this->escapeHtml($domId ?? 'combined_' . $searchClassId);
$jsMoreUrl = $this->escapeJs(html_entity_decode($moreUrl));
$countsJs = <<<JS
function updateJumpLink_$classId() {
const combinedJumpLinks = document.querySelector('.combined-jump-links');
const link = combinedJumpLinks?.querySelector('.$classId');
if (link) {
const linkContainer = combinedJumpLinks?.querySelector('.$classId');
if (linkContainer) {
let count = $recordTotal;
count = count.toLocaleString();
const spanText = document.createTextNode('(' + count + ')');
const totalSpan = document.createElement('span');
totalSpan.className = 'record-total';
totalSpan.append(spanText);
link.append(totalSpan);
link.style.display = "inline";
linkContainer.append(totalSpan);
linkContainer.style.display = "inline";
const link = linkContainer.querySelector('a');
if (link.dataset.linkMode === 'link') {
link.href = "$jsMoreUrl";
};
combinedJumpLinks.style.visibility = "inherit";
}
}
Expand Down
10 changes: 9 additions & 1 deletion themes/bootstrap3/templates/combined/results.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@
</div>
<?php endif; ?>
<?php if ($config['Layout']['jump_links'] ?? false): ?>
<?=$this->render('combined/jump-links.phtml', ['combinedResults' => $this->combinedResults])?>
<?=
$this->render(
'combined/jump-links.phtml',
[
'combinedResults' => $this->combinedResults,
'mode' => $config['Layout']['jump_links_mode'] ?? 'anchor',
]
)
?>
<?php endif; ?>
<form id="search-cart-form" class="form-inline" method="post" name="bulkActionForm" action="<?=$this->url('cart-searchresultsbulk')?>">
<?=$this->context($this)->renderInContext('search/bulk-action-buttons.phtml', ['idPrefix' => ''])?>
Expand Down
18 changes: 17 additions & 1 deletion themes/bootstrap5/templates/combined/jump-links.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
<span><?=$this->translate('combined_jump_links_intro')?></span>
<ul>
<?php foreach ($combinedResults as $section) : ?>
<?php
// If we're in link mode, try to directly link to results, but fail back to the anchor if we don't
// have access to results, or if the results object is "combined" (indicating we're in AJAX mode
// and don't have results yet):
if (
($mode ?? 'anchor') === 'link'
&& ($results = $section['view']->results ?? null)
&& !($results instanceof \VuFind\Search\Combined\Results)
) {
$params = $results->getParams();
$lookfor = $results->getUrlQuery()->isQuerySuppressed() ? '' : $params->getDisplayQuery();
$href = $this->url($params->getOptions()->getSearchAction()) . $results->getUrlQuery()->setPage(1)->setLimit($params->getOptions()->getDefaultLimit());
} else {
$href = '#' . $section['domId'];
}
?>
<li class="<?= $this->escapeHtmlAttr($section['domId']) ?>">
<a href="#<?= $this->escapeHtmlAttr($section['domId']) ?>"><?= $this->transEsc($section['label']) ?></a>
<a href="<?= $this->escapeHtmlAttr($href) ?>" data-link-mode="<?= $this->escapeHtmlAttr($mode) ?>"><?= $this->transEsc($section['label']) ?></a>
</li>
<?php endforeach; ?>
</ul>
Expand Down
13 changes: 9 additions & 4 deletions themes/bootstrap5/templates/combined/results-list.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,24 @@

// Update result counts in jump links
$classId = $this->escapeHtml($domId ?? 'combined_' . $searchClassId);
$jsMoreUrl = $this->escapeJs(html_entity_decode($moreUrl));
$countsJs = <<<JS
function updateJumpLink_$classId() {
const combinedJumpLinks = document.querySelector('.combined-jump-links');
const link = combinedJumpLinks?.querySelector('.$classId');
if (link) {
const linkContainer = combinedJumpLinks?.querySelector('.$classId');
if (linkContainer) {
let count = $recordTotal;
count = count.toLocaleString();
const spanText = document.createTextNode('(' + count + ')');
const totalSpan = document.createElement('span');
totalSpan.className = 'record-total';
totalSpan.append(spanText);
link.append(totalSpan);
link.style.display = "inline";
linkContainer.append(totalSpan);
linkContainer.style.display = "inline";
const link = linkContainer.querySelector('a');
if (link.dataset.linkMode === 'link') {
link.href = "$jsMoreUrl";
};
combinedJumpLinks.style.visibility = "inherit";
}
}
Expand Down
10 changes: 9 additions & 1 deletion themes/bootstrap5/templates/combined/results.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@
</div>
<?php endif; ?>
<?php if ($config['Layout']['jump_links'] ?? false): ?>
<?=$this->render('combined/jump-links.phtml', ['combinedResults' => $this->combinedResults])?>
<?=
$this->render(
'combined/jump-links.phtml',
[
'combinedResults' => $this->combinedResults,
'mode' => $config['Layout']['jump_links_mode'] ?? 'anchor',
]
)
?>
<?php endif; ?>
<form id="search-cart-form" class="form-inline" method="post" name="bulkActionForm" action="<?=$this->url('cart-searchresultsbulk')?>">
<?=$this->context($this)->renderInContext('search/bulk-action-buttons.phtml', ['idPrefix' => ''])?>
Expand Down

0 comments on commit df1ea7a

Please sign in to comment.