world-app.js 21 KB

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