index-app.js 40 KB

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