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