Preview.php
[Pman.Builder] / Pman.Builder.Wizard.js
index 32db3ad..e16de0c 100644 (file)
@@ -9,7 +9,9 @@
  * My initial idea was to parse the existing nodes, to generate the cfg.
  * -- this is far too complicated..
  * -- let's go back to the idea of storing the config on the relivant nodes.
- * -- 
+ * -- The downside is that if you modify the children, then the wizard will
+ *    not know about those columns. etc..
+ *    
  * 
  *
  */
@@ -29,7 +31,7 @@ Pman.Builder.Wizard = {
         'bigint' : 'int',
         'tinyint' : 'int',
         'smallint' : 'int',
-        'timestamp' : 'number',
+        'timestamp' : 'date', // for postgres... it's a date!??
         
         'double' : 'float',
         'decimal' : 'float',
@@ -64,25 +66,27 @@ Pman.Builder.Wizard = {
      */
    'Roo.GridPanel' : function(cfg, old)
     {
-        
-         
+    
+        var _t = this;
+        old = old || {};
+
         var gi = _t['Roo.grid.Grid'](cfg);
         gi['*prop'] = 'grid';
         
         return {
             '|xns' : 'Roo',
             xtype : "GridPanel",
-            '.buildercfg' : Roo.encode(cfg),
-            "title": cfg.table,
-            "fitToframe": true,
-            "fitContainer": true,
-            "tableName": cfg.table,
-            "background": true,
-            "region" : 'center',
-            "listeners": {
+            '.builderCfg' : Roo.encode(cfg),
+            title : cfg.table,
+            fitToframe : true,
+            fitContainer : true,
+            tableName: cfg.table,
+            background: true,
+            region : 'center',
+            listeners : {
                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
             },
-            "items": [ gi ]
+            items: [ gi ]
             
         };
     },
@@ -105,6 +109,7 @@ Pman.Builder.Wizard = {
         
         return  {
             "xtype": "Grid",
+            '.builderCfg' :  Roo.encode(cfg),
             "autoExpandColumn": cfg.cols_ex[0],
             "loadMask": true,
             "listeners": {
@@ -119,7 +124,7 @@ Pman.Builder.Wizard = {
                 "|rowdblclick": "function (_self, rowIndex, e)\n" + 
                     "{\n" + 
                     "    if (!_this.dialog) return;\n" + 
-                    "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
+                    "    _this.dialog.show( this.getDataSource().getAt(rowIndex).data, function() {\n" + 
                     "        _this.grid.footer.onClick('first');\n" + 
                     "    }); \n" + 
                     "}\n"
@@ -202,25 +207,19 @@ Pman.Builder.Wizard = {
     },
     
     
-    'Roo.data.Store' : function(cfg, old, parse_only)
+    'Roo.data.Store' : function(cfg, old)
     {
         var _t = this;
         old = old || false;
-        
-        if (old !== false) {
-            _t['Roo.data.Reader'](cfg, old, true);
-             
-            if (parse_only) { 
-                return false;
-            }
-        }
+      
         
         
-        var jreader = _t['Roo.data.Reader'](cfg);
+        var jreader = _t['Roo.data.JsonReader'](cfg);
         return  {
             
             "xtype": "Store",
-            "|xns": "Roo.data",             
+            "|xns": "Roo.data",
+             '.builderCfg' :  Roo.encode(cfg),
              remoteSort : true,
             '|sortInfo' : "{ field : '" +  cfg.cols_ex[0]  +  "', direction: 'ASC' }", 
             "items": [
@@ -238,7 +237,7 @@ Pman.Builder.Wizard = {
     },
     
         
-    'Roo.data.Reader' : function(cfg)
+    'Roo.data.JsonReader' : function(cfg, old)
     {
         // simple version to start with..
         
@@ -247,15 +246,7 @@ Pman.Builder.Wizard = {
        
         var _t = this;
         old = old || false;
-        
-        if (old !== false) {
-            
-             
-            if (parse_only) { 
-                return false;
-            }
-        }
-        
+       
         var fields = [];
 
         Roo.each(cfg.cols, function(cc) {
@@ -274,6 +265,7 @@ Pman.Builder.Wizard = {
             xtype : "JsonReader",
             totalProperty : "total",
             root : "data",
+            '.builderCfg' :  Roo.encode(cfg),
             '*prop' : "reader",
             id : 'id', // maybe no..
          
@@ -282,5 +274,436 @@ Pman.Builder.Wizard = {
     },
     
     
+    'Roo.grid.ColumnModel' : function(rcfg, old)
+    {
+        // simple version to start with..
+        var ty = typeof(this.typemap[rcfg.ctype]) == 'undefined' ? 'string' : this.typemap[rcfg.ctype];
+        
+        // some special kludges..
+        // remove table prefix..
+        
+        var desc = rcfg.columnshort;
+        if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
+            desc = desc.substring(rcfg.table.length+1);
+        }
+        desc = desc.replace(/_id$/, '');
+        
+        if (!desc.length) {
+            desc = rcfg.column;
+        }
+        if (rcfg.title && rcfg.title.length) {
+            desc = rcfg.title;
+        }
+        
+       
+        return {
+            "xtype": "ColumnModel",
+            '.builderCfg' : Roo.encode(rcfg),
+            "header":   desc,
+            "width":  ty == 'string' ? 200 : 75,
+            "dataIndex": rcfg.column,
+            "|renderer": ty != 'date' ? 
+                    "function(v) { return String.format('{0}', v); }" :
+                    "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
+            "|xns": "Roo.grid",
+            "*prop": "colModel[]"
+        };
+    },
+    
+    
+     
+    'Roo.LayoutDialog' : function(rcfg, old)
+    {
+        // simple version to start with..
+        var _t = this;
+        
+        var frmCfg =    _t['Roo.form.Form'](rcfg, old);
+        
+        // loop through the cols..
+        // we need a 'display column' for each of these.
+        // we could also have a 'renderer'...
+        
+     
+        
+         
+        var formHeight = (frmCfg.items.length * 25) + 100; // work out from number of form ites..
+       
+        return {
+            xtype : 'LayoutDialog',
+            "|xns": "Roo",
+            '.builderCfg' : Roo.encode(rcfg),
+            
+            closable : false,
+            collapsible: false,
+            modal : true,
+            height : formHeight,
+            resizable: true,
+            title: "Edit / Create " + rcfg.table,
+            width: 500,
+            
+            
+            items  : [
+                {
+                    "|xns": "Roo",
+                    "xtype": "LayoutRegion",
+                    "*prop": "center"
+                },
+                {
+                    "region": "center",
+                    "xtype": "ContentPanel",
+                    "|xns": "Roo",
+                    "items": [
+                        frmCfg
+                    ]
+                },
+                 {
+                    "listeners": {
+                        "click": "function (_self, e)\n{\n    _this.dialog.hide();\n}"
+                    },
+                    "*prop": "buttons[]",
+                    "text": "Cancel",
+                    "xtype": "Button",
+                    "|xns": "Roo"
+                },
+                {
+                    "listeners": {
+                        "click": "function (_self, e)\n{\n    // do some checks?\n     \n    \n    _this.dialog.el.mask(\"Saving\");\n    _this.form.doAction(\"submit\");\n\n}"
+                    },
+                    "*prop": "buttons[]",
+                    "text": "Save",
+                    "xtype": "Button",
+                    "|xns": "Roo"
+                }
+            ]
+        };
+    },
+    'Roo.form.Form' : function(rcfg, old)
+    {
+        // simple version to start with..
+        var _t = this;
+        
+        var formElements = [];
+          
+        var fcombo = function(cn) {
+            var cret = '';
+            
+            Roo.each(rcfg.cols_ex, function(n) {
+                
+                Roo.log(
+                        ['match' , n , 'to', cn, n.substring(0,cn.length) ].join(' ')
+                    );
+                if (n.substring(0,cn.length) == cn) {
+                    cret = n;
+                    return false;
+                }
+                return true;
+            });
+            Roo.log("RETURN: " + cret);
+            return cret;
+        }
+   
+        Roo.each(rcfg.cols, function(cc) {
+            
+            var ty = typeof(_t.typemap[cc.ctype]) == 'undefined' ? 'string' : _t.typemap[cc.ctype];
+            
+            if (ty == 'string' ) {
+                formElements.push( _t['Roo.form.TextField'](cc, {}));
+                return;
+            }
+            if (ty  == 'date' ) {
+                formElements.push( _t['Roo.form.DateField'](cc, {}));
+                return;
+            }
+            if (ty  == 'float' ) {
+                formElements.push( _t['Roo.form.NumberField'](cc, {}));
+                return;
+            }
+            if (ty  == 'int' ) {
+                cc.display = fcombo(cc.column);
+                if (cc.deps && cc.display.length) { 
+                   formElements.push( _t['Roo.form.ComboBox'](cc, {}));
+                    return;
+                }
+                formElements.push( _t['Roo.form.NumberField'](cc, {}));
+                return;
+            }
+            
+            
+        });
+        
+        
+        
+        return {
+            xtype : "Form",
+            '|xns' : 'Roo.form',
+            listeners : {
+                "|actioncomplete" : "function(_self,action)\n"+
+                    "{\n"+
+                    "    if (action.type == 'setdata') {\n"+
+                    "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
+                    "       return;\n"+
+                    "    }\n"+
+                    "    if (action.type == 'load') {\n"+
+                    "        return;\n"+
+                    "    }\n"+
+                    "    if (action.type =='submit') {\n"+
+                    "    \n"+
+                    "        _this.dialog.hide();\n"+
+                    "    \n"+
+                    "         if (_this.callback) {\n"+
+                    "            _this.callback.call(_this, _this.form.getValues());\n"+
+                    "         }\n"+
+                    "         _this.form.reset();\n"+
+                    "         return;\n"+
+                    "    }\n"+
+                    "}\n",
+                
+                "|rendered" : "function (form)\n"+
+                    "{\n"+
+                    "    _this.form= form;\n"+
+                    "}\n"
+            },
+            method : "POST",
+            style : "margin:10px;",
+            "|url" : "baseURL + '/Roo/" + rcfg.table + ".php'",
+            items : formElements
+        };
+        
+    },
+    'Roo.form.TextField' : function(rcfg, old)
+    {
+        var desc = rcfg.columnshort;
+        if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
+            desc = desc.substring(rcfg.table.length+1);
+        }
+        desc = desc.replace(/_id$/, '');
+        
+        if (!desc.length) {
+            desc = rcfg.column;
+        }
+        if (rcfg.title && rcfg.title.length) {
+            desc = rcfg.title;
+        }
+        
+        return { 
+            xtype : 'TextField',
+            '|xns' : 'Roo.form',
+            fieldLabel : desc,
+            name : rcfg.column,
+            width : 300  
+        };
+    
+    
+    },
+    
+    'Roo.form.NumberField' : function(rcfg, old)
+    {
+        var desc = rcfg.columnshort;
+        if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
+            desc = desc.substring(rcfg.table.length+1);
+        }
+        desc = desc.replace(/_id$/, '');
+        
+        if (!desc.length) {
+            desc = rcfg.column;
+        }
+        if (rcfg.title && rcfg.title.length) {
+            desc = rcfg.title;
+        }
+        return { 
+            xtype : 'NumberField',
+            '|xns' : 'Roo.form',
+            fieldLabel : desc,
+            name : rcfg.column,
+            width : 100,
+            align : 'right'
+        };
+     
+    },
+    
+     'Roo.form.DateField' : function(rcfg, old)
+    {
+        var desc = rcfg.columnshort;
+        if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
+            desc = desc.substring(rcfg.table.length+1);
+        }
+        desc = desc.replace(/_id$/, '');
+        
+        if (!desc.length) {
+            desc = rcfg.column;
+        }
+        if (rcfg.title && rcfg.title.length) {
+            desc = rcfg.title;
+        }
+        return { 
+            xtype : 'NumberField',
+            '|xns' : 'Roo.form',
+            fieldLabel : desc,
+            name : rcfg.column,
+            width : 100,
+            format : 'Y-m-d'
+        };
+     
+    },
+    
+    'Roo.form.ComboBox' : function(in_rcfg, old)
+    {
+        /*
+         * We need:
+         *   localtable:
+         *      the column
+         *      table name.
+         *   remote table:
+         *      table name
+         *      column 'it maps to'
+         *      title column
+         *      
+         *      
+         *
+         */
+        var rcfg = in_rcfg;
+        var table , desc, display, idcol, hiddenName, name;
+        
+        // rcfg can be one of two things: (from a form)
+        // or direct..
+        if (rcfg.cols) {
+            display = rcfg.cols_ex[0];
+            
+            
+            table = rcfg.table;
+            
+            var ix = rcfg.cols[0].columnshort == display ? 0 : 1;
+            
+            idcol =  rcfg.cols[ ix ? 0 : 1 ].columnshort;
+            
+            desc = rcfg.cols[ix].title
+            if (!desc.length) {
+                desc = rcfg.cols[ix].column;
+                if (desc.substring(0, table.length+1) == table+'_') {
+                    desc = desc.substring(table.length+1);
+                }
+                desc = desc.replace(/_id$/, '');
+                if (!desc.length) {
+                    desc = rcfg.cols[0].column;
+                }
+            }
+            combofields = [
+                { name : idcol, type : 'int' },
+                display
+            ];
+            hiddenName = idcol;
+            name = display;
+                
+        } else { 
+            
+            
+            
+            desc = rcfg.columnshort;
+            if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
+                desc = desc.substring(rcfg.table.length+1);
+            }
+            desc = desc.replace(/_id$/, '');
+            
+            if (!desc.length) {
+                desc = rcfg.column;
+            }
+            if (rcfg.title && rcfg.title.length) {
+                desc = rcfg.title;
+            }
+            // set the display column (from the remote table)
+            display = rcfg.display;
+            Roo.each(rcfg.deps, function(dn) {
+                if (dn.column == rcfg.display) {
+                    display = dn.columnshort;
+                }
+                
+            });
+            
+            table = rcfg.deps[0].table;
+            idcol =  rcfg.maps_to;
+            hiddenName  = rcfg.column;
+            name = rcfg.column + '_' + display;
+            
+        }
+        // a reader...( basic as we do meta queries to get the real one..)
+        var combofields = [
+                { name : idcol , type : 'int' },
+                display
+            ];
+         
+        
+        return {
+            '|xns' : 'Roo.form',
+            xtype: 'ComboBox',
+            
+            allowBlank : false,
+            editable : false,
+            emptyText : 'Select ' + table,
+            forceSelection : true,
+            listWidth : 400,
+            loadingText: 'Searching...',
+            minChars : 2,
+            pageSize : 20,
+            qtip: 'Select ' + table,
+            selectOnFocus: true,
+            triggerAction : 'all',
+            typeAhead: true,
+            
+            width: 300,
+            
+            tpl : '<div class="x-grid-cell-text x-btn button"><b>{'+ display + '}</b> </div>',  
+            queryParam : 'query['+display+']', 
+            fieldLabel : desc,   
+            
+            // from remote..
+            valueField : idcol,
+            displayField : display, 
+            
+            // from our table..
+            hiddenName : hiddenName, 
+            name : name, 
+            
+            items : [
+                {
+                    '*prop' : 'store',
+                    '|xns' : 'Roo.data',
+                    '|sortInfo' : '{ direction : \'ASC\', field: \'' + display  +'\' }',
+                    xtype : 'Store',
+                    
+                    remoteSort : true,
+                    
+                    listeners : {
+                        '|beforeload' : 'function (_self, o)' +
+                        "{\n" +
+                        "    o.params = o.params || {};\n" +
+                        "    // set more here\n" +
+                        "}\n"
+                    },
+                    items : [
+                        {
+                            '*prop' : 'proxy',
+                            'xtype' : 'HttpProxy',
+                            'method' : 'GET',
+                            '|xns' : 'Roo.data',
+                            '|url' : "baseURL + '/Roo/" + table + ".php'"
+                        },
+                        
+                        {
+                            '*prop' : 'reader',
+                            'xtype' : 'JsonReader',
+                            '|xns' : 'Roo.data',
+                            'id' : idcol,
+                            'root' : 'data',
+                            'totalProperty' : 'total',
+                            '|fields' : JSON.stringify(combofields)
+                            
+                        }
+                    ]
+                }
+            ]
+        };
+     
+    }
+    
     
 }
\ No newline at end of file