index-app.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. let userGUI = [];
  492. let cardInfo = {
  493. "title": ""
  494. };
  495. if (desc[3] == 'saveState') {
  496. cardInfo.title = desc[0].split('/')[2];
  497. }
  498. if (desc[3] == 'proto') {
  499. cardInfo.title = desc[0];
  500. userGUI.push(
  501. {
  502. $type: "a",
  503. class: "mdc-button mdc-button--compact mdc-card__action",
  504. $text: "States",
  505. onclick: function (e) {
  506. //console.log('clone');
  507. self.showOnlySaveStates(desc[0], desc[2]);
  508. //self.refresh();
  509. }
  510. }
  511. )
  512. }
  513. if (_LCSUSER.is) {
  514. if (_LCSUSER.is.alias == desc[2]) {
  515. userGUI.push(
  516. {
  517. $type: "a",
  518. class: "mdc-button mdc-button--compact mdc-card__action",
  519. $text: "Edit info",
  520. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  521. onclick: function (e) {
  522. //'/:user/:type/:name/edit/:file'
  523. if (desc[3] == 'proto') {
  524. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/info_json'
  525. } else if (desc[3] == 'saveState') {
  526. let names = desc[0].split('/');
  527. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  528. window.location.pathname = "/" + desc[2] + '/state/' + names[0] + '/edit/' + filename;
  529. }
  530. //self.refresh();
  531. }
  532. }
  533. );
  534. if (desc[3] == 'proto') {
  535. userGUI.push(
  536. {
  537. $type: "a",
  538. class: "mdc-button mdc-button--compact mdc-card__action",
  539. $text: "Edit proto",
  540. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  541. onclick: function (e) {
  542. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/index_vwf_yaml'
  543. }
  544. }
  545. );
  546. userGUI.push(
  547. {
  548. $type: "a",
  549. class: "mdc-button mdc-button--compact mdc-card__action",
  550. $text: "Delete",
  551. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  552. onclick: function (e) {
  553. _app.deleteWorld(desc[0], 'proto');
  554. }
  555. }
  556. );
  557. }
  558. if(desc[3] == 'saveState'){
  559. userGUI.push(
  560. {
  561. $type: "a",
  562. class: "mdc-button mdc-button--compact mdc-card__action",
  563. $text: "Delete",
  564. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  565. onclick: function (e) {
  566. _app.deleteWorld(desc[0], 'state');
  567. }
  568. }
  569. );
  570. }
  571. }
  572. if (desc[3] == 'proto') {
  573. userGUI.push(
  574. {
  575. $type: "a",
  576. class: "mdc-button mdc-button--compact mdc-card__action",
  577. $text: self.language.t('clone proto'),//"clone",
  578. onclick: function (e) {
  579. //console.log('clone');
  580. _app.cloneWorldPrototype(desc[0], desc[2]);
  581. //self.refresh();
  582. }
  583. }
  584. )
  585. } else if (desc[3] == 'saveState') {
  586. // userGUI.push(
  587. // {
  588. // $type: "a",
  589. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  590. // $text: "Clone",
  591. // onclick: function (e) {
  592. // //console.log('clone');
  593. // //self.cloneWorldState(desc[0], desc[2]);
  594. // //self.refresh();
  595. // }
  596. // })
  597. }
  598. }
  599. return {
  600. $cell: true,
  601. $type: "div",
  602. class: "mdc-card world-card",
  603. _appInfo: desc,
  604. $components: [
  605. {
  606. $type: "section",
  607. class: "mdc-card__media world-card__16-9-media",
  608. $init: function () {
  609. if (desc[1].imgUrl !== "") {
  610. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  611. }
  612. }
  613. },
  614. {
  615. $type: "section",
  616. class: "mdc-card__primary",
  617. $components: [
  618. {
  619. $type: "h1",
  620. class: "mdc-card__title mdc-card__title--large",
  621. $text: desc[1].title
  622. },
  623. {
  624. $type: "h2",
  625. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  626. $text: desc[1].text
  627. },
  628. {
  629. $type: "span",
  630. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  631. $text: 'id: '
  632. },
  633. {
  634. $type: "input",
  635. type: "text",
  636. disabled: "",
  637. style: "font-size:18px",
  638. value: cardInfo.title
  639. },
  640. {
  641. $type: "p",
  642. },
  643. {
  644. $type: "span",
  645. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  646. $text: 'created: ' + (new Date(desc[4])).toUTCString()
  647. },
  648. {
  649. $type: "p",
  650. }
  651. // ,{
  652. // $type: "span",
  653. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  654. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  655. // }
  656. ]
  657. },
  658. {
  659. $type: "section",
  660. class: "mdc-card__actions",
  661. $components: [
  662. {
  663. $type: "a",
  664. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  665. $text: self.language.t('start'),//"Start new",
  666. target: "_blank",
  667. href: "/" + desc[2] + '/' + desc[0],
  668. onclick: function (e) {
  669. self.refresh();
  670. }
  671. }
  672. ].concat(userGUI)
  673. },
  674. {
  675. $type: "section",
  676. class: "mdc-card__actions",
  677. $components: [
  678. {
  679. $type: "ul",
  680. _listData: {},
  681. _setListData: function (data) {
  682. this._listData = data;
  683. },
  684. class: "mdc-list mdc-list--two-line",
  685. 'aria-orientation': "vertical",
  686. id: desc[0] + 'List',
  687. $update: function () {
  688. var connectText = {}
  689. let cardListData = Object.entries(this._listData).filter(el => el[1].user == this._appInfo[2]);
  690. if (cardListData.length !== 0) {
  691. connectText = {
  692. // $type: "span",
  693. // class: "mdc-theme--text-secondary",
  694. // $text: "...or connect to:"
  695. }
  696. }
  697. this.$components = [
  698. {
  699. $type: "hr",
  700. class: "mdc-list-divider"
  701. }
  702. ].concat(cardListData.map(this._worldListItem))
  703. // [connectText]
  704. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  705. },
  706. _worldListItem: function (m) {
  707. return {
  708. $type: "li",
  709. class: "mdc-list-item",
  710. $components: [
  711. {
  712. $type: "span",
  713. class: "world-link mdc-list-item__text",
  714. $components: [
  715. {
  716. $type: "span",
  717. class: "mdc-list-item__primary-text",
  718. $components: [
  719. {
  720. $type: "a",
  721. $text: m[0],
  722. target: "_blank",
  723. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  724. onclick: function (e) {
  725. //self.refresh();
  726. }
  727. },
  728. ]
  729. },
  730. {
  731. $type: "span",
  732. class: "mdc-list-item__secondary-text",
  733. $text: self.language.t('users') + m[1].clients
  734. }
  735. ]
  736. }
  737. ]
  738. }
  739. }
  740. }
  741. ]
  742. }
  743. ]
  744. }
  745. }
  746. }
  747. document.querySelector("#main").$cell({
  748. $cell: true,
  749. $type: "div",
  750. $components: [worldsListGUI]
  751. })
  752. }
  753. async showOnlySaveStates(index, userAlias) {
  754. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  755. var db = _LCSDB.user(userPub);
  756. if (_LCSUSER.is) {
  757. if (_LCSUSER.is.alias == userAlias)
  758. db = _LCSUSER;
  759. }
  760. this.worlds = {};
  761. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  762. document.querySelector("#main")._status = "Save states of the World: " + index + ' for user: ' + userAlias;
  763. document.querySelector("#main").$update();
  764. //let userAlias = _LCSUSER.is.alias;
  765. db.get('documents').get(index).once(save => {
  766. if (save) {
  767. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  768. console.log(saves);
  769. if (saves) {
  770. saves.forEach(el => {
  771. db.get('documents').get(index).get(el).once(res => {
  772. if (res) {
  773. let created = res.created ? res.created: res.modified;
  774. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  775. let world = JSON.parse(res.file);
  776. let root = Object.keys(world)[0];
  777. world[root].user = userAlias;
  778. world[root].type = 'saveState';
  779. world[root].created = created;
  780. world[root].modified = res.modified;
  781. this.worlds[index + '/load/' + fileName] = world[root];
  782. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  783. }
  784. })
  785. })
  786. }
  787. }
  788. })
  789. }
  790. async getWorldsProtosFromUserDB(userAlias) {
  791. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  792. console.log('get user worlds for: ' + userAlias);
  793. this.worlds = {};
  794. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  795. if (!userPub) {
  796. document.querySelector("#main")._status = "no such user";
  797. document.querySelector("#main").$update();
  798. }
  799. if (userPub) {
  800. document.querySelector("#main")._status = "Worlds protos for: " + userAlias;
  801. document.querySelector("#main").$update();
  802. var db = _LCSDB.user(userPub);
  803. if (_LCSUSER.is) {
  804. if (_LCSUSER.is.alias == userAlias)
  805. db = _LCSUSER;
  806. }
  807. db.get('worlds').map().once((w, index) => {
  808. if (w) {
  809. db.get('worlds').get(index).get('info_json').once(res => {
  810. if (res && res !== 'null') {
  811. if (res.file && res.file !== 'null') {
  812. let created = res.created ? res.created: res.modified;
  813. let world = JSON.parse(res.file);
  814. let root = Object.keys(world)[0];
  815. world[root].user = userAlias;
  816. world[root].type = 'proto';
  817. world[root].created = created;
  818. world[root].modified = res.modified;
  819. this.worlds[index] = world[root];
  820. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  821. }
  822. }
  823. })
  824. }
  825. })
  826. }
  827. }
  828. async getWorldsFromUserDB(userAlias) {
  829. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  830. console.log('get user worlds for: ' + userAlias);
  831. this.worlds = {};
  832. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  833. if (!userPub) {
  834. document.querySelector("#main")._status = "no such user";
  835. document.querySelector("#main").$update();
  836. }
  837. if (userPub) {
  838. document.querySelector("#main")._status = "Worlds states for: " + userAlias;
  839. document.querySelector("#main").$update();
  840. var db = _LCSDB.user(userPub);
  841. if (_LCSUSER.is) {
  842. if (_LCSUSER.is.alias == userAlias)
  843. db = _LCSUSER;
  844. }
  845. db.get('worlds').map().once((w, index) => {
  846. if (w) {
  847. db.get('documents').get(index).once(save => {
  848. if (save) {
  849. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  850. console.log(saves);
  851. if (saves) {
  852. saves.forEach(el => {
  853. db.get('documents').get(index).get(el).once(res => {
  854. if (res ) {
  855. if(res.file && res.file !== "null") {
  856. let created = res.created ? res.created: res.modified;
  857. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  858. let world = JSON.parse(res.file);
  859. let root = Object.keys(world)[0];
  860. world[root].user = userAlias;
  861. world[root].type = 'saveState';
  862. world[root].created = created;
  863. world[root].modified = res.modified;
  864. this.worlds[index + '/load/' + fileName] = world[root];
  865. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  866. }
  867. }
  868. })
  869. })
  870. }
  871. }
  872. })
  873. }
  874. })
  875. }
  876. }
  877. async getAppDetailsFromDefaultDB(type) {
  878. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  879. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  880. page.redirect('/' + userAlias + '/worlds/' + type);
  881. }
  882. async getAppDetailsFromDB() {
  883. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  884. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  885. if (userAlias)
  886. this.getWorldsProtosFromUserDB(userAlias);
  887. }
  888. refresh() {
  889. // socket.emit('getWebAppUpdate', "");
  890. }
  891. }
  892. export { IndexApp }
  893. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};