ui.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. var reason ="";
  2. var storage = []; //{col:1, row:1, colamount:1, rowamount: 1}];
  3. function doOverlap(l1x, l1y, r1x, r1y, l2x, l2y, r2x, r2y) {
  4. // If one rectangle is on left side of other
  5. if (l1x > r2x || l2x > r1x)
  6. return false;
  7. // If one rectangle is above other
  8. if (l1y > r2y || l2y > r1y)
  9. return false;
  10. return true;
  11. }
  12. function makeFrame(lx, ly, rx, ry, content = "", id = "", showbutton=false) {
  13. //check if rects overlap if they do remove old ones
  14. for (i = 0; i < window.storage.length; i++) {
  15. if(doOverlap(window.storage[i].lx, window.storage[i].ly, window.storage[i].rx, window.storage[i].ry, lx, ly, rx, ry)){
  16. $(".item" + window.storage[i].lx + "-" + window.storage[i].ly).remove();
  17. //clearRectangel(window.storage[i].lx, window.storage[i].ly, window.storage[i].rx, window.storage[i].ry)
  18. window.storage.splice(i,1);
  19. --i;
  20. }
  21. }
  22. //add new ellement to storage
  23. window.storage.push({lx:lx, ly:ly, rx:rx, ry: ry})
  24. const container = document.getElementById("container");
  25. let cell = document.createElement("div");
  26. cell.classList.add("grid-item");
  27. cell.classList.add("item" + lx + "-" + ly);
  28. spancol= ""
  29. if(rx-lx+1 > 1){
  30. spancol = " / span " + (rx-lx+1);
  31. }
  32. spanrow= ""
  33. if(ry-ly+1 > 1){
  34. spanrow = " / span " + (ry-ly+1);
  35. }
  36. jQuery.cssNumber.gridColumnStart = true;
  37. jQuery.cssNumber.gridColumnEnd = true;
  38. jQuery.cssNumber.gridRowStart = true;
  39. jQuery.cssNumber.gridRowEnd = true;
  40. $(cell).css({"display": "block", "border-style": "solid", "border-color": "blue", "grid-column": (lx+1) + spancol, "grid-row": ly+1 + spanrow});
  41. container.appendChild(cell);
  42. //Create new element with width, heigth and content
  43. //$(".item" + lx + "-" + ly).css({"display": "block", "border-style": "solid", "border-color": "blue", "grid-column": (lx+1) + " / span " + (rx-lx+1), "grid-row": ly+1 + " / span " + (ry-ly+1)});
  44. if(content != null && content != ""){
  45. $(".item" + lx + "-" + ly).html("<iframe width=100% height=100% name='" + id +"' id='" + id +"' src='" + content + "' title=''></iframe>");
  46. if(showbutton && content.startsWith("https://centurio.work/out/forms")){
  47. $(".item" + lx + "-" + ly).append('<button class="formbutton" type="button" onclick="sendForm(\'' + '.item' + lx + '-' + ly +'\', \'' + encodeURIComponent(id) + '\', \'' + lx + '\', \'' + ly + '\')">Send Form</button>')
  48. }
  49. //hideRectangel(lx, ly, rx, ry)
  50. }
  51. else{
  52. $(".item" + lx + "-" + ly).html("No language available.<br> Nicht in der Sprache verfügbar.");
  53. //hideRectangel(lx, ly, rx, ry)
  54. }
  55. }
  56. function getFormData($form){
  57. var unindexed_array = $form.serializeArray();
  58. var indexed_array = {};
  59. $.map(unindexed_array, function(n, i){
  60. indexed_array[n['name']] = n['value'];
  61. });
  62. return indexed_array;
  63. }
  64. function sendForm(menuitem, cpeecallback,lx,ly){
  65. //Call iframe function that button has been pressed iframe decides what to do
  66. //document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed(cpeecallback);
  67. $.ajax({
  68. type: "PUT",
  69. url: decodeURIComponent(cpeecallback),
  70. contentType: "application/json",
  71. data: document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed(),
  72. success: function (data) {
  73. }
  74. });
  75. //close form
  76. $(menuitem).remove();
  77. //remove form from Server
  78. $.ajax({
  79. type: "Post",
  80. url: "",
  81. headers: {"content-id": "deleteframe"},
  82. data: {lx: lx, ly: ly},
  83. success: function (data) {
  84. }
  85. });
  86. }
  87. function showDocument() {
  88. $.ajax({
  89. type: "GET",
  90. url: 'style.url',
  91. success: function(ret) {
  92. $('head link.custom').attr('href',ret);
  93. }
  94. });
  95. $.ajax({
  96. type: "GET",
  97. url: 'info.json',
  98. success: function(ret) {
  99. makeGrid(ret.x_amount, ret.y_amount);
  100. $.ajax({
  101. type: "GET",
  102. url: 'frames.json',
  103. success: function(ret2) {
  104. for (i in ret2.data) {
  105. makeFrame(ret2.data[i].lx,ret2.data[i].ly,ret2.data[i].rx,ret2.data[i].ry, ret2.data[i].url, ret2.data[i].callback, ret2.data[i].showbutton);
  106. }
  107. }
  108. });
  109. },
  110. error: function() {
  111. reason = '';
  112. clearDocument();
  113. }
  114. });
  115. /*
  116. $.ajax({
  117. type: "GET",
  118. url: 'languages',
  119. success: function(ret) {
  120. $('#content .added').remove();
  121. $('#control .added').remove();
  122. var ctemplate = $('#content template')[0];
  123. var btemplate = $('#control template')[0];
  124. var promises = [];
  125. $('language',ret).each(function(i,e){
  126. var cclone = document.importNode(ctemplate.content, true);
  127. var bclone = document.importNode(btemplate.content, true);
  128. promises.push(new Promise((resolve, reject) => {
  129. $('> *',cclone).each(function(j,c){
  130. $(c).addClass('added');
  131. $(c).attr('lang', e.textContent);
  132. $.ajax({
  133. type: "GET",
  134. url: 'documents/' + e.textContent,
  135. success: function(doc) {
  136. if (c.nodeName == 'IFRAME') {
  137. $(c).attr('src',doc);
  138. } else {
  139. $('iframe',c).attr('src',doc);
  140. }
  141. $('#content').append(c);
  142. resolve(true);
  143. },
  144. error: function() {
  145. reject(false);
  146. setTimeout(function(){ showDocument(); }, 500);
  147. }
  148. });
  149. });
  150. }));
  151. promises.push(new Promise((resolve, reject) => {
  152. $('> *',bclone).each(function(j,c){
  153. $(c).addClass('added');
  154. $(c).attr('lang', e.textContent);
  155. $.ajax({
  156. type: "GET",
  157. url: 'buttons/' + e.textContent,
  158. success: function(but) {
  159. if (c.nodeName == 'BUTTON') {
  160. $(c).text(but);
  161. } else {
  162. $('button',c).text(but);
  163. }
  164. $('#control').append(c);
  165. resolve(true);
  166. },
  167. error: function() {
  168. resolve(true);
  169. }
  170. });
  171. });
  172. }));
  173. });
  174. Promise.all(promises).then((values) => {
  175. $.ajax({
  176. type: "GET",
  177. url: 'style.url',
  178. success: function(ret) {
  179. $('head link.custom').attr('href',ret);
  180. }
  181. });
  182. lang_init('#content','#languages');
  183. $('#languages').removeClass('hidden');
  184. $('#nope').addClass('hidden');
  185. });
  186. },
  187. error: function() {
  188. reason = '';
  189. clearDocument();
  190. }
  191. });
  192. */
  193. }
  194. function clearDocument() {
  195. console.log('rrrr');
  196. $('#languages').addClass('hidden');
  197. $('#nope').removeClass('hidden');
  198. $('#control .added').remove();
  199. $('#content .added').remove();
  200. $('#reason').text(reason);
  201. }
  202. function init() {
  203. es = new EventSource('sse/');
  204. es.onopen = function() {
  205. showDocument();
  206. // load
  207. };
  208. es.onmessage = function(e) {
  209. if (e.data == 'new') {
  210. reason = '';
  211. showDocument();
  212. }
  213. if (e.data == 'reset') {
  214. reason = '';
  215. showDocument();
  216. }
  217. else{
  218. if(e.data == "update"){
  219. alert("update")
  220. }
  221. if(e.data != "keepalive" && e.data != "started"){
  222. try {
  223. //alert(e.data)
  224. var frd = JSON.parse(e.data)
  225. makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url, frd.callback, frd.showbutton);
  226. }
  227. catch (e) {
  228. }
  229. }
  230. }
  231. };
  232. es.onerror = function() {
  233. reason = 'Server down.';
  234. clearDocument();
  235. setTimeout(init, 10000);
  236. };
  237. }
  238. function makeGrid(x, y) {
  239. const container = document.getElementById("container");
  240. container.style.setProperty('--grid-rows', y);
  241. container.style.setProperty('--grid-cols', x);
  242. /*
  243. for (c = 0; c < (x * y); c++) {
  244. let cell = document.createElement("div");
  245. //cell.innerText = (c + 1);
  246. cell.classList.add("item" + (c% y) + "-" + (Math.floor(c / y )));
  247. cell.classList.add("grid-item");
  248. container.appendChild(cell);
  249. };
  250. */
  251. };
  252. $(document).ready(function() {
  253. init();
  254. });