app.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. var options = {
  2. query: 'pathname=' + window.location.pathname.slice(1,
  3. window.location.pathname.lastIndexOf("/")),
  4. secure: window.location.protocol === "https:",
  5. reconnection: false,
  6. transports: ['websocket']
  7. };
  8. var socket = io.connect(window.location.protocol + "//" + window.location.host, options);
  9. socket.on('getWebAppUpdate', function (msg) {
  10. parseAppInstancesData(msg)
  11. //console.log(msg);
  12. });
  13. function parseAppInstancesData(data) {
  14. var needToUpdate = true;
  15. if (data == "{}") {
  16. var el = document.querySelector(".instance");
  17. if (el) {
  18. var topEl = el.parentNode;
  19. topEl.removeChild(el);
  20. }
  21. // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  22. }
  23. let jsonObj = JSON.parse(data);
  24. var parsed = {};
  25. for (var prop in jsonObj) {
  26. var name = prop.split('/')[1];
  27. if (parsed[name]) {
  28. parsed[name][prop] = jsonObj[prop];
  29. } else {
  30. parsed[name] = {};
  31. parsed[name][prop] = jsonObj[prop];
  32. }
  33. }
  34. //console.log(parsed);
  35. for (var prop in parsed) {
  36. var name = prop;
  37. let element = document.getElementById(name);
  38. if (element) {
  39. var list = document.getElementById(name + 'List');
  40. var topList = list.parentNode;
  41. topList.removeChild(list);
  42. var list = document.createElement('ol');
  43. list.setAttribute("id", name + 'List');
  44. topList.appendChild(list);
  45. var newListProps = parsed[prop];
  46. for (var propEntry in newListProps) {
  47. let entry = document.createElement('li');
  48. entry.setAttribute("class", "instance");
  49. let node = document.createElement("A");
  50. let textLink = document.createTextNode(newListProps[propEntry].instance);
  51. node.setAttribute("href", 'http://' + newListProps[propEntry].instance);
  52. node.setAttribute("target", "_blank");
  53. node.setAttribute("onclick", "refresh();");
  54. node.appendChild(textLink);
  55. let numClientsEl = document.createElement('span');
  56. numClientsEl.setAttribute("class", "numClients");
  57. let numClients = document.createTextNode('Users online: ' + newListProps[propEntry].clients);
  58. entry.appendChild(node);
  59. entry.appendChild(document.createElement('br'));
  60. entry.appendChild(numClientsEl).appendChild(numClients);
  61. list.appendChild(entry);
  62. }
  63. }
  64. //needToUpdate = true
  65. }
  66. // console.log(data)
  67. }
  68. function parseWebAppData(data) {
  69. let jsonObj = JSON.parse(data);
  70. for (var prop in jsonObj) {
  71. let main = document.getElementById('main');
  72. let app = document.createElement("DIV");
  73. app.setAttribute("class", "mdl-cell mdl-cell--4-col exp-card-wide mdl-card mdl-shadow--2dp");
  74. var appCard = '<div class="mdl-card__title"><h2 id="' + prop + 'Title" class="mdl-card__title-text">' + jsonObj[prop].title + '</h2></div><div id="' + prop + 'Text" class="mdl-card__supporting-text">' + jsonObj[prop].text + '</div><div id="' + prop + '" class="mdl-card__actions mdl-card--border"> <a href="/' + prop + '" onclick="refresh();" target="_blank" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">Start new</a><hr/><p/><span class="mdl-color-text--grey-500">...or connect to:</span><ol id="' + prop + 'List"></ol></div>';
  75. app.innerHTML = appCard;
  76. if (jsonObj[prop].imgUrl !== "") {
  77. app.firstChild.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), url(' + jsonObj[prop].imgUrl + ')';
  78. }
  79. main.appendChild(app);
  80. }
  81. //console.log(data);
  82. // getAllAppInstances();
  83. }
  84. function getAllAppInstances() {
  85. let allInatances = httpGetJson('allinstances.json')
  86. .then(res => {
  87. parseAppInstancesData(res);
  88. });
  89. }
  90. function getAppDetails() {
  91. let appDetails = httpGetJson('webapps.json')
  92. .then(res => {
  93. parseWebAppData(res)
  94. })
  95. .then(res => refresh());
  96. }
  97. function refresh() {
  98. // socket.emit('getWebAppUpdate', "");
  99. }
  100. function httpGet(url) {
  101. return new Promise(function (resolve, reject) {
  102. // do the usual Http request
  103. let request = new XMLHttpRequest();
  104. request.open('GET', url);
  105. request.onload = function () {
  106. if (request.status == 200) {
  107. resolve(request.response);
  108. } else {
  109. reject(Error(request.statusText));
  110. }
  111. };
  112. request.onerror = function () {
  113. reject(Error('Network Error'));
  114. };
  115. request.send();
  116. });
  117. }
  118. async function httpGetJson(url) {
  119. // check if the URL looks like a JSON file and call httpGet.
  120. let regex = /\.(json)$/i;
  121. if (regex.test(url)) {
  122. // call the async function, wait for the result
  123. return await httpGet(url);
  124. } else {
  125. throw Error('Bad Url Format');
  126. }
  127. }