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                 if (fn) {
154                     var bits = fn.split('.');
155                     node.xtype = bits.pop();
156                     node['|xns'] = bits.join('.');
157                 }
158                 // fix array???
159                  
160                 
161             }
162             if (!node.items || !node.items.length) {
163                 return;
164             }
165             var _this = this;
166             var aitems = [];
167             var nitems = [];
168             node.items.forEach(function(i) {
169                 
170                 
171                 
172                 _this.fixItems(i, true);
173                 if (i.xtype == 'Array') {
174                     aitems.push(i);
175                     return;
176                 }    
177                 nitems.push(i);
178             });
179             node.items = nitems; 
180             
181             if (!aitems.length) {
182                 return;
183             }
184             
185             aitems.forEach(function(i) {
186                 
187                 if (!i.items || !i.items.length) {
188                     return;
189                 }
190                 var prop = i['*prop'] + '[]';
191                 // colModel to cm?
192                 i.items.forEach(function(c) {
193                     c['*prop']  = prop;
194                     node.items.push(c);
195                     
196                 });
197                 
198                 
199             });
200             
201             
202             // array handling.. 
203             
204             
205             
206             
207             
208         },
209         
210         toSource: function()
211         {
212             // dump the file tree back out to a string.
213             
214             if (typeof(this.items[0]['|module']) != 'undefined') {
215                 return this.toSourceStdClass();
216             }
217             
218             if (this.items[0].region) {
219                 return this.toSourceLayout();
220             }
221             return this.toSourceDialog();
222             
223             
224                 
225                     
226                 
227                     
228                    
229             
230         },
231        
232         outputHeader : function()
233         {
234             return [
235                 "//<script type=\"text/javascript\">",
236                 "",
237                 "// Auto generated file - created by Builder Module - do not edit directly",
238                 ""
239             ].join("\n");
240             
241        
242         },
243         // a standard dialog module.
244         toSourceDialog : function() 
245         {
246             var b = this.items[0]; 
247             var o = this.mungePropObj(this.items[0]);
248             return [
249                 this.outputHeader(),
250                 b['|module'] + " = {",
251                 "",
252                 "    dialog : false,",
253                 "    callback:  false,",
254                 "",   
255                 "    show : function(data, cb)",
256                 "    {",
257                 "        if (!this.dialog) {",
258                 "            this.create();",
259                 "        }",
260                 "",
261                 "        this.callback = cb;",
262                 "        this.data = data;",
263                 "        this.dialog.show();",
264                 "        if (this.form) {",
265                 "           this.form.reset();",
266                 "           this.form.setValues(data);",
267                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
268                 "        }",
269                 "",   
270                 "    },",
271                 "",
272                 "    create : function()",
273                 "    {",
274                 "        var _this = this;",
275                 "        this.dialog = Roo.factory(" + this.objectToJsString(o,3) +  ");",
276                 "    }",
277                 "};",
278                 ""
279                 
280              ].join("\n");
281              
282              
283              
284         },
285         // a layout compoent 
286         toSourceLayout : function() 
287         {
288             var b = this.items[0]; 
289             var o = this.mungePropObj(this.items[0]);
290             
291             var disabled = typeof(b['|disabled']) == 'undefined' ? (b.disabled || false) : b['disabled'];
292             
293             return [
294                 this.outputHeader(),
295                 "",
296                 "",
297                 "// register the module first",
298                 "Pman.on('beforeload', function()",
299                 "{",
300                 "    Pman.register({",
301                 "        modKey : '" + (b.modkey  || "000-unnumbered module") +"',",
302                 "        module : " + b['|module']  + ",",
303                 "        region : '" + (b.region  || '') +"',",
304                 "        parent : " + (b.parent  || false) + ",",
305                 "        name : \"" + (b.name  || "unnamed module") + "\"",
306                 "        disabled : " + disabled +" ",
307                 "    });",
308                 "});",
309                 "",
310                 
311                 b['|module']  +  " = new Roo.util.Observable({",
312                 "",
313                 "    panel : false,",
314                 "    disabled : false,",
315                 "    parentLayout:  false,",
316                 "",
317                 "    add : function(parentLayout, region)",
318                 "    {",
319                 "",
320                 "        var _this = this;", // standard avaialbe..
321                 "        this.parentLayout = parentLayout;",
322                 "",
323                 "        this.panel = parentLayout.addxtype(" + this.objectToJsString(o,3) +  ");",
324                 "        this.layout = this.panel.layout;",
325                 "",
326                 "    }",
327                 "});",
328                 ""
329                  
330                 
331              ].join("\n");
332         },
333             
334         guessName : function(ar) // turns the object into full name.
335         {
336              // eg. xns: Roo, xtype: XXX -> Roo.xxx
337             var ret = [];
338             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
339             
340             
341             
342             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
343                 return false;
344             }
345             var xtype = ar['xtype'] + '';
346             if (xtype == '*') { // prefixes????
347                 xtype  = xtype.substring(1);
348             }
349             if (xtype.match(/^Roo/)) {
350                 // already starts with roo...
351                 ret = [];
352             }
353             ret.push(xtype);
354             var str =  ret.join('.');
355             
356             var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
357             return pm.getPalete('Roo').guessName(ret.join('.'));
358             
359                             
360                                  
361         },
362         /*
363         getTree : function( o ) {
364             
365             
366             
367         }
368         */
369          getHelpUrl : function(cls)
370         {
371             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
372         }
373         
374 });