ui.js 10.0 KB

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