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