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