ui.js 9.4 KB

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