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