4 changed files with 107 additions and 0 deletions
@ -0,0 +1,69 @@
|
||||
(function ($, Drupal, once) { |
||||
'use strict'; |
||||
|
||||
Drupal.behaviors.preserveAdvancedSearchParams = { |
||||
attach: function (context, settings) { |
||||
|
||||
console.log('preserve-advanced-search.js loaded'); |
||||
|
||||
// Extract a[] params from the current page URL
|
||||
const rawSearch = window.location.search; |
||||
const aParams = rawSearch |
||||
.substring(1) |
||||
.split('&') |
||||
.filter(part => part.startsWith('a%5B') || part.startsWith('a[')) |
||||
.join('&'); |
||||
|
||||
console.log('[AdvSearch] aParams found:', aParams); |
||||
|
||||
// Nothing to preserve — bail out
|
||||
if (!aParams) return; |
||||
|
||||
// Target the <ul> that has data-drupal-facet-id
|
||||
// then find the .facet-slider inside it which is the noUiSlider element
|
||||
once('preserve-advsearch-slider', '[data-drupal-facet-id]', context) |
||||
.forEach(function (el) { |
||||
const facetId = el.dataset.drupalFacetId; |
||||
const sliderEl = el.querySelector('.facet-slider'); |
||||
|
||||
console.log('[AdvSearch] Found facet:', facetId, 'slider el:', sliderEl); |
||||
|
||||
if (!sliderEl || !sliderEl.noUiSlider) { |
||||
console.warn('[AdvSearch] No noUiSlider instance found on', sliderEl); |
||||
return; |
||||
} |
||||
|
||||
// Get the facet settings from drupalSettings
|
||||
const facetSettings = settings.facets && settings.facets[facetId]; |
||||
console.log('[AdvSearch] facetSettings:', facetSettings); |
||||
|
||||
if (!facetSettings) { |
||||
console.warn('[AdvSearch] No drupalSettings.facets entry for:', facetId); |
||||
return; |
||||
} |
||||
|
||||
// Remove original change listener and replace with patched version
|
||||
sliderEl.noUiSlider.off('change'); |
||||
sliderEl.noUiSlider.on('change', function (values, handle, unencoded) { |
||||
let newUrl; |
||||
|
||||
if (facetSettings.range) { |
||||
newUrl = facetSettings.url |
||||
.replace('__range_slider_min__', Math.round(unencoded[0])) |
||||
.replace('__range_slider_max__', Math.round(unencoded[1])); |
||||
} else { |
||||
newUrl = facetSettings.urls['f_' + values[0]]; |
||||
} |
||||
|
||||
console.log('[AdvSearch] Navigating to:', newUrl + '&' + aParams); |
||||
|
||||
const separator = newUrl.includes('?') ? '&' : '?'; |
||||
window.location.href = newUrl + separator + aParams; |
||||
}); |
||||
|
||||
console.log('[AdvSearch] Slider change handler patched successfully'); |
||||
}); |
||||
} |
||||
}; |
||||
|
||||
})(jQuery, Drupal, once); |
||||
@ -0,0 +1,29 @@
|
||||
diff --git a/olivesnews.libraries.yml b/olivesnews.libraries.yml
|
||||
index 75479c7..701e67f 100755
|
||||
--- a/olivesnews.libraries.yml
|
||||
+++ b/olivesnews.libraries.yml
|
||||
@@ -73,6 +73,14 @@ nouislider-year-format:
|
||||
dependencies:
|
||||
- core/drupal
|
||||
|
||||
+preserve-advanced-search:
|
||||
+ js:
|
||||
+ js/preserve-advanced-search.js: {}
|
||||
+ dependencies:
|
||||
+ - core/drupal
|
||||
+ - core/once
|
||||
+ - core/jquery
|
||||
+
|
||||
book:
|
||||
version: VERSION
|
||||
css:
|
||||
diff --git a/olivesnews.theme b/olivesnews.theme
|
||||
index e81fa1d..e1ceeaf 100755
|
||||
--- a/olivesnews.theme
|
||||
+++ b/olivesnews.theme
|
||||
@@ -747,4 +747,5 @@ function _olivesnews_hex_to_hsl(string $hex_string)
|
||||
function olivesnews_preprocess_page(&$variables)
|
||||
{
|
||||
$variables['#attached']['library'][] = 'olivesnews/nouislider-year-format';
|
||||
+ $variables['#attached']['library'][] = 'olivesnews/preserve-advanced-search';
|
||||
}
|
||||
Loading…
Reference in new issue