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 isNull, " + 
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", << or is null
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));    Seed.quit();
299     
300     schema.forEach(function(e)  {
301         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
302         var row  = { }; 
303         if (type) {
304             e.Type = type[1];
305             e.Size = type[2];
306         }
307         
308         
309         
310         row.name = e.Field;
311         
312         
313         if (typeof(map[e.Type]) == 'undefined') {
314            console.dump(e);
315            throw {
316                 name: "ArgumentError", 
317                 message: "Unknown mapping for type : " + e.Type
318             };
319         }
320         row.type = map[e.Type];
321         
322         if (row.type == 'string' && !firstTxtCol.length) {
323             firstTxtCol = row.name;
324         }
325         
326         if (row.type == 'date') {
327             row.dateFormat = 'Y-m-d';
328         }
329         reader.push(row);
330         
331         if (combofields.length == 1 && row.type == 'string') {
332             combofields.push(row);
333         }
334         
335         
336         var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
337         title  = title[0].toUpperCase() + title.substring(1);
338         
339         colmodel.push({
340             "xtype": "ColumnModel",
341             "header": title,
342             "width":  row.type == 'string' ? 200 : 75,
343             "dataIndex": row.name,
344             "|renderer": row.type != 'date' ? 
345                     "function(v) { return String.format('{0}', v); }" :
346                     "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
347             "|xns": "Roo.grid",
348             "*prop": "colModel[]"
349         });
350         var xtype = 'TextField';
351         if (row.type == 'number') {
352             xtype = 'NumberField';
353         }
354         if (row.type == 'date') {
355             xtype = 'DateField';
356         }
357         if (e.Type == 'text') {
358             xtype = 'TextArea';
359         }
360         if (row.name == 'id') {
361             xtype = 'Hidden';
362         } 
363         // what about booleans.. -> checkboxes..
364         
365         
366         
367         form[row.name] = {
368             fieldLabel : title,
369             name : row.name,
370             width : row.type == 'string' ? 200 : 75,
371             '|xns' : 'Roo.form',
372             xtype : xtype
373         }
374         if (xtype == 'TextArea') {
375             form[row.name].height = 100;
376         }
377         if (xtype == 'Hidden') {
378             delete form[row.name].fieldLabel;
379             delete form[row.name].width;
380         }
381         
382     });
383     
384     var combo = {
385         '|xns' : 'Roo.form',
386         xtype: 'ComboBox',
387         allowBlank : 'false',
388         editable : 'false',
389         emptyText : 'Select ' + table,
390         forceSelection : true,
391         listWidth : 400,
392         loadingText: 'Searching...',
393         minChars : 2,
394         pageSize : 20,
395         qtip: 'Select ' + table,
396         selectOnFocus: true,
397         triggerAction : 'all',
398         typeAhead: true,
399         
400         width: 300,
401         
402         
403         
404         tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
405         queryParam : '',// SET WHEN USED
406         fieldLabel : table,  // SET WHEN USED
407         valueField : 'id',
408         displayField : '', // SET WHEN USED eg. project_id_name
409         hiddenName : '', // SET WHEN USED eg. project_id
410         name : '', // SET WHEN USED eg. project_id_name
411         items : [
412             {
413                     
414                 '*prop' : 'store',
415                 'xtype' : 'Store',
416                 '|xns' : 'Roo.data',
417                 'remoteSort' : true,
418                 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
419                 listeners : {
420                     '|beforeload' : 'function (_self, o)' +
421                     "{\n" +
422                     "    o.params = o.params || {};\n" +
423                     "    // set more here\n" +
424                     "}\n"
425                 },
426                 items : [
427                     {
428                         '*prop' : 'proxy',
429                         'xtype' : 'HttpProxy',
430                         'method' : 'GET',
431                         '|xns' : 'Roo.data',
432                         '|url' : "baseURL + '/Roo/" + table + ".php'",
433                     },
434                     
435                     {
436                         '*prop' : 'reader',
437                         'xtype' : 'JsonReader',
438                         '|xns' : 'Roo.data',
439                         'id' : 'id',
440                         'root' : 'data',
441                         'totalProperty' : 'total',
442                         '|fields' : JSON.stringify(combofields)
443                         
444                     }
445                 ]
446             }
447         ]
448     }
449     
450     
451     
452     
453     //print(JSON.stringify(reader,null,4));
454     readers.push({
455         table : table ,
456         combo : combo,
457         combofields : combofields,
458         reader :  reader,
459         oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
460         colmodel : colmodel,
461         firstTxtCol : firstTxtCol,
462         form : form
463     });
464     
465     //console.dump(schema );
466     
467      
468 });
469
470
471
472 // merge in the linked tables..
473 readers.forEach(function(reader) {
474     if (typeof(ini[reader.table]) == 'undefined') {
475      
476         return;
477     }
478     print("OVERLAY - " + reader.table);
479     // we have a map..
480     for (var col in ini[reader.table]) {
481         var kv = ini[reader.table][col].split(':');
482         
483         
484         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
485         if (!add) {
486             continue;
487         }
488         // merge in data (eg. project_id => project_id_*****
489      
490         add.oreader.forEach(function(or) {
491             reader.reader.push({
492                 name : col + '_' + or.name,
493                 type : or.type
494             });
495         });
496         
497         // col is mapped to something..
498         var combofields = add.combofields;
499         if (add.combofields.length < 2) {
500             continue;
501         }
502         if (typeof(reader.form[col]) == 'undefined') {
503             print (JSON.stringify(reader.form, null,4));
504             print("missing linked column " + col);
505             continue;
506         }
507         
508         var combofields_name = add.combofields[1].name;
509         var old =   reader.form[col];
510         reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
511         reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
512         reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
513         reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
514         reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
515         reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
516         reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
517         
518              
519     };
520     
521     
522 });
523
524 //readers.forEach(function(reader) {
525 //    delete reader.oreader;
526 //});
527
528  
529
530
531
532 //print(JSON.stringify(readers, null, 4));
533
534 readers.forEach(function(reader) {
535     
536
537     var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader'; 
538     if (!File.isDirectory(dir)) {
539         print("mkdir " + dir);
540         File.mkdir(dir);
541     }
542     
543     // READERS
544     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
545     
546                 
547     var jreader = {
548         '|xns' : 'Roo.data',
549         xtype : "JsonReader",
550         totalProperty : "total",
551         root : "data",
552         '*prop' : "reader",
553         id : 'id', // maybe no..
554        
555         '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
556     };
557     
558     File.write(
559         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
560         JSON.stringify(jreader, null, 4)
561     )
562     
563     
564     // GRIDS
565     dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel'; 
566     if (!File.isDirectory(dir)) {
567         print("mkdir " + dir);
568         File.mkdir(dir);
569     }
570     
571
572     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
573     
574     File.write(
575         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
576             
577        
578         JSON.stringify({
579             '|xns' : 'Roo',
580             xtype : "GridPanel",
581             "title": reader.table,
582             "fitToframe": true,
583             "fitContainer": true,
584             "tableName": reader.table,
585             "background": true,
586             "region" : 'center',
587             "listeners": {
588                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
589             },
590             "items": [
591                 {
592                     "*prop": "grid",
593                     "xtype": "Grid",
594                     "autoExpandColumn": reader.firstTxtCol,
595                     "loadMask": true,
596                     "listeners": {
597                         "|render": "function() \n" +
598                             "{\n" +
599                             "    _this.grid = this; \n" +
600                             "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
601                             "    if (_this.panel.active) {\n" +
602                             "       this.footer.onClick('first');\n" +
603                             "    }\n" +
604                             "}",
605                         "|rowdblclick": "function (_self, rowIndex, e)\n" + 
606                             "{\n" + 
607                             "    if (!_this.dialog) return;\n" + 
608                             "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
609                             "        _this.grid.footer.onClick('first');\n" + 
610                             "    }); \n" + 
611                             "}\n"
612                     },
613                     "|xns": "Roo.grid",
614
615                     "items": [
616                         {
617                             "*prop": "dataSource",
618                             "xtype": "Store",
619                              remoteSort : true,
620                             '|sortInfo' : "{ field : '" + reader.firstTxtCol  +  "', direction: 'ASC' }", 
621                             "|xns": "Roo.data",
622                             "items": [
623                                 
624                                 {
625                                     "*prop": "proxy",
626                                     "xtype": "HttpProxy",
627                                     "method": "GET",
628                                     "|url": "baseURL + '/Roo/" + reader.table + ".php'",
629                                     "|xns": "Roo.data"
630                                 },
631                                 jreader
632                             ]
633                         },
634                         {
635                             "*prop": "footer",
636                             "xtype": "PagingToolbar",
637                             "pageSize": 25,
638                             "displayInfo": true,
639                             "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
640                             "emptyMsg": "No " + reader.table + " found",
641                             "|xns": "Roo"
642                         },
643                         {
644                             "*prop": "toolbar",
645                             "xtype": "Toolbar",
646                             "|xns": "Roo",
647                             "items": [
648                                 {
649                                     "text": "Add",
650                                     "xtype": "Button",
651                                     "cls": "x-btn-text-icon",
652                                     "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
653                                     "listeners": {
654                                         "|click": "function()\n"+
655                                             "{\n"+
656                                             "    if (!_this.dialog) return;\n" +
657                                             "    _this.dialog.show( { id : 0 } , function() {\n"+
658                                             "        _this.grid.footer.onClick('first');\n"+
659                                             "   }); \n"+
660                                             "}\n"
661                                     },
662                                     "|xns": "Roo.Toolbar"
663                                 },
664                                 {
665                                     "text": "Edit",
666                                     "xtype": "Button",
667                                     "cls": "x-btn-text-icon",
668                                     "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
669                                     "listeners": {
670                                         "|click": "function()\n"+
671                                             "{\n"+
672                                             "    var s = _this.grid.getSelectionModel().getSelections();\n"+
673                                             "    if (!s.length || (s.length > 1))  {\n"+
674                                             "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
675                                             "        return;\n"+
676                                             "    }\n"+
677                                             "    if (!_this.dialog) return;\n" +
678                                             "    _this.dialog.show(s[0].data, function() {\n"+
679                                             "        _this.grid.footer.onClick('first');\n"+
680                                             "    }); \n"+
681                                             "    \n"+
682                                             "}\n" 
683                                         
684                                     },
685                                     "|xns": "Roo.Toolbar"
686                                 },
687                                 {
688                                     "text": "Delete",
689                                     "cls": "x-btn-text-icon",
690                                     "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
691                                     "xtype": "Button",
692                                     "listeners": {
693                                         "|click": "function()\n"+
694                                             "{\n"+
695                                             "     Pman.genericDelete(_this, '" + reader.table + "'); \n"+
696                                             "}\n"+
697                                             "        "
698                                     },
699                                     "|xns": "Roo.Toolbar"
700                                 }
701                             ]
702                         }, // end toolbar
703                     ].concat( reader.colmodel)
704                 }
705             ]
706             
707             
708         }, null, 4)
709     )
710     
711     /// FORMS..
712     
713     dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form'; 
714     if (!File.isDirectory(dir)) {
715         print("mkdir " + dir);
716         File.mkdir(dir);
717     }
718     var formElements = [];
719     var formHeight = 50;
720     for (var k in reader.form) {
721         if (k == 'id') { // should really do primary key testing..
722             continue;
723         }
724         formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
725         
726         formElements.push(reader.form[k]);
727     }
728     if (reader.form['id']) {
729         formElements.push(reader.form['id']);
730     }
731     
732
733     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
734     var frmCfg = 
735     {
736         '|xns' : 'Roo.form',
737         xtype : "Form",
738         listeners : {
739             "|actioncomplete" : "function(_self,action)\n"+
740                 "{\n"+
741                 "    if (action.type == 'setdata') {\n"+
742                 "       //_this.dialog.el.mask(\"Loading\");\n"+
743                 "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
744                 "       return;\n"+
745                 "    }\n"+
746                 "    if (action.type == 'load') {\n"+
747                 "        _this.dialog.el.unmask();\n"+
748                 "        return;\n"+
749                 "    }\n"+
750                 "    if (action.type =='submit') {\n"+
751                 "    \n"+
752                 "        _this.dialog.el.unmask();\n"+
753                 "        _this.dialog.hide();\n"+
754                 "    \n"+
755                 "         if (_this.callback) {\n"+
756                 "            _this.callback.call(_this, _this.form.getValues());\n"+
757                 "         }\n"+
758                 "         _this.form.reset();\n"+
759                 "         return;\n"+
760                 "    }\n"+
761                 "}\n",
762             
763             "|rendered" : "function (form)\n"+
764                 "{\n"+
765                 "    _this.form= form;\n"+
766                 "}\n"
767         },
768         method : "POST",
769         style : "margin:10px;",
770         "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
771         items : formElements
772     };
773     
774     
775     File.write(
776         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
777             
778        
779         JSON.stringify( frmCfg, null, 4)
780     );
781             
782             
783    
784    
785    
786      /// COMBO..
787     
788     dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
789     if (!File.isDirectory(dir)) {
790         print("mkdir " + dir);
791         File.mkdir(dir);
792     }
793    
794     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
795     
796     File.write(
797         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
798             
799        
800         JSON.stringify(reader.combo, null, 4)
801     );
802             
803    
804    
805    
806    
807    
808     // DIALOG.
809    
810    
811     dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog'; 
812     if (!File.isDirectory(dir)) {
813         print("mkdir " + dir);
814         File.mkdir(dir);
815     }
816     var formElements = [];
817     for (var k in reader.form) {
818         if (k == 'id') { // should really do primary key testing..
819             continue;
820         }
821         formElements.push(reader.form[k]);
822     }
823     formElements.push(reader.form['id']);
824
825     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
826     
827     File.write(
828         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
829             
830        
831         JSON.stringify({
832        
833             "closable": false,
834             "collapsible": false,
835             "height": formHeight,
836             "resizable": false,
837             "title": "Edit / Create " + reader.table,
838             "width": 400,
839             "xtype": "LayoutDialog",
840             "|xns": "Roo",
841             "items": [
842                 {
843                     "|xns": "Roo",
844                     "xtype": "LayoutRegion",
845                     "*prop": "center"
846                 },
847                 {
848                     "region": "center",
849                     "xtype": "ContentPanel",
850                     "|xns": "Roo",
851                     "items": [
852                         frmCfg
853                     ]
854                 },
855                 
856                 {
857                     "listeners": {
858                         "click": "function (_self, e)\n{\n    _this.dialog.hide();\n}"
859                     },
860                     "*prop": "buttons[]",
861                     "text": "Cancel",
862                     "xtype": "Button",
863                     "|xns": "Roo"
864                 },
865                 {
866                     "listeners": {
867                         "click": "function (_self, e)\n{\n    // do some checks?\n     \n    \n    _this.dialog.el.mask(\"Saving\");\n    _this.form.doAction(\"submit\");\n\n}"
868                     },
869                     "*prop": "buttons[]",
870                     "text": "Save",
871                     "xtype": "Button",
872                     "|xns": "Roo"
873                 }
874             ]
875         }, null,4)
876     );
877    
878    
879    
880 });
881
882