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;
18 public NodeToJs(JsRender.Node node, Gee.ArrayList<string> doubleStringProps)
21 this.doubleStringProps = doubleStringProps;
25 public string munge ( )
27 return this.mungeToString(this.node);
33 public string buildEls ( JsRender cnode)
37 pad = pad.length < 1 ? " " : pad;
42 var els = new Gee.ArrayList<string>();
43 var skip = new Gee.ArrayList<string>();
44 if (this.hasXnsType() ) {
45 // this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els); ??????
52 //var newitems = new Gee.ArrayList<JsRender.Node>();
56 // look throught he chilren == looking for * prop.. -- fixme might not work..
58 var ar_props = new Gee.HashMap<string,string>();
61 if (this.hasChildren()) {
64 for (var ii =0; ii< this.items.size; ii++) {
65 var pl = this.items.get(ii);
66 if (!pl.props.has_key("* prop")) {
71 //print(JSON.stringify(pl,null,4));
73 //var prop = pl['*prop'] + '';
75 var prop = pl.get("* prop");
76 print("got prop "+ prop + "\n");
79 if (! Regex.match_simple("\\[\\]$", prop)) {
80 // it's a standard prop..
83 els.add( prop + " : " + pl.mungeToString ( pad + " ", doubleStringProps));
93 var sprop = prop.replace("[]", "");
94 print("sprop is : " + sprop + "\n");
96 // it's an array type..
98 if (!ar_props.has_key(sprop)) {
100 ar_props.set(sprop, "");
103 old = ar_props.get(sprop);
105 var nstr = old += old.length > 0 ? ",\n" : "";
106 nstr += pl.mungeToString ( pad + " ", doubleStringProps);
108 ar_props.set(sprop, nstr);
119 func_regex = new Regex("^\\s+|\\s+$");
121 print("failed to build regex");
124 var piter = this.props.map_iterator();
125 while (piter.next() ) {
126 var k = piter.get_key();
127 var v = piter.get_value();
129 if (skip.contains(k) ) {
132 if ( Regex.match_simple("\\[\\]$", k)) {
138 string leftv = k[0] == '$' ? k.substring(2) : k;
139 // skip builder stuff. prefixed with '.' .. just like unix fs..
140 if (leftv[0] == '.') { // |. or . -- do not output..
149 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
150 left = "'" + leftv + "'";
151 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
152 var val = this.quoteString(leftv);
154 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
162 // next.. is it a function..
164 // does not hapepnd with arrays..
165 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
172 str = func_regex.replace(v,v.length, 0, "");
174 print("regex failed");
180 var lines = str.split("\n");
182 if (lines.length > 0) {
183 nstr = string.joinv("\n" + pad, lines);
185 //print("==> " + str + "\n");
186 els.add(left + nstr);
192 if (Lang.isNumber(v) || Lang.isBoolean(v)) { // boolean or number...?
193 els.add(left + v.down() );
198 if (doubleStringProps.size < 1) {
199 els.add(left + this.quoteString(v));
203 if (doubleStringProps.index_of(k) > -1) {
204 els.add(left + this.quoteString(v));
207 var vv = this.quoteString(v);
208 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
209 els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
216 // handle the childitems that are arrays.. eg. button[] = { }...
219 var iter = ar_props.map_iterator();
220 while (iter.next()) {
221 var k = iter.get_key();
222 var right = iter.get_value();
224 string leftv = k[0] == '|' ? k.substring(1) : k;
225 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
226 left = "'" + leftv + "'";
227 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
228 var val = this.quoteString(leftv);
230 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
239 if (right.length > 0){
240 els.add(left + "[\n" + pad + " " + right + "\n" + pad + "]");
247 if (this.listeners.size > 0) {
248 // munge the listeners.
249 //print("ADDING listeners?");
251 var liter = this.listeners.map_iterator();
253 var itms = "listeners : {\n";
255 while (liter.next()) {
257 itms += i >0 ? ",\n" : "";
259 var str = liter.get_value().strip();
260 var lines = str.split("\n");
261 if (lines.length > 0) {
262 str = string.joinv("\n" + pad + " ", lines);
267 itms += pad + " " + liter.get_key().replace("|", "") + " : " + str;
273 itms += "\n" + pad + "}";
274 //print ( "ADD " + itms);
281 // finally munge the children...
282 if (this.items.size> 0) {
283 var itms = "items : [\n";
285 for(var i = 0; i < this.items.size;i++) {
287 if (this.items.get(i).props.has_key("* prop")) {
295 this.items.get(i).mungeToString( pad + " ", doubleStringProps);
299 itms += "\n"+ pad + "]" + "\n";
303 // finally output listeners...
316 var spad = pad.substring(0, pad.length-4);
317 var str_props = gLibStringListJoin(",\n" + pad , els) ;
318 //print ("STR PROPS: " + str_props);