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