resources/RooUsage.txt
[app.Builder.js] / old-javascript / JsRender / Roo.js
1 //<Script type="text/javascript">
2
3  
4 Gio = imports.gi.Gio;
5 GLib= imports.gi.GLib;
6 XObject = imports.XObject.XObject;
7
8   
9 //----------------------- our roo verison
10 Base = imports.JsRender.Base.Base;
11 File = imports.File.File;
12
13
14 //----------------------- our roo verison
15
16 var rid = 0;
17
18 Roo = XObject.define(
19     function(cfg) {
20         
21         // id ,
22         //"name":"Edit Module Details",
23         // items : 
24         //"btype":"FORM", // was to be components...
25         //"app":"Builder",
26         //"module":"Pman.Tab.BuilderTop2"
27         //console.dump(cfg);
28         
29         if (!cfg.name || !cfg.fullname ) {
30             cfg.name = cfg.path.split('/').pop().replace(/\.bjs$/, '').replace(/\.js$/, '');
31             //cfg.fullname = (cfg.parent && cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
32             cfg.fullname = 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         Roo.superclass.constructor.call(this, cfg);
46
47         
48         // super?!?!
49         this.id = 'roo-file-' + (rid++);
50         // various loader methods..
51     },
52     Base,
53     {
54         doubleStringProps : [ 
55             'title',
56             'legend',
57             'loadingText',
58             'emptyText',
59             'qtip',
60             'value',
61             'text',
62             'emptyMsg',
63             'displayMsg'
64         ],
65         path : '', // the file path..
66         modOrder : '001', /// sequence id that this uses.
67         region : 'center',
68         parent : '',
69         title : '', // the title on displayed when loading.
70         disable : '', // use a function to that returns false to disable this..
71         permname: '', /// permission name
72         
73         items: false,
74         
75         setNSID : function(id)
76         {
77             
78             this.items[0]['|module'] = id;
79        
80             
81         },
82         
83         
84         getType: function() {
85             return 'Roo';
86         },
87        
88       
89         loadItems : function(cb, sync)
90         {
91             
92             
93             
94             print("load Items!");
95             if (this.items !== false) {
96                 return false;
97             }
98             
99             var _this = this;
100              
101             function loaded(src) {
102                 var cfg = JSON.parse(src);
103                 print("loaded data");
104                 //print(JSON.stringify(cfg, null,4));
105                 _this.modOrder = cfg.modOrder || '001';
106                 _this.name = cfg.name.replace(/\.bjs/, ''); // BC!
107                 _this.parent =  cfg.parent;
108                 _this.permname =  cfg.permname || '';
109                 _this.title =  cfg.title;
110                 _this.items = cfg.items || []; 
111                 
112                 _this.fixItems(_this, false);
113                 
114                 
115                 
116                 cb();
117             }
118             if (sync) {
119                 loaded(File.read(this.path));
120                 return true;
121             }
122             
123             
124             var file = Gio.file_new_for_path(this.path);
125             
126                              
127             file.read_async(0, null, function(source,result) {
128                 var stream = source.read_finish(result)
129                 var dstream = new Gio.DataInputStream.c_new(stream);
130                 
131                 loaded (dstream.read_until(""));
132                 
133                 
134                  
135                 
136             });
137             
138             return true;
139             
140         },
141         /**
142          * old code had broken xtypes and used arrays differently,
143          * this code should try and clean it up..
144          * 
145          * 
146          */
147         fixItems : function(node, fixthis)
148         {
149             if (fixthis) {
150                 // fix xtype.
151                 var fn = this.guessName(node);
152                 //print("guessname got " + fn);
153                 if (fn) {
154                     var bits = fn.split('.');
155                     node.xtype = bits.pop();
156                     node['|xns'] = bits.join('.');
157                     
158                 }
159                 // fix array???
160                  
161                 
162             }
163             if (!node.items || !node.items.length) {
164                 return;
165             }
166             var _this = this;
167             var aitems = [];
168             var nitems = [];
169             for (var ii = 0; ii < node.items.length;ii++) { 
170                 var i = node.items[ii]; 
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             
213             print("--- JsRender.Roo.save");
214             Base.prototype.save.call(this);
215             // now write the js file..
216             var js = this.path.replace(/\.bjs$/, '.js');
217             var d = new Date();
218             var js_src = this.toSource();            
219             print("TO SOURCE in " + ((new Date()) - d) + "ms");
220             File.write(js, js_src);
221             // for bootstrap - we can write the HTML to the templates directory..
222             
223             var top = this.guessName(this.items[0]);
224             print ("TOP = " + top)
225              
226             
227             
228             
229             
230             
231         },
232         saveHTML : function(frame) {
233             var top = this.guessName(this.items[0]);
234             print ("TOP = " + top)
235             if (top != 'Roo.bootstrap.Body') {
236                 return;
237             }
238             print("SAVE HTML -- ");
239             print(frame);
240             var _this = this;
241             // wait a second.
242             
243             GLib.timeout_add_seconds(0, 1, function() {
244                 //    print("run refresh?");
245                 var html = _this.traversedom(frame);
246                 //print(html);
247                 
248                 //print(_this.path);
249                 var dir = File.dirname(_this.path) +  File.SEPARATOR + 'templates';
250                 //print(dir);
251                 if (!File.isDirectory(dir)) {
252                     print("Skip no template sub-directory");
253                     return false;
254                 }
255                 var fn = dir + File.SEPARATOR + File.basename(_this.path).replace(/\.bjs$/, '.html');
256                 //print(fn);
257                 File.write(fn, html);
258                 
259                 
260                  return false; // only once..
261                 
262                 
263                 
264             });
265             
266             
267             
268             
269         },
270         
271         
272         traversedom :  function(web_frame) {
273             print("TRAVERSE DOM?");
274             
275             var dom = web_frame.get_dom_document().body;
276             print(dom);
277             var ret = '';
278             //Roo.select('body > div',true).each(function(el) {
279             this.traverseDOMTree(function(s) { ret+=s; }, dom, 1);
280             return ret;
281         },
282         
283         
284         traverseDOMTree : function(cb, currentElement, depth) {
285             if (!currentElement) {
286                 
287                 return;
288             }
289             if (currentElement.class_name.match(/roo-dynamic/)) {
290                 return;
291             }
292             
293             //Roo.log(currentElement);
294             var j;
295             var nodeName = currentElement.node_name;
296             var tagName = currentElement.tag_name;
297             
298             if  (nodeName == '#text') {
299                 cb(currentElement.node_value);
300                 return;
301             
302             }
303              
304             
305             
306             if(nodeName == 'BR'){
307                 cb("<BR/>");
308                 return;
309             }
310             if (nodeName != 'BODY') {
311                 
312             
313             
314                 var i = 0;
315               // Prints the node tagName, such as <A>, <IMG>, etc
316                 if (tagName) {
317                     var attr = [];
318                     for(i = 0; i < currentElement.attributes.length;i++) {
319                         var aname = currentElement.attributes.item(i).name;
320                         if (aname=='id') {
321                             aname= 'xbuilderid';
322                         }
323                         // skip
324                         if (currentElement.attributes.item(i).value == 'builderel') {
325                             return;
326                         }
327                         attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
328                     }
329                     
330                     
331                     cb("<"+currentElement.tag_name+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
332                 } 
333                 else {
334                   cb("[unknown tag]");
335                 }
336             } else {
337                 tagName = false;
338             }
339             // Traverse the tree
340             i = 0;
341             var currentElementChild = currentElement.child_nodes.item(i);
342             var allText = true;
343             while (currentElementChild) {
344                 // Formatting code (indent the tree so it looks nice on the screen)
345                 
346                 if  (currentElementChild.node_name == '#text') {
347                     cb(currentElementChild.node_value);
348                     i++;
349                     currentElementChild=currentElement.child_nodes.item(i);
350                     continue;
351                 }   
352                 allText = false;
353                 cb("\n");
354                 for (j = 0; j < depth; j++) {
355                   // &#166 is just a vertical line
356                   cb("  ");
357                 }               
358                 
359                     
360                 // Recursively traverse the tree structure of the child node
361                 this.traverseDOMTree(cb, currentElementChild, depth+1);
362                 i++;
363                 currentElementChild=currentElement.child_nodes.item(i);
364             }
365             if (!allText) {
366                     // The remaining code is mostly for formatting the tree
367                 cb("\n");
368                 for (j = 0; j < depth - 1; j++) {
369                   cb("  ");
370                 }     
371             }
372             if (tagName) {
373                 cb("</"+tagName+">");
374             }
375             
376         },
377         
378         
379         
380          /**
381          * convert xtype for munged output..
382          * 
383          */
384         mungeXtype : function(xtype, els)
385         {
386             var bits = xtype.split('.');
387             // assume it has lenght!
388             
389             els.push("xtype: '"+ bits.pop()+"'");
390             els.push('xns: '+ bits.join('.'));
391             if (xtype.match(/bootstrap/)) {
392                 els.push("'xtype-bootstrap' : '"+ xtype +"'");
393             }
394                 //code
395             
396             
397             
398         },
399         
400         
401         toSourcePreview: function()
402         {
403             
404             var top = this.guessName(this.items[0]);
405             print(JSON.stringify(this.items, null,4));
406                        
407             if (!top) {
408                 return false;
409             }
410             
411             
412             if (top.match(/Dialog/)) {
413                 return this.toSourceDialog(true);
414             }
415             
416             if (top.match(/Modal/)) {
417                 return this.toSourceModal(true);
418             }
419             
420             return this.toSourceLayout(true);
421             
422             
423             
424         },
425         
426         /**
427          * This needs to use some options on the project
428          * to determine how the file is output..
429          * 
430          * At present we are hard coding it..
431          * 
432          * 
433          */
434         toSource: function()
435         {
436             // dump the file tree back out to a string.
437             
438             // we have 2 types = dialogs and components
439             // 
440             var top = this.guessName(this.items[0]);
441             if (!top) {
442                 return false;
443             }
444             if (top.match(/Dialog/)) {
445                 return this.toSourceDialog();
446             }
447             
448             if (top.match(/Modal/)) {
449                 return this.toSourceModal(true);
450             }
451             return this.toSourceLayout();
452             
453             /*
454             eventually support 'classes??'
455              return this.toSourceStdClass();
456             */
457               
458         },
459        
460         outputHeader : function()
461         {
462             return [
463                 "//<script type=\"text/javascript\">",
464                 "",
465                 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
466                 ""
467             ].join("\n");
468             
469        
470         },
471         // a standard dialog module.
472         // fixme - this could be alot neater..
473         toSourceDialog : function(isPreview) 
474         {
475             
476             isPreview = isPreview || false;
477             var items = JSON.parse(JSON.stringify(this.items[0]));
478             var o = this.mungeToString(items, false, '            ');   
479             return [
480                 this.outputHeader(),
481                 this.name + " = {",
482                 "",
483                 "    dialog : false,",
484                 "    callback:  false,",
485                 "",   
486                 "    show : function(data, cb)",
487                 "    {",
488                 "        if (!this.dialog) {",
489                 "            this.create();",
490                 "        }",
491                 "",
492                 "        this.callback = cb;",
493                 "        this.data = data;",
494                 "        this.dialog.show(this.data._el);",
495                 "        if (this.form) {",
496                 "           this.form.reset();",
497                 "           this.form.setValues(data);",
498                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
499                 "        }",
500                 "",   
501                 "    },",
502                 "",
503                 "    create : function()",
504                 "    {",
505                 "        var _this = this;",
506                 "        this.dialog = Roo.factory(" + o +  ");",
507                 "    }",
508                 "};",
509                 ""
510                 
511              ].join("\n");
512              
513              
514              
515         },
516         
517         toSourceModal : function(isPreview) 
518         {
519             
520             isPreview = isPreview || false;
521             var items = JSON.parse(JSON.stringify(this.items[0]));
522             var o = this.mungeToString(items, false, '            ');   
523             return [
524                 this.outputHeader(),
525                 this.name + " = {",
526                 "",
527                 "    dialog : false,",
528                 "    callback:  false,",
529                 "",   
530                 "    show : function(data, cb)",
531                 "    {",
532                 "        if (!this.dialog) {",
533                 "            this.create();",
534                 "        }",
535                 "",
536                 "        this.callback = cb;",
537                 "        this.data = data;",
538                 "        this.dialog.show(this.data._el);",
539                 "        if (this.form) {",
540                 "           this.form.reset();",
541                 "           this.form.setValues(data);",
542                 "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
543                 "        }",
544                 "",   
545                 "    },",
546                 "",
547                 "    create : function()",
548                 "    {",
549                 "        var _this = this;",
550                 "        this.dialog = Roo.factory(" + o +  ");",
551                 "    }",
552                 "};",
553                 ""
554                 
555              ].join("\n");
556              
557              
558              
559         },
560         
561         
562         pathToPart : function()
563         {
564             var dir = File.basename(File.dirname(this.path));
565             var modname = dir.split('.').pop();
566             
567             // now we have the 'module name'..
568             var fbits = File.basename(this.path).split('.');
569             fbits.pop(); // remove extension..
570             var npart = fbits.pop(); // this should be 'AdminProjectManager' for example...
571             if (npart.substring(0, modname.length) == modname) {
572                 npart = npart.substring(modname.length);
573             }
574             return [ modname , npart];
575             
576             
577             
578             
579         },
580         
581         // a layout compoent 
582         toSourceLayout : function(isPreview) 
583         {
584             isPreview = isPreview || false;
585             var topItem = JSON.parse(JSON.stringify(this.items[0]));
586             if (isPreview) {
587                 topItem.region = 'center';
588                 topItem.background = false;
589             }
590             
591             var o = this.mungeToString(topItem, false, '            ');   
592              
593             var modkey = this.modOrder + '-' + this.name.replace(/[^A-Z.]+/ig, '-');
594             
595             var parent =   (this.parent ?  "'" + this.parent + "'" :  'false');
596             if (isPreview) {
597                 parent = 'false'
598             }
599             
600           
601             return [
602                 this.outputHeader(),
603                 
604                 this.name  +  " = new Roo.XComponent({",
605                 "    part     :  "+ JSON.stringify(this.pathToPart()) + ",",
606                         /// critical used by builder to associate modules/parts/persm
607                 "    order    : '" +modkey+"',",
608                 "    region   : '" + this.region   +"',",
609                 "    parent   : "+ parent + ",",
610                 "    name     : " + JSON.stringify(this.title  || "unnamed module") + ",",
611                 "    disabled : " + (this.disabled || 'false') +", ",
612                 "    permname : '" + (this.permname|| '') +"', ",
613                     
614                // "    tree : function() { return this._tree(); },",   //BC
615                 "    _tree : function()",
616                 "    {",
617                 "        var _this = this;", // bc
618                 "        var MODULE = this;", /// this looks like a better name.
619                 "        return " + o + ';',
620                 "    }",
621                 "});",
622                 ""
623                  
624              ].join("\n");
625             
626         },
627             
628         guessName : function(ar) // turns the object into full name.
629         {
630              // eg. xns: Roo, xtype: XXX -> Roo.xxx
631             if (!ar) {
632                 return false;
633             }
634             var ret = [];
635             ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
636             
637             
638             
639             if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
640                 return false;
641             }
642             var xtype = ar['xtype'] + '';
643             if (xtype[0] == '*') { // prefixes????
644                 xtype  = xtype.substring(1);
645             }
646             if (xtype.match(/^Roo/)) {
647                 // already starts with roo...
648                 ret = [];
649             }
650             ret.push(xtype);
651             var str =  ret.join('.');
652             
653             
654             
655             var pm = imports.ProjectManager.ProjectManager;
656             return pm.getPalete('Roo').guessName(ret.join('.'));
657             
658                             
659                                  
660         },
661         /*
662         getTree : function( o ) {
663             
664             
665             
666         }
667         */
668         getHelpUrl : function(cls)
669         {
670             return 'http://www.akbkhome.com/roojs1/docs/symbols/' + cls + '.html';
671         }
672         
673 });