ui.js 9.1 KB

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