From e52f1db4625a3c2c3caad1ae721fec665c51b6cb Mon Sep 17 00:00:00 2001 From: Kyle Huynh Date: Wed, 26 Jan 2022 19:46:24 +0000 Subject: [PATCH] Update pager links after searching in place --- js/advanced_search.form.js | 57 +++++++++++++++++++- src/Plugin/Block/SearchResultsPagerBlock.php | 4 +- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/js/advanced_search.form.js b/js/advanced_search.form.js index 7e45dda..fa15cc4 100644 --- a/js/advanced_search.form.js +++ b/js/advanced_search.form.js @@ -81,6 +81,26 @@ return window.location.href.split("?")[0] + "?" + $.param(params); } + $.urlParam = function(url, name){ + var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url); + if (results !== null) + return results[1] || 0; + else { + return null; + } + } + + $.updateParam = function(urlstring, param, value) { + var url = new URL(urlstring); + var search_params = url.searchParams; + + search_params.set(param, value); + + url.search = search_params.toString(); + + return url.toString(); + } + Drupal.behaviors.advanced_search_form = { attach: function (context, settings) { if (settings.advanced_search_form.id !== 'undefined') { @@ -108,6 +128,38 @@ e.stopPropagation(); const inputs = $form.serializeArray(); const href = url(inputs, settings.advanced_search_form); + + /* digitalutsc added*/ + $("li.pager__item a.pager__itemsperpage").each(function( index ) { + // update pager links - items per page + var new_link = href; + if (href.includes("items_per_page=") === false) { + console.log("url has items_per_page"); + new_link = new_link + '&items_per_page=' + $(this).text(); + $( this ).attr("href", new_link); + } + else { + // replace with new param + new_link = updateParam(new_link, "items_per_page", $(this).text()); + $( this ).attr("href", new_link); + } + }); + + $("li.pager__item a.pager__display").each(function( index ) { + // update pager links - display + var new_link = href; + if (href.includes("display=") === false) { + console.log("url has display"); + new_link = new_link + '&display=' + $(this).text(); + $( this ).attr("href", new_link); + } + else { + // replace with new param + new_link = updateParam(new_link, "display", $(this).text()); + $( this ).attr("href", new_link); + } + }); + window.history.pushState(null, document.title, href); }); } @@ -115,7 +167,10 @@ $form.find('input[data-drupal-selector = "edit-reset"]').mousedown(function (e) { const inputs = []; const href = url(inputs, settings.advanced_search_form); - window.history.pushState(null, document.title, href); + window.history.pushState(null, document.title, href.split('?')[0] ); + + + window.location.replace(href.split('?')[0]); }); } } diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 6ca6ce8..a196f49 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -185,7 +185,7 @@ class SearchResultsPagerBlock extends BlockBase implements ContainerFactoryPlugi '#url' => $url, '#title' => $items_per_page, '#attributes' => [ - 'class' => $active ? ['pager__link', 'pager__link--is-active'] : ['pager__link'], + 'class' => $active ? ['pager__link', 'pager__link--is-active', 'pager__itemsperpage'] : ['pager__link', 'pager__itemsperpage'], ], '#wrapper_attributes' => [ 'class' => $active ? ['pager__item', 'is-active'] : ['pager__item'], @@ -236,7 +236,7 @@ class SearchResultsPagerBlock extends BlockBase implements ContainerFactoryPlugi '#url' => $url, '#title' => Markup::create($text), '#attributes' => [ - 'class' => $active ? ['pager__link', 'pager__link--is-active'] : ['pager__link'], + 'class' => $active ? ['pager__link', 'pager__link--is-active', 'pager__display'] : ['pager__link', 'pager__display'], ], '#wrapper_attributes' => [ 'class' => $active ? ['pager__item', 'is-active'] : ['pager__item'],