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 // sort the key's so they always get rendered in the same order..
219 var keys = new Gee.ArrayList<string>();
220 var piter = this.node.props.map_iterator();
221 while (piter.next() ) {
225 this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
229 keys.sort(( a, b) => {
230 return ((string)a).collate((string)b);
231 //if (a == b) return 0;
232 //return a < b ? -1 : 1;
234 for (var i = 0; i< keys.size; i++) {
235 var key = this.node.get_key(keys.get(i));
236 print("ADD KEY %s\n", key);
241 this.node.normalize_key(key, out k, out kflag, out ktype);
244 var v = this.node.get(key);
247 //if (this.skip.contains(k) ) {
250 if ( Regex.match_simple("\\[\\]$", k)) {
251 // array .. not supported... here?
257 // skip builder stuff. prefixed with '.' .. just like unix fs..
258 if (kflag == ".") { // |. or . -- do not output..
262 // ignore '* prop'; ???
267 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
268 left = "'" + leftv + "'";
269 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
270 var val = this.node.quoteString(leftv);
272 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
280 // next.. is it a function.. or a raw string..
288 // ??? any others that are raw output..
290 // does not hapepnd with arrays..
291 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
298 str = func_regex.replace(v,v.length, 0, "");
300 print("regex failed");
306 var lines = str.split("\n");
308 if (lines.length > 0) {
309 nstr = string.joinv("\n" + this.pad, lines);
310 //nstr = string.joinv("\n", lines);
312 //print("==> " + str + "\n");
313 this.els.add(left + nstr);
324 ktype.down() == "boolean"
326 ktype.down() == "bool"
328 ktype.down() == "number"
330 ktype.down() == "int"
331 ) { // boolean or number...?
332 this.els.add(left + v.down() );
336 // is it a translated string?
342 //if (this.doubleStringProps.size < 1) {
343 // this.els.add(left + this.node.quoteString(v));
347 if (this.doubleStringProps.index_of(k) > -1) {
348 // then use the translated version...
350 els.add(left + "_this._strings['" +
351 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
356 if (ktype.down() == "string" && k[0] == '_') {
357 els.add(left + "_this._strings['" +
358 GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
363 // otherwise it needs to be encapsulated.. as single quotes..
365 var vv = this.node.quoteString(v);
366 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
367 this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
375 public void readArrayProps()
378 // handle the childitems that are arrays.. eg. button[] = { }...
382 var iter = this.ar_props.map_iterator();
383 while (iter.next()) {
384 var k = iter.get_key();
385 var right = iter.get_value();
387 string leftv = k[0] == '|' ? k.substring(1) : k;
388 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
389 left = "'" + leftv + "'";
390 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
391 var val = this.node.quoteString(leftv);
393 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
402 if (right.length > 0){
403 this.els.add(left + "[\n" + this.pad + indent_str + indent_str +
404 right + "\n" + this.pad + "]");
411 public void readListeners()
414 if (this.node.listeners.size < 1) {
417 // munge the listeners.
418 //print("ADDING listeners?");
420 var liter = this.node.listeners.map_iterator();
424 var keys = new Gee.ArrayList<string>();
425 var piter = this.node.listeners.map_iterator();
426 while (piter.next() ) {
428 keys.add(piter.get_key());
430 keys.sort(( a, b) => {
431 return ((string)a).collate((string)b);
432 //if (a == b) return 0;
433 //return a < b ? -1 : 1;
436 var itms = "listeners : {\n";
438 for (var i = 0; i< keys.size; i++) {
439 var key = keys.get(i);
440 var val = this.node.listeners.get(key);
443 itms += i >0 ? ",\n" : "";
445 var str = val.strip();
446 var lines = str.split("\n");
447 if (lines.length > 0) {
448 //str = string.joinv("\n" + this.pad + " ", lines);
449 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
452 itms += this.pad + indent_str + key.replace("|", "") + " : " + str;
457 itms += "\n" + this.pad + "}";
458 //print ( "ADD " + itms);
463 public void iterChildren()
467 // finally munge the children...
468 if (this.node.items.size < 1) {
471 var itms = "items : [\n";
473 for(var i = 0; i < this.node.items.size;i++) {
474 var ele = this.node.items.get(i);
475 if (ele.props.has_key("* prop")) {
482 itms += this.pad + indent_str +
483 this.mungeChild( this.pad + indent_str + indent_str , ele);
487 itms += "\n"+ this.pad + "]" + "\n";
491 // finally output listeners...
493 public void xIncludeToString()