function getAllData(){ $( "#data" ).empty(); var url_string = window.location.href var url = new URL(url_string); if(!url.searchParams.has("display")){ var urlarray = ["aktivieren","aktivierenPrio","stornieren","moveTop","moveX","edit"] }else{ var urlparams = url.searchParams.get("display").replace("/",""); var urlarray = urlparams.split(","); } $.ajax({ type: "GET", url: 'data.db', success: function(ret) { if(!$(ret.data).length == 0){ container = document.getElementById("data"); entry = document.createElement("div"); tablehead = ""; urlarray.forEach(function(element) { tablehead += "" }); if(ret.data[0].hasOwnProperty('pkvalue')){ tablehead += ""; } var joinkey = urlarray.length; for (var i = 0; i < Object.keys(JSON.parse(ret.data[0].alldata)).length; i++) { tablehead += "' } tablehead += "
Primary Key" + Object.keys(JSON.parse(ret.data[0].alldata))[i] if(url.searchParams.has("showfilter")){ tablehead += '
' } tablehead += '
"; $(entry).html(tablehead); $(container).append(entry); container = document.getElementById("tablecontent"); entry = document.createElement("tr"); $(entry).attr("id","AddEntry"); $(entry).attr("class","tableentry"); tableedit = "Add" urlarray.forEach(function(element) { }); for (var i = 1; i < urlarray.length; i++) { //start at 1 as add is already set tableedit += "" } if(ret.data[0].hasOwnProperty('pkvalue')){ tableedit += "Primary Key"; } tableedit += "" + Object.keys(JSON.parse(ret.data[0].alldata)).join("") + "" $(entry).append(tableedit); $("#tablecontent").append(entry); $.each(ret.data, function(i, item) { setSingleData(item, urlarray, i, url); }); } } }); } function searchtable(key) { console.log("AAA"); // Declare variables var input, filter, table, tr, td, i, txtValue; input = document.getElementById("searchtable" +key); filter = input.value.toUpperCase(); table = document.getElementById("tablecontent"); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[key]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } function sortTable(n) { var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; table = document.getElementById("tablecontent"); switching = true; // Set the sorting direction to ascending: dir = "asc"; /* Make a loop that will continue until no switching has been done: */ while (switching) { // Start by saying: no switching is done: switching = false; rows = table.rows; /* Loop through all table rows (except the first, which contains table headers): */ for (i = 1; i < (rows.length - 1); i++) { // Start by saying there should be no switching: shouldSwitch = false; /* Get the two elements you want to compare, one from current row and one from the next: */ x = rows[i].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n]; /* Check if the two rows should switch place, based on the direction, asc or desc: */ if (dir == "asc") { if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { // If so, mark as a switch and break the loop: shouldSwitch = true; break; } } else if (dir == "desc") { if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { // If so, mark as a switch and break the loop: shouldSwitch = true; break; } } } if (shouldSwitch) { /* If a switch has been marked, make the switch and mark that a switch has been done: */ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; // Each time a switch is done, increase this count by 1: switchcount ++; } else { /* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the while loop again. */ if (switchcount == 0 && dir == "asc") { dir = "desc"; switching = true; } } } } function deleteDataElement(dataelement){ console.log("Löschen") console.log(dataelement) $.ajax({ type: "DELETE", url: location.protocol + '//' + location.host + location.pathname, contentType: "application/json", headers: {"content-id": "deleteByID"}, data: JSON.stringify(dataelement), success: function(ret) { getAllData(); } }); } //if used in frames send row to frames function sendDataElementBack(dataelement, type){ parent.sendJson(window.name,{ type: type, dataelement: dataelement }) //deleteDataElement(dataelement.__orderID__) } function move(from, to){ $.ajax({ type: "PUT", url: location.protocol + '//' + location.host + location.pathname, headers: {"content-id": "move"}, contentType: "application/json", data: JSON.stringify({"From": from, "To": to}), success: function(ret) { getAllData(); } }); } function setSingleData(singleData, urlarray, i = -1, url = ""){ container = document.getElementById("tablecontent"); entry = document.createElement("tr"); $(entry).attr("id",singleData.__orderID__); $(entry).attr("class","tableentry"); if(url.searchParams.has("colorize")){ if(i == 0){ var tableheader = url.searchParams.has("colorize") ? url.searchParams.get("colorize").replace("/","") : ""; var colorizestart = url.searchParams.has("colorize2") ? url.searchParams.get("colorizestart").replace("/","") : ""; var colorizecol = url.searchParams.has("colorize2") ? url.searchParams.get("colorizecol").replace("/","") : ""; var table2header = url.searchParams.has("colorize2") ? url.searchParams.get("colorize2").replace("/","") : ""; var colorize2start = url.searchParams.has("colorize2start") ? url.searchParams.get("colorize2start").replace("/","") : ""; var colorize2col = url.searchParams.has("colorize2col") ? url.searchParams.get("colorize2col").replace("/","") : ""; if(JSON.parse(singleData.alldata).hasOwnProperty(tableheader)){ if(JSON.parse(singleData.alldata)[tableheader].startsWith(colorizestart)){ $(entry).attr("style","background-color: #" + colorizecol); } } if(JSON.parse(singleData.alldata).hasOwnProperty(table2header)){ if(JSON.parse(singleData.alldata)[table2header].startsWith(colorize2start)){ $(entry).attr("style","background-color: #" + colorize2col); } } } } tableentry = ""; if(urlarray.includes("aktivieren")){ tableentry += "Aktivieren" } if(urlarray.includes("aktivierenPrio")){ tableentry += "Aktivieren Prio" } if(urlarray.includes("stornieren")){ tableentry += "Stornieren" } if(urlarray.includes("moveTop")){ tableentry += "MoveTop" } if(urlarray.includes("moveX")){ tableentry += "MovetoX" } if(urlarray.includes("edit")){ tableentry += "Edit" } if(singleData.hasOwnProperty('pkvalue')){ tableentry += "" + singleData.pkvalue + ""; } tableentry += "" + Object.values(JSON.parse(singleData.alldata)).join("") + "" $(entry).html(tableentry); $(entry).hide().fadeIn(2000); $("#tablecontent tr:last").before(entry); } function getUpdateData(id){ //Json aufbauen var i = 0; var obj = {}; $("#tablecontent th[class='editable'").each(function( index ) { curname = $( this ).html(); curvalue = $("#" + id + " .editable:eq(" + i + ") input").val(); if(isNaN(curvalue)){ obj[curname] = curvalue; }else{ obj[curname] = parseInt(curvalue, 10); } ++i; }); console.log("TEST" + JSON.stringify(obj)); update(id, obj) } function update(id, obj){ $.ajax({ type: "PUT", url: location.protocol + '//' + location.host + location.pathname, headers: {"content-id": "update"}, contentType: "application/json", data: JSON.stringify({"ID": id, "alldata": obj}), success: function(ret) { getAllData(); } }); } function edit(id){ console.log("#" + id + " .editable"); $("#" + id + " .edit").each(function( index ) { $( this ).html("Update"); }); $("#" + id + " .editable").each(function( index ) { if(isNaN($( this ).text())){ $( this ).html(""); }else{ $( this ).html(""); } }); } function addEntry() { $("#AddEntry .addEntry").each(function( index ) { $( this ).html("Add"); }); $("#AddEntry .editableprimarykey").each(function( index ) { $( this ).html(""); }); $("#AddEntry .editable").each(function( index ) { $( this ).html(""); }); } function getNewData(id){ //Json aufbauen var i = 0; var obj = {}; $("#tablecontent th[class='editable'").each(function( index ) { curname = $( this ).html(); curvalue = $("#" + id + " .editable:eq(" + i + ") input").val(); if(isNaN(curvalue)){ obj[curname] = curvalue; }else{ obj[curname] = parseInt(curvalue, 10); } ++i; }); pk = $("#" + id + " .editableprimarykey input").val() obj = {alldata: obj, pkvalue: pk} addNewData(id, obj) } function addNewData(id, obj){ $.ajax({ type: "POST", url: location.protocol + '//' + location.host + location.pathname, headers: {"content-id": "directADD"}, contentType: "application/json", data: JSON.stringify(obj), success: function(ret) { getAllData(); } }); } function openlink(menuitem){ var menu = { name: menuitem }; $.ajax({ type: "PUT", url: window.name, contentType: "application/json", data: JSON.stringify(menu), success: function (data) { } }); } function showDocument() { $.ajax({ type: "GET", url: location.protocol + '//' + location.host + location.pathname + 'style.url', success: function(ret) { $('head link.custom').attr('href',ret); } }); /* $.ajax({ type: "GET", url: 'info.json', success: function(ret) { makeGrid(ret.x_amount, ret.y_amount); //set name document.title = ret.document_name; $.ajax({ type: "GET", url: 'frames.json', success: function(ret2) { for (i of ret2.data) { makeFrame(i.lx,i.ly,i.rx,i.ry, i.url, i.callback, i.default, i.showbutton, i.style); } } }); }, error: function() { reason = ''; clearDocument(); } });*/ } function clearDocument() { console.log('rrrr'); $('#languages').addClass('hidden'); $('#nope').removeClass('hidden'); $('#control .added').remove(); $('#content .added').remove(); $('#reason').text(reason); } function init() { es = new EventSource('sse/'); es.onopen = function() { getAllData(); // load }; es.onmessage = function(e) { console.log("Got SSE"); if (e.data == 'new') { reason = ''; showDocument(); } if (e.data == 'reset') { reason = ''; showDocument(); } else{ if(e.data == "update"){ alert("update") } if(e.data != "keepalive" && e.data != "started"){ try { var prdata = JSON.parse(e.data) //alert("LastIf" + prdata.seriennummer) setSingleData(prdata); } catch (e) { } } } }; es.onerror = function() { reason = 'Server down.'; clearDocument(); setTimeout(init, 10000); }; } $(document).ready(function() { init(); });