A small project that collects various nice things to get started with Go Web Development.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.9 KiB

class PaginateTable {
constructor(url) {
this.page = 0;
this.items = [];
this.url = url;
this.headers = [];
this.search_query="";
}
async contents() {
if(this.page < 0) this.page = 0;
let url = `${this.url}?page=${this.page}`;
if(this.search_query !== "") {
this.page = 0;
url += `&search=${this.search_query}`
}
const resp = await fetch(url);
console.assert(resp.status == 200, "failed to get it");
const items = await resp.json();
if(items) {
this.items = items;
this.headers = Object.keys(this.items[0]);
}
return this.items;
}
}
class ForeverScroll {
constructor(url) {
this.page = 0;
this.items = [];
this.url = url;
this.end = false;
}
async init() {
const resp = await fetch(this.url);
console.assert(resp.status == 200, "failed to get it");
const items = await resp.json();
if(items) this.items = items;
}
async load() {
this.page += 1
let url = `${this.url}?page=${this.page}`;
const resp = await fetch(url);
console.assert(resp.status == 200, "failed to get it");
const items = await resp.json();
if(items) {
this.items.push(...items);
} else {
this.end = true;
}
}
}
const GetJson = async (url) => {
const resp = await fetch(url);
console.assert(resp.status == 200, "failed to get it");
return await resp.json();
}
const ConfirmDelete = async (table, obj_id) => {
if(confirm("Are you sure?")) {
await fetch("/api/admin/table/" + table + "/" + obj_id,
{ method: "DELETE" });
window.location = "/admin/table/" + table;
} else {
return false;
}
}
const UrlId = () => {
let url = new URL(window.location.href);
let parts = url.pathname.split("/");
if(window.location.href.endsWith("/")) {
return parts[parts.length - 2];
} else {
return parts[parts.length - 1];
}
}