Pman.Builder.Wizard.js
[Pman.Builder] / Pman.Builder.Wizard.js
1 /**
2  * Code for wizards
3  *
4  * properties are
5  * [Roo.grid.grid] << convert a cfg into a table.
6  * [Roo.grid.grid-edit] << create a config from a tree.
7  *
8  *
9  * My initial idea was to parse the existing nodes, to generate the cfg.
10  * -- this is far too complicated..
11  * -- let's go back to the idea of storing the config on the relivant nodes.
12  * -- The downside is that if you modify the children, then the wizard will
13  *    not know about those columns. etc..
14  *    
15  * 
16  *
17  */
18
19
20 Pman.Builder.Wizard = {
21  
22     typemap : {
23         'bool' : 'boolean',
24         
25         'date' : 'date',
26         'datetime' : 'date',
27         'time' : 'string', //bogus
28         
29         'int' : 'int',
30         'int4' : 'int',
31         'bigint' : 'int',
32         'tinyint' : 'int',
33         'smallint' : 'int',
34         'timestamp' : 'number',
35         
36         'double' : 'float',
37         'decimal' : 'float',
38         'float' : 'float',
39         
40         'char' : 'string',
41         'varchar' : 'string',
42         'text' : 'string',
43         'longtext' : 'string',
44         'tinytext' : 'string',
45         'mediumtext' : 'string',
46         'enum' : 'string',
47         
48         'blob' : 'string'
49     },
50     /**
51      * auto builders..
52      * @param {Object} cfg the configuration
53      * @param {Object} old the previous def...
54      *  - table
55      *  - cols_ex : [ name, name]
56      *  - cols : [ 
57      *      { column : name
58      *        ctype : ctype,
59      *        desc : desc
60      *      }
61      *   ]
62      * // usage:
63       - gen config - prior to editing
64         -- builds up 'cfg'
65      * 
66      */
67    'Roo.GridPanel' : function(cfg, old)
68     {
69     
70         var _t = this;
71         old = old || {};
72
73         var gi = _t['Roo.grid.Grid'](cfg);
74         gi['*prop'] = 'grid';
75         
76         return {
77             '|xns' : 'Roo',
78             xtype : "GridPanel",
79             '.buildercfg' : Roo.encode(cfg),
80             title : cfg.table,
81             fitToframe : true,
82             fitContainer : true,
83             tableName: cfg.table,
84             background: true,
85             region : 'center',
86             listeners : {
87                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
88             },
89             items: [ gi ]
90             
91         };
92     },
93     
94     'Roo.grid.Grid' : function(cfg, old)
95     {
96    
97         var _t = this;
98         old = old || {};
99  
100         
101         
102         var colmodel = []
103         Roo.each(cfg.cols, function(cc) {
104             colmodel.push( _t['Roo.grid.ColumnModel'](cc) );
105         })
106         
107         var dataSource = _t['Roo.data.Store'](cfg);
108         dataSource['*prop'] = 'dataSource';
109         
110         return  {
111             "xtype": "Grid",
112             '.builderCfg' :  Roo.encode(cfg),
113             "autoExpandColumn": cfg.cols_ex[0],
114             "loadMask": true,
115             "listeners": {
116                 "|render": "function() \n" +
117                     "{\n" +
118                     "    _this.grid = this; \n" +
119                     "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
120                     "    if (_this.panel.active) {\n" +
121                     "       this.footer.onClick('first');\n" +
122                     "    }\n" +
123                     "}",
124                 "|rowdblclick": "function (_self, rowIndex, e)\n" + 
125                     "{\n" + 
126                     "    if (!_this.dialog) return;\n" + 
127                     "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
128                     "        _this.grid.footer.onClick('first');\n" + 
129                     "    }); \n" + 
130                     "}\n"
131             },
132             "|xns": "Roo.grid",
133
134             "items": [
135                 dataSource,
136                 {
137                     "*prop": "footer",
138                     "xtype": "PagingToolbar",
139                     "pageSize": 25,
140                     "displayInfo": true,
141                     "displayMsg": "Displaying " + cfg.table + "{0} - {1} of {2}",
142                     "emptyMsg": "No " + cfg.table + " found",
143                     "|xns": "Roo"
144                 },
145                 {
146                     "*prop": "toolbar",
147                     "xtype": "Toolbar",
148                     "|xns": "Roo",
149                     "items": [
150                         {
151                             "text": "Add",
152                             "xtype": "Button",
153                             "cls": "x-btn-text-icon",
154                             "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
155                             "listeners": {
156                                 "|click": "function()\n"+
157                                     "{\n"+
158                                     "    if (!_this.dialog) return;\n" +
159                                     "    _this.dialog.show( { id : 0 } , function() {\n"+
160                                     "        _this.grid.footer.onClick('first');\n"+
161                                     "   }); \n"+
162                                     "}\n"
163                             },
164                             "|xns": "Roo.Toolbar"
165                         },
166                         {
167                             "text": "Edit",
168                             "xtype": "Button",
169                             "cls": "x-btn-text-icon",
170                             "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
171                             "listeners": {
172                                 "|click": "function()\n"+
173                                     "{\n"+
174                                     "    var s = _this.grid.getSelectionModel().getSelections();\n"+
175                                     "    if (!s.length || (s.length > 1))  {\n"+
176                                     "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
177                                     "        return;\n"+
178                                     "    }\n"+
179                                     "    if (!_this.dialog) return;\n" +
180                                     "    _this.dialog.show(s[0].data, function() {\n"+
181                                     "        _this.grid.footer.onClick('first');\n"+
182                                     "    }); \n"+
183                                     "    \n"+
184                                     "}\n" 
185                                 
186                             },
187                             "|xns": "Roo.Toolbar"
188                         },
189                         {
190                             "text": "Delete",
191                             "cls": "x-btn-text-icon",
192                             "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
193                             "xtype": "Button",
194                             "listeners": {
195                                 "|click": "function()\n"+
196                                     "{\n"+
197                                     "     Pman.genericDelete(_this, '" + cfg.table + "'); \n"+
198                                     "}\n"+
199                                     "        "
200                             },
201                             "|xns": "Roo.Toolbar"
202                         }
203                     ]
204                 }, // end toolbar
205             ].concat( colmodel)
206         };
207     },
208     
209     
210     'Roo.data.Store' : function(cfg, old)
211     {
212         var _t = this;
213         old = old || false;
214       
215         
216         
217         var jreader = _t['Roo.data.JsonReader'](cfg);
218         return  {
219             
220             "xtype": "Store",
221             "|xns": "Roo.data",
222              '.builderCfg' :  Roo.encode(cfg),
223              remoteSort : true,
224             '|sortInfo' : "{ field : '" +  cfg.cols_ex[0]  +  "', direction: 'ASC' }", 
225             "items": [
226                 {
227                     "*prop": "proxy",
228                     "xtype": "HttpProxy",
229                     "method": "GET",
230                     "|url": "baseURL + '/Roo/" +cfg.table + ".php'",
231                     "|xns": "Roo.data"
232                 },
233                 jreader
234             ]
235         };
236     
237     },
238     
239         
240     'Roo.data.JsonReader' : function(cfg, old)
241     {
242         // simple version to start with..
243         
244         
245         
246        
247         var _t = this;
248         old = old || false;
249        
250         var fields = [];
251
252         Roo.each(cfg.cols, function(cc) {
253             var ty = typeof(_t.typemap[cc.ctype]) == 'undefined' ? 'string' : _t.typemap[cc.ctype];
254             if (cc.ctype == 'string' ) {
255                 fields.push(cc.column);
256                 return;
257             }
258             fields.push({ name : cc.column, type : _t.typemap[cc.ctype]} );
259         });
260         
261         
262        return { 
263         
264             '|xns' : 'Roo.data',
265             xtype : "JsonReader",
266             totalProperty : "total",
267             root : "data",
268             '.builderCfg' :  Roo.encode(cfg),
269             '*prop' : "reader",
270             id : 'id', // maybe no..
271          
272             '|fields' :  JSON.stringify(fields, null,4).replace(/"/g,"'")
273         };
274     },
275     
276     
277     'Roo.grid.ColumnModel' : function(rcfg, old)
278     {
279         // simple version to start with..
280         var ty = typeof(this.typemap[rcfg.ctype]) == 'undefined' ? 'string' : this.typemap[rcfg.ctype];
281         
282         // some special kludges..
283         // remove table prefix..
284         
285         var desc = rcfg.columnshort;
286         if (desc.substring(0, rcfg.table.length+1) == rcfg.table+'_') {
287             desc = desc.substring(rcfg.table.length+1);
288         }
289         desc = desc.replace(/_id$/, '');
290         
291         if (!desc.length) {
292             desc = rcfg.column;
293         }
294         
295         
296        
297         return {
298             "xtype": "ColumnModel",
299             '.builderCfg' : Roo.encode(rcfg),
300             "header": rcfg.desc.length ? rcfg.desc : desc,
301             "width":  ty == 'string' ? 200 : 75,
302             "dataIndex": rcfg.column,
303             "|renderer": ty != 'date' ? 
304                     "function(v) { return String.format('{0}', v); }" :
305                     "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
306             "|xns": "Roo.grid",
307             "*prop": "colModel[]"
308         };
309     }
310     
311 }