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