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