world-app.js 25 KB

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