Merge pull request #103 from alamin655/js
Browse filesStreamline search and navigation logic for better code maintainability
- public/static/index.js +21 -6
- public/static/pagination.js +27 -14
public/static/index.js
CHANGED
@@ -1,10 +1,25 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
}
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
7 |
if (e.key === 'Enter') {
|
8 |
-
|
9 |
}
|
10 |
-
})
|
|
|
1 |
+
/**
|
2 |
+
* Selects the input element for the search box
|
3 |
+
* @type {HTMLInputElement}
|
4 |
+
*/
|
5 |
+
const searchBox = document.querySelector('input');
|
6 |
+
|
7 |
+
/**
|
8 |
+
* Redirects the user to the search results page with the query parameter
|
9 |
+
*/
|
10 |
+
function searchWeb() {
|
11 |
+
const query = searchBox.value.trim();
|
12 |
+
if (query) {
|
13 |
+
window.location.href = `search?q=${encodeURIComponent(query)}`;
|
14 |
+
}
|
15 |
}
|
16 |
|
17 |
+
/**
|
18 |
+
* Listens for the 'Enter' key press event on the search box and calls the searchWeb function
|
19 |
+
* @param {KeyboardEvent} e - The keyboard event object
|
20 |
+
*/
|
21 |
+
searchBox.addEventListener('keyup', (e) => {
|
22 |
if (e.key === 'Enter') {
|
23 |
+
searchWeb();
|
24 |
}
|
25 |
+
});
|
public/static/pagination.js
CHANGED
@@ -1,26 +1,39 @@
|
|
|
|
|
|
|
|
|
|
1 |
function navigate_forward() {
|
2 |
-
const url = new URL(window.location)
|
3 |
-
const searchParams = url.searchParams
|
4 |
|
5 |
-
let q = searchParams.get('q')
|
6 |
-
let page = searchParams.get('page')
|
7 |
|
8 |
-
if (page
|
9 |
-
page =
|
10 |
-
window.location = `${url.origin}${url.pathname}?q=${q}&page=${page}`
|
11 |
} else {
|
12 |
-
|
13 |
}
|
|
|
|
|
14 |
}
|
15 |
|
|
|
|
|
|
|
|
|
16 |
function navigate_backward() {
|
17 |
-
const url = new URL(window.location)
|
18 |
-
const searchParams = url.searchParams
|
19 |
|
20 |
-
let q = searchParams.get('q')
|
21 |
-
let page = searchParams.get('page')
|
22 |
|
23 |
-
if (page
|
24 |
-
|
|
|
|
|
25 |
}
|
|
|
|
|
26 |
}
|
|
|
1 |
+
/**
|
2 |
+
* Navigates to the next page by incrementing the current page number in the URL query parameters.
|
3 |
+
* @returns {void}
|
4 |
+
*/
|
5 |
function navigate_forward() {
|
6 |
+
const url = new URL(window.location);
|
7 |
+
const searchParams = url.searchParams;
|
8 |
|
9 |
+
let q = searchParams.get('q');
|
10 |
+
let page = parseInt(searchParams.get('page'));
|
11 |
|
12 |
+
if (isNaN(page)) {
|
13 |
+
page = 1;
|
|
|
14 |
} else {
|
15 |
+
page++;
|
16 |
}
|
17 |
+
|
18 |
+
window.location.href = `${url.origin}${url.pathname}?q=${encodeURIComponent(q)}&page=${page}`;
|
19 |
}
|
20 |
|
21 |
+
/**
|
22 |
+
* Navigates to the previous page by decrementing the current page number in the URL query parameters.
|
23 |
+
* @returns {void}
|
24 |
+
*/
|
25 |
function navigate_backward() {
|
26 |
+
const url = new URL(window.location);
|
27 |
+
const searchParams = url.searchParams;
|
28 |
|
29 |
+
let q = searchParams.get('q');
|
30 |
+
let page = parseInt(searchParams.get('page'));
|
31 |
|
32 |
+
if (isNaN(page)) {
|
33 |
+
page = 1;
|
34 |
+
} else if (page > 1) {
|
35 |
+
page--;
|
36 |
}
|
37 |
+
|
38 |
+
window.location.href = `${url.origin}${url.pathname}?q=${encodeURIComponent(q)}&page=${page}`;
|
39 |
}
|