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