app.js 13 KB

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