111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
import { fetchQuery } from "./contentUtility.js";
|
|
|
|
const baseURL = "http://" + window.location.host + "/api/v1/datasets";
|
|
const defaultPagingValue = 20;
|
|
const lastQuery = {
|
|
url: "",
|
|
totalPages: 0,
|
|
currentPage: 0
|
|
};
|
|
|
|
// definition of all buttons
|
|
const addButton = document.getElementById("add-btn");
|
|
addButton.addEventListener("click", () => {
|
|
navigateToAdd();
|
|
});
|
|
|
|
const filterButton = document.getElementById("filter-btn");
|
|
filterButton.addEventListener("change", () => {
|
|
const filterString = filterButton.value;
|
|
filter(filterString);
|
|
});
|
|
|
|
const searchButton = document.getElementById("search-btn");
|
|
searchButton.addEventListener("click", () => {
|
|
const searchString = searchBar.value;
|
|
search(searchString);
|
|
});
|
|
|
|
const searchBar = document.getElementById("search-entry");
|
|
export let searchBarTimeout;
|
|
searchBar.addEventListener("input", () => {
|
|
clearTimeout(searchBarTimeout);
|
|
searchBarTimeout = setTimeout(() => {
|
|
const searchString = searchBar.value;
|
|
search(searchString);
|
|
}, 1000);
|
|
});
|
|
searchBar.addEventListener('keypress', function (e) {
|
|
if (e.key === 'Enter') {
|
|
const searchString = searchBar.value;
|
|
search(searchString);
|
|
}
|
|
})
|
|
|
|
const sortButton = document.getElementById("sort-btn");
|
|
sortButton.addEventListener("change", () => {
|
|
const sortString = sortButton.value;
|
|
sort(sortString);
|
|
});
|
|
|
|
const upvoteButtons = document.getElementsByClassName("upvote-btn");
|
|
const upvoteButtonClickListener = e => {
|
|
const entryID = e.target.parentElement.parentElement.dataset.id;
|
|
vote(entryID, true);
|
|
};
|
|
for (const upvoteButton of upvoteButtons) {
|
|
upvoteButton.addEventListener("click", upvoteButtonClickListener);
|
|
}
|
|
|
|
const downvoteButtons = document.getElementsByClassName("downvote-btn");
|
|
const downvoteButtonClickListener = e => {
|
|
const entryID = e.target.parentElement.parentElement.dataset.id;
|
|
vote(entryID, false);
|
|
};
|
|
for (const downvoteButton of downvoteButtons) {
|
|
downvoteButton.addEventListener("click", downvoteButtonClickListener);
|
|
}
|
|
|
|
// functions of the main page
|
|
function navigateToAdd() {
|
|
//TODO: url to add page not yet implemented, add here
|
|
}
|
|
|
|
function filter(filterString) {
|
|
filterString = filterString.toUpperCase();
|
|
const fetchURL = baseURL + "?type=" + encodeURIComponent(filterString) + "&size=" + defaultPagingValue;
|
|
console.log(fetchURL)
|
|
fetchQuery(fetchURL);
|
|
}
|
|
|
|
function search(searchString) {
|
|
const fetchURL = baseURL + "/search" + "?search=" + encodeURIComponent(searchString.length === 0?"%":searchString);
|
|
console.log(fetchURL);
|
|
fetchQuery(fetchURL);
|
|
}
|
|
|
|
function sort(sortString) {
|
|
let query = sortString.toLowerCase().split(" ");
|
|
if (query[1] === "a-z" || query[1] === "↑") {
|
|
query[1] = "asc";
|
|
} else {
|
|
query[1] = "desc";
|
|
}
|
|
const fetchURL = baseURL + "?sort=" + query[0] + "&direction=" + query[1];
|
|
console.log(fetchURL);
|
|
fetchQuery(fetchURL);
|
|
}
|
|
|
|
function vote(entryID, up) {
|
|
console.log(baseURL + "/id/" + entryID + "/" + (up?"upvote":"downvote"));
|
|
fetch(baseURL + "/id/" + entryID + "/" + up?"upvote":"downvote");
|
|
}
|
|
|
|
function incrementPageCount() {
|
|
lastQuery.currentPage++;
|
|
}
|
|
|
|
|
|
|
|
|