158 lines
4.3 KiB
JavaScript
158 lines
4.3 KiB
JavaScript
"use strict";
|
|
|
|
let query = (new URL(location)).searchParams.get("query")
|
|
, epoch = Math.floor(Date.now() / 1000)
|
|
;
|
|
let orderBy, ascending; // use `setOrderBy()` to modify orderBy
|
|
let lastOrderedValue, lastID;
|
|
|
|
|
|
window.onload = function () {
|
|
if (query !== null && query !== "") {
|
|
orderBy = "RELEVANCE";
|
|
}
|
|
|
|
const title = document.getElementsByTagName("title")[0];
|
|
if (query) {
|
|
title.textContent = query + " - magneticow";
|
|
const input = document.getElementsByTagName("input")[0];
|
|
input.setAttribute("value", query);
|
|
|
|
setOrderBy("RELEVANCE");
|
|
} else {
|
|
title.textContent = "Most recent torrents - magneticow";
|
|
|
|
ascending = false;
|
|
setOrderBy("DISCOVERED_ON");
|
|
}
|
|
|
|
const feedAnchor = document.getElementById("feed-anchor");
|
|
const sortDropdown = document.getElementById("sort-dropdown");
|
|
if (query) {
|
|
feedAnchor.setAttribute("href", "/feed?query=" + encodeURIComponent(query));
|
|
} else {
|
|
sortDropdown.selectedIndex = 3
|
|
}
|
|
|
|
const queryInput = document.getElementById("query");
|
|
queryInput.onchange = sortDropdown.onchange = function () {
|
|
const ul = document.querySelector("main ul");
|
|
|
|
query = queryInput.value
|
|
switch (sortDropdown.selectedIndex) {
|
|
case 0:
|
|
case 1:
|
|
setOrderBy("TOTAL_SIZE")
|
|
break;
|
|
case 2:
|
|
case 3:
|
|
setOrderBy("DISCOVERED_ON")
|
|
break;
|
|
case 4:
|
|
case 5:
|
|
setOrderBy("N_FILES")
|
|
break;
|
|
}
|
|
|
|
ascending = sortDropdown.selectedIndex % 2 === 0
|
|
|
|
ul.innerHTML = ""
|
|
lastID = lastOrderedValue = null
|
|
load(queryInput.value);
|
|
};
|
|
|
|
load(query);
|
|
};
|
|
|
|
|
|
function setOrderBy(x) {
|
|
const validValues = [
|
|
"TOTAL_SIZE",
|
|
"DISCOVERED_ON",
|
|
"UPDATED_ON",
|
|
"N_FILES",
|
|
"N_SEEDERS",
|
|
"N_LEECHERS",
|
|
"RELEVANCE"
|
|
];
|
|
if (!validValues.includes(x)) {
|
|
throw new Error("invalid value for @orderBy");
|
|
}
|
|
orderBy = x;
|
|
}
|
|
|
|
function orderedValue(torrent) {
|
|
if (orderBy === "TOTAL_SIZE") return torrent.size;
|
|
else if (orderBy === "DISCOVERED_ON") return torrent.discoveredOn;
|
|
else if (orderBy === "UPDATED_ON") alert("implement it server side first!");
|
|
else if (orderBy === "N_FILES") return torrent.nFiles;
|
|
else if (orderBy === "N_SEEDERS") alert("implement it server side first!");
|
|
else if (orderBy === "N_LEECHERS") alert("implement it server side first!");
|
|
else if (orderBy === "RELEVANCE") return torrent.relevance;
|
|
}
|
|
|
|
|
|
function load(queryParam) {
|
|
const button = document.getElementsByTagName("button")[0];
|
|
button.textContent = "Loading More Results...";
|
|
button.setAttribute("disabled", ""); // disable the button whilst loading...
|
|
|
|
if (queryParam == null) {
|
|
queryParam = query
|
|
}
|
|
|
|
const ul = document.querySelector("main ul");
|
|
const template = document.getElementById("item-template").innerHTML;
|
|
const reqURL = "/api/v0.1/torrents?" + encodeQueryData({
|
|
query: queryParam,
|
|
epoch: epoch,
|
|
lastID: lastID,
|
|
lastOrderedValue: lastOrderedValue,
|
|
orderBy: orderBy,
|
|
ascending: ascending
|
|
});
|
|
|
|
console.log("reqURL", reqURL);
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
function disableButtonWithMsg(msg) {
|
|
button.textContent = msg;
|
|
button.setAttribute("disabled", "");
|
|
}
|
|
|
|
req.onreadystatechange = function () {
|
|
if (req.readyState !== 4)
|
|
return;
|
|
|
|
disableButtonWithMsg("Load More Results")
|
|
|
|
if (req.status !== 200)
|
|
alert(req.responseText);
|
|
|
|
let torrents = JSON.parse(req.responseText);
|
|
if (torrents.length === 0) {
|
|
disableButtonWithMsg("No More Results")
|
|
return;
|
|
}
|
|
|
|
const last = torrents[torrents.length - 1];
|
|
lastID = last.id;
|
|
lastOrderedValue = orderedValue(last);
|
|
|
|
for (let t of torrents) {
|
|
t.size = fileSize(t.size);
|
|
t.discoveredOn = humaniseDate(t.discoveredOn);
|
|
|
|
ul.innerHTML += Mustache.render(template, t);
|
|
}
|
|
|
|
if (torrents.length < 20) {
|
|
disableButtonWithMsg("No More Results");
|
|
}
|
|
};
|
|
|
|
req.open("GET", reqURL);
|
|
req.send();
|
|
}
|