world-app.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. class WorldApp {
  7. constructor(userAlias, worldName, saveName) {
  8. console.log("world app constructor");
  9. this.userAlias = userAlias;
  10. this.worldName = worldName;
  11. this.saveName = saveName;
  12. //this.worlds = {};
  13. this.language = _LangManager.language;
  14. let el = document.createElement("div");
  15. el.setAttribute("id", "aboutWorld");
  16. document.body.appendChild(el);
  17. let el2 = document.createElement("div");
  18. el2.setAttribute("id", "worldStates");
  19. document.body.appendChild(el2);
  20. document.querySelector("#worldStates").$cell({
  21. id: "worldStates",
  22. $cell: true,
  23. $type: "div",
  24. _comps: [],
  25. _wcards: {},
  26. $components: [],
  27. _refresh: function (comps) {
  28. //do update;
  29. //this._userAlias = user;
  30. this._comps = comps;
  31. this.$components = this._comps;
  32. },
  33. $init: function () {
  34. console.log('init comp...');
  35. },
  36. $update: function () {
  37. //do update;
  38. console.log('update me');
  39. }
  40. });
  41. }
  42. createWorldStatesGUI() {
  43. let worldStatesGUI = {
  44. id: "worldStatesGUI",
  45. $type: "div",
  46. $components: [],
  47. _states: {},
  48. _refresh: function (data) {
  49. this._states = data
  50. },
  51. _makeWorldCard: function (data) {
  52. let cardID = data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  53. let card = _app.indexApp.createWorldCard(cardID, 'min');
  54. card._refresh(data[1]);
  55. card._updateComps();
  56. return {
  57. $cell: true,
  58. $type: "div",
  59. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  60. $components: [card]
  61. }
  62. //console.log(data);
  63. },
  64. $update: function(){},
  65. _updateComps: function () {
  66. this.$components = [
  67. {
  68. $type: "div",
  69. class: "mdc-layout-grid",
  70. $components: [
  71. {
  72. $type: "div",
  73. class: "mdc-layout-grid__inner",
  74. $components: [
  75. {
  76. $type: "div",
  77. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  78. $components: [
  79. {
  80. $type: "H3",
  81. $text: 'States'
  82. }
  83. ]
  84. }
  85. ]
  86. },
  87. {
  88. $type: "div",
  89. class: "mdc-layout-grid__inner",
  90. $components: Object.entries(this._states)
  91. .filter(el => Object.keys(el[1]).length !== 0)
  92. .sort(function (el1, el2) {
  93. return parseInt(el2[1].created) - parseInt(el1[1].created)
  94. })
  95. .map(this._makeWorldCard)
  96. }
  97. ]
  98. }
  99. ]
  100. }
  101. }
  102. return worldStatesGUI
  103. }
  104. async makeGUI(userPub) {
  105. let self = this;
  106. let user = { 'user': this.userAlias, pub: userPub };
  107. let space = this.worldName;
  108. let saveName = this.saveName;
  109. let cardID = user.user + '_' + space + '_' + (saveName ? saveName : "");
  110. let cardWorldType = saveName ? 'state' : 'proto';
  111. let worldNameInfo = saveName ? {protoName: space, stateName: saveName} : space
  112. let worldCardGUI = _app.indexApp.createWorldCard(cardWorldType, this.userAlias, userPub, worldNameInfo, cardID, "full", setWorldParameters); //createWorldCard(userAlias, userPub, worldName, id, type)
  113. let worldStatesGUI = [];
  114. //var runWorldGUI = {};
  115. function setWorldParameters(data) {
  116. console.log(data);
  117. let actionsGUI = {
  118. $cell: true,
  119. id: "worldActionsGUI",
  120. $type: "div",
  121. $components: [],
  122. _worldInfo: {},
  123. _refresh: function () {
  124. this._worldInfo = {
  125. 'userAlias': self.userAlias,
  126. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  127. 'type': self.saveName ? 'saveState' : 'proto'
  128. }
  129. // let worldCard = document.querySelector('#worldCard');
  130. // if(worldCard){
  131. // this._worldInfo = worldCard._worldInfo;
  132. // }
  133. },
  134. $init: function () {
  135. if (_LCSDB.user().is) {
  136. this._refresh();
  137. }
  138. },
  139. $update: function () {
  140. let desc = this._worldInfo;
  141. let userGUI = [];
  142. // if(!desc){
  143. // this.$components = [];
  144. // return
  145. // }
  146. if (_LCSDB.user().is) {
  147. if (_LCSDB.user().is.alias == desc.userAlias) {
  148. userGUI.push(
  149. {
  150. $type: "a",
  151. class: "mdc-button ",
  152. $text: "Edit info",
  153. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  154. onclick: function (e) {
  155. //'/:user/:type/:name/edit/:file'
  156. if (desc.type == 'proto') {
  157. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/info_json'
  158. } else if (desc.type == 'saveState') {
  159. let names = desc.worldName.split('/');
  160. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  161. window.location.pathname = "/" + desc.userAlias + '/state/' + names[0] + '/edit/' + filename;
  162. }
  163. //self.refresh();
  164. }
  165. },
  166. {
  167. $type: "a",
  168. class: "mdc-button ",
  169. $text: "Edit source",
  170. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  171. onclick: function (e) {
  172. //'/:user/:type/:name/edit/:file'
  173. if (desc.type == 'proto') {
  174. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_yaml'
  175. } else if (desc.type == 'saveState') {
  176. let names = desc.worldName.split('/');
  177. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_vwf_json').split('/').join("~");
  178. window.location.pathname = "/" + desc.userAlias + '/state/' + names[0] + '/edit/' + filename;
  179. }
  180. //self.refresh();
  181. }
  182. }
  183. );
  184. if (desc.type == 'proto') {
  185. userGUI.push(
  186. // {
  187. // $type: "a",
  188. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  189. // $text: "Edit proto",
  190. // //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  191. // onclick: function (e) {
  192. // window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_yaml'
  193. // }
  194. // },
  195. {
  196. $type: "a",
  197. class: "mdc-button ",
  198. $text: "Edit config",
  199. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  200. onclick: function (e) {
  201. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_config_yaml'
  202. }
  203. },
  204. {$type: "br"},
  205. {
  206. $type: "a",
  207. class: "mdc-button",
  208. $text: "Edit appui.js",
  209. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  210. onclick: function (e) {
  211. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/appui_js'
  212. }
  213. },
  214. {
  215. $type: "a",
  216. class: "mdc-button",
  217. $text: "Edit assets.json",
  218. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  219. onclick: function (e) {
  220. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/assets_json'
  221. }
  222. },
  223. {
  224. $type: "a",
  225. class: "mdc-button",
  226. $text: "Edit index.vwf.html",
  227. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  228. onclick: function (e) {
  229. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_html'
  230. }
  231. }
  232. );
  233. userGUI.push(
  234. {$type: "br"},
  235. {
  236. $type: "a",
  237. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  238. $text: "Delete",
  239. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  240. onclick: function (e) {
  241. _app.deleteWorld(desc.worldName, 'proto');
  242. }
  243. }
  244. );
  245. let proxyID = data.proxy;
  246. userGUI.push(
  247. {
  248. $type: "div",
  249. style: "margin-top: 20px;",
  250. _proxyName: null,
  251. _proxyNameField: null,
  252. $components:
  253. [
  254. window._app.widgets.inputTextFieldOutlined({
  255. "id": 'proxyName',
  256. "label": proxyID,
  257. "value": this._proxyName,
  258. "type": "text",
  259. "init": function () {
  260. this._proxyNameField = new mdc.textField.MDCTextField(this);
  261. if(!proxyID){
  262. //document.querySelector('#proxyName').value = res;
  263. } else {
  264. _app.helpers.getUserAlias(proxyID).then(res=>{
  265. document.querySelector('#proxyName').value = res;
  266. })
  267. }
  268. }
  269. }),
  270. {
  271. $type: "a",
  272. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  273. $text: 'Set proxy', //self.language.t('set proxy'),//"clone",
  274. onclick: function (e) {
  275. //console.log('clone');
  276. let newProxyName = this._proxyNameField.value;
  277. _app.setNewProxyForWorld(desc.worldName, newProxyName);
  278. //_app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  279. //self.refresh();
  280. }
  281. }
  282. ]
  283. }
  284. )
  285. }
  286. if (desc.type == 'saveState') {
  287. userGUI.push(
  288. {$type: "br"},
  289. {
  290. $type: "a",
  291. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  292. $text: "Delete",
  293. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  294. onclick: function (e) {
  295. _app.deleteWorld(desc.worldName, 'state');
  296. }
  297. }
  298. );
  299. }
  300. }
  301. if (desc.type == 'proto') {
  302. let worldID = window._app.helpers.GenerateInstanceID().toString();
  303. userGUI.push(
  304. {
  305. $type: "div",
  306. style: "margin-top: 20px;",
  307. _protoName: null,
  308. _protoNameField: null,
  309. $components:
  310. [
  311. window._app.widgets.inputTextFieldOutlined({
  312. "id": 'protoName',
  313. "label": worldID,
  314. "value": this._protoName,
  315. "type": "text",
  316. "init": function () {
  317. this._protoNameField = new mdc.textField.MDCTextField(this);
  318. }
  319. }),
  320. {
  321. $type: "a",
  322. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  323. $text: self.language.t('clone proto'),//"clone",
  324. onclick: function (e) {
  325. //console.log('clone');
  326. let newProtoName = this._protoNameField.value;
  327. _app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  328. //self.refresh();
  329. }
  330. }
  331. ]
  332. }
  333. )
  334. } else if (desc.type == 'saveState') {
  335. // userGUI.push(
  336. // {
  337. // $type: "a",
  338. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  339. // $text: "Clone",
  340. // onclick: function (e) {
  341. // //console.log('clone');
  342. // //self.cloneWorldState(desc[0], desc[2]);
  343. // //self.refresh();
  344. // }
  345. // })
  346. }
  347. }
  348. this.$components = [
  349. {
  350. $type: "div",
  351. $text: "World actions:"
  352. }
  353. ].concat(userGUI)
  354. }
  355. }
  356. document.querySelector("#aboutWorld")._actionsGUI = actionsGUI;
  357. ///settings
  358. let settings = data.settings;
  359. if (settings) {
  360. if (settings.ar) {
  361. let runWorldGUI = {
  362. id: "runWorldGUI",
  363. $type: "div",
  364. $init: function () {
  365. console.log(worldCardGUI);
  366. },
  367. _arSwitch: null,
  368. _turnArOnSwitch: null,
  369. $components: [
  370. {
  371. $type: "div",
  372. $text: "Settings for start:"
  373. },
  374. _cellWidgets.switch({
  375. 'id': 'arjsView',
  376. 'init': function () {
  377. this._switch = new mdc.switchControl.MDCSwitch(this);
  378. this._switch.checked = false;
  379. this._arSwitch = this._switch;
  380. }
  381. }
  382. ),
  383. {
  384. $type: 'label',
  385. for: 'input-forceReplace',
  386. $text: 'Edit mode'
  387. },
  388. {$type: "div", style: "margin-top: 20px"},
  389. _cellWidgets.switch({
  390. 'id': 'arOnView',
  391. 'init': function () {
  392. this._turnArOn = new mdc.switchControl.MDCSwitch(this);
  393. this._turnArOn.checked = false;
  394. this._turnArOnSwitch = this._turnArOn;
  395. }
  396. }
  397. ),
  398. {
  399. $type: 'label',
  400. for: 'input-forceReplace',
  401. $text: 'Ar mode'
  402. }
  403. ]
  404. }
  405. document.querySelector("#aboutWorld")._runWorldGUI = runWorldGUI;
  406. //document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  407. }
  408. }
  409. }
  410. document.querySelector("#aboutWorld").$cell({
  411. id: 'aboutWorld',
  412. $cell: true,
  413. $type: "div",
  414. _actionsGUI: {},
  415. _runWorldGUI: {},
  416. _worldsComps: {},
  417. _refresh: function(comps){
  418. this._worldsComps = comps
  419. },
  420. $init: function(){
  421. //this._worldsComps = worldCardGUI;
  422. },
  423. $update: function(){
  424. this.$components = [
  425. {
  426. $type: "div",
  427. class: "mdc-layout-grid",
  428. $components: [
  429. {
  430. $type: "div",
  431. class: "mdc-layout-grid__inner",
  432. $components: [
  433. {
  434. $type: "div",
  435. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  436. $components: [
  437. {
  438. $type: "h1",
  439. class: "mdc-typography--headline4",
  440. $text: self.worldName + ' by ' + self.userAlias
  441. }
  442. ]
  443. },
  444. {
  445. $type: "div",
  446. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  447. $components: [
  448. //worldCardGUI,
  449. this._worldsComps,
  450. { $type: 'p' },
  451. this._runWorldGUI
  452. ]
  453. },
  454. {
  455. $type: "div",
  456. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  457. $components: [
  458. this._actionsGUI
  459. ]
  460. },
  461. // {
  462. // $type: "div",
  463. // class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  464. // $components: [
  465. // ].concat(worldStatesGUI)
  466. // },
  467. ]
  468. }
  469. ]
  470. }
  471. ]
  472. }
  473. })
  474. document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  475. var info = {};
  476. // if (!saveName) {
  477. // info = await _app.getAllProtoWorldsInfoForUser(user.user, space) //await _app.getWorldInfo(user, space);
  478. // } else {
  479. // let loadName = space + "/load/" + saveName;
  480. // info = await _app.getAllStateWorldsInfoForUser(user.user, space, loadName) //await _app.getStateInfo(user, space, saveName);
  481. // }
  482. // worldCardGUI._worldInfo = info;
  483. // worldCardGUI.$update();
  484. //worldCardGUI._refresh(info);
  485. //worldCardGUI._updateComps();
  486. //document.querySelector("#aboutWorld")._refreshWorldComps(info);
  487. ///STATES FIX!!!
  488. // let worldStatesComp = this.createWorldStatesGUI();
  489. // document.querySelector("#worldStates").$cell({
  490. // id: 'worldStates',
  491. // $cell: true,
  492. // $type: "div",
  493. // $components: [worldStatesComp]
  494. // // $update: function(){
  495. // // this.$components = [
  496. // // {}
  497. // // ]
  498. // // }
  499. // })
  500. if (!saveName) {
  501. _app.indexApp.allWorldsStatesForUser(user.user, space, 'worldStates')
  502. }
  503. // if (!saveName) {
  504. // let statesData = await _app.getAllStateWorldsInfoForUser(user.user, space) //await _app.getSaveStates(user, space);
  505. // //let worldStates = this.createWorldStatesGUI();
  506. // let worldStates = document.querySelector("#worldStatesGUI");
  507. // worldStates._states = statesData;
  508. // worldStates._updateComps();
  509. // worldStatesComp.$components.push(worldStates);
  510. // }
  511. }
  512. }
  513. export { WorldApp }