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