// stand.js (stand_js.txt)
// @version 1.1.3
// @copyright 2022, Friedrich Wichmann
// Javascript Code für Standbesetzungsumfragen
'use strict';
const VERSIONjs="1.1.3";
var st_nopts = 3;
var st_test = 0;
var st_opton = [1, 1, 1, 1, 1, 1, 1, 1, 1, ];
/* Initialisierung mit den Daten aus Datenbank und Setup */
function initStand(jsonres) {
initStand(jsonres, false);
}
function initStand(jsonres, btwo) {
let len1 = (jsonres.a) ? jsonres.a.length : "undefined";
let nopts = (btwo) ? jsonres.inf2.nopts : jsonres.inf.nopts;
let nr = (btwo) ? jsonres.nr2 : jsonres.nr;
st_nopts = (nopts && nopts > 0 && nopts < 10) ? nopts : 3;
dlog("initStand nr="+nr+" nopts="+st_nopts+" len="+len1+" "+VERSIONjs);
st_test = (nr==99);
if (jsonres.a == undefined) {
return;
}
initStandInfos((btwo) ? jsonres.inf2 : jsonres.inf);
initStandTable(nr, jsonres.a, jsonres.inpo);
dlog(jsonres);
}
/* Initialisierung der Standtabelle mit den Daten aus Datenbank und Setup */
function initStandTable(standnr, jsonarr, inpo) {
if (jsonarr.length) {
/* Extraausgabe von Name und Kommentar von Eintrag [0] */
var s1 = document.querySelector("#st-res-1");
if (s1) {
s1.innerHTML = ""+jsonarr[0].name+" ("+jsonres[0].kommentar+")";
dlog("stand[0] name="+jsonarr[0].name+" kommentar="+jsonres[0].kommentar);
}
else dlog("querySelector #st-res-1 ohne Ergebnis (ok)");
}
/* Standnummer prüfen */
var form1 = document.querySelector("#st-plan-1");
var nrin = document.querySelector("#st-plan-1 input[name='st-nr-in']");
if (nrin) {
var stnrdb = (jsonarr.length) ? jsonarr[0].nr : 1;
nrin.setAttribute("value", standnr);
dlog("init Attribut 'value' von "+nrin.name+" auf "+standnr+" gesetzt"
+" (stnrdb "+stnrdb+" inpo "+inpo+")");
if (stnrdb != standnr && !inpo) {
form1.submit();
return;
}
}
else {
dlog("querySelector für st-nr-in ohne Ergebnis");
return;
}
/* Standdatum setzen */
/* Datenbankeinträge in neue Tabellenzeilen übertragen */
var tab1 = document.querySelector("#st-tab-tb1");
if (!tab1) dlog("querySelector #st-tab-1 ohne Ergebnis");
for (var i=0; iEdit */
var tb4e = document.createElement("button");
tb4e.type = "button";
tb4e.id = "st-x-edt-" + i;
tb4e.classList.add("st-b-edt");
tb4e.appendChild(tx4e);
td4.appendChild(tb4e);
var tx4l = document.createTextNode("Löschen");
/* */
var tb4l = document.createElement("button");
tb4l.type = "submit";
tb4l.id = "st-x-del-" + i;
tb4l.classList.add("st-b-del");
tb4l.appendChild(tx4l);
td4.appendChild(tb4l);
trow.appendChild(td4);
tab1.appendChild(trow);
}
/* Initialisierung Standinformationen */
function initStandInfos(jsoninf) {
var sp1 = document.querySelector("#st-wann");
if (sp1 && jsoninf.wann) {
sp1.innerHTML = jsoninf.wann;
}
for (var col = 1; col <= st_nopts; col++) {
var th1 = document.querySelector("#st-col"+col+"-th");
if (th1 && jsoninf.opt && jsoninf.opt[col-1]) {
var tx1 = jsoninf.opt[col-1];
th1.innerHTML = tx1;
st_opton[col-1] = 1;
if (tx1.length == 0 || tx1 == "-") {
th1.classList.add("st-optoff");
st_opton[col-1] = 0;
var td2 = document.querySelector("#st-col"+col+"-td-n");
var td3 = document.querySelector("#st-col"+col+"-td-b");
if (td2) td2.classList.add("st-optoff");
if (td3) td3.classList.add("st-optoff");
}
}
}
}
/* Optionszahlenwert in Ausgabe-Zeichen umwandeln */
function toOptChar(iOpt) {
if (iOpt==1) return "⊕";
if (iOpt==2) return "??";
return "⊖";
}
/* Optionszeichen in Zahlenwert umwandeln */
function toOptInt(ch) {
if (ch=='j' || ch=='⊕') return 1;
if (ch=='v' || ch=='??') return 2;
return 0;
}
/* Optionszahlenwert in Id (j/v/n) umwandeln */
function toOptId(iOpt) {
if (iOpt==1) return 'j';
if (iOpt==2) return 'v';
return 'n';
}
/* Eventhandler für Optionsknopf-Klick */
function standClick(bid, iopt, swi)
{
dlog("click id=" + bid + " iopt=" + iopt + " swi=" + swi);
for (var i=0; i<=2; i++) {
var tb = document.querySelector("#st-opt"+iopt+"-n-"+toOptId(i));
if (!tb) {
dlog("click id=#st-opt"+iopt+"-n-"+toOptId(i)+" not found");
continue;
}
if (i == swi) {
tb.hidden="1";
let e = tb.nextSibling;
if (e && e.nodeType == Node.TEXT_NODE) {
e.textContent = toOptChar(i) + e.textContent;
} else {
var tx = document.createTextNode(toOptChar(i));
var tp = tb.parentElement;
e = tp.appendChild(tx);
}
e.class = "st-click-t";
} else {
tb.hidden=null;
var e = tb.nextSibling;
if (e && e.class == "st-click-t" && e.textContent.charAt(0) == toOptChar(i).charAt(0)) {
e.textContent = e.textContent.substr( (i==2) ? 2 : 1);
}
}
var tp = tb.parentElement;
while (tp && tp.tagName != "TD") {
tp = tp.parentElement;
}
if (tp) {
tp.classList.remove('st-opt-j', 'st-opt-v', 'st-opt-n');
if (swi==0) tp.classList.add('st-opt-n');
if (swi==1) tp.classList.add('st-opt-j');
if (swi==2) tp.classList.add('st-opt-v');
}
}
}
/* Eventhandler für Standumfrage 1/2 */
function standClick2(bid)
{
var bt1 = document.querySelector("#st-b-tst-1");
var bt2 = document.querySelector("#st-b-tst-2");
var chk = (bt1 && bt1.disabled) ? 1 : (bt2 && bt2.disabled) ? 2 : 0;
dlog("click id=" + bid + " chk="+chk);
if (chk == 2) {
bt1.disabled = "1";
bt2.disabled = "";
/* TODO: Fügt Teile mehrfach ein... */
initStand(jsonresult, true);
} else if (chk == 1) {
bt1.disabled = "";
bt2.disabled = "1";
/* TODO: Fügt Teile mehrfach ein... */
initStand(jsonresult, false);
}
}
/* Eventhandler für Edit-/Leer-Knopf-Klick */
function standEditOp(bid)
{
dlog("click id=" + bid);
var row = (bid.startsWith("st-x-edt-")) ? bid.substr(9) : 'n';
var leer = (row == 'n');
/* Name leer/übertragen */
var tda = document.querySelector("#st-cola-td-"+row);
var inp = document.querySelector("input[name='st-cola-in']");
if (leer && inp) {
inp.value = '';
}
else if (tda && inp) {
var nam = tda.textContent;
inp.value = nam;
}
/* Optionen init/übertragen */
for (var col = 1; col <= st_nopts; col++) {
var tdo = document.querySelector("#st-col"+col+"-td-"+row);
if (tdo && leer) {
dlog("click id=" + bid + " col=" + col + " row=" + row);
tdo.classList.remove('st-opt-j', 'st-opt-v', 'st-opt-n');
for (var i=0; i<=2; i++) {
var tb = document.querySelector("#st-opt"+col+"-n-"+toOptId(i));
if (!tb) {
dlog("click id=#st-opt"+col+"-n-"+toOptId(i)+" not found");
continue;
}
tb.hidden=null;
var e = tb.nextSibling;
if (e && e.class == "st-click-t" && e.textContent.charAt(0) == toOptChar(i).charAt(0)) {
e.textContent = e.textContent.substr( (i==2) ? 2 : 1);
}
}
}
else if (tdo) {
var opid = "st-opt"+col+"-n-"+toOptId(col);
var swi = toOptInt(tdo.textContent);
dlog("click id=" + bid + " col=" + col + " tdo=" + tdo.textContent + " swi=" + swi + " opid=" + opid);
standClick(opid, col, swi);
}
}
/* Kommentar leer/übertragen */
var tdb = document.querySelector("#st-colb-td-"+row);
var inb = document.querySelector("input[name='st-colb-in']");
if (leer && inb) {
inb.value = '';
}
else if (tdb && inb) {
var komm = tdb.textContent;
inb.value = komm;
}
}
/* Eventhandler für Formularoperationen */
function standSubmitOp(bid, op)
{
dlog("standSubmitOp bid="+bid);
/* Operation eintragen */
var tdo = document.querySelector("input[name='st-do']");
if (tdo) {
tdo.value = op;
}
/* ggf. Name übertragen */
if (op=="33D") {
var i = (bid.startsWith("st-x-del-")) ? bid.substr(9) : '';
var tda = document.querySelector("#st-cola-td-"+i);
var inp = document.querySelector("input[name='st-cola-in']");
if (tda && inp) {
var nam = tda.textContent;
inp.value = nam;
}
dlog("standSubmitOp nam="+nam);
}
/* Optionen eintragen */
for (var col=1; col<=st_nopts; col++) {
var td = document.querySelector("#st-col"+col+"-td-n");
var ov = 0;
if (td) {
if (td.classList.contains("st-opt-n")) ov = 0;
else if (td.classList.contains("st-opt-j")) ov = 1;
else /* td.classList.contains("st-opt-v") */ ov = 2;
}
var tinp = document.querySelector("input[name='st-opt"+col+"-n']");
if (tinp) {
tinp.value = ov;
}
}
/* Für lokale Version Servlet-Pfad benutzen */
/*if (location.hostname == "localhost" && op!="") {
if (location.pathname.indexOf("/servlet/") < 0) {
var href = "http://"+location.host+"/servlet/StandServlet"+location.pathname;
location.href = href;
}
}*/
}