templateGen/js/9.9.9/init.js
2023-10-28 20:42:23 +02:00

155 lines
4.1 KiB
JavaScript

import {
hideMenus,
showMenu,
showSidebar,
showTextBlocks,
clickImportFiles,
modalNotifier,
printVersion,
} from "./evts.js";
import { buildFile } from "./files.js";
import setPassword, {
passwordHash,
inputRead,
getUsrId,
} from "./scripts.js";
import parseFormOnSubmit from "./parseForm.js";
import { createStorageObj, storeSettings } from "./storage.js";
import { loadNavBar, initTextBlocks } from "./web.js";
window.activeState = {
userId: "",
sessionToken: "",
activePage: "landing",
loadedTemplate: "",
fileName: "",
lastElement: "",
serverFilesTs: "",
settings: {
localOnly: "true",
lineBreak: 120,
font: "Arial",
fontSize: "10px",
notifierPause: 1,
persistentStorage: "false"
},
templates: [],
templateObjectsPurified: [],
templateObjects: [],
fullString: "",
templateFieldTypes: [
"simpleInput",
"longText",
"hiddenField",
"current_time",
"current_date",
"markup",
],
markups: ["title", "link", "italic", "green_highlighted", "highlighted"],
storage: []
};
function init() {
//init passwordhash to retrieve cookie info and set passwordHash
passwordHash.initHash();
//set user id
activeState.userId = getUsrId();
//check if user is logged in
if (passwordHash.verify()) {
//user logged in
document.getElementById("login").style.display = "none";
} else {
document.getElementById("login").style.display = "block";
}
//load settings from storage and apply
let settings = storeSettings("getInit", true);
if (settings != null) {
for (let setting of Object.entries(settings)) {
activeState.settings[setting[0]] = setting[1];
}
}
//load NavigationBar with templates according to server
loadNavBar();
//init Textblocks field with entries according to server
initTextBlocks();
//add event listeners to document and window
eventListeners();
//print current version and storage mode to footer
let msg = (activeState.settings.persistentStorage == "false") ? "temporary" : "persistent";
printVersion("storage mode: "+msg+" |");
//adjust title for mobile use
if (screen.width < 993) {
document.getElementById("siteTitle").innerHTML = "TG";
}
}
function eventListeners() {
//add hideMenu to Body
document
.body
.addEventListener("click", (e) => hideMenus(e));
//add set Password to loginForm
document
.getElementById("submitPassword")
.addEventListener("click", setPassword);
//add toggle Navigation Menu
document
.getElementById("toggleNavigationMenu")
.addEventListener("click", showMenu);
//add loadTemplateBtn event showMenu
document
.getElementById("loadTemplateBtn")
.addEventListener("click", showMenu);
//add toggle sideBar Menu
document
.getElementById("toggleSidebarMenu")
.addEventListener("click", showSidebar);
//add toggle files Menu and sidebar button
document
.getElementById("toggleFilesMenuSB")
.addEventListener("click", buildFile);
document
.getElementById("toggleFilesMenu")
.addEventListener("click", buildFile);
//add toggle textBLocks Menu
document
.getElementById("toggleTestBlocksMenu")
.addEventListener("click", showTextBlocks);
//add saveFiles to server listener on launch page
document
.getElementById("importFilesSB")
.addEventListener("click", () => clickImportFiles());
//add key listener for ctrl s in form mode
inputRead.init();
window.addEventListener("keydown", (e) => {
if (activeState.activePage == "template") {
inputRead.read(e);
if (e.ctrlKey && e.key == "s") {
createStorageObj();
parseFormOnSubmit();
modalNotifier("File copied to clipboard", activeState.settings.notifierPause);
let copyButton = document.getElementById("fromCopyBtn");
copyButton.className.replace(" w3-grey", " w3-flat-carrot");
copyButton.value = "Copied";
e.preventDefault();
}
}
});
}
init();