fix deleting hopefully
[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     },
18     Object,
19     {
20         /**
21          * @cfg {Array} doubleStringProps list of properties that can be double quoted.
22          */
23         doubleStringProps : false,
24         
25         id : false,
26         name : false,   // is the JS name of the file.
27         path : '',      // is the full path to the file.
28         parent : false, // JS parent.
29         
30         title : false,  // a title.. ?? nickname.. ??? - 
31         project: false, // name...
32         //Project : false, // link to container project!
33         
34         items : false, // the tree of nodes.
35         
36         cn : false, // array used by project tree.
37         
38         
39         save : function()
40         {
41             var write = { }; 
42             var _this = this;
43             var write = this.toJsonArray()
44             print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
45             File.write(this.path, JSON.stringify(write, null, 4));
46         },
47         
48         /**
49          * accepts:
50          * { success : , failure : , scope : }
51          * 
52          * 
53          * 
54          */
55          
56         getTree : function( o ) {
57             console.log("File.getTree tree called on base object?!?!");
58         },
59         toJsonArray : function()
60         {
61             var ret = { }; 
62             var _this = this;
63             ['id', 'name', 'parent', 'title', 'path', 'items' , 'permname', 'modOrder' ].forEach( function(k) {
64                 ret[k] = typeof(_this[k]) == 'undefined' ? '' : _this[k];
65             });
66             return ret;
67         },
68         getTitle : function()
69         {
70             if (this.title) {
71                 return this.title;
72             }
73             return this.path.split('/').pop();
74             
75         },
76         getTitleTip: function()
77         {
78             if (this.title) {
79                 return '<b>' + this.title + '</b> ' + this.path;
80             }
81             return this.path;
82         },
83         sortCn: function()
84         {
85             this.cn.sort(function(a,b) {
86                 return a.path > b.path;// ? 1 : -1;
87             });
88         },
89         // should be in palete provider really..
90         
91         guessName : function(ar) // turns the object into full name.
92         {
93              // eg. xns: Roo, xtype: XXX -> Roo.xxx
94             if (typeof( ar['|xns'] ) == 'undefined' || typeof( ar['xtype'] ) == 'undefined') {
95                 return '';
96                }
97              
98             return ar['|xns'] +'.' + ar['xtype'];
99                             
100                                  
101         },
102         
103         /*
104         Roo specific?
105         toSourceStdClass: function()
106         {
107             var cfg = this.items[0]
108             var fcfg = XObject.extend({ },  this.items[0]);
109             delete fcfg['*class'];
110             delete fcfg['*extends'];
111             delete fcfg['*static'];
112             delete fcfg['|constructor'];
113             
114             var hasExtends = (typeof(cfg['*extends']) != 'undefined') && cfg['*extends'].length;
115             var hasConstructor = (typeof(cfg['|constructor']) != 'undefined');
116             var isStatic = (typeof(cfg['*static']) == '*static');
117             
118             var newline = '';
119             var endline = '';
120             if (hasExtends) {
121                 newline =  hasConstructor ? 
122                 
123                  
124                     cfg['//constructor'] + "\n" + 
125                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
126                     "Roo.extend(" + cfg['*class'] + ":, " + cfg['*extends'] + ", " :
127                     
128                     cfg['//*class'] + "\n" + 
129                     cfg['*class'] + " = new " + cfg['*extends'] + "(" ;
130                 
131                 endline = ');';
132             } else {
133                 
134                 
135                 
136                 newline  = hasConstructor ? 
137                 
138                     cfg['//constructor'] + "\n" + 
139                     cfg['*class'] + " = " + cfg['|constructor'] + "\n\n"+ 
140                     'Roo.apply( ' +  cfg['*class'] + ".prototype , " :
141                     
142                     cfg['//*class'] + "\n" + 
143                     cfg['*class'] + " = ";
144                 
145                     
146                 endline = hasConstructor ? ');' : ';';
147             }
148                   
149             return this.outputHeader() + 
150                     newline + 
151                     this.objectToJsString(fcfg,1) +
152                     endline;
153             
154             
155             
156          
157         },
158         */
159         
160         copyTo: function(path, cb)
161         {
162             var _this = this;
163             this.loadItems(function() {
164                 
165                 _this.path = path;
166                 cb();
167             });
168             
169         },
170         
171         /**
172          * 
173          * munge JSON tree into Javascript code.
174          * 
175          * FIXME: + or / prefixes to properties hide it from renderer.
176          * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
177          * FIXME: needs to understand what properties might be translatable (eg. double quotes)
178          * 
179          * @arg {object} obj the object or array to munge..
180          * @arg {boolean} isListener - is the array being sent a listener..
181          * @arg {string} pad - the padding to indent with. 
182          */
183         
184         
185         mungeToString:  function(obj, isListener, pad)
186         {
187             pad = pad || '    ';
188             var keys = [];
189             var isArray = false;
190             isListener = isListener || false;
191              
192             // am I munging a object or array...
193             if (obj.constructor.toString() === Array.toString()) {
194                 for (var i= 0; i < obj.length; i++) {
195                     keys.push(i);
196                 }
197                 isArray = true;
198             } else {
199                 for (var i in obj) {
200                     keys.push(i);
201                 }
202             }
203             
204             
205             var els = []; 
206             var skip = [];
207             if (!isArray && 
208                     typeof(obj['|xns']) != 'undefined' &&
209                     typeof(obj['xtype']) != 'undefined'
210                 ) {
211                     this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els);
212                     //els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
213                     skip.push('|xns','xtype');
214                 }
215             
216             
217             if (!isArray && obj.items && obj.items.length) {
218                 // look for props..
219                 var newitems = [];
220                 obj.items.forEach(function(pl) {
221                     if (typeof(pl['*prop']) == 'undefined') {
222                         newitems.push(pl);
223                         return;
224                     }
225                     // we have a prop...
226                     var prop = pl['*prop'] + '';
227                     delete pl['*prop'];
228                     if (!prop.match(/\[\]$/)) {
229                         // it's a standard prop..
230                         obj[prop] = pl;
231                         keys.push(prop);
232                         return;
233                     }
234                     prop  = prop.substring(0, prop.length -2); //strip []
235                     // it's an array type..
236                     obj[prop] = obj[prop]  || [];
237                     obj[prop].push(pl);
238                     print("ADDNG PROP:" + prop + ' ' + keys.indexOf(prop) );
239                     if (keys.indexOf(prop) < 0) {
240                         keys.push(prop);
241                     }
242                     
243                     
244                     
245                 });
246                 obj.items = newitems;
247                 if (!obj.items.length) {
248                     delete obj.items;
249                 }
250                 
251             }
252             
253             
254             
255             
256             
257             var _this = this;
258             
259             var left =  '';
260             
261             keys.forEach(function(i) {
262               
263                 if (typeof(obj[i]) == 'undefined') { // empty or removed.
264                     return;
265                 }
266                 var el = obj[i];
267                 if (!isArray && skip.indexOf(i) > -1) { // things we do not write..
268                     return;
269                 }
270                 if (!isArray) {
271                     // set the key to be quoted with singel quotes..
272                     var leftv = i[0] == '|' ? i.substring(1) : i;
273                     if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
274                         left = "'" + leftv + "'";
275                     } else if (leftv.match(/[^A-Z_]+/i)) { // not plain a-z... - quoted.
276                         var val = JSON.stringify(leftv);
277                         left = "'" + val.substring(1, val.length-1).replace(/'/g, "\\'") + "'";
278                     } else {
279                         left = '' + leftv;
280                     }
281                     left += ' : ';
282                     
283                 }
284                 if (isListener) {
285                     // change the lines...
286                     var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
287                     var lines = str.split("\n");
288                     if (lines.length > 1) {
289                         str = lines.join("\n" + pad);
290                     }
291                     els.push(left  + str);
292                     return;
293                 }
294                  
295                 
296                 
297                 //var left = isArray ? '' : (JSON.stringify(i) + " : " )
298                 
299                 if (i[0] == '|') {
300                     // does not hapepnd with arrays..
301                     if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
302                         return;
303                     }
304                     // this needs to go...
305                     //if (typeof(el) == 'string'  && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
306                     //    return;
307                     //}
308                     
309                     var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
310                     var lines = str.split("\n");
311                     if (lines.length > 1) {
312                         str = lines.join("\n" + pad);
313                     }
314                     
315                     els.push(left + str);
316                     return;
317                 }
318                 
319                 
320                 
321                 
322                 if (typeof(el) == 'object') {
323                     
324                     // we can skip empty items lists and empty listeners..
325                     //if (!isArray && i == 'items' && !el.length) {
326                     //    return; 
327                     //}
328                    // 
329                     var right = _this.mungeToString(el, i == 'listeners', pad + '    ');
330                     if (typeof(right) != 'undefined') {
331                         els.push(left + right);
332                     }
333                 
334                     return;
335                 }
336                 // standard. .
337                 if (typeof(obj[i]) != 'string') {
338                     els.push(left + JSON.stringify(obj[i]));
339                     return;
340                 }
341                 // strings..
342                 if (!_this.doubleStringProps) {
343                     els.push(left + JSON.stringify(obj[i]));
344                     return;
345                 }
346                 if (_this.doubleStringProps.indexOf(i) > -1) {
347                     els.push(left + JSON.stringify(obj[i]));
348                     return;
349                 }
350                 // single quote..
351                 els.push(left + "'" + obj[i].replace(/'/g, "\\'") + "'");
352                 
353
354             });
355             
356             if (!isArray && !els.length) {
357                 return;
358             }
359             //output the thing.
360             var spad = pad.substring(0, pad.length-4);
361             return (isArray ? '[' : '{') + "\n" +
362                 pad  + els.join(",\n" + pad ) + 
363                 "\n" + spad + (isArray ? ']' : '}');
364                
365             
366             
367         } 
368         
369          
370         
371     }
372     
373      
374     
375 );
376
377
378
379
380
381