mirror of https://github.com/kiwix/libkiwix.git
language/category filtering in /nojs endpoint
Adds language and category filter in /nojs. Unlike the main page, the filtering is only done after user submits the form.
This commit is contained in:
parent
f843ea48f0
commit
37aadb86fb
|
@ -120,6 +120,8 @@ class Filter {
|
||||||
Filter& maxSize(size_t size);
|
Filter& maxSize(size_t size);
|
||||||
Filter& query(std::string query, bool partial=true);
|
Filter& query(std::string query, bool partial=true);
|
||||||
Filter& name(std::string name);
|
Filter& name(std::string name);
|
||||||
|
Filter& clearLang();
|
||||||
|
Filter& clearCategory();
|
||||||
|
|
||||||
bool hasQuery() const;
|
bool hasQuery() const;
|
||||||
const std::string& getQuery() const { return _query; }
|
const std::string& getQuery() const { return _query; }
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "libkiwix-resources.h"
|
#include "libkiwix-resources.h"
|
||||||
#include "tools/otherTools.h"
|
#include "tools/otherTools.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
|
#include "tools/regexTools.h"
|
||||||
|
|
||||||
namespace kiwix
|
namespace kiwix
|
||||||
{
|
{
|
||||||
|
@ -18,6 +19,13 @@ HTMLDumper::~HTMLDumper()
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
std::string humanFriendlyTitle(std::string title)
|
||||||
|
{
|
||||||
|
std::string humanFriendlyString = replaceRegex(title, "_", " ");
|
||||||
|
humanFriendlyString[0] = toupper(humanFriendlyString[0]);
|
||||||
|
return humanFriendlyString;
|
||||||
|
}
|
||||||
|
|
||||||
kainjow::mustache::list getTagList(std::string tags)
|
kainjow::mustache::list getTagList(std::string tags)
|
||||||
{
|
{
|
||||||
const auto tagsList = kiwix::split(tags, ";", true, false);
|
const auto tagsList = kiwix::split(tags, ";", true, false);
|
||||||
|
@ -38,6 +46,22 @@ std::string HTMLDumper::dumpPlainHTML(kiwix::Filter filter) const
|
||||||
kainjow::mustache::list booksData;
|
kainjow::mustache::list booksData;
|
||||||
const auto filteredBooks = library->filter(filter);
|
const auto filteredBooks = library->filter(filter);
|
||||||
const auto searchQuery = filter.getQuery();
|
const auto searchQuery = filter.getQuery();
|
||||||
|
auto languages = getLanguageData();
|
||||||
|
auto categories = getCategoryData();
|
||||||
|
|
||||||
|
for (auto &category : categories) {
|
||||||
|
const auto categoryName = category.get("name")->string_value();
|
||||||
|
if (categoryName == filter.getCategory()) {
|
||||||
|
category["selected"] = true;
|
||||||
|
}
|
||||||
|
category["hf_name"] = humanFriendlyTitle(categoryName);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &language : languages) {
|
||||||
|
if (language.get("lang_code")->string_value() == filter.getLang()) {
|
||||||
|
language["selected"] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for ( const auto& bookId : filteredBooks ) {
|
for ( const auto& bookId : filteredBooks ) {
|
||||||
const auto bookObj = library->getBookById(bookId);
|
const auto bookObj = library->getBookById(bookId);
|
||||||
|
@ -70,7 +94,9 @@ std::string HTMLDumper::dumpPlainHTML(kiwix::Filter filter) const
|
||||||
{"root", rootLocation},
|
{"root", rootLocation},
|
||||||
{"books", booksData },
|
{"books", booksData },
|
||||||
{"searchQuery", searchQuery},
|
{"searchQuery", searchQuery},
|
||||||
{"resultsCount", to_string(filteredBooks.size())}
|
{"resultsCount", to_string(filteredBooks.size())},
|
||||||
|
{"languages", languages},
|
||||||
|
{"categories", categories}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -878,6 +878,18 @@ Filter& Filter::name(std::string name)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Filter& Filter::clearLang()
|
||||||
|
{
|
||||||
|
activeFilters &= ~LANG;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Filter& Filter::clearCategory()
|
||||||
|
{
|
||||||
|
activeFilters &= ~CATEGORY;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
#define ACTIVE(X) (activeFilters & (X))
|
#define ACTIVE(X) (activeFilters & (X))
|
||||||
#define FILTER(TAG, TEST) if (ACTIVE(TAG) && !(TEST)) { return false; }
|
#define FILTER(TAG, TEST) if (ACTIVE(TAG) && !(TEST)) { return false; }
|
||||||
bool Filter::hasQuery() const
|
bool Filter::hasQuery() const
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include "tools/stringTools.h"
|
#include "tools/stringTools.h"
|
||||||
#include "tools/otherTools.h"
|
#include "tools/otherTools.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include "tools/regexTools.h"
|
|
||||||
|
|
||||||
namespace kiwix
|
namespace kiwix
|
||||||
{
|
{
|
||||||
|
|
|
@ -783,7 +783,17 @@ std::unique_ptr<Response> InternalServer::handle_no_js(const RequestContext& req
|
||||||
std::string content;
|
std::string content;
|
||||||
|
|
||||||
if (urlParts.size() == 1) {
|
if (urlParts.size() == 1) {
|
||||||
const auto filter = get_search_filter(request);
|
auto filter = get_search_filter(request);
|
||||||
|
try {
|
||||||
|
if (request.get_argument("category") == "") {
|
||||||
|
filter.clearCategory();
|
||||||
|
}
|
||||||
|
} catch (...) {}
|
||||||
|
try {
|
||||||
|
if (request.get_argument("lang") == "") {
|
||||||
|
filter.clearLang();
|
||||||
|
}
|
||||||
|
} catch (...) {}
|
||||||
content = htmlDumper.dumpPlainHTML(filter);
|
content = htmlDumper.dumpPlainHTML(filter);
|
||||||
} else if ((urlParts.size() == 3) && (urlParts[1] == "download")) {
|
} else if ((urlParts.size() == 3) && (urlParts[1] == "download")) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -66,6 +66,24 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class='kiwixNav'>
|
<div class='kiwixNav'>
|
||||||
|
<div class="kiwixNav__filters">
|
||||||
|
<div class="kiwixNav__select">
|
||||||
|
<select name="lang" id="languageFilter" class='kiwixNav__kiwixFilter filter' form="kiwixSearchForm">
|
||||||
|
<option value="" selected>All languages</option>
|
||||||
|
{{#languages}}
|
||||||
|
<option value="{{lang_code}}"{{#selected}} selected {{/selected}}>{{lang_self_name}}</option>
|
||||||
|
{{/languages}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="kiwixNav__select">
|
||||||
|
<select name="category" id="categoryFilter" class='kiwixNav__kiwixFilter filter' form="kiwixSearchForm">
|
||||||
|
<option value="">All categories</option>
|
||||||
|
{{#categories}}
|
||||||
|
<option value="{{name}}"{{#selected}} selected {{/selected}}>{{hf_name}}</option>
|
||||||
|
{{/categories}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<form id='kiwixSearchForm' class='kiwixNav__SearchForm' action="{{root}}/nojs">
|
<form id='kiwixSearchForm' class='kiwixNav__SearchForm' action="{{root}}/nojs">
|
||||||
<input type="text" name="q" placeholder="Search" id="searchFilter" class='kiwixSearch filter' value="{{searchQuery}}">
|
<input type="text" name="q" placeholder="Search" id="searchFilter" class='kiwixSearch filter' value="{{searchQuery}}">
|
||||||
<input type="submit" class="kiwixButton kiwixButtonHover" value="Search"/>
|
<input type="submit" class="kiwixButton kiwixButtonHover" value="Search"/>
|
||||||
|
|
Loading…
Reference in New Issue