world-app.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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("world app constructor");
  9. this.userAlias = userAlias;
  10. this.worldName = worldName;
  11. this.saveName = saveName;
  12. //this.worlds = {};
  13. this.language = _LangManager.language;
  14. let rootDoc = document.querySelector('#app');
  15. let el = document.createElement("div");
  16. el.setAttribute("id", "aboutWorld");
  17. rootDoc.appendChild(el);
  18. }
  19. async makeGUI(userPub) {
  20. let self = this;
  21. let user = { 'user': this.userAlias, pub: userPub };
  22. let space = this.worldName;
  23. let cardID = user.user + '_' + space + '_';
  24. let worldCardGUI = _app.indexApp.createWorldCard('proto', this.userAlias, userPub, space, cardID, "full", setWorldParameters); //createWorldCard(userAlias, userPub, worldName, id, type)
  25. //var runWorldGUI = {};
  26. function setWorldParameters(data) {
  27. //console.log(data);
  28. let actionsGUI = {
  29. $cell: true,
  30. _gen: "",
  31. id: "worldActionsGUI",
  32. $type: "div",
  33. $components: [],
  34. _worldInfo: {},
  35. _refresh: function () {
  36. this._worldInfo = {
  37. 'userAlias': self.userAlias,
  38. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  39. 'type': self.saveName ? 'saveState' : 'proto'
  40. }
  41. // let worldCard = document.querySelector('#worldCard');
  42. // if(worldCard){
  43. // this._worldInfo = worldCard._worldInfo;
  44. // }
  45. },
  46. $init: function () {
  47. //if (_LCSDB.user().is) {
  48. this._refresh();
  49. //}
  50. },
  51. $update: function () {
  52. let desc = this._worldInfo;
  53. let userGUI = [];
  54. // if(!desc){
  55. // this.$components = [];
  56. // return
  57. // }
  58. if (_LCSDB.user().is) {
  59. if (_LCSDB.user().is.alias == desc.userAlias) {
  60. userGUI.push(
  61. {
  62. $type: "div",
  63. id: "tree",
  64. _tree: [],
  65. _treeComp: {},
  66. $init: function () {
  67. let selfComp = this;
  68. _LCSDB.user().get('worlds').get(desc.worldName).load(res => {
  69. // console.log(res);
  70. if (res) {
  71. selfComp._tree = [{
  72. name: 'File sources: ',
  73. children: []
  74. }];
  75. Object.keys(res).filter(el => el.includes('_js') || el.includes('_json')).forEach(el => {
  76. selfComp._tree[0].children.push({
  77. name: el
  78. })
  79. })
  80. selfComp._treeComp = new TreeView(selfComp._tree, 'tree');
  81. selfComp._treeComp.on('select', function (evt) {
  82. console.log(evt);
  83. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/' + evt.data.name
  84. });
  85. }
  86. })
  87. },
  88. $components: [
  89. ]
  90. }
  91. );
  92. if (desc.type == 'proto') {
  93. userGUI.push(
  94. { $type: "br" },
  95. {
  96. $type: "a",
  97. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  98. $text: "Delete",
  99. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  100. onclick: function (e) {
  101. if (window.confirm("Do you really want to DELETE world?")) {
  102. _app.deleteWorld(desc.worldName, 'proto');
  103. }
  104. }
  105. }
  106. );
  107. let proxyID = data.proxy;
  108. userGUI.push(
  109. {
  110. $type: "div",
  111. style: "margin-top: 20px;",
  112. _proxyName: null,
  113. _proxyNameField: null,
  114. $components:
  115. [
  116. window._app.widgets.inputTextFieldOutlined({
  117. "id": 'proxyName',
  118. "label": proxyID,
  119. "value": this._proxyName,
  120. "type": "text",
  121. "init": function () {
  122. this._proxyNameField = new mdc.textField.MDCTextField(this);
  123. if (!proxyID) {
  124. //document.querySelector('#proxyName').value = res;
  125. } else {
  126. _app.helpers.getUserAlias(proxyID).then(res => {
  127. this._proxyNameField.value = res;
  128. //document.querySelector('#proxyName').value = res;
  129. })
  130. }
  131. }
  132. }),
  133. {
  134. $type: "a",
  135. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  136. $text: 'Set proxy', //self.language.t('set proxy'),//"clone",
  137. onclick: function (e) {
  138. //console.log('clone');
  139. let newProxyName = this._proxyNameField.value;
  140. _app.setNewProxyForWorld(desc.worldName, newProxyName);
  141. //_app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  142. //self.refresh();
  143. }
  144. }
  145. ]
  146. }
  147. )
  148. }
  149. // if (desc.type == 'saveState') {
  150. // userGUI.push(
  151. // { $type: "br" },
  152. // {
  153. // $type: "a",
  154. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  155. // $text: "Delete",
  156. // //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  157. // onclick: function (e) {
  158. // _app.deleteWorld(desc.worldName, 'state');
  159. // }
  160. // }
  161. // );
  162. // }
  163. }
  164. if (desc.type == 'proto') {
  165. let worldID = window._app.helpers.GenerateInstanceID().toString();
  166. userGUI.push(
  167. {
  168. $type: "div",
  169. style: "margin-top: 20px;",
  170. _protoName: null,
  171. _protoNameField: null,
  172. $components:
  173. [
  174. window._app.widgets.inputTextFieldOutlined({
  175. "id": 'protoName',
  176. "label": worldID,
  177. "value": this._protoName,
  178. "type": "text",
  179. "init": function () {
  180. this._protoNameField = new mdc.textField.MDCTextField(this);
  181. }
  182. }),
  183. {
  184. $type: "a",
  185. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  186. $text: self.language.t('clone proto'),//"clone",
  187. onclick: function (e) {
  188. //console.log('clone');
  189. let newProtoName = this._protoNameField.value;
  190. _app.cloneWorld(desc.worldName, desc.userAlias, newProtoName);
  191. let appEl = document.createElement("div");
  192. appEl.setAttribute("id", 'cloneLink');
  193. let entry = document.querySelector('#worldActionsGUI');
  194. if (entry) {
  195. entry.appendChild(appEl);
  196. document.querySelector("#cloneLink").$cell({
  197. id: 'cloneLink',
  198. $cell: true,
  199. $type: "div",
  200. $components: [
  201. {
  202. $type: "a",
  203. class: "mdc-button mdc-button--raised mdc-card__action",
  204. $text: "Go to new cloned World!",
  205. onclick: function (e) {
  206. let myName = _LCSDB.user().is.alias;
  207. window.location.pathname = '/' + myName + '/' + newProtoName + '/about'
  208. }
  209. }
  210. ]
  211. })
  212. }
  213. // _app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  214. //self.refresh();
  215. }
  216. }
  217. ]
  218. }
  219. );
  220. }
  221. }
  222. userGUI.push(
  223. window._app.widgets.p,
  224. {
  225. $type: "div",
  226. id: "tree_states",
  227. _tree: [],
  228. _treeComp: {},
  229. $init: function () {
  230. let selfComp = this;
  231. let userPub = new Promise(res => res(_app.helpers.getUserPub(desc.userAlias)));
  232. userPub.then(pub => {
  233. //console.log(pub);
  234. _LCSDB.user(pub).get('documents').get(desc.worldName).load(res => {
  235. // console.log(res);
  236. if (res) {
  237. selfComp._tree = [{
  238. name: 'States',
  239. children: []
  240. }];
  241. Object.keys(res).filter(el => el.includes('savestate_/' + desc.worldName + '/')).forEach(el => {
  242. let genLink = _app.helpers.replaceSubStringALL(el.split('/')[2], '_vwf_json', '');
  243. selfComp._tree[0].children.push({
  244. name: genLink
  245. })
  246. })
  247. selfComp._treeComp = new TreeView(selfComp._tree, 'tree_states');
  248. selfComp._treeComp.on('select', function (evt) {
  249. console.log(evt);
  250. window.location.pathname = "/" + desc.userAlias + "/" + desc.worldName + "/load/" + evt.data.name;
  251. });
  252. }
  253. })
  254. })
  255. },
  256. $components: [
  257. ]
  258. }
  259. );
  260. this.$components = [
  261. {
  262. $type: "h3",
  263. $text: "World details:"
  264. }
  265. ].concat(userGUI)
  266. }
  267. }
  268. document.querySelector("#aboutWorld")._actionsGUI = actionsGUI;
  269. ///settings
  270. let settings = data.settings;
  271. if (settings) {
  272. if (settings.ar) {
  273. let runWorldGUI = {
  274. id: "runWorldGUI",
  275. $type: "div",
  276. $init: function () {
  277. console.log(worldCardGUI);
  278. },
  279. _arSwitch: null,
  280. _turnArOnSwitch: null,
  281. $components: [
  282. {
  283. $type: "div",
  284. $text: "Settings for start:"
  285. },
  286. _cellWidgets.switch({
  287. 'id': 'arjsView',
  288. 'init': function () {
  289. this._switch = new mdc.switchControl.MDCSwitch(this);
  290. this._switch.checked = false;
  291. this._arSwitch = this._switch;
  292. }
  293. }
  294. ),
  295. {
  296. $type: 'label',
  297. for: 'input-forceReplace',
  298. $text: 'Edit mode'
  299. },
  300. { $type: "div", style: "margin-top: 20px" },
  301. _cellWidgets.switch({
  302. 'id': 'arOnView',
  303. 'init': function () {
  304. this._turnArOn = new mdc.switchControl.MDCSwitch(this);
  305. this._turnArOn.checked = false;
  306. this._turnArOnSwitch = this._turnArOn;
  307. }
  308. }
  309. ),
  310. {
  311. $type: 'label',
  312. for: 'input-forceReplace',
  313. $text: 'Ar mode'
  314. }
  315. ]
  316. }
  317. document.querySelector("#aboutWorld")._runWorldGUI = runWorldGUI;
  318. //document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  319. }
  320. }
  321. }
  322. document.querySelector("#aboutWorld").$cell({
  323. id: 'aboutWorld',
  324. $cell: true,
  325. $type: "div",
  326. _actionsGUI: {},
  327. _runWorldGUI: {},
  328. _worldsComps: {},
  329. _refresh: function (comps) {
  330. this._worldsComps = comps
  331. },
  332. $init: function () {
  333. //this._worldsComps = worldCardGUI;
  334. },
  335. $update: function () {
  336. this.$components = [
  337. {
  338. $type: "div",
  339. class: "mdc-layout-grid",
  340. $components: [
  341. {
  342. $type: "div",
  343. class: "mdc-layout-grid__inner",
  344. $components: [
  345. {
  346. $type: "div",
  347. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  348. $components: [
  349. {
  350. $type: "h1",
  351. class: "mdc-typography--headline4",
  352. $text: self.worldName + ' by ' + self.userAlias
  353. }
  354. ]
  355. },
  356. {
  357. $type: "div",
  358. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  359. $components: [
  360. //worldCardGUI,
  361. this._worldsComps,
  362. { $type: 'p' },
  363. this._runWorldGUI
  364. ]
  365. },
  366. {
  367. $type: "div",
  368. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  369. $components: [
  370. this._actionsGUI
  371. ]
  372. }
  373. ]
  374. }
  375. ]
  376. }
  377. ]
  378. }
  379. })
  380. document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  381. }
  382. }
  383. export { WorldApp }