1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-11-09 20:00:12 +00:00
jfa-go/ts/admin.ts

136 lines
5.3 KiB
TypeScript

import { toggleTheme, loadTheme } from "./modules/theme.js";
import { Modal } from "./modules/modal.js";
import { Tabs } from "./modules/tabs.js";
import { inviteList, createInvite } from "./modules/invites.js";
import { accountsList } from "./modules/accounts.js";
import { settingsList } from "./modules/settings.js";
import { _post, notificationBox, whichAnimationEvent, toggleLoader } from "./modules/common.js";
loadTheme();
(document.getElementById('button-theme') as HTMLSpanElement).onclick = toggleTheme;
window.animationEvent = whichAnimationEvent();
window.token = "";
window.availableProfiles = window.availableProfiles || [];
// load modals
(() => {
window.modals = {} as Modals;
window.modals.login = new Modal(document.getElementById('modal-login'), true);
window.modals.addUser = new Modal(document.getElementById('modal-add-user'));
window.modals.about = new Modal(document.getElementById('modal-about'));
(document.getElementById('setting-about') as HTMLSpanElement).onclick = window.modals.about.toggle;
window.modals.modifyUser = new Modal(document.getElementById('modal-modify-user'));
window.modals.deleteUser = new Modal(document.getElementById('modal-delete-user'));
window.modals.settingsRestart = new Modal(document.getElementById('modal-restart'));
window.modals.settingsRefresh = new Modal(document.getElementById('modal-refresh'));
window.modals.ombiDefaults = new Modal(document.getElementById('modal-ombi-defaults'));
document.getElementById('form-ombi-defaults').addEventListener('submit', window.modals.ombiDefaults.close);
})();
var inviteCreator = new createInvite();
var accounts = new accountsList();
window.invites = new inviteList();
var settings = new settingsList();
window.notifications = new notificationBox(document.getElementById('notification-box') as HTMLDivElement, 5);
/*const modifySettingsSource = function () {
const profile = document.getElementById('radio-use-profile') as HTMLInputElement;
const user = document.getElementById('radio-use-user') as HTMLInputElement;
const profileSelect = document.getElementById('modify-user-profiles') as HTMLDivElement;
const userSelect = document.getElementById('modify-user-users') as HTMLDivElement;
(user.nextElementSibling as HTMLSpanElement).classList.toggle('!normal');
(user.nextElementSibling as HTMLSpanElement).classList.toggle('!high');
(profile.nextElementSibling as HTMLSpanElement).classList.toggle('!normal');
(profile.nextElementSibling as HTMLSpanElement).classList.toggle('!high');
profileSelect.classList.toggle('unfocused');
userSelect.classList.toggle('unfocused');
}*/
// load tabs
window.tabs = new Tabs();
window.tabs.addTab("invitesTab");
window.tabs.addTab("accountsTab", null, accounts.reload);
window.tabs.addTab("settingsTab", null, settings.reload);
function login(username: string, password: string, run?: (state?: number) => void) {
const req = new XMLHttpRequest();
req.responseType = 'json';
let url = window.URLBase;
const refresh = (username == "" && password == "");
if (refresh) {
url += "/token/refresh";
} else {
url += "/token/login";
}
req.open("GET", url, true);
if (!refresh) {
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
}
req.onreadystatechange = function (): void {
if (this.readyState == 4) {
if (this.status != 200) {
let errorMsg = "Connection error.";
if (this.response) {
errorMsg = this.response["error"];
}
if (!errorMsg) {
errorMsg = "Unknown error";
}
if (!refresh) {
window.notifications.customError("loginError", errorMsg);
} else {
window.modals.login.show();
}
} else {
const data = this.response;
window.token = data["token"];
window.modals.login.close();
window.invites.reload();
accounts.reload();
setInterval(() => { window.invites.reload(); accounts.reload(); }, 30*1000);
document.getElementById("logout-button").classList.remove("unfocused");
}
if (run) { run(+this.status); }
}
};
req.send();
}
(document.getElementById('form-login') as HTMLFormElement).onsubmit = (event: SubmitEvent) => {
event.preventDefault();
const button = (event.target as HTMLElement).querySelector(".submit") as HTMLSpanElement;
const username = (document.getElementById("login-user") as HTMLInputElement).value;
const password = (document.getElementById("login-password") as HTMLInputElement).value;
if (!username || !password) {
window.notifications.customError("loginError", "The username and/or password were left blank.");
return;
}
toggleLoader(button);
login(username, password, () => toggleLoader(button));
};
login("", "");
(document.getElementById('logout-button') as HTMLButtonElement).onclick = () => _post("/logout", null, (req: XMLHttpRequest): boolean => {
if (req.readyState == 4 && req.status == 200) {
window.token = "";
location.reload();
return false;
}
});