ui.js 13 KB

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