world-app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. let runWorldGUI = {
  112. id: "runWorldGUI",
  113. $type: "div",
  114. _settingsSwitch: null,
  115. $components: [
  116. {
  117. $type: "div",
  118. $text: "Settings for start:"
  119. },
  120. _cellWidgets.switch({
  121. 'id': 'arjsView',
  122. 'init': function () {
  123. this._switch = new mdc.switchControl.MDCSwitch(this);
  124. this._switch.checked = false;
  125. this._settingsSwitch = this._switch;
  126. }
  127. }
  128. ),
  129. {
  130. $type: 'label',
  131. for: 'input-forceReplace',
  132. $text: 'AR'
  133. }
  134. ]
  135. }
  136. let actionsGUI = {
  137. $cell: true,
  138. id: "worldActionsGUI",
  139. $type: "div",
  140. $components: [],
  141. _worldInfo: {},
  142. _refresh: function () {
  143. this._worldInfo = {
  144. 'userAlias': self.userAlias,
  145. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  146. 'type': self.saveName ? 'saveState' : 'proto'
  147. }
  148. // let worldCard = document.querySelector('#worldCard');
  149. // if(worldCard){
  150. // this._worldInfo = worldCard._worldInfo;
  151. // }
  152. },
  153. $init: function () {
  154. if (_LCSDB.user().is) {
  155. this._refresh();
  156. }
  157. },
  158. $update: function () {
  159. let desc = this._worldInfo;
  160. let userGUI = [];
  161. // if(!desc){
  162. // this.$components = [];
  163. // return
  164. // }
  165. if (_LCSDB.user().is) {
  166. if (_LCSDB.user().is.alias == desc.userAlias) {
  167. userGUI.push(
  168. {
  169. $type: "a",
  170. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  171. $text: "Edit info",
  172. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  173. onclick: function (e) {
  174. //'/:user/:type/:name/edit/:file'
  175. if (desc.type == 'proto') {
  176. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/info_json'
  177. } else if (desc.type == 'saveState') {
  178. let names = desc.worldName.split('/');
  179. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  180. window.location.pathname = "/" + desc.userAlias + '/state/' + names[0] + '/edit/' + filename;
  181. }
  182. //self.refresh();
  183. }
  184. }
  185. );
  186. if (desc.type == 'proto') {
  187. userGUI.push(
  188. {
  189. $type: "a",
  190. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  191. $text: "Edit proto",
  192. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  193. onclick: function (e) {
  194. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_yaml'
  195. }
  196. }
  197. );
  198. userGUI.push(
  199. {
  200. $type: "a",
  201. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  202. $text: "Delete",
  203. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  204. onclick: function (e) {
  205. _app.deleteWorld(desc.worldName, 'proto');
  206. }
  207. }
  208. );
  209. }
  210. if (desc.type == 'saveState') {
  211. userGUI.push(
  212. {
  213. $type: "a",
  214. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  215. $text: "Delete",
  216. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  217. onclick: function (e) {
  218. _app.deleteWorld(desc.worldName, 'state');
  219. }
  220. }
  221. );
  222. }
  223. }
  224. if (desc.type == 'proto') {
  225. userGUI.push(
  226. {
  227. $type: "a",
  228. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  229. $text: self.language.t('clone proto'),//"clone",
  230. onclick: function (e) {
  231. //console.log('clone');
  232. _app.cloneWorldPrototype(desc.worldName, desc.userAlias);
  233. //self.refresh();
  234. }
  235. }
  236. )
  237. } else if (desc.type == 'saveState') {
  238. // userGUI.push(
  239. // {
  240. // $type: "a",
  241. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  242. // $text: "Clone",
  243. // onclick: function (e) {
  244. // //console.log('clone');
  245. // //self.cloneWorldState(desc[0], desc[2]);
  246. // //self.refresh();
  247. // }
  248. // })
  249. }
  250. }
  251. this.$components = [
  252. {
  253. $type: "div",
  254. $text: "World actions:"
  255. }
  256. ].concat(userGUI)
  257. }
  258. }
  259. document.querySelector("#aboutWorld").$cell({
  260. id: 'aboutWorld',
  261. $cell: true,
  262. $type: "div",
  263. $components: [
  264. {
  265. $type: "div",
  266. class: "mdc-layout-grid",
  267. $components: [
  268. {
  269. $type: "div",
  270. class: "mdc-layout-grid__inner",
  271. $components: [
  272. {
  273. $type: "div",
  274. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  275. $components: [
  276. {
  277. $type: "h1",
  278. class: "mdc-typography--headline4",
  279. $text: self.worldName + ' by ' + self.userAlias
  280. }
  281. ]
  282. },
  283. {
  284. $type: "div",
  285. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  286. $components: [
  287. worldCardGUI,
  288. {$type: 'p'},
  289. runWorldGUI
  290. ]
  291. },
  292. {
  293. $type: "div",
  294. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  295. $components: [
  296. actionsGUI
  297. ]
  298. },
  299. {
  300. $type: "div",
  301. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  302. $components: [
  303. ].concat(worldStatesGUI)
  304. },
  305. ]
  306. }
  307. ]
  308. }
  309. ]
  310. })
  311. }
  312. async initWorldGUI() {
  313. // _LCSDB.on('auth',
  314. // function (ack) {
  315. // if(ack.pub)
  316. // document.querySelector('#worldActionsGUI')._refresh();
  317. // });
  318. let self = this;
  319. let users = await _LCSDB.get('users').then();
  320. await _LCSDB.get('users').get(this.userAlias).get('pub').then(res=>{
  321. self.makeGUI(res)
  322. });
  323. }
  324. }
  325. export { WorldApp }