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         old = old || {};
71
72         var gi = _t['Roo.grid.Grid'](cfg);
73         gi['*prop'] = 'grid';
74         
75         return {
76             '|xns' : 'Roo',
77             xtype : "GridPanel",
78             '.buildercfg' : Roo.encode(cfg),
79             title : cfg.table,
80             fitToframe : true,
81             fitContainer : true,
82             tableName: cfg.table,
83             background: true,
84             region : 'center',
85             listeners : {
86                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
87             },
88             items: [ gi ]
89             
90         };
91     },
92     
93     'Roo.grid.Grid' : function(cfg, old)
94     {
95    
96         var _t = this;
97         old = old || {};
98  
99         
100         
101         var colmodel = []
102         Roo.each(cfg.cols, function(cc) {
103             colmodel.push( _t['Roo.grid.ColumnModel'](cc) );
104         })
105         
106         var dataSource = _t['Roo.data.Store'](cfg);
107         dataSource['*prop'] = 'dataSource';
108         
109         return  {
110             "xtype": "Grid",
111             '.builderCfg' : cfg,
112             "autoExpandColumn": cfg.cols_ex[0],
113             "loadMask": true,
114             "listeners": {
115                 "|render": "function() \n" +
116                     "{\n" +
117                     "    _this.grid = this; \n" +
118                     "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
119                     "    if (_this.panel.active) {\n" +
120                     "       this.footer.onClick('first');\n" +
121                     "    }\n" +
122                     "}",
123                 "|rowdblclick": "function (_self, rowIndex, e)\n" + 
124                     "{\n" + 
125                     "    if (!_this.dialog) return;\n" + 
126                     "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
127                     "        _this.grid.footer.onClick('first');\n" + 
128                     "    }); \n" + 
129                     "}\n"
130             },
131             "|xns": "Roo.grid",
132
133             "items": [
134                 dataSource,
135                 {
136                     "*prop": "footer",
137                     "xtype": "PagingToolbar",
138                     "pageSize": 25,
139                     "displayInfo": true,
140                     "displayMsg": "Displaying " + cfg.table + "{0} - {1} of {2}",
141                     "emptyMsg": "No " + cfg.table + " found",
142                     "|xns": "Roo"
143                 },
144                 {
145                     "*prop": "toolbar",
146                     "xtype": "Toolbar",
147                     "|xns": "Roo",
148                     "items": [
149                         {
150                             "text": "Add",
151                             "xtype": "Button",
152                             "cls": "x-btn-text-icon",
153                             "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
154                             "listeners": {
155                                 "|click": "function()\n"+
156                                     "{\n"+
157                                     "    if (!_this.dialog) return;\n" +
158                                     "    _this.dialog.show( { id : 0 } , function() {\n"+
159                                     "        _this.grid.footer.onClick('first');\n"+
160                                     "   }); \n"+
161                                     "}\n"
162                             },
163                             "|xns": "Roo.Toolbar"
164                         },
165                         {
166                             "text": "Edit",
167                             "xtype": "Button",
168                             "cls": "x-btn-text-icon",
169                             "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
170                             "listeners": {
171                                 "|click": "function()\n"+
172                                     "{\n"+
173                                     "    var s = _this.grid.getSelectionModel().getSelections();\n"+
174                                     "    if (!s.length || (s.length > 1))  {\n"+
175                                     "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
176                                     "        return;\n"+
177                                     "    }\n"+
178                                     "    if (!_this.dialog) return;\n" +
179                                     "    _this.dialog.show(s[0].data, function() {\n"+
180                                     "        _this.grid.footer.onClick('first');\n"+
181                                     "    }); \n"+
182                                     "    \n"+
183                                     "}\n" 
184                                 
185                             },
186                             "|xns": "Roo.Toolbar"
187                         },
188                         {
189                             "text": "Delete",
190                             "cls": "x-btn-text-icon",
191                             "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
192                             "xtype": "Button",
193                             "listeners": {
194                                 "|click": "function()\n"+
195                                     "{\n"+
196                                     "     Pman.genericDelete(_this, '" + cfg.table + "'); \n"+
197                                     "}\n"+
198                                     "        "
199                             },
200                             "|xns": "Roo.Toolbar"
201                         }
202                     ]
203                 }, // end toolbar
204             ].concat( colmodel)
205         };
206     },
207     
208     
209     'Roo.data.Store' : function(cfg, old, parse_only)
210     {
211         var _t = this;
212         old = old || false;
213         
214         if (old !== false) {
215             _t['Roo.data.Reader'](cfg, old, true);
216              
217             if (parse_only) { 
218                 return false;
219             }
220         }
221         
222         
223         var jreader = _t['Roo.data.Reader'](cfg);
224         return  {
225             
226             "xtype": "Store",
227             "|xns": "Roo.data",             
228              remoteSort : true,
229             '|sortInfo' : "{ field : '" +  cfg.cols_ex[0]  +  "', direction: 'ASC' }", 
230             "items": [
231                 {
232                     "*prop": "proxy",
233                     "xtype": "HttpProxy",
234                     "method": "GET",
235                     "|url": "baseURL + '/Roo/" +cfg.table + ".php'",
236                     "|xns": "Roo.data"
237                 },
238                 jreader
239             ]
240         };
241     
242     },
243     
244         
245     'Roo.data.Reader' : function(cfg)
246     {
247         // simple version to start with..
248         
249         
250         
251        
252         var _t = this;
253         old = old || false;
254         
255         if (old !== false) {
256             
257              
258             if (parse_only) { 
259                 return false;
260             }
261         }
262         
263         var fields = [];
264
265         Roo.each(cfg.cols, function(cc) {
266             var ty = typeof(_t.typemap[cc.ctype]) == 'undefined' ? 'string' : _t.typemap[cc.ctype];
267             if (cc.ctype == 'string' ) {
268                 fields.push(cc.column);
269                 return;
270             }
271             fields.push({ name : cc.column, type : _t.typemap[cc.ctype]} );
272         });
273         
274         
275        return { 
276         
277             '|xns' : 'Roo.data',
278             xtype : "JsonReader",
279             totalProperty : "total",
280             root : "data",
281             '*prop' : "reader",
282             id : 'id', // maybe no..
283          
284             '|fields' :  JSON.stringify(fields, null,4).replace(/"/g,"'")
285         };
286     },
287     
288     
289     
290 }