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