index-app.js 46 KB

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