Preserves the advanced search query parameters when using both the advanced search module and the nouislider module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

59 lines
2.0 KiB

(function (Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.preserveAdvancedSearchParams = {
attach: function (context, settings) {
// Capture the a[] advanced search params from the current URL
const rawSearch = window.location.search;
const aParams = rawSearch
.substring(1)
.split('&')
.filter(part => part.startsWith('a%5B') || part.startsWith('a['))
.join('&');
if (!aParams) return;
const sliders = settings.facets && settings.facets.sliders;
if (!sliders) return;
// Replace any a[] params in a URL with the current page's a[] params.
// The current URL is always the source of truth — the server-built
// slider URL may contain stale or partial a[] params.
function rebuildUrl(url) {
if (!url) return url;
const parts = url.split('?');
const basePath = parts[0];
const query = parts[1] || '';
// Keep everything that is NOT an a[] param (i.e. the f[] facet params)
const keptParams = query
.split('&')
.filter(p => p.length > 0)
.filter(p => !p.startsWith('a%5B') && !p.startsWith('a['))
.join('&');
const finalQuery = [keptParams, aParams].filter(Boolean).join('&');
return basePath + '?' + finalQuery;
}
// Range sliders expose a single `url`; single-handle sliders expose a
// `urls` map keyed by value. Handle both — which one is present depends
// on the widget the facet is configured with.
Object.keys(sliders).forEach(function (key) {
const sliderSettings = sliders[key];
if (sliderSettings.url) {
sliderSettings.url = rebuildUrl(sliderSettings.url);
}
if (sliderSettings.urls) {
Object.keys(sliderSettings.urls).forEach(function (urlKey) {
sliderSettings.urls[urlKey] = rebuildUrl(sliderSettings.urls[urlKey]);
});
}
});
}
};
})(Drupal, drupalSettings);