ui.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. //set name
  141. document.title = ret.document_name;
  142. $.ajax({
  143. type: "GET",
  144. url: 'frames.json',
  145. success: function(ret2) {
  146. for (i of ret2.data) {
  147. makeFrame(i.lx,i.ly,i.rx,i.ry, i.url, i.callback, i.default, i.showbutton, i.style);
  148. }
  149. }
  150. });
  151. },
  152. error: function() {
  153. reason = '';
  154. clearDocument();
  155. }
  156. });
  157. /*
  158. $.ajax({
  159. type: "GET",
  160. url: 'languages',
  161. success: function(ret) {
  162. $('#content .added').remove();
  163. $('#control .added').remove();
  164. var ctemplate = $('#content template')[0];
  165. var btemplate = $('#control template')[0];
  166. var promises = [];
  167. $('language',ret).each(function(i,e){
  168. var cclone = document.importNode(ctemplate.content, true);
  169. var bclone = document.importNode(btemplate.content, true);
  170. promises.push(new Promise((resolve, reject) => {
  171. $('> *',cclone).each(function(j,c){
  172. $(c).addClass('added');
  173. $(c).attr('lang', e.textContent);
  174. $.ajax({
  175. type: "GET",
  176. url: 'documents/' + e.textContent,
  177. success: function(doc) {
  178. if (c.nodeName == 'IFRAME') {
  179. $(c).attr('src',doc);
  180. } else {
  181. $('iframe',c).attr('src',doc);
  182. }
  183. $('#content').append(c);
  184. resolve(true);
  185. },
  186. error: function() {
  187. reject(false);
  188. setTimeout(function(){ showDocument(); }, 500);
  189. }
  190. });
  191. });
  192. }));
  193. promises.push(new Promise((resolve, reject) => {
  194. $('> *',bclone).each(function(j,c){
  195. $(c).addClass('added');
  196. $(c).attr('lang', e.textContent);
  197. $.ajax({
  198. type: "GET",
  199. url: 'buttons/' + e.textContent,
  200. success: function(but) {
  201. if (c.nodeName == 'BUTTON') {
  202. $(c).text(but);
  203. } else {
  204. $('button',c).text(but);
  205. }
  206. $('#control').append(c);
  207. resolve(true);
  208. },
  209. error: function() {
  210. resolve(true);
  211. }
  212. });
  213. });
  214. }));
  215. });
  216. Promise.all(promises).then((values) => {
  217. $.ajax({
  218. type: "GET",
  219. url: 'style.url',
  220. success: function(ret) {
  221. $('head link.custom').attr('href',ret);
  222. }
  223. });
  224. lang_init('#content','#languages');
  225. $('#languages').removeClass('hidden');
  226. $('#nope').addClass('hidden');
  227. });
  228. },
  229. error: function() {
  230. reason = '';
  231. clearDocument();
  232. }
  233. });
  234. */
  235. }
  236. function clearDocument() {
  237. console.log('rrrr');
  238. $('#languages').addClass('hidden');
  239. $('#nope').removeClass('hidden');
  240. $('#control .added').remove();
  241. $('#content .added').remove();
  242. $('#reason').text(reason);
  243. }
  244. function init() {
  245. es = new EventSource('sse/');
  246. es.onopen = function() {
  247. showDocument();
  248. // load
  249. };
  250. es.onmessage = function(e) {
  251. if (e.data == 'new') {
  252. reason = '';
  253. showDocument();
  254. }
  255. if (e.data == 'reset') {
  256. reason = '';
  257. showDocument();
  258. }
  259. else{
  260. if(e.data == "update"){
  261. alert("update")
  262. }
  263. if(e.data != "keepalive" && e.data != "started"){
  264. try {
  265. //alert(e.data)
  266. var frd = JSON.parse(e.data)
  267. makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url, frd.callback, frd.default, frd.showbutton, frd.style);
  268. }
  269. catch (e) {
  270. }
  271. }
  272. }
  273. };
  274. es.onerror = function() {
  275. reason = 'Server down.';
  276. clearDocument();
  277. setTimeout(init, 10000);
  278. };
  279. }
  280. function makeGrid(x, y) {
  281. const container = document.getElementById("container");
  282. container.style.setProperty('--grid-rows', y);
  283. container.style.setProperty('--grid-cols', x);
  284. /*
  285. for (c = 0; c < (x * y); c++) {
  286. let cell = document.createElement("div");
  287. //cell.innerText = (c + 1);
  288. cell.classList.add("item" + (c% y) + "-" + (Math.floor(c / y )));
  289. cell.classList.add("grid-item");
  290. container.appendChild(cell);
  291. };
  292. */
  293. };
  294. $(document).ready(function() {
  295. init();
  296. });