dbgenerate.js
[app.Builder.js] / dbgenerate.js
1 //<script type="text/javascript">
2
3 /**
4  * This is a hacky generator to generate element definitions for the Roo version from the database
5  * 
6  * Let's see if libgda can be used to generate our Readers for roo...
7  * 
8  * Concept - conect to database..
9  * 
10  * list tables
11  * 
12  * extra schemas..
13  * 
14  * write readers..
15  * 
16  * usage: seed generate.js
17  *
18  *
19  *
20  *
21  *Hack needed to latest GLib-2.0.gir 
22  *
23  * <record name="KeyFile" c:type="GKeyFile" disguised="1">
24         <constructor name="new" c:identifier="g_key_file_new">
25         <return-value transfer-ownership="full">
26           <type name="KeyFile" c:type="GKeyFile*"/>
27         </return-value>
28       </constructor>
29  *
30  *
31  * remove introspectable =0 from g_key_file_get_groups
32  *   and add transfer-owneership = none to return value
33  * remove introspectable =0 from g_key_file_get_keys
34  *   and add transfer-owneership = none to return value* 
35  * 
36  */
37 Gda  = imports.gi.Gda;
38 GObject = imports.gi.GObject;
39
40 GLib = imports.gi.GLib;
41
42 console = imports.console;
43 File = imports.File.File;
44 Options = imports.Options.Options;
45
46 //Gda.init();
47
48 var prov = Gda.Config.list_providers ();
49 //print(prov.dump_as_string());
50
51 var o = new Options({
52     help_description : 'Element builder for App Builder based on database schema',
53     
54     options:  [
55         { arg_long : 'DBTYPE' , arg_short : 't', description : 'Database Type (eg. MySQL or PostgreSQL ' },
56         { arg_long : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
57         { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
58         { arg_long : 'PASSWORD' , arg_short : 'p', description : 'Password' , arg_default :'' },
59         { arg_long : 'INI' , arg_short : 'I', description :
60                     'Either base directory which has Pman/***/DataObjects/***.links.ini or location of ini file.' },
61     ]
62            
63 })
64
65
66
67 var cfg = o.parse(Seed.argv);
68 print(JSON.stringify(cfg, null,4));
69
70 var   cnc = Gda.Connection.open_from_string (cfg.DBTYPE,
71          "DB_NAME=" + cfg.DBNAME, 
72         "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
73         Gda.ConnectionOptions.NONE, null);
74
75
76
77                                               
78
79  
80 Gda.DataSelect.prototype.fetchAll = function()
81 {
82     var cols = [];
83     
84     for (var i =0;i < this.get_n_columns(); i++) {
85         cols.push(this.get_column_name(i));
86     }
87     //print(JSON.stringify(cols, null,4));
88     var iter = this.create_iter();
89     var res = [];
90     //print(this.get_n_rows());
91     var _this = this;
92     for (var r = 0; r < this.get_n_rows(); r++) {
93         
94         // single clo..
95         //print("GOT ROW");
96         if (cols.length == 1) {
97             res.push(this.get_value_at(0,r).get_string());
98             continue;
99         }
100         var add = { };
101         
102         cols.forEach(function(n,i) {
103             var val = _this.get_value_at(i,r);
104             var type = GObject.type_name(val.g_type) ;
105             var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
106             //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
107             //print (n + '=' + iter.get_value_at(i).value);
108             add[n] = vs;
109         });
110         
111         res.push(add);
112         
113     }
114     return res;
115
116 }
117
118 var map = {
119     'date' : 'date',
120     'datetime' : 'date',
121     'timestamp with time zone' : 'date',
122     'timestamp without time zone' : 'date',
123     'time' : 'string', //bogus
124     'int' : 'int',
125     'integer' : 'int',
126     'bigint' : 'int',
127     'double' : 'float',
128     'tinyint' : 'int',
129     'smallint' : 'int',
130     'decimal' : 'float',
131     'float' : 'float',
132     'numeric' : 'float',
133     'char' : 'string',
134     'character' : 'string',
135     'character varying' : 'string',
136     'varchar' : 'string',
137     'text' : 'string',
138     'longtext' : 'string',
139     'tinytext' : 'string',
140     'mediumtext' : 'string',
141     'enum' : 'string',
142     'timestamp' : 'number',
143     'blob' : 'text',
144     'bytea' : 'text',
145     'boolean' : 'int',
146     'text[]' : 'string',
147     
148 }
149
150 var ini = { }
151
152 function readIni(fn)
153 {
154     print('Read INI : ' + fn);
155     var key_file = new GLib.KeyFile.c_new();
156     if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
157         return;
158     }
159    
160     var groups = key_file.get_groups();
161     groups.forEach(function(g) {
162         ini[g] = {}
163         print("KEY:"+g);
164         var keys = key_file.get_keys(g);
165         if (!keys) { return; }
166         
167         keys.forEach(function(k) {
168                 print("GET val: " + k);
169                 ini[g][k] = key_file.get_value(g,k);
170                 print(ini[g][k] );
171         })
172     })
173     
174 }
175  print("DONE KEYS");
176 if (File.isFile(cfg.INI)) {
177     if (cfg.INI.match(/links\.ini$/)) {
178         readIni(cfg.INI);
179     } else {
180         readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
181     }
182 }
183
184
185 if (File.isDirectory(cfg.INI)) {
186         
187
188     //--- load ini files..
189     // this is very specific.
190     
191     var dirs = File.list( cfg.INI + '/Pman').filter( 
192         function(e) { 
193             if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
194                 return false;
195             }
196             return true;
197         }
198     );
199     
200      
201     dirs.forEach(function(d) {
202         // this currently misses the web.*/Pman/XXXX/DataObjects..
203         var path = cfg.INI + '/Pman/' + d + '/DataObjects';
204          
205         if (!File.isDirectory(path)) {
206             return; //skip
207         }
208         var inis = File.list(path).filter(
209             function(e) { return e.match(/\.links\.ini$/); }
210         );
211         if (!inis.length) {
212             return;
213         }
214         
215         inis.forEach(function(i) {
216             readIni(path + '/' + i); 
217             
218         })
219  
220     });
221     // look at web.XXXX/Pman/XXX/DataObjects/*.ini
222     var inis = File.list(cfg.INI).filter(
223         function(e) { return e.match(/\.links\.ini$/); }
224     )
225     
226      inis.forEach(function(i) {
227         readIni(path + '/' + i); 
228         
229     })
230     
231     
232 }
233 //print(JSON.stringify(ini, null,4));
234  //console.dump(ini);
235
236
237  //Seed.quit();
238
239 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
240
241
242 switch(cfg.DBTYPE) {
243     case "MySQL":
244         query_tables = "SHOW TABLES";
245         query_describe_table = "DESCRIBE `%s`";
246         break;
247     
248     case 'PostgreSQL':
249         query_tables = "select c.relname FROM pg_catalog.pg_class c " + 
250             "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " + 
251             "WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')" +
252             "AND pg_catalog.pg_table_is_visible(c.oid) ";
253          query_describe_table =  
254                 "SELECT " +
255                 "f.attnum AS number, " +
256                 "f.attname AS Field, " +
257                 "f.attnum, " +
258                 "CASE WHEN f.attnotnull = 't' THEN 'NO' ELSE 'YES' END AS isNull, " + 
259                 "pg_catalog.format_type(f.atttypid,f.atttypmod) AS Type, " +
260                 "CASE WHEN p.contype = 'p' THEN 't' ELSE 'f' END AS primarykey, " +
261                 "CASE WHEN p.contype = 'u' THEN 't' ELSE 'f' END AS uniquekey, " +
262                 "CASE WHEN p.contype = 'f' THEN g.relname END AS foreignkey, " +
263                 "CASE WHEN p.contype = 'f' THEN p.confkey END AS foreignkey_fieldnum, " +
264                 "CASE WHEN p.contype = 'f' THEN g.relname END AS foreignkey, " +
265                 "CASE WHEN p.contype = 'f' THEN p.conkey END AS foreignkey_connnum, " +
266                 "CASE WHEN f.atthasdef = 't' THEN d.adsrc END AS default " +
267                 "FROM pg_attribute f JOIN pg_class c ON c.oid = f.attrelid " +
268                 "        JOIN pg_type t ON t.oid = f.atttypid " +
269                 "        LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum " +
270                 "        LEFT JOIN pg_namespace n ON n.oid = c.relnamespace " +
271                 "        LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY ( p.conkey ) " +
272                 "        LEFT JOIN pg_class AS g ON p.confrelid = g.oid " +
273                 "WHERE c.relkind = 'r'::char AND n.nspname = '%n' " +
274                 "AND c.relname = '%s' AND f.attnum > 0 ORDER BY number";
275                 
276                 
277                 
278         break;
279     default:
280         throw "Invalid data base type " + cfg.DBTYPE + " should be MySQL or PostgreSQL";
281 /*
282            "Field": "province",
283         "Type": "varchar(255)",
284         "Null": "NO", << or is null
285         "Key": null,
286         "Default": null,
287         "Extra": 
288 */  
289 }
290
291
292  
293
294 var tables = cnc.execute_select_command( query_tables).fetchAll();
295 print(JSON.stringify(tables));
296
297 var readers = [];
298 tables.forEach(function(table) {
299     //print(table);
300     var schema = cnc.execute_select_command(
301             query_describe_table.replace(/%s/, table).replace(/%n/,'public')
302             ).fetchAll();
303     
304     
305     var reader = []; 
306     var colmodel = []; 
307     var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
308          
309     var form = {}
310        
311     var firstTxtCol = '';
312     
313     //print(JSON.stringify(schema, null,4));    Seed.quit();
314     
315     schema.forEach(function(e)  {
316         e.Type = e.Type || e.type;
317         e.Field = e.Field || e.field;
318          
319         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
320         var row  = { }; 
321         if (type) {
322             e.Type = type[1];
323             e.Size = type[2];
324         }
325         
326         
327         
328         row.name = e.Field;
329         
330         
331         if (typeof(map[e.Type]) == 'undefined') {
332            console.dump(e);
333            throw {
334                 name: "ArgumentError", 
335                 message: "Unknown mapping for type : " + e.Type
336             };
337         }
338         row.type = map[e.Type];
339         
340         if (row.type == 'string' && !firstTxtCol.length) {
341             firstTxtCol = row.name;
342         }
343         
344         if (row.type == 'date') {
345             row.dateFormat = 'Y-m-d';
346         }
347         reader.push(row);
348         
349         if (combofields.length == 1 && row.type == 'string') {
350             combofields.push(row);
351         }
352         
353         
354         var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
355         title  = title[0].toUpperCase() + title.substring(1);
356         
357         colmodel.push({
358             "xtype": "ColumnModel",
359             "header": title,
360             "width":  row.type == 'string' ? 200 : 75,
361             "dataIndex": row.name,
362             "|renderer": row.type != 'date' ? 
363                     "function(v) { return String.format('{0}', v); }" :
364                     "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
365             "|xns": "Roo.grid",
366             "*prop": "colModel[]"
367         });
368         var xtype = 'TextField';
369         
370         
371         if (row.type == 'number') {
372             xtype = 'NumberField';
373         }
374         if (row.type == 'date') {
375             xtype = 'DateField';
376         }
377         if (e.Type == 'text') {
378             xtype = 'TextArea';
379         }
380         if (row.name == 'id') {
381             xtype = 'Hidden';
382         } 
383         // what about booleans.. -> checkboxes..
384         
385         
386         
387         form[row.name] = {
388             fieldLabel : title,
389             name : row.name,
390             width : row.type == 'string' ? 200 : 75,
391             '|xns' : 'Roo.form',
392             xtype : xtype
393         }
394          if (xtype == 'DateField') {
395             form[row.name].format = 'Y-m-d';
396             form[row.name].width = 100;
397         }
398         
399         if (xtype == 'TextArea') {
400             form[row.name].height = 100;
401         }
402         if (xtype == 'Hidden') {
403             delete form[row.name].fieldLabel;
404             delete form[row.name].width;
405         }
406         
407     });
408     
409     var combo = {
410         '|xns' : 'Roo.form',
411         xtype: 'ComboBox',
412         allowBlank : false,
413         editable : false,
414         emptyText : 'Select ' + table,
415         forceSelection : true,
416         listWidth : 400,
417         loadingText: 'Searching...',
418         minChars : 2,
419         pageSize : 20,
420         qtip: 'Select ' + table,
421         selectOnFocus: true,
422         triggerAction : 'all',
423         typeAhead: true,
424         
425         width: 300,
426         
427         
428         
429         tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
430         queryParam : '',// SET WHEN USED
431         fieldLabel : table,  // SET WHEN USED
432         valueField : 'id',
433         displayField : '', // SET WHEN USED eg. project_id_name
434         hiddenName : '', // SET WHEN USED eg. project_id
435         name : '', // SET WHEN USED eg. project_id_name
436         items : [
437             {
438                     
439                 '*prop' : 'store',
440                 'xtype' : 'Store',
441                 '|xns' : 'Roo.data',
442                 'remoteSort' : true,
443                 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
444                 listeners : {
445                     '|beforeload' : 'function (_self, o)' +
446                     "{\n" +
447                     "    o.params = o.params || {};\n" +
448                     "    // set more here\n" +
449                     "}\n"
450                 },
451                 items : [
452                     {
453                         '*prop' : 'proxy',
454                         'xtype' : 'HttpProxy',
455                         'method' : 'GET',
456                         '|xns' : 'Roo.data',
457                         '|url' : "baseURL + '/Roo/" + table + ".php'",
458                     },
459                     
460                     {
461                         '*prop' : 'reader',
462                         'xtype' : 'JsonReader',
463                         '|xns' : 'Roo.data',
464                         'id' : 'id',
465                         'root' : 'data',
466                         'totalProperty' : 'total',
467                         '|fields' : JSON.stringify(combofields)
468                         
469                     }
470                 ]
471             }
472         ]
473     }
474     
475     
476     
477     
478     //print(JSON.stringify(reader,null,4));
479     readers.push({
480         table : table ,
481         combo : combo,
482         combofields : combofields,
483         reader :  reader,
484         oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
485         colmodel : colmodel,
486         firstTxtCol : firstTxtCol,
487         form : form
488     });
489     
490     //console.dump(schema );
491     
492      
493 });
494
495
496
497 // merge in the linked tables..
498 readers.forEach(function(reader) {
499     if (typeof(ini[reader.table]) == 'undefined') {
500      
501         return;
502     }
503     print("OVERLAY - " + reader.table);
504     // we have a map..
505     for (var col in ini[reader.table]) {
506         var kv = ini[reader.table][col].split(':');
507         
508         
509         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
510         if (!add) {
511             continue;
512         }
513         // merge in data (eg. project_id => project_id_*****
514      
515         add.oreader.forEach(function(or) {
516             reader.reader.push({
517                 name : col + '_' + or.name,
518                 type : or.type
519             });
520         });
521         
522         // col is mapped to something..
523         var combofields = add.combofields;
524         if (add.combofields.length < 2) {
525             continue;
526         }
527         if (typeof(reader.form[col]) == 'undefined') {
528             print (JSON.stringify(reader.form, null,4));
529             print("missing linked column " + col);
530             continue;
531         }
532         
533         var combofields_name = add.combofields[1].name;
534         var old =   reader.form[col];
535         reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
536         reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
537         reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
538         reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
539         reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
540         reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
541         reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
542         
543              
544     };
545     
546     
547 });
548
549 //readers.forEach(function(reader) {
550 //    delete reader.oreader;
551 //});
552
553  
554
555
556
557 //print(JSON.stringify(readers, null, 4));
558
559 readers.forEach(function(reader) {
560     
561
562     var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader'; 
563     if (!File.isDirectory(dir)) {
564         print("mkdir " + dir);
565         File.mkdir(dir);
566     }
567     
568     // READERS
569     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
570     
571                 
572     var jreader = {
573         '|xns' : 'Roo.data',
574         xtype : "JsonReader",
575         totalProperty : "total",
576         root : "data",
577         '*prop' : "reader",
578         id : 'id', // maybe no..
579        
580         '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
581     };
582     
583     File.write(
584         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
585         JSON.stringify(jreader, null, 4)
586     )
587     
588     
589     // GRIDS
590     dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel'; 
591     if (!File.isDirectory(dir)) {
592         print("mkdir " + dir);
593         File.mkdir(dir);
594     }
595     
596
597     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
598     
599     File.write(
600         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
601             
602        
603         JSON.stringify({
604             '|xns' : 'Roo',
605             xtype : "GridPanel",
606             "title": reader.table,
607             "fitToframe": true,
608             "fitContainer": true,
609             "tableName": reader.table,
610             "background": true,
611             "region" : 'center',
612             "listeners": {
613                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
614             },
615             "items": [
616                 {
617                     "*prop": "grid",
618                     "xtype": "Grid",
619                     "autoExpandColumn": reader.firstTxtCol,
620                     "loadMask": true,
621                     "listeners": {
622                         "|render": "function() \n" +
623                             "{\n" +
624                             "    _this.grid = this; \n" +
625                             "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
626                             "    if (_this.panel.active) {\n" +
627                             "       this.footer.onClick('first');\n" +
628                             "    }\n" +
629                             "}",
630                         "|rowdblclick": "function (_self, rowIndex, e)\n" + 
631                             "{\n" + 
632                             "    if (!_this.dialog) return;\n" + 
633                             "    _this.dialog.show( this.getDataSource().getAt(rowIndex).data, function() {\n" + 
634                             "        _this.grid.footer.onClick('first');\n" + 
635                             "    }); \n" + 
636                             "}\n"
637                     },
638                     "|xns": "Roo.grid",
639
640                     "items": [
641                         {
642                             "*prop": "dataSource",
643                             "xtype": "Store",
644                              remoteSort : true,
645                             '|sortInfo' : "{ field : '" + reader.firstTxtCol  +  "', direction: 'ASC' }", 
646                             "|xns": "Roo.data",
647                             "items": [
648                                 
649                                 {
650                                     "*prop": "proxy",
651                                     "xtype": "HttpProxy",
652                                     "method": "GET",
653                                     "|url": "baseURL + '/Roo/" + reader.table + ".php'",
654                                     "|xns": "Roo.data"
655                                 },
656                                 jreader
657                             ]
658                         },
659                         {
660                             "*prop": "footer",
661                             "xtype": "PagingToolbar",
662                             "pageSize": 25,
663                             "displayInfo": true,
664                             "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
665                             "emptyMsg": "No " + reader.table + " found",
666                             "|xns": "Roo"
667                         },
668                         {
669                             "*prop": "toolbar",
670                             "xtype": "Toolbar",
671                             "|xns": "Roo",
672                             "items": [
673                                 {
674                                     "text": "Add",
675                                     "xtype": "Button",
676                                     "cls": "x-btn-text-icon",
677                                     "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
678                                     "listeners": {
679                                         "|click": "function()\n"+
680                                             "{\n"+
681                                             "    if (!_this.dialog) return;\n" +
682                                             "    _this.dialog.show( { id : 0 } , function() {\n"+
683                                             "        _this.grid.footer.onClick('first');\n"+
684                                             "   }); \n"+
685                                             "}\n"
686                                     },
687                                     "|xns": "Roo.Toolbar"
688                                 },
689                                 {
690                                     "text": "Edit",
691                                     "xtype": "Button",
692                                     "cls": "x-btn-text-icon",
693                                     "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
694                                     "listeners": {
695                                         "|click": "function()\n"+
696                                             "{\n"+
697                                             "    var s = _this.grid.getSelectionModel().getSelections();\n"+
698                                             "    if (!s.length || (s.length > 1))  {\n"+
699                                             "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
700                                             "        return;\n"+
701                                             "    }\n"+
702                                             "    if (!_this.dialog) return;\n" +
703                                             "    _this.dialog.show(s[0].data, function() {\n"+
704                                             "        _this.grid.footer.onClick('first');\n"+
705                                             "    }); \n"+
706                                             "    \n"+
707                                             "}\n" 
708                                         
709                                     },
710                                     "|xns": "Roo.Toolbar"
711                                 },
712                                 {
713                                     "text": "Delete",
714                                     "cls": "x-btn-text-icon",
715                                     "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
716                                     "xtype": "Button",
717                                     "listeners": {
718                                         "|click": "function()\n"+
719                                             "{\n"+
720                                             "     Pman.genericDelete(_this, '" + reader.table + "'); \n"+
721                                             "}\n"+
722                                             "        "
723                                     },
724                                     "|xns": "Roo.Toolbar"
725                                 }
726                             ]
727                         }, // end toolbar
728                     ].concat( reader.colmodel)
729                 }
730             ]
731             
732             
733         }, null, 4)
734     )
735     
736     /// FORMS..
737     
738     dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form'; 
739     if (!File.isDirectory(dir)) {
740         print("mkdir " + dir);
741         File.mkdir(dir);
742     }
743     var formElements = [];
744     var formHeight = 50;
745     for (var k in reader.form) {
746         if (k == 'id') { // should really do primary key testing..
747             continue;
748         }
749         formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
750         
751         formElements.push(reader.form[k]);
752     }
753     if (reader.form['id']) {
754         formElements.push(reader.form['id']);
755     }
756     
757
758     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
759     var frmCfg = 
760     {
761         '|xns' : 'Roo.form',
762         xtype : "Form",
763         listeners : {
764             "|actioncomplete" : "function(_self,action)\n"+
765                 "{\n"+
766                 "    if (action.type == 'setdata') {\n"+
767                 "       //_this.dialog.el.mask(\"Loading\");\n"+
768                 "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
769                 "       return;\n"+
770                 "    }\n"+
771                 "    if (action.type == 'load') {\n"+
772                 "        _this.dialog.el.unmask();\n"+
773                 "        return;\n"+
774                 "    }\n"+
775                 "    if (action.type =='submit') {\n"+
776                 "    \n"+
777                 "        _this.dialog.el.unmask();\n"+
778                 "        _this.dialog.hide();\n"+
779                 "    \n"+
780                 "         if (_this.callback) {\n"+
781                 "            _this.callback.call(_this, _this.form.getValues());\n"+
782                 "         }\n"+
783                 "         _this.form.reset();\n"+
784                 "         return;\n"+
785                 "    }\n"+
786                 "}\n",
787             
788             "|rendered" : "function (form)\n"+
789                 "{\n"+
790                 "    _this.form= form;\n"+
791                 "}\n"
792         },
793         method : "POST",
794         style : "margin:10px;",
795         "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
796         items : formElements
797     };
798     
799     
800     File.write(
801         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
802             
803        
804         JSON.stringify( frmCfg, null, 4)
805     );
806             
807             
808    
809    
810    
811      /// COMBO..
812     
813     dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
814     if (!File.isDirectory(dir)) {
815         print("mkdir " + dir);
816         File.mkdir(dir);
817     }
818    
819     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
820     
821     File.write(
822         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
823             
824        
825         JSON.stringify(reader.combo, null, 4)
826     );
827             
828    
829    
830    
831    
832    
833     // DIALOG.
834    
835    
836     dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog'; 
837     if (!File.isDirectory(dir)) {
838         print("mkdir " + dir);
839         File.mkdir(dir);
840     }
841     var formElements = [];
842     for (var k in reader.form) {
843         if (k == 'id') { // should really do primary key testing..
844             continue;
845         }
846         formElements.push(reader.form[k]);
847     }
848     formElements.push(reader.form['id']);
849
850     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
851     
852     File.write(
853         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
854             
855        
856         JSON.stringify({
857        
858             "closable": false,
859             "collapsible": false,
860             "height": formHeight,
861             "resizable": false,
862             "title": "Edit / Create " + reader.table,
863             "width": 400,
864             "modal" : true,
865             "xtype": "LayoutDialog",
866             "|xns": "Roo",
867             "items": [
868                 {
869                     "|xns": "Roo",
870                     "xtype": "LayoutRegion",
871                     "*prop": "center"
872                 },
873                 {
874                     "region": "center",
875                     "xtype": "ContentPanel",
876                     "|xns": "Roo",
877                     "items": [
878                         frmCfg
879                     ]
880                 },
881                 
882                 {
883                     "listeners": {
884                         "click": "function (_self, e)\n{\n    _this.dialog.hide();\n}"
885                     },
886                     "*prop": "buttons[]",
887                     "text": "Cancel",
888                     "xtype": "Button",
889                     "|xns": "Roo"
890                 },
891                 {
892                     "listeners": {
893                         "click": "function (_self, e)\n{\n    // do some checks?\n     \n    \n    _this.dialog.el.mask(\"Saving\");\n    _this.form.doAction(\"submit\");\n\n}"
894                     },
895                     "*prop": "buttons[]",
896                     "text": "Save",
897                     "xtype": "Button",
898                     "|xns": "Roo"
899                 }
900             ]
901         }, null,4)
902     );
903    
904    
905    
906 });
907
908