world-app.js 24 KB

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