world-app.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 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. }
  228. if (desc.type == 'saveState') {
  229. userGUI.push(
  230. {$type: "br"},
  231. {
  232. $type: "a",
  233. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  234. $text: "Delete",
  235. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  236. onclick: function (e) {
  237. _app.deleteWorld(desc.worldName, 'state');
  238. }
  239. }
  240. );
  241. }
  242. }
  243. if (desc.type == 'proto') {
  244. let worldID = window._app.helpers.GenerateInstanceID().toString();
  245. userGUI.push(
  246. {
  247. $type: "div",
  248. style: "margin-top: 20px;",
  249. _protoName: null,
  250. _protoNameField: null,
  251. $components:
  252. [
  253. window._app.widgets.inputTextFieldOutlined({
  254. "id": 'protoName',
  255. "label": worldID,
  256. "value": this._protoName,
  257. "type": "text",
  258. "init": function () {
  259. this._protoNameField = new mdc.textField.MDCTextField(this);
  260. }
  261. }),
  262. {
  263. $type: "a",
  264. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  265. $text: self.language.t('clone proto'),//"clone",
  266. onclick: function (e) {
  267. //console.log('clone');
  268. let newProtoName = this._protoNameField.value;
  269. _app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  270. //self.refresh();
  271. }
  272. }
  273. ]
  274. }
  275. )
  276. } else if (desc.type == 'saveState') {
  277. // userGUI.push(
  278. // {
  279. // $type: "a",
  280. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  281. // $text: "Clone",
  282. // onclick: function (e) {
  283. // //console.log('clone');
  284. // //self.cloneWorldState(desc[0], desc[2]);
  285. // //self.refresh();
  286. // }
  287. // })
  288. }
  289. }
  290. this.$components = [
  291. {
  292. $type: "div",
  293. $text: "World actions:"
  294. }
  295. ].concat(userGUI)
  296. }
  297. }
  298. document.querySelector("#aboutWorld").$cell({
  299. id: 'aboutWorld',
  300. $cell: true,
  301. $type: "div",
  302. _runWorldGUI: {},
  303. $update: function(){
  304. this.$components = [
  305. {
  306. $type: "div",
  307. class: "mdc-layout-grid",
  308. $components: [
  309. {
  310. $type: "div",
  311. class: "mdc-layout-grid__inner",
  312. $components: [
  313. {
  314. $type: "div",
  315. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  316. $components: [
  317. {
  318. $type: "h1",
  319. class: "mdc-typography--headline4",
  320. $text: self.worldName + ' by ' + self.userAlias
  321. }
  322. ]
  323. },
  324. {
  325. $type: "div",
  326. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  327. $components: [
  328. worldCardGUI,
  329. { $type: 'p' },
  330. this._runWorldGUI
  331. ]
  332. },
  333. {
  334. $type: "div",
  335. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  336. $components: [
  337. actionsGUI
  338. ]
  339. },
  340. // {
  341. // $type: "div",
  342. // class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  343. // $components: [
  344. // ].concat(worldStatesGUI)
  345. // },
  346. ]
  347. }
  348. ]
  349. }
  350. ]
  351. }
  352. })
  353. let worldStatesComp = this.createWorldStatesGUI();
  354. document.querySelector("#worldStates").$cell({
  355. id: 'worldStates',
  356. $cell: true,
  357. $type: "div",
  358. $components: [worldStatesComp]
  359. // $update: function(){
  360. // this.$components = [
  361. // {}
  362. // ]
  363. // }
  364. })
  365. var info = {};
  366. if (!saveName) {
  367. info = await _app.getWorldInfo(user, space);
  368. } else {
  369. info = await _app.getStateInfo(user, space, saveName);
  370. }
  371. worldCardGUI._worldInfo = info;
  372. worldCardGUI.$update();
  373. document.querySelector("#aboutWorld").$update();
  374. if (!saveName) {
  375. let statesData = await _app.getSaveStates(user, space);
  376. //let worldStates = this.createWorldStatesGUI();
  377. let worldStates = document.querySelector("#worldStatesGUI");
  378. worldStates._states = statesData;
  379. worldStates.$update();
  380. worldStatesComp.$components.push(worldStates);
  381. }
  382. let settings = worldCardGUI._worldInfo.settings;
  383. if (settings) {
  384. if (settings.ar) {
  385. let runWorldGUI = {
  386. id: "runWorldGUI",
  387. $type: "div",
  388. $init: function () {
  389. console.log(worldCardGUI);
  390. },
  391. _arSwitch: null,
  392. _turnArOnSwitch: null,
  393. $components: [
  394. {
  395. $type: "div",
  396. $text: "Settings for start:"
  397. },
  398. _cellWidgets.switch({
  399. 'id': 'arjsView',
  400. 'init': function () {
  401. this._switch = new mdc.switchControl.MDCSwitch(this);
  402. this._switch.checked = false;
  403. this._arSwitch = this._switch;
  404. }
  405. }
  406. ),
  407. {
  408. $type: 'label',
  409. for: 'input-forceReplace',
  410. $text: 'Edit mode'
  411. },
  412. {$type: "div", style: "margin-top: 20px"},
  413. _cellWidgets.switch({
  414. 'id': 'arOnView',
  415. 'init': function () {
  416. this._turnArOn = new mdc.switchControl.MDCSwitch(this);
  417. this._turnArOn.checked = false;
  418. this._turnArOnSwitch = this._turnArOn;
  419. }
  420. }
  421. ),
  422. {
  423. $type: 'label',
  424. for: 'input-forceReplace',
  425. $text: 'Ar mode'
  426. }
  427. ]
  428. }
  429. document.querySelector("#aboutWorld")._runWorldGUI = runWorldGUI;
  430. }
  431. }
  432. }
  433. initWorldGUI() {
  434. // _LCSDB.on('auth',
  435. // function (ack) {
  436. // if(ack.pub)
  437. // document.querySelector('#worldActionsGUI')._refresh();
  438. // });
  439. let self = this;
  440. _LCSDB.get('users').get(this.userAlias).get('pub').once(function (res) {
  441. self.makeGUI(res)
  442. });
  443. }
  444. }
  445. export { WorldApp }