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