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 = " ";
18 Gee.ArrayList<string> doubleStringProps; // need to think if this is a good idea like this
21 Gee.ArrayList<string> els;
22 //Gee.ArrayList<string> skip;
23 Gee.HashMap<string,string> ar_props;
25 Gee.HashMap<string,string> out_props;
26 Gee.HashMap<string,string> out_listeners;
27 Gee.HashMap<string,Node> out_nodeprops;
28 Gee.ArrayList<Node> out_children;
37 public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad, NodeToJs? parent)
40 this.doubleStringProps = doubleStringProps;
43 this.els = new Gee.ArrayList<string>();
44 this.ar_props = new Gee.HashMap<string,string>();
47 this.out_props = new Gee.HashMap<string,string>();
48 this.out_listeners = new Gee.HashMap<string,string>();
49 this.out_nodeprops = new Gee.HashMap<string,Node>() ;
50 this.out_children = new Gee.ArrayList<Node> ();
56 this.cur_line = parent == null ? 0 : parent.cur_line;
58 this.top = parent == null ? this : parent.top;
59 this.line_buffer = "";
66 public void addLine(string str= "")
68 if (this.line_buffer.length > 0) {
70 this.ret += this.line_buffer+ ",\n";
72 this.line_buffer = str;
76 public void addMultiLine(string str= "")
79 this.cur_line += str.split("\n").length;
80 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
81 this.ret += str + "\n";
86 This currently works by creating a key/value array of this.els, which is just an array of properties..
87 this is so that join() works...
91 b) output plan properties.
104 public string munge ( )
106 //return this.mungeToString(this.node);
108 this.node.line_start = this.cur_line;
110 this.checkChildren();
112 this.readArrayProps();
113 this.readListeners();
115 if (!this.node.props.has_key("* xinclude")) {
122 if (this.els.size < 1) {
127 var spad = pad.substring(0, this.pad.length-indent);
130 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
131 //print ("STR PROPS: " + str_props);
135 if (!this.node.props.has_key("* xinclude")) {
137 this.pad + str_props +
143 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
145 this.pad + str_props +
150 string gLibStringListJoin( string sep, Gee.ArrayList<string> ar)
153 for (var i = 0; i < ar.size; i++) {
154 ret += i>0 ? sep : "";
160 public string mungeChild(string pad , Node cnode)
162 var x = new NodeToJs(cnode, this.doubleStringProps, pad, this);
169 public void checkChildren ()
173 // look throught he chilren == looking for * prop.. -- fixme might not work..
176 if (!this.node.hasChildren()) {
181 for (var ii =0; ii< this.node.items.size; ii++) {
182 var pl = this.node.items.get(ii);
183 if (!pl.props.has_key("* prop")) {
188 //print(JSON.stringify(pl,null,4));
190 //var prop = pl['*prop'] + '';
191 //delete pl['*prop'];
192 var prop = pl.get("* prop");
193 print("got prop "+ prop + "\n");
196 if (! Regex.match_simple("\\[\\]$", prop)) {
197 // it's a standard prop..
199 // munge property..??
201 this.out_nodeprops.set(prop, pl);
203 this.els.add( prop + " : " + this.mungeChild ( this.pad + indent_str, pl));
213 var sprop = prop.replace("[]", "");
214 print("sprop is : " + sprop + "\n");
216 // it's an array type..
218 if (!this.ar_props.has_key(sprop)) {
220 this.ar_props.set(sprop, "");
223 old = this.ar_props.get(sprop);
225 var nstr = old += old.length > 0 ? ",\n" : "";
226 nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str , pl);
228 this.ar_props.set(sprop, nstr);
235 * Standardize this crap...
237 * standard properties (use to set)
238 * If they are long values show the dialog..
241 * bool is_xxx :: can show a pulldown.. (true/false)
243 * $ string html = string with value interpolated eg. baseURL + ".."
244 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
245 * $ untypedvalue = javascript untyped value...
246 * _ string html ... = translatable..
249 * object properties (not part of the GOjbect being wrapped?
250 * # Gee.ArrayList<Xcls_fileitem> fileitems
255 * methods -- always text editor..
263 * * init -- big string?
265 * event handlers (listeners)
270 * +XXXX -- indicates it's a instance property / not glob...
271 * *XXXX -- skip writing glob property (used as classes that can be created...)
275 public void readProps()
280 if (this.node.props.has_key("$ xns")) {
281 this.out_props("'|xns'", this.node.props.get("$ xns") );
283 this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
289 func_regex = new Regex("^\\s+|\\s+$");
291 print("failed to build regex");
294 // sort the key's so they always get rendered in the same order..
296 var keys = new Gee.ArrayList<string>();
297 var piter = this.node.props.map_iterator();
298 while (piter.next() ) {
302 this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
307 keys.sort(( a, b) => {
308 return ((string)a).collate((string)b);
309 //if (a == b) return 0;
310 //return a < b ? -1 : 1;
315 for (var i = 0; i< keys.size; i++) {
316 var key = this.node.get_key(keys.get(i));
317 print("ADD KEY %s\n", key);
322 this.node.normalize_key(key, out k, out kflag, out ktype);
325 var v = this.node.get(key);
328 //if (this.skip.contains(k) ) {
331 if ( Regex.match_simple("\\[\\]$", k)) {
332 // array .. not supported... here?
338 // skip builder stuff. prefixed with '.' .. just like unix fs..
339 if (kflag == ".") { // |. or . -- do not output..
343 // ignore '* prop'; ???
348 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
349 left = "'" + leftv + "'";
350 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
351 var val = this.node.quoteString(leftv);
353 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
361 // next.. is it a function.. or a raw string..
369 // ??? any others that are raw output..
371 // does not hapepnd with arrays..
372 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
379 str = func_regex.replace(v,v.length, 0, "");
381 print("regex failed");
387 var lines = str.split("\n");
389 if (lines.length > 0) {
390 nstr = string.joinv("\n" + this.pad, lines);
391 //nstr = string.joinv("\n", lines);
393 //print("==> " + str + "\n");
394 this.els.add(left + nstr);
405 ktype.down() == "boolean"
407 ktype.down() == "bool"
409 ktype.down() == "number"
411 ktype.down() == "int"
412 ) { // boolean or number...?
413 this.els.add(left + v.down() );
417 // is it a translated string?
423 //if (this.doubleStringProps.size < 1) {
424 // this.els.add(left + this.node.quoteString(v));
428 if (this.doubleStringProps.index_of(k) > -1) {
429 // then use the translated version...
431 els.add(left + "_this._strings['" +
432 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
437 if (ktype.down() == "string" && k[0] == '_') {
438 els.add(left + "_this._strings['" +
439 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
444 // otherwise it needs to be encapsulated.. as single quotes..
446 var vv = this.node.quoteString(v);
447 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
448 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
456 public void readArrayProps()
459 // handle the childitems that are arrays.. eg. button[] = { }...
463 var iter = this.ar_props.map_iterator();
464 while (iter.next()) {
465 var k = iter.get_key();
466 var right = iter.get_value();
468 string leftv = k[0] == '|' ? k.substring(1) : k;
469 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
470 left = "'" + leftv + "'";
471 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
472 var val = this.node.quoteString(leftv);
474 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
483 if (right.length > 0){
484 this.els.add(left + "[\n" + this.pad + indent_str + indent_str +
485 right + "\n" + this.pad + "]");
492 public void readListeners()
495 if (this.node.listeners.size < 1) {
498 // munge the listeners.
499 //print("ADDING listeners?");
501 var liter = this.node.listeners.map_iterator();
505 var keys = new Gee.ArrayList<string>();
506 var piter = this.node.listeners.map_iterator();
507 while (piter.next() ) {
509 keys.add(piter.get_key());
511 keys.sort(( a, b) => {
512 return ((string)a).collate((string)b);
513 //if (a == b) return 0;
514 //return a < b ? -1 : 1;
517 var itms = "listeners : {\n";
519 for (var i = 0; i< keys.size; i++) {
520 var key = keys.get(i);
521 var val = this.node.listeners.get(key);
524 itms += i >0 ? ",\n" : "";
526 var str = val.strip();
527 var lines = str.split("\n");
528 if (lines.length > 0) {
529 //str = string.joinv("\n" + this.pad + " ", lines);
530 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
533 itms += this.pad + indent_str + key.replace("|", "") + " : " + str;
538 itms += "\n" + this.pad + "}";
539 //print ( "ADD " + itms);
544 public void iterChildren()
548 // finally munge the children...
549 if (this.node.items.size < 1) {
552 var itms = "items : [\n";
554 for(var i = 0; i < this.node.items.size;i++) {
555 var ele = this.node.items.get(i);
556 if (ele.props.has_key("* prop")) {
563 itms += this.pad + indent_str +
564 this.mungeChild( this.pad + indent_str + indent_str , ele);
568 itms += "\n"+ this.pad + "]" + "\n";
572 // finally output listeners...
574 public void xIncludeToString()