47c31bfcb16f14542d0bfe956a89c34a75a410cc
[app.Builder.js] / Project / Base.js
1 //<Script type="text/javascript">
2
3 /**
4  * Project Object
5  * 
6  * Projects can only contain one directory... - it can import other projects..(later)
7  * 
8  * we need to sort out that - paths is currently a key/value array..
9  * 
10  * 
11  * 
12  */
13
14 Gio = imports.gi.Gio;
15 GLib = imports.gi.GLib; 
16
17
18 console = imports.console;
19 XObject = imports.XObject.XObject;
20 File = imports.File.File;
21
22 ProjectManager = imports.ProjectManager.ProjectManager;
23 Observable = imports.Observable.Observable;
24
25 Base = XObject.define( 
26     function(cfg) {
27         
28         
29         this.addEvents({ 'changed'  : true });
30         var _this = this;
31         this.on('changed' , function() {
32             Seed.print("Project.Base: Calling PM - changed");
33             
34             ProjectManager.fireEvent('changed');
35         });
36         XObject.extend(this, cfg);
37         this.files = { }; 
38         /*
39         if (this.files ){
40             for (var f in this.files) {
41                 var xt = this.xtype;
42                 var cls = imports.Builder.Provider.File[xt][xt];
43                 this.files[f] = cls(this.files[f]);
44             }
45         }
46         */
47         
48         
49         this.scanDirs();
50         
51     },
52     Observable, 
53     {
54         id : false,
55         fn:  false, // the md5 sum for this one..
56         paths : false,
57         files : false,
58         tree : false,
59         xtype : false,
60         
61         load : function (o)  // is this used??
62         {
63             if (!this.fetchTree) {
64                 console.log("Project.getTree tree called on base object?!?!");
65                 return false;
66             }
67             
68             if (this.files) {
69                 return o.success.apply(o.scope || this, [this]);
70             }
71             return this.fetchTree(o);
72             
73         },
74         getPalete : function()
75         {
76             print("Project.Base: GET PROVIDER FOR " + this.xtype);
77             return  ProjectManager.getPalete(this.xtype);
78         },
79         toJSON : function()
80         {
81             var ret = { };
82             var _this = this;
83             for (var k in _this) {
84                 
85                 if (['files', 'tree'].indexOf(k) > -1) {
86                     continue;
87                 }
88                 if (k != 'paths') {
89                     if ((typeof(_this[k]) == 'object') ||(typeof(_this[k]) == 'function')) {
90                         continue;
91                     }
92                 }
93                 print("Project.Base: Storing " + k);
94                 ret[k] = _this[k];
95                 
96             }
97             
98             
99             // deal with files..
100             //for (var f in this.files) {
101             //    print(f);
102             //    ret.files[f] = this.files[f].toJsonArray();
103            // }
104             
105             
106             return JSON.(ret);
107           
108           
109         },
110         getName :function()
111         {
112             //returns the basename of the first path..
113             for (var i in this.paths) {
114                 return GLib.basename(i);
115             }
116             return false;
117         },
118         
119         toTree : function()
120         {
121             
122             
123             // normally this will build tree's based on info in the file stuff..
124             // but for the time being, we just return an array of all the files.
125             
126             
127             
128             //if (this.tree) {
129              //   return this.tree;
130             //}
131             
132             this.tree = [];
133             /*
134             for (var f in this.files) {
135                 this.tree.push(this.files[f]);
136             }
137             return this.tree;
138             */
139             
140             // have parents -> add them to parent...
141             var files = {};
142             var parents = {};
143             for (var k in this.files) {
144                 
145                 var f = this.files[k];
146                 if (!f) {
147                     continue;
148                    }
149                 ///console.dump(f);
150                 f.hasParent = false;
151                 f.cn = [];
152                 //console.log(f.name);
153                 if (f.fullname) {
154                     files[f.fullname] = f;
155                 }
156             };
157             
158             // add Parent pointer..
159             for (var k in this.files) {
160                 var f = this.files[k];
161                 console.log(f.parent + ":" + f.name);
162                 if (f.parent && typeof(files[f.parent]) != 'undefined') {
163                     // add it to parent;
164                     
165                     files[f.parent].cn.push(f);
166                     f.hasParent = true;
167                     
168                 }
169                 
170                 
171             };
172             
173               
174             
175             var ret = [];
176             for (var k in this.files) {
177                 var f = this.files[k];
178                 
179                 f.sortCn();
180                 if (f.hasParent) {
181                     continue;
182                 }
183                 if (typeof(files[f.fullname]) != 'undefined') {
184                     ret.push(f);
185                 }
186                 
187             };
188             
189             
190             ret.sort(function(a,b) {
191                 return a.path > b.path ? 1 : -1;
192             });
193             this.tree = ret;
194             return this.tree;
195              
196             
197         },
198         getById : function(id)
199         {
200             var ret = false;
201             for (var k in this.files) {
202                 var f = this.files[k];
203                 
204                 console.log(f.id + '?=' + id);
205                 if (f.id == id) {
206                     return f;
207                 }
208             };
209             return ret;
210         },
211         
212         loadFileOnly : function(path)
213         {
214             
215             var xt = this.xtype;
216             var cls = imports.JsRender[xt][xt];
217             return  new cls({
218                 path : path,
219                 parent : '',
220                 project : this
221             });
222         },
223         create : function(filename)
224         {
225             var ret = this.loadFileOnly(filename);
226             ret.save();
227             this.addFile(ret);
228             return ret;
229             
230         },
231         
232          
233         addFile: function(pfile) { // add a single file, and trigger changed.
234             this.files[pfile.path] = pfile
235             this.fireEvent('changed', this);
236         },
237         
238         add: function(path, type)
239         {
240             this.paths = this.paths || { };
241             this.paths[path] = type;
242             //Seed.print(" type is '" + type + "'");
243             if (type == 'dir') {
244                 this.scanDir(path);
245             //    console.dump(this.files);
246             }
247             if (type == 'file' ) {
248                 
249                 this.files[path] = this.loadFileOnly( path );
250             }
251             
252             // console.dump(this.files);
253             this.fireEvent('changed', this);
254             
255         },
256         
257         scanDirs: function()
258         {
259             this.files = this.files  || { };
260             for (var d in this.paths) {
261                 if (this.paths[d] == 'dir') {
262                     this.scanDir(d);
263                 }
264                 // otherwise add a file..
265             }
266             //console.dump(this.files);
267             
268         },
269         
270         
271         // list files.
272         scanDir : function(dir, dp) 
273         {
274             dp = dp || 0;
275             Seed.print("Project.Base: Running scandir on " + dir);
276             if (dp > 5) { // no more than 5 deep?
277                 return;
278             }
279             // this should be done async -- but since we are getting the proto up ...
280             var dirs = File.list(dir);
281             
282             ///print(dirs); Seed.exit();
283             
284             var subs = [];
285             var _this = this;
286             dirs.forEach(function( fn ){ 
287                  
288                 //console.log('trying ' + dir + '/' + fn);
289                 if (!fn) {
290                     subs.forEach( function(s) {
291                         _this.scanDir(s, dp+1);
292                     });
293                     return;
294                 }
295                 if (fn[0] == '.') { // skip hidden
296                     return;
297                 }
298                 
299                 if (GLib.file_test(dir  + '/' + fn, GLib.FileTest.IS_DIR)) {
300                     subs.push(dir  + '/' + fn);
301                     return;
302                 }
303                 
304                 if (!fn.match(/\.bjs$/)) {
305                     return;
306                 }
307                 var parent = '';
308                 //if (dp > 0 ) {
309                 var sp = dir.split('/');
310                 sp = sp.splice(sp.length - (dp +1), (dp +1));
311                 parent = sp.join('.');
312                 
313                 
314                 if (typeof(_this.files[dir  + '/' + fn]) != 'undefined') {
315                     // we already have it..
316                     _this.files[dir  + '/' + fn].parent = parent;
317                     return;
318                 }
319                 var xt = _this.xtype;
320                 var cls = imports.JsRender[xt][xt];
321                 
322                 //Seed.print("Adding file " + dir  + '/' + fn);
323                 _this.files[dir  + '/' + fn] = new cls({
324                     path : dir  + '/' + fn,
325                     parent : parent,
326                     project : _this
327                 });
328                 //console.log(this.files[dir  + '/' + fn] );
329                 /*
330                 var f = Gio.file_new_for_path(dir + '/' + fn);
331                 var inf = f.query_info('standard::*');
332                 var tv = new GLib.TimeVal();
333                 inf.get_modification_time(tv);
334                 
335                 // should it do something about this information..
336                 // fixme... time data is busted..
337                 this.files[dir  + '/' + fn] = '' + tv.tv_sec + '.' + tv.tv_usec;
338                 */
339             });
340              
341             
342         }
343         
344         
345         
346         
347         
348
349     }
350 ); 
351
352
353      
354