index-app.js 50 KB

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