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