Browse Source

Fix Web UI search categories only working in English

Closes #10239.
adaptive-webui-19844
Thomas Piccirello 5 years ago
parent
commit
8e8cd59d90
  1. 43
      src/webui/api/searchcontroller.cpp
  2. 1
      src/webui/api/searchcontroller.h
  3. 43
      src/webui/www/private/views/search.html

43
src/webui/api/searchcontroller.cpp

@ -43,8 +43,6 @@
#include "apierror.h" #include "apierror.h"
#include "isessionmanager.h" #include "isessionmanager.h"
class SearchPluginManager;
using SearchHandlerPtr = QSharedPointer<SearchHandler>; using SearchHandlerPtr = QSharedPointer<SearchHandler>;
using SearchHandlerDict = QMap<int, SearchHandlerPtr>; using SearchHandlerDict = QMap<int, SearchHandlerPtr>;
@ -59,6 +57,32 @@ namespace
if (activeSearches.remove(id)) if (activeSearches.remove(id))
session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches)); session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches));
} }
/**
* Returns the search categories in JSON format.
*
* The return value is an array of dictionaries.
* The dictionary keys are:
* - "id"
* - "name"
*/
QJsonArray getPluginCategories(QStringList categories)
{
QJsonArray categoriesInfo {QJsonObject {
{QLatin1String("id"), "all"},
{QLatin1String("name"), SearchPluginManager::categoryFullName("all")}
}};
categories.sort(Qt::CaseInsensitive);
for (const QString &category : categories) {
categoriesInfo << QJsonObject {
{QLatin1String("id"), category},
{QLatin1String("name"), SearchPluginManager::categoryFullName(category)}
};
}
return categoriesInfo;
}
} }
void SearchController::startAction() void SearchController::startAction()
@ -201,19 +225,6 @@ void SearchController::deleteAction()
removeActiveSearch(session, id); removeActiveSearch(session, id);
} }
void SearchController::categoriesAction()
{
QStringList categories;
const QString name = params()["pluginName"].trimmed();
categories << SearchPluginManager::categoryFullName("all");
for (const QString &category : asConst(SearchPluginManager::instance()->getPluginCategories(name)))
categories << SearchPluginManager::categoryFullName(category);
const QJsonArray result = QJsonArray::fromStringList(categories);
setResult(result);
}
void SearchController::pluginsAction() void SearchController::pluginsAction()
{ {
const QStringList allPlugins = SearchPluginManager::instance()->allPlugins(); const QStringList allPlugins = SearchPluginManager::instance()->allPlugins();
@ -363,7 +374,7 @@ QJsonArray SearchController::getPluginsInfo(const QStringList &plugins) const
{"version", QString(pluginInfo->version)}, {"version", QString(pluginInfo->version)},
{"fullName", pluginInfo->fullName}, {"fullName", pluginInfo->fullName},
{"url", pluginInfo->url}, {"url", pluginInfo->url},
{"supportedCategories", QJsonArray::fromStringList(pluginInfo->supportedCategories)}, {"supportedCategories", getPluginCategories(pluginInfo->supportedCategories)},
{"enabled", pluginInfo->enabled} {"enabled", pluginInfo->enabled}
}; };
} }

1
src/webui/api/searchcontroller.h

@ -55,7 +55,6 @@ private slots:
void statusAction(); void statusAction();
void resultsAction(); void resultsAction();
void deleteAction(); void deleteAction();
void categoriesAction();
void pluginsAction(); void pluginsAction();
void installPluginAction(); void installPluginAction();
void uninstallPluginAction(); void uninstallPluginAction();

43
src/webui/www/private/views/search.html

@ -335,8 +335,6 @@
const plugins = $('pluginsSelect').getProperty('value'); const plugins = $('pluginsSelect').getProperty('value');
if (!pattern || !category || !plugins) return; if (!pattern || !category || !plugins) return;
if (category === "QBT_TR(All categories)QBT_TR[CONTEXT=SearchEngineWidget]")
category = "all";
resetFilters(); resetFilters();
@ -462,36 +460,43 @@
const populateCategorySelect = function(categories) { const populateCategorySelect = function(categories) {
const categoryHtml = []; const categoryHtml = [];
categories.each(function(category) { categories.each(function(category) {
categoryHtml.push("<option>" + category + "</option>"); const option = new Element("option");
option.set("value", category.id);
option.set("html", category.name);
categoryHtml.push(option.outerHTML);
}); });
// first category is "All Categories" // first category is "All Categories"
if (categoryHtml.length > 1) if (categoryHtml.length > 1) {
// add separator // add separator
categoryHtml.splice(1, 0, "<option disabled>──────────</option>"); const option = new Element("option");
option.set("disabled", true);
option.set("html", "──────────");
categoryHtml.splice(1, 0, option.outerHTML);
}
$('categorySelect').set('html', categoryHtml.join("")); $('categorySelect').set('html', categoryHtml.join(""));
}; };
const selectedPlugin = $('pluginsSelect').get("value"); const selectedPlugin = $('pluginsSelect').get("value");
if ((selectedPlugin === "all") || (selectedPlugin === "enabled")) { if ((selectedPlugin === "all") || (selectedPlugin === "enabled")) {
const url = new URI('api/v2/search/categories'); const uniqueCategories = {};
url.setData('name', selectedPlugin); for (const plugin of searchPlugins) {
new Request.JSON({ if ((selectedPlugin === "enabled") && !plugin.enabled) continue
url: url, for (const category of plugin.supportedCategories) {
noCache: true, if (uniqueCategories[category.id] === undefined) {
method: 'get', uniqueCategories[category.id] = category;
onSuccess: function(response) { }
populateCategorySelect(response); }
} }
}).send(); // we must sort the ids to maintain consistent order.
const categories = Object.keys(uniqueCategories).sort().map(id => uniqueCategories[id]);
populateCategorySelect(categories);
} }
else { else {
let plugins = ["QBT_TR(All categories)QBT_TR[CONTEXT=SearchEngineWidget]"];
const plugin = getPlugin(selectedPlugin); const plugin = getPlugin(selectedPlugin);
if (plugin !== null) const plugins = (plugin === null) ? [] : plugin.supportedCategories
plugins = plugins.concat(plugin.supportedCategories);
populateCategorySelect(plugins); populateCategorySelect(plugins);
} }

Loading…
Cancel
Save