Project/Project.vala
[app.Builder.js] / Project / Project.vala
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
15 public class Project.Project {
16     
17     
18     public string id = "";
19     public string fn = "";
20     public string name = "";
21     public Gee.HashMap<string,string> paths;
22     public GLib.List<JsRender.JsRender> files ;
23     //tree : false,
24     public string xtype = "";
25     
26     
27     Project (string path) {
28         
29         //this.name = name;
30         
31         
32         this.paths = new Gee.HashMap<string,string>();
33         this.files = new GLib.List<JsRender.JsRender>();
34         //XObject.extend(this, cfg);
35         //this.files = { }; 
36         
37         
38         this.scanDirs();
39         
40     }
41     
42     public void onChanged() {
43        // ProjectManager.fireEvent('changed');
44         
45     }
46     /*
47     public load
48      
49         
50         load : function (o)  // is this used??
51         {
52             if (!this.fetchTree) {
53                 console.log("Project.getTree tree called on base object?!?!");
54                 return false;
55             }
56             
57             if (this.files) {
58                 return o.success.apply(o.scope || this, [this]);
59             }
60             return this.fetchTree(o);
61             
62         },
63     */
64    // public Palete  getPalete ()
65     //{
66             //print("Project.Base: GET PROVIDER FOR " + this.xtype);
67    //         return  ProjectManager.getPalete(this.xtype);
68    // }
69     
70     public string toJSON()
71     {
72         
73         var builder = new Json.Builder ();
74         
75         builder.begin_object ();
76         
77         builder.set_member_name ("name");
78         builder.add_string_value (this.name);
79
80         
81         builder.set_member_name ("fn");
82         builder.add_string_value (this.fn);
83
84         builder.set_member_name ("xtype");
85         builder.add_string_value (this.xtype);
86         
87         // file ??? try/false?
88         builder.set_member_name ("paths");
89         
90         
91         builder.begin_array ();
92         
93         
94         var iter = this.paths.map_iterator();
95         while (null != iter.next()) {
96             builder.add_string_value (iter.get_key());
97         }
98         builder.end_array ();
99         
100         
101         var  generator = new Json.Generator ();
102         var  root = builder.get_root ();
103         generator.set_root (root);
104
105         return  generator.to_data (null);
106               
107           
108     }
109     // returns the first path
110     public string getName()
111     {
112         var iter = this.paths.map_iterator();
113         while (null != iter.next()) {
114             return GLib.basename(iter.get_key());
115         }
116       
117         return "";
118     }
119     /**
120      *
121      * to tree - > should
122      */
123  
124     public Array<JsRender.JsRender> toTree ()
125     {
126             
127          
128          
129         var files = new Gee.Map<string,Json.Object>();
130         
131         
132         for(var i = 0; i < this.files.length; i++) {
133             var fo = this.files.index(i);
134             
135             fo.hasParent = false;
136             fo.cn = new Array<JsRender.JsRender>();
137             
138             if (this.files.index(i).fullname.length > 0) {
139                 files.set(fo.fullname, f);
140             }
141         }
142         
143         var iter = files.map_iterator();
144         while (null != iter.next()) {
145             var f = iter.get_value();
146             
147             var par = f.parent;
148             if (par.length < 1) {
149                 return;
150             }
151             if (!files.has_key(par)) {
152                 return;
153             }
154             files.get(par).cn.add(f);
155             f.hasParent = true;
156              
157         };
158             
159         var ret = new Array<JsRender.JsRender>();
160         iter = files.map_iterator();
161         while (null != iter.next()) {
162             var f = iter.get_value();
163                 
164             //   f.sortCn();
165             if (f.hasParent) {
166                 continue;
167             }
168             if (files.has_key(f.fullname)) {
169             
170                 ret.add(f);
171             }
172         }
173         ret.sort( (a,b) => {
174             return a.path > b.path ? 1 : -1;
175         });
176         
177         
178         //print(JSON.stringify(ret,null,4));
179             
180         return ret;  
181              
182             
183     }
184     
185     
186     
187     public JsRender.JsRender? getById(string id)
188     {
189         
190        for(var i = 0; i < this.files.length; i++) {
191             var fo = this.files.index(i);
192             
193             
194             //console.log(f.id + '?=' + id);
195             if (f.id == id) {
196                 return f;
197             }
198         };
199         return null;
200     }
201         
202     public JsRender.JsRender loadFileOnly (string path)
203     {
204         var xt = this.xtype;
205         return JsRender.Base.factory(xt, this, path);
206         
207     }
208     
209     public JsRender.JsRender create(string filename)
210     {
211         var ret = this.loadFileOnly(filename);
212         ret.save();
213         this.addFile(ret);
214         return ret;
215         
216     }
217         
218          
219     public void addFile(JsRender.JsRender pfile) { // add a single file, and trigger changed.
220         this.files.append_val(pfile); // duplicate check?
221         this.onChanged();
222     }
223     
224     public void add(string path, string type)
225     {
226         this.paths.set(path,type);
227         //Seed.print(" type is '" + type + "'");
228         if (type == "dir") {
229             this.scanDir(path);
230         //    console.dump(this.files);
231         }
232         if (type == "file" ) {
233             this.files.append_val(this.loadFileOnly( path ));
234         }
235         this.onChanged();
236         
237     }
238     public void  scanDirs()
239     {
240         var iter = this.paths.map_iterator();
241         while (iter.next()) {
242             if (iter.get_value() != "dir") {
243                 continue;
244             }
245             this.scanDir(iter.get_key());
246         }
247         //console.dump(this.files);
248         
249     }
250         // list files.
251     public void scanDir(string dir, int dp =0 ) 
252     {
253         //dp = dp || 0;
254         //Seed.print("Project.Base: Running scandir on " + dir);
255         if (dp > 5) { // no more than 5 deep?
256             return;
257         }
258         // this should be done async -- but since we are getting the proto up ...
259         
260             
261         var f = File.new_for_path(dir);
262         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
263         
264         string[] subs;
265         
266         while ((next_file = file_enum.next_file(null)) != null) {
267             var fn = next_file.get_display_name();
268     
269              
270             //console.log('trying ' + dir + '/' + fn);
271             
272             if (fn[0] == '.') { // skip hidden
273                 continue;
274             }
275             
276             if (FileUtils.test(dir  + '/' + fn, GLib.FileTest.IS_DIR)) {
277                 subs += (dir  + '/' + fn);
278                 continue;
279             }
280             
281             if (!Regex.match_simple("\\.bjs$", fn)) {
282                 continue;
283             }
284             /*
285             var parent = "";
286             //if (dp > 0 ) {
287             
288             var sp = dir.split("/");
289             var parent = "";
290             for (var i = 0; i < sp.length; i++) {
291                 
292             }
293             
294             /*
295             sp = sp.splice(sp.length - (dp +1), (dp +1));
296             parent = sp.join('.');
297             
298             
299             if (typeof(_this.files[dir  + '/' + fn]) != 'undefined') {
300                 // we already have it..
301                 _this.files[dir  + '/' + fn].parent = parent;
302                 return;
303             }
304             */
305             var xt = this.xtype;
306             JsRender.Base.factory(xt,this, dir + "/" + fn);
307             // parent ?? 
308             
309              
310         }
311         for (var i = 0; i < subs.length; i++) {
312              this.scanDir(subs[i], dp+1);
313         }
314         
315     }
316       
317 }
318
319