Browse Source

fix clone proto and load

Nikolay Suslov 5 years ago
parent
commit
e2ec415a7f
2 changed files with 82 additions and 68 deletions
  1. 66 55
      public/app.js
  2. 16 13
      public/vwf/view/document.js

+ 66 - 55
public/app.js

@@ -1281,12 +1281,18 @@ class App {
     let newOwner = _LCSDB.user().is.pub;
 
     let myWorlds = (await _LCSDB.user(newOwner).get('worlds').promOnce()).data;
-    if (!myWorlds) _LCSDB.user(newOwner).get('worlds').put({});
+    //if (!myWorlds) _LCSDB.user(newOwner).get('worlds').put({});
+
+    _LCSDB.user(newOwner).get('worlds').not(res=>{
+      _LCSDB.user(newOwner).get('worlds').put({});
+    })
+
+    let checkExist = Object.keys(myWorlds).filter(el=> el == newWorldName);
 
     if (newWorldName) {
 
-      let worldProto = (await _LCSDB.user(newOwner).get('worlds').get(newWorldName).promOnce()).data;
-      if (worldProto) {
+      //let worldProto = (await _LCSDB.user(newOwner).get('worlds').get(newWorldName).promOnce()).data;
+      if (checkExist.length > 0) {
         console.log('already exist!');
         return
       }
@@ -1310,62 +1316,67 @@ class App {
       'published': true
     };
 
-    let fileNamesAll = (await _LCSDB.user(userPub).get('worlds').get(worldName).promOnce()).data;
-    let worldFileNames = Object.keys(fileNamesAll).filter(el => (el !== '_') && (el !== 'owner') && (el !== 'parent') && (el !== 'featured') && (el !== 'published') && (el !== '_config_yaml') && (el !== '_yaml') && (el !== '_html'));
+   // let fileNamesAll = 
+    await _LCSDB.user(userPub).get('worlds').get(worldName).load(all=> {
 
-    for (var doc in worldFileNames) {
+      let worldFileNames = Object.keys(all).filter(el => (el !== '_') && (el !== 'owner') && (el !== 'parent') && (el !== 'featured') && (el !== 'published') && (el !== '_config_yaml') && (el !== '_yaml') && (el !== '_html'));
 
-      let fn = worldFileNames[doc];
-      let res = (await _LCSDB.user(userPub).get('worlds').get(worldName).get(fn).promOnce()).data;
-      let data = {
-        'file': JSON.stringify(res.file),
-        'modified': created
+      for (var doc in worldFileNames) {
+  
+        let fn = worldFileNames[doc];
+        let res = all[fn]; //(await _LCSDB.user(userPub).get('worlds').get(worldName).get(fn).promOnce()).data;
+        let data = {
+          'file': JSON.stringify(res.file),
+          'modified': created
+        }
+        worldObj[fn] = data;
       }
-      worldObj[fn] = data;
-    }
-    console.log(worldObj);
-
-    // for (const obj of Object.keys(worldObj)) {
-    //   let myWorlds = _LCSDB.user().get('worlds');
-    //   let myNewWorld = myWorlds.get(worldID);
-    //   myNewWorld.get(obj).put(worldObj[obj]);
-    // }
-
-    //let myWorlds = await _LCSDB.user(newOwner).get('worlds').once().then();
-    let myWorld = _LCSDB.user(newOwner).get('worlds').get(worldID).put({});
-    myWorld.put(worldObj, function (res) {
-      console.log(res)
-    }); //.get(worldID) let myWorld =
-
-    // let myWorld = _LCSDB.user().get(worldID).put(worldObj);
-    // _LCSDB.user().get('worlds').set(myWorld);
-
-    _app.hideProgressBar();
-    console.log('CLONED!!!');
-
-    let appEl = document.createElement("div");
-    appEl.setAttribute("id", 'cloneLink');
-    let entry = document.querySelector('#worldActionsGUI');
-    if (entry) {
-      entry.appendChild(appEl);
-
-      document.querySelector("#cloneLink").$cell({
-        id: 'cloneLink',
-        $cell: true,
-        $type: "div",
-        $components: [
-          {
-            $type: "a",
-            class: "mdc-button mdc-button--raised mdc-card__action",
-            $text: "Go to new cloned World!",
-            onclick: function (e) {
-              let myName = _LCSDB.user().is.alias;
-              window.location.pathname = '/' + myName + '/' + worldID + '/about'
+      console.log(worldObj);
+  
+      // for (const obj of Object.keys(worldObj)) {
+      //   let myWorlds = _LCSDB.user().get('worlds');
+      //   let myNewWorld = myWorlds.get(worldID);
+      //   myNewWorld.get(obj).put(worldObj[obj]);
+      // }
+  
+      //let myWorlds = await _LCSDB.user(newOwner).get('worlds').once().then();
+      let myWorld = _LCSDB.user(newOwner).get('worlds').get(worldID).put({});
+      myWorld.put(worldObj, function (res) {
+        console.log(res)
+      }); //.get(worldID) let myWorld =
+  
+      // let myWorld = _LCSDB.user().get(worldID).put(worldObj);
+      // _LCSDB.user().get('worlds').set(myWorld);
+  
+      _app.hideProgressBar();
+      console.log('CLONED!!!');
+  
+      let appEl = document.createElement("div");
+      appEl.setAttribute("id", 'cloneLink');
+      let entry = document.querySelector('#worldActionsGUI');
+      if (entry) {
+        entry.appendChild(appEl);
+  
+        document.querySelector("#cloneLink").$cell({
+          id: 'cloneLink',
+          $cell: true,
+          $type: "div",
+          $components: [
+            {
+              $type: "a",
+              class: "mdc-button mdc-button--raised mdc-card__action",
+              $text: "Go to new cloned World!",
+              onclick: function (e) {
+                let myName = _LCSDB.user().is.alias;
+                window.location.pathname = '/' + myName + '/' + worldID + '/about'
+              }
             }
-          }
-        ]
-      })
-    }
+          ]
+        })
+      }
+
+    },{wait: 500}).then();
+   
 
     //window.location.pathname = '/' + userName + '/' + worldID + '/about'
     //page()

+ 16 - 13
public/vwf/view/document.js

@@ -80,30 +80,33 @@ define( [ "module", "vwf/view", "vwf/utility"], function( module, view, utility)
 
               }
 
-              userDB.get('worlds').get(worldName).once(res=>{
+              userDB.get('worlds').get(worldName).load(all=>{
 
-                if(res){
-                    if(Object.keys(res).includes(dbPath)){
-                        userDB.get('worlds').get(worldName).get(dbPath).get('file').once(function(res) { 
+                if(all){
+                    if(Object.keys(all).includes(dbPath)){
+
+                        //userDB.get('worlds').get(worldName).get(dbPath).get('file').once(function(res) { 
+                        let res = all[dbPath].file;
                         loadDoc(res);
                         callback( true );
-                        })
+                       // })
                     } else {
                         //NEED TO FIXED!!! Error: Callback was already called.
-                        userDB.get('worlds').get('empty').get(dbPath).get('file').once(function(res) { 
-                            loadDoc(res);
-                            callback( true );
-                            })
-                        //   var emptyDoc = '<!DOCTYPE html><html><head><script type=\"text\/javascript\">\r\n\r\n        vwf_view.satProperty = function (nodeID, propertyName, propertyValue) {\r\n            if (propertyValue === undefined || propertyValue == null) {\r\n                return;\r\n            }\r\n        }\r\n\r\n\r\n    <\/script>\r\n<\/head>\r\n\r\n<body>\r\n<\/body>\r\n\r\n<\/html>';
+                        // userDB.get('worlds').get('empty').get(dbPath).get('file').once(function(res) { 
+                        //     loadDoc(res);
+                        //     callback( true );
+                        //     },{wait:300})
+
+                          var emptyDoc = '<!DOCTYPE html><html><head><script type=\"text\/javascript\">\r\n\r\n        vwf_view.satProperty = function (nodeID, propertyName, propertyValue) {\r\n            if (propertyValue === undefined || propertyValue == null) {\r\n                return;\r\n            }\r\n        }\r\n\r\n\r\n    <\/script>\r\n<\/head>\r\n\r\n<body>\r\n<\/body>\r\n\r\n<\/html>';
                        
-                        //   loadDoc(emptyDoc);
-                        //   callback( true );
+                          loadDoc(emptyDoc);
+                          callback( true );
                         
 
                     }
                 }
                 
-              })
+              },{wait:300})