stylehub/assets/script.js

657 lines
19 KiB
JavaScript
Raw Normal View History

2024-02-06 20:57:25 +01:00
let UserInfo = {};
2024-02-22 14:08:55 +01:00
let PageIntervals = [];
2024-03-10 22:55:29 +01:00
let config =
{
articleRefresh: 300000,
messageFadeIn: 300,
messageDisappear: 200
};
2024-02-07 18:31:01 +01:00
2024-03-10 22:55:29 +01:00
function isLoggedIn() {
"use strict";
return UserInfo.Email && 0 < UserInfo.Email.length;
2024-02-07 18:41:15 +01:00
}
2024-02-06 20:53:57 +01:00
async function handleResponse(data, successMessage, failureMessage) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 19:33:25 +01:00
const statusMessageContainer = document.getElementById("statusMessageContainer");
const statusMessage = document.createElement("div");
statusMessage.classList.add("status-message");
2024-03-10 22:55:29 +01:00
if ('Success' === data.Status) {
2024-02-06 19:33:25 +01:00
statusMessage.innerText = successMessage;
statusMessage.classList.add("success");
} else {
statusMessage.innerText = failureMessage;
statusMessage.classList.add("failure");
}
statusMessageContainer.appendChild(statusMessage);
// Automatically remove the message after 3 seconds
setTimeout(() => {
statusMessage.style.opacity = "0";
setTimeout(() => {
statusMessage.remove();
2024-03-10 22:55:29 +01:00
}, config.messageFadeIn);
}, config.messageDisappear);
2024-02-06 19:33:25 +01:00
}
2024-02-06 20:00:32 +01:00
async function showDashboardGreeting() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 20:59:03 +01:00
document.getElementById("welcomeMsg").innerText = `Ahoj, ${UserInfo.FirstName}.`;
2024-02-06 20:00:32 +01:00
}
2024-03-10 22:55:29 +01:00
async function doAction(url, requestData, successMessage, failureMessage, silent) {
"use strict";
const params = new URLSearchParams();
2024-02-06 16:24:57 +01:00
2024-03-10 22:55:29 +01:00
for (const key in requestData) {
params.append(key, requestData[key]);
}
2024-02-03 16:08:26 +01:00
2024-03-10 22:55:29 +01:00
const response = await fetch(url, {
method: 'POST',
body: params,
});
2024-02-05 21:21:04 +01:00
2024-03-10 22:55:29 +01:00
if (!response.ok) {
console.error(`HTTP error! Status: ${response.status}`);
}
2024-02-05 21:21:04 +01:00
2024-03-10 22:55:29 +01:00
const data = await response.json();
2024-02-06 16:24:57 +01:00
2024-03-10 22:55:29 +01:00
if (!silent) {
await handleResponse(data, successMessage, failureMessage);
2024-02-05 21:21:04 +01:00
}
2024-03-10 22:55:29 +01:00
return data;
2024-02-05 21:21:04 +01:00
}
2024-02-06 20:19:36 +01:00
async function handlePageResponse(data) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 20:00:32 +01:00
const navbar = document.getElementById("navbar_container");
const pageArea = document.getElementById("page_container");
2024-02-06 16:24:57 +01:00
if (data.Navigation) {
navbar.innerHTML = data.Navigation;
}
2024-02-24 09:18:31 +01:00
if (data.PageTitle) {
2024-02-24 09:16:21 +01:00
document.title = data.PageTitle;
}
2024-02-06 16:24:57 +01:00
if (data.Page) {
pageArea.innerHTML = data.Page;
2024-02-07 18:31:01 +01:00
if (data.PageLocation) {
2024-02-06 16:38:23 +01:00
history.pushState({}, "", data.PageLocation);
}
2024-02-06 16:24:57 +01:00
}
2024-02-05 21:21:04 +01:00
}
2024-02-06 20:19:36 +01:00
async function displayList(data, elementId, deleteFunction) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const tableContainer = document.getElementById(elementId);
2024-02-04 09:50:04 +01:00
tableContainer.innerHTML = ""; // Clear previous content
const table = document.createElement("table");
table.classList.add("list-table");
const headerRow = table.insertRow(0);
for (const key in data[0]) {
const th = document.createElement("th");
th.appendChild(document.createTextNode(key));
headerRow.appendChild(th);
}
2024-03-10 22:55:29 +01:00
if ("function" === typeof deleteFunction) {
2024-02-04 09:50:04 +01:00
const th = document.createElement("th");
2024-04-26 01:19:58 +02:00
th.appendChild(document.createElement("<i class='ri-delete-bin-line'></i>"));
2024-02-04 09:50:04 +01:00
headerRow.appendChild(th);
}
for (const line of data) {
const dataRow = table.insertRow();
for (const key in line) {
const td = document.createElement("td");
td.appendChild(document.createTextNode(line[key]));
dataRow.appendChild(td);
}
2024-03-10 22:55:29 +01:00
if ("function" === typeof deleteFunction) {
2024-02-04 09:50:04 +01:00
const td = document.createElement("td");
2024-02-06 16:24:57 +01:00
const deleteButton = document.createElement('button');
2024-04-26 01:19:00 +02:00
deleteButton.innerHTML = "<i class='ri-delete-bin-line'></i>";
2024-02-06 16:24:57 +01:00
deleteButton.onclick = () => deleteFunction(line.ID);
td.appendChild(deleteButton);
2024-02-04 09:50:04 +01:00
dataRow.appendChild(td);
}
}
tableContainer.appendChild(table);
}
2024-03-10 22:55:29 +01:00
async function doPageAction(requestData) {
"use strict";
const response = await fetch('/page', {
method: 'POST',
body: new URLSearchParams(requestData),
});
2024-02-03 16:08:26 +01:00
2024-03-10 22:55:29 +01:00
if (!response.ok) {
console.error(`HTTP error! Status: ${response.status}`);
2024-02-06 16:24:57 +01:00
}
2024-03-10 22:55:29 +01:00
const data = await response.json();
await handlePageResponse(data);
return data;
2024-02-03 16:08:26 +01:00
}
2024-02-07 18:31:01 +01:00
async function initAjaxNavigationEvents() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-07 18:17:38 +01:00
const allLinks = document.querySelectorAll('.navsite_link, .navpage_link');
const pageLinks = document.querySelectorAll('.navpage_link');
pageLinks.forEach(function (link) {
link.addEventListener('click', function () {
navLinks.classList.remove("active");
});
});
2024-02-05 21:21:04 +01:00
2024-02-07 18:17:38 +01:00
allLinks.forEach(function (link) {
2024-02-05 21:23:21 +01:00
link.addEventListener('click', function (e) {
e.preventDefault();
2024-02-05 21:25:41 +01:00
let site = this.dataset.site;
let page = this.dataset.page;
2024-02-05 21:23:21 +01:00
if (site && page) {
navigateTo(site, page);
}
});
2024-02-05 21:21:04 +01:00
});
2024-03-10 22:55:29 +01:00
const toggleButton = document.getElementById("toggle_button");
const navLinks = document.getElementById("navsite_list");
2024-02-06 20:58:14 +01:00
toggleButton.addEventListener('click', () => {
2024-03-10 22:55:29 +01:00
navLinks.classList.toggle("active");
});
2024-02-07 18:17:38 +01:00
}
async function initAjax() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-07 18:17:38 +01:00
await initAjaxNavigationEvents();
2024-02-06 20:19:36 +01:00
await onPageLoad();
2024-02-05 21:23:21 +01:00
}
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
async function togglearticlecreate() {
"use strict";
2024-02-22 10:43:18 +01:00
let articleContainerElement = document.getElementById("articlecreatecontainer");
articleContainerElement.classList.toggle("hidden");
}
2024-03-10 22:55:29 +01:00
async function renderarticles() {
"use strict";
2024-02-22 17:54:46 +01:00
let template = document.querySelector('template[data-template-name="article"]').innerHTML;
2024-02-22 14:08:55 +01:00
let articles = await doAction(
"/newsarticle",
{
action: "getNewsArticles"
},
"Články načítané",
"Nastala chyba pri načítavaní článkov",
true
);
2024-02-22 13:57:05 +01:00
2024-02-22 14:08:55 +01:00
let articleout = "";
2024-02-22 17:52:20 +01:00
for (const article of articles.Articles) {
2024-03-10 22:55:29 +01:00
articleout += template.replace("__TEMPLATE_ARTICLE_TITLE__", article.Title).replace("__TEMPLATE_ARTICLE_AUTHOR__", article.WrittenByName).replace("__TEMPLATE_ARTICLE_DATE__", article.WrittenAt).replace("__TEMPLATE_ARTICLE_BODY__", article.Body);
2024-02-22 14:08:55 +01:00
}
document.getElementById("articleslist").innerHTML = articleout;
2024-02-22 13:57:05 +01:00
}
2024-03-10 22:55:29 +01:00
async function submitarticle() {
"use strict";
2024-02-22 10:43:18 +01:00
let articleTitleElement = document.getElementById("articletitleinput");
let articleBodyElement = document.getElementById("articlebodyinput");
2024-02-22 13:57:05 +01:00
await doAction(
"/newsarticle",
{
action: "addNewsArticle",
title: articleTitleElement.value,
body: articleBodyElement.value
},
"Článok úspešne pridaný",
2024-02-22 12:28:16 +01:00
"Nastala chyba pri pridávaní článku",
2024-02-22 13:57:05 +01:00
false
);
2024-02-22 18:16:42 +01:00
await togglearticlecreate();
2024-02-22 10:43:18 +01:00
}
2024-03-10 22:55:29 +01:00
async function articleInit() {
"use strict";
2024-02-22 12:19:57 +01:00
let articleContainerElement = document.getElementById("articlecreatecontainer");
2024-02-24 09:01:13 +01:00
let articleCreateOpenElement = document.getElementById("articlecreateopen");
2024-02-22 12:19:57 +01:00
articleContainerElement.addEventListener("keyup", function (ev) {
2024-03-10 22:55:29 +01:00
if ("Escape" === ev.key) {
2024-02-22 12:19:57 +01:00
togglearticlecreate();
}
2024-03-10 22:55:29 +01:00
});
PageIntervals.push(setInterval(renderarticles, config.articleRefresh));
2024-02-24 09:01:13 +01:00
document.getElementById("articleprivilegeinput").setAttribute("max", UserInfo.Privileges);
2024-03-10 22:55:29 +01:00
if (2 > UserInfo.Privileges) {
2024-02-24 09:01:13 +01:00
articleContainerElement.style.display = "none";
articleCreateOpenElement.style.display = "none";
2024-03-10 22:55:29 +01:00
} else {
2024-02-24 09:01:13 +01:00
articleCreateOpenElement.style.display = "inline-block";
}
2024-02-22 10:43:18 +01:00
}
2024-02-07 18:31:01 +01:00
async function onPageLoad() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 20:46:31 +01:00
await restoreUserInfo();
2024-02-05 22:52:44 +01:00
let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage");
2024-02-22 14:08:55 +01:00
2024-03-10 22:55:29 +01:00
for (let interval of PageIntervals) {
2024-02-22 14:08:55 +01:00
clearInterval(interval);
}
2024-03-10 22:55:29 +01:00
if ("home" === currentSite && "settings" === currentPage) {
2024-02-07 18:31:01 +01:00
if (document.getElementById("user-settings")) {
2024-02-06 20:19:36 +01:00
await populateUserInfoFields(UserInfo);
2024-02-05 22:52:44 +01:00
}
2024-02-07 18:31:01 +01:00
if (document.getElementById("admin-settings")) {
2024-02-06 21:09:33 +01:00
await listActivationCodes(true);
await listUsers(true);
2024-02-05 22:52:44 +01:00
}
}
2024-03-10 22:55:29 +01:00
if ("account" === currentSite && "index" === currentPage && isLoggedIn()) {
2024-02-06 20:19:36 +01:00
await showDashboardGreeting();
2024-02-06 20:00:32 +01:00
}
2024-03-10 22:55:29 +01:00
if ("news" === currentSite && "index" === currentPage) {
2024-02-22 10:43:18 +01:00
await articleInit();
}
2024-04-25 15:01:17 +02:00
if ("files" === currentSite && "list" === currentPage) {
await listFiles();
}
2024-04-25 22:38:04 +02:00
if ("memes" === currentSite && "create" === currentPage) {
await getMemeImages();
}
2024-02-05 22:52:44 +01:00
}
2024-02-07 18:31:01 +01:00
async function navigateTo(site, page) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "getPage",
site: site,
page: page,
};
2024-03-10 22:55:29 +01:00
doPageAction(data).then(() => {
2024-02-06 16:24:57 +01:00
localStorage.setItem("currentSite", site);
localStorage.setItem("currentPage", page);
onPageLoad();
2024-02-06 16:47:51 +01:00
});
2024-02-06 16:24:57 +01:00
}
2024-02-07 18:31:01 +01:00
async function softReload() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 17:04:56 +01:00
let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage");
2024-02-06 20:19:36 +01:00
await navigateTo(currentSite, currentPage);
2024-02-06 17:04:56 +01:00
umami.track("softReload");
2024-02-06 16:24:57 +01:00
}
2024-02-07 18:31:01 +01:00
async function refreshNavbar() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "getNavigation",
};
2024-02-06 20:19:36 +01:00
await doPageAction(data);
2024-02-06 17:04:56 +01:00
umami.track("refreshNavbar");
2024-02-07 18:43:04 +01:00
await initAjaxNavigationEvents();
2024-02-06 16:24:57 +01:00
}
2024-02-06 20:19:36 +01:00
async function logout() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "logout",
};
2024-03-10 22:55:29 +01:00
doAction('/account', data, "Logout Successful!", "Logout failed.", false)
2024-02-06 20:19:36 +01:00
.then(async () => {
await refreshNavbar();
await navigateTo(localStorage.getItem("defaultSite"), localStorage.getItem("defaultPage"));
2024-02-06 20:48:30 +01:00
localStorage.clear();
2024-02-06 20:19:36 +01:00
umami.track("logout");
})
.catch((error) => {
// Handle errors if needed
console.error("An error occurred during logout:", error);
});
2024-02-06 16:24:57 +01:00
}
2024-02-05 22:22:52 +01:00
2024-02-06 20:19:36 +01:00
async function login() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const email = document.getElementById("login_email").value;
const password = document.getElementById("login_password").value;
2024-02-06 20:19:36 +01:00
await doLogin(email, password);
await getUserInfo();
await refreshNavbar();
await softReload();
2024-02-05 22:22:52 +01:00
}
2024-02-06 16:24:57 +01:00
async function doLogin(email, password) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "login",
email: email,
password: password,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
await doAction('/account', data, "Login Successful!", "Login failed. Please check your credentials.", false);
2024-02-06 17:04:56 +01:00
umami.track("login");
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function register() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const firstName = document.getElementById("register_firstName").value;
const lastName = document.getElementById("register_lastName").value;
const email = document.getElementById("register_email").value;
const password = document.getElementById("register_password").value;
const activationToken = document.getElementById("register_activationToken").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "register",
firstname: firstName,
lastname: lastName,
email: email,
password: password,
activation_token: activationToken,
};
2024-02-05 22:22:52 +01:00
2024-02-06 20:19:36 +01:00
await doRegister(data);
2024-02-05 22:22:52 +01:00
}
2024-02-06 16:24:57 +01:00
async function doRegister(requestData) {
2024-03-10 22:55:29 +01:00
"use strict";
await doAction('/account', requestData, "Registration Successful!", "Registration failed.", false);
2024-02-06 17:04:56 +01:00
umami.track("register");
2024-02-05 22:22:52 +01:00
}
//User settings start
2024-02-06 20:19:36 +01:00
async function changePassword() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const oldPassword = document.getElementById("changeOldPassword").value;
const newPassword = document.getElementById("changeNewPassword").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "change_password",
old_password: oldPassword,
new_password: newPassword,
};
2024-02-05 22:22:52 +01:00
2024-02-06 20:19:36 +01:00
await doChangePassword(data, "Password change Successful!", "Password change failed.");
2024-02-05 22:22:52 +01:00
}
2024-02-06 16:24:57 +01:00
async function doChangePassword(requestData, successMessage, failureMessage) {
2024-03-10 22:55:29 +01:00
"use strict";
await doAction('/account', requestData, successMessage, failureMessage, false);
2024-02-06 17:04:56 +01:00
umami.track("passwordChange");
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function updateUserProfile() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const firstName = document.getElementById("updateFirstName").value;
const lastName = document.getElementById("updateLastName").value;
const nickname = document.getElementById("updateNickname").value;
const minecraftNick = document.getElementById("updateMinecraftNick").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "update_user_profile",
first_name: firstName,
last_name: lastName,
nickname: nickname,
minecraft_nick: minecraftNick,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
await doAction('/account', data, "Profile update Successful!", "Profile update failed.", false);
2024-02-06 17:04:56 +01:00
umami.track("updateUserProfile");
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function updateEmail() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const newEmail = document.getElementById("updateNewEmail").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "update_user_email",
email: newEmail,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
await doAction('/account', data, "Email update Successful!", "Email update failed.", false);
2024-02-06 17:04:56 +01:00
umami.track("updateEmail");
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function populateUserInfoFields(userData) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
document.getElementById("updateFirstName").value = userData.FirstName || "";
document.getElementById("updateLastName").value = userData.LastName || "";
document.getElementById("updateNickname").value = userData.Nickname || "";
document.getElementById("updateMinecraftNick").value = userData.MinecraftNick || "";
document.getElementById("updateNewEmail").value = userData.Email || "";
}
2024-02-07 18:31:01 +01:00
async function restoreUserInfo() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 20:46:31 +01:00
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
2024-02-07 18:31:01 +01:00
if (key.startsWith("UserInfo_")) {
2024-02-06 20:46:31 +01:00
let keyClean = key.replace("UserInfo_", "");
UserInfo[keyClean] = localStorage.getItem(key);
}
}
}
2024-02-06 20:19:36 +01:00
async function getUserInfo() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "get_user_info",
};
2024-02-07 18:31:01 +01:00
const result = await doAction('/account', data, "User info retrieved Successfully!", "User info retrieval failed.", true);
2024-03-10 22:55:29 +01:00
if (result && "Success" === result.Status) {
2024-02-07 18:38:36 +01:00
Object.keys(result.UserInfo).forEach(index => {
let value = result.UserInfo[index];
2024-02-07 18:31:01 +01:00
localStorage.setItem("UserInfo_" + index, value);
UserInfo[index] = value;
});
}
2024-02-05 22:22:52 +01:00
}
//User settings end
//Admin settings start
2024-02-06 20:19:36 +01:00
async function addActivationCodes() {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-05 22:22:52 +01:00
const count = document.getElementById("activationCodeCount").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "add_activation_codes",
count: count,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
doAction('/account', data, "Activation codes added Successfully!", "Activation codes addition failed.", false).then((result) => {
2024-02-06 20:10:06 +01:00
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
umami.track("addActivationCodes");
});
2024-02-05 22:22:52 +01:00
}
2024-03-10 22:55:29 +01:00
async function listUsers(silent) {
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "list_users",
};
2024-02-05 22:22:52 +01:00
2024-02-06 21:09:33 +01:00
doAction('/account', data, "User list retrieved Successfully!", "User list retrieval failed.", silent).then((result) => {
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
if (result && "Success" === result.Status) {
2024-02-06 20:10:06 +01:00
displayList(result.Users, "userListTable", deleteUser);
}
});
2024-02-05 22:22:52 +01:00
}
2024-03-10 22:55:29 +01:00
async function listActivationCodes(silent) {
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "list_activation_codes",
};
2024-02-05 22:22:52 +01:00
2024-02-06 21:09:33 +01:00
doAction('/account', data, "Activation code list retrieved Successfully!", "Activation code list retrieval failed.", silent).then((result) => {
2024-02-06 20:10:06 +01:00
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
});
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function deleteUser(userId) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "delete_user",
user_id: userId,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
await doAction('/account', data, "User deleted Successfully!", "User deletion failed.", false);
await listUsers(false);
2024-02-06 17:04:56 +01:00
umami.track("deleteUser");
2024-02-05 22:22:52 +01:00
}
2024-02-06 20:19:36 +01:00
async function deleteActivationCode(activationCode) {
2024-03-10 22:55:29 +01:00
"use strict";
2024-02-06 16:24:57 +01:00
const data = {
action: "delete_activation_code",
activation_code: activationCode,
};
2024-02-05 22:22:52 +01:00
2024-03-10 22:55:29 +01:00
await doAction('/account', data, "Activation code deleted Successfully!", "Activation code deletion failed.", false);
await listActivationCodes(false);
2024-02-06 17:04:56 +01:00
umami.track("deleteActivationCode");
2024-02-05 22:22:52 +01:00
}
2024-02-07 18:31:01 +01:00
2024-02-05 22:22:52 +01:00
//Admin settings end
2024-02-07 18:13:24 +01:00
2024-03-10 22:55:29 +01:00
if ("loading" === document.readyState) {
2024-02-07 18:13:24 +01:00
document.addEventListener("DOMContentLoaded", initAjax);
2024-03-10 22:55:29 +01:00
} else {
setTimeout(initAjax, 0);
2024-04-25 10:19:24 +02:00
}
function uploadFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
let formData = new FormData();
formData.append('userfile', file);
2024-04-25 10:21:53 +02:00
formData.append('action', 'uploadFiles');
2024-04-25 10:19:24 +02:00
let xhr = new XMLHttpRequest();
2024-04-25 10:20:32 +02:00
xhr.open('POST', '/upload', true);
2024-04-25 10:19:24 +02:00
xhr.onload = function () {
2024-04-25 22:08:02 +02:00
const respData = JSON.parse(xhr.responseText);
handleResponse(resp, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru");
fileInput.reset();
2024-04-25 10:19:24 +02:00
};
2024-04-25 15:01:17 +02:00
xhr.send(formData);
}
2024-04-25 15:05:03 +02:00
function deleteFile(fileID) {
2024-04-25 15:01:17 +02:00
let formData = new FormData();
formData.append('action', 'deleteFile');
formData.append('file_id', fileID);
let xhr = new XMLHttpRequest();
xhr.open('POST', '/upload', true);
xhr.send(formData);
}
2024-04-25 22:38:04 +02:00
async function getFileList() {
2024-04-25 15:01:17 +02:00
let formData = new FormData();
formData.append('action', 'getAllFiles');
2024-04-25 22:47:06 +02:00
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
2024-04-25 15:01:17 +02:00
2024-04-25 22:47:06 +02:00
if (!response.ok) {
throw new Error('Network response was not ok');
2024-04-25 15:01:17 +02:00
}
2024-04-25 22:47:06 +02:00
const resp = await response.json();
if (resp.Status === "Success") {
return resp.Files;
} else {
return false;
}
} catch (error) {
return false;
}
2024-04-25 22:08:02 +02:00
}
2024-04-25 22:38:04 +02:00
async function listFiles() {
const fileList = getFileList();
if(fileList){
displayList(fileList, "filelist", deleteFile);
}
}
2024-04-25 22:08:02 +02:00
function addMeme() {
let formData = new FormData();
let memeTitleElement = document.getElementById("meme_title_input");
let memeTextElement = document.getElementById("meme_text_input");
let memeImageElement = document.getElementById("meme_image_input");
formData.append('action', 'addMeme');
formData.append('meme_title', memeTitleElement.value);
formData.append('meme_text', memeTextElement.value);
formData.append('meme_image_id', memeImageElement.value);
2024-04-25 15:01:17 +02:00
2024-04-25 22:08:02 +02:00
let xhr = new XMLHttpRequest();
xhr.open('POST', '/meme', true);
xhr.onload = function () {
const resp = JSON.parse(xhr.responseText);
handleResponse(resp, "Meme bol pridaný", "Nastala chyba pri pridávaní meme-u");
memeTitleElement.reset();
memeTextElement.reset();
memeImageElement.reset();
};
2024-04-25 15:05:33 +02:00
xhr.send(formData);
2024-04-25 22:38:04 +02:00
}
2024-04-26 01:17:49 +02:00
async function deleteMeme(memeId) {
let formData = new FormData();
formData.append('action', 'deleteMeme');
formData.append('meme_id', memeId);
let xhr = new XMLHttpRequest();
xhr.open('POST', '/meme', true);
xhr.onload = function () {
const resp = JSON.parse(xhr.responseText);
handleResponse(resp, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u");
};
xhr.send(formData);
}
2024-04-25 22:38:04 +02:00
async function getMemeImages() {
let memeImageSelector = document.getElementById("meme_image_input");
2024-04-25 22:41:23 +02:00
let fileList = await getFileList();
2024-04-25 22:38:04 +02:00
fileList.forEach((item) => {
let option = document.createElement("option");
option.value = item.ID;
2024-04-25 22:48:37 +02:00
let splitPath = item.Path.split("/");
option.text = `${splitPath[splitPath.length - 1]} - (${item.ID}) [${item.UploadedBy}]`;
2024-04-25 22:38:04 +02:00
memeImageSelector.appendChild(option);
});
2024-02-07 18:13:24 +01:00
}