index-app.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. _initData: function () {
  234. this._alias = '';
  235. this._pass = '';
  236. if (window.sessionStorage.getItem('alias')) {
  237. this._alias = window.sessionStorage.getItem('alias')
  238. }
  239. if (window.sessionStorage.getItem('tmp')) {
  240. this._pass = window.sessionStorage.getItem('tmp')
  241. }
  242. },
  243. $init: function () {
  244. this._initData();
  245. },
  246. $update: function () {
  247. this.$components = [
  248. {
  249. $type: "div",
  250. class: "mdc-layout-grid__inner",
  251. $components: [
  252. {
  253. $type: "div",
  254. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  255. $components: [
  256. {
  257. $type: "span",
  258. class: "mdc-typography--headline5",
  259. $text: "Login: "
  260. },
  261. {
  262. class: "mdc-text-field",
  263. $type: "span",
  264. $components: [
  265. {
  266. class: "mdc-text-field__input prop-text-field-input mdc-typography--headline6",
  267. id: "alias",
  268. $type: "input",
  269. type: "text",
  270. value: this._alias,
  271. onchange: function (e) {
  272. this._alias = this.value;
  273. }
  274. }
  275. ]
  276. }
  277. ]
  278. },
  279. {
  280. $type: "div",
  281. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  282. $components: [
  283. {
  284. $type: "span",
  285. class: "mdc-typography--headline5",
  286. $text: "Password: "
  287. },
  288. {
  289. class: "mdc-text-field",
  290. $type: "span",
  291. $components: [
  292. {
  293. class: "mdc-text-field__input prop-text-field-input mdc-typography--headline6",
  294. id: "pass",
  295. $type: "input",
  296. type: "password",
  297. value: this._pass,
  298. onchange: function (e) {
  299. this._pass = this.value;
  300. }
  301. }
  302. ]
  303. }
  304. ]
  305. },
  306. {
  307. $type: "div",
  308. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  309. $components: [
  310. window._app.widgets.buttonRaised(
  311. {
  312. "label": 'Sign UP',
  313. "onclick": function (e) {
  314. e.preventDefault();
  315. if (this._pass.length < 7) {
  316. new Noty({
  317. text: "Your passphrase needs to be longer than 7 letters",
  318. timeout: 2000,
  319. theme: 'mint',
  320. layout: 'bottomRight',
  321. type: 'error'
  322. }).show();
  323. } else {
  324. //
  325. _LCSUSER.create(this._alias, this._pass,
  326. (ack) => {
  327. if (!ack.wait) { }
  328. if (ack.err) {
  329. console.log(ack.err)
  330. return ack.err
  331. };
  332. if (ack.pub) {
  333. _LCSUSER.auth(this._alias, this._pass);
  334. _LCSDB.get('users').get(this._alias).put({
  335. 'alias': this._alias,
  336. 'pub': ack.pub
  337. });
  338. }
  339. });
  340. }
  341. }
  342. }),
  343. {
  344. $type: "span",
  345. $text: " "
  346. },
  347. window._app.widgets.buttonRaised(
  348. {
  349. "label": 'Sign IN',
  350. "onclick": function (e) {
  351. e.preventDefault();
  352. _LCSUSER.auth(this._alias, this._pass, ack => {
  353. if (ack.err) {
  354. new Noty({
  355. text: ack.err,
  356. timeout: 2000,
  357. theme: 'mint',
  358. layout: 'bottomRight',
  359. type: 'error'
  360. }).show();
  361. }
  362. });
  363. }
  364. })
  365. ]
  366. }
  367. ]
  368. }
  369. ]
  370. }
  371. }
  372. document.querySelector("#userLobby").$cell({
  373. id: "userLobby",
  374. $cell: true,
  375. $type: "div",
  376. $components: [userGUI, loginGUI, _app.widgets.divider, worldGUI]
  377. }
  378. );
  379. }
  380. initReflectorSelector() {
  381. const ref = document.querySelector('#currentReflector');
  382. ref.value = window._app.reflectorHost;
  383. const reflectorSelect = document.querySelector('#reflectorSelect');
  384. mdc.textField.MDCTextField.attachTo(reflectorSelect);
  385. reflectorSelect.addEventListener('change', function (e) {
  386. console.log(e.target.value);
  387. let config = JSON.parse(localStorage.getItem('lcs_config'));
  388. config.reflector = e.target.value;
  389. localStorage.setItem('lcs_config', JSON.stringify(config));
  390. window.location.reload(true);
  391. });
  392. }
  393. initDBSelector() {
  394. const ref = document.querySelector('#currentdb');
  395. ref.value = window._app.dbHost;
  396. const dbSelect = document.querySelector('#dbSelect');
  397. mdc.textField.MDCTextField.attachTo(dbSelect);
  398. dbSelect.addEventListener('change', function (e) {
  399. console.log(e.target.value);
  400. let config = JSON.parse(localStorage.getItem('lcs_config'));
  401. config.dbhost = e.target.value;
  402. localStorage.setItem('lcs_config', JSON.stringify(config));
  403. window.location.reload(true);
  404. });
  405. }
  406. parseAppInstancesData(data) {
  407. if (data == "{}") {
  408. var el = document.querySelector(".instance");
  409. if (el) {
  410. var topEl = el.parentNode;
  411. topEl.removeChild(el);
  412. }
  413. // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  414. }
  415. let jsonObj = JSON.parse(data);
  416. var parsed = {};
  417. for (var prop in jsonObj) {
  418. var name = prop.split('/')[1];
  419. if (parsed[name]) {
  420. parsed[name][prop] = jsonObj[prop];
  421. } else {
  422. parsed[name] = {};
  423. parsed[name][prop] = jsonObj[prop];
  424. }
  425. }
  426. //console.log(parsed);
  427. if (Object.entries(this.worlds).length !== 0)
  428. document.querySelector("#main")._emptyLists();
  429. for (var prop in parsed) {
  430. var name = prop;
  431. let obj = Object.entries(parsed[prop]);
  432. var lists = {};
  433. obj.forEach(el => {
  434. let sotSave = prop;
  435. if (el[1].loadInfo['save_name']) {
  436. let saveName = prop + '/load/' + el[1].loadInfo.save_name;
  437. if (!lists[saveName])
  438. lists[saveName] = {};
  439. lists[saveName][el[0]] = el[1]
  440. } else {
  441. if (!lists[name])
  442. lists[name] = {};
  443. lists[name][el[0]] = el[1]
  444. }
  445. });
  446. // console.log(lists);
  447. Object.entries(lists).forEach(list => {
  448. let element = document.getElementById(list[0] + 'List');
  449. if (element) {
  450. element._setListData(list[1]);
  451. }
  452. })
  453. }
  454. // console.log(data)
  455. }
  456. initWorldsListGUI() {
  457. var self = this;
  458. let worldsListGUI = {
  459. $cell: true,
  460. $type: "div",
  461. id: "main",
  462. _status: "",
  463. _jsonData: {},
  464. _emptyLists: function () {
  465. Object.entries(this._jsonData).forEach(function (element) {
  466. //console.log(element);
  467. let el = document.getElementById(element[0] + 'List');
  468. if (el)
  469. el._setListData({});
  470. });
  471. },
  472. $init: function () {
  473. this._jsonData = {} //data//JSON.parse(data);
  474. },
  475. _makeWorldCard: function (m) {
  476. let langID = localStorage.getItem('krestianstvo_locale');
  477. var appInfo = m
  478. if (langID) {
  479. if (m[1][langID]) {
  480. appInfo = [m[0], m[1][langID], m[1].user, m[1].type]
  481. }
  482. }
  483. return {
  484. $cell: true,
  485. $type: "div",
  486. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  487. $components: [
  488. this._worldCardDef(appInfo)
  489. ]
  490. }
  491. },
  492. $update: function () {
  493. this.$components = [
  494. {
  495. $type: "div",
  496. class: "mdc-layout-grid",
  497. $components: [
  498. {
  499. $type: "div",
  500. class: "mdc-layout-grid__inner",
  501. $components: [
  502. {
  503. $type: "div",
  504. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  505. $components: [
  506. {
  507. $type: "H3",
  508. $text: this._status
  509. }
  510. ]
  511. }
  512. ]
  513. },
  514. {
  515. $type: "div",
  516. class: "mdc-layout-grid__inner",
  517. $components: Object.entries(this._jsonData).map(this._makeWorldCard)
  518. }
  519. ]
  520. },
  521. ]
  522. },
  523. _worldCardDef: function (desc) {
  524. var userGUI = [];
  525. if (desc[3] == 'proto') {
  526. userGUI.push(
  527. {
  528. $type: "a",
  529. class: "mdc-button mdc-button--compact mdc-card__action",
  530. $text: "States",
  531. onclick: function (e) {
  532. //console.log('clone');
  533. self.showOnlySaveStates(desc[0], desc[2]);
  534. //self.refresh();
  535. }
  536. }
  537. )
  538. }
  539. if (_LCSUSER.is) {
  540. if (_LCSUSER.is.alias == desc[2]) {
  541. userGUI.push(
  542. {
  543. $type: "a",
  544. class: "mdc-button mdc-button--compact mdc-card__action",
  545. $text: "Edit info",
  546. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  547. onclick: function (e) {
  548. //'/:user/:type/:name/edit/:file'
  549. if (desc[3] == 'proto') {
  550. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/info_json'
  551. } else if (desc[3] == 'saveState') {
  552. let names = desc[0].split('/');
  553. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  554. window.location.pathname = "/" + desc[2] + '/state/' + names[0] + '/edit/' + filename;
  555. }
  556. //self.refresh();
  557. }
  558. }
  559. );
  560. if (desc[3] == 'proto') {
  561. userGUI.push(
  562. {
  563. $type: "a",
  564. class: "mdc-button mdc-button--compact mdc-card__action",
  565. $text: "Edit proto",
  566. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  567. onclick: function (e) {
  568. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/index_vwf_yaml'
  569. }
  570. }
  571. );
  572. }
  573. }
  574. if (desc[3] == 'proto') {
  575. userGUI.push(
  576. {
  577. $type: "a",
  578. class: "mdc-button mdc-button--compact mdc-card__action",
  579. $text: self.language.t('clone proto'),//"clone",
  580. onclick: function (e) {
  581. //console.log('clone');
  582. _app.cloneWorldPrototype(desc[0], desc[2]);
  583. //self.refresh();
  584. }
  585. }
  586. )
  587. } else if (desc[3] == 'saveState') {
  588. // userGUI.push(
  589. // {
  590. // $type: "a",
  591. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  592. // $text: "Clone",
  593. // onclick: function (e) {
  594. // //console.log('clone');
  595. // //self.cloneWorldState(desc[0], desc[2]);
  596. // //self.refresh();
  597. // }
  598. // })
  599. }
  600. }
  601. return {
  602. $cell: true,
  603. $type: "div",
  604. class: "mdc-card world-card",
  605. _appInfo: desc,
  606. $components: [
  607. {
  608. $type: "section",
  609. class: "mdc-card__media world-card__16-9-media",
  610. $init: function () {
  611. if (desc[1].imgUrl !== "") {
  612. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  613. }
  614. }
  615. },
  616. {
  617. $type: "section",
  618. class: "mdc-card__primary",
  619. $components: [
  620. {
  621. $type: "h1",
  622. class: "mdc-card__title mdc-card__title--large",
  623. $text: desc[1].title
  624. },
  625. {
  626. $type: "h2",
  627. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  628. $text: desc[1].text
  629. }
  630. ]
  631. },
  632. {
  633. $type: "section",
  634. class: "mdc-card__actions",
  635. $components: [
  636. {
  637. $type: "a",
  638. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  639. $text: self.language.t('start'),//"Start new",
  640. target: "_blank",
  641. href: "/" + desc[2] + '/' + desc[0],
  642. onclick: function (e) {
  643. self.refresh();
  644. }
  645. }
  646. ].concat(userGUI)
  647. },
  648. {
  649. $type: "section",
  650. class: "mdc-card__actions",
  651. $components: [
  652. {
  653. $type: "ul",
  654. _listData: {},
  655. _setListData: function (data) {
  656. this._listData = data;
  657. },
  658. class: "mdc-list mdc-list--two-line",
  659. 'aria-orientation': "vertical",
  660. id: desc[0] + 'List',
  661. $update: function () {
  662. var connectText = {}
  663. let cardListData = Object.entries(this._listData).filter(el => el[1].user == this._appInfo[2]);
  664. if (cardListData.length !== 0) {
  665. connectText = {
  666. // $type: "span",
  667. // class: "mdc-theme--text-secondary",
  668. // $text: "...or connect to:"
  669. }
  670. }
  671. this.$components = [
  672. {
  673. $type: "hr",
  674. class: "mdc-list-divider"
  675. }
  676. ].concat(cardListData.map(this._worldListItem))
  677. // [connectText]
  678. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  679. },
  680. _worldListItem: function (m) {
  681. return {
  682. $type: "li",
  683. class: "mdc-list-item",
  684. $components: [
  685. {
  686. $type: "span",
  687. class: "world-link mdc-list-item__text",
  688. $components: [
  689. {
  690. $type: "span",
  691. class: "mdc-list-item__primary-text",
  692. $components: [
  693. {
  694. $type: "a",
  695. $text: m[0],
  696. target: "_blank",
  697. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  698. onclick: function (e) {
  699. //self.refresh();
  700. }
  701. },
  702. ]
  703. },
  704. {
  705. $type: "span",
  706. class: "mdc-list-item__secondary-text",
  707. $text: self.language.t('users') + m[1].clients
  708. }
  709. ]
  710. }
  711. ]
  712. }
  713. }
  714. }
  715. ]
  716. }
  717. ]
  718. }
  719. }
  720. }
  721. document.querySelector("#main").$cell({
  722. $cell: true,
  723. $type: "div",
  724. $components: [worldsListGUI]
  725. })
  726. }
  727. async showOnlySaveStates(index, userAlias) {
  728. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  729. var db = _LCSDB.user(userPub);
  730. if (_LCSUSER.is) {
  731. if (_LCSUSER.is.alias == userAlias)
  732. db = _LCSUSER;
  733. }
  734. this.worlds = {};
  735. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  736. document.querySelector("#main")._status = "Save states of the World: " + index + ' for user: ' + userAlias;
  737. document.querySelector("#main").$update();
  738. //let userAlias = _LCSUSER.is.alias;
  739. db.get('documents').get(index).once(save => {
  740. if (save) {
  741. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  742. console.log(saves);
  743. if (saves) {
  744. saves.forEach(el => {
  745. db.get('documents').get(index).get(el).once(res => {
  746. if (res) {
  747. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  748. let world = JSON.parse(res.file);
  749. let root = Object.keys(world)[0];
  750. world[root].user = userAlias;
  751. world[root].type = 'saveState';
  752. this.worlds[index + '/load/' + fileName] = world[root];
  753. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  754. }
  755. })
  756. })
  757. }
  758. }
  759. })
  760. }
  761. async getWorldsProtosFromUserDB(userAlias) {
  762. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  763. console.log('get user worlds for: ' + userAlias);
  764. this.worlds = {};
  765. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  766. if (!userPub) {
  767. document.querySelector("#main")._status = "no such user";
  768. document.querySelector("#main").$update();
  769. }
  770. if (userPub) {
  771. document.querySelector("#main")._status = "Worlds protos for: " + userAlias;
  772. document.querySelector("#main").$update();
  773. var db = _LCSDB.user(userPub);
  774. if (_LCSUSER.is) {
  775. if (_LCSUSER.is.alias == userAlias)
  776. db = _LCSUSER;
  777. }
  778. db.get('worlds').map().once((w, index) => {
  779. if (w) {
  780. db.get('worlds').get(index).get('info_json').once(res => {
  781. if (res) {
  782. let world = JSON.parse(res.file);
  783. let root = Object.keys(world)[0];
  784. world[root].user = userAlias;
  785. world[root].type = 'proto';
  786. this.worlds[index] = world[root];
  787. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  788. }
  789. })
  790. }
  791. })
  792. }
  793. }
  794. async getWorldsFromUserDB(userAlias) {
  795. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  796. console.log('get user worlds for: ' + userAlias);
  797. this.worlds = {};
  798. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  799. if (!userPub) {
  800. document.querySelector("#main")._status = "no such user";
  801. document.querySelector("#main").$update();
  802. }
  803. if (userPub) {
  804. document.querySelector("#main")._status = "Worlds states for: " + userAlias;
  805. document.querySelector("#main").$update();
  806. var db = _LCSDB.user(userPub);
  807. if (_LCSUSER.is) {
  808. if (_LCSUSER.is.alias == userAlias)
  809. db = _LCSUSER;
  810. }
  811. db.get('worlds').map().once((w, index) => {
  812. if (w) {
  813. db.get('documents').get(index).once(save => {
  814. if (save) {
  815. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  816. console.log(saves);
  817. if (saves) {
  818. saves.forEach(el => {
  819. db.get('documents').get(index).get(el).once(res => {
  820. if (res) {
  821. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  822. let world = JSON.parse(res.file);
  823. let root = Object.keys(world)[0];
  824. world[root].user = userAlias;
  825. world[root].type = 'saveState';
  826. this.worlds[index + '/load/' + fileName] = world[root];
  827. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  828. }
  829. })
  830. })
  831. }
  832. }
  833. })
  834. }
  835. })
  836. }
  837. }
  838. async getAppDetailsFromDefaultDB(type) {
  839. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  840. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  841. page.redirect('/' + userAlias + '/worlds/' + type);
  842. }
  843. async getAppDetailsFromDB() {
  844. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  845. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  846. if (userAlias)
  847. this.getWorldsProtosFromUserDB(userAlias);
  848. }
  849. refresh() {
  850. // socket.emit('getWebAppUpdate', "");
  851. }
  852. }
  853. export { IndexApp }
  854. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};