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