index-app.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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: ' + cardInfo.title
  632. },
  633. {
  634. $type: "p",
  635. },
  636. {
  637. $type: "span",
  638. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  639. $text: 'created: ' + (new Date(desc[4])).toUTCString()
  640. },
  641. {
  642. $type: "p",
  643. }
  644. // ,{
  645. // $type: "span",
  646. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  647. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  648. // }
  649. ]
  650. },
  651. {
  652. $type: "section",
  653. class: "mdc-card__actions",
  654. $components: [
  655. {
  656. $type: "a",
  657. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  658. $text: self.language.t('start'),//"Start new",
  659. target: "_blank",
  660. href: "/" + desc[2] + '/' + desc[0],
  661. onclick: function (e) {
  662. self.refresh();
  663. }
  664. }
  665. ].concat(userGUI)
  666. },
  667. {
  668. $type: "section",
  669. class: "mdc-card__actions",
  670. $components: [
  671. {
  672. $type: "ul",
  673. _listData: {},
  674. _setListData: function (data) {
  675. this._listData = data;
  676. },
  677. class: "mdc-list mdc-list--two-line",
  678. 'aria-orientation': "vertical",
  679. id: desc[0] + 'List',
  680. $update: function () {
  681. var connectText = {}
  682. let cardListData = Object.entries(this._listData).filter(el => el[1].user == this._appInfo[2]);
  683. if (cardListData.length !== 0) {
  684. connectText = {
  685. // $type: "span",
  686. // class: "mdc-theme--text-secondary",
  687. // $text: "...or connect to:"
  688. }
  689. }
  690. this.$components = [
  691. {
  692. $type: "hr",
  693. class: "mdc-list-divider"
  694. }
  695. ].concat(cardListData.map(this._worldListItem))
  696. // [connectText]
  697. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  698. },
  699. _worldListItem: function (m) {
  700. return {
  701. $type: "li",
  702. class: "mdc-list-item",
  703. $components: [
  704. {
  705. $type: "span",
  706. class: "world-link mdc-list-item__text",
  707. $components: [
  708. {
  709. $type: "span",
  710. class: "mdc-list-item__primary-text",
  711. $components: [
  712. {
  713. $type: "a",
  714. $text: m[0],
  715. target: "_blank",
  716. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  717. onclick: function (e) {
  718. //self.refresh();
  719. }
  720. },
  721. ]
  722. },
  723. {
  724. $type: "span",
  725. class: "mdc-list-item__secondary-text",
  726. $text: self.language.t('users') + m[1].clients
  727. }
  728. ]
  729. }
  730. ]
  731. }
  732. }
  733. }
  734. ]
  735. }
  736. ]
  737. }
  738. }
  739. }
  740. document.querySelector("#main").$cell({
  741. $cell: true,
  742. $type: "div",
  743. $components: [worldsListGUI]
  744. })
  745. }
  746. async showOnlySaveStates(index, userAlias) {
  747. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  748. var db = _LCSDB.user(userPub);
  749. if (_LCSUSER.is) {
  750. if (_LCSUSER.is.alias == userAlias)
  751. db = _LCSUSER;
  752. }
  753. this.worlds = {};
  754. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  755. document.querySelector("#main")._status = "Save states of the World: " + index + ' for user: ' + userAlias;
  756. document.querySelector("#main").$update();
  757. //let userAlias = _LCSUSER.is.alias;
  758. db.get('documents').get(index).once(save => {
  759. if (save) {
  760. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  761. console.log(saves);
  762. if (saves) {
  763. saves.forEach(el => {
  764. db.get('documents').get(index).get(el).once(res => {
  765. if (res) {
  766. let created = res.created ? res.created: res.modified;
  767. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  768. let world = JSON.parse(res.file);
  769. let root = Object.keys(world)[0];
  770. world[root].user = userAlias;
  771. world[root].type = 'saveState';
  772. world[root].created = created;
  773. world[root].modified = res.modified;
  774. this.worlds[index + '/load/' + fileName] = world[root];
  775. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  776. }
  777. })
  778. })
  779. }
  780. }
  781. })
  782. }
  783. async getWorldsProtosFromUserDB(userAlias) {
  784. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  785. console.log('get user worlds for: ' + userAlias);
  786. this.worlds = {};
  787. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  788. if (!userPub) {
  789. document.querySelector("#main")._status = "no such user";
  790. document.querySelector("#main").$update();
  791. }
  792. if (userPub) {
  793. document.querySelector("#main")._status = "Worlds protos for: " + userAlias;
  794. document.querySelector("#main").$update();
  795. var db = _LCSDB.user(userPub);
  796. if (_LCSUSER.is) {
  797. if (_LCSUSER.is.alias == userAlias)
  798. db = _LCSUSER;
  799. }
  800. db.get('worlds').map().once((w, index) => {
  801. if (w) {
  802. db.get('worlds').get(index).get('info_json').once(res => {
  803. if (res && res !== 'null') {
  804. if (res.file && res.file !== 'null') {
  805. let created = res.created ? res.created: res.modified;
  806. let world = JSON.parse(res.file);
  807. let root = Object.keys(world)[0];
  808. world[root].user = userAlias;
  809. world[root].type = 'proto';
  810. world[root].created = created;
  811. world[root].modified = res.modified;
  812. this.worlds[index] = world[root];
  813. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  814. }
  815. }
  816. })
  817. }
  818. })
  819. }
  820. }
  821. async getWorldsFromUserDB(userAlias) {
  822. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  823. console.log('get user worlds for: ' + userAlias);
  824. this.worlds = {};
  825. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  826. if (!userPub) {
  827. document.querySelector("#main")._status = "no such user";
  828. document.querySelector("#main").$update();
  829. }
  830. if (userPub) {
  831. document.querySelector("#main")._status = "Worlds states for: " + userAlias;
  832. document.querySelector("#main").$update();
  833. var db = _LCSDB.user(userPub);
  834. if (_LCSUSER.is) {
  835. if (_LCSUSER.is.alias == userAlias)
  836. db = _LCSUSER;
  837. }
  838. db.get('worlds').map().once((w, index) => {
  839. if (w) {
  840. db.get('documents').get(index).once(save => {
  841. if (save) {
  842. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  843. console.log(saves);
  844. if (saves) {
  845. saves.forEach(el => {
  846. db.get('documents').get(index).get(el).once(res => {
  847. if (res ) {
  848. if(res.file && res.file !== "null") {
  849. let created = res.created ? res.created: res.modified;
  850. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  851. let world = JSON.parse(res.file);
  852. let root = Object.keys(world)[0];
  853. world[root].user = userAlias;
  854. world[root].type = 'saveState';
  855. world[root].created = created;
  856. world[root].modified = res.modified;
  857. this.worlds[index + '/load/' + fileName] = world[root];
  858. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  859. }
  860. }
  861. })
  862. })
  863. }
  864. }
  865. })
  866. }
  867. })
  868. }
  869. }
  870. async getAppDetailsFromDefaultDB(type) {
  871. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  872. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  873. page.redirect('/' + userAlias + '/worlds/' + type);
  874. }
  875. async getAppDetailsFromDB() {
  876. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  877. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  878. if (userAlias)
  879. this.getWorldsProtosFromUserDB(userAlias);
  880. }
  881. refresh() {
  882. // socket.emit('getWebAppUpdate', "");
  883. }
  884. }
  885. export { IndexApp }
  886. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};