world-app.js 26 KB

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