ui.js 9.7 KB

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