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