sync
[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                 // sort the key's so they always get rendered in the same order..
218                 
219                 var keys = new Gee.ArrayList<string>();
220                 var piter = this.node.props.map_iterator();
221                 while (piter.next() ) {
222                         string k;
223                         string ktype;
224                         string kflag;
225                         this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
226                         
227                         keys.add(k);
228                 }
229                 keys.sort((  a,  b) => {
230                         return ((string)a).collate((string)b);
231                         //if (a == b) return 0;
232                         //return a < b ? -1 : 1;
233                 });
234                 for (var i = 0; i< keys.size; i++) {
235                         var key = this.node.get_key(keys.get(i));
236                         print("ADD KEY %s\n", key);
237                         string k;
238                         string ktype;
239                         string kflag;
240                         
241                         this.node.normalize_key(key, out k, out kflag, out ktype);
242                         
243                         
244                         var v = this.node.get(key);
245                          
246                         
247                         //if (this.skip.contains(k) ) {
248                         //      continue;
249                         //}
250                         if (  Regex.match_simple("\\[\\]$", k)) {
251                                 // array .. not supported... here?
252                                 
253
254                         }
255                         
256                         string leftv = k;
257                         // skip builder stuff. prefixed with  '.' .. just like unix fs..
258                         if (kflag == ".") { // |. or . -- do not output..
259                                 continue;
260                         }
261                          if (kflag == "*") {
262                                 // ignore '* prop'; ??? 
263                                 continue;
264                          }
265                                 
266                         
267                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
268                                 left = "'" + leftv + "'";
269                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
270                                 var val = this.node.quoteString(leftv);
271                                 
272                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
273                         } else {
274                                 left = leftv;
275                         }
276                         left += " : ";
277                         
278                         
279                          
280                         // next.. is it a function.. or a raw string..
281                         if (
282                                 kflag == "|" 
283                                 || 
284                                 kflag == "$" 
285                                 || 
286                                 ktype == "function"
287                                
288                                 // ??? any others that are raw output..
289                                 ) {
290                                 // does not hapepnd with arrays.. 
291                                 if (v.length < 1) {  //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
292                                         continue;
293                                 }
294                                 /*
295                                 print(v);
296                                 string str = "";
297                                 try {
298                                         str = func_regex.replace(v,v.length, 0, "");
299                                 } catch(Error e) {
300                                         print("regex failed");
301                                         return "";
302                                 }
303                                 */
304                                 var str = v.strip();
305                                   
306                                 var lines = str.split("\n");
307                                 var nstr = "" + str;
308                                 if (lines.length > 0) {
309                                         nstr =  string.joinv("\n" + this.pad, lines);
310                                         //nstr =  string.joinv("\n", lines);
311                                 }
312                                 //print("==> " +  str + "\n");
313                                 this.els.add(left + nstr);
314                                 continue;
315                         }
316                         // standard..
317                         
318                         
319                         if (
320                                 Lang.isNumber(v) 
321                                 || 
322                                 Lang.isBoolean(v)
323                                 ||
324                                 ktype.down() == "boolean"
325                                 || 
326                                 ktype.down() == "bool"
327                                 || 
328                                 ktype.down() == "number"
329                                 || 
330                                 ktype.down() == "int"
331                             ) { // boolean or number...?
332                                 this.els.add(left + v.down() );
333                                 continue;
334                         }
335                         
336                         // is it a translated string?
337                         
338                         
339                         
340                         
341                         // strings..
342                         //if (this.doubleStringProps.size < 1) {
343                         //      this.els.add(left + this.node.quoteString(v));
344                         //      continue;
345                         //}
346                    
347                         if (this.doubleStringProps.index_of(k) > -1) {
348                                 // then use the translated version...
349                                 
350                                 els.add(left + "_this._strings['" + 
351                                         GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
352                                         "']"
353                                 );
354                                 continue;
355                         }
356                         if (ktype.down() == "string" && k[0] == '_') {
357                                 els.add(left + "_this._strings['" + 
358                                         GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
359                                         "']"
360                                 );
361                                 continue;
362                         }
363                         // otherwise it needs to be encapsulated.. as single quotes..
364                         
365                         var vv = this.node.quoteString(v);
366                         // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
367                         this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
368                         
369
370                    
371                    
372                    
373                 }
374         }
375         public void readArrayProps()
376         {
377         
378                 // handle the childitems  that are arrays.. eg. button[] = {  }...
379                 
380                 string left;
381                 
382                 var iter = this.ar_props.map_iterator();
383                 while (iter.next()) {
384                         var k = iter.get_key();
385                         var right = iter.get_value();
386                         
387                         string leftv = k[0] == '|' ? k.substring(1) : k;
388                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
389                                 left = "'" + leftv + "'";
390                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
391                                 var val = this.node.quoteString(leftv);
392                                 
393                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
394                         } else {
395                                 left = leftv;
396                         }
397                         left += " : ";
398                         
399                          
400                         
401                         
402                         if (right.length > 0){
403                                 this.els.add(left + "[\n" +  this.pad + indent_str + indent_str +  
404                                              right + "\n" + this.pad + "]");
405                         }
406                 
407                         
408                 }
409
410         }
411         public void readListeners()
412         {
413                 
414                 if (this.node.listeners.size < 1) {
415                         return;
416                 }
417                         // munge the listeners.
418                         //print("ADDING listeners?");
419                 
420                 var liter = this.node.listeners.map_iterator();
421         
422         
423         
424                 var keys = new Gee.ArrayList<string>();
425                 var piter = this.node.listeners.map_iterator();
426                 while (piter.next() ) {
427                          
428                         keys.add(piter.get_key());
429                 }
430                 keys.sort((  a,  b) => {
431                         return ((string)a).collate((string)b);
432                         //if (a == b) return 0;
433                         //return a < b ? -1 : 1;
434                 });
435         
436                 var itms = "listeners : {\n";
437                 
438                 for (var i = 0; i< keys.size; i++) {
439                         var key = keys.get(i);
440                         var val = this.node.listeners.get(key);
441                 
442         
443                         itms += i >0 ? ",\n" : "";      
444                         // 
445                         var str = val.strip();
446                         var lines = str.split("\n");
447                         if (lines.length > 0) {
448                                 //str = string.joinv("\n" + this.pad + "           ", lines);
449                                 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
450                         }
451                         
452                         itms +=  this.pad + indent_str  + key.replace("|", "")  + " : " + str;
453
454                 
455                         
456                 }
457                 itms += "\n" + this.pad + "}";
458                 //print ( "ADD " + itms); 
459                 this.els.add(itms);
460
461         }
462
463         public void iterChildren()
464         {
465                 
466                 
467                 // finally munge the children...
468                 if (this.node.items.size < 1) {
469                         return;
470                 }
471                 var itms = "items : [\n";
472                 var n = 0;
473                 for(var i = 0; i < this.node.items.size;i++) {
474                         var ele = this.node.items.get(i);
475                         if (ele.props.has_key("* prop")) {
476                                 continue;
477                         }
478                         if (n > 0) {
479                                  itms += ",\n";
480                         }
481                         n++;
482                         itms += this.pad + indent_str  +
483                                 this.mungeChild( this.pad + indent_str + indent_str ,  ele);
484                         
485                         
486                 }
487                 itms +=  "\n"+  this.pad + "]"  + "\n";
488                 this.els.add(itms);
489         }
490
491                 // finally output listeners...
492                 
493         public void xIncludeToString()
494         {
495                 
496
497         }
498
499 }
500         
501          
502         
503