ui.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. function getAllData(){
  2. $( "#data" ).empty();
  3. var url_string = window.location.href
  4. var url = new URL(url_string);
  5. if(!url.searchParams.has("display")){
  6. var urlarray = ["aktivieren","aktivierenPrio","stornieren","moveTop","moveX","move","edit"]
  7. }else{
  8. var urlparams = url.searchParams.get("display").replace("/","");
  9. var urlarray = urlparams.split(",");
  10. }
  11. if(url.searchParams.has("menu")){
  12. console.log("MENU");
  13. menuentries = url.searchParams.get("menu").split(",")
  14. $( "#data" ).append("<a class='ontop'href='" + menuentries[1] + "' target='_blank'>" + menuentries[0] +"</a>" );
  15. }
  16. $.ajax({
  17. type: "GET",
  18. url: 'data.db',
  19. async: false,
  20. success: function(ret) {
  21. if(!$(ret.data).length == 0){
  22. container = document.getElementById("data");
  23. entry = document.createElement("div");
  24. tablehead = "<table><tbody id='tablecontent'><tr>";
  25. urlarray.forEach(function(element) { tablehead += "<th></th>" });
  26. if(ret.data[0].hasOwnProperty('pkvalue')){
  27. tablehead += "<th class='primarykey'>Primary Key</th>";
  28. }
  29. var joinkey = urlarray.length;
  30. for (var i = 0; i < Object.keys(JSON.parse(ret.data[0].alldata)).length; i++) {
  31. tablehead += "<th class='editable'>" + Object.keys(JSON.parse(ret.data[0].alldata))[i]
  32. if(url.searchParams.has("showfilter")){
  33. tablehead += '<br><input styl="width: 100%;" type="text" id="searchtable' + ++joinkey +'" onkeyup="searchtable(' + joinkey + ')" placeholder="Search for ' + Object.keys(JSON.parse(ret.data[0].alldata))[i] + '" name="' +Object.keys(JSON.parse(ret.data[0].alldata))[i] + '">'
  34. }
  35. tablehead += '</th>'
  36. }
  37. tablehead += "</tr></tbody></table>";
  38. $(entry).html(tablehead);
  39. $(container).append(entry);
  40. container = document.getElementById("tablecontent");
  41. entry = document.createElement("tr");
  42. $(entry).attr("id","AddEntry");
  43. $(entry).attr("class","tableentry");
  44. tableedit = "<td class='addEntry'><a onclick='addEntry()' href='#'>Add</a></td>"
  45. urlarray.forEach(function(element) { });
  46. for (var i = 1; i < urlarray.length; i++) { //start at 1 as add is already set
  47. tableedit += "<td></td>"
  48. }
  49. if(ret.data[0].hasOwnProperty('pkvalue')){
  50. tableedit += "<td class='editableprimarykey'>Primary Key</td>";
  51. }
  52. tableedit += "<td class='editable'>" + Object.keys(JSON.parse(ret.data[0].alldata)).join("</td><td class='editable'>") + "</td>"
  53. $(entry).append(tableedit);
  54. $("#tablecontent").append(entry);
  55. $.each(ret.data, function(i, item) {
  56. setSingleData(item, urlarray, i, url);
  57. });
  58. }
  59. }
  60. });
  61. if(url.searchParams.has("searchkey")){
  62. var searchkey = url.searchParams.get("searchkey").replace("/","");
  63. $('[name="' + searchkey +'"]').val(url.searchParams.get("searchterm").replace("/",""));
  64. $('[name="' + searchkey +'"]').keyup();
  65. }
  66. if(url.searchParams.has("sendListBack")){
  67. $('body').append('<a onclick="collectdata2sendBack()" href="#">Sende Liste</a>');
  68. }
  69. /*
  70. new Sortable(document.getElementById('data'),{
  71. handle: '.handle',
  72. animation: 150
  73. });
  74. */
  75. }
  76. function collectdata2sendBack(){
  77. console.log("sending");
  78. var sendback = [];
  79. $('[name="pkvalues"]:visible').each(function(){
  80. sendback.push($(this).html());
  81. });
  82. sendDataElementBack(sendback)
  83. }
  84. //https://www.therogerlab.com/sandbox/pages/how-to-reorder-table-rows-in-javascript?s=0ea4985d74a189e8b7b547976e7192ae.4122809346f6a15e41c9a43f6fcb5fd5
  85. var row;
  86. function start(){
  87. row = event.target;
  88. }
  89. function dragover(){
  90. var e = event;
  91. e.preventDefault();
  92. let children= Array.from(e.target.parentNode.parentNode.children);
  93. if(children.indexOf(e.target.parentNode)>children.indexOf(row))
  94. e.target.parentNode.after(row);
  95. else
  96. e.target.parentNode.before(row);
  97. }
  98. function searchtable(key) {
  99. console.log("AAA");
  100. // Declare variables
  101. var input, filter, table, tr, td, i, txtValue;
  102. input = document.getElementById("searchtable" +key);
  103. filter = input.value.toUpperCase();
  104. table = document.getElementById("tablecontent");
  105. tr = table.getElementsByTagName("tr");
  106. // Loop through all table rows, and hide those who don't match the search query
  107. for (i = 0; i < tr.length; i++) {
  108. td = tr[i].getElementsByTagName("td")[key];
  109. if (td) {
  110. txtValue = td.textContent || td.innerText;
  111. if (txtValue.toUpperCase().indexOf(filter) > -1) {
  112. tr[i].style.display = "";
  113. } else {
  114. tr[i].style.display = "none";
  115. }
  116. }
  117. }
  118. }
  119. function sortTable(n) {
  120. var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  121. table = document.getElementById("tablecontent");
  122. switching = true;
  123. // Set the sorting direction to ascending:
  124. dir = "asc";
  125. /* Make a loop that will continue until
  126. no switching has been done: */
  127. while (switching) {
  128. // Start by saying: no switching is done:
  129. switching = false;
  130. rows = table.rows;
  131. /* Loop through all table rows (except the
  132. first, which contains table headers): */
  133. for (i = 1; i < (rows.length - 1); i++) {
  134. // Start by saying there should be no switching:
  135. shouldSwitch = false;
  136. /* Get the two elements you want to compare,
  137. one from current row and one from the next: */
  138. x = rows[i].getElementsByTagName("TD")[n];
  139. y = rows[i + 1].getElementsByTagName("TD")[n];
  140. /* Check if the two rows should switch place,
  141. based on the direction, asc or desc: */
  142. if (dir == "asc") {
  143. if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
  144. // If so, mark as a switch and break the loop:
  145. shouldSwitch = true;
  146. break;
  147. }
  148. } else if (dir == "desc") {
  149. if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
  150. // If so, mark as a switch and break the loop:
  151. shouldSwitch = true;
  152. break;
  153. }
  154. }
  155. }
  156. if (shouldSwitch) {
  157. /* If a switch has been marked, make the switch
  158. and mark that a switch has been done: */
  159. rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  160. switching = true;
  161. // Each time a switch is done, increase this count by 1:
  162. switchcount ++;
  163. } else {
  164. /* If no switching has been done AND the direction is "asc",
  165. set the direction to "desc" and run the while loop again. */
  166. if (switchcount == 0 && dir == "asc") {
  167. dir = "desc";
  168. switching = true;
  169. }
  170. }
  171. }
  172. }
  173. function deleteDataElement(dataelement){
  174. console.log("Löschen")
  175. console.log(dataelement)
  176. $.ajax({
  177. type: "DELETE",
  178. url: location.protocol + '//' + location.host + location.pathname,
  179. contentType: "application/json",
  180. headers: {"content-id": "deleteByID"},
  181. data: JSON.stringify(dataelement),
  182. success: function(ret) {
  183. getAllData();
  184. }
  185. });
  186. }
  187. //if used in frames send row to frames
  188. function sendDataElementBack(dataelement, type){
  189. parent.sendJson(window.name,{ type: type, dataelement: dataelement })
  190. //deleteDataElement(dataelement.__orderID__)
  191. }
  192. function move(from, to){
  193. $.ajax({
  194. type: "PUT",
  195. url: location.protocol + '//' + location.host + location.pathname,
  196. headers: {"content-id": "move"},
  197. contentType: "application/json",
  198. data: JSON.stringify({"From": from, "To": to}),
  199. success: function(ret) {
  200. getAllData();
  201. }
  202. });
  203. }
  204. function setSingleData(singleData, urlarray, i = -1, url = ""){
  205. container = document.getElementById("tablecontent");
  206. entry = document.createElement("tr");
  207. $(entry).attr("id",singleData.__orderID__);
  208. $(entry).attr("class","tableentry");
  209. if(urlarray.includes("move")){
  210. $(entry).attr("draggable","true");
  211. $(entry).attr("ondragstart","start()");
  212. $(entry).attr("ondragover","dragover()");
  213. }
  214. if(url.searchParams.has("colorize")){
  215. if(i == 0){
  216. var tableheader = url.searchParams.has("colorize") ? url.searchParams.get("colorize").replace("/","") : "";
  217. var colorizestart = url.searchParams.has("colorize2") ? url.searchParams.get("colorizestart").replace("/","") : "";
  218. var colorizecol = url.searchParams.has("colorize2") ? url.searchParams.get("colorizecol").replace("/","") : "";
  219. var table2header = url.searchParams.has("colorize2") ? url.searchParams.get("colorize2").replace("/","") : "";
  220. var colorize2start = url.searchParams.has("colorize2start") ? url.searchParams.get("colorize2start").replace("/","") : "";
  221. var colorize2col = url.searchParams.has("colorize2col") ? url.searchParams.get("colorize2col").replace("/","") : "";
  222. if(JSON.parse(singleData.alldata).hasOwnProperty(tableheader)){
  223. if(JSON.parse(singleData.alldata)[tableheader].startsWith(colorizestart)){
  224. $(entry).attr("style","background-color: #" + colorizecol);
  225. }
  226. }
  227. if(JSON.parse(singleData.alldata).hasOwnProperty(table2header)){
  228. if(JSON.parse(singleData.alldata)[table2header].startsWith(colorize2start)){
  229. $(entry).attr("style","background-color: #" + colorize2col);
  230. }
  231. }
  232. }
  233. }
  234. tableentry = "";
  235. if(urlarray.includes("aktivieren")){
  236. tableentry += "<td><a onclick='sendDataElementBack(" + JSON.stringify(singleData) +", \"normal\")' href='#'>Aktivieren</a></td>"
  237. }
  238. if(urlarray.includes("aktivierenPrio")){
  239. tableentry += "<td><a onclick='sendDataElementBack(" + JSON.stringify(singleData) +", \"prio\")' href='#'>Aktivieren Prio</a></td>"
  240. }
  241. if(urlarray.includes("stornieren")){
  242. tableentry += "<td><a onclick='deleteDataElement(" + singleData.__orderID__ +")' href='#'>Stornieren</a></td>"
  243. }
  244. if(urlarray.includes("moveTop")){
  245. tableentry += "<td><a onclick='move(" + singleData.__orderID__ +",0)' href='#'>MoveTop</a></td>"
  246. }
  247. if(urlarray.includes("moveX")){
  248. tableentry += "<td><a onclick='move(" + singleData.__orderID__ +",7)' href='#'>MovetoX</a></td>"
  249. }
  250. if(urlarray.includes("move")){
  251. tableentry += '<td class="handle"><a href="#">⇅</a></td>'
  252. }
  253. if(urlarray.includes("edit")){
  254. tableentry += "<td class='edit'><a onclick='edit(" + singleData.__orderID__ +")' href='#'>Edit</a></td>"
  255. }
  256. if(singleData.hasOwnProperty('pkvalue')){
  257. tableentry += "<td name='pkvalues'>" + singleData.pkvalue + "</td>";
  258. }
  259. tableentry += "<td class='editable'>" + Object.values(JSON.parse(singleData.alldata)).join("</td><td class='editable'>") + "</td>"
  260. $(entry).html(tableentry);
  261. $(entry).hide().fadeIn(2000);
  262. $("#tablecontent tr:last").before(entry);
  263. }
  264. function getUpdateData(id){
  265. //Json aufbauen
  266. var i = 0;
  267. var obj = {};
  268. $("#tablecontent th[class='editable'").each(function( index ) {
  269. curname = $( this ).html();
  270. curvalue = $("#" + id + " .editable:eq(" + i + ") input").val();
  271. if(isNaN(curvalue)){
  272. obj[curname] = curvalue;
  273. }else{
  274. obj[curname] = parseInt(curvalue, 10);
  275. }
  276. ++i;
  277. });
  278. console.log("TEST" + JSON.stringify(obj));
  279. update(id, obj)
  280. }
  281. function update(id, obj){
  282. $.ajax({
  283. type: "PUT",
  284. url: location.protocol + '//' + location.host + location.pathname,
  285. headers: {"content-id": "update"},
  286. contentType: "application/json",
  287. data: JSON.stringify({"ID": id, "alldata": obj}),
  288. success: function(ret) {
  289. getAllData();
  290. }
  291. });
  292. }
  293. function edit(id){
  294. console.log("#" + id + " .editable");
  295. $("#" + id + " .edit").each(function( index ) {
  296. $( this ).html("<a onclick='getUpdateData(" + id +")' href='#'>Update</a>");
  297. });
  298. $("#" + id + " .editable").each(function( index ) {
  299. if(isNaN($( this ).text())){
  300. $( this ).html("<input type='text' value='" + $( this ).text() +"'>");
  301. }else{
  302. $( this ).html("<input type='number' value='" + $( this ).text() +"'>");
  303. }
  304. });
  305. }
  306. function addEntry() {
  307. $("#AddEntry .addEntry").each(function( index ) {
  308. $( this ).html("<a onclick='getNewData(\"AddEntry\")' href='#'>Add</a>");
  309. });
  310. $("#AddEntry .editableprimarykey").each(function( index ) {
  311. $( this ).html("<input type='text' value='" + $( this ).text() +"'>");
  312. });
  313. $("#AddEntry .editable").each(function( index ) {
  314. $( this ).html("<input type='text' value='" + $( this ).text() +"'>");
  315. });
  316. }
  317. function getNewData(id){
  318. //Json aufbauen
  319. var i = 0;
  320. var obj = {};
  321. $("#tablecontent th[class='editable'").each(function( index ) {
  322. curname = $( this ).html();
  323. curvalue = $("#" + id + " .editable:eq(" + i + ") input").val();
  324. if(isNaN(curvalue)){
  325. obj[curname] = curvalue;
  326. }else{
  327. obj[curname] = parseInt(curvalue, 10);
  328. }
  329. ++i;
  330. });
  331. pk = $("#" + id + " .editableprimarykey input").val()
  332. obj = {alldata: obj, pkvalue: pk}
  333. addNewData(id, obj)
  334. }
  335. function addNewData(id, obj){
  336. $.ajax({
  337. type: "POST",
  338. url: location.protocol + '//' + location.host + location.pathname,
  339. headers: {"content-id": "directADD"},
  340. contentType: "application/json",
  341. data: JSON.stringify(obj),
  342. success: function(ret) {
  343. getAllData();
  344. }
  345. });
  346. }
  347. function openlink(menuitem){
  348. var menu = { name: menuitem };
  349. $.ajax({
  350. type: "PUT",
  351. url: window.name,
  352. contentType: "application/json",
  353. data: JSON.stringify(menu),
  354. success: function (data) {
  355. }
  356. });
  357. }
  358. function showDocument() {
  359. $.ajax({
  360. type: "GET",
  361. url: location.protocol + '//' + location.host + location.pathname + 'style.url',
  362. success: function(ret) {
  363. $('head link.custom').attr('href',ret);
  364. }
  365. });
  366. /*
  367. $.ajax({
  368. type: "GET",
  369. url: 'info.json',
  370. success: function(ret) {
  371. makeGrid(ret.x_amount, ret.y_amount);
  372. //set name
  373. document.title = ret.document_name;
  374. $.ajax({
  375. type: "GET",
  376. url: 'frames.json',
  377. success: function(ret2) {
  378. for (i of ret2.data) {
  379. makeFrame(i.lx,i.ly,i.rx,i.ry, i.url, i.callback, i.default, i.showbutton, i.style);
  380. }
  381. }
  382. });
  383. },
  384. error: function() {
  385. reason = '';
  386. clearDocument();
  387. }
  388. });*/
  389. }
  390. function clearDocument() {
  391. console.log('rrrr');
  392. $('#languages').addClass('hidden');
  393. $('#nope').removeClass('hidden');
  394. $('#control .added').remove();
  395. $('#content .added').remove();
  396. $('#reason').text(reason);
  397. }
  398. function init() {
  399. es = new EventSource('sse/');
  400. es.onopen = function() {
  401. getAllData();
  402. // load
  403. };
  404. es.onmessage = function(e) {
  405. console.log("Got SSE");
  406. if (e.data == 'new') {
  407. reason = '';
  408. showDocument();
  409. }
  410. if (e.data == 'reset') {
  411. reason = '';
  412. showDocument();
  413. }
  414. else{
  415. if(e.data == "update"){
  416. alert("update")
  417. }
  418. if(e.data != "keepalive" && e.data != "started"){
  419. try {
  420. var prdata = JSON.parse(e.data)
  421. //alert("LastIf" + prdata.seriennummer)
  422. setSingleData(prdata);
  423. }
  424. catch (e) {
  425. }
  426. }
  427. }
  428. };
  429. es.onerror = function() {
  430. reason = 'Server down.';
  431. clearDocument();
  432. setTimeout(init, 10000);
  433. };
  434. }
  435. $(document).ready(function() {
  436. init();
  437. });