world-app.js 26 KB

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