index-app.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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', 'main', 'worldsGUI', 'lab'];
  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, self.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. self.authGUI();
  339. }
  340. console.log(_LCSDB.user().is);
  341. });
  342. }
  343. }
  344. getLookWorlds(){
  345. let self = this;
  346. let lookWorlds =
  347. {
  348. $type: "div",
  349. id: "lookWorlds",
  350. class: "mdc-layout-grid mdc-layout-grid--align-left",
  351. _status: '',
  352. $init: function () {
  353. this._status = "init";
  354. },
  355. $update: function () {
  356. //change for LiveCoding.space site 'app'
  357. let defaultName = '';
  358. let guiForAll = [
  359. {
  360. $type: "div",
  361. style: "margin-top: 20px;",
  362. _userName: null,
  363. _userNameField: null,
  364. $components:
  365. [
  366. _app.widgets.inputTextFieldOutlined({
  367. "id": 'worldsUserName',
  368. "label": 'Enter user name',
  369. "value": defaultName,
  370. "type": "text",
  371. "init": function () {
  372. this._userNameField = new mdc.textField.MDCTextField(this);
  373. }
  374. }),
  375. _app.widgets.p,
  376. // {
  377. // $type: "a",
  378. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  379. // $text: 'World Protos', //self.language.t('set proxy'),//"clone",
  380. // onclick: function (e) {
  381. // //console.log('clone');
  382. // let searchName = this._userNameField.value;
  383. // self.initWorldsProtosListForUser(searchName);
  384. // }
  385. // },
  386. // {
  387. // $type: "a",
  388. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  389. // $text: 'World States', //self.language.t('set proxy'),//"clone",
  390. // onclick: function (e) {
  391. // //console.log('clone');
  392. // let searchName = this._userNameField.value;
  393. // self.initWorldsStatesListForUser(searchName);
  394. // }
  395. // }
  396. _app.widgets.buttonRaised(
  397. {
  398. "label": 'World Protos',
  399. "onclick": function (e) {
  400. e.preventDefault();
  401. //page("/app/worlds/protos")
  402. let searchName = this._userNameField.value;
  403. if(searchName !== "")
  404. window.location.pathname = "/"+searchName+"/worlds/protos"
  405. //_app.indexApp.getAppDetailsFromDefaultDB('protos');
  406. }
  407. }),
  408. _app.widgets.space,
  409. _app.widgets.buttonRaised(
  410. {
  411. "label": 'World States',
  412. "onclick": function (e) {
  413. e.preventDefault();
  414. //page("/app/worlds/states")
  415. let searchName = this._userNameField.value;
  416. if(searchName !== "")
  417. window.location.pathname = "/"+searchName+"/worlds/states"
  418. //_app.indexApp.getAppDetailsFromDefaultDB('states');
  419. }
  420. })
  421. ]
  422. },
  423. // window._app.widgets.buttonStroked(
  424. // {
  425. // "label": 'Default World Protos',
  426. // "onclick": function (e) {
  427. // e.preventDefault();
  428. // //page("/app/worlds/protos")
  429. // window.location.pathname = "/app/worlds/protos"
  430. // //_app.indexApp.getAppDetailsFromDefaultDB('protos');
  431. // }
  432. // }),
  433. // window._app.widgets.buttonStroked(
  434. // {
  435. // "label": 'Default World States',
  436. // "onclick": function (e) {
  437. // e.preventDefault();
  438. // //page("/app/worlds/states")
  439. // window.location.pathname = "/app/worlds/states"
  440. // //_app.indexApp.getAppDetailsFromDefaultDB('states');
  441. // }
  442. // })
  443. ];
  444. this.$components = [
  445. {
  446. $type: "h1",
  447. class: "mdc-typography--headline4",
  448. $text: "Looking for Worlds made by other Users!"
  449. }
  450. ].concat(guiForAll, _app.widgets.p)
  451. }
  452. }
  453. return lookWorlds
  454. }
  455. getLoginGUI(){
  456. let self = this;
  457. let loginGUI =
  458. {
  459. $type: "div",
  460. id: "loginGUI",
  461. //style:"background-color: #efefef",
  462. class: "mdc-layout-grid mdc-layout-grid--align-left",
  463. _alias: null,
  464. _pass: null,
  465. _passField: null,
  466. _aliasField: null,
  467. _initData: function () {
  468. this._alias = '';
  469. this._pass = '';
  470. // if (window.sessionStorage.getItem('alias')) {
  471. // this._alias = window.sessionStorage.getItem('alias')
  472. // }
  473. // if (window.sessionStorage.getItem('tmp')) {
  474. // this._pass = window.sessionStorage.getItem('tmp')
  475. // }
  476. },
  477. $init: function () {
  478. this._initData();
  479. },
  480. $update: function () {
  481. this.$components = [
  482. {
  483. $type: "div",
  484. class: "mdc-layout-grid__inner",
  485. $components: [
  486. {
  487. $type: "div",
  488. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  489. $components: [
  490. {
  491. $type: "span",
  492. class: "mdc-typography--headline5",
  493. $text: "Login: "
  494. },
  495. window._app.widgets.inputTextFieldOutlined({
  496. "id": 'aliasInput',
  497. "label": "Login",
  498. "value": this._alias,
  499. "type": "text",
  500. "init": function () {
  501. this._aliasField = new mdc.textField.MDCTextField(this);
  502. }
  503. }),
  504. ]
  505. },
  506. {
  507. $type: "div",
  508. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  509. $components: [
  510. {
  511. $type: "span",
  512. class: "mdc-typography--headline5",
  513. $text: "Password: "
  514. },
  515. window._app.widgets.inputTextFieldOutlined({
  516. "id": 'passwordInput',
  517. "label": "Password",
  518. "value": this._pass,
  519. "type": "password",
  520. "init": function () {
  521. this._passField = new mdc.textField.MDCTextField(this);
  522. }
  523. }),
  524. ]
  525. },
  526. {
  527. $type: "div",
  528. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  529. $components: [
  530. window._app.widgets.buttonRaised(
  531. {
  532. "label": 'Sign UP',
  533. "onclick": function (e) {
  534. e.preventDefault();
  535. let alias = this._aliasField.value;
  536. let pass = this._passField.value
  537. if (pass.length < 7) {
  538. new Noty({
  539. text: "Your passphrase needs to be longer than 7 letters",
  540. timeout: 2000,
  541. theme: 'mint',
  542. layout: 'bottomRight',
  543. type: 'error'
  544. }).show();
  545. } else {
  546. //
  547. _LCSDB.user().create(alias, pass,
  548. function (ack) {
  549. if (!ack.wait) { }
  550. if (ack.err) {
  551. console.log(ack.err)
  552. return ack.err
  553. };
  554. if (ack.pub) {
  555. let userObj = {
  556. 'alias': alias,
  557. 'pub': ack.pub
  558. };
  559. _LCSDB.get('users').get(alias).put(userObj);
  560. }
  561. _LCSDB.user().auth(alias, pass);
  562. });
  563. }
  564. }
  565. }),
  566. _app.widgets.space,
  567. window._app.widgets.buttonRaised(
  568. {
  569. "label": 'Sign IN',
  570. "onclick": function (e) {
  571. e.preventDefault();
  572. let alias = this._aliasField.value;
  573. let pass = this._passField.value
  574. _app.helpers.authUser(alias, pass);
  575. // _LCSDB.user().auth.call(_LCSDB, alias, pass
  576. // // , function(ack) {
  577. // // if (ack.err) {
  578. // // new Noty({
  579. // // text: ack.err,
  580. // // timeout: 2000,
  581. // // theme: 'mint',
  582. // // layout: 'bottomRight',
  583. // // type: 'error'
  584. // // }).show();
  585. // // }
  586. // //}
  587. // );
  588. }
  589. })
  590. ]
  591. }
  592. ]
  593. }
  594. ]
  595. }
  596. }
  597. return loginGUI
  598. }
  599. refresh() {
  600. // socket.emit('getWebAppUpdate', "");
  601. }
  602. parseOnlineData(data) {
  603. let parcedData = _app.parseAppInstancesData(data);
  604. //if (Object.entries(parcedData).length !== 0)
  605. let onlineGUIs = document.querySelectorAll('.online');
  606. onlineGUIs.forEach(function (item) {
  607. item._refresh(parcedData)
  608. });
  609. }
  610. createWorldCard(worldType, userAlias, userPub, worldName, id, type, cb) {
  611. let self = this;
  612. let db = _LCSDB.user(userPub);
  613. let onlineGUI = {
  614. $cell: true,
  615. id: "onlineGUI_" + id,
  616. class: "online",
  617. $type: "div",
  618. _instances: {},
  619. _worldListItem: function (m) {
  620. return {
  621. $type: "li",
  622. class: "mdc-list-item",
  623. $components: [
  624. {
  625. $type: "span",
  626. class: "world-link mdc-list-item__text",
  627. $components: [
  628. {
  629. $type: "span",
  630. class: "mdc-list-item__primary-text",
  631. $components: [
  632. {
  633. $type: "a",
  634. $text: m[0],
  635. //target: "_blank",
  636. // href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  637. onclick: function (e) {
  638. self.checkForManualSettings();
  639. window.location.pathname = "/" + m[1].user + m[0];
  640. //self.refresh();
  641. }
  642. },
  643. ]
  644. },
  645. {
  646. $type: "span",
  647. class: "mdc-list-item__secondary-text",
  648. $text: _app.isLuminary ? _LangManager.language.t('users') + Object.keys(m[1].clients).length : _LangManager.language.t('users') + m[1].clients
  649. }
  650. ]
  651. }
  652. ]
  653. }
  654. },
  655. $components: [],
  656. _refresh: function (data) {
  657. if (data) {
  658. if (Object.entries(data).length !== 0) {
  659. if (this._worldInfo) {
  660. let insts = Object.entries(data).filter(el => el[0] == this._worldInfo.worldName);
  661. if (insts.length !== 0)
  662. this._instances = insts[0][1];
  663. }
  664. } else {
  665. this._instances = {}
  666. }
  667. }
  668. },
  669. $init: function () {
  670. if (_app.isLuminary) {
  671. let luminaryPath = _app.luminaryPath;
  672. let ref = _LCSDB.get(luminaryPath);
  673. setInterval(function () {
  674. ref.get('allclients').once().map().once(res => {
  675. if (res) {
  676. if (res.id) {
  677. let clientTime = Gun.state.is(res, 'live');
  678. let now = Gun.time.is();
  679. if (now - clientTime < 10000) {
  680. let instance = res.user + res.instance;
  681. //let data = JSON.stringify({[res.instance]: {instance: instance, clients: {}, user: res.user, loadInfo: {}}});
  682. //console.log(data);
  683. if (!self.instances[res.instance]) {
  684. self.instances[res.instance] = { id: res.instance, instance: instance, clients: { [res.id]: res }, user: res.user, loadInfo: {} }
  685. } else {
  686. self.instances[res.instance].clients[res.id] = res
  687. }
  688. let data = JSON.stringify(self.instances);
  689. self.parseOnlineData(data);
  690. } else {
  691. if (self.instances[res.instance]) {
  692. delete self.instances[res.instance].clients[res.id];
  693. if (Object.keys(self.instances[res.instance].clients).length == 0) {
  694. delete self.instances[res.instance];
  695. self.parseOnlineData(JSON.stringify({}));
  696. }
  697. }
  698. //ref.get('instances').get(res.instance).get(res.id).put(null);
  699. }
  700. }
  701. }
  702. }
  703. )
  704. }, 5000);
  705. }
  706. this._refresh();
  707. },
  708. $update: function () {
  709. if (this._instances) {
  710. let cardListData = Object.entries(this._instances).filter(el => el[1].user == this._worldInfo.userAlias);
  711. this.$components = [
  712. {
  713. $type: "hr",
  714. class: "mdc-list-divider"
  715. }
  716. ].concat(cardListData.map(this._worldListItem))
  717. }
  718. }
  719. }
  720. return {
  721. $cell: true,
  722. id: 'worldCard_' + id,
  723. $type: "div",
  724. _worldName: "",
  725. _worldInfo: {},
  726. _refresh: function (data) {
  727. this._worldInfo = data;
  728. this.$components = [this._updateCard()]
  729. },
  730. $init: function () {
  731. this._worldName = worldName;
  732. if(type == 'min') {
  733. if(worldType == 'proto') {
  734. db.get('worlds').get(this._worldName).path('info_json').on((res)=>{
  735. console.log(res);
  736. let worldDesc = JSON.parse(res);
  737. let root = Object.keys(worldDesc)[0];
  738. var appInfo = worldDesc[root]['en'];
  739. let langID = localStorage.getItem('krestianstvo_locale');
  740. if (langID) {
  741. appInfo = worldDesc[root][langID]
  742. }
  743. let doc = {
  744. 'worldName': this._worldName,
  745. 'created': undefined,
  746. 'modified': undefined,
  747. 'type': 'proto',
  748. 'userAlias': userAlias,
  749. 'info': appInfo
  750. }
  751. this._refresh(doc);
  752. //callback
  753. if(cb)
  754. cb(doc);
  755. })
  756. } else if (worldType == 'state'){
  757. let pathName = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_info_vwf_json';
  758. db.get('documents').get(this._worldName.protoName).path(pathName).on((res)=>{
  759. console.log(res);
  760. let worldDesc = JSON.parse(res);
  761. let root = Object.keys(worldDesc)[0];
  762. var appInfo = worldDesc[root]['en'];
  763. let langID = localStorage.getItem('krestianstvo_locale');
  764. if (langID) {
  765. appInfo = worldDesc[root][langID]
  766. }
  767. let doc = {
  768. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  769. 'created': undefined,
  770. 'modified': undefined,
  771. 'type': 'saveState',
  772. 'userAlias': userAlias,
  773. 'info': appInfo
  774. }
  775. this._refresh(doc);
  776. //callback
  777. if(cb)
  778. cb(doc);
  779. })
  780. }
  781. } else if (type == 'full'){
  782. if(worldType == 'proto') {
  783. db.get('worlds').get(this._worldName).on((res)=>{
  784. console.log(res);
  785. let worldDesc = JSON.parse(res['info_json']);
  786. let root = Object.keys(worldDesc)[0];
  787. var appInfo = worldDesc[root]['en'];
  788. let langID = localStorage.getItem('krestianstvo_locale');
  789. if (langID) {
  790. appInfo = worldDesc[root][langID]
  791. }
  792. let settings = worldDesc[root]['settings'];
  793. let doc = {
  794. 'worldName': this._worldName,
  795. 'created': res.created ? res.created : "",
  796. 'modified': res.modified ? res.modified : "",
  797. 'proxy': res.proxy,
  798. 'type': 'proto',
  799. 'userAlias': userAlias,
  800. 'info': appInfo,
  801. 'settings': settings
  802. }
  803. this._refresh(doc);
  804. //callback
  805. if(cb)
  806. cb(doc);
  807. })
  808. } else if (worldType == 'state') {
  809. let pathNameInfo = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_info_vwf_json';
  810. db.get('documents').get(this._worldName.protoName).path(pathNameInfo).on((res)=>{
  811. console.log(res);
  812. let worldDesc = JSON.parse(res);
  813. let root = Object.keys(worldDesc)[0];
  814. var appInfo = worldDesc[root]['en'];
  815. let langID = localStorage.getItem('krestianstvo_locale');
  816. if (langID) {
  817. appInfo = worldDesc[root][langID]
  818. }
  819. let settings = worldDesc[root]['settings'];
  820. let doc = {
  821. 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  822. 'created': undefined,
  823. 'modified': undefined,
  824. 'type': 'saveState',
  825. 'userAlias': userAlias,
  826. 'info': appInfo,
  827. 'settings': settings
  828. }
  829. this._refresh(doc);
  830. //callback
  831. if(cb)
  832. cb(doc);
  833. })
  834. // let pathNameState = 'savestate_/' + this._worldName.protoName+ '/' + this._worldName.stateName + '_vwf_json';
  835. // db.get('documents').get(this._worldName.protoName).path(pathNameState).on((res)=>{
  836. // console.log(res);
  837. // // let worldDesc = JSON.parse(res);
  838. // // let root = Object.keys(worldDesc)[0];
  839. // // var appInfo = worldDesc[root]['en'];
  840. // // let langID = localStorage.getItem('krestianstvo_locale');
  841. // // if (langID) {
  842. // // appInfo = worldDesc[root][langID]
  843. // // }
  844. // let settings = worldDesc[root]['settings'];
  845. // let doc = {
  846. // 'worldName': this._worldName.protoName + '/load/' + this._worldName.stateName,
  847. // 'created': res.created,
  848. // 'modified': res.modified,
  849. // 'type': 'saveState',
  850. // 'userAlias': userAlias,
  851. // 'info': appInfo,
  852. // 'settings': settings
  853. // }
  854. // this._refresh(doc);
  855. // //callback
  856. // if(cb)
  857. // cb(doc);
  858. // })
  859. }
  860. }
  861. },
  862. $update: function () {
  863. //this.$components = [this._updateCard()]
  864. },
  865. _updateComps: function () {
  866. //console.log(this._worldInfo);
  867. },
  868. _updateCard: function () {
  869. let desc = this._worldInfo;
  870. if (!desc || Object.keys(desc).length == 0) {
  871. return {
  872. $type: "h1",
  873. class: "mdc-typography--headline4",
  874. $text: "ERROR: NO WORLD!"
  875. }
  876. }
  877. let userGUI = [];
  878. let online = [];
  879. let cardInfo = {
  880. "title": ""
  881. };
  882. if (type == "full") {
  883. } else {
  884. userGUI.push({
  885. $type: "a",
  886. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  887. $text: "Details",
  888. onclick: function (e) {
  889. e.preventDefault();
  890. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName + '/about'
  891. }
  892. });
  893. }
  894. userGUI.push({
  895. $type: "a",
  896. class: "mdc-button mdc-button--raised mdc-card__action ",
  897. $text: _LangManager.language.t('start'),//"Start new",
  898. //target: "_blank",
  899. //href: "/" + desc.userAlias + '/' + desc.worldName,
  900. onclick: function (e) {
  901. self.checkForManualSettings();
  902. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName
  903. //self.refresh();
  904. }
  905. });
  906. let protoID = {}
  907. if (desc.type == 'saveState') {
  908. cardInfo.title = desc.worldName.split('/')[2];
  909. let protoIDComp = {
  910. $type: 'div',
  911. $components: [
  912. {
  913. $type: "span",
  914. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  915. $text: 'proto: '
  916. },
  917. {
  918. $type: "input",
  919. type: "text",
  920. disabled: "",
  921. style: "font-size:18px",
  922. value: desc.worldName.split('/')[0]
  923. }
  924. ]
  925. }
  926. Object.assign(protoID, protoIDComp)
  927. }
  928. if (desc.type == 'proto') {
  929. cardInfo.title = desc.worldName;
  930. // userGUI.push(
  931. // {
  932. // $type: "a",
  933. // class: "mdc-button mdc-button--compact mdc-card__action",
  934. // $text: "States",
  935. // onclick: async function (e) {
  936. // e.preventDefault();
  937. // window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName +'/about'
  938. // //console.log('clone');
  939. // // document.querySelector('#worldStatesGUI')._refresh({});
  940. // // let data = await _app.getSaveStates(desc.userAlias, desc.worldName);
  941. // // document.querySelector('#worldStatesGUI')._refresh(data);
  942. // }
  943. // }
  944. // )
  945. }
  946. online.push(onlineGUI);
  947. if(!desc.info){
  948. desc.info = {
  949. imgUrl: "/defaults/worlds/webrtc/webimg.jpg",
  950. text: "..no text",
  951. title: "..no title"
  952. }
  953. }
  954. return {
  955. $type: "div",
  956. class: "mdc-card world-card",
  957. $components: [
  958. {
  959. $type: "section",
  960. class: "mdc-card__media world-card__16-9-media",
  961. $init: function () {
  962. if (desc.info.imgUrl !== "") {
  963. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc.info.imgUrl + ')';
  964. }
  965. }
  966. },
  967. {
  968. $type: "section",
  969. class: "mdc-card__primary",
  970. $components: [
  971. {
  972. $type: "h1",
  973. class: "mdc-card__title mdc-card__title--large",
  974. $text: desc.info.title
  975. },
  976. {
  977. $type: "h2",
  978. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  979. $text: desc.info.text
  980. },
  981. {
  982. $type: "span",
  983. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  984. $text: 'id: '
  985. },
  986. {
  987. $type: "input",
  988. type: "text",
  989. disabled: "",
  990. style: "font-size:18px",
  991. value: cardInfo.title
  992. },
  993. {
  994. $type: "p",
  995. },
  996. protoID,
  997. {
  998. $type: "p",
  999. },
  1000. {
  1001. $type: "span",
  1002. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  1003. $text: desc.created ? 'created: ' + (new Date(desc.created)).toUTCString() : ""
  1004. },
  1005. {
  1006. $type: "p",
  1007. }
  1008. // ,{
  1009. // $type: "span",
  1010. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  1011. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  1012. // }
  1013. ]
  1014. },
  1015. {
  1016. $type: "section",
  1017. class: "mdc-card__actions",
  1018. $components: [
  1019. ].concat(userGUI)
  1020. },
  1021. {
  1022. $type: "section",
  1023. class: "mdc-card__actions",
  1024. $components: [
  1025. {
  1026. $type: 'div',
  1027. $text: 'online now: '
  1028. }
  1029. ].concat(online)
  1030. }
  1031. ]
  1032. }
  1033. }
  1034. }
  1035. }
  1036. createWorldsGUI(worldType, userAlias, userPub, worldName) {
  1037. let self = this;
  1038. let db = _LCSDB.user(userPub);
  1039. var headerText = 'Worlds';
  1040. if(worldType == 'state' && !worldName){
  1041. headerText = 'All World States for ' + userAlias;
  1042. } else {
  1043. headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  1044. }
  1045. let id = worldName ? worldName + '_' + userAlias : "allWorlds_" + userAlias
  1046. //let headerText = worldName ? 'States for ' + worldName : 'All Worlds Protos'
  1047. let worldCards = {
  1048. $cell: true,
  1049. id: id,
  1050. $type: "div",
  1051. $components: [],
  1052. _cards: [],
  1053. // _states: {},
  1054. // _refresh: function (data) {
  1055. // this._states = data;
  1056. // },
  1057. $init: function () {
  1058. console.log('init lab...');
  1059. if(worldType == 'proto'){
  1060. db.get('worlds')
  1061. .map()
  1062. .on((res,k)=>{
  1063. console.log('From world: ', k);
  1064. //let doc = document.querySelector('#'+ k);
  1065. let cardID = userAlias + '_' + k;
  1066. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  1067. if(!doc) {
  1068. doc = this._makeWorldCard(k, cardID);
  1069. this._cards.push(doc);
  1070. }
  1071. })
  1072. } else if(worldType == 'state') {
  1073. //get states
  1074. if(!worldName) {
  1075. console.log('get states');
  1076. db.get('documents')
  1077. .map()
  1078. .on((res,k)=>{
  1079. if( k !== 'id'){
  1080. console.log('From world: ', k);
  1081. let worldStatesInfo = Object.entries(res).filter(el=>el[0].includes('_info_vwf_json'));
  1082. worldStatesInfo.map(el=>{
  1083. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  1084. let cardID = userAlias + '_' + saveName + '_' + k;
  1085. console.log(cardID, ' - ', el);
  1086. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  1087. if(!doc) {
  1088. doc = this._makeWorldCard({protoName: k, stateName: saveName}, cardID);
  1089. this._cards.push(doc);
  1090. }
  1091. })
  1092. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  1093. }
  1094. //let doc = document.querySelector('#'+ k);
  1095. // let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ userAlias + '_' + k)[0];
  1096. // if(!doc) {
  1097. // doc = this._makeWorldCard(k);
  1098. // this._cards.push(doc);
  1099. // }
  1100. })
  1101. } else {
  1102. console.log('get states for ' + worldName);
  1103. db.get('documents')
  1104. .map((res, k) => {if (k == worldName) return res})
  1105. .on((res,k)=>{
  1106. if( k !== 'id'){
  1107. console.log('From world: ', k);
  1108. let worldStatesInfo = Object.entries(res).filter(el=>el[0].includes('_info_vwf_json'));
  1109. worldStatesInfo.map(el=>{
  1110. let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
  1111. let cardID = userAlias + '_' + saveName + '_' + k;
  1112. console.log(cardID, ' - ', el);
  1113. let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ cardID)[0];
  1114. if(!doc) {
  1115. doc = this._makeWorldCard({protoName: k, stateName: saveName}, cardID);
  1116. this._cards.push(doc);
  1117. }
  1118. })
  1119. //let saveName = el.stateName.split('/')[2].replace('_info_vwf_json', "");
  1120. }
  1121. //let doc = document.querySelector('#'+ k);
  1122. // let doc = this._cards.filter(el=> el.$components[0].id == 'worldCard_'+ userAlias + '_' + k)[0];
  1123. // if(!doc) {
  1124. // doc = this._makeWorldCard(k);
  1125. // this._cards.push(doc);
  1126. // }
  1127. })
  1128. }
  1129. }
  1130. //this._refresh();
  1131. },
  1132. _makeWorldCard: function (worldID, cardID) {
  1133. //let cardID = userAlias + '_' + worldID//data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  1134. let card = self.createWorldCard(worldType, userAlias, userPub, worldID, cardID, 'min');
  1135. //card._refresh(data[1]);
  1136. //card._worldInfo = data[1];
  1137. //card._updateComps();
  1138. return {
  1139. $cell: true,
  1140. $type: "div",
  1141. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  1142. $components: [card]
  1143. }
  1144. },
  1145. $update: function () {
  1146. // let cards = Object.entries(this._states)
  1147. // .filter(el => Object.keys(el[1]).length !== 0)
  1148. // .sort(function (el1, el2) {
  1149. // return parseInt(el2[1].created) - parseInt(el1[1].created)
  1150. // })
  1151. // .map(this._makeWorldCard);
  1152. this.$components = [
  1153. {
  1154. $type: "div",
  1155. class: "mdc-layout-grid",
  1156. $components: [
  1157. {
  1158. $type: "div",
  1159. class: "mdc-layout-grid__inner",
  1160. $components: [
  1161. {
  1162. $type: "div",
  1163. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  1164. $components: [
  1165. {
  1166. $type: "H3",
  1167. $text: headerText
  1168. }
  1169. ]
  1170. }
  1171. ]
  1172. },
  1173. {
  1174. $type: "div",
  1175. class: "mdc-layout-grid__inner",
  1176. $components: this._cards
  1177. }
  1178. ]
  1179. }
  1180. ]
  1181. }
  1182. }
  1183. return worldCards
  1184. }
  1185. checkForManualSettings() {
  1186. console.log("check for manual settings");
  1187. let manualSettings = localStorage.getItem('lcs_app_manual_settings');
  1188. if (manualSettings) {
  1189. localStorage.removeItem('lcs_app_manual_settings');
  1190. }
  1191. let el = document.querySelector('#runWorldGUI');
  1192. if (el) {
  1193. if (el._arSwitch.checked) {
  1194. let arSettings = {
  1195. model: {
  1196. 'vwf/model/aframe': null
  1197. },
  1198. view: {
  1199. 'vwf/view/aframe': null,
  1200. 'vwf/view/editor-new': null
  1201. }
  1202. }
  1203. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1204. }
  1205. if (el._turnArOnSwitch.checked) {
  1206. let arSettings = {
  1207. model: {
  1208. 'vwf/model/aframe': null
  1209. },
  1210. view: {
  1211. 'vwf/view/aframe': null,
  1212. 'vwf/view/aframe-ar-driver': null
  1213. }
  1214. }
  1215. localStorage.setItem('lcs_app_manual_settings', JSON.stringify(arSettings));
  1216. }
  1217. }
  1218. }
  1219. }
  1220. export { IndexApp as default }
  1221. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};