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