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));
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'].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         guessName : function(ar) // turns the object into full name.
85         {
86              // eg. xns: Roo, xtype: XXX -> Roo.xxx
87             if (typeof( ar['|xns'] ) == 'undefined' || typeof( ar['xtype'] ) == 'undefined') {
88                 return '';
89                }
90              
91             return ar['|xns'] +'.' + ar['xtype'];
92                             
93                                  
94         },
95         
96         /*
97         Roo specific?
98         toSourceStdClass: function()
99         {
100             var cfg = this.items[0]
101             var fcfg = XObject.extend({ },  this.items[0]);
102             delete fcfg['*class'];
103             delete fcfg['*extends'];
104             delete fcfg['*static'];
105             delete fcfg['|constructor'];
106             
107             var hasExtends = (typeof(cfg['*extends']) != 'undefined') && cfg['*extends'].length;
108             var hasConstructor = (typeof(cfg['|constructor']) != 'undefined');
109             var isStatic = (typeof(cfg['*static']) == '*static');
110             
111             var newline = '';
112             var endline = '';
113             if (hasExtends) {
114                 newline =  hasConstructor ? 
115                 
116                  
117                     cfg['//constructor'] + "\n" + 
118                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
119                     "Roo.extend(" + cfg['*class'] + ":, " + cfg['*extends'] + ", " :
120                     
121                     cfg['//*class'] + "\n" + 
122                     cfg['*class'] + " = new " + cfg['*extends'] + "(" ;
123                 
124                 endline = ');';
125             } else {
126                 
127                 
128                 
129                 newline  = hasConstructor ? 
130                 
131                     cfg['//constructor'] + "\n" + 
132                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
133                     'Roo.apply( ' +  cfg['*class'] + ".prototype , " :
134                     
135                     cfg['//*class'] + "\n" + 
136                     cfg['*class'] + " = ";
137                 
138                     
139                 endline = hasConstructor ? ');' : ';';
140             }
141                   
142             return this.outputHeader() + 
143                     newline + 
144                     this.objectToJsString(fcfg,1) +
145                     endline;
146             
147             
148             
149          
150         },
151         */
152         
153         copyTo: function(path, cb)
154         {
155             var _this = this;
156             this.loadItems(function() {
157                 
158                 _this.path = path;
159                 cb();
160             });
161             
162         },
163         
164         
165         
166         
167         /**
168          * munges a prop object, removing all the special stuff..
169          * putting props back where they should go...
170          */
171         
172         mungePropObj : function(o)
173         {
174             
175            // console.log('mungePropObj: enter');
176             var ret = {};
177             // standard props do not do anyting.
178             for (var i in o){
179                 if (['items', '*prop'].indexOf(i) > -1) {
180                     continue;
181                 }
182                 
183                 ret[i] = o[i];
184             }
185             ret.items = [];
186             o.items = o.items || [];
187             var _this = this;
188             o.items.forEach( function(e) {
189                 if (typeof(e) == 'undefined') {
190                     return;
191                 }
192                 
193                 if (typeof(e) != 'object') {
194                     // should not really hapen?!!?
195                     ret.items.push(e); // could be 
196                     return;
197                 }
198                 if (typeof(e['*prop']) != 'undefined') {
199                     var pn = e['*prop'];
200                     var val = _this.mungePropObj(e);
201                     
202                     if (e['xtype'].match(/^Array/)) {
203                         ret[pn] = val.items;
204                         return;
205                     }
206                     
207                     ret[pn] = val;
208                     return;
209                 }
210                 // handle fake arrays...
211                 var val = _this.mungePropObj(e);
212                 
213                 ret.items.push(val); // again should not really happen...
214                      
215                 
216             });
217             //console.log('mungePropObj: leave');
218             // do we munge '*' xtypes?
219             return ret;
220             
221         },
222         objectKeys : function(o) {
223             var ret = [];
224             for (var k in o) {
225                 ret.push(k)
226             }
227             return ret;
228         },
229         
230         objectToJsString : function (o, ind) 
231         {
232             ind = ind || 0;
233             
234             
235             var ret = '';
236             var ix = new Array(ind+1).join("    ");
237             var ix1 = new Array(ind).join("    ");
238             for (var k in o) {
239                 var v = o[k];
240                 if (k[0] == '+') { // + means  hide from renderer.. we do not save this.
241                     continue;
242                 }
243                 if (k[0] == '/') { //  means  hide from renderer.. we prefix the col with it..
244                     continue;
245                 }
246             
247                 
248                 if (typeof(v) == 'object') {
249                     
250                     if ((v.constructor != Array) && !this.objectKeys(v).length) {
251                         continue;
252                     }
253                     if ((v.constructor == Array) && !v.length && k == 'items') {
254                         continue;
255                     }   
256                 }
257                 ret += ret.length ? ",\n" : '';
258                 
259                 var kk = k[0] == '|' ? k.substring(1) : k;
260                 if (typeof(o['//' + kk]) != 'undefined') {
261                     ret += ix + o['//' + kk].split("\n").join( "\n" + ix) + "\n";
262                 }
263                 
264                 switch(typeof(v)) {
265                     case 'object': 
266                         if (v.constructor == Array) {
267                             ret += ix + this.toJsProp(k) +  ' : ' + this.arrayToJsString(v, ind+1);
268                             continue;
269                         }
270                     
271                     
272                         ret += ix + this.toJsProp(k) +  ' : ' + this.objectToJsString(v, ind+1);
273                         continue;
274                     
275                     case 'boolean' : 
276                         ret += ix + this.toJsProp(k) +  ' : ' +  (v ? 'true' : 'false');
277                         continue;
278                     
279                     case 'number' : 
280                         ret += ix + this.toJsProp(k) +  ' : ' +  v;
281                         continue;
282                         
283                     
284                     case 'string': 
285                         if (k[0] == '|') {
286                             ret += ix + this.toJsProp(k) +  ' : ' +  v.split("\n").join( "\n" + ix);
287                             continue;
288                         }
289                         // fallthru
290                     
291                     default:
292                         // we should use special stuff here to determine if it's a singly or dobuley 
293                         // quoted string..
294                         ret += ix + this.toJsProp(k) +  ' : ' +  this.stringToJsString(v, k, o);
295                         continue;
296                         
297                      
298                     }
299             }
300             return "{\n" + ret + "\n" + ix1 + '}'; 
301             
302         },
303         arrayToJsString : function (ar, ind)
304         {
305             var isobjar = false;
306             ar.forEach( function(o) {
307                 if (typeof(o) == 'object' && (o.constructor != Array)) {
308                     isobjar = true;
309                 }
310             });
311             var ix = '';
312             var ix1 = '';
313             var cr = ', ';
314             var lb = ' ';
315             if (isobjar) {
316                 ix = new Array(ind+1).join("    ");
317                 ix1 = new Array(ind).join("    ");
318                 cr = ",\n";
319                 lb = "\n";
320                  
321             }
322             // array of parts...
323             var ret =  '';
324             var _this = this;
325             ar.forEach( function(v, n) {
326                 // skip blank last element in an array
327                 if ((n == (ar.length -1))  && typeof(v) == 'undefined') {
328                     return;
329                 }
330                 
331                 // empty objects in array?
332                 if (typeof(v) == 'object' && v.constructor != Array) {
333                     if (!_this.objectKeys(v).length) {
334                         return;
335                     }
336                 }
337                     
338                 
339                 ret += ret.length ? cr : '';
340                 
341                 switch(typeof(v)) {
342                 
343                     case 'object': 
344                         if (v.constructor == Array) {
345                             
346                             ret += ix + _this.arrayToJsString(v, ind+1);
347                             return;
348                         }
349                     
350                         ret += ix + _this.objectToJsString(v, ind+1);
351                         return;
352                     
353                     case 'boolean' : 
354                         ret += ix +  (v ? 'true' : 'false');
355                         return;
356                     
357                     case 'number' : 
358                         ret += ix +  v;
359                         return;
360                         
361                     
362                     case 'string': 
363                         if (k[0] == '|') {
364                             ret += ix + v.split("\n").join( "\n" + ix);
365                             return;
366                         }
367                         // fallthru
368                     
369                     default:
370                         // we should use special stuff here to determine if it's a singly or dobuley 
371                         // quoted string..
372                         ret += ix + JSON.stringify(v);
373                         return;
374                         
375                  
376                 }
377                  
378             });
379             return "[" + lb  + ret + lb + ix1 + "]";
380             
381         },
382         stringToJsString :  function(v, k , o) {
383             // since most properties can use single quotes (non-translatable)
384             // we try to fix this here..
385             var val = JSON.stringify(v);
386             if (['xns', 'xtype'   ].indexOf(k) > -1) {
387                 return "'" + val.substring(1, val.length-1).replace(/'/, "\\'") + "'";
388             }
389             return val;
390             
391             
392         },
393         
394         
395         toJsProp:  function(v) {
396             var vv = v[0] == '|' ? v.substring(1) : v;
397             if (Lang.isKeyword(vv) || Lang.isBuiltin(vv)) {
398                 return "'" + vv + "'";
399             }
400             if (vv.match(/[^A-Z_]+/i)) {
401                 var val = JSON.stringify(vv);
402                 return "'" + val.substring(1, val.length-1).replace(/'/, "\\'") + "'";
403             }
404             return vv;
405         }
406         
407         
408     }
409 );
410
411
412
413
414
415