index-app.js 40 KB

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