app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. document.querySelector("#main")._emptyLists();
  36. for (var prop in parsed) {
  37. var name = prop;
  38. let element = document.getElementById(name + 'List');
  39. if (element) {
  40. element._setListData(parsed[prop]);
  41. }
  42. //needToUpdate = true
  43. }
  44. // console.log(data)
  45. }
  46. function getAllAppInstances() {
  47. let allInatances = httpGetJson('allinstances.json')
  48. .then(res => {
  49. parseAppInstancesData(res);
  50. });
  51. }
  52. function parseWebAppDataForCell(data) {
  53. document.querySelector("#main").$cell({
  54. $cell: true,
  55. $type: "div",
  56. id: "main",
  57. _jsonData: {},
  58. _emptyLists: function () {
  59. Object.entries(this._jsonData).forEach(function (element) {
  60. //console.log(element);
  61. document.getElementById(element[0] + 'List')._setListData({});
  62. });
  63. },
  64. $init: function () {
  65. this._jsonData = JSON.parse(data);
  66. },
  67. _makeWorldCard: function (m) {
  68. return {
  69. $cell: true,
  70. $type: "div",
  71. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  72. $components: [
  73. this._worldCardDef(m)
  74. ]
  75. }
  76. },
  77. $update: function () {
  78. this.$components = [
  79. // siteHeader,
  80. {
  81. $type: "div",
  82. class: "mdc-layout-grid",
  83. $components: [
  84. {
  85. $type: "div",
  86. class: "mdc-layout-grid__inner",
  87. $components: Object.entries(this._jsonData).map(this._makeWorldCard)
  88. }
  89. ]
  90. },
  91. ]
  92. },
  93. _worldCardDef: function (desc) {
  94. return {
  95. $cell: true,
  96. $type: "div",
  97. class: "mdc-card world-card",
  98. $components: [
  99. {
  100. $type: "section",
  101. class: "mdc-card__media world-card__16-9-media",
  102. $init: function () {
  103. if (desc[1].imgUrl !== "") {
  104. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  105. }
  106. }
  107. },
  108. {
  109. $type: "section",
  110. class: "mdc-card__primary",
  111. $components: [
  112. {
  113. $type: "h1",
  114. class: "mdc-card__title mdc-card__title--large",
  115. $text: desc[1].title
  116. },
  117. {
  118. $type: "h2",
  119. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  120. $text: desc[1].text
  121. }
  122. ]
  123. },
  124. {
  125. $type: "section",
  126. class: "mdc-card__actions",
  127. $components: [
  128. {
  129. $type: "a",
  130. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--raised",
  131. $text: "Start new",
  132. target: "_blank",
  133. href: "/" + desc[0],
  134. onclick: function (e) {
  135. refresh();
  136. }
  137. }
  138. ]
  139. },
  140. {
  141. $type: "section",
  142. class: "mdc-card__actions",
  143. $components: [
  144. {
  145. $type: "ul",
  146. _listData: {},
  147. _setListData: function (data) {
  148. this._listData = data;
  149. },
  150. class: "mdc-list mdc-list--two-line",
  151. id: desc[0] + 'List',
  152. $update: function () {
  153. var connectText = {}
  154. if (Object.entries(this._listData).length !== 0) {
  155. connectText = {
  156. // $type: "span",
  157. // class: "mdc-theme--text-secondary",
  158. // $text: "...or connect to:"
  159. }
  160. }
  161. this.$components = [
  162. {
  163. $type: "hr",
  164. class: "mdc-list-divider"
  165. }
  166. ].concat(Object.entries(this._listData).map(this._worldListItem))
  167. // [connectText]
  168. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  169. },
  170. _worldListItem: function (m) {
  171. return {
  172. $type: "li",
  173. class: "mdc-list-item",
  174. $components: [
  175. {
  176. $type: "span",
  177. class: "world-link mdc-list-item__text",
  178. $components: [
  179. {
  180. $type: "a",
  181. $text: m[1].instance,
  182. target: "_blank",
  183. href: window.location.protocol + "//" + m[1].instance,
  184. onclick: function (e) {
  185. refresh();
  186. }
  187. },
  188. {
  189. $type: "span",
  190. class: "mdc-list-item__text__secondary",
  191. $text: "Users online: " + m[1].clients
  192. }
  193. ]
  194. }
  195. ]
  196. }
  197. }
  198. }
  199. ]
  200. }
  201. ]
  202. }
  203. }
  204. })
  205. }
  206. function getAppDetails(val) {
  207. let appDetails = httpGetJson(val)
  208. .then(res => {
  209. parseWebAppDataForCell(res)
  210. })
  211. .then(res => refresh());
  212. }
  213. function refresh() {
  214. // socket.emit('getWebAppUpdate', "");
  215. }
  216. function httpGet(url) {
  217. return new Promise(function (resolve, reject) {
  218. // do the usual Http request
  219. let request = new XMLHttpRequest();
  220. request.open('GET', url);
  221. request.onload = function () {
  222. if (request.status == 200) {
  223. resolve(request.response);
  224. } else {
  225. reject(Error(request.statusText));
  226. }
  227. };
  228. request.onerror = function () {
  229. reject(Error('Network Error'));
  230. };
  231. request.send();
  232. });
  233. }
  234. async function httpGetJson(url) {
  235. // check if the URL looks like a JSON file and call httpGet.
  236. let regex = /\.(json)$/i;
  237. if (regex.test(url)) {
  238. // call the async function, wait for the result
  239. return await httpGet(url);
  240. } else {
  241. throw Error('Bad Url Format');
  242. }
  243. }