galerie/web/www/settings.ts

227 lines
4.6 KiB
TypeScript

let apiUrl: string = `${location.protocol}//${location.hostname}:8856/`;
class Favorites {
private data: string[] = [];
constructor() {
this.data = JSON.parse(localStorage.getItem("settings.favorites")) || [];
this.sync();
}
public async sync(): Promise<void> {
localStorage.setItem("settings.favorites", JSON.stringify(this.data));
}
public async clear(): Promise<void> {
this.data = [];
this.sync();
}
public async pop(): Promise<string> {
let item: any = this.data.pop();
this.sync();
return item;
}
public async push(toadd): Promise<void> {
this.data.push(toadd);
this.sync();
}
public async delete(d): Promise<void> {
this.data = this.data.filter((v) => { return v != d });
this.sync();
}
public async at(d): Promise<string> {
return this.data.at(d);
}
public async contains(d): Promise<boolean> {
return this.data.includes(d);
}
public async shuffle(): Promise<string[]> {
let data: string[] = this.data;
for (let i = 0; i < await this.data.length; i++) {
data.sort(() => { return Math.random() - 0.5 });
}
return data;
}
}
class Blacklist {
private data: string[] = [];
constructor() {
this.data = JSON.parse(localStorage.getItem("settings.blacklist")) || [];
this.sync();
}
public async sync(): Promise<void> {
localStorage.setItem("settings.blacklist", JSON.stringify(this.data));
}
public async clear(): Promise<void> {
this.data = [];
this.sync();
}
public async pop(): Promise<string> {
let item: any = this.data.pop();
this.sync();
return item;
}
public async push(toadd): Promise<void> {
this.data.push(toadd);
this.sync();
}
public async delete(d): Promise<void> {
this.data = this.data.filter((v) => { return v != d });
this.sync();
}
public async at(d): Promise<string> {
return this.data.at(d);
}
public async contains(d): Promise<boolean> {
return this.data.includes(d);
}
}
class Cache {
private cache: string[];
private checksum: string;
constructor() {}
async load(): Promise<void> {
if (!localStorage.getItem("cache") || !localStorage.getItem("cache.checksum")) {
await this.sync();
} else {
if (!await this.validate()) {
await this.sync();
} else {
this.cache = JSON.parse(localStorage.getItem("cache"));
this.checksum = localStorage.getItem("cache.checksum");
}
}
}
async sync(): Promise<void> {
try {
let sync = await fetch(apiUrl + "sync");
let data = await sync.json();
if (!sync.ok)
throw Error(sync.statusText);
this.checksum = data.checksum;
this.cache = data.files;
await this.update();
} catch (e) {
throw e;
}
}
private async update(): Promise<void> {
localStorage.setItem("cache", JSON.stringify(this.cache));
localStorage.setItem("cache.checksum", this.checksum);
}
private async validate(): Promise<boolean> {
try {
let sync = await fetch(apiUrl + "sync", {
body: this.checksum,
method: "POST",
});
if (!sync.ok)
throw Error(sync.statusText);
return (await sync.json()).valid;
} catch(e) {
throw e;
}
}
public async at(d): Promise<string> {
return this.cache.at(d);
}
public async shuffle(): Promise<string[]> {
let data: string[] = this.cache;
for (let i = 0; i < await this.length(); i++) {
data.sort(() => { return Math.random() - 0.5 });
}
return data;
}
public async length(): Promise<Number> {
return this.cache.length;
}
}
class Config {
private data = {};
private defaultConfig = {
"test": 123,
}
constructor() {
if (!localStorage.getItem("config"))
localStorage.setItem("config", JSON.stringify(this.defaultConfig));
this.data = JSON.parse(localStorage.getItem("config"));
this.verify();
this.sync();
}
private verify() {
let keys = this.keys();
for (let k in this.defaultConfig)
if (!keys[k])
this.data[k] = this.defaultConfig[k];
}
public sync() {
localStorage.setItem("config", JSON.stringify(this.data));
}
public set(vr, vl) {
this.data[vr] = vl;
}
public get(vr) {
return this.data[vr];
}
public keys() {
return Object.keys(this.data);
}
}
export default class Settings {
favorites: Favorites;
blacklist: Blacklist;
cache: Cache;
config: Config;
constructor() {
this.favorites = new Favorites();
this.blacklist = new Blacklist();
this.cache = new Cache();
this.config = new Config();
}
}
export { apiUrl };