world-app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 initWorldGUI() {
  86. // _LCSDB.on('auth',
  87. // function (ack) {
  88. // if(ack.pub)
  89. // document.querySelector('#worldActionsGUI')._refresh();
  90. // });
  91. let self = this;
  92. let user = this.userAlias;
  93. let space = this.worldName;
  94. let saveName = this.saveName;
  95. let el = document.createElement("div");
  96. el.setAttribute("id", "aboutWorld");
  97. document.body.appendChild(el);
  98. let cardID = user + '_' + space + '_' + (saveName ? saveName : "");
  99. let worldCardGUI = _app.indexApp.createWorldCard(cardID, 'full');
  100. let worldStatesGUI = [];
  101. var info = {};
  102. if (!saveName) {
  103. info = await _app.getWorldInfo(user, space);
  104. } else {
  105. info = await _app.getStateInfo(user, space, saveName);
  106. }
  107. worldCardGUI._worldInfo = info;
  108. worldCardGUI.$update();
  109. if (!saveName) {
  110. let statesData = await _app.getSaveStates(user, space);
  111. let worldStates = this.createWorldStatesGUI();
  112. worldStates._states = statesData;
  113. worldStates.$update();
  114. worldStatesGUI.push(worldStates);
  115. }
  116. let actionsGUI = {
  117. $cell: true,
  118. id: "worldActionsGUI",
  119. $type: "div",
  120. $components: [],
  121. _worldInfo: {},
  122. _refresh: function () {
  123. this._worldInfo = {
  124. 'userAlias': self.userAlias,
  125. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  126. 'type': self.saveName ? 'saveState' : 'proto'
  127. }
  128. // let worldCard = document.querySelector('#worldCard');
  129. // if(worldCard){
  130. // this._worldInfo = worldCard._worldInfo;
  131. // }
  132. },
  133. $init: function () {
  134. if (_LCSDB.user().is) {
  135. this._refresh();
  136. }
  137. },
  138. $update: function () {
  139. let desc = this._worldInfo;
  140. let userGUI = [];
  141. // if(!desc){
  142. // this.$components = [];
  143. // return
  144. // }
  145. if (_LCSDB.user().is) {
  146. if (_LCSDB.user().is.alias == desc.userAlias) {
  147. userGUI.push(
  148. {
  149. $type: "a",
  150. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  151. $text: "Edit info",
  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/info_json'
  157. } else if (desc.type == 'saveState') {
  158. let names = desc.worldName.split('/');
  159. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_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. userGUI.push(
  179. {
  180. $type: "a",
  181. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  182. $text: "Delete",
  183. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  184. onclick: function (e) {
  185. _app.deleteWorld(desc.worldName, 'proto');
  186. }
  187. }
  188. );
  189. }
  190. if (desc.type == 'saveState') {
  191. userGUI.push(
  192. {
  193. $type: "a",
  194. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  195. $text: "Delete",
  196. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  197. onclick: function (e) {
  198. _app.deleteWorld(desc.worldName, 'state');
  199. }
  200. }
  201. );
  202. }
  203. }
  204. if (desc.type == 'proto') {
  205. userGUI.push(
  206. {
  207. $type: "a",
  208. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  209. $text: self.language.t('clone proto'),//"clone",
  210. onclick: function (e) {
  211. //console.log('clone');
  212. _app.cloneWorldPrototype(desc.worldName, desc.userAlias);
  213. //self.refresh();
  214. }
  215. }
  216. )
  217. } else if (desc.type == 'saveState') {
  218. // userGUI.push(
  219. // {
  220. // $type: "a",
  221. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  222. // $text: "Clone",
  223. // onclick: function (e) {
  224. // //console.log('clone');
  225. // //self.cloneWorldState(desc[0], desc[2]);
  226. // //self.refresh();
  227. // }
  228. // })
  229. }
  230. }
  231. this.$components = [
  232. {
  233. $type: "div",
  234. $text: "World actions:"
  235. }
  236. ].concat(userGUI)
  237. }
  238. }
  239. document.querySelector("#aboutWorld").$cell({
  240. id: 'aboutWorld',
  241. $cell: true,
  242. $type: "div",
  243. $components: [
  244. {
  245. $type: "div",
  246. class: "mdc-layout-grid",
  247. $components: [
  248. {
  249. $type: "div",
  250. class: "mdc-layout-grid__inner",
  251. $components: [
  252. {
  253. $type: "div",
  254. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  255. $components: [
  256. {
  257. $type: "h1",
  258. class: "mdc-typography--headline4",
  259. $text: self.worldName + ' by ' + self.userAlias
  260. }
  261. ]
  262. },
  263. {
  264. $type: "div",
  265. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  266. $components: [
  267. worldCardGUI
  268. ]
  269. },
  270. {
  271. $type: "div",
  272. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  273. $components: [
  274. actionsGUI
  275. ]
  276. },
  277. {
  278. $type: "div",
  279. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  280. $components: [
  281. ].concat(worldStatesGUI)
  282. },
  283. ]
  284. }
  285. ]
  286. }
  287. ]
  288. })
  289. }
  290. }
  291. export { WorldApp }