Builder/Provider/File/Roo.js
[app.Builder.js] / Builder / Provider / File / Roo.js
1 //<Script type="text/javascript">
2
3  
4 Gio = imports.gi.Gio;
5 console = imports.console;
6 XObject = imports.XObject.XObject;
7
8   
9 //----------------------- our roo verison
10 Base = imports.Builder.Provider.File.Base.Base;
11 Gio = imports.gi.Gio;
12 File = imports.File.File;
13
14 //JSDOC =  imports['JSDOC.js'];
15 //----------------------- our roo verison
16
17 var rid = 0;
18
19 Roo = XObject.define(
20     function(cfg) {
21         
22         // id ,
23         //"name":"Edit Module Details",
24         // items : 
25         //"btype":"FORM", // was to be components...
26         //"app":"Builder",
27         //"module":"Pman.Tab.BuilderTop2"
28         //console.dump(cfg);
29         
30         if (!cfg.name || !cfg.fullname ) {
31             cfg.name = cfg.path.split('/').pop().replace(/\.bjs$/, '');
32             //cfg.fullname = (cfg.parent && cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
33             cfg.fullname = cfg.name;
34         }
35         
36         
37         this.items = false;
38         if (cfg.json) {
39             var jstr =  JSON.parse(cfg.json);
40             this.items = [ jstr ];
41             //console.log(cfg.items.length);
42             delete cfg.json; // not needed!
43         }
44         this.cn = [];
45          /*
46         var p = cfg.items && cfg.items.length && cfg.items[0].parent ? cfg.items[0].parent : false;
47         
48         // ensure real data is set...
49         Roo.apply(this, {
50             name : cfg.module,
51             parent : p,
52             title : cfg.name,
53             project : cfg.app
54             
55         });
56         
57         this.cn = [];
58         */
59         Roo.superclass.constructor.call(this, cfg);
60
61         
62         // super?!?!
63         this.id = 'roo-file-' + (rid++);
64         // various loader methods..
65     },
66     Base,
67     {
68         
69         modOrder : '001', /// sequence id that this uses.
70         region : 'center',
71         parent : '',
72         title : '', // the title on displayed when loading.
73         disable : '', // use a function to that returns false to disable this..
74         
75         setNSID : function(id)
76         {
77             
78             this.items[0]['|module'] = id;
79        
80             
81         },
82         
83         
84         getType: function() {
85             return 'Roo';
86         },
87         
88       
89         loadItems : function(cb)
90         {
91             console.log("load Items!");
92             if (this.items !== false) {
93                 return false;
94             }
95             var file = Gio.file_new_for_path(this.path);
96             
97             var _this = this;                        
98             file.read_async(0, null, function(source,result) {
99                 var stream = source.read_finish(result)
100                 var dstream = new Gio.DataInputStream.c_new(stream);
101                 
102                 var src = dstream.read_until("")
103                 
104                 var cfg = JSON.parse(src);
105                 print("loaded data");
106                 console.dump(cfg);
107                 
108                 _this.name = cfg.name.replace(/\.bjs/, ''); // BC!
109                 _this.parent =  cfg.parent;
110                 _this.title =  cfg.title;
111                 _this.items = cfg.items || []; 
112                 
113                 _this.fixItems(_this, false);
114                 
115                 
116                 
117                 cb();
118                 /*
119                 var tstream =  new JSDOC.TextStream(src);
120                 var tr = new  JSDOC.TokenReader( {
121                     keepWhite : true,
122                     keepComments : true
123                 });
124                 var toks = tr.tokenize(tstream);
125                 var ts = new JSDOC.Collapse(toks);
126                 //console.dump(ts.tokens);
127                 var rd = new JSDOC.RooFile(ts.tokens);
128                 try {
129                     rd.parse();
130                 } catch (e) {
131                     console.log(e);
132                     _this.items = [ src ];
133                     cb();
134                     return;
135                 }
136                  
137                 console.dump(rd.cfg);
138                 //!!??? have we got engouth!
139                 // try parsing self..
140                 _this.items = [ rd.cfg ];
141                 cb();
142                 */
143                 
144             });
145             
146             
147             
148         },
149         /**
150          * old code had broken xtypes and used arrays differently,
151          * this code should try and clean it up..
152          * 
153          * 
154          */
155         fixItems : function(node, fixthis)
156         {
157             if (fixthis) {
158                 // fix xtype.
159                 var fn = this.guessName(node);
160                 //print("guessname got " + fn);
161                 if (fn) {
162                     var bits = fn.split('.');
163                     node.xtype = bits.pop();
164                     node['|xns'] = bits.join('.');
165                     
166                 }
167                 // fix array???
168                  
169                 
170             }
171             if (!node.items || !node.items.length) {
172                 return;
173             }
174             var _this = this;
175             var aitems = [];
176             var nitems = [];
177             node.items.forEach(function(i) {
178                 
179                 
180                 
181                 _this.fixItems(i, true);
182                 if (i.xtype == 'Array') {
183                     aitems.push(i);
184                     return;
185                 }    
186                 nitems.push(i);
187             });
188             node.items = nitems; 
189             
190             if (!aitems.length) {
191                 return;
192             }
193             
194             aitems.forEach(function(i) {
195                 
196                 if (!i.items || !i.items.length) {
197                     return;
198                 }
199                 var prop = i['*prop'] + '[]';
200                 // colModel to cm?
201                 i.items.forEach(function(c) {
202                     c['*prop']  = prop;
203                     node.items.push(c);
204                     
205                 });
206                 
207                 
208             });
209             
210             
211             // array handling.. 
212             
213             
214             
215             
216             
217         },
218         
219         save : function()
220         {
221             Base.prototype.save.call(this);
222             // now write the js file..
223             var js = this.path.replace(/\.bjs$/, '.js');
224             File.write(js, this.toSource());
225             
226             
227             
228         },
229          /**
230          * convert xtype for munged output..
231          * 
232          */
233         mungeXtype : function(xtype, els)
234         {
235             var bits = xtype.split('.');
236             // assume it has lenght!
237             
238             els.push("xtype: '"+ bits.pop()+"'");
239             els.push('xns: '+ bits.join('.'));
240         },
241         
242         /**
243          * This needs to use some options on the project
244          * to determine how the file is output..
245          * 
246          * At present we are hard coding it..
247          * 
248          * 
249          */
250         toSource: function()
251         {
252             // dump the file tree back out to a string.
253             
254             // we have 2 types = dialogs and components
255             // 
256             var top = this.guessName(this.items[0]);
257             if (!top) {
258                 return false;
259             }
260             if (top.match(/Dialog/)) {
261                 return this.toSourceDialog();
262             }
263             return this.toSourceLayout();
264             
265             /*
266             eventually support 'classes??'
267              return this.toSourceStdClass();
268             */
269               
270         },
271        
272         outputHeader : function()
273         {
274             return [
275                 "//<script type=\"text/javascript\">",
276                 "",
277                 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
278                 ""
279             ].join("\n");
280             
281        
282         },
283         // a standard dialog module.
284         toSourceDialog : function() 
285         {
286             
287             var o = this.mungeToString(this.items[0], false, '            ');   
288             return [
289                 this.outputHeader(),
290                 this.name + " = {",
291                 "",
292                 "    dialog : false,",
293                 "    callback:  false,",
294                 "",   
295                 "    show : function(data, cb)",
296                 "    {",
297                 "        if (!this.dialog) {",
298                 "            this.create();",
299                 "        }",
300                 "",
301                 "        this.callback = cb;",
302                 "        this.data = data;",
303                 "        this.dialog.show();",
304                 "        if (this.form) {",
305                 "           this.form.reset();",
306                 "           this.form.setValues(data);",
307                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
308                 "        }",
309                 "",   
310                 "    },",
311                 "",
312                 "    create : function()",
313                 "    {",
314                 "        var _this = this;",
315                 "        this.dialog = Roo.factory(" + o +  ");",
316                 "    }",
317                 "};",
318                 ""
319                 
320              ].join("\n");
321              
322              
323              
324         },
325         // a layout compoent 
326         toSourceLayout : function() 
327         {
328             
329             var o = this.mungeToString(this.items[0], false, '            ');
330             
331             var modkey = this.modOrder + '-' + this.name.replace('/[^A-Z]+/ig', '-');
332             
333             
334             return [
335                 this.outputHeader(),
336                 "",
337                 "",
338                 "// register the module first",
339                 "Pman.on('beforeload', function()",
340                 "{",
341                 "    Pman.register({",
342                 "        modKey : '" +modkey+"',",
343                 "        module : " + this.name + ",",
344                 "        region : '" + this.region   +"',",
345                 "        parent : " + (this.parent ||  'false') + ",",
346                 "        name : " + JSON.stringify(this.title  || "unnamed module") + ",",
347                 "        disabled : " + (this.disabled || 'false') +" ",
348                 "    });",
349                 "});",
350                 "",
351                 
352                 this.name  +  " = new Roo.util.Observable({",
353                 "",
354                 "    panel : false,",
355                 "    disabled : false,",
356                 "    parentLayout:  false,",
357                 "",
358                 "    add : function(parentLayout, region)",
359                 "    {",
360                 "",
361                 "        var _this = this;", // standard avaialbe..
362                 "        this.parentLayout = parentLayout;",
363                 "",
364                 "        this.panel = parentLayout.addxtype(" + o +  ");",
365                 "        this.layout = this.panel.layout;",
366                 "",
367                 "    }",
368                 "});",
369                 ""
370                  
371                 
372              ].join("\n");
373         },
374             
375         guessName : function(ar) // turns the object into full name.
376         {
377              // eg. xns: Roo, xtype: XXX -> Roo.xxx
378             if (!ar) {
379                 return false;
380             }
381             var ret = [];
382             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
383             
384             
385             
386             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
387                 return false;
388             }
389             var xtype = ar['xtype'] + '';
390             if (xtype[0] == '*') { // prefixes????
391                 xtype  = xtype.substring(1);
392             }
393             if (xtype.match(/^Roo/)) {
394                 // already starts with roo...
395                 ret = [];
396             }
397             ret.push(xtype);
398             var str =  ret.join('.');
399             
400             
401             
402             var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
403             return pm.getPalete('Roo').guessName(ret.join('.'));
404             
405                             
406                                  
407         },
408         /*
409         getTree : function( o ) {
410             
411             
412             
413         }
414         */
415          getHelpUrl : function(cls)
416         {
417             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
418         }
419         
420 });