Attribute changed old-javascript
[app.Builder.js] / old-javascript / 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.stringify(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             print(JSON.stringify(this.files,null,4));
140             
141             // have parents -> add them to parent...
142             var files = {};
143             var parents = {};
144             for (var k in this.files) {
145                 
146                 var f = this.files[k];
147                 if (!f) {
148                     continue;
149                    }
150                 ///console.dump(f);
151                 f.hasParent = false;
152                 f.cn = [];
153                 //console.log(f.name);
154                 if (f.fullname) {
155                     files[f.fullname] = f;
156                 }
157             };
158             
159             // add Parent pointer..
160             for (var k in this.files) {
161                 var f = this.files[k];
162                 console.log(f.parent + ":" + f.name);
163                 if (f.parent && typeof(files[f.parent]) != 'undefined') {
164                     // add it to parent;
165                     
166                     files[f.parent].cn.push(f);
167                     f.hasParent = true;
168                     
169                 }
170                 
171                 
172             };
173             
174               
175             
176             var ret = [];
177             for (var k in this.files) {
178                 var f = this.files[k];
179                 
180                 f.sortCn();
181                 if (f.hasParent) {
182                     continue;
183                 }
184                 if (typeof(files[f.fullname]) != 'undefined') {
185                     ret.push(f);
186                 }
187                 
188             };
189             
190             
191             ret.sort(function(a,b) {
192                 return a.path > b.path ? 1 : -1;
193             });
194             
195             print(JSON.stringify(ret,null,4));
196             
197             
198             this.tree = ret;
199             return this.tree;
200              
201             
202         },
203         getById : function(id)
204         {
205             var ret = false;
206             for (var k in this.files) {
207                 var f = this.files[k];
208                 
209                 console.log(f.id + '?=' + id);
210                 if (f.id == id) {
211                     return f;
212                 }
213             };
214             return ret;
215         },
216         
217         loadFileOnly : function(path)
218         {
219             
220             var xt = this.xtype;
221             var cls = imports.JsRender[xt][xt];
222             return  new cls({
223                 path : path,
224                 parent : '',
225                 project : this
226             });
227         },
228         create : function(filename)
229         {
230             var ret = this.loadFileOnly(filename);
231             ret.save();
232             this.addFile(ret);
233             return ret;
234             
235         },
236         
237          
238         addFile: function(pfile) { // add a single file, and trigger changed.
239             this.files[pfile.path] = pfile
240             this.fireEvent('changed', this);
241         },
242         
243         add: function(path, type)
244         {
245             this.paths = this.paths || { };
246             this.paths[path] = type;
247             //Seed.print(" type is '" + type + "'");
248             if (type == 'dir') {
249                 this.scanDir(path);
250             //    console.dump(this.files);
251             }
252             if (type == 'file' ) {
253                 
254                 this.files[path] = this.loadFileOnly( path );
255             }
256             
257             // console.dump(this.files);
258             this.fireEvent('changed', this);
259             
260         },
261         
262         scanDirs: function()
263         {
264             this.files = this.files  || { };
265             for (var d in this.paths) {
266                 if (this.paths[d] == 'dir') {
267                     this.scanDir(d);
268                 }
269                 // otherwise add a file..
270             }
271             //console.dump(this.files);
272             
273         },
274         
275         
276         // list files.
277         scanDir : function(dir, dp) 
278         {
279             dp = dp || 0;
280             Seed.print("Project.Base: Running scandir on " + dir);
281             if (dp > 5) { // no more than 5 deep?
282                 return;
283             }
284             // this should be done async -- but since we are getting the proto up ...
285             var dirs = File.list(dir);
286             
287             ///print(dirs); Seed.exit();
288             
289             var subs = [];
290             var _this = this;
291             dirs.forEach(function( fn ){ 
292                  
293                 //console.log('trying ' + dir + '/' + fn);
294                 if (!fn) {
295                     subs.forEach( function(s) {
296                         _this.scanDir(s, dp+1);
297                     });
298                     return;
299                 }
300                 if (fn[0] == '.') { // skip hidden
301                     return;
302                 }
303                 
304                 if (GLib.file_test(dir  + '/' + fn, GLib.FileTest.IS_DIR)) {
305                     subs.push(dir  + '/' + fn);
306                     return;
307                 }
308                 
309                 if (!fn.match(/\.bjs$/)) {
310                     return;
311                 }
312                 var parent = '';
313                 //if (dp > 0 ) {
314                 var sp = dir.split('/');
315                 sp = sp.splice(sp.length - (dp +1), (dp +1));
316                 parent = sp.join('.');
317                 
318                 
319                 if (typeof(_this.files[dir  + '/' + fn]) != 'undefined') {
320                     // we already have it..
321                     _this.files[dir  + '/' + fn].parent = parent;
322                     return;
323                 }
324                 var xt = _this.xtype;
325                 var cls = imports.JsRender[xt][xt];
326                 
327                 //Seed.print("Adding file " + dir  + '/' + fn);
328                 _this.files[dir  + '/' + fn] = new cls({
329                     path : dir  + '/' + fn,
330                     parent : parent,
331                     project : _this
332                 });
333                 //console.log(this.files[dir  + '/' + fn] );
334                 /*
335                 var f = Gio.file_new_for_path(dir + '/' + fn);
336                 var inf = f.query_info('standard::*');
337                 var tv = new GLib.TimeVal();
338                 inf.get_modification_time(tv);
339                 
340                 // should it do something about this information..
341                 // fixme... time data is busted..
342                 this.files[dir  + '/' + fn] = '' + tv.tv_sec + '.' + tv.tv_usec;
343                 */
344             });
345              
346             
347         }
348         
349         
350         
351         
352         
353
354     }
355 ); 
356
357
358      
359