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