index-app.js 50 KB

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