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     },
17     Object,
18     {
19         
20         id : false,
21         name : false,   // is the JS name of the file.
22         path : '',      // is the full path to the file.
23         parent : false, // JS parent.
24         
25         title : false,  // a title.. ?? nickname.. ??? - 
26         project: false, // name...
27         //Project : false, // link to container project!
28         
29         items : false, // the tree of nodes.
30         
31         cn : false, // array used by project tree.
32         
33         
34         save : function()
35         {
36             var write = { }; 
37             var _this = this;
38             var write = this.toJsonArray()
39             print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
40             File.write(this.path, JSON.stringify(write, null, 4));
41         },
42         
43         /**
44          * accepts:
45          * { success : , failure : , scope : }
46          * 
47          * 
48          * 
49          */
50          
51         getTree : function( o ) {
52             console.log("File.getTree tree called on base object?!?!");
53         },
54         toJsonArray : function()
55         {
56             var ret = { }; 
57             var _this = this;
58             ['id', 'name', 'parent', 'title', 'path', 'items', 'project'].forEach( function(k) {
59                 ret[k] = _this[k];
60             });
61             return ret;
62         },
63         getTitle : function()
64         {
65             if (this.title) {
66                 return this.title;
67             }
68             return this.path.split('/').pop();
69             
70         },
71         getTitleTip: function()
72         {
73             if (this.title) {
74                 return '<b>' + this.title + '</b> ' + this.path;
75             }
76             return this.path;
77         },
78         sortCn: function()
79         {
80             this.cn.sort(function(a,b) {
81                 return a.path > b.path;// ? 1 : -1;
82             });
83         },
84         // should be in palete provider really..
85         
86         guessName : function(ar) // turns the object into full name.
87         {
88              // eg. xns: Roo, xtype: XXX -> Roo.xxx
89             if (typeof( ar['|xns'] ) == 'undefined' || typeof( ar['xtype'] ) == 'undefined') {
90                 return '';
91                }
92              
93             return ar['|xns'] +'.' + ar['xtype'];
94                             
95                                  
96         },
97         
98         /*
99         Roo specific?
100         toSourceStdClass: function()
101         {
102             var cfg = this.items[0]
103             var fcfg = XObject.extend({ },  this.items[0]);
104             delete fcfg['*class'];
105             delete fcfg['*extends'];
106             delete fcfg['*static'];
107             delete fcfg['|constructor'];
108             
109             var hasExtends = (typeof(cfg['*extends']) != 'undefined') && cfg['*extends'].length;
110             var hasConstructor = (typeof(cfg['|constructor']) != 'undefined');
111             var isStatic = (typeof(cfg['*static']) == '*static');
112             
113             var newline = '';
114             var endline = '';
115             if (hasExtends) {
116                 newline =  hasConstructor ? 
117                 
118                  
119                     cfg['//constructor'] + "\n" + 
120                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
121                     "Roo.extend(" + cfg['*class'] + ":, " + cfg['*extends'] + ", " :
122                     
123                     cfg['//*class'] + "\n" + 
124                     cfg['*class'] + " = new " + cfg['*extends'] + "(" ;
125                 
126                 endline = ');';
127             } else {
128                 
129                 
130                 
131                 newline  = hasConstructor ? 
132                 
133                     cfg['//constructor'] + "\n" + 
134                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
135                     'Roo.apply( ' +  cfg['*class'] + ".prototype , " :
136                     
137                     cfg['//*class'] + "\n" + 
138                     cfg['*class'] + " = ";
139                 
140                     
141                 endline = hasConstructor ? ');' : ';';
142             }
143                   
144             return this.outputHeader() + 
145                     newline + 
146                     this.objectToJsString(fcfg,1) +
147                     endline;
148             
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                 var el = obj[i];
258                 if (typeof(obj) == 'undefined') { // empty or removed.
259                     return;
260                 }
261                 
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(/'/, "\\'") + "'";
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                 
333                 els.push(left + JSON.stringify(obj[i]));
334             });
335             
336             if (!isArray && !els.length) {
337                 return;
338             }
339             //output the thing.
340             var spad = pad.substring(0, pad.length-4);
341             return (isArray ? '[' : '{') + "\n" +
342                 pad  + els.join(",\n" + pad ) + 
343                 "\n" + spad + (isArray ? ']' : '}');
344                
345             
346             
347         } 
348         
349          
350         
351     }
352 );
353
354
355
356
357
358