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