3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
12 public class JsRender.NodeToJs : Object {
15 Gee.ArrayList<string> doubleStringProps;
17 Gee.ArrayList<string> els;
18 //Gee.ArrayList<string> skip;
19 Gee.HashMap<string,string> ar_props;
23 public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad)
26 this.doubleStringProps = doubleStringProps;
28 this.els = new Gee.ArrayList<string>();
29 //this.skip = new Gee.ArrayList<string>();
30 this.ar_props = new Gee.HashMap<string,string>();
34 public string munge ( )
36 //return this.mungeToString(this.node);
42 this.readArrayProps();
45 if (!this.node.props.has("* xinclude")) {
52 if (this.els.size < 1) {
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("* xinclude")) {
62 this.pad + str_props +
68 return "Roo.apply(" + this.node.props.get("* xinclude") + ", "+
70 this.pad + str_props +
75 string gLibStringListJoin( string sep, Gee.ArrayList<string> ar)
78 for (var i = 0; i < ar.size; i++) {
79 ret += i>0 ? sep : "";
85 public string mungeChild(string pad , Node cnode)
87 var x = new NodeToJs(cnode, this.doubleStringProps, pad);
94 public void checkChildren ()
98 // look throught he chilren == looking for * prop.. -- fixme might not work..
101 if (!this.node.hasChildren()) {
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")) {
113 //print(JSON.stringify(pl,null,4));
115 //var prop = pl['*prop'] + '';
116 //delete pl['*prop'];
117 var prop = pl.get("* prop");
118 print("got prop "+ prop + "\n");
121 if (! Regex.match_simple("\\[\\]$", prop)) {
122 // it's a standard prop..
124 // munge property..??
125 this.els.add( prop + " : " + this.mungeChild ( this.pad + " ", pl));
135 var sprop = prop.replace("[]", "");
136 print("sprop is : " + sprop + "\n");
138 // it's an array type..
140 if (!this.ar_props.has_key(sprop)) {
142 this.ar_props.set(sprop, "");
145 old = this.ar_props.get(sprop);
147 var nstr = old += old.length > 0 ? ",\n" : "";
148 nstr += this.mungeChild( this.pad + " ", pl);
150 this.ar_props.set(sprop, nstr);
157 * Standardize this crap...
159 * standard properties (use to set)
160 * If they are long values show the dialog..
163 * bool is_xxx :: can show a pulldown.. (true/false)
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..
171 * object properties (not part of the GOjbect being wrapped?
172 * # Gee.ArrayList<Xcls_fileitem> fileitems
177 * methods -- always text editor..
185 * * init -- big string?
187 * event handlers (listeners)
192 * +XXXX -- indicates it's a instance property / not glob...
193 * *XXXX -- skip writing glob property (used as classes that can be created...)
197 public void readProps()
202 func_regex = new Regex("^\\s+|\\s+$");
204 print("failed to build regex");
207 var piter = this.node.props.map_iterator();
208 while (piter.next() ) {
209 var kk = piter.get_key().split(" ");
210 var v = piter.get_value();
211 var k = kk[kk.length-1];
214 //if (this.skip.contains(k) ) {
217 if ( Regex.match_simple("\\[\\]$", k)) {
224 // skip builder stuff. prefixed with '.' .. just like unix fs..
225 if (kk[0][0] == '.') { // |. or . -- do not output..
228 if (kk[0][0] == '*') {
234 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
235 left = "'" + leftv + "'";
236 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
237 var val = this.node.quoteString(leftv);
239 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
247 // next.. is it a function.. or a raw string..
255 // ??? any others that are raw output..
257 // does not hapepnd with arrays..
258 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
265 str = func_regex.replace(v,v.length, 0, "");
267 print("regex failed");
273 var lines = str.split("\n");
275 if (lines.length > 0) {
276 nstr = string.joinv("\n" + this.pad, lines);
278 //print("==> " + str + "\n");
279 this.els.add(left + nstr);
290 kk[0].down() == "boolean"
292 kk[0].down() == "bool"
294 kk[0].down() == "number"
296 kk[0].down() == "int"
297 ) { // boolean or number...?
298 this.els.add(left + v.down() );
303 if (this.doubleStringProps.size < 1) {
304 this.els.add(left + this.node.quoteString(v));
308 if (this.doubleStringProps.index_of(k) > -1) {
309 els.add(left + this.node.quoteString(v));
312 var vv = this.node.quoteString(v);
313 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
314 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
322 public void readArrayProps()
325 // handle the childitems that are arrays.. eg. button[] = { }...
329 var iter = this.ar_props.map_iterator();
330 while (iter.next()) {
331 var k = iter.get_key();
332 var right = iter.get_value();
334 string leftv = k[0] == '|' ? k.substring(1) : k;
335 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
336 left = "'" + leftv + "'";
337 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
338 var val = this.node.quoteString(leftv);
340 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
349 if (right.length > 0){
350 this.els.add(left + "[\n" + this.pad + " " + right + "\n" + this.pad + "]");
357 public void readListeners()
360 if (this.node.listeners.size < 1) {
363 // munge the listeners.
364 //print("ADDING listeners?");
366 var liter = this.node.listeners.map_iterator();
368 var itms = "listeners : {\n";
370 while (liter.next()) {
372 itms += i >0 ? ",\n" : "";
374 var str = liter.get_value().strip();
375 var lines = str.split("\n");
376 if (lines.length > 0) {
377 str = string.joinv("\n" + this.pad + " ", lines);
382 itms += this.pad + " " + liter.get_key().replace("|", "") + " : " + str;
388 itms += "\n" + this.pad + "}";
389 //print ( "ADD " + itms);
394 public void iterChildren()
398 // finally munge the children...
399 if (this.node.items.size < 1) {
402 var itms = "items : [\n";
404 for(var i = 0; i < this.node.items.size;i++) {
405 var ele = this.node.items.get(i);
406 if (ele.props.has_key("* prop")) {
413 itms += this.pad + " " +
414 this.mungeChild( this.pad + " ", ele);
418 itms += "\n"+ this.pad + "]" + "\n";
422 // finally output listeners...
424 public void xIncludeToString()