Skip to content

Commit

Permalink
less javascript (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaptopCat authored Oct 18, 2024
1 parent d52da3a commit 56bfcbb
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 106 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 1 addition & 36 deletions public/static/index.js
Original file line number Diff line number Diff line change
@@ -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 = ''
}
39 changes: 0 additions & 39 deletions public/static/pagination.js

This file was deleted.

18 changes: 0 additions & 18 deletions public/static/search_area_options.js

This file was deleted.

4 changes: 2 additions & 2 deletions public/static/themes/simple.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -457,7 +457,7 @@ footer div {
border: none;
}

.page_navigation button:active {
.page_navigation a:active {
filter: brightness(1.2);
}

Expand Down
1 change: 1 addition & 0 deletions src/server/routes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub async fn search(
&config.style.theme,
&config.style.animation,
query,
page,
&results.0,
)
.0,
Expand Down
7 changes: 4 additions & 3 deletions src/templates/partials/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<form action=\"/search\">"))
(PreEscaped("<div class=\"search_bar\">"))
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";
}
)
Expand Down
9 changes: 5 additions & 4 deletions src/templates/partials/search_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand All @@ -56,10 +56,10 @@ pub fn search_bar(
(PreEscaped("</div>"))
.search_options {
@if safe_search_level >= 3 {
(PreEscaped("<select name=\"safe_search_levels\" disabled>"))
(PreEscaped("<select name=\"safesearch\" disabled>"))
}
@else{
(PreEscaped("<select name=\"safe_search_levels\">"))
(PreEscaped(format!("<select name=\"safesearch\" value=\"{}\">", safe_search_level)))
}
@for (idx, name) in SAFE_SEARCH_LEVELS_NAME.iter().enumerate() {
@if (safe_search_level as usize) == idx {
Expand All @@ -71,6 +71,7 @@ pub fn search_bar(
}
(PreEscaped("</select>"))
}
(PreEscaped("</form>"))
}
)
}
8 changes: 4 additions & 4 deletions src/templates/views/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn search(
theme: &str,
animation: &Option<String>,
query: &str,
page: u32,
search_results: &SearchResults,
) -> Markup {
html!(
Expand Down Expand Up @@ -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("&#8592;")) "previous"
}
button type="button" onclick="navigate_forward()"{"next" (PreEscaped("&#8594;"))}
a href=(format!("/search?q={}&safesearch={}&page={}", query, search_results.safe_search_level, page+2)) {
"next" (PreEscaped("&#8594;"))}
}
}
script src="static/index.js"{}
script src="static/search_area_options.js"{}
script src="static/pagination.js"{}
script src="static/error_box.js"{}
(footer())
)
Expand Down

0 comments on commit 56bfcbb

Please sign in to comment.