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_key("* 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_key("* xinclude")) {
62 this.pad + str_props +
68 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
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 if (this.node.props.has_key("$ xns")) {
204 this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
210 func_regex = new Regex("^\\s+|\\s+$");
212 print("failed to build regex");
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];
222 //if (this.skip.contains(k) ) {
225 if ( Regex.match_simple("\\[\\]$", k)) {
232 // skip builder stuff. prefixed with '.' .. just like unix fs..
233 if (kk[0][0] == '.') { // |. or . -- do not output..
236 if (kk[0][0] == '*') {
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);
247 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
255 // next.. is it a function.. or a raw string..
263 // ??? any others that are raw output..
265 // does not hapepnd with arrays..
266 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
273 str = func_regex.replace(v,v.length, 0, "");
275 print("regex failed");
281 var lines = str.split("\n");
283 if (lines.length > 0) {
284 nstr = string.joinv("\n" + this.pad, lines);
286 //print("==> " + str + "\n");
287 this.els.add(left + nstr);
298 kk[0].down() == "boolean"
300 kk[0].down() == "bool"
302 kk[0].down() == "number"
304 kk[0].down() == "int"
305 ) { // boolean or number...?
306 this.els.add(left + v.down() );
311 if (this.doubleStringProps.size < 1) {
312 this.els.add(left + this.node.quoteString(v));
316 if (this.doubleStringProps.index_of(k) > -1) {
317 els.add(left + this.node.quoteString(v));
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("'", "\\'") + "'");
330 public void readArrayProps()
333 // handle the childitems that are arrays.. eg. button[] = { }...
337 var iter = this.ar_props.map_iterator();
338 while (iter.next()) {
339 var k = iter.get_key();
340 var right = iter.get_value();
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);
348 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
357 if (right.length > 0){
358 this.els.add(left + "[\n" + this.pad + " " + right + "\n" + this.pad + "]");
365 public void readListeners()
368 if (this.node.listeners.size < 1) {
371 // munge the listeners.
372 //print("ADDING listeners?");
374 var liter = this.node.listeners.map_iterator();
376 var itms = "listeners : {\n";
378 while (liter.next()) {
380 itms += i >0 ? ",\n" : "";
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);
390 itms += this.pad + " " + liter.get_key().replace("|", "") + " : " + str;
396 itms += "\n" + this.pad + "}";
397 //print ( "ADD " + itms);
402 public void iterChildren()
406 // finally munge the children...
407 if (this.node.items.size < 1) {
410 var itms = "items : [\n";
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")) {
421 itms += this.pad + " " +
422 this.mungeChild( this.pad + " ", ele);
426 itms += "\n"+ this.pad + "]" + "\n";
430 // finally output listeners...
432 public void xIncludeToString()