world-app.js 14 KB

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