From 434e1a8f0caa685891f73cc229d1b2045ed37c9e Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 8 Nov 2023 14:35:28 +0100 Subject: [PATCH] Improve database selection when invoking package search from params * Select all relevant DBs, also when multiple `db` parameters are present * Avoid the architecture filter from interfering by simply selecting "All archs" when starting a package search from query params --- srv/static/js/packagesearchpage.js | 26 ++++++++++++++++---------- srv/static/js/utils.js | 15 +++++++++++++-- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/srv/static/js/packagesearchpage.js b/srv/static/js/packagesearchpage.js index c9d9afb..880c9a7 100644 --- a/srv/static/js/packagesearchpage.js +++ b/srv/static/js/packagesearchpage.js @@ -69,21 +69,27 @@ function searchForPackagesFromParams(searchParams) { const form = document.getElementById('package-search-form'); form.reset(); - for (const [key, value] of Object.entries(Utils.hashAsObject(searchParams))) { + const archFilter = document.getElementById('package-search-db-arch-filter'); + if (archFilter) { + archFilter.selectedIndex = 0; + archFilter.onchange(); + } + const params = Utils.hashAsObject(searchParams, true); + for (const [key, value] of Object.entries(params)) { const formElement = form[key]; if (!formElement) { - return; + continue; } - if (formElement.multiple) { - Array.from(formElement.options).forEach(function(optionElement) { - if (optionElement.value === value) { - optionElement.selected = true; - return; - } - }); - } else { + if (!formElement.multiple) { formElement.value = value; + continue; } + Array.from(formElement.options).forEach(function(optionElement) { + optionElement.selected = Array.isArray(value) ? value.includes(optionElement.value) : value === optionElement.value; + }); + } + if (!params.db) { + form.db.selectedIndex = 0; } const res = AjaxHelper.startFormQueryEx('package-search-form', showPackageSearchResults); SinglePageHelper.sections['package-search'].state.params = res.params; diff --git a/srv/static/js/utils.js b/srv/static/js/utils.js index 0c75254..a0a4b25 100644 --- a/srv/static/js/utils.js +++ b/srv/static/js/utils.js @@ -9,7 +9,7 @@ export function splitHashParts() return hashParts; } -export function hashAsObject(hash) +export function hashAsObject(hash, multipleValuesAsArray) { const hashObject = {}; (hash || location.hash.substr(1)).split('&').forEach(function(hashPart) { @@ -17,7 +17,18 @@ export function hashAsObject(hash) if (parts.length < 1) { return; } - hashObject[decodeURIComponent(parts[0])] = parts.length > 1 ? decodeURIComponent(parts[1]) : undefined; + const key = decodeURIComponent(parts[0]); + const thisValue = parts.length > 1 ? decodeURIComponent(parts[1]) : undefined; + const existingValue = hashObject[key]; + if (multipleValuesAsArray && existingValue !== undefined) { + if (Array.isArray(existingValue)) { + existingValue.push(thisValue); + } else { + hashObject[key] = [existingValue, thisValue]; + } + } else { + hashObject[key] = thisValue; + } }); return hashObject; }