resources/RooUsage.txt
[app.Builder.js] / old-javascript / JsRender / Base.js
1 //<Script type="text/javascript">
2
3
4 var XObject     = imports.XObject.XObject;
5
6
7 var Lang        = imports.JsRender.Lang.Lang;
8
9
10 var JsParser    = imports.JsParser.JsParser;
11 var TokenReader = imports.JSDOC.TokenReader.TokenReader;
12 var TextStream  = imports.JSDOC.TextStream.TextStream;
13
14 var File = imports.File.File;
15 // File Provider..
16
17 Base = XObject.define(
18     
19     function(cfg) {
20         
21         XObject.extend(this, cfg);
22         
23     },
24     Object,
25     {
26         /**
27          * @cfg {Array} doubleStringProps list of properties that can be double quoted.
28          */
29         doubleStringProps : false,
30         
31         id : false,
32         name : false,   // is the JS name of the file.
33         path : '',      // is the full path to the file.
34         parent : false, // JS parent.
35         
36         title : false,  // a title.. ?? nickname.. ??? - 
37         project: false, // name...
38         //Project : false, // link to container project!
39         
40         items : false, // the tree of nodes.
41         
42         cn : false, // array used by project tree.
43         
44         
45         save : function()
46         {
47             var write = { }; 
48             var _this = this;
49             var write = this.toJsonArray()
50             print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
51             File.write(this.path, JSON.stringify(write, null, 4));
52         },
53         
54         saveHTML : function()
55         {
56             // NOOP
57         },
58         
59         /**
60          *
61          * load from a javascript file.. rather than bjs..
62          * 
63          *
64          */
65          
66         _loadItems : function(cb)
67         {
68             // already loaded..
69             if (this.items !== false) {
70                 return false;
71             }
72               
73             
74             
75             var tr = new  TokenReader(  { 
76                 keepDocs :true, 
77                 keepWhite : true,  
78                 keepComments : true, 
79                 sepIdents : false,
80                 collapseWhite : false,
81                 filename : args[0],
82                 ignoreBadGrammer: true
83             });
84             
85             var str = File.read(this.path);
86             var toks = tr.tokenize(new TextStream(str));  
87             var rf = new JsParser(toks);
88             rf.parse();
89             var cfg = rf.cfg;
90             
91             this.modOrder = cfg.modOrder || '001';
92             this.name = cfg.name.replace(/\.bjs/, ''); // BC!
93             this.parent =  cfg.parent;
94             this.permname =  cfg.permname || '';
95             this.title =  cfg.title || cfg.name;;
96             this.items = cfg.items || []; 
97             //???
98             //this.fixItems(_this, false);
99             cb();
100             return true;    
101                 
102         },
103         
104         /**
105          * accepts:
106          * { success : , failure : , scope : }
107          * 
108          * 
109          * 
110          */
111          
112         getTree : function( o ) {
113             print("File.getTree tree called on base object?!?!");
114         },
115         toJsonArray : function()
116         {
117             var ret = { }; 
118             var _this = this;
119             ['id', 'name', 'parent', 'title', 'path', 'items' , 'permname', 'modOrder' ].forEach( function(k) {
120                 ret[k] = typeof(_this[k]) == 'undefined' ? '' : _this[k];
121             });
122             return ret;
123         },
124         getTitle : function()
125         {
126             if (this.title) {
127                 return this.title;
128             }
129             return this.path.split('/').pop();
130             
131         },
132         getTitleTip: function()
133         {
134             if (this.title) {
135                 return '<b>' + this.title + '</b> ' + this.path;
136             }
137             return this.path;
138         },
139         sortCn: function()
140         {
141             this.cn.sort(function(a,b) {
142                 return a.path > b.path;// ? 1 : -1;
143             });
144         },
145         // should be in palete provider really..
146         
147         guessName : function(ar) // turns the object into full name.
148         {
149              // eg. xns: Roo, xtype: XXX -> Roo.xxx
150             if (typeof( ar['|xns'] ) == 'undefined' || typeof( ar['xtype'] ) == 'undefined') {
151                 return '';
152                }
153              
154             return ar['|xns'] +'.' + ar['xtype'];
155                             
156                                  
157         },
158         
159         
160         
161         copyTo: function(path, cb)
162         {
163             var _this = this;
164             this.loadItems(function() {
165                 
166                 _this.path = path;
167                 cb();
168             });
169             
170         },
171         
172         /**
173          * 
174          * munge JSON tree into Javascript code.
175          * 
176          * FIXME: + or / prefixes to properties hide it from renderer.
177          * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
178          * FIXME: needs to understand what properties might be translatable (eg. double quotes)
179          * 
180          * @arg {object} obj the object or array to munge..
181          * @arg {boolean} isListener - is the array being sent a listener..
182          * @arg {string} pad - the padding to indent with. 
183          */
184         
185         
186         mungeToString:  function(obj, isListener, pad)
187         {
188             
189              
190             pad = pad || '    ';
191             var keys = [];
192             var isArray = false;
193             isListener = isListener || false;
194
195             if (!obj) {
196                 print("missing obj?");
197                 return;
198             }            
199
200             // am I munging a object or array...
201             if (obj.constructor && obj.constructor.toString() === Array.toString()) {
202                 for (var i= 0; i < obj.length; i++) {
203                     keys.push(i);
204                 }
205                 isArray = true;
206             } else {
207                 for (var i in obj) {
208                     keys.push(i);
209                 }
210             }
211             
212             
213             var els = []; 
214             var skip = [];
215             if (!isArray && 
216                     typeof(obj['|xns']) != 'undefined' &&
217                     typeof(obj['xtype']) != 'undefined'
218                 ) {
219                     this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els);
220                     //els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
221                      
222                     skip.push('|xns','xtype');
223                     
224                 }
225             
226             
227             if (!isArray && obj.items && obj.items.length) {
228                 // look for props..
229                 var newitems = [];
230                 for (var ii =0; ii< obj.items.length; ii++) {
231                     var pl = obj.items[ii];
232                     
233  
234                     if (typeof(pl['*prop']) == 'undefined') {
235                         newitems.push(pl);
236                         continue;
237                     }
238                     
239                     //print(JSON.stringify(pl,null,4));
240                     // we have a prop...
241                     var prop = pl['*prop'] + '';
242                     delete pl['*prop'];
243                     if (!prop.match(/\[\]$/)) {
244                         // it's a standard prop..
245                         
246                         // munge property..??
247                         
248                         obj[prop] = pl;
249                         
250                         keys.push(prop);
251                         continue;
252                     }
253                     prop  = prop.substring(0, prop.length -2); //strip []
254                     // it's an array type..
255                     obj[prop] = obj[prop]  || [];
256                     obj[prop].push(pl);
257                   //  print("ADDNG PROP:" + prop + ' ' + keys.indexOf(prop) );
258                     if (keys.indexOf(prop) < 0) {
259                         keys.push(prop);
260                     }
261                     
262                     
263                     
264                 }
265                 
266                 obj.items = newitems;
267                 if (!obj.items.length) {
268                     delete obj.items;
269                 }
270                 
271             }
272             
273              
274             
275             //if (isArray) { print(JSON.stringify(keys, null,4)); }
276             // keys is just the real keys of the object.
277             var _this = this;
278             
279             var left =  '';
280             
281             for (var ii =0; ii< keys.length; ii++) {
282             
283                 var i = keys[ii];
284               
285                 if (typeof(obj[i]) == 'undefined') { // empty or removed.
286                     continue;
287                 }
288                 var el = obj[i];
289                 if (!isArray && skip.indexOf(i) > -1) { // things we do not write..
290                     continue;
291                 }
292                 if (!isArray) {
293                     // set the key to be quoted with singel quotes..
294                     var leftv = i[0] == '|' ? i.substring(1) : i;
295                     // skip builder stuff. prefixed with  '.' .. just like unix fs..
296                     if (leftv[0] == '.') {
297                         continue;
298                     }
299                     if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
300                         left = "'" + leftv + "'";
301                     } else if (leftv.match(/[^A-Z_]+/i)) { // not plain a-z... - quoted.
302                         var val = JSON.stringify(leftv);
303                         left = "'" + val.substring(1, val.length-1).replace(/'/g, "\\'") + "'";
304                     } else {
305                         left = '' + leftv;
306                     }
307                     left += ' : ';
308                     
309                 }
310                 
311                 
312                 if (isListener) {
313                     // change the lines...
314                     var str= ('' + obj[i]).replace(/^\s+|\s+$/g,""); // remove bar.
315                     var lines = str.split("\n");
316                     if (lines.length > 1) {
317                         str = lines.join("\n" + pad);
318                     }
319                     
320                     els.push(left  + str);
321                     continue;
322                 }
323                  
324                 
325                 
326                 //var left = isArray ? '' : (JSON.stringify(i) + " : " )
327                 
328                 if (i[0] == '|') {
329                     // does not hapepnd with arrays..
330                     if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
331                         continue;
332                     }
333                     // this needs to go...
334                     //if (typeof(el) == 'string'  && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
335                     //    continue;;
336                     //}
337                     
338                     var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
339                     var lines = str.split("\n");
340                     if (lines.length > 1) {
341                         str = lines.join("\n" + pad);
342                     }
343                     
344                     els.push(left + str);
345                     continue;
346                 }
347                 
348                 
349                 
350                 
351                 if (typeof(el) == 'object') {
352                     
353                     // we can skip empty items lists and empty listeners..
354                     //if (!isArray && i == 'items' && !el.length) {
355                     //    return; 
356                     //}
357                    // 
358                     var right = _this.mungeToString(el, i == 'listeners', pad + '    ');
359                     
360                     //if (!left.length && isArray) print(right);
361                     
362                     if ((typeof(right) != 'undefined') && right.length){
363                         els.push(left + right);
364                     }
365                 
366                     continue;
367                 }
368                 // standard. .
369                 if (typeof(obj[i]) != 'string') {
370                     els.push(left + JSON.stringify(obj[i]));
371                     continue;
372                 }
373                 // strings..
374                 if (!_this.doubleStringProps) {
375                     els.push(left + JSON.stringify(obj[i]));
376                     continue;
377                 }
378                 if (_this.doubleStringProps.indexOf(i) > -1) {
379                     els.push(left + JSON.stringify(obj[i]));
380                     continue;
381                 }
382                 // single quote..
383                 els.push(left + "'" + obj[i].replace(/'/g, "\\'") + "'");
384                 
385
386             }
387             
388             if (!isArray && !els.length) {
389                 return '';
390             }
391             //output the thing.
392             var spad = pad.substring(0, pad.length-4);
393             return (isArray ? '[' : '{') + "\n" +
394                 pad  + els.join(",\n" + pad ) + 
395                 "\n" + spad + (isArray ? ']' : '}');
396                
397             
398             
399         } 
400         
401          
402         
403     }
404     
405      
406     
407 );
408
409
410
411
412
413