index-app.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. class IndexApp {
  2. constructor() {
  3. console.log("app constructor");
  4. this.worlds = {};
  5. this.language = _LangManager.language;
  6. this.options = {
  7. query: 'pathname=' + window.location.pathname.slice(1,
  8. window.location.pathname.lastIndexOf("/")),
  9. secure: window.location.protocol === "https:",
  10. reconnection: false,
  11. path: '',
  12. transports: ['websocket']
  13. }
  14. //window.location.host
  15. var socket = io.connect(window._app.reflectorHost, this.options);
  16. const parse = (msg) => {
  17. this.parseAppInstancesData(msg)
  18. }
  19. socket.on('getWebAppUpdate', msg => parse.call(self, msg));
  20. socket.on("connect", function () {
  21. let noty = new Noty({
  22. text: 'Connected to Reflector!',
  23. timeout: 2000,
  24. theme: 'mint',
  25. layout: 'bottomRight',
  26. type: 'success'
  27. });
  28. noty.show();
  29. })
  30. socket.on('connect_error', function (err) {
  31. console.log(err);
  32. var errDiv = document.createElement("div");
  33. errDiv.innerHTML = "<div class='vwf-err' style='z-index: 10; position: absolute; top: 80px; right: 50px'>Connection error to Reflector!" + err + "</div>";
  34. document.querySelector('body').appendChild(errDiv);
  35. let noty = new Noty({
  36. text: 'Connection error to Reflector! ' + err,
  37. theme: 'mint',
  38. layout: 'bottomRight',
  39. type: 'error'
  40. });
  41. noty.show();
  42. });
  43. }
  44. initReflectorServer() {
  45. this.currentReflector = localStorage.getItem('lcs_reflector');
  46. if (!this.currentReflector) {
  47. localStorage.setItem('lcs_reflector', window.location.host);
  48. this.currentReflector = localStorage.getItem('lcs_reflector');
  49. }
  50. }
  51. async generateFrontPage() {
  52. let infoEl = document.createElement("div");
  53. infoEl.setAttribute("id", "info");
  54. let lang = _LangManager.locale;
  55. let infoElHTML = await _app.helpers.getHtmlText('/web/locale/' + lang + '/index.html');
  56. infoEl.innerHTML = infoElHTML;
  57. document.body.appendChild(infoEl);
  58. document.querySelector('#ruLang').addEventListener('click', function (e) {
  59. _LangManager.locale = 'ru';
  60. window.location.reload(true);
  61. });
  62. document.querySelector('#enLang').addEventListener('click', function (e) {
  63. _LangManager.locale = 'en';
  64. window.location.reload(true);
  65. });
  66. }
  67. async initApp() {
  68. let appEl = document.createElement("div");
  69. appEl.setAttribute("id", "app");
  70. let appElHTML = await _app.helpers.getHtmlText('/web/app.html');
  71. appEl.innerHTML = appElHTML;
  72. document.body.appendChild(appEl);
  73. this.initReflectorSelector();
  74. this.initDBSelector();
  75. this.initUser();
  76. this.initUserGUI();
  77. this.initWorldsListGUI();
  78. //this.getAppDetailsFromDB();
  79. }
  80. initUser() {
  81. _LCSDB.on('auth', (ack) => {
  82. if (ack.pub) {
  83. let alias = _LCSUSER.is.alias;
  84. let userEl = document.querySelector('#userGUI');
  85. userEl._status = 'Welcome ' + alias + '!';
  86. //userEl.style.backgroundColor = '#e6e6e6';
  87. userEl.$update();
  88. document.querySelector('#worldGUI').$update();
  89. document.querySelector('#main').$update();
  90. _LCSUSER.get('profile').once(function (data) { console.log(data) })
  91. var el = document.getElementById("loginGUI");
  92. if (el) {
  93. el.remove();
  94. }
  95. _LCSUSER.get('profile').not(function (key) {
  96. let profile = { 'alias': alias };
  97. _LCSUSER.get('profile').put(profile);
  98. })
  99. new Noty({
  100. text: alias + ' is succesfully authenticated!',
  101. timeout: 2000,
  102. theme: 'mint',
  103. layout: 'bottomRight',
  104. type: 'success'
  105. }).show();
  106. //this.getAppDetailsFromUserDB();
  107. }
  108. console.log(_LCSUSER.is);
  109. });
  110. }
  111. initUserGUI() {
  112. let worldGUI =
  113. {
  114. $type: "div",
  115. id: "worldGUI",
  116. class: "mdc-layout-grid mdc-layout-grid--align-left",
  117. _status: '',
  118. $init: function () {
  119. this._status = "init";
  120. },
  121. $update: function () {
  122. let guiForAll = [
  123. window._app.widgets.buttonStroked(
  124. {
  125. "label": 'Default World Protos',
  126. "onclick": function (e) {
  127. e.preventDefault();
  128. _app.indexApp.getAppDetailsFromDefaultDB('protos');
  129. }
  130. }),
  131. window._app.widgets.buttonStroked(
  132. {
  133. "label": 'Default World States',
  134. "onclick": function (e) {
  135. e.preventDefault();
  136. _app.indexApp.getAppDetailsFromDefaultDB('states');
  137. }
  138. })
  139. ];
  140. var guiUser = [];
  141. if (_LCSUSER.is) {
  142. guiUser = []
  143. }
  144. this.$components = [
  145. {
  146. $type: "h1",
  147. class: "mdc-typography--headline4",
  148. $text: "Worlds list"
  149. }
  150. ].concat(guiForAll).concat(guiUser)
  151. }
  152. }
  153. let userGUI =
  154. {
  155. $type: "div",
  156. id: "userGUI",
  157. // style:"background-color: #ffeb3b",
  158. class: "mdc-layout-grid mdc-layout-grid--align-left",
  159. _status: "",
  160. $init: function () {
  161. this._status = "Welcome!"
  162. },
  163. $update: function () {
  164. var gui = {};
  165. if (_LCSUSER.is) {
  166. gui = [
  167. window._app.widgets.buttonRaised(
  168. {
  169. "label": 'Sign OUT',
  170. "onclick": function (e) {
  171. _LCSUSER.leave().then(ack => {
  172. if (ack.ok == 0) {
  173. window.sessionStorage.removeItem('alias');
  174. window.sessionStorage.removeItem('tmp');
  175. window.location.reload(true);
  176. }
  177. });
  178. }
  179. }),
  180. {
  181. $type: "p"
  182. },
  183. window._app.widgets.buttonStroked(
  184. {
  185. "label": 'PROFILE',
  186. "onclick": function (e) {
  187. e.preventDefault();
  188. window.location.pathname = "/profile"
  189. }
  190. }),
  191. {
  192. $type: "p"
  193. },
  194. window._app.widgets.buttonStroked(
  195. {
  196. "label": 'My World protos',
  197. "onclick": function (e) {
  198. e.preventDefault();
  199. let alias = _LCSUSER.is.alias;
  200. page.redirect('/' + alias + '/worlds/protos');
  201. //_app.indexApp.getWorldsProtosFromUserDB(alias);
  202. }
  203. }),
  204. window._app.widgets.buttonStroked(
  205. {
  206. "label": 'My World states',
  207. "onclick": function (e) {
  208. e.preventDefault();
  209. let alias = _LCSUSER.is.alias;
  210. page.redirect('/' + alias + '/worlds/states');
  211. //_app.indexApp.getWorldsFromUserDB(alias);
  212. }
  213. })
  214. ]
  215. }
  216. this.$components = [
  217. {
  218. $type: "h1",
  219. class: "mdc-typography--headline3",
  220. $text: this._status
  221. }
  222. ].concat(gui)
  223. }
  224. }
  225. let loginGUI =
  226. {
  227. $type: "div",
  228. id: "loginGUI",
  229. //style:"background-color: #efefef",
  230. class: "mdc-layout-grid mdc-layout-grid--align-left",
  231. _alias: null,
  232. _pass: null,
  233. _passField: null,
  234. _aliasField: null,
  235. _initData: function () {
  236. this._alias = '';
  237. this._pass = '';
  238. if (window.sessionStorage.getItem('alias')) {
  239. this._alias = window.sessionStorage.getItem('alias')
  240. }
  241. if (window.sessionStorage.getItem('tmp')) {
  242. this._pass = window.sessionStorage.getItem('tmp')
  243. }
  244. },
  245. $init: function () {
  246. this._initData();
  247. },
  248. $update: function () {
  249. this.$components = [
  250. {
  251. $type: "div",
  252. class: "mdc-layout-grid__inner",
  253. $components: [
  254. {
  255. $type: "div",
  256. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  257. $components: [
  258. {
  259. $type: "span",
  260. class: "mdc-typography--headline5",
  261. $text: "Login: "
  262. },
  263. window._app.widgets.inputTextFieldOutlined({
  264. "id": 'aliasInput',
  265. "label": "Login",
  266. "value": this._alias,
  267. "type": "text",
  268. "init": function() {
  269. this._aliasField = new mdc.textField.MDCTextField(this);
  270. }
  271. }),
  272. // {
  273. // class: "mdc-text-field",
  274. // $type: "span",
  275. // $init: function() {
  276. // this._aliasField = new mdc.textField.MDCTextField(this);
  277. // },
  278. // $components: [
  279. // {
  280. // class: "mdc-text-field__input prop-text-field-input mdc-typography--headline6",
  281. // $type: "input",
  282. // type: "text",
  283. // value: this._alias
  284. // }
  285. // ]
  286. // }
  287. ]
  288. },
  289. {
  290. $type: "div",
  291. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  292. $components: [
  293. {
  294. $type: "span",
  295. class: "mdc-typography--headline5",
  296. $text: "Password: "
  297. },
  298. window._app.widgets.inputTextFieldOutlined({
  299. "id": 'passwordInput',
  300. "label": "Password",
  301. "value": this._pass,
  302. "type": "password",
  303. "init": function() {
  304. this._passField = new mdc.textField.MDCTextField(this);
  305. }
  306. }),
  307. // {
  308. // class: "mdc-text-field",
  309. // $type: "span",
  310. // $init: function() {
  311. // this._passField = new mdc.textField.MDCTextField(this);
  312. // },
  313. // $components: [
  314. // {
  315. // class: "mdc-text-field__input prop-text-field-input mdc-typography--headline6",
  316. // $type: "input",
  317. // type: "password",
  318. // value: this._pass
  319. // }
  320. // ]
  321. // }
  322. ]
  323. },
  324. {
  325. $type: "div",
  326. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  327. $components: [
  328. window._app.widgets.buttonRaised(
  329. {
  330. "label": 'Sign UP',
  331. "onclick": function (e) {
  332. e.preventDefault();
  333. let alias = this._aliasField.value;
  334. let pass = this._passField.value
  335. if (pass.length < 7) {
  336. new Noty({
  337. text: "Your passphrase needs to be longer than 7 letters",
  338. timeout: 2000,
  339. theme: 'mint',
  340. layout: 'bottomRight',
  341. type: 'error'
  342. }).show();
  343. } else {
  344. //
  345. _LCSUSER.create(alias, pass,
  346. (ack) => {
  347. if (!ack.wait) { }
  348. if (ack.err) {
  349. console.log(ack.err)
  350. return ack.err
  351. };
  352. if (ack.pub) {
  353. _LCSUSER.auth(alias, pass);
  354. _LCSDB.get('users').get(alias).put({
  355. 'alias': alias,
  356. 'pub': ack.pub
  357. });
  358. }
  359. });
  360. }
  361. }
  362. }),
  363. {
  364. $type: "span",
  365. $text: " "
  366. },
  367. window._app.widgets.buttonRaised(
  368. {
  369. "label": 'Sign IN',
  370. "onclick": function (e) {
  371. e.preventDefault();
  372. _LCSUSER.auth(this._aliasField.value, this._passField.value, ack => {
  373. if (ack.err) {
  374. new Noty({
  375. text: ack.err,
  376. timeout: 2000,
  377. theme: 'mint',
  378. layout: 'bottomRight',
  379. type: 'error'
  380. }).show();
  381. }
  382. });
  383. }
  384. })
  385. ]
  386. }
  387. ]
  388. }
  389. ]
  390. }
  391. }
  392. document.querySelector("#userLobby").$cell({
  393. id: "userLobby",
  394. $cell: true,
  395. $type: "div",
  396. $components: [userGUI, loginGUI, _app.widgets.divider, worldGUI]
  397. }
  398. );
  399. }
  400. initReflectorSelector() {
  401. const ref = document.querySelector('#currentReflector');
  402. ref.value = window._app.reflectorHost;
  403. const reflectorSelect = document.querySelector('#reflectorSelect');
  404. mdc.textField.MDCTextField.attachTo(reflectorSelect);
  405. reflectorSelect.addEventListener('change', function (e) {
  406. console.log(e.target.value);
  407. let config = JSON.parse(localStorage.getItem('lcs_config'));
  408. config.reflector = e.target.value;
  409. localStorage.setItem('lcs_config', JSON.stringify(config));
  410. window.location.reload(true);
  411. });
  412. }
  413. initDBSelector() {
  414. const ref = document.querySelector('#currentdb');
  415. ref.value = window._app.dbHost;
  416. const dbSelect = document.querySelector('#dbSelect');
  417. mdc.textField.MDCTextField.attachTo(dbSelect);
  418. dbSelect.addEventListener('change', function (e) {
  419. console.log(e.target.value);
  420. let config = JSON.parse(localStorage.getItem('lcs_config'));
  421. config.dbhost = e.target.value;
  422. localStorage.setItem('lcs_config', JSON.stringify(config));
  423. window.location.reload(true);
  424. });
  425. }
  426. parseAppInstancesData(data) {
  427. if (data == "{}") {
  428. var el = document.querySelector(".instance");
  429. if (el) {
  430. var topEl = el.parentNode;
  431. topEl.removeChild(el);
  432. }
  433. // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  434. }
  435. let jsonObj = JSON.parse(data);
  436. var parsed = {};
  437. for (var prop in jsonObj) {
  438. var name = prop.split('/')[1];
  439. if (parsed[name]) {
  440. parsed[name][prop] = jsonObj[prop];
  441. } else {
  442. parsed[name] = {};
  443. parsed[name][prop] = jsonObj[prop];
  444. }
  445. }
  446. //console.log(parsed);
  447. if (Object.entries(this.worlds).length !== 0)
  448. document.querySelector("#main")._emptyLists();
  449. for (var prop in parsed) {
  450. var name = prop;
  451. let obj = Object.entries(parsed[prop]);
  452. var lists = {};
  453. obj.forEach(el => {
  454. let sotSave = prop;
  455. if (el[1].loadInfo['save_name']) {
  456. let saveName = prop + '/load/' + el[1].loadInfo.save_name;
  457. if (!lists[saveName])
  458. lists[saveName] = {};
  459. lists[saveName][el[0]] = el[1]
  460. } else {
  461. if (!lists[name])
  462. lists[name] = {};
  463. lists[name][el[0]] = el[1]
  464. }
  465. });
  466. // console.log(lists);
  467. Object.entries(lists).forEach(list => {
  468. let element = document.getElementById(list[0] + 'List');
  469. if (element) {
  470. element._setListData(list[1]);
  471. }
  472. })
  473. }
  474. // console.log(data)
  475. }
  476. initWorldsListGUI() {
  477. var self = this;
  478. let worldsListGUI = {
  479. $cell: true,
  480. $type: "div",
  481. id: "main",
  482. _status: "",
  483. _jsonData: {},
  484. _emptyLists: function () {
  485. Object.entries(this._jsonData).forEach(function (element) {
  486. //console.log(element);
  487. let el = document.getElementById(element[0] + 'List');
  488. if (el)
  489. el._setListData({});
  490. });
  491. },
  492. $init: function () {
  493. this._jsonData = {} //data//JSON.parse(data);
  494. },
  495. _makeWorldCard: function (m) {
  496. let langID = localStorage.getItem('krestianstvo_locale');
  497. var appInfo = m
  498. if (langID) {
  499. if (m[1][langID]) {
  500. appInfo = [m[0], m[1][langID], m[1].user, m[1].type]
  501. }
  502. }
  503. return {
  504. $cell: true,
  505. $type: "div",
  506. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  507. $components: [
  508. this._worldCardDef(appInfo)
  509. ]
  510. }
  511. },
  512. $update: function () {
  513. this.$components = [
  514. {
  515. $type: "div",
  516. class: "mdc-layout-grid",
  517. $components: [
  518. {
  519. $type: "div",
  520. class: "mdc-layout-grid__inner",
  521. $components: [
  522. {
  523. $type: "div",
  524. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  525. $components: [
  526. {
  527. $type: "H3",
  528. $text: this._status
  529. }
  530. ]
  531. }
  532. ]
  533. },
  534. {
  535. $type: "div",
  536. class: "mdc-layout-grid__inner",
  537. $components: Object.entries(this._jsonData).map(this._makeWorldCard)
  538. }
  539. ]
  540. },
  541. ]
  542. },
  543. _worldCardDef: function (desc) {
  544. var userGUI = [];
  545. if (desc[3] == 'proto') {
  546. userGUI.push(
  547. {
  548. $type: "a",
  549. class: "mdc-button mdc-button--compact mdc-card__action",
  550. $text: "States",
  551. onclick: function (e) {
  552. //console.log('clone');
  553. self.showOnlySaveStates(desc[0], desc[2]);
  554. //self.refresh();
  555. }
  556. }
  557. )
  558. }
  559. if (_LCSUSER.is) {
  560. if (_LCSUSER.is.alias == desc[2]) {
  561. userGUI.push(
  562. {
  563. $type: "a",
  564. class: "mdc-button mdc-button--compact mdc-card__action",
  565. $text: "Edit info",
  566. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  567. onclick: function (e) {
  568. //'/:user/:type/:name/edit/:file'
  569. if (desc[3] == 'proto') {
  570. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/info_json'
  571. } else if (desc[3] == 'saveState') {
  572. let names = desc[0].split('/');
  573. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  574. window.location.pathname = "/" + desc[2] + '/state/' + names[0] + '/edit/' + filename;
  575. }
  576. //self.refresh();
  577. }
  578. }
  579. );
  580. if (desc[3] == 'proto') {
  581. userGUI.push(
  582. {
  583. $type: "a",
  584. class: "mdc-button mdc-button--compact mdc-card__action",
  585. $text: "Edit proto",
  586. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  587. onclick: function (e) {
  588. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/index_vwf_yaml'
  589. }
  590. }
  591. );
  592. }
  593. }
  594. if (desc[3] == 'proto') {
  595. userGUI.push(
  596. {
  597. $type: "a",
  598. class: "mdc-button mdc-button--compact mdc-card__action",
  599. $text: self.language.t('clone proto'),//"clone",
  600. onclick: function (e) {
  601. //console.log('clone');
  602. _app.cloneWorldPrototype(desc[0], desc[2]);
  603. //self.refresh();
  604. }
  605. }
  606. )
  607. } else if (desc[3] == 'saveState') {
  608. // userGUI.push(
  609. // {
  610. // $type: "a",
  611. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  612. // $text: "Clone",
  613. // onclick: function (e) {
  614. // //console.log('clone');
  615. // //self.cloneWorldState(desc[0], desc[2]);
  616. // //self.refresh();
  617. // }
  618. // })
  619. }
  620. }
  621. return {
  622. $cell: true,
  623. $type: "div",
  624. class: "mdc-card world-card",
  625. _appInfo: desc,
  626. $components: [
  627. {
  628. $type: "section",
  629. class: "mdc-card__media world-card__16-9-media",
  630. $init: function () {
  631. if (desc[1].imgUrl !== "") {
  632. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  633. }
  634. }
  635. },
  636. {
  637. $type: "section",
  638. class: "mdc-card__primary",
  639. $components: [
  640. {
  641. $type: "h1",
  642. class: "mdc-card__title mdc-card__title--large",
  643. $text: desc[1].title
  644. },
  645. {
  646. $type: "h2",
  647. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  648. $text: desc[1].text
  649. }
  650. ]
  651. },
  652. {
  653. $type: "section",
  654. class: "mdc-card__actions",
  655. $components: [
  656. {
  657. $type: "a",
  658. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  659. $text: self.language.t('start'),//"Start new",
  660. target: "_blank",
  661. href: "/" + desc[2] + '/' + desc[0],
  662. onclick: function (e) {
  663. self.refresh();
  664. }
  665. }
  666. ].concat(userGUI)
  667. },
  668. {
  669. $type: "section",
  670. class: "mdc-card__actions",
  671. $components: [
  672. {
  673. $type: "ul",
  674. _listData: {},
  675. _setListData: function (data) {
  676. this._listData = data;
  677. },
  678. class: "mdc-list mdc-list--two-line",
  679. 'aria-orientation': "vertical",
  680. id: desc[0] + 'List',
  681. $update: function () {
  682. var connectText = {}
  683. let cardListData = Object.entries(this._listData).filter(el => el[1].user == this._appInfo[2]);
  684. if (cardListData.length !== 0) {
  685. connectText = {
  686. // $type: "span",
  687. // class: "mdc-theme--text-secondary",
  688. // $text: "...or connect to:"
  689. }
  690. }
  691. this.$components = [
  692. {
  693. $type: "hr",
  694. class: "mdc-list-divider"
  695. }
  696. ].concat(cardListData.map(this._worldListItem))
  697. // [connectText]
  698. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  699. },
  700. _worldListItem: function (m) {
  701. return {
  702. $type: "li",
  703. class: "mdc-list-item",
  704. $components: [
  705. {
  706. $type: "span",
  707. class: "world-link mdc-list-item__text",
  708. $components: [
  709. {
  710. $type: "span",
  711. class: "mdc-list-item__primary-text",
  712. $components: [
  713. {
  714. $type: "a",
  715. $text: m[0],
  716. target: "_blank",
  717. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  718. onclick: function (e) {
  719. //self.refresh();
  720. }
  721. },
  722. ]
  723. },
  724. {
  725. $type: "span",
  726. class: "mdc-list-item__secondary-text",
  727. $text: self.language.t('users') + m[1].clients
  728. }
  729. ]
  730. }
  731. ]
  732. }
  733. }
  734. }
  735. ]
  736. }
  737. ]
  738. }
  739. }
  740. }
  741. document.querySelector("#main").$cell({
  742. $cell: true,
  743. $type: "div",
  744. $components: [worldsListGUI]
  745. })
  746. }
  747. async showOnlySaveStates(index, userAlias) {
  748. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  749. var db = _LCSDB.user(userPub);
  750. if (_LCSUSER.is) {
  751. if (_LCSUSER.is.alias == userAlias)
  752. db = _LCSUSER;
  753. }
  754. this.worlds = {};
  755. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  756. document.querySelector("#main")._status = "Save states of the World: " + index + ' for user: ' + userAlias;
  757. document.querySelector("#main").$update();
  758. //let userAlias = _LCSUSER.is.alias;
  759. db.get('documents').get(index).once(save => {
  760. if (save) {
  761. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  762. console.log(saves);
  763. if (saves) {
  764. saves.forEach(el => {
  765. db.get('documents').get(index).get(el).once(res => {
  766. if (res) {
  767. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  768. let world = JSON.parse(res.file);
  769. let root = Object.keys(world)[0];
  770. world[root].user = userAlias;
  771. world[root].type = 'saveState';
  772. this.worlds[index + '/load/' + fileName] = world[root];
  773. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  774. }
  775. })
  776. })
  777. }
  778. }
  779. })
  780. }
  781. async getWorldsProtosFromUserDB(userAlias) {
  782. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  783. console.log('get user worlds for: ' + userAlias);
  784. this.worlds = {};
  785. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  786. if (!userPub) {
  787. document.querySelector("#main")._status = "no such user";
  788. document.querySelector("#main").$update();
  789. }
  790. if (userPub) {
  791. document.querySelector("#main")._status = "Worlds protos for: " + userAlias;
  792. document.querySelector("#main").$update();
  793. var db = _LCSDB.user(userPub);
  794. if (_LCSUSER.is) {
  795. if (_LCSUSER.is.alias == userAlias)
  796. db = _LCSUSER;
  797. }
  798. db.get('worlds').map().once((w, index) => {
  799. if (w) {
  800. db.get('worlds').get(index).get('info_json').once(res => {
  801. if (res) {
  802. let world = JSON.parse(res.file);
  803. let root = Object.keys(world)[0];
  804. world[root].user = userAlias;
  805. world[root].type = 'proto';
  806. this.worlds[index] = world[root];
  807. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  808. }
  809. })
  810. }
  811. })
  812. }
  813. }
  814. async getWorldsFromUserDB(userAlias) {
  815. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  816. console.log('get user worlds for: ' + userAlias);
  817. this.worlds = {};
  818. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  819. if (!userPub) {
  820. document.querySelector("#main")._status = "no such user";
  821. document.querySelector("#main").$update();
  822. }
  823. if (userPub) {
  824. document.querySelector("#main")._status = "Worlds states for: " + userAlias;
  825. document.querySelector("#main").$update();
  826. var db = _LCSDB.user(userPub);
  827. if (_LCSUSER.is) {
  828. if (_LCSUSER.is.alias == userAlias)
  829. db = _LCSUSER;
  830. }
  831. db.get('worlds').map().once((w, index) => {
  832. if (w) {
  833. db.get('documents').get(index).once(save => {
  834. if (save) {
  835. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  836. console.log(saves);
  837. if (saves) {
  838. saves.forEach(el => {
  839. db.get('documents').get(index).get(el).once(res => {
  840. if (res) {
  841. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  842. let world = JSON.parse(res.file);
  843. let root = Object.keys(world)[0];
  844. world[root].user = userAlias;
  845. world[root].type = 'saveState';
  846. this.worlds[index + '/load/' + fileName] = world[root];
  847. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  848. }
  849. })
  850. })
  851. }
  852. }
  853. })
  854. }
  855. })
  856. }
  857. }
  858. async getAppDetailsFromDefaultDB(type) {
  859. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  860. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  861. page.redirect('/' + userAlias + '/worlds/' + type);
  862. }
  863. async getAppDetailsFromDB() {
  864. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  865. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  866. if (userAlias)
  867. this.getWorldsProtosFromUserDB(userAlias);
  868. }
  869. refresh() {
  870. // socket.emit('getWebAppUpdate', "");
  871. }
  872. }
  873. export { IndexApp }
  874. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};