forked from security/hakurei
137 lines
4.2 KiB
JavaScript
137 lines
4.2 KiB
JavaScript
class PackageIndexEntry {
|
|
name;
|
|
description;
|
|
website;
|
|
version;
|
|
report;
|
|
}
|
|
function toHTML(entry) {
|
|
let v = entry.version != null ? `<span>${escapeHtml(entry.version)}</span>` : "";
|
|
let d = entry.description != null ? `<p>${escapeHtml(entry.description)}</p>` : "";
|
|
let w = entry.website != null ? `<a href="${encodeURI(entry.website)}">Website</a>` : "";
|
|
let r = entry.report != null ? `<a href="${encodeURI(entry.report)}">Log</a>` : "";
|
|
let row = (document.createElement('tr'));
|
|
row.innerHTML = `<td>
|
|
<h2>${escapeHtml(entry.name)} ${v}</h2>
|
|
${d}
|
|
${w}
|
|
${r}
|
|
</td>`;
|
|
return row;
|
|
}
|
|
const API_VERSION = 1;
|
|
const ENDPOINT = `/api/v${API_VERSION}`;
|
|
class InfoPayload {
|
|
count;
|
|
hakurei_version;
|
|
}
|
|
async function infoRequest() {
|
|
const res = await fetch(`${ENDPOINT}/info`);
|
|
const res_1 = await res.json();
|
|
return res_1;
|
|
}
|
|
class GetPayload {
|
|
count;
|
|
values;
|
|
}
|
|
var SortOrders;
|
|
(function (SortOrders) {
|
|
SortOrders[SortOrders["DeclarationAscending"] = 0] = "DeclarationAscending";
|
|
SortOrders[SortOrders["DeclarationDescending"] = 1] = "DeclarationDescending";
|
|
SortOrders[SortOrders["NameAscending"] = 2] = "NameAscending";
|
|
SortOrders[SortOrders["NameDescending"] = 3] = "NameDescending";
|
|
})(SortOrders || (SortOrders = {}));
|
|
async function getRequest(limit, index, sort) {
|
|
const res = await fetch(`${ENDPOINT}/get?limit=${limit}&index=${index}&sort=${sort.valueOf()}`);
|
|
const res_1 = await res.json();
|
|
return res_1;
|
|
}
|
|
class State {
|
|
entriesPerPage = 10;
|
|
currentPage = 1;
|
|
entryIndex = 0;
|
|
maxEntries = 100;
|
|
getEntriesPerPage() {
|
|
return this.entriesPerPage;
|
|
}
|
|
setEntriesPerPage(entriesPerPage) {
|
|
this.entriesPerPage = entriesPerPage;
|
|
if (this.currentPage > this.getMaxPage()) {
|
|
this.setCurrentPage(this.getMaxPage());
|
|
}
|
|
}
|
|
getCurrentPage() {
|
|
return this.currentPage;
|
|
}
|
|
setCurrentPage(page) {
|
|
this.currentPage = page;
|
|
this.setEntryIndex((this.getCurrentPage() - 1) * this.getEntriesPerPage());
|
|
document.getElementById("page-number").innerText = String(this.getCurrentPage());
|
|
}
|
|
getEntryIndex() {
|
|
return this.entryIndex;
|
|
}
|
|
setEntryIndex(entryIndex) {
|
|
this.entryIndex = entryIndex;
|
|
this.updateRange();
|
|
this.updateListings();
|
|
}
|
|
getMaxEntries() {
|
|
return this.maxEntries;
|
|
}
|
|
setMaxEntries(max) {
|
|
this.maxEntries = max;
|
|
}
|
|
getMaxPage() {
|
|
return Math.ceil(this.getMaxEntries() / this.getEntriesPerPage());
|
|
}
|
|
updateRange() {
|
|
let max = Math.min(this.getEntryIndex() + this.getEntriesPerPage(), this.getMaxEntries());
|
|
document.getElementById("entry-counter").innerText = `${this.getEntryIndex() + 1}-${max} of ${this.getMaxEntries()}`;
|
|
}
|
|
updateListings() {
|
|
getRequest(this.getEntriesPerPage(), this.entryIndex, 0)
|
|
.then(res => {
|
|
let table = document.getElementById("pkg-list");
|
|
table.innerHTML = '';
|
|
for (let i = 0; i < res.count; i++) {
|
|
table.appendChild(toHTML(res.values[i]));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
let STATE;
|
|
function prevPage() {
|
|
let current = STATE.getCurrentPage();
|
|
if (current > 1) {
|
|
STATE.setCurrentPage(STATE.getCurrentPage() - 1);
|
|
}
|
|
}
|
|
function nextPage() {
|
|
let current = STATE.getCurrentPage();
|
|
if (current < STATE.getMaxPage()) {
|
|
STATE.setCurrentPage(STATE.getCurrentPage() + 1);
|
|
}
|
|
}
|
|
function escapeHtml(str) {
|
|
return str
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
STATE = new State();
|
|
infoRequest()
|
|
.then(res => {
|
|
STATE.setMaxEntries(res.count);
|
|
document.getElementById("hakurei-version").innerText = res.hakurei_version;
|
|
STATE.updateRange();
|
|
STATE.updateListings();
|
|
});
|
|
document.getElementById("count").addEventListener("change", (event) => {
|
|
STATE.setEntriesPerPage(parseInt(event.target.value));
|
|
});
|
|
});
|