cleanup parseForm

This commit is contained in:
maru21 2023-11-06 18:51:20 +01:00
parent 11521adff7
commit d737b43be6
8 changed files with 52 additions and 62 deletions

View File

@ -78,14 +78,6 @@ function handleOnBlur(t) {
activeState.lastElement = t.id; activeState.lastElement = t.id;
} }
function clickClearForm() {
//document.activeElement.blur();
//document.getElementById("sidebar").focus();
clearData("userInput");
let lT = activeState.loadedTemplate;
loadTemplate(lT);
}
function hideMenus(evt) { function hideMenus(evt) {
if (evt === undefined) return; if (evt === undefined) return;
if (evt != "force" && evt.target.parentElement != null) { if (evt != "force" && evt.target.parentElement != null) {
@ -311,7 +303,6 @@ export {
showTextBlocks, showTextBlocks,
insertTextBlocks, insertTextBlocks,
handleOnBlur, handleOnBlur,
clickClearForm,
modalNotifier, modalNotifier,
clickImportFiles, clickImportFiles,
resetNavBar, resetNavBar,

View File

@ -4,7 +4,7 @@ import {
retrieveData, retrieveData,
} from "./storage.js"; } from "./storage.js";
import { loadTemplate } from "./web.js"; import { loadTemplate } from "./web.js";
import parseFormOnSubmit from "./parseForm.js"; import parseForm from "./parseForm.js";
import { copyToClipBoard, modalNotifier, resetNavBar } from "./evts.js"; import { copyToClipBoard, modalNotifier, resetNavBar } from "./evts.js";
import { passwordHash } from "./scripts.js"; import { passwordHash } from "./scripts.js";
@ -153,7 +153,22 @@ function loadFileDivCallBack() {
fileDisplay.appendChild(title); fileDisplay.appendChild(title);
let div = document.createElement("div"); let div = document.createElement("div");
let parsedTemplate = parseFormOnSubmit(false, true); let parsedTemplate = parseForm(true);
if (parsedTemplate == null) {
let wrapper = document.createElement("div");
wrapper.innerHTML = mainFormPlaceholder("Error: file empty");
let div = wrapper.firstChild;
parsedTemplate = div;
}
if (parsedTemplate == "") {
let wrapper = document.createElement("div");
wrapper.innerHTML = mainFormPlaceholder("File empty");
let div = wrapper.firstChild;
parsedTemplate = div;
}
div.appendChild(parsedTemplate); div.appendChild(parsedTemplate);
fileDisplay.appendChild(div); fileDisplay.appendChild(div);
@ -231,7 +246,7 @@ function clearFileData(storData) {
storeData("templateFiles", newArray); storeData("templateFiles", newArray);
clearData(fileName); clearData(fileName);
clearData("userInput"); clearData("userInputForce");
document.getElementById("mainForm").innerHTML = ""; document.getElementById("mainForm").innerHTML = "";
document.getElementById("output").innerHTML = ""; document.getElementById("output").innerHTML = "";

View File

@ -1,9 +1,9 @@
import { storeData, createStorageObj } from "./storage.js"; import { retrieveData, storeData, createStorageObj } from "./storage.js";
import parseInput from "./parseTemplate.js"; import parseInput from "./parseTemplate.js";
import transformTemplateObject from "./buildForm.js"; import transformTemplateObject from "./buildForm.js";
import { handleOnBlur, modalNotifier } from "./evts.js"; import { copyToClipBoard, handleOnBlur, modalNotifier } from "./evts.js";
import parseFormOnSubmit, { parseTextMarkups } from "./parseForm.js"; import parseForm, { parseTextMarkups } from "./parseForm.js";
import { setTemplatePreset } from "./web.js"; import { setTemplatePreset } from "./web.js";
function buildForm(templateInput, loadOnly = false) { function buildForm(templateInput, loadOnly = false) {
@ -225,7 +225,7 @@ function formEvts() {
switch (e.target.value) { switch (e.target.value) {
case "Copy": case "Copy":
createStorageObj("copy"); createStorageObj("copy");
parseFormOnSubmit(); copyToClipBoard(parseForm());
e.target.className = e.target.className.replace(" w3-grey", " w3-flat-carrot"); e.target.className = e.target.className.replace(" w3-grey", " w3-flat-carrot");
e.target.style.pointerEvents = "none"; e.target.style.pointerEvents = "none";
e.target.value = "Copied"; e.target.value = "Copied";
@ -256,15 +256,15 @@ function formEvts() {
document.getElementById("sidebar").addEventListener("click", (e) => { document.getElementById("sidebar").addEventListener("click", (e) => {
if (e.target) { if (e.target) {
if (e.target.id == "sb-submit") { if (e.target.id == "sb-submit") {
createStorageObj("save"); createStorageObj("copy");
parseFormOnSubmit(); copyToClipBoard(parseForm());
modalNotifier("File saved and copied to clipboard", activeState.settings.notifierPause); modalNotifier("File saved and copied to clipboard", activeState.settings.notifierPause);
} }
if (e.target.id == "sb-setform") { if (e.target.id == "sb-setform") {
createStorageObj("setform"); createStorageObj("setform");
let dataArray = parseFormOnSubmit(true); let dataArray = retrieveData("userInput");
let lT = activeState.loadedTemplate; let lT = activeState.loadedTemplate;
setTemplatePreset(lT, JSON.stringify(dataArray)); setTemplatePreset(lT, dataArray);
modalNotifier("Input saved as preset", activeState.settings.notifierPause); modalNotifier("Input saved as preset", activeState.settings.notifierPause);
} }
if (e.target.id == "sb-item") { if (e.target.id == "sb-item") {

View File

@ -5,14 +5,15 @@ import {
showTextBlocks, showTextBlocks,
clickImportFiles, clickImportFiles,
modalNotifier, modalNotifier,
printVersion printVersion,
copyToClipBoard
} from "./evts.js"; } from "./evts.js";
import { buildFile } from "./files.js"; import { buildFile } from "./files.js";
import setPassword, { import setPassword, {
passwordHash, passwordHash,
inputRead inputRead
} from "./scripts.js"; } from "./scripts.js";
import parseFormOnSubmit from "./parseForm.js"; import parseForm from "./parseForm.js";
import { createStorageObj, storeSettings } from "./storage.js"; import { createStorageObj, storeSettings } from "./storage.js";
import { loadNavBar, initTextBlocks, loadNewTemplate } from "./web.js"; import { loadNavBar, initTextBlocks, loadNewTemplate } from "./web.js";
@ -147,7 +148,7 @@ function eventListeners() {
inputRead.read(e); inputRead.read(e);
if (e.ctrlKey && e.key == "s") { if (e.ctrlKey && e.key == "s") {
createStorageObj("copy"); createStorageObj("copy");
parseFormOnSubmit(); copyToClipBoard(parseForm());
modalNotifier("File copied to clipboard", activeState.settings.notifierPause); modalNotifier("File copied to clipboard", activeState.settings.notifierPause);
let copyButton = document.getElementById("fromCopyBtn"); let copyButton = document.getElementById("fromCopyBtn");
copyButton.className = "w3-button w3-flat-carrot"; copyButton.className = "w3-button w3-flat-carrot";

View File

@ -1,34 +1,22 @@
import { copyToClipBoard } from "./evts.js"; import { clearData, retrieveData } from "./storage.js";
import { storeData, clearData, retrieveData } from "./storage.js";
function parseFormOnSubmit(returnJSON = false, parseOnly = false) { function parseForm(returnDIV = false) {
//event.preventDefault;
//get user Inout Data from preset or from file
let dataArray = []; let dataArray = [];
if (returnDIV) {
if (parseOnly) { dataArray = retrieveData("userInputForce");
let lT = activeState.loadedTemplate; clearData("userInputForce");
dataArray = retrieveData("userInputForce", lT);
if (dataArray == null) {
let wrapper = document.createElement("div");
wrapper.innerHTML = mainFormPlaceholder("Error: file empty");
let div = wrapper.firstChild;
return div;
}
if (dataArray.length <= 0) {
let wrapper = document.createElement("div");
wrapper.innerHTML = mainFormPlaceholder("File empty");
let div = wrapper.firstChild;
return div;
}
} else { } else {
dataArray = retrieveData("userInput"); dataArray = retrieveData("userInput");
} }
//if the decryption went wrong return null
if (dataArray == null) return dataArray;
//if there are no files yet return empty array
if (dataArray == []) return "";
//get original objects from sessionstorage gen from loadTemplate //get original objects from sessionstorage gen from loadTemplate
let objects = activeState.templateObjects; let objects = activeState.templateObjects;
@ -89,6 +77,7 @@ function parseFormOnSubmit(returnJSON = false, parseOnly = false) {
} }
} }
//build final output string by inserting objects
b = fullString.substring(0, objects[0].spos); b = fullString.substring(0, objects[0].spos);
for (let i = 0; i < objects.length; i++) { for (let i = 0; i < objects.length; i++) {
let j = i + 1; let j = i + 1;
@ -106,7 +95,7 @@ function parseFormOnSubmit(returnJSON = false, parseOnly = false) {
} }
} }
//sanitizing //sanitizing final output string
let bHtml = b; let bHtml = b;
bHtml = bHtml.replace(/ /g, "&nbsp;"); bHtml = bHtml.replace(/ /g, "&nbsp;");
bHtml = bHtml.replace(/(?:\r\n|\r|\n)/g, "<br />"); bHtml = bHtml.replace(/(?:\r\n|\r|\n)/g, "<br />");
@ -122,6 +111,7 @@ function parseFormOnSubmit(returnJSON = false, parseOnly = false) {
bHtml = bHtml.replace(/!ls /g, " ○ "); bHtml = bHtml.replace(/!ls /g, " ○ ");
} }
//creating output div
let divContent = document.createElement("div"); let divContent = document.createElement("div");
divContent.style.fontFamily = activeState.settings.font + ", Helvetica, sans-serif"; divContent.style.fontFamily = activeState.settings.font + ", Helvetica, sans-serif";
divContent.style.fontSize = activeState.settings.fontSize; divContent.style.fontSize = activeState.settings.fontSize;
@ -132,17 +122,10 @@ function parseFormOnSubmit(returnJSON = false, parseOnly = false) {
div.id = "fileDisplay"; div.id = "fileDisplay";
div.appendChild(divContent); div.appendChild(divContent);
if (parseOnly) { if (returnDIV) {
return div; return div;
}
storeData("userInput", dataArray);
clearData("userInput");
if (returnJSON) {
return dataArray;
} else { } else {
copyToClipBoard(bHtml); return bHtml;
} }
} }
@ -387,8 +370,5 @@ function loadTextBlocks() {
return textBlocksObject; return textBlocksObject;
} }
function mainFormPlaceholder(msg) {
return "<div class='w3-row-padding w3-padding-24 w3-container w3-flat-clouds'><div class='w3-code notranslate w3-border-white' style='font-family: Arial, Helvetica, sans-serif;'><p>" + msg + "</p><br><br><br><br><br><br><br><br><br><br><br></div></div>";
}
export default parseFormOnSubmit; export default parseForm;

View File

@ -154,6 +154,8 @@ function retrieveData(name, template = "none") {
function clearData(name, template = "none") { function clearData(name, template = "none") {
let lT; let lT;
let key; let key;
if (name == "userInputForce") name = "userInput";
if (template == "none") { if (template == "none") {
lT = activeState.loadedTemplate; lT = activeState.loadedTemplate;
key = name + "_m21_" + lT; key = name + "_m21_" + lT;

View File

@ -47,6 +47,7 @@ function loadTemplate(template, newFlag = false, loadOnly = false) {
cdata = retrieveData("templatePreset", template); cdata = retrieveData("templatePreset", template);
} else { } else {
cdata = retrieveData("userInputForce"); cdata = retrieveData("userInputForce");
clearData("userInputForce");
} }
if (cdata != "") { if (cdata != "") {
if (cdata != null) { if (cdata != null) {
@ -212,7 +213,7 @@ function setTemplatePreset(template, formData) {
}; };
let data = { let data = {
template: template, template: template,
data: formData, data: JSON.stringify(formData),
}; };
xhttp.send(JSON.stringify(data)); xhttp.send(JSON.stringify(data));

View File

@ -1 +1 @@
[{"value":"Folgetermin Sozialdienst","name":"Termin_Sozialdienst"},{"value":"Pat.","name":"Involvierte_Personen"},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":"Eintrag_in_Listenform"},{"value":"!none","name":"Procedere_einblenden"},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":"cl-Procedere:!l"}] [{"value":"Folgetermin Sozialdienst","name":"Termin_Sozialdienst"},{"value":"Pat.","name":"Involvierte_Personen"},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"<div><br></div>","name":"Eintrag_in_Listenform"},{"value":"!none","name":"clM-Procedere_einblenden"},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":""},{"value":"","name":"cl-Procedere:!l"}]