ui.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var reason ="";
  2. function showDocument() {
  3. $.ajax({
  4. type: "GET",
  5. url: 'info.json',
  6. success: function(ret) {
  7. makeGrid(ret.x_amount, ret.y_amount);
  8. $.ajax({
  9. type: "GET",
  10. url: 'frames.json',
  11. success: function(ret2) {
  12. for (i in ret2.data) {
  13. makeFrame(ret2.data[i].lx,ret2.data[i].ly,ret2.data[i].rx,ret2.data[i].ry, ret2.data[i].url);
  14. }
  15. }
  16. });
  17. },
  18. error: function() {
  19. reason = '';
  20. clearDocument();
  21. }
  22. });
  23. /*
  24. $.ajax({
  25. type: "GET",
  26. url: 'languages',
  27. success: function(ret) {
  28. $('#content .added').remove();
  29. $('#control .added').remove();
  30. var ctemplate = $('#content template')[0];
  31. var btemplate = $('#control template')[0];
  32. var promises = [];
  33. $('language',ret).each(function(i,e){
  34. var cclone = document.importNode(ctemplate.content, true);
  35. var bclone = document.importNode(btemplate.content, true);
  36. promises.push(new Promise((resolve, reject) => {
  37. $('> *',cclone).each(function(j,c){
  38. $(c).addClass('added');
  39. $(c).attr('lang', e.textContent);
  40. $.ajax({
  41. type: "GET",
  42. url: 'documents/' + e.textContent,
  43. success: function(doc) {
  44. if (c.nodeName == 'IFRAME') {
  45. $(c).attr('src',doc);
  46. } else {
  47. $('iframe',c).attr('src',doc);
  48. }
  49. $('#content').append(c);
  50. resolve(true);
  51. },
  52. error: function() {
  53. reject(false);
  54. setTimeout(function(){ showDocument(); }, 500);
  55. }
  56. });
  57. });
  58. }));
  59. promises.push(new Promise((resolve, reject) => {
  60. $('> *',bclone).each(function(j,c){
  61. $(c).addClass('added');
  62. $(c).attr('lang', e.textContent);
  63. $.ajax({
  64. type: "GET",
  65. url: 'buttons/' + e.textContent,
  66. success: function(but) {
  67. if (c.nodeName == 'BUTTON') {
  68. $(c).text(but);
  69. } else {
  70. $('button',c).text(but);
  71. }
  72. $('#control').append(c);
  73. resolve(true);
  74. },
  75. error: function() {
  76. resolve(true);
  77. }
  78. });
  79. });
  80. }));
  81. });
  82. Promise.all(promises).then((values) => {
  83. $.ajax({
  84. type: "GET",
  85. url: 'style.url',
  86. success: function(ret) {
  87. $('head link.custom').attr('href',ret);
  88. }
  89. });
  90. lang_init('#content','#languages');
  91. $('#languages').removeClass('hidden');
  92. $('#nope').addClass('hidden');
  93. });
  94. },
  95. error: function() {
  96. reason = '';
  97. clearDocument();
  98. }
  99. });
  100. */
  101. }
  102. function clearDocument() {
  103. console.log('rrrr');
  104. $('#languages').addClass('hidden');
  105. $('#nope').removeClass('hidden');
  106. $('#control .added').remove();
  107. $('#content .added').remove();
  108. $('#reason').text(reason);
  109. }
  110. function init() {
  111. es = new EventSource('sse/');
  112. es.onopen = function() {
  113. showDocument();
  114. // load
  115. };
  116. es.onmessage = function(e) {
  117. if (e.data == 'new') {
  118. reason = '';
  119. showDocument();
  120. }
  121. if (e.data == 'reset') {
  122. reason = '';
  123. showDocument();
  124. }
  125. else{
  126. if(e.data != "keepalive" && e.data != "started"){
  127. try {
  128. //alert(e.data)
  129. var frd = JSON.parse(e.data)
  130. makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url);
  131. }
  132. catch (e) {
  133. }
  134. }
  135. }
  136. };
  137. es.onerror = function() {
  138. reason = 'Server down.';
  139. clearDocument();
  140. setTimeout(init, 10000);
  141. };
  142. }
  143. function makeGrid(x, y) {
  144. const container = document.getElementById("container");
  145. container.style.setProperty('--grid-rows', y);
  146. container.style.setProperty('--grid-cols', x);
  147. /*
  148. for (c = 0; c < (x * y); c++) {
  149. let cell = document.createElement("div");
  150. //cell.innerText = (c + 1);
  151. cell.classList.add("item" + (c% y) + "-" + (Math.floor(c / y )));
  152. cell.classList.add("grid-item");
  153. container.appendChild(cell);
  154. };
  155. */
  156. };
  157. $(document).ready(function() {
  158. $('#control').on('click','button[name=send]',b_send);
  159. init();
  160. });
  161. function b_send() {
  162. var formData = new FormData();
  163. var content = JSON.stringify($('iframe:visible')[0].contentWindow.send_it());
  164. var blob = new Blob([content], { type: "application/json"});
  165. formData.append("op", "result");
  166. formData.append("value", blob);
  167. var request = new XMLHttpRequest();
  168. request.open("DELETE", location.href);
  169. request.send(formData);
  170. }