2021-01-05 18:16:23 +00:00
|
|
|
import { toggleTheme, loadTheme } from "./modules/theme.js";
|
2021-01-12 23:43:44 +00:00
|
|
|
import { lang, LangFile, loadLangSelector } from "./modules/lang.js";
|
2021-01-05 18:16:23 +00:00
|
|
|
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 { ProfileEditor } from "./modules/profiles.js";
|
2021-01-12 23:15:12 +00:00
|
|
|
import { _get, _post, notificationBox, whichAnimationEvent, toggleLoader } from "./modules/common.js";
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
loadTheme();
|
|
|
|
(document.getElementById('button-theme') as HTMLSpanElement).onclick = toggleTheme;
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-12 23:15:12 +00:00
|
|
|
window.lang = new lang(window.langFile as LangFile);
|
2021-01-12 23:43:44 +00:00
|
|
|
loadLangSelector("admin");
|
2021-01-12 23:15:12 +00:00
|
|
|
// _get(`/lang/admin/${window.language}.json`, null, (req: XMLHttpRequest) => {
|
|
|
|
// if (req.readyState == 4 && req.status == 200) {
|
|
|
|
// langLoaded = true;
|
|
|
|
// window.lang = new lang(req.response as LangFile);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
window.animationEvent = whichAnimationEvent();
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
window.token = "";
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
window.availableProfiles = window.availableProfiles || [];
|
2020-10-22 16:50:40 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
window.modals.profiles = new Modal(document.getElementById("modal-user-profiles"));
|
|
|
|
|
|
|
|
window.modals.addProfile = new Modal(document.getElementById("modal-add-profile"));
|
|
|
|
})();
|
2020-10-22 16:50:40 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
var inviteCreator = new createInvite();
|
|
|
|
var accounts = new accountsList();
|
|
|
|
|
|
|
|
window.invites = new inviteList();
|
|
|
|
|
|
|
|
var settings = new settingsList();
|
|
|
|
|
|
|
|
var profiles = new ProfileEditor();
|
|
|
|
|
|
|
|
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("invites", null, window.invites.reload);
|
|
|
|
window.tabs.addTab("accounts", null, accounts.reload);
|
|
|
|
window.tabs.addTab("settings", null, settings.reload);
|
|
|
|
|
|
|
|
for (let tab of ["invites", "accounts", "settings"]) {
|
|
|
|
if (window.location.pathname == "/" + tab) {
|
|
|
|
window.tabs.switch(tab, true);
|
|
|
|
}
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
if (window.location.pathname == "/") {
|
|
|
|
window.tabs.switch("invites", true);
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
document.addEventListener("tab-change", (event: CustomEvent) => {
|
|
|
|
let tab = "/" + event.detail;
|
|
|
|
if (tab == "/invites") {
|
|
|
|
if (window.location.pathname == "/") {
|
|
|
|
tab = "/";
|
|
|
|
} else { tab = "../"; }
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
window.history.replaceState("", "Admin - jfa-go", tab);
|
|
|
|
});
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
function login(username: string, password: string, run?: (state?: number) => void) {
|
2020-09-21 21:03:20 +00:00
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.responseType = 'json';
|
2020-11-22 16:36:43 +00:00
|
|
|
let url = window.URLBase;
|
2020-11-12 21:04:35 +00:00
|
|
|
const refresh = (username == "" && password == "");
|
|
|
|
if (refresh) {
|
2020-11-22 16:36:43 +00:00
|
|
|
url += "/token/refresh";
|
|
|
|
} else {
|
|
|
|
url += "/token/login";
|
2020-11-12 21:04:35 +00:00
|
|
|
}
|
|
|
|
req.open("GET", url, true);
|
|
|
|
if (!refresh) {
|
|
|
|
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
|
|
|
|
}
|
2020-09-21 21:03:20 +00:00
|
|
|
req.onreadystatechange = function (): void {
|
|
|
|
if (this.readyState == 4) {
|
|
|
|
if (this.status != 200) {
|
2021-01-12 23:15:12 +00:00
|
|
|
let errorMsg = window.lang.notif("errorConnection");
|
2021-01-05 18:16:23 +00:00
|
|
|
if (this.response) {
|
|
|
|
errorMsg = this.response["error"];
|
|
|
|
}
|
2020-09-21 21:03:20 +00:00
|
|
|
if (!errorMsg) {
|
2021-01-12 23:15:12 +00:00
|
|
|
errorMsg = window.lang.notif("errorUnknown");
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
if (!refresh) {
|
|
|
|
window.notifications.customError("loginError", errorMsg);
|
2020-09-21 21:03:20 +00:00
|
|
|
} else {
|
2021-01-05 18:16:23 +00:00
|
|
|
window.modals.login.show();
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const data = this.response;
|
|
|
|
window.token = data["token"];
|
2021-01-05 18:16:23 +00:00
|
|
|
window.modals.login.close();
|
|
|
|
setInterval(() => { window.invites.reload(); accounts.reload(); }, 30*1000);
|
|
|
|
const currentTab = window.tabs.current;
|
|
|
|
switch (currentTab) {
|
|
|
|
case "invites":
|
|
|
|
window.invites.reload();
|
|
|
|
break;
|
|
|
|
case "accounts":
|
|
|
|
accounts.reload();
|
|
|
|
break;
|
|
|
|
case "settings":
|
|
|
|
settings.reload();
|
|
|
|
break;
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
document.getElementById("logout-button").classList.remove("unfocused");
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
if (run) { run(+this.status); }
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send();
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
(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) {
|
2021-01-12 23:15:12 +00:00
|
|
|
window.notifications.customError("loginError", window.lang.notif("errorLoginBlank"));
|
2021-01-05 18:16:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleLoader(button);
|
|
|
|
login(username, password, () => toggleLoader(button));
|
2020-09-21 21:03:20 +00:00
|
|
|
};
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
login("", "");
|
2020-09-21 21:03:20 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
(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;
|
2020-09-21 21:03:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|