From 9d5abd7e351d01e7b6cca9442f9c594a7d33bc16 Mon Sep 17 00:00:00 2001 From: Manan Jethwani Date: Tue, 11 May 2021 20:25:05 +0530 Subject: [PATCH] replaced topbar with custom topbar --- src/server/internalServer.cpp | 2 +- src/server/internalServer.h | 2 +- src/server/response.cpp | 8 +-- src/server/response.h | 4 +- static/skin/index.js | 63 +++++++++++++++---- static/templates/index.html | 114 ++++++++++++++++++---------------- 6 files changed, 118 insertions(+), 75 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index f28c3df03..d8bb9d003 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -325,7 +325,7 @@ InternalServer::get_matching_if_none_match_etag(const RequestContext& r) const std::unique_ptr InternalServer::build_homepage(const RequestContext& request) { - return ContentResponse::build(*this, RESOURCE::templates::index_html, homepage_data(), "text/html; charset=utf-8"); + return ContentResponse::build(*this, RESOURCE::templates::index_html, homepage_data(), "text/html; charset=utf-8", true); } std::unique_ptr InternalServer::handle_meta(const RequestContext& request) diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 318f4d426..5d429fcab 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -107,7 +107,7 @@ class InternalServer { std::string m_server_id; friend std::unique_ptr Response::build(const InternalServer& server); - friend std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype); + friend std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage); friend std::unique_ptr ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item); friend std::unique_ptr Response::build_500(const InternalServer& server, const std::string& msg); diff --git a/src/server/response.cpp b/src/server/response.cpp index 292b55e5a..7c61aabf5 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -349,21 +349,21 @@ ContentResponse::ContentResponse(const std::string& root, bool verbose, bool wit add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType); } -std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype) +std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage) { return std::unique_ptr(new ContentResponse( server.m_root, server.m_verbose.load(), - server.m_withTaskbar, + server.m_withTaskbar && !isHomePage, server.m_withLibraryButton, server.m_blockExternalLinks, content, mimetype)); } -std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype) { +std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype, bool isHomePage) { auto content = render_template(template_str, data); - return ContentResponse::build(server, content, mimetype); + return ContentResponse::build(server, content, mimetype, isHomePage); } ItemResponse::ItemResponse(bool verbose, const zim::Item& item, const std::string& mimetype, const ByteRange& byterange) : diff --git a/src/server/response.h b/src/server/response.h index 76bb82f7a..8d113ad7f 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -79,8 +79,8 @@ class Response { class ContentResponse : public Response { public: ContentResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype); - static std::unique_ptr build(const InternalServer& server, const std::string& content, const std::string& mimetype); - static std::unique_ptr build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype); + static std::unique_ptr build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage = false); + static std::unique_ptr build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype, bool isHomePage = false); void set_taskbar(const std::string& bookName, const std::string& bookTitle); diff --git a/static/skin/index.js b/static/skin/index.js index 67745d911..b8c4fae49 100644 --- a/static/skin/index.js +++ b/static/skin/index.js @@ -1,13 +1,24 @@ const root = $( `link[type='root']` ).attr("href"); let viewPortHeight = window.innerHeight; -let count = Math.floor(viewPortHeight/100 + 1) * 3; let isFetching = false; let isEnd = false; -let prevStart = 0; +const params = new URLSearchParams(window.location.search); +const basicConfig = { + 'start': 0, + 'count': Math.floor(viewPortHeight/100 + 1) * 3 +}; -async function loadAndDisplay(query, append = false) { +function queryUrlBuilder() { + let url = `${root}/catalog/search?` + Object.keys(basicConfig).forEach((key, idx) => { + url += `${key}=${basicConfig[key]}${idx !== Object.keys(basicConfig).length - 1 ? '&' : ''}`; + }); + return url + (params.toString() ? `&${params.toString()}` : ''); +} + +async function loadAndDisplayBooks(append = false) { isFetching = true; - await fetch(`${root}/catalog/search${query}`) + await fetch(queryUrlBuilder()) .then(async (resp) => { const data = new window.DOMParser().parseFromString(await resp.text(), 'application/xml'); const books = data.querySelectorAll("entry"); @@ -31,6 +42,29 @@ async function loadAndDisplay(query, append = false) { }); } +async function loadAndDisplayOptions(nodeQuery, query) { + // currently taking an array in place of query, will replace it with query while fetching data from backend later on. + query.forEach((option) => { + let value = Object.keys(option)[0]; + let label = option[value]; + document.querySelector(nodeQuery).innerHTML += ``; + }) +} + +async function filterBooks(filterType, filterValue) { + isEnd = false; + isFetching = false; + basicConfig['start'] = 0; + const params = new URLSearchParams(window.location.search); + if (!filterValue) { + params.delete(filterType); + } else { + params.set(filterType, filterValue); + } + window.location.search = params.toString(); + loadAndDisplayBooks(); +} + function getInnerHtml(node, query) { return node.querySelector(query).innerHTML; } @@ -38,21 +72,24 @@ function getInnerHtml(node, query) { window.addEventListener('resize', async () => { if (isFetching || isEnd) return; viewPortHeight = window.innerHeight; - count = Math.floor(viewPortHeight/100 + 1) * 3; - start = prevStart + count; - loadAndDisplay(`?start=${start}&count=${count}`, true); - prevStart = start; -}) + basicConfig['count'] = Math.floor(viewPortHeight/100 + 1) * 3; + basicConfig['start'] = basicConfig['start'] + basicConfig['count']; + loadAndDisplayBooks(true); +}); window.addEventListener('scroll', async () => { if (isFetching || isEnd) return; if (viewPortHeight + window.scrollY >= document.body.offsetHeight) { - start = prevStart + count; - loadAndDisplay(`?start=${start}&count=${count}`, true); - prevStart = start; + basicConfig['start'] = basicConfig['start'] + basicConfig['count']; + loadAndDisplayBooks(true); } }); window.onload = async (event) => { - loadAndDisplay(`?start=0&count=${count}`); + loadAndDisplayBooks(); + loadAndDisplayOptions('#languageFilter', [{'eng': 'English'}, {'fra': 'french'}, {'ara': 'arab'}, {'hin': 'Hindi'}]); + loadAndDisplayOptions('#categoryFilter', [{'stack_exchange': 'stack exchange'}, {'wikipedia': 'wikipedia'}, {'phet': 'phet'}, {'youtube': 'youtube'}]); + for (const key of params.keys()) { + document.getElementsByName(key)[0].value = params.get(key); + } } \ No newline at end of file diff --git a/static/templates/index.html b/static/templates/index.html index 1994c327e..1eae2538c 100644 --- a/static/templates/index.html +++ b/static/templates/index.html @@ -1,57 +1,63 @@ - - - - Welcome to Kiwix Server - - - - - - - - - -
-
-
- -
- Powered by Kiwix -
- - + + + + Welcome to Kiwix Server + + + + + + + + +
+ + + +
+
+
+
+
+ Powered by Kiwix +
+