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