world-app.js 15 KB

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