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