index-app.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. //import page from '/lib/page.mjs';
  6. //import { Header } from '/web/header.js';
  7. class IndexApp {
  8. constructor(entry) {
  9. console.log("index app constructor");
  10. this.entry = entry;
  11. this.worlds = {};
  12. this.instances = {};
  13. if (!_app.isLuminary) {
  14. this.initReflectorConnection();
  15. }
  16. this.initHTML();
  17. }
  18. initReflectorConnection() {
  19. this.options = {
  20. query: 'pathname=' + window.location.pathname.slice(1,
  21. window.location.pathname.lastIndexOf("/")),
  22. secure: window.location.protocol === "https:",
  23. reconnection: false,
  24. path: '',
  25. transports: ['websocket']
  26. }
  27. //window.location.host
  28. var socket = io.connect(window._app.reflectorHost, this.options);
  29. const parse = (msg) => {
  30. this.parseOnlineData(msg)
  31. }
  32. socket.on('getWebAppUpdate', msg => parse.call(this, msg));
  33. socket.on("connect", function () {
  34. let noty = new Noty({
  35. text: 'Connected to Reflector!',
  36. timeout: 2000,
  37. theme: 'mint',
  38. layout: 'bottomRight',
  39. type: 'success'
  40. });
  41. noty.show();
  42. })
  43. socket.on('connect_error', function (err) {
  44. console.log(err);
  45. var errDiv = document.createElement("div");
  46. errDiv.innerHTML = "<div class='vwf-err' style='z-index: 10; position: absolute; top: 80px; right: 50px'>Connection error to Reflector!" + err + "</div>";
  47. document.querySelector('body').appendChild(errDiv);
  48. let noty = new Noty({
  49. text: 'Connection error to Reflector! ' + err,
  50. theme: 'mint',
  51. layout: 'bottomRight',
  52. type: 'error'
  53. });
  54. noty.show();
  55. });
  56. }
  57. initHTML() {
  58. let self = this;
  59. //first init from _app
  60. document.querySelector('head').innerHTML += '<link rel="stylesheet" href="/web/index-app.css">';
  61. //if(this.entry !== 'index'){
  62. import('/web/header.js').then(res => {
  63. let gui = new res.Header();
  64. gui.init();
  65. })
  66. // }
  67. import('/web/footer.js').then(res => {
  68. let gui = new res.Footer();
  69. gui.init();
  70. })
  71. //add HTML
  72. let entry = document.createElement("div");
  73. entry.setAttribute("id", 'app');
  74. document.body.appendChild(entry);
  75. let divs = ['appGUI', 'userLobby', 'worldsGUI'];
  76. divs.forEach(el => {
  77. let appEl = document.createElement("div");
  78. appEl.setAttribute("id", el);
  79. entry.appendChild(appEl);
  80. })
  81. document.querySelector("#worldsGUI").$cell({
  82. id: "worldsGUI",
  83. $cell: true,
  84. $type: "div",
  85. _comps: [],
  86. _wcards: {},
  87. $components: [],
  88. _refresh: function (comps) {
  89. //do update;
  90. //this._userAlias = user;
  91. this._comps = comps;
  92. this.$components = this._comps;
  93. },
  94. $init: function () {
  95. console.log('init comp...');
  96. },
  97. $update: function () {
  98. //do update;
  99. console.log('update me');
  100. }
  101. });
  102. //init CELL
  103. let userGUI = {
  104. $type: "div",
  105. id: "userGUI",
  106. // style:"background-color: #ffeb3b",
  107. class: "mdc-layout-grid mdc-layout-grid--align-left",
  108. _status: _l.t("welcome") + '!',//"Welcome!",
  109. $init: function () {
  110. //this._status = "Welcome!"
  111. //this._status = 'Welcome!';
  112. self.initUser();
  113. this._refresh(); //$update();
  114. },
  115. $update: function () { },
  116. _refresh: function () {
  117. var gui = {};
  118. if (_LCSDB.user().is) {
  119. gui = [
  120. window._app.widgets.buttonRaised(
  121. {
  122. "label": _l.t("sign out"), //'Sign OUT',
  123. "onclick": function (e) {
  124. _LCSDB.user().leave();
  125. setTimeout(() => {
  126. window.location.reload(true);
  127. }, 1);
  128. }
  129. }),
  130. {
  131. $type: "p"
  132. },
  133. window._app.widgets.buttonStroked(
  134. {
  135. "label": _l.t("profile"),//'PROFILE',
  136. "onclick": function (e) {
  137. e.preventDefault();
  138. //page("/profile")
  139. window.location.pathname = "/profile"
  140. }
  141. }),
  142. {
  143. $type: "p"
  144. },
  145. window._app.widgets.buttonStroked(
  146. {
  147. "label": _l.t("my world protos"),
  148. "onclick": function (e) {
  149. e.preventDefault();
  150. let alias = _LCSDB.user().is.alias;
  151. window.location.pathname = '/' + alias + '/worlds/protos'
  152. //page('/' + alias + '/worlds/protos');
  153. //_app.indexApp.getWorldsProtosFromUserDB(alias);
  154. }
  155. }),
  156. window._app.widgets.buttonStroked(
  157. {
  158. "label": _l.t("my world states"),
  159. "onclick": function (e) {
  160. e.preventDefault();
  161. let alias = _LCSDB.user().is.alias;
  162. window.location.pathname = '/' + alias + '/worlds/states'
  163. //page('/' + alias + '/worlds/states');
  164. // page.redirect('/' + alias + '/worlds/states');
  165. //_app.indexApp.getWorldsFromUserDB(alias);
  166. }
  167. })
  168. ]
  169. }
  170. this.$components = [
  171. {
  172. $type: "h1",
  173. class: "mdc-typography--headline3",
  174. $text: this._status
  175. }
  176. // ,{
  177. // $type: "div",
  178. // class: "mdc-typography--headline4",
  179. // $text: navigator.userAgent
  180. // }
  181. ].concat(gui)
  182. }
  183. }
  184. document.querySelector("#userLobby").$cell({
  185. id: "userLobby",
  186. $cell: true,
  187. $type: "div",
  188. $components: [],
  189. _comps: [],
  190. _refresh: function () {
  191. this.$components = this._comps.concat([userGUI, _app.widgets.getLoginGUI(), _app.widgets.divider, self.getLookWorlds()]);
  192. },
  193. $init: function () {
  194. //this._comps = self.initUserGUI()
  195. this._refresh();
  196. },
  197. $update: function () {
  198. }
  199. });
  200. }
  201. async allWorldsProtosForUser(userAlias) {
  202. let userPub = await _app.helpers.getUserPub(userAlias);
  203. //let db = _LCSDB.user(userPub);
  204. let doc = document.querySelector("#worldsGUI");
  205. var worlds = {};
  206. if (userPub) {
  207. worlds = this.createWorldsGUI('proto', userAlias, userPub)
  208. } else {
  209. worlds = {
  210. $type: 'div',
  211. $text: 'Could not find user with name: ' + userAlias,
  212. class: "mdc-typography--headline4"
  213. }
  214. }
  215. let components = [
  216. {
  217. $type: "div",
  218. class: "mdc-layout-grid",
  219. $components: [
  220. {
  221. $type: "div",
  222. class: "mdc-layout-grid__inner",
  223. $components: [
  224. {
  225. $type: "div",
  226. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  227. $components: [
  228. {
  229. $type: "h1",
  230. class: "mdc-typography--headline4",
  231. $text: _l.t("protos for") + userAlias
  232. }
  233. ]
  234. },
  235. {
  236. $type: "div",
  237. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  238. $components: [worlds]
  239. }
  240. ]
  241. }
  242. ]
  243. }
  244. ];
  245. doc._refresh(components);
  246. }
  247. async allWorldsStatesForUser(userAlias, worldName, elID) {
  248. let userPub = await _app.helpers.getUserPub(userAlias);
  249. //let db = _LCSDB.user(userPub);
  250. let doc = elID ? document.querySelector("#" + elID) : document.querySelector("#worldsGUI");
  251. var worlds = {};
  252. if (userPub) {
  253. if (!worldName) {
  254. worlds = this.createWorldsGUI('state', userAlias, userPub)
  255. } else {
  256. worlds = this.createWorldsGUI('state', userAlias, userPub, worldName)
  257. }
  258. } else {
  259. worlds = {
  260. $type: 'div',
  261. $text: 'Could not find user with name: ' + userAlias,
  262. class: "mdc-typography--headline4"
  263. }
  264. }
  265. let components = [
  266. {
  267. $type: "div",
  268. class: "mdc-layout-grid",
  269. $components: [
  270. {
  271. $type: "div",
  272. class: "mdc-layout-grid__inner",
  273. $components: [
  274. {
  275. $type: "div",
  276. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  277. $components: [
  278. {
  279. $type: "h1",
  280. class: "mdc-typography--headline4",
  281. $text: _l.t("states for") + userAlias
  282. }
  283. ]
  284. },
  285. {
  286. $type: "div",
  287. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  288. $components: [].concat(worlds)
  289. }
  290. ]
  291. }
  292. ]
  293. }
  294. ]
  295. doc._refresh(components);
  296. }
  297. authGUI() {
  298. let alias = _LCSDB.user().is.alias;
  299. let userEl = document.querySelector('#userGUI');
  300. userEl._status = _l.t("welcome") +', ' + alias + '!';
  301. //userEl.style.backgroundColor = '#e6e6e6';
  302. userEl._refresh(); //$update();
  303. //_LCSDB.user().get('profile').once(function (data) { console.log(data) })
  304. let el = document.getElementById("loginGUI");
  305. if (el) {
  306. el.remove();
  307. }
  308. _LCSDB.user().get('profile').not(function (key) {
  309. let profile = { 'alias': alias };
  310. _LCSDB.user().get('profile').put(profile);
  311. })
  312. // not load proxy by default
  313. // _LCSDB.user().get('proxy').not(res=>{
  314. // console.log('user has no proxy');
  315. // window._app.loadProxyDefaults();
  316. // });
  317. let actionsGUI = document.querySelector('#worldActionsGUI');
  318. if (actionsGUI)
  319. actionsGUI._refresh();
  320. new Noty({
  321. text: alias + ' is succesfully authenticated!',
  322. timeout: 2000,
  323. theme: 'mint',
  324. layout: 'bottomRight',
  325. type: 'success'
  326. }).show();
  327. if (this.entry == 'index') {
  328. //change for LiveCoding.space to 'app'
  329. //this.initWorldsProtosListForUserNew(alias);
  330. this.allWorldsProtosForUser(alias)
  331. }
  332. }
  333. initUser() {
  334. let self = this;
  335. if (_LCSDB.user().is) {
  336. self.authGUI()
  337. } else {
  338. _LCSDB.on('auth', function (ack) {
  339. if (ack.sea.pub) {
  340. _app.helpers.checkUserCollision();
  341. self.authGUI();
  342. }
  343. console.log(_LCSDB.user().is);
  344. });
  345. }
  346. }
  347. getLookWorlds() {
  348. let self = this;
  349. let lookWorlds =
  350. {
  351. $type: "div",
  352. id: "lookWorlds",
  353. class: "mdc-layout-grid mdc-layout-grid--align-left",
  354. _status: '',
  355. $init: function () {
  356. this._status = "init";
  357. },
  358. $update: function () {
  359. //change for LiveCoding.space site 'app'
  360. let defaultName = '';
  361. let guiForAll = [
  362. {
  363. $type: "div",
  364. style: "margin-top: 20px;",
  365. _userName: null,
  366. _userNameField: null,
  367. $components:
  368. [
  369. _app.widgets.inputTextFieldOutlined({
  370. "id": 'worldsUserName',
  371. "label": _l.t("enter user name"),
  372. "value": defaultName,
  373. "type": "text",
  374. "init": function () {
  375. this._userNameField = new mdc.textField.MDCTextField(this);
  376. }
  377. }),
  378. _app.widgets.p,
  379. // {
  380. // $type: "a",
  381. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  382. // $text: 'World Protos', //self.language.t('set proxy'),//"clone",
  383. // onclick: function (e) {
  384. // //console.log('clone');
  385. // let searchName = this._userNameField.value;
  386. // self.initWorldsProtosListForUser(searchName);
  387. // }
  388. // },
  389. // {
  390. // $type: "a",
  391. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  392. // $text: 'World States', //self.language.t('set proxy'),//"clone",
  393. // onclick: function (e) {
  394. // //console.log('clone');
  395. // let searchName = this._userNameField.value;
  396. // self.initWorldsStatesListForUser(searchName);
  397. // }
  398. // }
  399. _app.widgets.buttonRaised(
  400. {
  401. "label": _l.t("world protos"),
  402. "onclick": function (e) {
  403. e.preventDefault();
  404. //page("/app/worlds/protos")
  405. let searchName = this._userNameField.value;
  406. if (searchName !== "")
  407. window.location.pathname = "/" + searchName + "/worlds/protos"
  408. //_app.indexApp.getAppDetailsFromDefaultDB('protos');
  409. }
  410. }),
  411. _app.widgets.space,
  412. _app.widgets.buttonRaised(
  413. {
  414. "label": _l.t("world states"),
  415. "onclick": function (e) {
  416. e.preventDefault();
  417. //page("/app/worlds/states")
  418. let searchName = this._userNameField.value;
  419. if (searchName !== "")
  420. window.location.pathname = "/" + searchName + "/worlds/states"
  421. //_app.indexApp.getAppDetailsFromDefaultDB('states');
  422. }
  423. })
  424. ]
  425. },
  426. // window._app.widgets.buttonStroked(
  427. // {
  428. // "label": 'Default World Protos',
  429. // "onclick": function (e) {
  430. // e.preventDefault();
  431. // //page("/app/worlds/protos")
  432. // window.location.pathname = "/app/worlds/protos"
  433. // //_app.indexApp.getAppDetailsFromDefaultDB('protos');
  434. // }
  435. // }),
  436. // window._app.widgets.buttonStroked(
  437. // {
  438. // "label": 'Default World States',
  439. // "onclick": function (e) {
  440. // e.preventDefault();
  441. // //page("/app/worlds/states")
  442. // window.location.pathname = "/app/worlds/states"
  443. // //_app.indexApp.getAppDetailsFromDefaultDB('states');
  444. // }
  445. // })
  446. ];
  447. this.$components = [
  448. {
  449. $type: "h1",
  450. class: "mdc-typography--headline4",
  451. $text: _l.t("show worlds")//"Looking for Worlds made by other Users!"
  452. }
  453. ].concat(guiForAll, _app.widgets.p)
  454. }
  455. }
  456. return lookWorlds
  457. }
  458. refresh() {
  459. // socket.emit('getWebAppUpdate', "");
  460. }
  461. parseOnlineData(data) {
  462. let parcedData = _app.parseAppInstancesData(data);
  463. //if (Object.entries(parcedData).length !== 0)
  464. let onlineGUIs = document.querySelectorAll('.online');
  465. onlineGUIs.forEach(function (item) {
  466. item._refresh(parcedData)
  467. });
  468. }
  469. createWorldCard(worldType, userAlias, userPub, worldName, id, type, cb) {
  470. let self = this;
  471. let db = _LCSDB.user(userPub);
  472. let onlineGUI = {
  473. $cell: true,
  474. id: "onlineGUI_" + id,
  475. class: "online",
  476. $type: "div",
  477. _instances: {},
  478. _worldListItem: function (m) {
  479. return {
  480. $type: "li",
  481. class: "mdc-list-item",
  482. $components: [
  483. {
  484. $type: "span",
  485. class: "world-link mdc-list-item__text",
  486. $components: [
  487. {
  488. $type: "span",
  489. class: "mdc-list-item__primary-text",
  490. $components: [
  491. {
  492. $type: "a",
  493. $text: m[0],
  494. //target: "_blank",
  495. // href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  496. onclick: function (e) {
  497. self.checkForManualSettings();
  498. window.location.pathname = "/" + m[1].user + m[0];
  499. //self.refresh();
  500. }
  501. },
  502. ]
  503. },
  504. {
  505. $type: "span",
  506. class: "mdc-list-item__secondary-text",
  507. $text: _app.isLuminary ? _LangManager.language.t('users') + Object.keys(m[1].clients).length : _LangManager.language.t('users') + m[1].clients
  508. }
  509. ]
  510. }
  511. ]
  512. }
  513. },
  514. $components: [],
  515. _refresh: function (data) {
  516. if (data) {
  517. if (Object.entries(data).length !== 0) {
  518. if (this._worldInfo) {
  519. let insts = Object.entries(data).filter(el => el[0] == this._worldInfo.worldName);
  520. if (insts.length !== 0)
  521. this._instances = insts[0][1];
  522. }
  523. } else {
  524. this._instances = {}
  525. }
  526. }
  527. },
  528. $init: function () {
  529. if (_app.isLuminary) {
  530. let luminaryPath = _app.luminaryPath;
  531. let ref = _LCSDB.get(luminaryPath);
  532. setInterval(function () {
  533. ref.get('allclients').once().map().once(res => {
  534. if (res) {
  535. if (res.id) {
  536. let clientTime = Gun.state.is(res, 'live');
  537. let now = Gun.time.is();
  538. if (now - clientTime < 10000) {
  539. let instance = res.user + res.instance;
  540. //let data = JSON.stringify({[res.instance]: {instance: instance, clients: {}, user: res.user, loadInfo: {}}});
  541. //console.log(data);
  542. if (!self.instances[res.instance]) {
  543. self.instances[res.instance] = { id: res.instance, instance: instance, clients: { [res.id]: res }, user: res.user, loadInfo: {} }
  544. } else {
  545. self.instances[res.instance].clients[res.id] = res
  546. }
  547. let data = JSON.stringify(self.instances);
  548. self.parseOnlineData(data);
  549. } else {
  550. if (self.instances[res.instance]) {
  551. delete self.instances[res.instance].clients[res.id];
  552. if (Object.keys(self.instances[res.instance].clients).length == 0) {
  553. delete self.instances[res.instance];
  554. self.parseOnlineData(JSON.stringify({}));
  555. }
  556. }
  557. //ref.get('instances').get(res.instance).get(res.id).put(null);
  558. }
  559. }
  560. }
  561. }
  562. )
  563. }, 5000);
  564. }
  565. this._refresh();
  566. },
  567. $update: function () {
  568. if (this._instances) {
  569. let cardListData = Object.entries(this._instances).filter(el => el[1].user == this._worldInfo.userAlias);
  570. this.$components = [
  571. {
  572. $type: "hr",
  573. class: "mdc-list-divider"
  574. }
  575. ].concat(cardListData.map(this._worldListItem))
  576. }
  577. }
  578. }
  579. return {
  580. $cell: true,
  581. id: 'worldCard_' + id,
  582. $type: "div",
  583. _worldName: "",
  584. _worldInfo: {},
  585. _refresh: function (data) {
  586. this._worldInfo = data;
  587. this.$components = [this._updateCard()]
  588. },
  589. $init: function () {
  590. this._worldName = worldName;
  591. if (type == 'min') {
  592. if (worldType == 'proto') {
  593. db.get('worlds').get(this._worldName).path('info_json').on((res) => {
  594. if (res) {
  595. if (res.file) {
  596. let doc = {
  597. 'worldName': this._worldName,
  598. 'created': undefined,
  599. 'modified': undefined,
  600. 'type': 'proto',
  601. 'userAlias': userAlias,
  602. 'info': { title: 'Need to repair!' }
  603. }
  604. this._refresh(doc);
  605. return
  606. }
  607. console.log(res);
  608. let worldDesc = JSON.parse(res);
  609. let root = Object.keys(worldDesc)[0];
  610. var appInfo = worldDesc[root]['en'];
  611. let langID = localStorage.getItem('krestianstvo_locale');
  612. if (langID) {
  613. appInfo = worldDesc[root][langID]
  614. }
  615. let doc = {
  616. 'worldName': this._worldName,
  617. 'created': undefined,
  618. 'modified': undefined,
  619. 'type': 'proto',
  620. 'userAlias': userAlias,
  621. 'info': appInfo
  622. }
  623. this._refresh(doc);
  624. //callback
  625. if (cb)
  626. cb(doc);
  627. }
  628. })
  629. } else if (worldType == 'state') {
  630. let pathName = 'savestate_/' + this._worldName.protoName + '/' + this._worldName.stateName + '_info_vwf_json';
  631. db.get('documents').get(this._worldName.protoName).path(pathName).on((res) => {
  632. if (res) {
  633. if (res.file) {
  634. let doc = {
  635. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  636. 'created': undefined,
  637. 'modified': undefined,
  638. 'type': 'state',
  639. 'userAlias': userAlias,
  640. 'info': { title: 'Need to repair!' }
  641. }
  642. this._refresh(doc);
  643. return
  644. }
  645. console.log(res);
  646. let worldDesc = JSON.parse(res);
  647. let root = Object.keys(worldDesc)[0];
  648. var appInfo = worldDesc[root]['en'];
  649. let langID = localStorage.getItem('krestianstvo_locale');
  650. if (langID) {
  651. appInfo = worldDesc[root][langID]
  652. }
  653. let doc = {
  654. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  655. 'created': undefined,
  656. 'modified': undefined,
  657. 'type': 'saveState',
  658. 'userAlias': userAlias,
  659. 'info': appInfo
  660. }
  661. this._refresh(doc);
  662. //callback
  663. if (cb)
  664. cb(doc);
  665. }
  666. })
  667. }
  668. } else if (type == 'full') {
  669. if (worldType == 'proto') {
  670. db.get('worlds').get(this._worldName).on((res) => {
  671. if (res) {
  672. if (res["info_json"]['#']) {
  673. let doc = {
  674. 'worldName': this._worldName,
  675. 'created': undefined,
  676. 'modified': undefined,
  677. 'type': 'proto',
  678. 'userAlias': userAlias,
  679. 'info': { title: 'Need to repair!' }
  680. }
  681. this._refresh(doc);
  682. // if(cb)
  683. // cb(doc);
  684. return
  685. }
  686. console.log(res);
  687. let worldDesc = JSON.parse(res['info_json']);
  688. let root = Object.keys(worldDesc)[0];
  689. var appInfo = worldDesc[root]['en'];
  690. let langID = localStorage.getItem('krestianstvo_locale');
  691. if (langID) {
  692. appInfo = worldDesc[root][langID]
  693. }
  694. let settings = worldDesc[root]['settings'];
  695. let doc = {
  696. 'worldName': this._worldName,
  697. 'created': res.created ? res.created : "",
  698. 'modified': res.modified ? res.modified : "",
  699. 'proxy': res.proxy,
  700. 'type': 'proto',
  701. 'userAlias': userAlias,
  702. 'info': appInfo,
  703. 'settings': settings
  704. }
  705. this._refresh(doc);
  706. //callback
  707. if (cb)
  708. cb(doc);
  709. } else {
  710. //no world
  711. this._refresh({})
  712. }
  713. })
  714. } else if (worldType == 'state') {
  715. let pathNameInfo = 'savestate_/' + this._worldName.protoName + '/' + this._worldName.stateName + '_info_vwf_json';
  716. db.get('documents').get(this._worldName.protoName).path(pathNameInfo).on((res) => {
  717. if (res) {
  718. if (res.file) {
  719. let doc = {
  720. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  721. 'created': undefined,
  722. 'modified': undefined,
  723. 'type': 'state',
  724. 'userAlias': userAlias,
  725. 'info': { title: 'Need to repair!' }
  726. }
  727. this._refresh(doc);
  728. return
  729. }
  730. console.log(res);
  731. let worldDesc = JSON.parse(res);
  732. let root = Object.keys(worldDesc)[0];
  733. var appInfo = worldDesc[root]['en'];
  734. let langID = localStorage.getItem('krestianstvo_locale');
  735. if (langID) {
  736. appInfo = worldDesc[root][langID]
  737. }
  738. let settings = worldDesc[root]['settings'];
  739. let doc = {
  740. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  741. 'created': undefined,
  742. 'modified': undefined,
  743. 'type': 'saveState',
  744. 'userAlias': userAlias,
  745. 'info': appInfo,
  746. 'settings': settings
  747. }
  748. this._refresh(doc);
  749. //callback
  750. if (cb)
  751. cb(doc);
  752. } else {
  753. //no world
  754. this._refresh({})
  755. }
  756. })
  757. }
  758. }
  759. },
  760. $update: function () {
  761. //this.$components = [this._updateCard()]
  762. },
  763. _updateComps: function () {
  764. //console.log(this._worldInfo);
  765. },
  766. _updateCard: function () {
  767. let desc = this._worldInfo;
  768. if (!desc || Object.keys(desc).length == 0) {
  769. return {
  770. $type: "h1",
  771. class: "mdc-typography--headline4",
  772. $text: "ERROR: NO WORLD!"
  773. }
  774. }
  775. let userGUI = [];
  776. let online = [];
  777. let cardInfo = {
  778. "title": ""
  779. };
  780. if (type == "full") {
  781. } else {
  782. userGUI.push({
  783. $type: "a",
  784. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  785. $text: _l.t("details"),
  786. onclick: function (e) {
  787. e.preventDefault();
  788. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName + '/about'
  789. }
  790. });
  791. }
  792. if (desc.info.title !== 'Need to repair!') {
  793. userGUI.push({
  794. $type: "a",
  795. class: "mdc-button mdc-button--raised mdc-card__action ",
  796. $text: _LangManager.language.t('start'),//"Start new",
  797. //target: "_blank",
  798. //href: "/" + desc.userAlias + '/' + desc.worldName,
  799. onclick: function (e) {
  800. self.checkForManualSettings();
  801. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName
  802. //self.refresh();
  803. }
  804. });
  805. }
  806. let protoID = {}
  807. if (desc.type == 'saveState') {
  808. cardInfo.title = desc.worldName.split('/')[2];
  809. let protoIDComp = {
  810. $type: 'div',
  811. $components: [
  812. {
  813. $type: "span",
  814. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  815. $text: 'proto: '
  816. },
  817. {
  818. $type: "input",
  819. type: "text",
  820. disabled: "",
  821. style: "font-size:18px",
  822. value: desc.worldName.split('/')[0]
  823. }
  824. ]
  825. }
  826. Object.assign(protoID, protoIDComp)
  827. }
  828. if (desc.type == 'proto') {
  829. cardInfo.title = desc.worldName;
  830. // userGUI.push(
  831. // {
  832. // $type: "a",
  833. // class: "mdc-button mdc-button--compact mdc-card__action",
  834. // $text: "States",
  835. // onclick: async function (e) {
  836. // e.preventDefault();
  837. // window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName +'/about'
  838. // //console.log('clone');
  839. // // document.querySelector('#worldStatesGUI')._refresh({});
  840. // // let data = await _app.getSaveStates(desc.userAlias, desc.worldName);
  841. // // document.querySelector('#worldStatesGUI')._refresh(data);
  842. // }
  843. // }
  844. // )
  845. }
  846. online.push(onlineGUI);
  847. if (!desc.info) {
  848. desc.info = {
  849. imgUrl: "/defaults/worlds/webrtc/webimg.jpg",
  850. text: "..no text",
  851. title: "..no title"
  852. }
  853. }
  854. return {
  855. $type: "div",
  856. class: "mdc-card world-card",
  857. $components: [
  858. {
  859. $type: "section",
  860. class: "mdc-card__media world-card__16-9-media",
  861. $init: function () {
  862. if (desc.info.imgUrl !== "") {
  863. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc.info.imgUrl + ')';
  864. }
  865. }
  866. },
  867. {
  868. $type: "section",
  869. class: "mdc-card__primary",
  870. $components: [
  871. {
  872. $type: "h1",
  873. class: "mdc-card__title mdc-card__title--large",
  874. $text: desc.info.title
  875. },
  876. {
  877. $type: "h2",
  878. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  879. $text: desc.info.text
  880. },
  881. {
  882. $type: "span",
  883. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  884. $text: 'id: '
  885. },
  886. {
  887. $type: "input",
  888. type: "text",
  889. disabled: "",
  890. style: "font-size:18px",
  891. value: cardInfo.title
  892. },
  893. {
  894. $type: "p",
  895. },
  896. protoID,
  897. {
  898. $type: "p",
  899. },
  900. {
  901. $type: "span",
  902. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  903. $text: desc.created ? 'created: ' + (new Date(desc.created)).toUTCString() : ""
  904. },
  905. {
  906. $type: "p",
  907. }
  908. // ,{
  909. // $type: "span",
  910. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  911. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  912. // }
  913. ]
  914. },
  915. {
  916. $type: "section",
  917. class: "mdc-card__actions",
  918. $components: [
  919. ].concat(userGUI)
  920. },
  921. {
  922. $type: "section",
  923. class: "mdc-card__actions",
  924. $components: [
  925. {
  926. $type: 'div',
  927. $text: _l.t("online now") + ': '
  928. }
  929. ].concat(online)
  930. }
  931. ]
  932. }
  933. }
  934. }
  935. }
  936. createWorldsGUI(worldType, userAlias, userPub, worldName) {
  937. let self = this;
  938. let db = _LCSDB.user(userPub);
  939. var headerText = 'Worlds';
  940. if (worldType == 'state' && !worldName) {
  941. headerText = 'All World States for ' + userAlias;
  942. } else {
  943. headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  944. }
  945. let id = worldName ? worldName + '_' + userAlias : "allWorlds_" + userAlias
  946. //let headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  947. let worldCards = {
  948. $cell: true,
  949. id: id,
  950. $type: "div",
  951. $components: [],
  952. _cards: [],
  953. // _states: {},
  954. // _refresh: function (data) {
  955. // this._states = data;
  956. // },
  957. $init: function () {
  958. console.log('init lab...');
  959. if (worldType == 'proto') {
  960. db.get('worlds')
  961. .map()
  962. .on((res, k) => {
  963. console.log('From world: ', k);
  964. //let doc = document.querySelector('#'+ k);
  965. if (res) {
  966. let cardID = userAlias + '_' + k;
  967. let doc = this._cards.filter(el => el.$components[0].id == 'worldCard_' + cardID)[0];
  968. if (!doc) {
  969. doc = this._makeWorldCard(k, cardID);
  970. this._cards.push(doc);
  971. }
  972. }
  973. })
  974. } else if (worldType == 'state') {
  975. //get states
  976. if (!worldName) {
  977. console.log('get states');
  978. db.get('documents')
  979. .map()
  980. .on((res, k) => {
  981. if (k !== 'id') {
  982. console.log('From world: ', k);
  983. let worldStatesInfo = Object.entries(res).filter(el => el[0].includes('_info_vwf_json'));
  984. worldStatesInfo.map(el => {
  985. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  986. let stateEntry = 'savestate_/' + k + '/' + saveName + '_vwf_json';
  987. if (res[stateEntry]) {
  988. let cardID = userAlias + '_' + saveName + '_' + k;
  989. console.log(cardID, ' - ', el);
  990. let doc = this._cards.filter(el => el.$components[0].id == 'worldCard_' + cardID)[0];
  991. if (!doc) {
  992. doc = this._makeWorldCard({ protoName: k, stateName: saveName }, cardID);
  993. this._cards.push(doc);
  994. }
  995. }
  996. })
  997. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  998. }
  999. })
  1000. } else {
  1001. console.log('get states for ' + worldName);
  1002. db.get('documents')
  1003. .map((res, k) => { if (k == worldName) return res })
  1004. .on((res, k) => {
  1005. if (k !== 'id') {
  1006. console.log('From world: ', k);
  1007. let worldStatesInfo = Object.entries(res).filter(el => el[0].includes('_info_vwf_json'));
  1008. worldStatesInfo.map(el => {
  1009. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  1010. let stateEntry = 'savestate_/' + k + '/' + saveName + '_vwf_json';
  1011. if (res[stateEntry]) {
  1012. let cardID = userAlias + '_' + saveName + '_' + k;
  1013. console.log(cardID, ' - ', el);
  1014. let doc = this._cards.filter(el => el.$components[0].id == 'worldCard_' + cardID)[0];
  1015. if (!doc) {
  1016. doc = this._makeWorldCard({ protoName: k, stateName: saveName }, cardID);
  1017. this._cards.push(doc);
  1018. }
  1019. }
  1020. })
  1021. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  1022. }
  1023. })
  1024. }
  1025. }
  1026. //this._refresh();
  1027. },
  1028. _makeWorldCard: function (worldID, cardID) {
  1029. //let cardID = userAlias + '_' + worldID//data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  1030. let card = self.createWorldCard(worldType, userAlias, userPub, worldID, cardID, 'min');
  1031. return {
  1032. $cell: true,
  1033. $type: "div",
  1034. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  1035. $components: [card]
  1036. }
  1037. },
  1038. $update: function () {
  1039. // let cards = Object.entries(this._states)
  1040. // .filter(el => Object.keys(el[1]).length !== 0)
  1041. // .sort(function (el1, el2) {
  1042. // return parseInt(el2[1].created) - parseInt(el1[1].created)
  1043. // })
  1044. // .map(this._makeWorldCard);
  1045. this.$components = [
  1046. {
  1047. $type: "div",
  1048. class: "mdc-layout-grid",
  1049. $components: [
  1050. // {
  1051. // $type: "div",
  1052. // class: "mdc-layout-grid__inner",
  1053. // $components: [
  1054. // {
  1055. // $type: "div",
  1056. // class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  1057. // $components: [
  1058. // {
  1059. // $type: "H3",
  1060. // $text: headerText
  1061. // }
  1062. // ]
  1063. // }
  1064. // ]
  1065. // },
  1066. {
  1067. $type: "div",
  1068. class: "mdc-layout-grid__inner",
  1069. $components: this._cards
  1070. }
  1071. ]
  1072. }
  1073. ]
  1074. }
  1075. }
  1076. return worldCards
  1077. }
  1078. checkForManualSettings() {
  1079. console.log("check for manual settings");
  1080. let manualSettings = localStorage.getItem('lcs_app_manual_settings');
  1081. if (manualSettings) {
  1082. localStorage.removeItem('lcs_app_manual_settings');
  1083. }
  1084. let el = document.querySelector('#runWorldGUI');
  1085. if (el) {
  1086. if (el._arSwitch.checked) {
  1087. let arSettings = {
  1088. model: {
  1089. 'vwf/model/aframe': null
  1090. },
  1091. view: {
  1092. 'vwf/view/aframe': null,
  1093. 'vwf/view/editor-new': null
  1094. }
  1095. }
  1096. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1097. }
  1098. if (el._turnArOnSwitch.checked) {
  1099. let arSettings = {
  1100. model: {
  1101. 'vwf/model/aframe': null
  1102. },
  1103. view: {
  1104. 'vwf/view/aframe': null,
  1105. 'vwf/view/aframe-ar-driver': null
  1106. }
  1107. }
  1108. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1109. }
  1110. }
  1111. }
  1112. }
  1113. export { IndexApp as default }
  1114. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};