resources/RooUsage.txt
[app.Builder.js] / old-javascript / JsRender / Node.vala
1
2 // test..
3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
4
5
6 /*
7  * 
8  * props:
9  * 
10  * key value view of properties.
11  * 
12  * Old standard..
13  * XXXXX : YYYYY  -- standard - should be rendered as XXXX : "YYYY" usually.
14  * |XXXXX : YYYYY  -- standard - should be rendered as XXXX : YYYY usually.
15  * |init  -- the initialization...
16  * *prop : a property which is actually an object definition... 
17  * *args : contructor args
18  * .ctor : Full contruct line...  
19  * 
20  * Newer code
21  * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type  name 
22  * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23  *  "|void:clearFiles": "() .... some code...."  | type name
24  *
25  * 
26  * 
27  * 
28  * 
29  * Standardize this crap...
30  * 
31  * standard properties (use to set)
32  *          If they are long values show the dialog..
33  * 
34  * bool is_xxx  :: can show a pulldown.. (true/false)
35  * string html  
36  * $ string html  = string with value interpolated eg. baseURL + ".." 
37  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
38  * $ untypedvalue = javascript untyped value... 
39  * 
40  * object properties (not part of the GOjbect being wrapped?
41  * # Gee.ArrayList<Xcls_fileitem> fileitems
42  * 
43  * signals
44  * @ void open 
45  * 
46  * methods -- always text editor..
47  * | void clearFiles
48  * | someJSmethod
49  * 
50  * specials
51  * * prop -- string
52  * * args  -- string
53  * * ctor -- string
54  * * init -- big string?
55  * 
56  * event handlers (listeners)
57  *   just shown 
58  * 
59  * -----------------
60  * special ID values
61  *  +XXXX -- indicates it's a instance property / not glob...
62  *  *XXXX -- skip writing glob property (used as classes that can be created...)
63  * 
64  * 
65  * 
66  *  FORMATING?
67 .method {
68          color : green;
69          font-weight: bold;      
70 }
71 .prop {
72         color : #333;
73 }
74 .prop-code {
75     font-style: italic;
76  }
77 .listener {
78     color: #600;
79     font-weight: bold;   
80 }
81 .special { 
82   color : #00c;    font-weight: bold;    
83
84
85 */
86
87
88
89
90
91
92 public class JsRender.Node : Object {
93         
94
95         public static int uid_count = 0;
96         
97         public Node parent;
98         public Gee.ArrayList<Node> items; // child items..
99         
100         public Gee.HashMap<string,string> props; // the properties..
101         public Gee.HashMap<string,string> listeners; // the listeners..
102         public string  xvala_cls;
103         public string xvala_xcls; // 'Xcls_' + id;
104         public string xvala_id; // item id or ""
105                 
106
107
108         public Node()
109         {
110                 this.items = new Gee.ArrayList<Node>();
111                 this.props = new Gee.HashMap<string,string>();
112                 this.listeners = new Gee.HashMap<string,string>();
113                 this.xvala_cls = "";
114                 this.xvala_xcls = "";
115                 this.xvala_id = "";
116                 this.parent = null;
117         }
118
119         
120         public string uid()
121         {
122                 if (this.props.get("id") == null) {
123                         uid_count++;
124                         return "uid-%d".printf(uid_count);
125                 }
126                 return this.props.get("id");
127         }
128         
129         
130         public bool hasChildren()
131         {
132                 return this.items.size > 0;
133         }
134         public bool hasXnsType()
135         {
136                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
137                         return true;
138                         
139                 }
140                 return false;
141         }
142         public string fqn()
143         {
144                 if (!this.hasXnsType ()) {
145                         return "";
146                 }
147                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
148
149         }
150         public void setFqn(string name)
151         {
152                 var ar = name.split(".");
153                 this.props.set("xtype", ar[ar.length-1]);
154                 var l = name.length - (ar[ar.length-1].length +1);
155                 this.props.set("$ xns", name.substring(0, l));
156                 print("setFQN %s to %s\n", name , this.fqn());
157                                
158
159         }
160         // wrapper around get props that returns empty string if not found.
161         public string get(string key)
162         {
163                 var k = this.props.get(key);
164                 if (k != null) {
165                         return k;
166                 }
167                 
168                 k = this.props.get("$ " + key);
169                 if (k != null) {
170                         return k;
171                 }
172                 
173                 var iter = this.props.map_iterator();
174                 while (iter.next()) {
175                         var kk = iter.get_key().split(" ");
176                         if (kk[kk.length-1] == key) {
177                                 return iter.get_value();
178                         }
179                 }
180                 
181                 
182                 return "";
183                 
184         }
185         
186         public string get_key(string key)
187         {
188                 var k = this.props.get(key);
189                 if (k != null) {
190                         return key;
191                 }
192                 
193                 k = this.props.get("$ " + key);
194                 if (k != null) {
195                         return "$ " + key;
196                 }
197                 
198                 var iter = this.props.map_iterator();
199                 while (iter.next()) {
200                         var kk = iter.get_key().split(" ");
201                         if (kk[kk.length-1] == key) {
202                                 return iter.get_key();
203                         }
204                 }
205                 
206                 
207                 return "";
208                 
209         }
210         
211         public void set(string key, string value) {
212                 this.props.set(key,value);
213         }
214          public bool has(string key)
215         {
216                 var k = this.props.get(key);
217                 if (k != null) {
218                         return true;
219                 }
220                 var iter = this.props.map_iterator();
221                 while (iter.next()) {
222                         var kk = iter.get_key().strip().split(" ");
223                         if (kk[kk.length-1] == key) {
224                                 return true;
225                         }
226                 }
227                 
228                 return false;
229                 
230         }
231
232         public void  remove()
233         {
234                 if (this.parent == null) {
235                         
236                         
237                         return;
238                 }
239                 var nlist = new Gee.ArrayList<Node>();
240                 for (var i =0;i < this.parent.items.size; i++) {
241                         if (this.parent.items.get(i) == this) {
242                                 continue;
243                         }
244                         nlist.add(this.parent.items.get(i));
245                 }
246                 this.parent.items = nlist;
247                 this.parent = null;
248
249         }
250          
251         /* creates javascript based on the rules */
252         public Node? findProp(string n) {
253                 for(var i=0;i< this.items.size;i++) {
254                         var p = this.items.get(i).get("* prop");
255                         if (this.items.get(i).get("* prop").length < 1) {
256                                 continue;
257                         }
258                         if (p == n) {
259                                 return this.items.get(i);
260                         }
261                 }
262                 return null;
263
264         }
265
266         
267         
268          
269         static Json.Generator gen = null;
270         
271         public string quoteString(string str)
272         {
273                 if (Node.gen == null) {
274                         Node.gen = new Json.Generator();
275                 }
276                  var n = new Json.Node(Json.NodeType.VALUE);
277                 n.set_string(str);
278  
279                 Node.gen.set_root (n);
280                 return  Node.gen.to_data (null);   
281         }
282
283         public void loadFromJson(Json.Object obj, int version) {
284                 obj.foreach_member((o , key, value) => {
285                         //print(key+"\n");
286                         if (key == "items") {
287                                 var ar = value.get_array();
288                                 ar.foreach_element( (are, ix, el) => {
289                                         var node = new Node();
290                                         node.parent = this;
291                                         node.loadFromJson(el.get_object(), version);
292                                         this.items.add(node);
293                                 });
294                                 return;
295                         }
296                         if (key == "listeners") {
297                                 var li = value.get_object();
298                                 li.foreach_member((lio , li_key, li_value) => {
299                                         this.listeners.set(li_key, li_value.get_string());
300
301                                 });
302                                 return;
303                         }
304                         var v = value.get_value();
305                         var sv =  Value (typeof (string));
306                         v.transform(ref sv);
307
308                         var rkey = key;
309                         if (version == 1) {
310                                 rkey = this.upgradeKey(key, (string)sv);
311                         }
312
313                         
314                         this.props.set(rkey,  (string)sv);
315                 });
316                 
317
318
319
320         }
321
322         public string upgradeKey(string key, string val)
323         {
324                 // convert V1 to V2
325                 if (key.length < 1) {
326                         return key;
327                 }
328                 switch(key) {
329                         case "*prop":
330                         case "*args":
331                         case ".ctor":
332                         case "|init":
333                                 return "* " + key.substring(1);
334                                 
335                         case "pack":
336                                 return "* " + key;
337                 }
338                 if (key[0] == '.') { // v2 does not start with '.' ?
339                         var bits = key.substring(1).split(":");
340                         if (bits[0] == "signal") {
341                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
342                         }
343                         return "# " + string.joinv(" ", bits);                  
344                 }
345                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
346                         return key;
347                 }
348                 var bits = key.substring(1).split(":");
349                 // two types '$' or '|' << for methods..
350                 // javascript 
351                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
352                         return "| " + key.substring(1);
353                 }
354                 // vala function..
355                 
356                 if  (Regex.match_simple ("^\\(", val.strip())) {
357                 
358                         return "| " + string.joinv(" ", bits);
359                 }
360                 
361                 // guessing it's a property..
362                 return "$ " + string.joinv(" ", bits);
363                 
364                 
365
366         }
367
368
369
370
371
372         
373         public Node  deepClone()
374         {
375                 var n = new Node();
376                 n.loadFromJson(this.toJsonObject(), 2);
377                 return n;
378
379         }
380         public string toJsonString()
381         {
382                 if (Node.gen == null) {
383                         Node.gen = new Json.Generator();
384                         gen.pretty =  true;
385                         gen.indent = 4;
386                 }
387                 var n = new Json.Node(Json.NodeType.OBJECT);
388                 n.set_object(this.toJsonObject () );
389                 Node.gen.set_root (n);
390                 return  Node.gen.to_data (null);   
391         }
392         
393         public Json.Object toJsonObject()
394         {
395                 var ret = new Json.Object();
396
397                 // listeners...
398                 if (this.listeners.size > 0) {
399                         var li = new Json.Object();
400                         ret.set_object_member("listeners", li);
401                         var liter = this.listeners.map_iterator();
402                         while (liter.next()) {
403                                 li.set_string_member(liter.get_key(), liter.get_value());
404                         }
405                 }
406                 //props
407                 if (this.props.size > 0 ) {
408                         var iter = this.props.map_iterator();
409                         while (iter.next()) {
410                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
411                         }
412                 }
413                 if (this.items.size > 0) {
414                         var ar = new Json.Array();
415                         ret.set_array_member("items", ar);
416                 
417                         // children..
418                         for(var i =0;i < this.items.size;i++) {
419                                 ar.add_object_element(this.items.get(i).toJsonObject());
420                         }
421                 }
422                 return ret;
423                 
424  
425         }
426          
427         public void jsonObjectsetMember(Json.Object o, string key, string val) {
428                 if (Lang.isBoolean(val)) {
429                         o.set_boolean_member(key, val.down() == "false" ? false : true);
430                         return;
431                 }
432                 
433                 
434                 if (Lang.isNumber(val)) {
435                         if (val.contains(".")) {
436                                 //print( "ADD " + key + "=" + val + " as a double?\n");
437                                 o.set_double_member(key, double.parse (val));
438                                 return;
439
440                         }
441                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
442                         o.set_int_member(key,long.parse(val));
443                         return;
444                 }
445                 ///print( "ADD " + key + "=" + val + " as a string?\n");
446                 o.set_string_member(key,val);
447                 
448         }
449         public string nodeTip()
450         {
451                 var ret = this.nodeTitle(true);
452                 var funcs = "";
453                 var iter = this.props.map_iterator();
454                 while (iter.next()) {
455                         var i =  iter.get_key();
456                         //, iter.get_value());
457                         if ( i[0] != '|') {
458                                 continue;
459                         }
460                 
461                         //if (i == "* init") { 
462                         //      continue;
463                         //}
464                         var val = iter.get_value();
465                         if (Regex.match_simple("^\\s*function", val)) { 
466                                 funcs += "\n<b>" + 
467                                         GLib.Markup.escape_text(i.substring(1)) +"</b> : " + 
468                                         GLib.Markup.escape_text(val.split("\n")[0]);
469                                 continue;
470                         }
471                         if (Regex.match_simple("^\\s*\\(", val)) {
472                                 funcs += "\n<b>" + GLib.Markup.escape_text(i.substring(1)) +
473                                         "</b> : " + 
474                                         GLib.Markup.escape_text(val.split("\n")[0]);
475                                 continue;
476                         }
477                         
478                 }
479                 if (funcs.length > 0) {
480                         ret+="\n\nMethods:" + funcs;
481                 } 
482                 return ret;
483
484         }
485         public string nodeTitle(bool for_tip = false) {
486                 string[] txt = {};
487
488                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
489                 //if (sr) txt.push('<s>');
490
491                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
492                 
493                 //if (renderfull && c['|xns']) {
494                 var fqn = this.fqn();
495                 var fqn_ar = fqn.split(".");
496                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
497                         
498                 //}
499                 
500                 //if (c.xtype)    { txt.push(c.xtype); }
501                         
502                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
503                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
504                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
505                 
506                 
507                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
508                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
509                 if (this.has("html"))    { txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>"); }
510                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
511                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
512                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
513                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
514                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
515                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
516                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
517                 
518                 // for flat classes...
519                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
520                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
521                 
522                 
523                 //if (sr) txt.push('</s>');
524                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
525         }
526
527 }