world-app.js 20 KB

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