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