stylehub/assets/script.js

742 lines
23 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 && UserInfo.Email.length > 0;
2024-02-07 18:41:15 +01:00
}
async function setElementClasses(element, newClasses) {
// Ensure the element exists
if (!element) return;
// Clear all existing classes
element.className = '';
// Add the new classes to the element
element.classList.add(...newClasses);
}
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");
if (data.Status === 'Success') {
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";
2024-04-26 15:00:18 +02:00
const params = new FormData();
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);
}
if (typeof deleteFunction === "function") {
2024-02-04 09:50:04 +01:00
const th = document.createElement("th");
2024-04-26 01:21:44 +02:00
let deleteBtn = document.createElement('i');
deleteBtn.classList.add("ri-delete-bin-line");
2024-04-26 01:21:44 +02:00
th.appendChild(deleteBtn);
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);
}
if (typeof deleteFunction === "function") {
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-05-16 10:03:26 +02:00
async function togglememecreate() {
"use strict";
let memeContainerElement = document.getElementById("memecreatecontainer");
memeContainerElement.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) {
if (ev.key === "Escape") {
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);
if (UserInfo.Privileges < 2) {
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-05-15 19:18:15 +02:00
if (currentSite === "account" && currentPage === "settings") {
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
}
}
if (currentSite === "account" && currentPage === "index" && isLoggedIn()) {
2024-02-06 20:19:36 +01:00
await showDashboardGreeting();
2024-02-06 20:00:32 +01:00
}
if (currentSite === "news" && currentPage === "index") {
2024-02-22 10:43:18 +01:00
await articleInit();
}
if (currentSite === "account" && currentPage === "files") {
2024-04-25 15:01:17 +02:00
await listFiles();
}
if (currentSite === "memes" && currentPage === "index") {
2024-04-25 22:38:04 +02:00
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-05-01 17:29:27 +02:00
UserInfo = {};
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);
if (result && result.Status === "Success") {
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
if (result && result.Status === "Success") {
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
if (document.readyState === "loading") {
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
}
2024-04-26 14:55:45 +02:00
async function uploadFile() {
2024-04-25 10:19:24 +02:00
const fileInput = document.getElementById('fileInput');
2024-04-26 15:03:12 +02:00
const fileForm = document.getElementById('uploadForm');
2024-04-26 14:55:45 +02:00
let data = {
action: "uploadFiles"
2024-04-25 10:19:24 +02:00
};
2024-04-26 14:59:04 +02:00
for (let i = 0; i < fileInput.files.length; i++) {
data[`userFile${i}`] = fileInput.files[i];
}
2024-04-26 14:55:45 +02:00
await doAction("/upload", data, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru", false);
2024-04-26 15:03:12 +02:00
fileForm.reset();
2024-04-27 11:56:05 +02:00
await listFiles();
2024-04-25 15:01:17 +02:00
}
2024-04-26 14:55:45 +02:00
async function deleteFile(fileID) {
await doAction("/upload", {
action: "deleteFile",
file_id: fileID
}, "Súbor bol úspešne zmazaný", "Nastala chyba pri mazaní súboru", true);
await listFiles();
2024-04-25 15:01:17 +02:00
}
2024-04-25 22:38:04 +02:00
async function getFileList() {
2024-04-26 14:55:45 +02:00
const resp = await doAction("/upload", {
action: "getAllFiles"
}, "Zoznam súborov bol úspešne stiahnutý", "Nastala chyba pri sťahovaní zoznamu súborov", true);
2024-04-25 15:01:17 +02:00
if (resp.Status === "Success") {
2024-04-26 14:37:54 +02:00
return resp.Files;
} else {
2024-04-25 22:47:06 +02:00
return false;
}
2024-04-25 22:08:02 +02:00
}
2024-04-25 22:38:04 +02:00
async function listFiles() {
2024-04-26 14:37:54 +02:00
const fileList = await getFileList();
if (fileList) {
await displayList(fileList, "filelist", deleteFile);
}
2024-04-25 22:38:04 +02:00
}
2024-04-26 14:55:45 +02:00
async function addMeme() {
2024-04-25 22:08:02 +02:00
let memeTitleElement = document.getElementById("meme_title_input");
let memeTextElement = document.getElementById("meme_text_input");
let memeImageElement = document.getElementById("meme_image_input");
2024-04-26 15:03:12 +02:00
let memeFormElement = document.getElementById("memeAddForm");
2024-04-26 14:55:45 +02:00
await doAction("/meme", {
action: "addMeme",
meme_title: memeTitleElement.value,
meme_text: memeTextElement.value,
meme_image_id: memeImageElement.value
}, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u", false);
2024-04-26 15:03:12 +02:00
memeFormElement.reset();
2024-05-16 10:12:23 +02:00
await togglememecreate();
2024-04-25 22:38:04 +02:00
}
2024-04-26 01:17:49 +02:00
async function deleteMeme(memeId) {
2024-04-26 14:55:45 +02:00
await doAction("/meme", {
action: "deleteMeme",
meme_id: memeId
}, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u", false);
2024-04-26 15:06:08 +02:00
await softReload();
2024-04-26 01:17:49 +02:00
}
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("/");
2024-04-26 15:12:05 +02:00
option.text = `${splitPath[splitPath.length - 1]} - ID: (${item.ID}) Autor: [${item.UploadedBy} (${item.UploadedByID})]`;
2024-04-25 22:38:04 +02:00
memeImageSelector.appendChild(option);
});
2024-04-27 10:30:01 +02:00
}
async function reloadMemeVotes(memeID) {
let memeVoteCounterElement = document.getElementById(`meme_votes_counter_${memeID}`);
2024-04-27 11:23:42 +02:00
let memeVoteUpvoteElement = document.getElementById(`meme_votes_upvote_${memeID}`);
let memeVoteDownvoteElement = document.getElementById(`meme_votes_downvote_${memeID}`);
2024-04-27 12:17:20 +02:00
let memeVoteUpvoteButtonElement = document.getElementById(`meme_votes_upvote_button_${memeID}`);
let memeVoteDownvoteButtonElement = document.getElementById(`meme_votes_downvote_button_${memeID}`);
2024-04-27 10:30:01 +02:00
let memeVoteResponse = await doAction('/meme', {
action: "getMemeVotes",
meme_id: memeID
}, "Počet hlasov k meme-u bol stiahnutý", "Nastala chyba pri sťahovaní počtu hlasov k meme-u", true);
2024-04-27 11:23:42 +02:00
let memeVotes = memeVoteResponse.NetVotes;
let userVote = memeVoteResponse.UserVote;
memeVoteCounterElement.innerText = memeVotes;
memeVoteCounterElement.classList.remove("positive", "negative", "neutral");
if (0 < memeVotes) {
2024-04-27 11:23:42 +02:00
memeVoteCounterElement.classList.add("positive");
2024-05-15 19:18:15 +02:00
} else if (0 > memeVotes) {
2024-04-27 11:23:42 +02:00
memeVoteCounterElement.classList.add("negative");
2024-05-15 19:18:15 +02:00
} else {
2024-04-27 11:23:42 +02:00
memeVoteCounterElement.classList.add("neutral");
}
2024-04-27 12:17:20 +02:00
memeVoteUpvoteButtonElement.classList.remove('visual_hover');
memeVoteDownvoteButtonElement.classList.remove('visual_hover');
let memeUpvoteVariant = "line";
let memeDownvoteVariant = "line";
if (0 < userVote) {
2024-04-27 11:23:42 +02:00
memeUpvoteVariant = "fill";
2024-04-27 12:17:20 +02:00
memeVoteUpvoteButtonElement.classList.add('visual_hover');
} else if (0 > userVote) {
2024-04-27 11:23:42 +02:00
memeDownvoteVariant = "fill";
2024-04-27 12:17:20 +02:00
memeVoteDownvoteButtonElement.classList.add('visual_hover');
2024-04-27 11:23:42 +02:00
}
await setElementClasses(memeVoteUpvoteElement, [`ri-arrow-up-circle-${memeUpvoteVariant}`]);
await setElementClasses(memeVoteDownvoteElement, [`ri-arrow-down-circle-${memeDownvoteVariant}`])
2024-04-27 11:23:42 +02:00
2024-04-27 10:30:01 +02:00
}
2024-05-15 19:18:15 +02:00
async function voteMeme(memeID, isUpvote) {
2024-04-27 11:28:30 +02:00
let memeVoteUpvoteElement = document.getElementById(`meme_votes_upvote_${memeID}`);
let memeVoteDownvoteElement = document.getElementById(`meme_votes_downvote_${memeID}`);
let memeVoteDelete = false;
2024-05-15 19:18:15 +02:00
if (isUpvote) {
if (memeVoteUpvoteElement.classList.contains("ri-arrow-up-circle-fill")) {
await deleteVoteMeme(memeID);
2024-04-27 11:28:30 +02:00
memeVoteDelete = true;
}
2024-05-15 19:18:15 +02:00
} else {
if (memeVoteDownvoteElement.classList.contains("ri-arrow-down-circle-fill")) {
await deleteVoteMeme(memeID);
2024-04-27 11:28:30 +02:00
memeVoteDelete = true;
}
}
if (!memeVoteDelete) {
await doAction("/meme", {
action: "voteMeme",
meme_id: memeID,
is_upvote: isUpvote
}, "Meme bol votovaný", "Nastala chyba pri votovaný", true);
}
2024-04-27 10:30:01 +02:00
await reloadMemeVotes(memeID);
2024-04-27 11:28:30 +02:00
}
async function deleteVoteMeme(memeId) {
await doAction("/meme", {
action: "deleteVoteMeme",
meme_id: memeId
}, "Hlas na meme bol zmazaný", "Nastala chyba pri mazaní hlasu na meme", true);
await reloadMemeVotes(memeId);
2024-05-15 19:18:15 +02:00
}
async function surveySubmit() {
2024-05-15 19:41:00 +02:00
const satisfaction = document.querySelector('input[name="satisfaction"]:checked');
const functionality = document.querySelector('input[name="functionality"]:checked');
const content = document.querySelector('input[name="content"]:checked');
2024-05-15 19:49:36 +02:00
const comment = document.querySelector('textarea[name="comment"]');
if (satisfaction && functionality && content && comment.value) {
await doAction("/survey", {
2024-05-15 19:18:15 +02:00
action: "surveySubmit",
2024-05-15 19:41:00 +02:00
satisfaction: satisfaction.value,
functionality: functionality.value,
content: content.value,
2024-05-15 19:49:36 +02:00
comment: comment.value
}, "Zaznamenané",
"Nastala chyba");
satisfaction.checked = false;
functionality.checked = false;
content.checked = false;
comment.value = "";
2024-05-15 19:18:15 +02:00
}
2024-02-07 18:13:24 +01:00
}