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