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
This commit is contained in:
Martchus 2023-11-08 14:35:28 +01:00
parent f0d9b684df
commit 434e1a8f0c
2 changed files with 29 additions and 12 deletions

View File

@ -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;

View File

@ -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;
}