world-app.js 24 KB

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