123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082 |
- /*
- The MIT License (MIT)
- Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
- Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
- */
- import page from '/lib/page.mjs';
- import { Helpers } from '/core/helpers.js';
- import { VWF } from '/core/vwf.js';
- import { WorldApp } from '/web/world-app.js';
- import { Widgets } from '/lib/ui/widgets.js';
- import {Spinner} from '/lib/ui/spinjs/spin.js';
- import { createAdapter } from '/lib/fun/@most/adapter/dist/index.mjs';
- import * as mostSubject from '/lib/fun/@most/subject/dist/index.all.js';
- class App {
- constructor() {
- console.log("app constructor");
- this.widgets = new Widgets;
- //globals
- window._app = this;
- window._cellWidgets = this.widgets;
- //global functional objects M - for MostJS Core; R - for Ramda; L - for Partial Lenses
- window.M = mostCore;
- M.createAdapter = createAdapter;
- M.subject = mostSubject;
- M.scheduler = mostScheduler;
- M.prelude = mostPrelude;
- M.e = mostDomEvent;
-
- ///
- this.setupLoadScreen();
- window._noty = new Noty;
- this.helpers = new Helpers;
- this.log = this.helpers.log;
- this.hashids = new Hashids.default();
- this.clearLocalStorage();
- // this.luminary = new Luminary;
- this.config = {};
- this.initDB()
- new Promise(res => { this.initUser(); res });
- import('/lib/locale/locale.js').then(res => {
- window._LangManager = new res.default;
- return new Promise(r => r(_LangManager.setLanguage()))
- })
- .then(res => {
- window._l = _LangManager.language;
- })
- .then(res => {
- return import('/web/index-app.js');
- }).then(res => {
- window.IndexApp = res.default;
- this.setPageRoutes();
- });
- }
- setPageRoutes() {
- //client routes
- page('*', this.HandleQuery);
- page('/', this.HandleIndex);
- page('/setup', this.HandleSetupIndex);
- page('/debug', this.HandleDebugIndex);
- page('/settings', this.HandleSettingsIndex);
- page('/profile', this.HandleUserIndex);
- page('/worlds', this.HandleIndex);
- page('/:user', this.HandleUserWorlds);
- page('/:user/worlds', this.HandleUserWorlds);
- page('/:user/:type/:name/edit/:file', this.HandleFileEdit);
- page('/:user/:space', this.HandleParsableRequestWithID);
- page('/:user/:space/index.vwf', this.HandleParsableRequestWithID);
- page('/:user/:space/about', this.HandleWorldAbout);
- //page('/:user/:space/:id', this.HandleParsableRequestWithID);
- //page('/:user/:space/index.vwf/:id', this.HandleParsableRequestWithID);
- page('/:user/:space/load/:savename', this.HandleParsableLoadRequest);
- page('/:user/:space/:id/load/:savename', this.HandleParsableRequestWithID);
- page('/:user/:space/index.vwf/:id/load/:savename', this.HandleParsableRequestWithID);
- // page('/:user/worlds/:type', this.HandleUserWorldsWithType);
- // page('/:user/:space/load/:savename/about', this.HandleWorldAbout);
- // page('/:user/:space/load/:savename/:rev', this.HandleParsableLoadRequestWithRev);
- // page('/:user/:space/:id/load/:savename/:rev', this.HandleParsableRequestWithID);
- page('*', this.HandleNoPage);
- page();
- }
- initDB() {
- let configDefaults = {
- 'luminary': false,
- 'luminaryPath': 'https://localhost:8081',
- 'luminaryGlobalHBPath': 'server/heartbeat',
- 'luminaryGlobalHB': false,
- 'dbhost': window.location.origin + '/gun', // 'https://' + window.location.hostname + ':8080/gun', //'http://localhost:8080/gun',
- 'reflector': 'https://' + window.location.hostname + ':3002',
- 'webrtc': false,
- 'language': 'en',
- 'd3DoF': false,
- 'd6DoF': false,
- 'streamMsg': false,
- 'multisocket': false
- }
- let conf = JSON.parse(localStorage.getItem('lcs_config'));
- let config = conf ? conf : {};
- if(conf){
- Object.keys(configDefaults).forEach(el => {
- config[el] = config[el] ? config[el] : configDefaults[el]
- })
- } else {
- Object.assign(config, configDefaults);
- }
- localStorage.setItem('lcs_config', JSON.stringify(config));
- this.config = config;
- let webrtcConnection = this.config.webrtc;
- const opt = { peers: this.dbHost, localStorage: false, multicast: false, RTCPeerConnection: webrtcConnection, axe: false } //localStorage: false,
- //const opt = { peers: this.dbHost, localStorage: false, until: 1000, chunk: 5, axe: false} //until: 5000, chunk: 5
- //opt.store = RindexedDB(opt);
- this.db = Gun(opt);
- //In production
- //Gun.log.off = true;
- //window._LCS_SYS_USER = undefined;
- window._LCSDB = this.db;
- window._LCS_WORLD_USER = undefined;
- _LCSDB.on('hi', function (peer) {
- let msg = peer.url ? 'Connected to DB at: ' + peer.url : 'Peer added!';
- let noty = new Noty({
- text: msg,
- timeout: 100,
- theme: 'mint',
- layout: 'bottomRight',
- type: 'success'
- });
- if (peer.url) {
- noty.show();
- }
- console.log(msg)
- })
- _LCSDB.on('bye', function (peer) {
- let msg = 'No connection to ' + peer.url;
- let noty = new Noty({
- text: msg,
- timeout: 100,
- theme: 'mint',
- layout: 'bottomRight',
- type: 'error'
- });
- noty.show();
- console.log(msg)
- })
- }
- clearLocalStorage() {
- let config = localStorage.getItem('lcs_config');
- let langConfig = localStorage.getItem('krestianstvo_locale');
- let manualConfig = localStorage.getItem('lcs_app_manual_settings');
- let lcsappConfig = localStorage.getItem('lcs_app');
- localStorage.clear();
- if (config)
- localStorage.setItem('lcs_config', config);
- if (langConfig)
- localStorage.setItem('krestianstvo_locale', langConfig);
- if (manualConfig)
- localStorage.setItem('lcs_app_manual_settings', manualConfig);
- if (lcsappConfig)
- localStorage.setItem('lcs_app', lcsappConfig);
- }
- setupLoadScreen(){
- let opts = {
- lines: 13,
- length: 5,
- width: 14,
- radius: 42,
- scale: 1,
- corners: 1,
- color: '#CCC',
- opacity: 0.25,
- rotate: 0,
- direction: 1,
- speed: 1,
- trail: 60,
- fps: 20,
- zIndex: 2e9,
- className: 'spinner',
- top: '50%',
- left: '50%',
- shadow: false,
- hwaccel: false,
- position: 'absolute',
- }
- let el = document.getElementById('loadscreen');
- this.spinner = new Spinner(opts).spin(el);
- }
- initUser() {
- function recall() {
- _LCSDB.user().recall({ sessionStorage: 1 }, res => {
- console.log('User is: ', _LCSDB.user().is);
- if (_LCSDB.user().is)
- _app.helpers.checkUserCollision();
- });
- }
- setTimeout(
- recall, 1000)
- }
- get streamMsg(){
- return this.config.streamMsg
- }
- set streamMsg(value){
- this.config.streamMsg = value
- }
- get isLuminary() {
- return this.config.luminary;
- }
- get isLuminaryGlobalHB() {
- return this.config.luminaryGlobalHB;
- }
- get isWebRTC() {
- return this.config.webrtc;
- }
- get luminaryGlobalHBPath() {
- var res = "";
- let config = localStorage.getItem('lcs_config');
- if (config) {
- res = JSON.parse(config).luminaryGlobalHBPath;
- }
- return res;
- }
- get luminaryPath() {
- var res = "";
- let config = localStorage.getItem('lcs_config');
- if (config) {
- res = JSON.parse(config).luminaryPath;
- }
- return res;
- }
- get reflectorHost() {
- var res = "";
- let config = localStorage.getItem('lcs_config');
- if (config) {
- res = JSON.parse(config).reflector;
- }
- return res;
- }
- get dbHost() {
- var res = "";
- let config = localStorage.getItem('lcs_config');
- if (config) {
- res = JSON.parse(config).dbhost;
- }
- return res;
- }
- async loadProxyDefaults() {
- // Optional req from server
- // load to DB default proxy files (VWF & A-Frame components)
- let proxyResponse = await fetch('/proxy-files', { method: 'get' });
- let proxyFiles = await proxyResponse.json();
- let filterProxyFiles = proxyFiles.filter(el => (el !== null));
- console.log(filterProxyFiles);
- var origin = window.location.origin;
- //var userPub = this.db.user().is.pub;
- let proxyObj = {};
- for (var index in filterProxyFiles) {
- let doc = filterProxyFiles[index];
- if (doc) {
- var url = origin + doc;
- var entryName = url.replace(origin + '/defaults/', "").split(".").join("_");
- let proxyFile = await fetch(url, { method: 'get' });
- let responseText = await proxyFile.text();
- if (responseText) {
- proxyObj[entryName] = responseText;
- }
- }
- }
- console.log(proxyObj);
- let proxy = _LCSDB.user().get('proxy');
- proxy.put(proxyObj);
- }
- async loadWorldsDefaults(replace) {
- // Optional req from server
- //load to DB default worlds
- let worldsResponse = await fetch('/world-files', { method: 'get' });
- let worldFiles = await worldsResponse.json();
- let filterworldFiles = worldFiles.filter(el => (el !== null));
- console.log(filterworldFiles);
- let worldsObj = {};
- for (var index in filterworldFiles) {
- let doc = filterworldFiles[index];
- if (doc) {
- let url = window.location.origin + doc;
- var entryName = url.replace(window.location.origin + '/defaults/worlds/', "").split(".").join("_");
- let worldName = entryName.split("/")[0];
- let userPub = _LCSDB.user().is.pub;
- let worldFile = await fetch(url, { method: 'get' });
- let worldSource = await worldFile.text();
- if (worldSource) {
- //let modified = new Date().valueOf();
- let created = new Date().valueOf();
- if (!worldsObj[worldName]) {
- worldsObj[worldName] = {
- 'parent': '-',
- 'owner': userPub,
- 'featured': true,
- 'published': true,
- 'proxy': userPub,
- 'created': created,
- 'modified': created
- }
- }
- let entry = entryName.replace(worldName + '/', "");
- worldsObj[worldName][entry] = worldSource
- }
- }
- }
- console.log(worldsObj);
- if (replace) {
- //force replace all default worlds
- let worlds = _LCSDB.user().get('worlds');
- worlds.put(worldsObj);
- } else {
- Object.entries(worldsObj).forEach(res => {
- let worldName = res[0];
- let files = res[1];
- Object.entries(files).forEach(file => {
- _LCSDB.user().get('worlds').get(worldName).get(file[0]).not(function (res) {
- _LCSDB.user().get('worlds').get(worldName).get(file[0]).put(file[1]);
- })
- })
- })
- }
- }
- async loadEmptyDefaultProto() {
- //empty proto world
- let userPub = _LCSDB.user().is.pub;
- let worldsObj = {};
- let emptyWorld = {
- "index_vwf_json": JSON.stringify(
- {
- "extends": "proxy/aframe/ascene.vwf"
- }, null, 4),
- "index_vwf_config_json": JSON.stringify(
- {
- "info": {
- "title": "Empty World"
- },
- "model": {
- "/drivers/model/aframe": null,
- "/drivers/model/aframeComponent": null
- },
- "view": {
- "/drivers/view/aframe": null,
- "/drivers/view/aframeComponent": null,
- "/drivers/view/editor": null
- }
- }, null, 4),
- "assets_json": JSON.stringify({}),
- "index_vwf_html": JSON.stringify("<!-- DEFAULT HTML -->"),
- "appui_js": JSON.stringify("//appui in JS"),
- "info_json": JSON.stringify({
- "info": {
- "en": {
- "title": "Empty World",
- "imgUrl": "",
- "text": "Empty World"
- },
- "ru": {
- "title": "Новый Мир",
- "imgUrl": "",
- "text": "Новый Мир"
- }
- }
- }, null, 4)
- }
- let created = new Date().valueOf();
- worldsObj['empty'] = {
- 'parent': '-',
- 'owner': userPub,
- 'featured': true,
- 'published': true,
- 'proxy': userPub,
- 'created': created,
- 'modified': created
- }
- Object.keys(emptyWorld).forEach(el => {
- worldsObj['empty'][el] = emptyWorld[el];
- })
- console.log(worldsObj);
- Object.entries(worldsObj).forEach(el => {
- let worldName = el[0];
- let files = el[1];
- Object.entries(files).forEach(file => {
- _LCSDB.user().get('worlds').get(worldName).get(file[0]).put(file[1]);
- })
- })
- }
- HandleDebugIndex() {
- //window._app.hideUIControl();
- _app.generalIndex().then(r=>{
- _app.hideProgressBar();
- let el = document.createElement("div");
- el.setAttribute("id", "appGUI");
- document.body.appendChild(el);
- _cellWidgets.debugGUI();
- })
- }
- HandleSettingsIndex() {
- window._app.hideProgressBar();
- //window._app.hideUIControl();
- _app.generalIndex().then(res=>{
- let el = document.createElement("div");
- el.setAttribute("id", "appGUI");
- document.body.appendChild(el);
- _cellWidgets.reflectorGUI();
- })
- }
- HandleWorldAbout(ctx) {
- console.log("about world");
- let userAlias = ctx.params.user;
- let worldName = ctx.params.space;
- let saveName = ctx.params.savename;
- window._app.hideProgressBar();
- //window._app.hideUIControl();
- _app.generalIndex().then(res=>{
- if (!_app.indexApp) {
- _app.indexApp = new IndexApp;
- }
- let worldApp = new WorldApp(userAlias, worldName, saveName);
- _app.helpers.getUserPub(userAlias).then(res => {
- worldApp.makeGUI(res)
- })
- })
- }
- HandleSetupIndex() {
- window._app.hideProgressBar();
- //window._app.hideUIControl();
- _LCSDB.on('auth',
- function (ack) {
- let el = document.createElement("div");
- el.setAttribute("id", "admin");
- document.body.appendChild(el);
- if (_LCSDB.user().is) {
- let adminComponents = [];
- document.querySelector("#admin").$cell({
- $cell: true,
- id: 'admin',
- $type: "div",
- $components: adminComponents,
- $update: function () {
- this.$components = adminComponents
- }
- });
- document.querySelector("#admin").$update();
- if (_LCSDB.user().is) {
- let loadEmpty = {
- $cell: true,
- $components: [
- {
- $type: "p",
- class: "mdc-typography--headline5",
- $text: "Initialize empty World proto"
- },
- {
- $type: "button",
- id: "loadDefaults",
- class: "mdc-button mdc-button--raised",
- $text: "Init empty world",
- onclick: function (e) {
- console.log("admin action");
- window._app.loadEmptyDefaultProto();
- }
- }
- ]
- }
- let loadDefaults = {
- $cell: true,
- _replaceSwitch: null,
- $components: [
- {
- $type: "p",
- class: "mdc-typography--headline5",
- $text: "Load Sample Worlds protos from server (optional)"
- },
- {
- $type: "button",
- id: "loadDefaults",
- class: "mdc-button mdc-button--raised",
- $text: "Load default worlds (from server)",
- onclick: function (e) {
- console.log("admin action");
- let forceReplace = this._replaceSwitch.checked;
- //console.log(forceReplace);
- window._app.loadWorldsDefaults(forceReplace);
- }
- },
- {
- $type: 'p'
- },
- _cellWidgets.switch({
- 'id': 'forceReplace',
- 'init': function () {
- this._switch = new mdc.switchControl.MDCSwitch(this);
- this._replaceSwitch = this._switch;
- this._switch.checked = false;
- }
- }
- ),
- {
- $type: 'label',
- for: 'input-forceReplace',
- $text: 'Force replace'
- }
- ]
- }
- let loadDefaultsProxy = {
- $cell: true,
- $components: [
- {
- $type: "p",
- class: "mdc-typography--headline5",
- $text: "Load VWF & A-Frame default components"
- },
- {
- $type: "button",
- class: "mdc-button mdc-button--raised",
- $text: "Load defaults Proxy",
- onclick: async function (e) {
- console.log("admin action");
- await window._app.loadProxyDefaults();
- }
- }
- ]
- }
- adminComponents.push(loadDefaultsProxy, loadEmpty, loadDefaults);
- document.querySelector("#admin").$update();
- }
- }
- })
- }
- //TODO: profile
- HandleUserIndex(ctx) {
- console.log("USER INDEX");
- window._app.hideProgressBar();
- //window._app.hideUIControl();
- _app.generalIndex().then(r=>{
- import('/web/header.js').then(res => {
- let gui = new res.Header();
- gui.init();
- })
- _LCSDB.on('auth',
- async function (ack) {
- if (ack.sea.pub) {
- _app.helpers.checkUserCollision();
- let alias = _LCSDB.user().is.alias;
- let pub = _LCSDB.user().is.pub;
- document.querySelector("#profile")._refresh(
- {
- user: {
- alias: alias,
- pub: pub
- }
- }
- );
- }
- })
- let el = document.createElement("div");
- el.setAttribute("id", "userProfile");
- document.body.appendChild(el);
- let dragDropWorldsArea = {
- $cell: true,
- $type: 'div',
- id: "ddWorlds",
- class: 'dragdropArea',
- _ddText: '',
- _refresh: function (aText) {
- this._ddText = aText;
- },
- $init: function () {
- console.log('init d&d area for worlds protos');
- this._refresh('Drag & Drop a folder with world files here...');
- let self = this;
- DragDrop("#ddWorlds",
- {
- onDrop: function (files, pos, fileList, directories) {
- console.log('onDrop: ' + files.length + ' files at ' + pos.x + ', ' + pos.y);
- //let worldsObj = {};
- let worlds = _LCSDB.user().get('worlds');
- files.forEach(function (file) {
- let world = {};
- if ((file.name.indexOf('.yaml') !== -1) ||
- (file.type == "text/javascript") ||
- (file.type == "text/html") ||
- (file.type == "application/json")) {
- console.log('- ' + file.name + ' (' + file.size + ') (' + file.type + ')');
- // convert the file to a Buffer that we can use!
- const reader = new FileReader()
- reader.addEventListener('load', e => {
- // e.target.result is an ArrayBuffer
- const arr = new Uint8Array(e.target.result)
- const fileBuffer = new buffer.Buffer(arr);
- const fileSource = fileBuffer.toString();
- // do something with the buffer!
- var entryName = file.fullPath.slice(1).split(".").join("_");
- let worldName = entryName.split("/")[0];
- let userPub = _LCSDB.user().is.pub;
- //let modified = new Date().valueOf();
- let created = new Date().valueOf();
- world[worldName] = {
- 'parent': '-',
- 'owner': userPub,
- 'featured': true,
- 'published': true,
- 'proxy': userPub,
- 'created': created,
- 'modified': created
- }
- let entry = entryName.replace(worldName + '/', "");
- world[worldName][entry] = fileSource;
- console.log(world);
- worlds.put(world);
- })
- reader.addEventListener('error', err => {
- console.error('FileReader error' + err)
- })
- reader.readAsArrayBuffer(file)
- }
- })
- //console.log('Worlds', worldsObj);
- console.log('files array', files)
- console.log('FileList object', fileList)
- console.log('directories array', directories)
- self._refresh(directories.map(el => { return el.name }).toString());
- },
- onDropText: function (text, pos) {
- console.log('onDropText: ' + text + ' at ' + pos.x + ', ' + pos.y)
- }
- }
- )
- },
- $update: function () {
- this.$components = [
- {
- $type: "h5",
- class: "mdc-typography--headline5",
- $text: this._ddText
- }
- ]
- }
- }
- let dragDropProxyArea = {
- $cell: true,
- $type: 'div',
- id: "ddProxy",
- class: 'dragdropArea',
- _ddText: '',
- _refresh: function (aText) {
- this._ddText = aText;
- },
- $init: function () {
- console.log('init d&d area for proxy files');
- this._refresh('Drag & Drop a folder with proxy files here...');
- let self = this;
- DragDrop("#ddProxy",
- {
- onDrop: function (files, pos, fileList, directories) {
- console.log('onDrop: ' + files.length + ' files at ' + pos.x + ', ' + pos.y);
- //let worldsObj = {};
- let proxy = _LCSDB.user().get('proxy').put({ id: 'proxy' });
- files.forEach(function (file) {
- let proxyObj = {};
- if ((file.name.indexOf('.yaml') !== -1) ||
- (file.type == "text/javascript") ||
- (file.type == "text/html") ||
- (file.type == "application/json")) {
- console.log('- ' + file.name + ' (' + file.size + ') (' + file.type + ')');
- // convert the file to a Buffer that we can use!
- const reader = new FileReader()
- reader.addEventListener('load', e => {
- // e.target.result is an ArrayBuffer
- const arr = new Uint8Array(e.target.result)
- const fileBuffer = new buffer.Buffer(arr);
- const fileSource = fileBuffer.toString();
- // do something with the buffer!
- var entryName = file.fullPath.slice(1).split(".").join("_");
- if (entryName.slice(0, 6) !== 'proxy/') {
- entryName = 'proxy/' + entryName
- }
- // let userPub = _LCSDB.user().is.pub;
- // let created = new Date().valueOf();
- // let obj = {
- // 'owner': userPub,
- // 'file': fileSource,
- // 'modified': created,
- // 'created': created
- // }
- proxyObj[entryName] = fileSource;
- console.log(proxyObj);
- proxy.put(proxyObj);
- })
- reader.addEventListener('error', err => {
- console.error('FileReader error' + err)
- })
- reader.readAsArrayBuffer(file)
- }
- })
- console.log('files array', files)
- console.log('FileList object', fileList)
- console.log('directories array', directories)
- self._refresh(directories.map(el => { return el.name }).toString());
- },
- onDropText: function (text, pos) {
- console.log('onDropText: ' + text + ' at ' + pos.x + ', ' + pos.y)
- }
- }
- )
- },
- $update: function () {
- this.$components = [
- {
- $type: "h5",
- class: "mdc-typography--headline5",
- $text: this._ddText
- }
- ]
- }
- }
- let loadDefaultsProxy = {
- $type: 'div',
- $components: [
- {
- $type: "button",
- class: "mdc-button mdc-button--raised",
- $text: "Load default Proxy from LiveCoding.space server",
- onclick: async function (e) {
- console.log("user action");
- await window._app.loadProxyDefaults();
- }
- }
- ]
- }
- let loadEmpty = {
- $type: 'div',
- $components: [
- {
- $type: "button",
- id: "loadDefaults",
- class: "mdc-button mdc-button--raised",
- $text: "Init empty world",
- onclick: function (e) {
- console.log("user action");
- window._app.loadEmptyDefaultProto();
- }
- }
- ]
- }
- let proxySource =
- {
- $type: "div",
- id: "tree_proxy",
- _tree: [],
- _treeComp: {},
- $init: function () {
- let selfComp = this;
- _LCSDB.user().get('proxy').load(res => {
- // console.log(res);
- if (res) {
- selfComp._tree = [{
- name: 'Proxy sources: ',
- children: []
- }];
- Object.keys(res).filter(el => el.includes('_js') || el.includes('_json')).forEach(el => {
- selfComp._tree[0].children.push({
- name: el
- })
- })
- selfComp._treeComp = new TreeView(selfComp._tree, 'tree_proxy');
- selfComp._treeComp.on('select', function (evt) {
- console.log(evt);
- //window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/' + evt.data.name
- });
- }
- })
- },
- $components: [
- ]
- }
- let userProfile = {
- $type: 'div',
- id: "profile",
- _user: {},
- _refresh: function (data) {
- this._user = data.user;
- },
- $init: function () {
- this._user = { alias: "", pub: "" }
- },
- $update: function () {
- if (_LCSDB.user().is) {
- this.$components = [
- {
- $type: "div",
- class: "mdc-layout-grid",
- $components: [
- {
- $type: "div",
- class: "mdc-layout-grid__inner",
- $components: [
- {
- $type: "div",
- class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
- $components: [
- {
- $type: "h5",
- class: "mdc-typography--headline4 unselectable",
- $text: "User alias: " + this._user.alias //"Profile for: " + this.db.user().is.alias
- },
- {
- $type: "h5",
- class: "mdc-typography--headline5 unselectable",
- $text: "User public key: " + this._user.pub//"Profile for: " + this.db.user().is.alias
- },
- ]
- },
- {
- $type: "div",
- class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
- $components: [
- {
- $type: "h3",
- class: "mdc-typography",
- $text: 'Load my world\'s protos:' //"Profile for: " + this.db.user().is.alias
- },
- dragDropWorldsArea, _app.widgets.emptyDiv,
- {
- $text: 'or'
- },
- _app.widgets.p,
- loadEmpty
- ]
- },
- {
- $type: "div",
- class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
- $components: [
- {
- $type: "h3",
- class: "mdc-typography",
- $text: 'Load proxy files:' //"Profile for: " + this.db.user().is.alias
- },
- dragDropProxyArea,
- _app.widgets.emptyDiv,
- {
- $text: 'or'
- },
- _app.widgets.p,
- loadDefaultsProxy,
- _app.widgets.p,
- _app.widgets.divider,
- proxySource
- ]
- }
- ]
- }
- ]
- }
- ]
- } else {
- this.$components = [
- {
- $type: "h3",
- class: "mdc-typography--headline3",
- $text: "user is not signed in..." //"Profile for: " + this.db.user().is.alias
- },
- _app.widgets.divider
- ]
- }
- }
- }
- document.querySelector("#userProfile").$cell({
- $cell: true,
- $type: "div",
- $components: [userProfile]
- })
- })
- }
- HandleUserWorlds(ctx) {
- console.log("USER WORLDS INDEX");
- console.log(ctx.params);
- let user = ctx.params.user;
- let type = ctx.params.type;
- window._app.hideProgressBar();
- // window._app.hideUIControl();
- _app.generalIndex().then(r=>{
- if (!_app.indexApp) {
- _app.indexApp = new IndexApp;
- }
- _app.indexApp.allWorldsForUser(user)
- })
- }
- HandleFileEdit(ctx) {
- console.log("USER WORLD FILE EDIT");
- let user = ctx.params.user;
- let worldName = ctx.params.name;
- let fileOriginal = ctx.params.file;
- let type = ctx.params.type;
- window._app.hideProgressBar();
- //window._app.hideUIControl();
- _app.generalIndex().then(r=>{
- _LCSDB.on('auth',
- async function (ack) {
- if (_LCSDB.user().is) {
- _app.helpers.checkUserCollision();
- if (_LCSDB.user().is.alias == user) {
- var worldType = 'worlds';
- var file = fileOriginal;
- if (type == 'state') {
- worldType = 'documents';
- file = _app.helpers.replaceSubStringALL(fileOriginal, "~", '/');
- }
- _LCSDB.user().get(worldType).get(worldName).get(file).load(worldFile => {
- if (worldFile) {
- var source = worldFile;
- if (type == 'state') {
- if (!file.includes('_info_vwf_json')) {
- source = worldFile.jsonState;
- var saveName = worldFile.filename;
- }
- }
- //console.log(source);
- source = _app.helpers.convertFileSource(file, source);
- let el = document.createElement("div");
- el.setAttribute("id", "worldFILE");
- document.body.appendChild(el);
- var saveGUI = {};
- if (type == 'proto' || file.includes('_info_vwf_json')) {
- saveGUI = {
- $type: "button",
- class: "mdc-button mdc-button--raised",
- $text: "Save",
- onclick: async function (e) {
- console.log("save new info");
- let editor = document.querySelector("#aceEditor").env.editor;
- let newInfo = editor.getValue();
- _LCSDB.user().get(worldType).get(worldName).get(file).put(newInfo, function (res) {
- if (res) {
- _app.helpers.notyOK('Saved!');
- // let modified = new Date().valueOf();
- // _LCSDB.user().get(worldType).get(worldName).get(file).get('modified').put(modified);
- }
- })
- }
- }
- }
- let aceEditorCell = {
- $type: "div",
- $components: [
- {
- class: "aceEditor",
- id: "aceEditor",
- //style: "width:1200px; height: 800px",
- $type: "div",
- $text: source,
- $init: function () {
- var mode = "ace/mode/json";
- if (file.includes('_yaml'))
- mode = "ace/mode/yaml"
- if (file.includes('_js'))
- mode = "ace/mode/javascript"
- var editor = ace.edit("aceEditor");
- editor.setTheme("ace/theme/monokai");
- editor.setFontSize(16);
- editor.getSession().setMode(mode);
- editor.setOptions({
- maxLines: Infinity
- });
- editor.session.setUseWrapMode(true);
- }
- },
- saveGUI,
- {
- $type: "button",
- class: "mdc-button mdc-button--raised",
- $text: "Close",
- onclick: function (e) {
- console.log("close");
- //window.location.pathname = "/" + user + '/' + worldName + '/about'
- window.history.back();
- // if (type == "proto")
- // window.location.pathname = "/" + user + '/' + worldName + '/about'
- // if (type == "state")
- // window.location.pathname = "/" + user + '/' + worldName + '/about'
- }
- }
- ]
- }
- document.querySelector("#worldFILE").$cell({
- $cell: true,
- $type: "div",
- $components: [aceEditorCell
- ]
- })
- }
- });
- }
- }
- })
- })
- }
- async generateFrontPage() {
- let infoEl = document.createElement("div");
- infoEl.setAttribute("id", "indexPage");
- //infoEl.classList.add("mdc-typography");
- let lang = _LangManager.locale;
- let infoElHTML = await _app.helpers.getHtmlText('/web/locale/' + lang + '/index.html');
- infoEl.innerHTML = infoElHTML;
- document.body.appendChild(infoEl);
- }
- async loadIndexLibs() {
- return loadjs([
- '/lib/ui/cell.min.js',
- '/lib/ui/treeview/treeview.min.css',
- '/lib/ui/treeview/treeview.min.js',
- '/lib/ui/mdc/dist/material-components-web.min.css',
- '/lib/ui/mdc/dist/material-components-web.min.js',
- '/lib/ui/mdc-fonts.css',
- '/lib/ui/mdc.css',
- '/lib/ui/ace/ace.js',
- '/lib/ui/drag-drop.js',
- '/lib/buffer5.6.0.min.js',
- ], {
- async: false,
- returnPromise: true
- })
- }
- generalIndex() {
- let p = new Promise(res => res())
- .then(res=>{
- return _app.loadIndexLibs();
- })
- .then(res=>{
- document.querySelector('body').classList.add("mdc-typography");
- mdc.autoInit();
- })
- return p
- }
- ////////HANDLERS//////
- HandleQuery(ctx, next) {
- ctx.query = qs.parse(location.search.slice(1));
- next();
- }
- HandleIndex() {
- console.log("INDEX");
- //window._app.hideUIControl();
- _app.generateFrontPage();
- _app.generalIndex().then(res=>{
- if (!_app.indexApp) {
- _app.indexApp = new IndexApp('index');
- _app.hideProgressBar();
- }
- })
- }
- HandleNoPage() {
- console.log("no such page");
- _app.hideProgressBar();
- let t404 = `
- <h1>404 no page...</h1>
- `;
- let el = document.getElementById("loadscreen");
- el.innerHTML = t404;
- }
- //handle parcable requests
- HandleParsableLoadRequest(ctx) {
- let app = window._app;
- console.log(ctx.params);
- //var pathname = ctx.pathname;
- var spaceName = ctx.params.space;
- var saveName = ctx.params.savename;
- let user = ctx.params.user;
- page.redirect('/' + user + '/' + spaceName + '/' + app.helpers.GenerateInstanceID() + '/load/' + saveName);
- }
- HandleParsableLoadRequestWithRev(ctx) {
- let app = window._app;
- console.log(ctx.params);
- //var pathname = ctx.pathname;
- var spaceName = ctx.params.space;
- var saveName = ctx.params.savename;
- var rev = ctx.params.rev;
- let user = ctx.params.user;
- page.redirect('/' + user + '/' + spaceName + '/' + app.helpers.GenerateInstanceID() + '/load/' + saveName + '/' + rev);
- }
- HandleParsableRequestGenID(ctx) {
- let app = window._app;
- console.log(ctx.params);
- let user = ctx.params.user;
- let space = ctx.params.space;
- var pathname = ctx.pathname;
- //await app.setUserPaths(user);
- _app.helpers.getUserPub(user).then(function (res) {
- if (res)
- window._LCS_WORLD_USER = {
- alias: user,
- pub: res
- }
- if (pathname[pathname.length - 1] == '/') {
- pathname = pathname.slice(0, -1)
- }
- let pathToParse = '/' + space; // pathname.replace('/' + user, "");
- let parsedRequest = {
- application: "index.vwf",
- instance: undefined,
- private_path: undefined,
- public_path: pathToParse
- }
- localStorage.setItem('lcs_app', JSON.stringify({ path: parsedRequest }));
- console.log(parsedRequest);
- if ((parsedRequest['instance'] == undefined) && (parsedRequest['private_path'] == undefined) && (parsedRequest['public_path'] !== "/") && (parsedRequest['application'] !== undefined)) {
- //page.redirect(pathname + '/' + app.helpers.GenerateInstanceID());
- window.location.pathname = pathname + '/' + app.helpers.GenerateInstanceID()
- }
- })
- }
- generateInstanceID(){
- window.location.pathname = pathname + '/' + app.helpers.GenerateInstanceID()
- }
- HandleParsableRequestWithID(ctx) {
- let app = window._app;
- console.log(ctx.params);
- var pathname = ctx.pathname;
- let user = ctx.params.user;
- let space = ctx.params.space;
- let savename = ctx.params.savename;
- //let genID = ctx.params.id;
- let query = ctx.query;
- var genID = undefined;
- if(Object.keys(query).length > 0){
- if(query.k){
- genID = query.k
- }
- }
- if (!genID){
- genID = app.helpers.GenerateInstanceID();
- let urlAddon = '?k=' + genID;
- let state = {
- path: ctx.pathname + urlAddon
- }
- window.history.replaceState(state, space, window.location.href + urlAddon);
- }
-
- if (pathname[pathname.length - 1] == '/') {
- pathname = pathname.slice(0, -1)
- }
- let vwfApp = {}
- //await app.setUserPaths(user);
- _app.helpers.getUserPub(user).then(function (res) {
- if (res)
- window._LCS_WORLD_USER = {
- alias: user,
- pub: res
- }
- }).then(res => {
- let pathToParse = '/' + space; //pathname.replace('/' + user, "");
- let parsedRequest = {
- application: "index.vwf",
- instance: genID,
- private_path: undefined,
- public_path: pathToParse
- }
- if (savename) {
- parsedRequest.private_path = 'load/' + savename;
- }
- localStorage.setItem('lcs_app', JSON.stringify({ path: parsedRequest }));
- return parsedRequest
- }).then(pr => {
- let cpath = pr.public_path;
- return new Promise(res => _LCSDB.user(_LCS_WORLD_USER.pub).get('worlds').get(cpath.slice(1)).load(res)) //, { wait: 400 }
- }).then(val => {
- let fileConf = val['index_vwf_config_json'];
- vwfApp.conf = {};
- if (fileConf) {
- let config = JSON.parse(fileConf);
- vwfApp.conf = config
- } else {
- vwfApp.conf =
- {
- "info":{
- "title": "LCS Application"
- },
- "model": {},
- "view": {}
- }
- }
- let infoFile = val['info_json'];
- let infoSettings = infoFile ? JSON.parse(infoFile).info.settings: null;
- if(infoSettings){
- let manualSettings = localStorage.getItem('lcs_app_manual_settings');
- if (manualSettings) {
- let manualConf = JSON.parse(manualSettings);
- vwfApp.conf.model = manualConf.model;
- vwfApp.conf.view = manualConf.view;
- }
- }
- //check & set default proxy for world
- vwfApp.proxy = val.proxy ? val.proxy : _LCS_WORLD_USER.pub
- // Try to load all required docs from Gun DB...
- let promises = []
- let worldPromise = new Promise(res => _LCSDB.user(_LCS_WORLD_USER.pub).get('worlds').get(space).load(res));
- promises.push(worldPromise);
- if (savename) {
- let entryPath = 'savestate_/' + space + '/' + savename + '_vwf_json';
- let savePromise = new Promise(res => _LCSDB.user(_LCS_WORLD_USER.pub).get('documents').get(space).path(entryPath).load(res, { wait: 400 }));
- promises.push(savePromise);
- }
- let proxyPromise = new Promise(res => _LCSDB.user(vwfApp.proxy).get('proxy').load(res, { wait: 400 }));
- promises.push(proxyPromise);
- return Promise.all(promises)
- }).then(res => {
- //Load vwf_view Document
- let dbPath = _app.helpers.appName + '_js';
- vwfApp.doc = res[0][dbPath];
- //Load user model Document
- // let modelDBPath = 'modelDriver_js';
- // vwfApp.modelDoc = res[0][modelDBPath];
- //Load libs for selected config drivers
- let libs = app.getLibsForConfig(vwfApp.conf);
- if(libs.length !== 0)
- return app.loadAppLibs(libs);
-
- return 'nodriver'
- })
- .then(res => {
- //Load VWF libs
- return app.loadVWF();
- }).then(res => {
- let connectionConf = {
- luminary: _app.isLuminary,
- luminaryGlobalHB: _app.isLuminaryGlobalHB,
- luminaryGlobalHBPath: _app.luminaryGlobalHBPath
- }
- //Load main VWF app
- window.vwf = new VWF(vwfApp.conf, vwfApp.proxy, vwfApp.doc, connectionConf)
- let userLibraries = { model: {}, view: {} };
- let application = undefined;
- vwf.loadConfiguration(application, userLibraries, null);
- })
- }
- getLibsForConfig(conf){
- //Load not ES6 standard libs before driver loads
- //Use import in drivers by default
- const confLibsDefaults = {
- '/drivers/model/aframe':[
- '/drivers/model/aframe/aframe-master.min.js',
- '/drivers/model/aframe/addon/SkyShader.js',
- '/drivers/model/aframe/addon/BVHLoader.js',
- '/drivers/model/aframe/addon/TransformControls.js',
- '/drivers/model/aframe/addon/THREE.MeshLine.js',
- '/drivers/model/aframe/addon/three/BufferGeometryUtils.js',
- '/drivers/model/aframe/addon/virtualgc/virtual-gamepad-controls.css',
- '/drivers/model/aframe/addon/virtualgc/nipplejs.js',
- '/drivers/model/aframe/addon/aframe-sun-sky.min.js',
- '/drivers/model/aframe/extras/aframe-extras.loaders.min.js',
- '/drivers/model/aframe/extras/aframe-extras.controls.min.js',
- '/drivers/model/aframe/addon/aframe-teleport-controls.js',
- '/drivers/model/aframe/kframe/aframe-aabb-collider-component.min.js',
- '/drivers/model/aframe/addon/aframe-interpolation.js',
- '/drivers/model/aframe/addon/aframe-components.js'
- ],
- '/drivers/view/webrtc': [
- '/drivers/view/webrtc/adapter-latest.js'
- ],
- '/drivers/view/editor': [
- '/lib/ui/cell.min.js',
- '/lib/ui/treeview/treeview.min.css',
- '/lib/ui/treeview/treeview.min.js',
- '/lib/ui/mdc/dist/material-components-web.min.css',
- '/lib/ui/mdc/dist/material-components-web.min.js',
- '/lib/ui/mdc-fonts.css',
- '/lib/ui/mdc.css',
- '/lib/ui/ace/ace.js',
- '/lib/ui/screenfull/screenfull.min.js',
- '/lib/ui/drag-drop.js',
- '/lib/buffer5.6.0.min.js',
- '/lib/ui/qrcode.min.js',
- '/drivers/view/editor/draggabilly/draggabilly.pkgd.js',
- '/drivers/view/editor/colorpicker/colorpicker.min.js',
- '/drivers/view/editor/colorpicker/themes.css',
- '/drivers/view/editor/editorLive.css'
- ],
- '/drivers/view/aframe-ar-driver':[
- '/drivers/view/arjs/aframe-ar.js'
- ],
- '/drivers/view/lego-boost':[
- '/drivers/view/lego-boost/bundle.js'
- ],
- '/drivers/view/osc':[
- '/drivers/view/oscjs/osc-browser.min.js'
- ]
- }
- var appLibs = [];
- Object.keys(conf.model).concat(Object.keys(conf.view)).forEach(el => {
- let driver = confLibsDefaults[el];
- if(driver)
- appLibs = appLibs.concat(confLibsDefaults[el])
- });
- return appLibs
- }
- async loadVWF() {
- return loadjs([
- '/lib/async.min.js',
- '/lib/qheap.js',
- '/lib/lively.vm_standalone.js',
- '/lib/crypto.js',
- '/lib/md5.js',
- '/lib/alea.js',
- '/lib/mash.js'
- ], {
- async: false,
- returnPromise: true
- })
- }
- async loadAppLibs(libs) {
- return loadjs(libs, {
- async: false,
- returnPromise: true
- });
- }
- //get DB application state information for reflector (called from VWF)
- async getApplicationState() {
- let dataJson = JSON.parse(localStorage.getItem('lcs_app'));
- if (dataJson) {
- if (!dataJson.path['instance']) return undefined;
- }
- let userAlias = _LCS_WORLD_USER.alias;
- let parsedRequest = dataJson.path;
- if (parsedRequest['private_path']) {
- var loadInfo = {
- application_path: parsedRequest.public_path,
- save_name: parsedRequest.private_path.split('/')[1]
- }
- var saveInfo = await this.loadSaveObject(loadInfo);
- }
- let loadObj = {
- loadInfo: loadInfo ? loadInfo : {},
- path: dataJson.path,
- saveObject: saveInfo,
- user: userAlias
- }
- localStorage.setItem('lcs_app', JSON.stringify(loadObj));
- console.log(loadObj);
- return loadObj
- }
- async loadSaveObject(loadInfo) {
- if (!loadInfo.save_name) {
- return undefined
- }
- let objName = "savestate_" + loadInfo['application_path'] + '/' + loadInfo['save_name'] + '_vwf_json';
- let worldName = this.helpers.appPath;
- let dbNode = _LCSDB.user(_LCS_WORLD_USER.pub).get('documents').get(worldName).get(objName);
- let saveObject = await new Promise(res => dbNode.load(res, { wait: 300 }));
- if (saveObject) {
- let saveInfo = (typeof (saveObject.jsonState) == 'object') ? saveObject.jsonState : JSON.parse(saveObject.jsonState);
- return saveInfo;
- }
- return undefined
- }
- async getProtoWorldFiles(userPub, worldName, date) {
- return (new Promise(res => _LCSDB.user(userPub).get('worlds').get(worldName).once(res))).then(res => {
- let worldFiles = Object.entries(res).filter(el => (el[0].includes('_json')) || (el[0].includes('_yaml')) || (el[0].includes('_html')) || (el[0].includes('_js')))
- .filter(el => (el[0] !== 'info_json'));
- console.log(worldFiles);
- return worldFiles
- });
- }
- get worldName() {
- return vwf.application().split('/')[1];
- }
- async cloneWorld(worldName, userName, name, currentState) {
- //if(worldName == name && userName == _LCSDB.user().is.alias
- var userPub = _LCSDB.user().is.pub;
- if (userName !== _LCSDB.user().is.alias) {
- userPub = await _app.helpers.getUserPub(userName);
- }
- let newOwner = _LCSDB.user().is.pub;
- let db = _LCSDB.user();
- let newWorldName = name ? name : 'world' + this.helpers.randId();
- let myWorlds = await (new Promise(res => db.get('worlds').load(res, { wait: 400 })));
- if (myWorlds) {
- let checkExist = Object.keys(myWorlds).filter(el => el == newWorldName);
- if (checkExist.length > 0 && myWorlds[newWorldName]) {
- _app.helpers.notyOK('World already exists!')
- return
- }
- }
- let created = new Date().valueOf();
- await _LCSDB.user(userPub).get('worlds').get(worldName).load(all => {
- let worldObj = Object.assign({}, all);
- worldObj.owner = newOwner;
- worldObj.parent = userName + '/' + worldName;
- worldObj.featured = true
- worldObj.published = true
- worldObj.created = created
- if (!all.proxy) {
- worldObj.proxy = userPub;
- } else {
- worldObj.proxy = all.proxy;
- }
- console.log(worldObj);
- let myWorld = _LCSDB.user().get('worlds').get(newWorldName).put({ id: newWorldName });
- myWorld.put(worldObj, function (res) {
- if (currentState) {
- _app.saveWorld(newWorldName);
- }
- _app.helpers.notyOK('World cloned!')
- });
- })
- }
- async saveWorld(wn) {
- let name = wn ? wn : _app.helpers.appPath;
- let proto = this.helpers.getWorldProto();
- _LCSDB.user().get('worlds').get(name).get('index_vwf_json').put(JSON.stringify(proto), res => {
- _app.helpers.notyOK('World current saved!')
- });
- }
- async saveState(filename) // invoke with the view as "this"
- {
- //var clients = this.nodes["http://vwf.example.com/clients.vwf"];
- // Save State Information
- //let filename = self.helpers.appPath;
- console.log("Saving: " + filename);
- var state = vwf.getState();
- state.nodes[0].children = {};
- var timestamp = state["queue"].time;
- timestamp = Math.round(timestamp * 1000);
- let jsonValuePure = _app.helpers.replaceFloatArraysInNodeDef(state);
- //remove all Ohm generated grammarsfrom state
- let jsonValue = _app.helpers.removeGrammarObj(jsonValuePure);
- var jsonState = JSON.stringify(jsonValue, null, '\t'); //JSON.stringify(jsonValue);
- let rootPath = this.helpers.getRoot();
- var inst = rootPath.inst;
- var root = rootPath.root;
- var json = jsonState;
- //var documents = this.db.user().get('documents');
- _LCSDB.user().get('documents').not(res => {
- _LCSDB.user().get('documents').put({ id: 'documents' })
- })
- var saveRevision = new Date().valueOf();
- var stateForStore = {
- "root": root,
- "filename": filename,
- "inst": inst,
- "timestamp": timestamp,
- "extension": ".vwf.json",
- "jsonState": json,
- "publish": true,
- "saveRevision": saveRevision
- };
- let stateObj = Object.assign({}, stateForStore);
- var docName = 'savestate_/' + root + '/' + filename + '_vwf_json';
- let myNewWorldState = _LCSDB.user().get('documents').get(root).get(docName).put({ 'id': docName });
- //_LCSDB.user().get('documents').get(root).get(docName).put(stateWithRev, function(res) {
- myNewWorldState.put(stateObj, function (res) {
- if (res) {
- _app.helpers.notyOK('Saved to ' + docName);
- }
- });
- }
- hideUIControl() {
- let el = document.getElementById("ui-controls");
- if (el) {
- el.classList.remove("visible");
- el.classList.add("not-visible");
- }
- }
- showUIControl() {
- let el = document.getElementById("ui-controls");
- if (el) {
- el.classList.remove("not-visible");
- el.classList.add("visible");
- }
- }
- hideProgressBar() {
- let el = document.getElementById('loadscreen');
- if(el){
- el.getElementsByClassName('spinner')[0].remove();
- //window._spinner.stop(false);
- }
- }
- showProgressBar() {
- let el = document.getElementById('loadscreen');
- _app.spinner.spin(el);
- }
- // SUPPORT of DELETE USER WORLDS & SAVE STATES (experimental)
- // TODO: manual garbage collection
- async deleteWorldState(worldName, stateName) {
- let db = _LCSDB.user();
- let stateEntry = 'savestate_/' + worldName + '/' + stateName + '_vwf_json';
- db.get('documents').get(worldName).get(stateEntry).put(null, res => { });
- }
- async deleteWorld(name, type) {
- let self = this;
- if (type == 'proto') {
- let worldName = name;
- //TODO check for states (ask for deleting all states first...)
- //delete states
- let db = _LCSDB.user();
- db.get('documents').once().map((res, k) => { if (k == worldName) return res }).once((res, k) => {
- if (res) {
- let worldStatesInfo = Object.entries(res).filter(el => el[0].includes('_info_vwf_json'));
- worldStatesInfo.map(el => {
- let saveName = el[0].split('/')[2].replace('_info_vwf_json', "");
- console.log(saveName);
- self.deleteWorldState(worldName, saveName)
- //let stateEntry = 'savestate_/' + k + '/' + saveName + '_vwf_json';
- })
- }
- })
- db.get('worlds').get(worldName).put(null, res => {
- let id = 'worldCard_' + _LCSDB.user().is.alias + '_' + worldName + '_';
- let doc = document.querySelector('#' + id);
- if (doc)
- doc._refresh({})
- })
- } else if (type == 'state') {
- let worldName = name.split('/')[0];
- let stateName = name.split('/')[2];
- await this.deleteWorldState(worldName, stateName);
- }
- app.helpers.notyOK("World Deleted!");
- }
- parseAppInstancesData(data) {
- let jsonObj = JSON.parse(data);
- var parsed = {};
- let listData = {};
- for (var prop in jsonObj) {
- var name = prop.split('/')[1];
- if (parsed[name]) {
- parsed[name][prop] = jsonObj[prop];
- } else {
- parsed[name] = {};
- parsed[name][prop] = jsonObj[prop];
- }
- }
- //console.log(parsed);
- for (var prop in parsed) {
- var name = prop;
- let obj = Object.entries(parsed[prop]);
- var lists = {};
- obj.forEach(el => {
- if (!lists[name])
- lists[name] = {};
- lists[name][el[0]] = el[1]
- });
- // console.log(lists);
- Object.entries(lists).forEach(list => {
- listData[list[0]] = list[1];
- })
- }
- return listData
- // console.log(data)
- }
- async getAllProtoWorldsInfoForUser(userAlias, worldName) {
- //let userPub = await new Promise(res => _LCSDB.get('users').get(userAlias).get('pub').once(res));
- let userPub = await _app.helpers.getUserPub(userAlias);
- var db = _LCSDB.user(userPub);
- if (_LCSDB.user().is) {
- if (_LCSDB.user().is.alias == userAlias)
- db = _LCSDB.user();
- }
- let list = await (new Promise(res => db.get('worlds').load(res, { wait: 400 })))
- .then(r => {
- if (!worldName) {
- return Promise.all(Object.keys(r).map(k => db.get('worlds').get(k).then(res => { return [k, res] })))
- } else {
- return Promise.all([db.get('worlds').get(worldName).then(res => { return [worldName, res] })])
- }
- }
- )
- .then(r => Promise.all(Object.keys(r).map(k => {
- let objEl = r[k][1];
- if (objEl) {
- let obj = objEl.info_json;
- if (obj) {
- return _LCSDB.get(obj["#"]).then(res => { return { world: r[k], info: res } })
- }
- }
- }))).then(r => { return r })
- //console.log(list);
- let docs = {};
- list.forEach(el => {
- if (el) {
- let doc = {}
- let res = el.info;
- let index = el.world[0];
- let proxy = el.world[1].proxy;
- if (res && res !== 'null') {
- //if (res.file && res.file !== 'null') {
- //let worldDesc = JSON.parse(res.file);
- var worldDesc = {};
- if (typeof (res) == 'object') {
- worldDesc = res
- } else {
- worldDesc = JSON.parse(res)
- }
- let root = Object.keys(worldDesc)[0];
- var appInfo = worldDesc[root]['en'];
- let langID = localStorage.getItem('krestianstvo_locale');
- if (langID) {
- appInfo = worldDesc[root][langID]
- }
- let settings = worldDesc[root]['settings'];
- doc = {
- 'worldName': index,
- 'created': res.created ? res.created : res.modified,
- 'modified': res.modified,
- 'proxy': proxy,
- 'type': 'proto',
- 'userAlias': userAlias,
- 'info': appInfo,
- 'settings': settings
- }
- //}
- }
- if (Object.keys(doc).length !== 0)
- docs[index] = doc;
- }
- })
- if (worldName) {
- return docs[worldName]
- }
- return docs
- }
- async setNewProxyForWorld(worldName, proxyName) {
- if (_LCSDB.user().is) {
- let newProxy = await _app.helpers.getUserPub(proxyName)
- if (newProxy)
- _LCSDB.user().get('worlds').get(worldName).put({ 'proxy': newProxy })
- }
- }
- }
- export { App }
|