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;
29 Gee.HashMap<string,Gee.ArrayList<Node>> out_array_props;
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> ();
51 this.out_array_props = new Gee.HashMap<string,Gee.ArrayList<Node>>() ;
57 this.cur_line = parent == null ? 0 : parent.cur_line;
59 this.top = parent == null ? this : parent.top;
60 this.line_buffer = "";
67 public void addLine(string str= "")
69 if (this.line_buffer.length > 0) {
71 this.ret += this.line_buffer+ ",\n";
73 this.line_buffer = str;
77 public void addMultiLine(string str= "")
80 this.cur_line += str.split("\n").length;
81 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
82 this.ret += str + "\n";
87 This currently works by creating a key/value array of this.els, which is just an array of properties..
88 this is so that join() works...
92 b) output plan properties.
105 public string munge ( )
107 //return this.mungeToString(this.node);
109 this.node.line_start = this.cur_line;
111 this.checkChildren();
113 this.readArrayProps();
114 this.readListeners();
116 if (!this.node.props.has_key("* xinclude")) {
123 if (this.els.size < 1) {
128 var spad = pad.substring(0, this.pad.length-indent);
131 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
132 //print ("STR PROPS: " + str_props);
136 if (!this.node.props.has_key("* xinclude")) {
138 this.pad + str_props +
144 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
146 this.pad + str_props +
151 string gLibStringListJoin( string sep, Gee.ArrayList<string> ar)
154 for (var i = 0; i < ar.size; i++) {
155 ret += i>0 ? sep : "";
161 public string mungeChild(string pad , Node cnode)
163 var x = new NodeToJs(cnode, this.doubleStringProps, pad, this);
170 public void checkChildren ()
174 // look throught he chilren == looking for * prop.. -- fixme might not work..
177 if (!this.node.hasChildren()) {
182 for (var ii =0; ii< this.node.items.size; ii++) {
183 var pl = this.node.items.get(ii);
184 if (!pl.props.has_key("* prop")) {
189 //print(JSON.stringify(pl,null,4));
191 //var prop = pl['*prop'] + '';
192 //delete pl['*prop'];
193 var prop = pl.get("* prop");
194 print("got prop "+ prop + "\n");
197 if (! Regex.match_simple("\\[\\]$", prop)) {
198 // it's a standard prop..
200 // munge property..??
202 this.out_nodeprops.set(prop, pl);
204 this.els.add( prop + " : " + this.mungeChild ( this.pad + indent_str, pl));
214 var sprop = prop.replace("[]", "");
215 print("sprop is : " + sprop + "\n");
217 // it's an array type..
219 if (!this.ar_props.has_key(sprop)) {
221 this.ar_props.set(sprop, "");
224 old = this.ar_props.get(sprop);
226 var nstr = old += old.length > 0 ? ",\n" : "";
227 nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str , pl);
229 this.ar_props.set(sprop, nstr);
236 * Standardize this crap...
238 * standard properties (use to set)
239 * If they are long values show the dialog..
242 * bool is_xxx :: can show a pulldown.. (true/false)
244 * $ string html = string with value interpolated eg. baseURL + ".."
245 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
246 * $ untypedvalue = javascript untyped value...
247 * _ string html ... = translatable..
250 * object properties (not part of the GOjbect being wrapped?
251 * # Gee.ArrayList<Xcls_fileitem> fileitems
256 * methods -- always text editor..
264 * * init -- big string?
266 * event handlers (listeners)
271 * +XXXX -- indicates it's a instance property / not glob...
272 * *XXXX -- skip writing glob property (used as classes that can be created...)
276 public void readProps()
281 if (this.node.props.has_key("$ xns")) {
282 this.out_props.set("'|xns'", this.node.props.get("$ xns") );
284 this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
290 func_regex = new Regex("^\\s+|\\s+$");
292 print("failed to build regex");
295 // sort the key's so they always get rendered in the same order..
297 var keys = new Gee.ArrayList<string>();
298 var piter = this.node.props.map_iterator();
299 while (piter.next() ) {
303 this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
308 keys.sort(( a, b) => {
309 return ((string)a).collate((string)b);
310 //if (a == b) return 0;
311 //return a < b ? -1 : 1;
316 for (var i = 0; i< keys.size; i++) {
317 var key = this.node.get_key(keys.get(i));
318 print("ADD KEY %s\n", key);
323 this.node.normalize_key(key, out k, out kflag, out ktype);
326 var v = this.node.get(key);
329 //if (this.skip.contains(k) ) {
332 if ( Regex.match_simple("\\[\\]$", k)) {
333 // array .. not supported... here?
339 // skip builder stuff. prefixed with '.' .. just like unix fs..
340 if (kflag == ".") { // |. or . -- do not output..
344 // ignore '* prop'; ???
349 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
350 left = "'" + leftv + "'";
351 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
352 var val = this.node.quoteString(leftv);
354 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
362 // next.. is it a function.. or a raw string..
370 // ??? any others that are raw output..
372 // does not hapepnd with arrays..
373 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
380 str = func_regex.replace(v,v.length, 0, "");
382 print("regex failed");
388 var lines = str.split("\n");
390 if (lines.length > 0) {
391 nstr = string.joinv("\n" + this.pad, lines);
392 //nstr = string.joinv("\n", lines);
394 //print("==> " + str + "\n");
395 this.els.add(left + nstr);
406 ktype.down() == "boolean"
408 ktype.down() == "bool"
410 ktype.down() == "number"
412 ktype.down() == "int"
413 ) { // boolean or number...?
414 this.els.add(left + v.down() );
418 // is it a translated string?
424 //if (this.doubleStringProps.size < 1) {
425 // this.els.add(left + this.node.quoteString(v));
429 if (this.doubleStringProps.index_of(k) > -1) {
430 // then use the translated version...
432 els.add(left + "_this._strings['" +
433 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
438 if (ktype.down() == "string" && k[0] == '_') {
439 els.add(left + "_this._strings['" +
440 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
445 // otherwise it needs to be encapsulated.. as single quotes..
447 var vv = this.node.quoteString(v);
448 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
449 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
457 public void readArrayProps()
460 // handle the childitems that are arrays.. eg. button[] = { }...
464 var iter = this.ar_props.map_iterator();
465 while (iter.next()) {
466 var k = iter.get_key();
467 var right = iter.get_value();
469 string leftv = k[0] == '|' ? k.substring(1) : k;
470 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
471 left = "'" + leftv + "'";
472 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
473 var val = this.node.quoteString(leftv);
475 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
484 if (right.length > 0){
485 this.els.add(left + "[\n" + this.pad + indent_str + indent_str +
486 right + "\n" + this.pad + "]");
493 public void readListeners()
496 if (this.node.listeners.size < 1) {
499 // munge the listeners.
500 //print("ADDING listeners?");
502 var liter = this.node.listeners.map_iterator();
506 var keys = new Gee.ArrayList<string>();
507 var piter = this.node.listeners.map_iterator();
508 while (piter.next() ) {
510 keys.add(piter.get_key());
512 keys.sort(( a, b) => {
513 return ((string)a).collate((string)b);
514 //if (a == b) return 0;
515 //return a < b ? -1 : 1;
518 var itms = "listeners : {\n";
520 for (var i = 0; i< keys.size; i++) {
521 var key = keys.get(i);
522 var val = this.node.listeners.get(key);
525 itms += i >0 ? ",\n" : "";
527 var str = val.strip();
528 var lines = str.split("\n");
529 if (lines.length > 0) {
530 //str = string.joinv("\n" + this.pad + " ", lines);
531 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
534 itms += this.pad + indent_str + key.replace("|", "") + " : " + str;
539 itms += "\n" + this.pad + "}";
540 //print ( "ADD " + itms);
545 public void iterChildren()
549 // finally munge the children...
550 if (this.node.items.size < 1) {
553 var itms = "items : [\n";
555 for(var i = 0; i < this.node.items.size;i++) {
556 var ele = this.node.items.get(i);
557 if (ele.props.has_key("* prop")) {
564 itms += this.pad + indent_str +
565 this.mungeChild( this.pad + indent_str + indent_str , ele);
569 itms += "\n"+ this.pad + "]" + "\n";
573 // finally output listeners...
575 public void xIncludeToString()