ui.js 9.1 KB

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