ui.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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({"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='' frameBorder='0' ></iframe>");
  46. if(showbutton){
  47. $(".item" + lx + "-" + ly).append('<button class="formbutton" type="button" onclick="sendForm(\'' + '.item' + lx + '-' + ly +'\', \'' + encodeURIComponent(id) + '\', \'' + lx + '\', \'' + ly + '\')">Submit</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. }
  54. }
  55. function sendForm(menuitem, cpeecallback,lx,ly){
  56. //Call iframe function that button has been pressed iframe should send json back
  57. //document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed(cpeecallback);
  58. var formdata;
  59. if (typeof document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed !== 'undefined' && $.isFunction(document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed)) {
  60. var formdata = document.getElementById(decodeURIComponent(cpeecallback)).contentWindow.buttonPressed();
  61. }
  62. $.ajax({
  63. type: "PUT",
  64. url: decodeURIComponent(cpeecallback),
  65. contentType: "application/json",
  66. data: JSON.stringify(formdata),
  67. success: function (data) {
  68. }
  69. });
  70. //Its a design question if removing the frame should be done within centurio, do have more controll, or automatic within code?
  71. //close frame
  72. $(menuitem).remove();
  73. //remove frame from Server
  74. $.ajax({
  75. type: "Post",
  76. url: "",
  77. headers: {"content-id": "deleteframe"},
  78. data: {lx: lx, ly: ly},
  79. success: function (data) {
  80. }
  81. });
  82. }
  83. function sendData(dataelement, datavalue){
  84. $.ajax({
  85. type: "Get",
  86. url: 'cpeeinstance.url',
  87. success: function(ret) {
  88. $.ajax({
  89. type: "Put",
  90. url: ret + "/properties/dataelements/" + dataelement,
  91. data: {value: datavalue},
  92. success: function (data) {
  93. alert("Data Sent")
  94. }
  95. });
  96. }
  97. });
  98. }
  99. function sendCallback(callbackUrl, jsonToSend){
  100. $.ajax({
  101. type: "PUT",
  102. url: callbackUrl,
  103. contentType: "application/json",
  104. data: jsonToSend,
  105. success: function (data) {
  106. }
  107. });
  108. }
  109. function showDocument() {
  110. $.ajax({
  111. type: "GET",
  112. url: 'style.url',
  113. success: function(ret) {
  114. $('head link.custom').attr('href',ret);
  115. }
  116. });
  117. $.ajax({
  118. type: "GET",
  119. url: 'info.json',
  120. success: function(ret) {
  121. makeGrid(ret.x_amount, ret.y_amount);
  122. $.ajax({
  123. type: "GET",
  124. url: 'frames.json',
  125. success: function(ret2) {
  126. for (i in ret2.data) {
  127. 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);
  128. }
  129. }
  130. });
  131. },
  132. error: function() {
  133. reason = '';
  134. clearDocument();
  135. }
  136. });
  137. /*
  138. $.ajax({
  139. type: "GET",
  140. url: 'languages',
  141. success: function(ret) {
  142. $('#content .added').remove();
  143. $('#control .added').remove();
  144. var ctemplate = $('#content template')[0];
  145. var btemplate = $('#control template')[0];
  146. var promises = [];
  147. $('language',ret).each(function(i,e){
  148. var cclone = document.importNode(ctemplate.content, true);
  149. var bclone = document.importNode(btemplate.content, true);
  150. promises.push(new Promise((resolve, reject) => {
  151. $('> *',cclone).each(function(j,c){
  152. $(c).addClass('added');
  153. $(c).attr('lang', e.textContent);
  154. $.ajax({
  155. type: "GET",
  156. url: 'documents/' + e.textContent,
  157. success: function(doc) {
  158. if (c.nodeName == 'IFRAME') {
  159. $(c).attr('src',doc);
  160. } else {
  161. $('iframe',c).attr('src',doc);
  162. }
  163. $('#content').append(c);
  164. resolve(true);
  165. },
  166. error: function() {
  167. reject(false);
  168. setTimeout(function(){ showDocument(); }, 500);
  169. }
  170. });
  171. });
  172. }));
  173. promises.push(new Promise((resolve, reject) => {
  174. $('> *',bclone).each(function(j,c){
  175. $(c).addClass('added');
  176. $(c).attr('lang', e.textContent);
  177. $.ajax({
  178. type: "GET",
  179. url: 'buttons/' + e.textContent,
  180. success: function(but) {
  181. if (c.nodeName == 'BUTTON') {
  182. $(c).text(but);
  183. } else {
  184. $('button',c).text(but);
  185. }
  186. $('#control').append(c);
  187. resolve(true);
  188. },
  189. error: function() {
  190. resolve(true);
  191. }
  192. });
  193. });
  194. }));
  195. });
  196. Promise.all(promises).then((values) => {
  197. $.ajax({
  198. type: "GET",
  199. url: 'style.url',
  200. success: function(ret) {
  201. $('head link.custom').attr('href',ret);
  202. }
  203. });
  204. lang_init('#content','#languages');
  205. $('#languages').removeClass('hidden');
  206. $('#nope').addClass('hidden');
  207. });
  208. },
  209. error: function() {
  210. reason = '';
  211. clearDocument();
  212. }
  213. });
  214. */
  215. }
  216. function clearDocument() {
  217. console.log('rrrr');
  218. $('#languages').addClass('hidden');
  219. $('#nope').removeClass('hidden');
  220. $('#control .added').remove();
  221. $('#content .added').remove();
  222. $('#reason').text(reason);
  223. }
  224. function init() {
  225. es = new EventSource('sse/');
  226. es.onopen = function() {
  227. showDocument();
  228. // load
  229. };
  230. es.onmessage = function(e) {
  231. if (e.data == 'new') {
  232. reason = '';
  233. showDocument();
  234. }
  235. if (e.data == 'reset') {
  236. reason = '';
  237. showDocument();
  238. }
  239. else{
  240. if(e.data == "update"){
  241. alert("update")
  242. }
  243. if(e.data != "keepalive" && e.data != "started"){
  244. try {
  245. //alert(e.data)
  246. var frd = JSON.parse(e.data)
  247. makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url, frd.callback, frd.showbutton);
  248. }
  249. catch (e) {
  250. }
  251. }
  252. }
  253. };
  254. es.onerror = function() {
  255. reason = 'Server down.';
  256. clearDocument();
  257. setTimeout(init, 10000);
  258. };
  259. }
  260. function makeGrid(x, y) {
  261. const container = document.getElementById("container");
  262. container.style.setProperty('--grid-rows', y);
  263. container.style.setProperty('--grid-cols', x);
  264. /*
  265. for (c = 0; c < (x * y); c++) {
  266. let cell = document.createElement("div");
  267. //cell.innerText = (c + 1);
  268. cell.classList.add("item" + (c% y) + "-" + (Math.floor(c / y )));
  269. cell.classList.add("grid-item");
  270. container.appendChild(cell);
  271. };
  272. */
  273. };
  274. $(document).ready(function() {
  275. init();
  276. });