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