index-app.js 53 KB

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