world-app.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. document.querySelector('#proxyName').value = res;
  128. })
  129. }
  130. }
  131. }),
  132. {
  133. $type: "a",
  134. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  135. $text: 'Set proxy', //self.language.t('set proxy'),//"clone",
  136. onclick: function (e) {
  137. //console.log('clone');
  138. let newProxyName = this._proxyNameField.value;
  139. _app.setNewProxyForWorld(desc.worldName, newProxyName);
  140. //_app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  141. //self.refresh();
  142. }
  143. }
  144. ]
  145. }
  146. )
  147. }
  148. // if (desc.type == 'saveState') {
  149. // userGUI.push(
  150. // { $type: "br" },
  151. // {
  152. // $type: "a",
  153. // class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  154. // $text: "Delete",
  155. // //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  156. // onclick: function (e) {
  157. // _app.deleteWorld(desc.worldName, 'state');
  158. // }
  159. // }
  160. // );
  161. // }
  162. }
  163. if (desc.type == 'proto') {
  164. let worldID = window._app.helpers.GenerateInstanceID().toString();
  165. userGUI.push(
  166. {
  167. $type: "div",
  168. style: "margin-top: 20px;",
  169. _protoName: null,
  170. _protoNameField: null,
  171. $components:
  172. [
  173. window._app.widgets.inputTextFieldOutlined({
  174. "id": 'protoName',
  175. "label": worldID,
  176. "value": this._protoName,
  177. "type": "text",
  178. "init": function () {
  179. this._protoNameField = new mdc.textField.MDCTextField(this);
  180. }
  181. }),
  182. {
  183. $type: "a",
  184. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  185. $text: self.language.t('clone proto'),//"clone",
  186. onclick: function (e) {
  187. //console.log('clone');
  188. let newProtoName = this._protoNameField.value;
  189. _app.cloneWorld(desc.worldName, desc.userAlias, newProtoName);
  190. let appEl = document.createElement("div");
  191. appEl.setAttribute("id", 'cloneLink');
  192. let entry = document.querySelector('#worldActionsGUI');
  193. if (entry) {
  194. entry.appendChild(appEl);
  195. document.querySelector("#cloneLink").$cell({
  196. id: 'cloneLink',
  197. $cell: true,
  198. $type: "div",
  199. $components: [
  200. {
  201. $type: "a",
  202. class: "mdc-button mdc-button--raised mdc-card__action",
  203. $text: "Go to new cloned World!",
  204. onclick: function (e) {
  205. let myName = _LCSDB.user().is.alias;
  206. window.location.pathname = '/' + myName + '/' + newProtoName + '/about'
  207. }
  208. }
  209. ]
  210. })
  211. }
  212. // _app.cloneWorldPrototype(desc.worldName, desc.userAlias, newProtoName);
  213. //self.refresh();
  214. }
  215. }
  216. ]
  217. }
  218. );
  219. }
  220. }
  221. userGUI.push(
  222. window._app.widgets.p,
  223. {
  224. $type: "div",
  225. id: "tree_states",
  226. _tree: [],
  227. _treeComp: {},
  228. $init: function () {
  229. let selfComp = this;
  230. let userPub = new Promise(res => res(_app.helpers.getUserPub(desc.userAlias)));
  231. userPub.then(pub => {
  232. //console.log(pub);
  233. _LCSDB.user(pub).get('documents').get(desc.worldName).load(res => {
  234. // console.log(res);
  235. if (res) {
  236. selfComp._tree = [{
  237. name: 'States',
  238. children: []
  239. }];
  240. Object.keys(res).filter(el => el.includes('savestate_/' + desc.worldName + '/')).forEach(el => {
  241. let genLink = _app.helpers.replaceSubStringALL(el.split('/')[2], '_vwf_json', '');
  242. selfComp._tree[0].children.push({
  243. name: genLink
  244. })
  245. })
  246. selfComp._treeComp = new TreeView(selfComp._tree, 'tree_states');
  247. selfComp._treeComp.on('select', function (evt) {
  248. console.log(evt);
  249. window.location.pathname = "/" + desc.userAlias + "/" + desc.worldName + "/load/" + evt.data.name;
  250. });
  251. }
  252. })
  253. })
  254. },
  255. $components: [
  256. ]
  257. }
  258. );
  259. this.$components = [
  260. {
  261. $type: "h3",
  262. $text: "World details:"
  263. }
  264. ].concat(userGUI)
  265. }
  266. }
  267. document.querySelector("#aboutWorld")._actionsGUI = actionsGUI;
  268. ///settings
  269. let settings = data.settings;
  270. if (settings) {
  271. if (settings.ar) {
  272. let runWorldGUI = {
  273. id: "runWorldGUI",
  274. $type: "div",
  275. $init: function () {
  276. console.log(worldCardGUI);
  277. },
  278. _arSwitch: null,
  279. _turnArOnSwitch: null,
  280. $components: [
  281. {
  282. $type: "div",
  283. $text: "Settings for start:"
  284. },
  285. _cellWidgets.switch({
  286. 'id': 'arjsView',
  287. 'init': function () {
  288. this._switch = new mdc.switchControl.MDCSwitch(this);
  289. this._switch.checked = false;
  290. this._arSwitch = this._switch;
  291. }
  292. }
  293. ),
  294. {
  295. $type: 'label',
  296. for: 'input-forceReplace',
  297. $text: 'Edit mode'
  298. },
  299. { $type: "div", style: "margin-top: 20px" },
  300. _cellWidgets.switch({
  301. 'id': 'arOnView',
  302. 'init': function () {
  303. this._turnArOn = new mdc.switchControl.MDCSwitch(this);
  304. this._turnArOn.checked = false;
  305. this._turnArOnSwitch = this._turnArOn;
  306. }
  307. }
  308. ),
  309. {
  310. $type: 'label',
  311. for: 'input-forceReplace',
  312. $text: 'Ar mode'
  313. }
  314. ]
  315. }
  316. document.querySelector("#aboutWorld")._runWorldGUI = runWorldGUI;
  317. //document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  318. }
  319. }
  320. }
  321. document.querySelector("#aboutWorld").$cell({
  322. id: 'aboutWorld',
  323. $cell: true,
  324. $type: "div",
  325. _actionsGUI: {},
  326. _runWorldGUI: {},
  327. _worldsComps: {},
  328. _refresh: function (comps) {
  329. this._worldsComps = comps
  330. },
  331. $init: function () {
  332. //this._worldsComps = worldCardGUI;
  333. },
  334. $update: function () {
  335. this.$components = [
  336. {
  337. $type: "div",
  338. class: "mdc-layout-grid",
  339. $components: [
  340. {
  341. $type: "div",
  342. class: "mdc-layout-grid__inner",
  343. $components: [
  344. {
  345. $type: "div",
  346. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  347. $components: [
  348. {
  349. $type: "h1",
  350. class: "mdc-typography--headline4",
  351. $text: self.worldName + ' by ' + self.userAlias
  352. }
  353. ]
  354. },
  355. {
  356. $type: "div",
  357. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  358. $components: [
  359. //worldCardGUI,
  360. this._worldsComps,
  361. { $type: 'p' },
  362. this._runWorldGUI
  363. ]
  364. },
  365. {
  366. $type: "div",
  367. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  368. $components: [
  369. this._actionsGUI
  370. ]
  371. }
  372. ]
  373. }
  374. ]
  375. }
  376. ]
  377. }
  378. })
  379. document.querySelector("#aboutWorld")._refresh(worldCardGUI);
  380. }
  381. }
  382. export { WorldApp }