ui.js 9.7 KB

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