Builder/Provider/File/Base.js
[app.Builder.js] / Builder / Provider / File / Base.js
index b4c38f0..c37d992 100644 (file)
@@ -11,11 +11,16 @@ File = imports.File.File;
 Base = XObject.define(
     
     function(cfg) {
-    
+        
         XObject.extend(this, cfg);
+        
     },
     Object,
     {
+        /**
+         * @cfg {Array} doubleStringProps list of properties that can be double quoted.
+         */
+        doubleStringProps : false,
         
         id : false,
         name : false,   // is the JS name of the file.
@@ -37,7 +42,7 @@ Base = XObject.define(
             var _this = this;
             var write = this.toJsonArray()
             print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
-            File.write(this.path, JSON.stringify(write));
+            File.write(this.path, JSON.stringify(write, null, 4));
         },
         
         /**
@@ -55,8 +60,8 @@ Base = XObject.define(
         {
             var ret = { }; 
             var _this = this;
-            ['id', 'name', 'parent', 'title', 'path', 'items', 'project'].forEach( function(k) {
-                ret[k] = _this[k];
+            ['id', 'name', 'parent', 'title', 'path', 'items' , 'permname', 'modOrder' ].forEach( function(k) {
+                ret[k] = typeof(_this[k]) == 'undefined' ? '' : _this[k];
             });
             return ret;
         },
@@ -209,11 +214,41 @@ Base = XObject.define(
                 }
             
             
-            if (!isArray) {
-                if (obj.items && obj.items.length) {
-                     console.dump(obj.items);
+            if (!isArray && obj.items && obj.items.length) {
+                // look for props..
+                var newitems = [];
+                obj.items.forEach(function(pl) {
+                    if (typeof(pl['*prop']) == 'undefined') {
+                        newitems.push(pl);
+                        return;
+                    }
+                    // we have a prop...
+                    var prop = pl['*prop'] + '';
+                    delete pl['*prop'];
+                    if (!prop.match(/\[\]$/)) {
+                        // it's a standard prop..
+                        obj[prop] = pl;
+                        keys.push(prop);
+                        return;
+                    }
+                    prop  = prop.substring(0, prop.length -2); //strip []
+                    // it's an array type..
+                    obj[prop] = obj[prop]  || [];
+                    obj[prop].push(pl);
+                    print("ADDNG PROP:" + prop + ' ' + keys.indexOf(prop) );
+                    if (keys.indexOf(prop) < 0) {
+                        keys.push(prop);
+                    }
+                    
+                    
+                    
+                });
+                obj.items = newitems;
+                if (!obj.items.length) {
+                    delete obj.items;
                 }
-               }
+                
+            }
             
             
             
@@ -224,6 +259,10 @@ Base = XObject.define(
             var left =  '';
             
             keys.forEach(function(i) {
+              
+                if (typeof(obj[i]) == 'undefined') { // empty or removed.
+                    return;
+                }
                 var el = obj[i];
                 if (!isArray && skip.indexOf(i) > -1) { // things we do not write..
                     return;
@@ -235,7 +274,7 @@ Base = XObject.define(
                         left = "'" + leftv + "'";
                     } else if (leftv.match(/[^A-Z_]+/i)) { // not plain a-z... - quoted.
                         var val = JSON.stringify(leftv);
-                        left = "'" + leftv.substring(1, leftv.length-1).replace(/'/, "\\'") + "'";
+                        left = "'" + val.substring(1, val.length-1).replace(/'/g, "\\'") + "'";
                     } else {
                         left = '' + leftv;
                     }
@@ -281,14 +320,42 @@ Base = XObject.define(
                 
                 
                 if (typeof(el) == 'object') {
-                    els.push(left + _this.mungeToString(el, i == 'listeners', pad + '    '));
+                    
+                    // we can skip empty items lists and empty listeners..
+                    //if (!isArray && i == 'items' && !el.length) {
+                    //    return; 
+                    //}
+                   // 
+                    var right = _this.mungeToString(el, i == 'listeners', pad + '    ');
+                    if (typeof(right) != 'undefined') {
+                        els.push(left + right);
+                    }
+                
                     return;
                 }
                 // standard. .
+                if (typeof(obj[i]) != 'string') {
+                    els.push(left + JSON.stringify(obj[i]));
+                    return;
+                }
+                // strings..
+                if (!_this.doubleStringProps) {
+                    els.push(left + JSON.stringify(obj[i]));
+                    return;
+                }
+                if (_this.doubleStringProps.indexOf(i) > -1) {
+                    els.push(left + JSON.stringify(obj[i]));
+                    return;
+                }
+                // single quote..
+                els.push(left + "'" + obj[i].replace(/'/g, "\\'") + "'");
                 
-                els.push(left + JSON.stringify(obj[i]));
+
             });
             
+            if (!isArray && !els.length) {
+                return;
+            }
             //output the thing.
             var spad = pad.substring(0, pad.length-4);
             return (isArray ? '[' : '{') + "\n" +
@@ -302,6 +369,9 @@ Base = XObject.define(
          
         
     }
+    
+     
+    
 );