Palete/Palete.vala
[app.Builder.js] / Palete / Palete.vala
1
2 namespace Palete 
3 {
4     public errordomain Error {
5         INVALID_TYPE,
6         NEED_IMPLEMENTING
7     }
8     
9
10     static Gee.HashMap<string,Palete>? cache = null;
11     
12     public static   Palete factory(string xtype)
13     {
14         if (cache == null) {
15             cache = new Gee.HashMap<string,Palete>();
16         }
17         if (this.cache.get(xtype) != null) {
18             return this.cache.get(xtype);
19         }
20         switch(xtype) {
21             case "Gtk":
22                 this.cache.set(xtype, new Gtk());
23                 break;
24             case "Roo":
25                 this.cache.set(xtype, new Roo());
26                 break;
27             default:
28                 throw Error.INVALID_TYPE("invalid argument to Palete factory");
29         }
30         return this.cache.get(xtype);
31     }
32        
33
34     public class Palete : Object 
35     {
36         
37        
38   
39         
40         public Palete()
41         {
42             // nothing?
43         }
44         
45         
46         //map : false, // array of mappings   left: [] , right : []
47         
48         //items : false, // the tree of nodes.
49         
50         
51
52
53         string  guessName(JsRender.Node ar) throws Error // turns the object into full name.
54         {
55             throw new Error.NEED_IMPLEMENTING("xxx. guessName needs implimenting");
56         }
57             
58
59             
60         /**
61          * gather a  list of potentional objects that can be added..
62          * 
63          */
64         /*
65         gatherList: function (existing) {
66             existing = existing || [];
67            // existing.push('*top'); // always have top
68             var ret  = []; 
69             console.log("GATHER LIST? " + this.map.length);
70             
71             
72             function addRight(right) {
73                 right.forEach(function(r) {
74                     if (ret.indexOf(r) > -1) {
75                         return;
76                     }
77                     ret.push(r);
78                 });
79             }
80             
81             this.map.forEach(function(m) {
82                 var done = false
83                 m.left.forEach( function(left) {
84                     if (done) return; 
85                     
86                     var l = left.replace(/:.*$/, '');
87                    // print("chk:" + l + " in " + existing.join(',')); 
88                     if (existing.indexOf(l) > -1) {
89                         addRight(m.right);
90                         done =true;
91                         //return true; // no more needed..
92                     }
93                 });
94                 
95             });
96             ret.sort();
97             
98            // console.dump(ret);
99             return ret;
100             
101             
102             
103         },
104         
105         getDropList : function(rval)
106         {
107             
108             var ret = [];
109             this.map.forEach( function(m) {
110                 if (m.right.indexOf(rval) > -1) {
111                     m.left.forEach(function(l) {
112                         if (ret.indexOf(l) > -1) {
113                             return;
114                         }
115                         ret.push(l)
116                     });
117                 }
118                 
119             });
120             console.log("DROP LIST:");
121             console.dump(ret);
122             return ret;
123             
124         },
125         /**
126          * basic guess type.. 
127          * 
128          * /
129         findType : function (data, prop, value)
130         {
131             if (prop[0] == '|') {
132                 return 'function';
133             }
134             return typeof(value);
135         },
136         
137         
138         findOptions : function(ename)
139         {
140             switch(ename.toLowerCase()) {
141                 case 'boolean': 
142                     return [ 'true', 'false' ];
143                 // everything else does not have options.
144                 case 'string': 
145                 case 'utf8': 
146                 case 'int': 
147                 case 'uint': 
148                 case 'function': 
149                     return false;
150                 default: 
151                     console.log("OOPS: = unknown type: " + ename);
152                     return false;
153             }
154         },
155         confirmCanAdd: function(parent, child) {
156             // confirms that one obj can be added to another.
157             // returns true, for items, or list of properties that can hold it..
158             return true;
159             
160         },
161         getDefaultPack: function(pname, cname) {
162             return 'add';
163         },
164         saveTemplate: function(name, data)
165         {
166             var gn = this.guessName(JSON.parse(data));
167             // store it in user's directory..
168             var appdir = GLib.get_home_dir() + '/.Builder'; 
169             
170             if (!File.isDirectory(appdir+ '/' + gn)) {
171                 File.mkdir(appdir+ '/' + gn);
172             }
173             File.write(appdir+ '/' + gn + '/' +  name + '.json', data);
174             
175         },
176         /**
177          * list templates - in home directory (and app dir in future...)
178          * @param {String} name  - eg. Gtk.Window..
179          * @return {Array} list of templates available..
180          * /
181         listTemplates : function(name)
182         {
183             
184             var gn = name;
185             if (typeof(gn) != 'string') {
186                 gn = this.guessName(gn);
187             }
188             
189             
190             var dir= GLib.get_home_dir() + '/.Builder/' + gn; 
191             if (!File.isDirectory(dir)) {
192                 return [];
193             }
194             var ret =  [];
195             File.list(dir).forEach(function(n) {
196                 if (!n.match(/\.json$/)) {
197                     return;
198                 }
199                 
200                 ret.push({
201                     path : dir + '/' + n,
202                     name:  n.replace(/\.json$/,'')
203                 });
204             });
205             return ret;
206             
207         },
208         loadTemplate : function(path)
209         {
210             return JSON.parse(File.read(path));
211         }
212     */        
213         
214     }
215
216 }
217
218
219