tools/build_gtk_tree.js
[app.Builder.js] / JsRender / Roo.js
1 //<Script type="text/javascript">
2
3  
4 Gio = imports.gi.Gio;
5 XObject = imports.XObject.XObject;
6
7   
8 //----------------------- our roo verison
9 Base = imports.JsRender.Base.Base;
10 File = imports.File.File;
11
12
13 //----------------------- our roo verison
14
15 var rid = 0;
16
17 Roo = XObject.define(
18     function(cfg) {
19         
20         // id ,
21         //"name":"Edit Module Details",
22         // items : 
23         //"btype":"FORM", // was to be components...
24         //"app":"Builder",
25         //"module":"Pman.Tab.BuilderTop2"
26         //console.dump(cfg);
27         
28         if (!cfg.name || !cfg.fullname ) {
29             cfg.name = cfg.path.split('/').pop().replace(/\.bjs$/, '').replace(/\.js$/, '');
30             //cfg.fullname = (cfg.parent && cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
31             cfg.fullname = cfg.name;
32         }
33         
34         
35         this.items = false;
36         if (cfg.json) {
37             var jstr =  JSON.parse(cfg.json);
38             this.items = [ jstr ];
39             //console.log(cfg.items.length);
40             delete cfg.json; // not needed!
41         }
42         this.cn = [];
43         
44         Roo.superclass.constructor.call(this, cfg);
45
46         
47         // super?!?!
48         this.id = 'roo-file-' + (rid++);
49         // various loader methods..
50     },
51     Base,
52     {
53         doubleStringProps : [ 
54             'title',
55             'legend',
56             'loadingText',
57             'emptyText',
58             'qtip',
59             'value',
60             'text',
61             'emptyMsg',
62             'displayMsg'
63         ],
64         path : '', // the file path..
65         modOrder : '001', /// sequence id that this uses.
66         region : 'center',
67         parent : '',
68         title : '', // the title on displayed when loading.
69         disable : '', // use a function to that returns false to disable this..
70         permname: '', /// permission name
71         
72         items: false,
73         
74         setNSID : function(id)
75         {
76             
77             this.items[0]['|module'] = id;
78        
79             
80         },
81         
82         
83         getType: function() {
84             return 'Roo';
85         },
86        
87       
88         loadItems : function(cb, sync)
89         {
90             
91             
92             
93             print("load Items!");
94             if (this.items !== false) {
95                 return false;
96             }
97             
98             var _this = this;
99              
100             function loaded(src) {
101                 var cfg = JSON.parse(src);
102                 print("loaded data");
103                 //print(JSON.stringify(cfg, null,4));
104                 _this.modOrder = cfg.modOrder || '001';
105                 _this.name = cfg.name.replace(/\.bjs/, ''); // BC!
106                 _this.parent =  cfg.parent;
107                 _this.permname =  cfg.permname || '';
108                 _this.title =  cfg.title;
109                 _this.items = cfg.items || []; 
110                 
111                 _this.fixItems(_this, false);
112                 
113                 
114                 
115                 cb();
116             }
117             if (sync) {
118                 loaded(File.read(this.path));
119                 return true;
120             }
121             
122             
123             var file = Gio.file_new_for_path(this.path);
124             
125                              
126             file.read_async(0, null, function(source,result) {
127                 var stream = source.read_finish(result)
128                 var dstream = new Gio.DataInputStream.c_new(stream);
129                 
130                 loaded (dstream.read_until(""));
131                 
132                 
133                  
134                 
135             });
136             
137             return true;
138             
139         },
140         /**
141          * old code had broken xtypes and used arrays differently,
142          * this code should try and clean it up..
143          * 
144          * 
145          */
146         fixItems : function(node, fixthis)
147         {
148             if (fixthis) {
149                 // fix xtype.
150                 var fn = this.guessName(node);
151                 //print("guessname got " + fn);
152                 if (fn) {
153                     var bits = fn.split('.');
154                     node.xtype = bits.pop();
155                     node['|xns'] = bits.join('.');
156                     
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         save : function()
211         {
212             Base.prototype.save.call(this);
213             // now write the js file..
214             var js = this.path.replace(/\.bjs$/, '.js');
215             var d = new Date();
216             var js_src = this.toSource();
217             print("TO SOURCE in " + ((new Date()) - d) + "ms");
218             File.write(js, js_src);
219             
220             
221             
222         },
223          /**
224          * convert xtype for munged output..
225          * 
226          */
227         mungeXtype : function(xtype, els)
228         {
229             var bits = xtype.split('.');
230             // assume it has lenght!
231             
232             els.push("xtype: '"+ bits.pop()+"'");
233             els.push('xns: '+ bits.join('.'));
234         },
235         
236         
237         toSourcePreview: function()
238         {
239             
240             var top = this.guessName(this.items[0]);
241             if (!top) {
242                 return false;
243             }
244             if (top.match(/Dialog/)) {
245                 return this.toSourceDialog(true);
246             }
247             return this.toSourceLayout(true);
248             
249             
250             
251         },
252         
253         /**
254          * This needs to use some options on the project
255          * to determine how the file is output..
256          * 
257          * At present we are hard coding it..
258          * 
259          * 
260          */
261         toSource: function()
262         {
263             // dump the file tree back out to a string.
264             
265             // we have 2 types = dialogs and components
266             // 
267             var top = this.guessName(this.items[0]);
268             if (!top) {
269                 return false;
270             }
271             if (top.match(/Dialog/)) {
272                 return this.toSourceDialog();
273             }
274             return this.toSourceLayout();
275             
276             /*
277             eventually support 'classes??'
278              return this.toSourceStdClass();
279             */
280               
281         },
282        
283         outputHeader : function()
284         {
285             return [
286                 "//<script type=\"text/javascript\">",
287                 "",
288                 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
289                 ""
290             ].join("\n");
291             
292        
293         },
294         // a standard dialog module.
295         // fixme - this could be alot neater..
296         toSourceDialog : function(isPreview) 
297         {
298             
299             isPreview = isPreview || false;
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         
340         
341         pathToPart : function()
342         {
343             var dir = File.basename(File.dirname(this.path));
344             var modname = dir.split('.').pop();
345             
346             // now we have the 'module name'..
347             var fbits = File.basename(this.path).split('.');
348             fbits.pop(); // remove extension..
349             var npart = fbits.pop(); // this should be 'AdminProjectManager' for example...
350             if (npart.substring(0, modname.length) == modname) {
351                 npart = npart.substring(modname.length);
352             }
353             return [ modname , npart];
354             
355             
356             
357             
358         },
359         
360         // a layout compoent 
361         toSourceLayout : function(isPreview) 
362         {
363             isPreview = isPreview || false;
364             var topItem = JSON.parse(JSON.stringify(this.items[0]));
365             if (isPreview) {
366                 topItem.region = 'center';
367                 topItem.background = false;
368             }
369             
370             var o = this.mungeToString(topItem, false, '            ');   
371              
372             var modkey = this.modOrder + '-' + this.name.replace(/[^A-Z.]+/ig, '-');
373             
374             var parent =   (this.parent ?  "'" + this.parent + "'" :  'false');
375             if (isPreview) {
376                 parent = 'false'
377             }
378           
379             return [
380                 this.outputHeader(),
381                 
382                 this.name  +  " = new Roo.XComponent({",
383                 "    part     :  "+ JSON.stringify(this.pathToPart()) + ",",
384                         /// critical used by builder to associate modules/parts/persm
385                 "    order    : '" +modkey+"',",
386                 "    region   : '" + this.region   +"',",
387                 "    parent   : "+ parent + ",",
388                 "    name     : " + JSON.stringify(this.title  || "unnamed module") + ",",
389                 "    disabled : " + (this.disabled || 'false') +", ",
390                 "    permname : '" + (this.permname|| '') +"', ",
391                     
392                // "    tree : function() { return this._tree(); },",   //BC
393                 "    _tree : function()",
394                 "    {",
395                 "        var _this = this;", // bc
396                 "        var MODULE = this;", /// this looks like a better name.
397                 "        return " + o + ';',
398                 "    }",
399                 "});",
400                 ""
401                  
402              ].join("\n");
403             
404         },
405             
406         guessName : function(ar) // turns the object into full name.
407         {
408              // eg. xns: Roo, xtype: XXX -> Roo.xxx
409             if (!ar) {
410                 return false;
411             }
412             var ret = [];
413             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
414             
415             
416             
417             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
418                 return false;
419             }
420             var xtype = ar['xtype'] + '';
421             if (xtype[0] == '*') { // prefixes????
422                 xtype  = xtype.substring(1);
423             }
424             if (xtype.match(/^Roo/)) {
425                 // already starts with roo...
426                 ret = [];
427             }
428             ret.push(xtype);
429             var str =  ret.join('.');
430             
431             
432             
433             var pm = imports.ProjectManager.ProjectManager;
434             return pm.getPalete('Roo').guessName(ret.join('.'));
435             
436                             
437                                  
438         },
439         /*
440         getTree : function( o ) {
441             
442             
443             
444         }
445         */
446          getHelpUrl : function(cls)
447         {
448             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
449         }
450         
451 });