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