From 56bfcbba0e58dcff9e35af2967cafe5f6cb1bbf9 Mon Sep 17 00:00:00 2001 From: Laptop Date: Fri, 18 Oct 2024 13:16:32 +0300 Subject: [PATCH] less javascript (#621) --- Cargo.lock | 13 ++++++++++ public/static/index.js | 37 +------------------------- public/static/pagination.js | 39 ---------------------------- public/static/search_area_options.js | 18 ------------- public/static/themes/simple.css | 4 +-- src/server/routes/search.rs | 1 + src/templates/partials/bar.rs | 7 ++--- src/templates/partials/search_bar.rs | 9 ++++--- src/templates/views/search.rs | 8 +++--- 9 files changed, 30 insertions(+), 106 deletions(-) delete mode 100644 public/static/pagination.js delete mode 100644 public/static/search_area_options.js diff --git a/Cargo.lock b/Cargo.lock index c273074..fdce525 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3273,6 +3273,7 @@ dependencies = [ "sync_wrapper", "tokio 1.40.0", "tokio-rustls", + "tokio-socks", "tokio-util", "tower-service", "url 2.5.2", @@ -4103,6 +4104,18 @@ dependencies = [ "tokio 1.40.0", ] +[[package]] +name = "tokio-socks" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4770b8024672c1101b3f6733eab95b18007dbe0847a8afe341fcf79e06043f" +dependencies = [ + "either", + "futures-util", + "thiserror", + "tokio 1.40.0", +] + [[package]] name = "tokio-sync" version = "0.1.8" diff --git a/public/static/index.js b/public/static/index.js index c743497..6858a66 100644 --- a/public/static/index.js +++ b/public/static/index.js @@ -1,41 +1,6 @@ -/** - * Selects the input element for the search box - * @type {HTMLInputElement} - */ -const searchBox = document.querySelector('input') - -/** - * Redirects the user to the search results page with the query parameter - */ -function searchWeb() { - const query = searchBox.value.trim() - try { - let safeSearchLevel = document.querySelector('.search_options select').value - if (query) { - window.location.href = `search?q=${encodeURIComponent( - query, - )}&safesearch=${encodeURIComponent(safeSearchLevel)}` - } - } catch (error) { - if (query) { - window.location.href = `search?q=${encodeURIComponent(query)}` - } - } -} - -/** - * Listens for the 'Enter' key press event on the search box and calls the searchWeb function - * @param {KeyboardEvent} e - The keyboard event object - */ -searchBox.addEventListener('keyup', (e) => { - if (e.key === 'Enter') { - searchWeb() - } -}) - /** * A function that clears the search input text when the clear button is clicked. */ function clearSearchText() { - document.querySelector('.search_bar input').value = '' + document.querySelector('.search_bar > input').value = '' } diff --git a/public/static/pagination.js b/public/static/pagination.js deleted file mode 100644 index bdbfb39..0000000 --- a/public/static/pagination.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Navigates to the next page by incrementing the current page number in the URL query string. - * @returns {void} - */ -function navigate_forward() { - let url = new URL(window.location); - let searchParams = url.searchParams; - - let q = searchParams.get('q'); - let page = parseInt(searchParams.get('page')); - - if (isNaN(page)) { - page = 1; - } else { - page++; - } - - window.location.href = `${url.origin}${url.pathname}?q=${encodeURIComponent(q)}&page=${page}`; -} - -/** - * Navigates to the previous page by decrementing the current page number in the URL query string. - * @returns {void} - */ -function navigate_backward() { - let url = new URL(window.location); - let searchParams = url.searchParams; - - let q = searchParams.get('q'); - let page = parseInt(searchParams.get('page')); - - if (isNaN(page)) { - page = 0; - } else if (page > 0) { - page--; - } - - window.location.href = `${url.origin}${url.pathname}?q=${encodeURIComponent(q)}&page=${page}`; -} diff --git a/public/static/search_area_options.js b/public/static/search_area_options.js deleted file mode 100644 index 10e0390..0000000 --- a/public/static/search_area_options.js +++ /dev/null @@ -1,18 +0,0 @@ -document.addEventListener( - 'DOMContentLoaded', - () => { - let url = new URL(window.location) - let searchParams = url.searchParams - - let safeSearchLevel = searchParams.get('safesearch') - - if ( - safeSearchLevel >= 0 && - safeSearchLevel <= 2 && - safeSearchLevel !== null - ) { - document.querySelector('.search_options select').value = safeSearchLevel - } - }, - false, -) diff --git a/public/static/themes/simple.css b/public/static/themes/simple.css index dc8fcbc..36472a9 100644 --- a/public/static/themes/simple.css +++ b/public/static/themes/simple.css @@ -448,7 +448,7 @@ footer div { align-items: center; } -.page_navigation button { +.page_navigation a { background: var(--background-color); color: var(--foreground-color); padding: 1rem; @@ -457,7 +457,7 @@ footer div { border: none; } -.page_navigation button:active { +.page_navigation a:active { filter: brightness(1.2); } diff --git a/src/server/routes/search.rs b/src/server/routes/search.rs index e4f40de..883017f 100644 --- a/src/server/routes/search.rs +++ b/src/server/routes/search.rs @@ -129,6 +129,7 @@ pub async fn search( &config.style.theme, &config.style.animation, query, + page, &results.0, ) .0, diff --git a/src/templates/partials/bar.rs b/src/templates/partials/bar.rs index bf66c4c..a381186 100644 --- a/src/templates/partials/bar.rs +++ b/src/templates/partials/bar.rs @@ -14,12 +14,13 @@ use maud::{html, Markup, PreEscaped}; /// It returns the compiled html code for the search bar as a result. pub fn bar(query: &str) -> Markup { html!( + (PreEscaped("
")) (PreEscaped("
")) - input type="search" name="search-box" value=(query) placeholder="Type to search"; - button type="reset" onclick="clearSearchText()" { + input type="search" name="q" value=(query) placeholder="Type to search"; + button type="button" onclick="clearSearchText()" { img src="./images/close.svg" alt="Clear button icon for clearing search input text"; } - button type="submit" onclick="searchWeb()" { + button type="submit" { img src="./images/magnifying_glass.svg" alt="Info icon for error box"; } ) diff --git a/src/templates/partials/search_bar.rs b/src/templates/partials/search_bar.rs index 9c6ea9c..3c7478c 100644 --- a/src/templates/partials/search_bar.rs +++ b/src/templates/partials/search_bar.rs @@ -29,7 +29,7 @@ pub fn search_bar( (bar(query)) .error_box { @if !engine_errors_info.is_empty(){ - button onclick="toggleErrorBox()" class="error_box_toggle_button"{ + button type="button" onclick="toggleErrorBox()" class="error_box_toggle_button"{ img src="./images/warning.svg" alt="Info icon for error box"; } .dropdown_error_box{ @@ -43,7 +43,7 @@ pub fn search_bar( } } @else { - button onclick="toggleErrorBox()" class="error_box_toggle_button"{ + button type="button" onclick="toggleErrorBox()" class="error_box_toggle_button"{ img src="./images/info.svg" alt="Warning icon for error box"; } .dropdown_error_box { @@ -56,10 +56,10 @@ pub fn search_bar( (PreEscaped("
")) .search_options { @if safe_search_level >= 3 { - (PreEscaped("")) } @else{ - (PreEscaped("", safe_search_level))) } @for (idx, name) in SAFE_SEARCH_LEVELS_NAME.iter().enumerate() { @if (safe_search_level as usize) == idx { @@ -71,6 +71,7 @@ pub fn search_bar( } (PreEscaped("")) } + (PreEscaped("
")) } ) } diff --git a/src/templates/views/search.rs b/src/templates/views/search.rs index c5ab456..4ffa423 100644 --- a/src/templates/views/search.rs +++ b/src/templates/views/search.rs @@ -24,6 +24,7 @@ pub fn search( theme: &str, animation: &Option, query: &str, + page: u32, search_results: &SearchResults, ) -> Markup { html!( @@ -108,15 +109,14 @@ pub fn search( } } .page_navigation { - button type="button" onclick="navigate_backward()"{ + a href=(format!("/search?q={}&safesearch={}&page={}", query, search_results.safe_search_level, if page > 1 {page-1} else {1})) { (PreEscaped("←")) "previous" } - button type="button" onclick="navigate_forward()"{"next" (PreEscaped("→"))} + a href=(format!("/search?q={}&safesearch={}&page={}", query, search_results.safe_search_level, page+2)) { + "next" (PreEscaped("→"))} } } script src="static/index.js"{} - script src="static/search_area_options.js"{} - script src="static/pagination.js"{} script src="static/error_box.js"{} (footer()) )