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);
40 this.readArrayProps();
44 if (this.els.size < 1) {
49 var spad = pad.substring(0, this.pad.length-4);
50 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
51 //print ("STR PROPS: " + str_props);
59 string gLibStringListJoin( string sep, Gee.ArrayList<string> ar)
62 for (var i = 0; i < ar.size; i++) {
63 ret += i>0 ? sep : "";
69 public string mungeChild(string pad , Node cnode)
71 var x = new NodeToJs(cnode, this.doubleStringProps, pad);
78 public void checkChildren ()
82 // look throught he chilren == looking for * prop.. -- fixme might not work..
85 if (!this.node.hasChildren()) {
90 for (var ii =0; ii< this.node.items.size; ii++) {
91 var pl = this.node.items.get(ii);
92 if (!pl.props.has_key("* prop")) {
97 //print(JSON.stringify(pl,null,4));
99 //var prop = pl['*prop'] + '';
100 //delete pl['*prop'];
101 var prop = pl.get("* prop");
102 print("got prop "+ prop + "\n");
105 if (! Regex.match_simple("\\[\\]$", prop)) {
106 // it's a standard prop..
108 // munge property..??
109 this.els.add( prop + " : " + this.mungeChild ( this.pad + " ", pl));
119 var sprop = prop.replace("[]", "");
120 print("sprop is : " + sprop + "\n");
122 // it's an array type..
124 if (!this.ar_props.has_key(sprop)) {
126 this.ar_props.set(sprop, "");
129 old = this.ar_props.get(sprop);
131 var nstr = old += old.length > 0 ? ",\n" : "";
132 nstr += this.mungeChild( this.pad + " ", pl);
134 this.ar_props.set(sprop, nstr);
141 * Standardize this crap...
143 * standard properties (use to set)
144 * If they are long values show the dialog..
146 * bool is_xxx :: can show a pulldown.. (true/false)
148 * $ string html = string with value interpolated eg. baseURL + ".."
149 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
150 * $ untypedvalue = javascript untyped value...
151 * _ string html ... = translatable..
153 * object properties (not part of the GOjbect being wrapped?
154 * # Gee.ArrayList<Xcls_fileitem> fileitems
159 * methods -- always text editor..
167 * * init -- big string?
169 * event handlers (listeners)
174 * +XXXX -- indicates it's a instance property / not glob...
175 * *XXXX -- skip writing glob property (used as classes that can be created...)
179 public void readProps()
184 func_regex = new Regex("^\\s+|\\s+$");
186 print("failed to build regex");
189 var piter = this.node.props.map_iterator();
190 while (piter.next() ) {
191 var kk = piter.get_key().split(" ");
192 var v = piter.get_value();
193 var k = kk[kk.length-1];
196 //if (this.skip.contains(k) ) {
199 if ( Regex.match_simple("\\[\\]$", k)) {
206 // skip builder stuff. prefixed with '.' .. just like unix fs..
207 if (kk[0][0] == '.') { // |. or . -- do not output..
210 if (kk[0][0] == '*') {
216 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
217 left = "'" + leftv + "'";
218 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
219 var val = this.node.quoteString(leftv);
221 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
229 // next.. is it a function.. or a raw string..
237 // ??? any others that are raw output..
239 // does not hapepnd with arrays..
240 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
247 str = func_regex.replace(v,v.length, 0, "");
249 print("regex failed");
255 var lines = str.split("\n");
257 if (lines.length > 0) {
258 nstr = string.joinv("\n" + this.pad, lines);
260 //print("==> " + str + "\n");
261 this.els.add(left + nstr);
272 kk[0].down() == "boolean"
274 kk[0].down() == "bool"
276 kk[0].down() == "number"
278 kk[0].down() == "int"
279 ) { // boolean or number...?
280 this.els.add(left + v.down() );
285 if (this.doubleStringProps.size < 1) {
286 this.els.add(left + this.node.quoteString(v));
290 if (this.doubleStringProps.index_of(k) > -1) {
291 els.add(left + this.node.quoteString(v));
294 var vv = this.node.quoteString(v);
295 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
296 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
304 public void readArrayProps()
307 // handle the childitems that are arrays.. eg. button[] = { }...
311 var iter = this.ar_props.map_iterator();
312 while (iter.next()) {
313 var k = iter.get_key();
314 var right = iter.get_value();
316 string leftv = k[0] == '|' ? k.substring(1) : k;
317 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
318 left = "'" + leftv + "'";
319 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
320 var val = this.node.quoteString(leftv);
322 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
331 if (right.length > 0){
332 this.els.add(left + "[\n" + this.pad + " " + right + "\n" + this.pad + "]");
339 public void readListeners()
342 if (this.node.listeners.size < 1) {
345 // munge the listeners.
346 //print("ADDING listeners?");
348 var liter = this.node.listeners.map_iterator();
350 var itms = "listeners : {\n";
352 while (liter.next()) {
354 itms += i >0 ? ",\n" : "";
356 var str = liter.get_value().strip();
357 var lines = str.split("\n");
358 if (lines.length > 0) {
359 str = string.joinv("\n" + this.pad + " ", lines);
364 itms += this.pad + " " + liter.get_key().replace("|", "") + " : " + str;
370 itms += "\n" + this.pad + "}";
371 //print ( "ADD " + itms);
376 public void iterChildren()
380 // finally munge the children...
381 if (this.node.items.size < 0) {
384 var itms = "items : [\n";
386 for(var i = 0; i < this.node.items.size;i++) {
387 var ele = this.node.items.get(i);
388 if (ele.props.has_key("* prop")) {
395 itms += this.pad + " " +
396 this.mungeChild( this.pad + " ", ele);
400 itms += "\n"+ this.pad + "]" + "\n";
404 // finally output listeners...