index-app.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. if(res.file){
  594. let doc = {
  595. 'worldName': this._worldName,
  596. 'created': undefined,
  597. 'modified': undefined,
  598. 'type': 'proto',
  599. 'userAlias': userAlias,
  600. 'info': {title:'Need to repair!'}
  601. }
  602. this._refresh(doc);
  603. return
  604. }
  605. console.log(res);
  606. let worldDesc = JSON.parse(res);
  607. let root = Object.keys(worldDesc)[0];
  608. var appInfo = worldDesc[root]['en'];
  609. let langID = localStorage.getItem('krestianstvo_locale');
  610. if (langID) {
  611. appInfo = worldDesc[root][langID]
  612. }
  613. let doc = {
  614. 'worldName': this._worldName,
  615. 'created': undefined,
  616. 'modified': undefined,
  617. 'type': 'proto',
  618. 'userAlias': userAlias,
  619. 'info': appInfo
  620. }
  621. this._refresh(doc);
  622. //callback
  623. if(cb)
  624. cb(doc);
  625. }
  626. })
  627. } else if (worldType == 'state'){
  628. let pathName = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_info_vwf_json';
  629. db.get('documents').get(this._worldName.protoName).path(pathName).on((res)=>{
  630. if(res){
  631. if(res.file){
  632. let doc = {
  633. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  634. 'created': undefined,
  635. 'modified': undefined,
  636. 'type': 'state',
  637. 'userAlias': userAlias,
  638. 'info': {title:'Need to repair!'}
  639. }
  640. this._refresh(doc);
  641. return
  642. }
  643. console.log(res);
  644. let worldDesc = JSON.parse(res);
  645. let root = Object.keys(worldDesc)[0];
  646. var appInfo = worldDesc[root]['en'];
  647. let langID = localStorage.getItem('krestianstvo_locale');
  648. if (langID) {
  649. appInfo = worldDesc[root][langID]
  650. }
  651. let doc = {
  652. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  653. 'created': undefined,
  654. 'modified': undefined,
  655. 'type': 'saveState',
  656. 'userAlias': userAlias,
  657. 'info': appInfo
  658. }
  659. this._refresh(doc);
  660. //callback
  661. if(cb)
  662. cb(doc);
  663. }
  664. })
  665. }
  666. } else if (type == 'full'){
  667. if(worldType == 'proto') {
  668. db.get('worlds').get(this._worldName).on((res)=>{
  669. if(res){
  670. if(res["info_json"]['#']){
  671. let doc = {
  672. 'worldName': this._worldName,
  673. 'created': undefined,
  674. 'modified': undefined,
  675. 'type': 'proto',
  676. 'userAlias': userAlias,
  677. 'info': {title:'Need to repair!'}
  678. }
  679. this._refresh(doc);
  680. // if(cb)
  681. // cb(doc);
  682. return
  683. }
  684. console.log(res);
  685. let worldDesc = JSON.parse(res['info_json']);
  686. let root = Object.keys(worldDesc)[0];
  687. var appInfo = worldDesc[root]['en'];
  688. let langID = localStorage.getItem('krestianstvo_locale');
  689. if (langID) {
  690. appInfo = worldDesc[root][langID]
  691. }
  692. let settings = worldDesc[root]['settings'];
  693. let doc = {
  694. 'worldName': this._worldName,
  695. 'created': res.created ? res.created : "",
  696. 'modified': res.modified ? res.modified : "",
  697. 'proxy': res.proxy,
  698. 'type': 'proto',
  699. 'userAlias': userAlias,
  700. 'info': appInfo,
  701. 'settings': settings
  702. }
  703. this._refresh(doc);
  704. //callback
  705. if(cb)
  706. cb(doc);
  707. } else {
  708. //no world
  709. this._refresh({})
  710. }
  711. })
  712. } else if (worldType == 'state') {
  713. let pathNameInfo = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_info_vwf_json';
  714. db.get('documents').get(this._worldName.protoName).path(pathNameInfo).on((res)=>{
  715. if(res){
  716. if(res.file){
  717. let doc = {
  718. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  719. 'created': undefined,
  720. 'modified': undefined,
  721. 'type': 'state',
  722. 'userAlias': userAlias,
  723. 'info': {title:'Need to repair!'}
  724. }
  725. this._refresh(doc);
  726. return
  727. }
  728. console.log(res);
  729. let worldDesc = JSON.parse(res);
  730. let root = Object.keys(worldDesc)[0];
  731. var appInfo = worldDesc[root]['en'];
  732. let langID = localStorage.getItem('krestianstvo_locale');
  733. if (langID) {
  734. appInfo = worldDesc[root][langID]
  735. }
  736. let settings = worldDesc[root]['settings'];
  737. let doc = {
  738. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  739. 'created': undefined,
  740. 'modified': undefined,
  741. 'type': 'saveState',
  742. 'userAlias': userAlias,
  743. 'info': appInfo,
  744. 'settings': settings
  745. }
  746. this._refresh(doc);
  747. //callback
  748. if(cb)
  749. cb(doc);
  750. } else {
  751. //no world
  752. this._refresh({})
  753. }
  754. })
  755. // let pathNameState = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_vwf_json';
  756. // db.get('documents').get(this._worldName.protoName).path(pathNameState).on((res)=>{
  757. // console.log(res);
  758. // // let worldDesc = JSON.parse(res);
  759. // // let root = Object.keys(worldDesc)[0];
  760. // // var appInfo = worldDesc[root]['en'];
  761. // // let langID = localStorage.getItem('krestianstvo_locale');
  762. // // if (langID) {
  763. // // appInfo = worldDesc[root][langID]
  764. // // }
  765. // let settings = worldDesc[root]['settings'];
  766. // let doc = {
  767. // 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  768. // 'created': res.created,
  769. // 'modified': res.modified,
  770. // 'type': 'saveState',
  771. // 'userAlias': userAlias,
  772. // 'info': appInfo,
  773. // 'settings': settings
  774. // }
  775. // this._refresh(doc);
  776. // //callback
  777. // if(cb)
  778. // cb(doc);
  779. // })
  780. }
  781. }
  782. },
  783. $update: function () {
  784. //this.$components = [this._updateCard()]
  785. },
  786. _updateComps: function () {
  787. //console.log(this._worldInfo);
  788. },
  789. _updateCard: function () {
  790. let desc = this._worldInfo;
  791. if (!desc || Object.keys(desc).length == 0) {
  792. return {
  793. $type: "h1",
  794. class: "mdc-typography--headline4",
  795. $text: "ERROR: NO WORLD!"
  796. }
  797. }
  798. let userGUI = [];
  799. let online = [];
  800. let cardInfo = {
  801. "title": ""
  802. };
  803. if (type == "full") {
  804. } else {
  805. userGUI.push({
  806. $type: "a",
  807. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  808. $text: "Details",
  809. onclick: function (e) {
  810. e.preventDefault();
  811. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName + '/about'
  812. }
  813. });
  814. }
  815. if(desc.info.title !== 'Need to repair!'){
  816. userGUI.push({
  817. $type: "a",
  818. class: "mdc-button mdc-button--raised mdc-card__action ",
  819. $text: _LangManager.language.t('start'),//"Start new",
  820. //target: "_blank",
  821. //href: "/" + desc.userAlias + '/' + desc.worldName,
  822. onclick: function (e) {
  823. self.checkForManualSettings();
  824. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName
  825. //self.refresh();
  826. }
  827. });
  828. }
  829. let protoID = {}
  830. if (desc.type == 'saveState') {
  831. cardInfo.title = desc.worldName.split('/')[2];
  832. let protoIDComp = {
  833. $type: 'div',
  834. $components: [
  835. {
  836. $type: "span",
  837. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  838. $text: 'proto: '
  839. },
  840. {
  841. $type: "input",
  842. type: "text",
  843. disabled: "",
  844. style: "font-size:18px",
  845. value: desc.worldName.split('/')[0]
  846. }
  847. ]
  848. }
  849. Object.assign(protoID, protoIDComp)
  850. }
  851. if (desc.type == 'proto') {
  852. cardInfo.title = desc.worldName;
  853. // userGUI.push(
  854. // {
  855. // $type: "a",
  856. // class: "mdc-button mdc-button--compact mdc-card__action",
  857. // $text: "States",
  858. // onclick: async function (e) {
  859. // e.preventDefault();
  860. // window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName +'/about'
  861. // //console.log('clone');
  862. // // document.querySelector('#worldStatesGUI')._refresh({});
  863. // // let data = await _app.getSaveStates(desc.userAlias, desc.worldName);
  864. // // document.querySelector('#worldStatesGUI')._refresh(data);
  865. // }
  866. // }
  867. // )
  868. }
  869. online.push(onlineGUI);
  870. if(!desc.info){
  871. desc.info = {
  872. imgUrl: "/defaults/worlds/webrtc/webimg.jpg",
  873. text: "..no text",
  874. title: "..no title"
  875. }
  876. }
  877. return {
  878. $type: "div",
  879. class: "mdc-card world-card",
  880. $components: [
  881. {
  882. $type: "section",
  883. class: "mdc-card__media world-card__16-9-media",
  884. $init: function () {
  885. if (desc.info.imgUrl !== "") {
  886. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc.info.imgUrl + ')';
  887. }
  888. }
  889. },
  890. {
  891. $type: "section",
  892. class: "mdc-card__primary",
  893. $components: [
  894. {
  895. $type: "h1",
  896. class: "mdc-card__title mdc-card__title--large",
  897. $text: desc.info.title
  898. },
  899. {
  900. $type: "h2",
  901. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  902. $text: desc.info.text
  903. },
  904. {
  905. $type: "span",
  906. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  907. $text: 'id: '
  908. },
  909. {
  910. $type: "input",
  911. type: "text",
  912. disabled: "",
  913. style: "font-size:18px",
  914. value: cardInfo.title
  915. },
  916. {
  917. $type: "p",
  918. },
  919. protoID,
  920. {
  921. $type: "p",
  922. },
  923. {
  924. $type: "span",
  925. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  926. $text: desc.created ? 'created: ' + (new Date(desc.created)).toUTCString() : ""
  927. },
  928. {
  929. $type: "p",
  930. }
  931. // ,{
  932. // $type: "span",
  933. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  934. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  935. // }
  936. ]
  937. },
  938. {
  939. $type: "section",
  940. class: "mdc-card__actions",
  941. $components: [
  942. ].concat(userGUI)
  943. },
  944. {
  945. $type: "section",
  946. class: "mdc-card__actions",
  947. $components: [
  948. {
  949. $type: 'div',
  950. $text: 'online now: '
  951. }
  952. ].concat(online)
  953. }
  954. ]
  955. }
  956. }
  957. }
  958. }
  959. createWorldsGUI(worldType, userAlias, userPub, worldName) {
  960. let self = this;
  961. let db = _LCSDB.user(userPub);
  962. var headerText = 'Worlds';
  963. if(worldType == 'state' && !worldName){
  964. headerText = 'All World States for ' + userAlias;
  965. } else {
  966. headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  967. }
  968. let id = worldName ? worldName + '_' + userAlias : "allWorlds_" + userAlias
  969. //let headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  970. let worldCards = {
  971. $cell: true,
  972. id: id,
  973. $type: "div",
  974. $components: [],
  975. _cards: [],
  976. // _states: {},
  977. // _refresh: function (data) {
  978. // this._states = data;
  979. // },
  980. $init: function () {
  981. console.log('init lab...');
  982. if(worldType == 'proto'){
  983. db.get('worlds')
  984. .map()
  985. .on((res,k)=>{
  986. console.log('From world: ', k);
  987. //let doc = document.querySelector('#'+ k);
  988. if(res){
  989. let cardID = userAlias + '_' + k;
  990. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  991. if(!doc) {
  992. doc = this._makeWorldCard(k, cardID);
  993. this._cards.push(doc);
  994. }
  995. }
  996. })
  997. } else if(worldType == 'state') {
  998. //get states
  999. if(!worldName) {
  1000. console.log('get states');
  1001. db.get('documents')
  1002. .map()
  1003. .on((res,k)=>{
  1004. if( k !== 'id'){
  1005. console.log('From world: ', k);
  1006. let worldStatesInfo = Object.entries(res).filter(el=>el[0].includes('_info_vwf_json'));
  1007. worldStatesInfo.map(el=>{
  1008. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  1009. let stateEntry = 'savestate_/' + k + '/' + saveName + '_vwf_json';
  1010. if (res[stateEntry]) {
  1011. let cardID = userAlias + '_' + saveName + '_' + k;
  1012. console.log(cardID, ' - ', el);
  1013. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  1014. if(!doc) {
  1015. doc = this._makeWorldCard({protoName: k, stateName: saveName}, cardID);
  1016. this._cards.push(doc);
  1017. }
  1018. }
  1019. })
  1020. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  1021. }
  1022. //let doc = document.querySelector('#'+ k);
  1023. // let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ userAlias + '_' + k)[0];
  1024. // if(!doc) {
  1025. // doc = this._makeWorldCard(k);
  1026. // this._cards.push(doc);
  1027. // }
  1028. })
  1029. } else {
  1030. console.log('get states for ' + worldName);
  1031. db.get('documents')
  1032. .map((res, k) => {if (k == worldName) return res})
  1033. .on((res,k)=>{
  1034. if( k !== 'id'){
  1035. console.log('From world: ', k);
  1036. let worldStatesInfo = Object.entries(res).filter(el=>el[0].includes('_info_vwf_json'));
  1037. worldStatesInfo.map(el=>{
  1038. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  1039. let stateEntry = 'savestate_/' + k + '/' + saveName + '_vwf_json';
  1040. if (res[stateEntry]) {
  1041. let cardID = userAlias + '_' + saveName + '_' + k;
  1042. console.log(cardID, ' - ', el);
  1043. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  1044. if(!doc) {
  1045. doc = this._makeWorldCard({protoName: k, stateName: saveName}, cardID);
  1046. this._cards.push(doc);
  1047. }
  1048. }
  1049. })
  1050. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  1051. }
  1052. //let doc = document.querySelector('#'+ k);
  1053. // let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ userAlias + '_' + k)[0];
  1054. // if(!doc) {
  1055. // doc = this._makeWorldCard(k);
  1056. // this._cards.push(doc);
  1057. // }
  1058. })
  1059. }
  1060. }
  1061. //this._refresh();
  1062. },
  1063. _makeWorldCard: function (worldID, cardID) {
  1064. //let cardID = userAlias + '_' + worldID//data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  1065. let card = self.createWorldCard(worldType, userAlias, userPub, worldID, cardID, 'min');
  1066. //card._refresh(data[1]);
  1067. //card._worldInfo = data[1];
  1068. //card._updateComps();
  1069. return {
  1070. $cell: true,
  1071. $type: "div",
  1072. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  1073. $components: [card]
  1074. }
  1075. },
  1076. $update: function () {
  1077. // let cards = Object.entries(this._states)
  1078. // .filter(el => Object.keys(el[1]).length !== 0)
  1079. // .sort(function (el1, el2) {
  1080. // return parseInt(el2[1].created) - parseInt(el1[1].created)
  1081. // })
  1082. // .map(this._makeWorldCard);
  1083. this.$components = [
  1084. {
  1085. $type: "div",
  1086. class: "mdc-layout-grid",
  1087. $components: [
  1088. {
  1089. $type: "div",
  1090. class: "mdc-layout-grid__inner",
  1091. $components: [
  1092. {
  1093. $type: "div",
  1094. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  1095. $components: [
  1096. {
  1097. $type: "H3",
  1098. $text: headerText
  1099. }
  1100. ]
  1101. }
  1102. ]
  1103. },
  1104. {
  1105. $type: "div",
  1106. class: "mdc-layout-grid__inner",
  1107. $components: this._cards
  1108. }
  1109. ]
  1110. }
  1111. ]
  1112. }
  1113. }
  1114. return worldCards
  1115. }
  1116. checkForManualSettings() {
  1117. console.log("check for manual settings");
  1118. let manualSettings = localStorage.getItem('lcs_app_manual_settings');
  1119. if (manualSettings) {
  1120. localStorage.removeItem('lcs_app_manual_settings');
  1121. }
  1122. let el = document.querySelector('#runWorldGUI');
  1123. if (el) {
  1124. if (el._arSwitch.checked) {
  1125. let arSettings = {
  1126. model: {
  1127. 'vwf/model/aframe': null
  1128. },
  1129. view: {
  1130. 'vwf/view/aframe': null,
  1131. 'vwf/view/editor-new': null
  1132. }
  1133. }
  1134. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1135. }
  1136. if (el._turnArOnSwitch.checked) {
  1137. let arSettings = {
  1138. model: {
  1139. 'vwf/model/aframe': null
  1140. },
  1141. view: {
  1142. 'vwf/view/aframe': null,
  1143. 'vwf/view/aframe-ar-driver': null
  1144. }
  1145. }
  1146. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1147. }
  1148. }
  1149. }
  1150. }
  1151. export { IndexApp as default }
  1152. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};