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                 _this.modOrder = cfg.modOrder || '001';
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             var d = new Date();
235             var js_src = this.toSource();
236             print("TO SOURCE in " + ((new Date()) - d) + "ms");
237             File.write(js, js_src);
238             
239             
240             
241         },
242          /**
243          * convert xtype for munged output..
244          * 
245          */
246         mungeXtype : function(xtype, els)
247         {
248             var bits = xtype.split('.');
249             // assume it has lenght!
250             
251             els.push("xtype: '"+ bits.pop()+"'");
252             els.push('xns: '+ bits.join('.'));
253         },
254         
255         /**
256          * This needs to use some options on the project
257          * to determine how the file is output..
258          * 
259          * At present we are hard coding it..
260          * 
261          * 
262          */
263         toSource: function()
264         {
265             // dump the file tree back out to a string.
266             
267             // we have 2 types = dialogs and components
268             // 
269             var top = this.guessName(this.items[0]);
270             if (!top) {
271                 return false;
272             }
273             if (top.match(/Dialog/)) {
274                 return this.toSourceDialog();
275             }
276             return this.toSourceLayout();
277             
278             /*
279             eventually support 'classes??'
280              return this.toSourceStdClass();
281             */
282               
283         },
284        
285         outputHeader : function()
286         {
287             return [
288                 "//<script type=\"text/javascript\">",
289                 "",
290                 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
291                 ""
292             ].join("\n");
293             
294        
295         },
296         // a standard dialog module.
297         // fixme - this could be alot neater..
298         toSourceDialog : function() 
299         {
300             var items = JSON.parse(JSON.stringify(this.items[0]));
301             var o = this.mungeToString(items, false, '            ');   
302             return [
303                 this.outputHeader(),
304                 this.name + " = {",
305                 "",
306                 "    dialog : false,",
307                 "    callback:  false,",
308                 "",   
309                 "    show : function(data, cb)",
310                 "    {",
311                 "        if (!this.dialog) {",
312                 "            this.create();",
313                 "        }",
314                 "",
315                 "        this.callback = cb;",
316                 "        this.data = data;",
317                 "        this.dialog.show(this.data._el);",
318                 "        if (this.form) {",
319                 "           this.form.reset();",
320                 "           this.form.setValues(data);",
321                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
322                 "        }",
323                 "",   
324                 "    },",
325                 "",
326                 "    create : function()",
327                 "    {",
328                 "        var _this = this;",
329                 "        this.dialog = Roo.factory(" + o +  ");",
330                 "    }",
331                 "};",
332                 ""
333                 
334              ].join("\n");
335              
336              
337              
338         },
339         // a layout compoent 
340         toSourceLayout : function() 
341         {
342             var items = JSON.parse(JSON.stringify(this.items[0]));
343             var o = this.mungeToString(items, false, '            ');   
344              
345             var modkey = this.modOrder + '-' + this.name.replace('/[^A-Z]+/ig', '-');
346             
347             
348             if (this.name.match(/^Pman/)) {
349                     
350                 
351                 // old BC way we did things..
352                 return [
353                     this.outputHeader(),
354                     "",
355                     "",
356                     "// register the module first",
357                     "Pman.on('beforeload', function()",
358                     "{",
359                     "    Pman.register({",
360                     "        modKey : '" +modkey+"',",
361                     "        module : " + this.name + ",",
362                     "        moduleName : '" + this.name + "',",
363                     "        region : '" + this.region   +"',",
364                     "        parent : " + (this.parent ||  'false') + ",",
365                     "        name : " + JSON.stringify(this.title  || "unnamed module") + ",",
366                     "        disabled : " + (this.disabled || 'false') +", ",
367                     "        permname: '" + (this.permname|| '') +"' ",
368                     "    });",
369                     "});",
370                     "",
371                     
372                     this.name  +  " = new Roo.util.Observable({",
373                     "",
374                     "    panel : false,",
375                     "    disabled : false,",
376                     "    parentLayout:  false,",
377                     "",
378                     "    add : function(parentLayout, region)",
379                     "    {",
380                     "",
381                     "        var _this = this;", // standard avaialbe..
382                     "        this.parentLayout = parentLayout;",
383                     "",
384                     "        this.panel = parentLayout.addxtype(" + o +  ");",
385                     "        this.layout = this.panel.layout;",
386                     "",
387                     "    }",
388                     "});",
389                     ""
390                      
391                     
392                  ].join("\n");
393             }
394             
395         
396             return [
397                 this.outputHeader(),
398                 
399                 this.name  +  " = new Roo.XComponent({",
400                 "    order    : '" +modkey+"',",
401                 "    region   : '" + this.region   +"',",
402                 "    parent   : "+ (this.parent ?  "'" + this.parent + "'" :  'false') + ",",
403                 "    name     : " + JSON.stringify(this.title  || "unnamed module") + ",",
404                 "    disabled : " + (this.disabled || 'false') +", ",
405                 "    tree : function()",
406                 "    {",
407                 "        var _this = this;", // bc
408                 "        var MODULE = this;", /// this looks like a better name.
409                 "        return " + o + ';',
410                 "    }",
411                 "});",
412                 ""
413                  
414              ].join("\n");
415             
416         },
417             
418         guessName : function(ar) // turns the object into full name.
419         {
420              // eg. xns: Roo, xtype: XXX -> Roo.xxx
421             if (!ar) {
422                 return false;
423             }
424             var ret = [];
425             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
426             
427             
428             
429             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
430                 return false;
431             }
432             var xtype = ar['xtype'] + '';
433             if (xtype[0] == '*') { // prefixes????
434                 xtype  = xtype.substring(1);
435             }
436             if (xtype.match(/^Roo/)) {
437                 // already starts with roo...
438                 ret = [];
439             }
440             ret.push(xtype);
441             var str =  ret.join('.');
442             
443             
444             
445             var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
446             return pm.getPalete('Roo').guessName(ret.join('.'));
447             
448                             
449                                  
450         },
451         /*
452         getTree : function( o ) {
453             
454             
455             
456         }
457         */
458          getHelpUrl : function(cls)
459         {
460             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
461         }
462         
463 });