src/JsRender/NodeToJs.vala
[app.Builder.js] / src / JsRender / NodeToJs.vala
1 /**
2  * 
3  * Code to convert node tree to Javascript...
4  * 
5  * usage : x = (new JsRender.NodeToJs(node)).munge();
6  * 
7 */
8
9
10
11
12 public class JsRender.NodeToJs : Object {
13
14         static uint indent = 1;
15          static string indent_str = " ";
16         Node node;
17         Gee.ArrayList<string>  doubleStringProps;
18         string pad;
19         Gee.ArrayList<string> els;
20         //Gee.ArrayList<string> skip;
21         Gee.HashMap<string,string> ar_props;
22
23
24         
25         public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad) 
26         {
27                 this.node = node;
28                 this.doubleStringProps = doubleStringProps;
29                 this.pad = pad;
30                 this.els = new Gee.ArrayList<string>(); 
31                 //this.skip = new Gee.ArrayList<string>();
32                 this.ar_props = new Gee.HashMap<string,string>();
33
34         }
35         
36         public string munge ( )
37         {
38                 //return this.mungeToString(this.node);
39
40         
41                 
42                 this.checkChildren();
43                 this.readProps();
44                 this.readArrayProps();
45                 this.readListeners();
46
47                 if (!this.node.props.has_key("* xinclude")) {
48                         this.iterChildren();
49                 }
50                 
51                 
52                 
53                 
54                 if (this.els.size < 1) {
55                         return "";
56                 }
57                 // oprops...    
58                         
59                 var spad = pad.substring(0, this.pad.length-indent);
60                 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
61                 //print ("STR PROPS: " + str_props);
62                 if (!this.node.props.has_key("* xinclude")) {
63                         return   "{\n" +
64                                 this.pad  + str_props + 
65                                 "\n" + spad +  "}";
66                 }
67                 // xinclude...
68
69
70                 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
71                          "{\n" +
72                                 this.pad  + str_props + 
73                                 "\n" + spad +  "})";
74                      
75         } 
76
77         string gLibStringListJoin( string sep, Gee.ArrayList<string> ar) 
78         {
79                 var ret = "";
80                 for (var i = 0; i < ar.size; i++) {
81                         ret += i>0 ? sep : "";
82                         ret += ar.get(i);
83                 }
84                 return ret;
85
86         }
87         public string mungeChild(string pad ,  Node cnode)
88         {
89                 var x = new  NodeToJs(cnode, this.doubleStringProps, pad);
90                 return x.munge();
91         }
92         
93         
94
95         
96         public void checkChildren () 
97         {
98                 
99                  
100                 // look throught he chilren == looking for * prop.. -- fixme might not work..
101                 
102                 
103                 if (!this.node.hasChildren()) {
104                         return;
105                 }
106                 // look for '*props'
107            
108                 for (var ii =0; ii< this.node.items.size; ii++) {
109                         var pl = this.node.items.get(ii);
110                         if (!pl.props.has_key("* prop")) {
111                                 //newitems.add(pl);
112                                 continue;
113                         }
114                         
115                         //print(JSON.stringify(pl,null,4));
116                         // we have a prop...
117                         //var prop = pl['*prop'] + '';
118                         //delete pl['*prop'];
119                         var prop = pl.get("* prop");
120                         print("got prop "+ prop + "\n");
121                         
122                         // name ends in [];
123                         if (! Regex.match_simple("\\[\\]$", prop)) {
124                                 // it's a standard prop..
125                                 
126                                 // munge property..??
127                                 this.els.add( prop  + " : " + this.mungeChild (  this.pad + indent_str,  pl));
128                                 
129                                 
130                                 //keys.push(prop);
131                                 continue;
132                         }
133
134
135
136                         
137                         var sprop  = prop.replace("[]", "");
138                         print("sprop is : " + sprop + "\n");
139                         
140                         // it's an array type..
141                         var old = "";
142                         if (!this.ar_props.has_key(sprop)) {
143                                 
144                                 this.ar_props.set(sprop, "");
145                                 
146                         } else {
147                                 old = this.ar_props.get(sprop);
148                         }
149                         var nstr  = old += old.length > 0 ? ",\n" : "";
150                         nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str ,   pl);
151                         
152                         this.ar_props.set(sprop, nstr);
153                          
154                         
155                 }
156                  
157         }
158         /*
159  * Standardize this crap...
160  * 
161  * standard properties (use to set)
162  *          If they are long values show the dialog..
163  *
164  * someprop : ....
165  * bool is_xxx  :: can show a pulldown.. (true/false)
166  * string html  
167  * $ string html  = string with value interpolated eg. baseURL + ".." 
168  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
169  * $ untypedvalue = javascript untyped value...  
170  * _ string html ... = translatable..
171
172  * 
173  * object properties (not part of the GOjbect being wrapped?
174  * # Gee.ArrayList<Xcls_fileitem> fileitems
175  * 
176  * signals
177  * @ void open 
178  * 
179  * methods -- always text editor..
180  * | void clearFiles
181  * | someJSmethod
182  * 
183  * specials
184  * * prop -- string
185  * * args  -- string
186  * * ctor -- string
187  * * init -- big string?
188  * 
189  * event handlers (listeners)
190  *   just shown 
191  * 
192  * -----------------
193  * special ID values
194  *  +XXXX -- indicates it's a instance property / not glob...
195  *  *XXXX -- skip writing glob property (used as classes that can be created...)
196  * 
197  * 
198  */
199         public void readProps()
200         {
201                 string left;
202                 Regex func_regex ;
203
204                 if (this.node.props.has_key("$ xns")) {
205          
206                         this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
207
208                 }
209
210                 
211                 try {
212                         func_regex = new Regex("^\\s+|\\s+$");
213                 } catch (Error e) {
214                         print("failed to build regex");
215                         return;
216                 }
217                 var piter = this.node.props.map_iterator();
218                 while (piter.next() ) {
219                         var kk = piter.get_key().split(" ");
220                         var v = piter.get_value();
221                         var k = kk[kk.length-1];
222
223                         
224                         //if (this.skip.contains(k) ) {
225                         //      continue;
226                         //}
227                         if (  Regex.match_simple("\\[\\]$", k)) {
228                                 
229                                 
230
231                         }
232                         
233                         string leftv = k;
234                         // skip builder stuff. prefixed with  '.' .. just like unix fs..
235                         if (kk[0][0] == '.') { // |. or . -- do not output..
236                                 continue;
237                         }
238                          if (kk[0][0] == '*') {
239                                 // ignore '*prop';
240                                 continue;
241                          }
242                                 
243                         
244                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
245                                 left = "'" + leftv + "'";
246                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
247                                 var val = this.node.quoteString(leftv);
248                                 
249                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
250                         } else {
251                                 left = leftv;
252                         }
253                         left += " : ";
254                         
255                         
256                          
257                         // next.. is it a function.. or a raw string..
258                         if (
259                                 kk[0][0] == '|' 
260                                 || 
261                                 kk[0][0] == '$' 
262                                 || 
263                                 kk[0] == "function"
264                                
265                                 // ??? any others that are raw output..
266                                 ) {
267                                 // does not hapepnd with arrays.. 
268                                 if (v.length < 1) {  //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
269                                         continue;
270                                 }
271                                 /*
272                                 print(v);
273                                 string str = "";
274                                 try {
275                                         str = func_regex.replace(v,v.length, 0, "");
276                                 } catch(Error e) {
277                                         print("regex failed");
278                                         return "";
279                                 }
280                                 */
281                                 var str = v.strip();
282                                   
283                                 var lines = str.split("\n");
284                                 var nstr = "" + str;
285                                 if (lines.length > 0) {
286                                         nstr =  string.joinv("\n" + this.pad, lines);
287                                 }
288                                 //print("==> " +  str + "\n");
289                                 this.els.add(left + nstr);
290                                 continue;
291                         }
292                         // standard..
293                         
294                         
295                         if (
296                                 Lang.isNumber(v) 
297                                 || 
298                                 Lang.isBoolean(v)
299                                 ||
300                                 kk[0].down() == "boolean"
301                                 || 
302                                 kk[0].down() == "bool"
303                                 || 
304                                 kk[0].down() == "number"
305                                 || 
306                                 kk[0].down() == "int"
307                             ) { // boolean or number...?
308                                 this.els.add(left + v.down() );
309                                 continue;
310                         }
311                         
312                         // strings..
313                         if (this.doubleStringProps.size < 1) {
314                                 this.els.add(left + this.node.quoteString(v));
315                                 continue;
316                         }
317                    
318                         if (this.doubleStringProps.index_of(k) > -1) {
319                                 els.add(left + this.node.quoteString(v));
320                                 continue;
321                         }
322                         var vv = this.node.quoteString(v);
323                         // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
324                         this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
325                         
326
327                    
328                    
329                    
330                 }
331         }
332         public void readArrayProps()
333         {
334         
335                 // handle the childitems  that are arrays.. eg. button[] = {  }...
336                 
337                 string left;
338                 
339                 var iter = this.ar_props.map_iterator();
340                 while (iter.next()) {
341                         var k = iter.get_key();
342                         var right = iter.get_value();
343                         
344                         string leftv = k[0] == '|' ? k.substring(1) : k;
345                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
346                                 left = "'" + leftv + "'";
347                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
348                                 var val = this.node.quoteString(leftv);
349                                 
350                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
351                         } else {
352                                 left = leftv;
353                         }
354                         left += " : ";
355                         
356                          
357                         
358                         
359                         if (right.length > 0){
360                                 this.els.add(left + "[\n" +  this.pad + "        " +  right + "\n" + this.pad + "]");
361                         }
362                 
363                         
364                 }
365
366         }
367         public void readListeners()
368         {
369                 
370                 if (this.node.listeners.size < 1) {
371                         return;
372                 }
373                         // munge the listeners.
374                         //print("ADDING listeners?");
375                 
376                 var liter = this.node.listeners.map_iterator();
377         
378                 var itms = "listeners : {\n";
379                 var i =0;
380                 while (liter.next()) {
381                         
382                         itms += i >0 ? ",\n" : "";      
383                         // 
384                         var str = liter.get_value().strip();
385                         var lines = str.split("\n");
386                         if (lines.length > 0) {
387                                 str = string.joinv("\n" + this.pad + "     ", lines);
388                         }
389                         
390
391                         
392                         itms +=  this.pad + indent_str  + liter.get_key().replace("|", "")  + " : " + str;
393
394                         i++;
395                 
396                         
397                 }
398                 itms += "\n" + this.pad + "}";
399                 //print ( "ADD " + itms); 
400                 this.els.add(itms);
401
402         }
403
404         public void iterChildren()
405         {
406                 
407                 
408                 // finally munge the children...
409                 if (this.node.items.size < 1) {
410                         return;
411                 }
412                 var itms = "items : [\n";
413                 var n = 0;
414                 for(var i = 0; i < this.node.items.size;i++) {
415                         var ele = this.node.items.get(i);
416                         if (ele.props.has_key("* prop")) {
417                                 continue;
418                         }
419                         if (n > 0) {
420                                  itms += ",\n";
421                         }
422                         n++;
423                         itms += this.pad + indent_str  +
424                                 this.mungeChild( this.pad + indent_str + indent_str ,  ele);
425                         
426                         
427                 }
428                 itms +=  "\n"+  this.pad + "]"  + "\n";
429                 this.els.add(itms);
430         }
431
432                 // finally output listeners...
433                 
434         public void xIncludeToString()
435         {
436                 
437
438         }
439
440 }
441         
442          
443         
444