world-app.js 14 KB

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