3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
12 public class JsRender.NodeToJs : Object {
14 static uint indent = 1;
15 static string indent_str = " ";
17 Gee.ArrayList<string> doubleStringProps;
19 Gee.ArrayList<string> els;
20 //Gee.ArrayList<string> skip;
21 Gee.HashMap<string,string> ar_props;
25 public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad)
28 this.doubleStringProps = doubleStringProps;
30 this.els = new Gee.ArrayList<string>();
31 //this.skip = new Gee.ArrayList<string>();
32 this.ar_props = new Gee.HashMap<string,string>();
36 public string munge ( )
38 //return this.mungeToString(this.node);
44 this.readArrayProps();
47 if (!this.node.props.has_key("* xinclude")) {
54 if (this.els.size < 1) {
59 var spad = pad.substring(0, this.pad.length-indent);
60 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
61 //print ("STR PROPS: " + str_props);
62 if (!this.node.props.has_key("* xinclude")) {
64 this.pad + str_props +
70 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
72 this.pad + str_props +
77 string gLibStringListJoin( string sep, Gee.ArrayList<string> ar)
80 for (var i = 0; i < ar.size; i++) {
81 ret += i>0 ? sep : "";
87 public string mungeChild(string pad , Node cnode)
89 var x = new NodeToJs(cnode, this.doubleStringProps, pad);
96 public void checkChildren ()
100 // look throught he chilren == looking for * prop.. -- fixme might not work..
103 if (!this.node.hasChildren()) {
108 for (var ii =0; ii< this.node.items.size; ii++) {
109 var pl = this.node.items.get(ii);
110 if (!pl.props.has_key("* prop")) {
115 //print(JSON.stringify(pl,null,4));
117 //var prop = pl['*prop'] + '';
118 //delete pl['*prop'];
119 var prop = pl.get("* prop");
120 print("got prop "+ prop + "\n");
123 if (! Regex.match_simple("\\[\\]$", prop)) {
124 // it's a standard prop..
126 // munge property..??
127 this.els.add( prop + " : " + this.mungeChild ( this.pad + indent_str, pl));
137 var sprop = prop.replace("[]", "");
138 print("sprop is : " + sprop + "\n");
140 // it's an array type..
142 if (!this.ar_props.has_key(sprop)) {
144 this.ar_props.set(sprop, "");
147 old = this.ar_props.get(sprop);
149 var nstr = old += old.length > 0 ? ",\n" : "";
150 nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str , pl);
152 this.ar_props.set(sprop, nstr);
159 * Standardize this crap...
161 * standard properties (use to set)
162 * If they are long values show the dialog..
165 * bool is_xxx :: can show a pulldown.. (true/false)
167 * $ string html = string with value interpolated eg. baseURL + ".."
168 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
169 * $ untypedvalue = javascript untyped value...
170 * _ string html ... = translatable..
173 * object properties (not part of the GOjbect being wrapped?
174 * # Gee.ArrayList<Xcls_fileitem> fileitems
179 * methods -- always text editor..
187 * * init -- big string?
189 * event handlers (listeners)
194 * +XXXX -- indicates it's a instance property / not glob...
195 * *XXXX -- skip writing glob property (used as classes that can be created...)
199 public void readProps()
204 if (this.node.props.has_key("$ xns")) {
206 this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
212 func_regex = new Regex("^\\s+|\\s+$");
214 print("failed to build regex");
217 var piter = this.node.props.map_iterator();
218 while (piter.next() ) {
219 var kk = piter.get_key().split(" ");
220 var v = piter.get_value();
221 var k = kk[kk.length-1];
224 //if (this.skip.contains(k) ) {
227 if ( Regex.match_simple("\\[\\]$", k)) {
234 // skip builder stuff. prefixed with '.' .. just like unix fs..
235 if (kk[0][0] == '.') { // |. or . -- do not output..
238 if (kk[0][0] == '*') {
244 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
245 left = "'" + leftv + "'";
246 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
247 var val = this.node.quoteString(leftv);
249 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
257 // next.. is it a function.. or a raw string..
265 // ??? any others that are raw output..
267 // does not hapepnd with arrays..
268 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
275 str = func_regex.replace(v,v.length, 0, "");
277 print("regex failed");
283 var lines = str.split("\n");
285 if (lines.length > 0) {
286 nstr = string.joinv("\n" + this.pad, lines);
288 //print("==> " + str + "\n");
289 this.els.add(left + nstr);
300 kk[0].down() == "boolean"
302 kk[0].down() == "bool"
304 kk[0].down() == "number"
306 kk[0].down() == "int"
307 ) { // boolean or number...?
308 this.els.add(left + v.down() );
313 if (this.doubleStringProps.size < 1) {
314 this.els.add(left + this.node.quoteString(v));
318 if (this.doubleStringProps.index_of(k) > -1) {
319 els.add(left + this.node.quoteString(v));
322 var vv = this.node.quoteString(v);
323 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
324 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
332 public void readArrayProps()
335 // handle the childitems that are arrays.. eg. button[] = { }...
339 var iter = this.ar_props.map_iterator();
340 while (iter.next()) {
341 var k = iter.get_key();
342 var right = iter.get_value();
344 string leftv = k[0] == '|' ? k.substring(1) : k;
345 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
346 left = "'" + leftv + "'";
347 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
348 var val = this.node.quoteString(leftv);
350 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
359 if (right.length > 0){
360 this.els.add(left + "[\n" + this.pad + " " + right + "\n" + this.pad + "]");
367 public void readListeners()
370 if (this.node.listeners.size < 1) {
373 // munge the listeners.
374 //print("ADDING listeners?");
376 var liter = this.node.listeners.map_iterator();
378 var itms = "listeners : {\n";
380 while (liter.next()) {
382 itms += i >0 ? ",\n" : "";
384 var str = liter.get_value().strip();
385 var lines = str.split("\n");
386 if (lines.length > 0) {
387 str = string.joinv("\n" + this.pad + " ", lines);
392 itms += this.pad + indent_str + liter.get_key().replace("|", "") + " : " + str;
398 itms += "\n" + this.pad + "}";
399 //print ( "ADD " + itms);
404 public void iterChildren()
408 // finally munge the children...
409 if (this.node.items.size < 1) {
412 var itms = "items : [\n";
414 for(var i = 0; i < this.node.items.size;i++) {
415 var ele = this.node.items.get(i);
416 if (ele.props.has_key("* prop")) {
423 itms += this.pad + indent_str +
424 this.mungeChild( this.pad + indent_str + indent_str , ele);
428 itms += "\n"+ this.pad + "]" + "\n";
432 // finally output listeners...
434 public void xIncludeToString()