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         // fixme - this could be alot neater..
285         toSourceDialog : function() 
286         {
287             var items = JSON.parse(JSON.stringify(this.items[0]));
288             var o = this.mungeToString(items, false, '            ');   
289             return [
290                 this.outputHeader(),
291                 this.name + " = {",
292                 "",
293                 "    dialog : false,",
294                 "    callback:  false,",
295                 "",   
296                 "    show : function(data, cb)",
297                 "    {",
298                 "        if (!this.dialog) {",
299                 "            this.create();",
300                 "        }",
301                 "",
302                 "        this.callback = cb;",
303                 "        this.data = data;",
304                 "        this.dialog.show();",
305                 "        if (this.form) {",
306                 "           this.form.reset();",
307                 "           this.form.setValues(data);",
308                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
309                 "        }",
310                 "",   
311                 "    },",
312                 "",
313                 "    create : function()",
314                 "    {",
315                 "        var _this = this;",
316                 "        this.dialog = Roo.factory(" + o +  ");",
317                 "    }",
318                 "};",
319                 ""
320                 
321              ].join("\n");
322              
323              
324              
325         },
326         // a layout compoent 
327         toSourceLayout : function() 
328         {
329             var items = JSON.parse(JSON.stringify(this.items[0]));
330             var o = this.mungeToString(items, false, '            ');   
331              
332             var modkey = this.modOrder + '-' + this.name.replace('/[^A-Z]+/ig', '-');
333             
334             
335             if (this.name.match(/^Pman/)) {
336                     
337                 
338                 // old BC way we did things..
339                 return [
340                     this.outputHeader(),
341                     "",
342                     "",
343                     "// register the module first",
344                     "Pman.on('beforeload', function()",
345                     "{",
346                     "    Pman.register({",
347                     "        modKey : '" +modkey+"',",
348                     "        module : " + this.name + ",",
349                     "        region : '" + this.region   +"',",
350                     "        parent : " + (this.parent ||  'false') + ",",
351                     "        name : " + JSON.stringify(this.title  || "unnamed module") + ",",
352                     "        disabled : " + (this.disabled || 'false') +" ",
353                     "    });",
354                     "});",
355                     "",
356                     
357                     this.name  +  " = new Roo.util.Observable({",
358                     "",
359                     "    panel : false,",
360                     "    disabled : false,",
361                     "    parentLayout:  false,",
362                     "",
363                     "    add : function(parentLayout, region)",
364                     "    {",
365                     "",
366                     "        var _this = this;", // standard avaialbe..
367                     "        this.parentLayout = parentLayout;",
368                     "",
369                     "        this.panel = parentLayout.addxtype(" + o +  ");",
370                     "        this.layout = this.panel.layout;",
371                     "",
372                     "    }",
373                     "});",
374                     ""
375                      
376                     
377                  ].join("\n");
378             }
379             
380         
381             return [
382                 this.outputHeader(),
383                 
384                 this.name  +  " = new Roo.XComponent({",
385                 "    order    : '" +modkey+"',",
386                 "    region   : '" + this.region   +"',",
387                 "    parent   : '"+ (this.parent ||  'false') + "',",
388                 "    name     : " + JSON.stringify(this.title  || "unnamed module") + ",",
389                 "    disabled : " + (this.disabled || 'false') +", ",
390                 "    items : [ ",
391                 "        " + o,
392                 "    ]",
393                 "});",
394                 ""
395                  
396              ].join("\n");
397             
398         },
399             
400         guessName : function(ar) // turns the object into full name.
401         {
402              // eg. xns: Roo, xtype: XXX -> Roo.xxx
403             if (!ar) {
404                 return false;
405             }
406             var ret = [];
407             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
408             
409             
410             
411             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
412                 return false;
413             }
414             var xtype = ar['xtype'] + '';
415             if (xtype[0] == '*') { // prefixes????
416                 xtype  = xtype.substring(1);
417             }
418             if (xtype.match(/^Roo/)) {
419                 // already starts with roo...
420                 ret = [];
421             }
422             ret.push(xtype);
423             var str =  ret.join('.');
424             
425             
426             
427             var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
428             return pm.getPalete('Roo').guessName(ret.join('.'));
429             
430                             
431                                  
432         },
433         /*
434         getTree : function( o ) {
435             
436             
437             
438         }
439         */
440          getHelpUrl : function(cls)
441         {
442             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
443         }
444         
445 });