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