app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import { Lang } from '/web/lib/polyglot-lang.js';
  2. class WebApp {
  3. constructor() {
  4. console.log("app constructor");
  5. this.options = {
  6. query: 'pathname=' + window.location.pathname.slice(1,
  7. window.location.pathname.lastIndexOf("/")),
  8. secure: window.location.protocol === "https:",
  9. reconnection: false,
  10. transports: ['websocket']
  11. }
  12. this.lang = new Lang;
  13. this.language = this.prepareLang();
  14. this.initReflectorServer();
  15. //window.location.host
  16. var socket = io.connect(window.location.protocol + "//" + this.currentReflector, this.options);
  17. var self = this;
  18. socket.on('getWebAppUpdate', function (msg) {
  19. self.parseAppInstancesData(msg)
  20. //console.log(msg);
  21. });
  22. }
  23. initReflectorServer() {
  24. this.currentReflector = localStorage.getItem('lcs_reflector');
  25. if (!this.currentReflector) {
  26. localStorage.setItem('lcs_reflector', window.location.host);
  27. this.currentReflector = localStorage.getItem('lcs_reflector');
  28. }
  29. }
  30. prepareLang() {
  31. let phrases = this.lang.langPhrases;
  32. return new this.lang.polyglot({ phrases });
  33. }
  34. setLanguage(langID) {
  35. this.lang.setLocale(langID);
  36. this.generateFrontPage();
  37. }
  38. generateFrontPage() {
  39. this.lang.initLocale();
  40. let allTextEl = ["titleText", "headerText", "featuresText", "worldInfo", "demoText"];
  41. allTextEl.forEach(el => {
  42. let textEl = document.querySelector("#" + el);
  43. textEl.innerHTML = this.lang.getTranslationFor(el);
  44. })
  45. this.initReflectorSelector();
  46. }
  47. initReflectorSelector() {
  48. const ref = document.querySelector('#currentReflector');
  49. ref.value = this.currentReflector;
  50. //let currentItem = Array.from(items).filter(el => el.textContent == this.currentReflector);
  51. const reflectorSelect = document.querySelector('#reflectorSelect');
  52. mdc.textField.MDCTextField.attachTo(reflectorSelect);
  53. reflectorSelect.addEventListener('change', function(e) {
  54. console.log(e.target.value);
  55. localStorage.setItem('lcs_reflector', e.target.value);
  56. window.location.reload(true);
  57. });
  58. }
  59. parseAppInstancesData(data) {
  60. var needToUpdate = true;
  61. if (data == "{}") {
  62. var el = document.querySelector(".instance");
  63. if (el) {
  64. var topEl = el.parentNode;
  65. topEl.removeChild(el);
  66. }
  67. // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  68. }
  69. let jsonObj = JSON.parse(data);
  70. var parsed = {};
  71. for (var prop in jsonObj) {
  72. var name = prop.split('/')[1];
  73. if (parsed[name]) {
  74. parsed[name][prop] = jsonObj[prop];
  75. } else {
  76. parsed[name] = {};
  77. parsed[name][prop] = jsonObj[prop];
  78. }
  79. }
  80. //console.log(parsed);
  81. document.querySelector("#main")._emptyLists();
  82. for (var prop in parsed) {
  83. var name = prop;
  84. let element = document.getElementById(name + 'List');
  85. if (element) {
  86. element._setListData(parsed[prop]);
  87. }
  88. //needToUpdate = true
  89. }
  90. // console.log(data)
  91. }
  92. getAllAppInstances() {
  93. let allInatances = this.httpGetJson('allinstances.json')
  94. .then(res => {
  95. this.parseAppInstancesData(res);
  96. });
  97. }
  98. parseWebAppDataForCell(data) {
  99. var self = this;
  100. document.querySelector("#main").$cell({
  101. $cell: true,
  102. $type: "div",
  103. id: "main",
  104. _jsonData: {},
  105. _emptyLists: function () {
  106. Object.entries(this._jsonData).forEach(function (element) {
  107. //console.log(element);
  108. document.getElementById(element[0] + 'List')._setListData({});
  109. });
  110. },
  111. $init: function () {
  112. this._jsonData = JSON.parse(data);
  113. },
  114. _makeWorldCard: function (m) {
  115. let langID = localStorage.getItem('krestianstvo_locale');
  116. var appInfo = m
  117. if (langID) {
  118. if (m[1][langID]) {
  119. appInfo = [m[0], m[1][langID]]
  120. }
  121. }
  122. return {
  123. $cell: true,
  124. $type: "div",
  125. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  126. $components: [
  127. this._worldCardDef(appInfo)
  128. ]
  129. }
  130. },
  131. $update: function () {
  132. this.$components = [
  133. //getSiteHeader(),// siteHeader,
  134. {
  135. $type: "div",
  136. class: "mdc-layout-grid",
  137. $components: [
  138. {
  139. $type: "div",
  140. class: "mdc-layout-grid__inner",
  141. $components: Object.entries(this._jsonData).map(this._makeWorldCard)
  142. }
  143. ]
  144. },
  145. ]
  146. },
  147. _worldCardDef: function (desc) {
  148. return {
  149. $cell: true,
  150. $type: "div",
  151. class: "mdc-card world-card",
  152. $components: [
  153. {
  154. $type: "section",
  155. class: "mdc-card__media world-card__16-9-media",
  156. $init: function () {
  157. if (desc[1].imgUrl !== "") {
  158. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  159. }
  160. }
  161. },
  162. {
  163. $type: "section",
  164. class: "mdc-card__primary",
  165. $components: [
  166. {
  167. $type: "h1",
  168. class: "mdc-card__title mdc-card__title--large",
  169. $text: desc[1].title
  170. },
  171. {
  172. $type: "h2",
  173. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  174. $text: desc[1].text
  175. }
  176. ]
  177. },
  178. {
  179. $type: "section",
  180. class: "mdc-card__actions",
  181. $components: [
  182. {
  183. $type: "a",
  184. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--stroked",
  185. $text: self.language.t('start'),//"Start new",
  186. target: "_blank",
  187. href: "/" + desc[0],
  188. onclick: function (e) {
  189. self.refresh();
  190. }
  191. }
  192. ]
  193. },
  194. {
  195. $type: "section",
  196. class: "mdc-card__actions",
  197. $components: [
  198. {
  199. $type: "ul",
  200. _listData: {},
  201. _setListData: function (data) {
  202. this._listData = data;
  203. },
  204. class: "mdc-list mdc-list--two-line",
  205. id: desc[0] + 'List',
  206. $update: function () {
  207. var connectText = {}
  208. if (Object.entries(this._listData).length !== 0) {
  209. connectText = {
  210. // $type: "span",
  211. // class: "mdc-theme--text-secondary",
  212. // $text: "...or connect to:"
  213. }
  214. }
  215. this.$components = [
  216. {
  217. $type: "hr",
  218. class: "mdc-list-divider"
  219. }
  220. ].concat(Object.entries(this._listData).map(this._worldListItem))
  221. // [connectText]
  222. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  223. },
  224. _worldListItem: function (m) {
  225. return {
  226. $type: "li",
  227. class: "mdc-list-item",
  228. $components: [
  229. {
  230. $type: "span",
  231. class: "world-link mdc-list-item__text",
  232. $components: [
  233. {
  234. $type: "a",
  235. $text: m[0],
  236. target: "_blank",
  237. href: window.location.protocol + "//" + window.location.host + m[0],
  238. onclick: function (e) {
  239. self.refresh();
  240. }
  241. },
  242. {
  243. $type: "span",
  244. class: "mdc-list-item__text__secondary",
  245. $text: self.language.t('users') + m[1].clients
  246. }
  247. ]
  248. }
  249. ]
  250. }
  251. }
  252. }
  253. ]
  254. }
  255. ]
  256. }
  257. }
  258. })
  259. }
  260. getAppDetails(val) {
  261. let appDetails = this.httpGetJson(val)
  262. .then(res => {
  263. this.parseWebAppDataForCell(res)
  264. })
  265. .then(res => this.refresh());
  266. }
  267. refresh() {
  268. // socket.emit('getWebAppUpdate', "");
  269. }
  270. httpGet(url) {
  271. return new Promise(function (resolve, reject) {
  272. // do the usual Http request
  273. let request = new XMLHttpRequest();
  274. request.open('GET', url);
  275. request.onload = function () {
  276. if (request.status == 200) {
  277. resolve(request.response);
  278. } else {
  279. reject(Error(request.statusText));
  280. }
  281. };
  282. request.onerror = function () {
  283. reject(Error('Network Error'));
  284. };
  285. request.send();
  286. });
  287. }
  288. async httpGetJson(url) {
  289. // check if the URL looks like a JSON file and call httpGet.
  290. let regex = /\.(json)$/i;
  291. if (regex.test(url)) {
  292. // call the async function, wait for the result
  293. return await this.httpGet(url);
  294. } else {
  295. throw Error('Bad Url Format');
  296. }
  297. }
  298. }
  299. export { WebApp }
  300. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};