mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-22 05:58:21 -05:00
Merge pull request #103 from alamin655/js
Streamline search and navigation logic for better code maintainability
This commit is contained in:
commit
e8935bc52d
@ -1,10 +1,25 @@
|
|||||||
let search_box = document.querySelector('input')
|
/**
|
||||||
function search_web() {
|
* Selects the input element for the search box
|
||||||
window.location = `search?q=${search_box.value}`
|
* @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();
|
||||||
|
if (query) {
|
||||||
|
window.location.href = `search?q=${encodeURIComponent(query)}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
search_box.addEventListener('keyup', (e) => {
|
/**
|
||||||
|
* 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') {
|
if (e.key === 'Enter') {
|
||||||
search_web()
|
searchWeb();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
@ -1,26 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Navigates to the next page by incrementing the current page number in the URL query parameters.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
function navigate_forward() {
|
function navigate_forward() {
|
||||||
const url = new URL(window.location)
|
const url = new URL(window.location);
|
||||||
const searchParams = url.searchParams
|
const searchParams = url.searchParams;
|
||||||
|
|
||||||
let q = searchParams.get('q')
|
let q = searchParams.get('q');
|
||||||
let page = searchParams.get('page')
|
let page = parseInt(searchParams.get('page'));
|
||||||
|
|
||||||
if (page === null) {
|
if (isNaN(page)) {
|
||||||
page = 2
|
page = 1;
|
||||||
window.location = `${url.origin}${url.pathname}?q=${q}&page=${page}`
|
|
||||||
} else {
|
} else {
|
||||||
window.location = `${url.origin}${url.pathname}?q=${q}&page=${++page}`
|
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 parameters.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
function navigate_backward() {
|
function navigate_backward() {
|
||||||
const url = new URL(window.location)
|
const url = new URL(window.location);
|
||||||
const searchParams = url.searchParams
|
const searchParams = url.searchParams;
|
||||||
|
|
||||||
let q = searchParams.get('q')
|
let q = searchParams.get('q');
|
||||||
let page = searchParams.get('page')
|
let page = parseInt(searchParams.get('page'));
|
||||||
|
|
||||||
if (page !== null && page > 1) {
|
if (isNaN(page)) {
|
||||||
window.location = `${url.origin}${url.pathname}?q=${q}&page=${--page}`
|
page = 1;
|
||||||
|
} else if (page > 1) {
|
||||||
|
page--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.location.href = `${url.origin}${url.pathname}?q=${encodeURIComponent(q)}&page=${page}`;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user