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