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
10 * key value view of properties.
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...
21 * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type name
22 * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23 * "|void:clearFiles": "() .... some code...." | type name
29 * Standardize this crap...
31 * standard properties (use to set)
32 * If they are long values show the dialog..
34 * bool is_xxx :: can show a pulldown.. (true/false)
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...
40 * object properties (not part of the GOjbect being wrapped?
41 * # Gee.ArrayList<Xcls_fileitem> fileitems
46 * methods -- always text editor..
54 * * init -- big string?
56 * event handlers (listeners)
61 * +XXXX -- indicates it's a instance property / not glob...
62 * *XXXX -- skip writing glob property (used as classes that can be created...)
63 * _XXXX -- (string) a translatable string.
82 color : #00c; font-weight: bold;
92 public class JsRender.Node : GLib.Object {
95 public static int uid_count = 0;
97 public int oid { get; private set; }
99 private Gee.ArrayList<Node> items; // child items..
100 public GLib.ListStore childstore; // must be kept in sync with items
101 public GLib.ListStore? propstore; // must be kept in sync with items
102 public string xvala_cls;
103 public string xvala_xcls; // 'Xcls_' + id;
104 public string xvala_id; // item id or ""
107 public int line_start;
109 public Gee.ArrayList<int> lines;
110 public Gee.HashMap<int,string> line_map; // store of l:xxx or p:.... // fixme - not needed as we can store line numbers in props now.
111 public Gee.ArrayList<int> node_lines;
112 public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
114 private int _updated_count = 0;
115 public int updated_count {
117 return this._updated_count;
120 this.nodeTitleProp = ""; // ?? should trigger set?
121 this.iconFilename = "";
122 this. _updated_count = value;
125 } // changes to this trigger updates on the tree..
129 this.items = new Gee.ArrayList<Node>();
130 //this._props = new Gee.HashMap<string,NodeProp>();
131 //this._listeners = new Gee.HashMap<string,NodeProp>(); // Nodeprop can include line numbers..
132 this.propstore = new GLib.ListStore(typeof(NodeProp)); // Nodeprop can include line numbers..
134 this.xvala_xcls = "";
137 this.line_start = -1;
139 this.lines = new Gee.ArrayList<int>();
140 this.line_map = new Gee.HashMap<int,string>();
141 this.node_lines = new Gee.ArrayList<int>();
142 this.node_lines_map = new Gee.HashMap<int,Node>();
143 this.childstore = new GLib.ListStore( typeof(Node));
144 this.oid = uid_count++;
148 public Gee.ArrayList<Node> readItems()
150 return this.items; // note should not modify add/remove from this directly..
153 public void setNodeLine(int line, Node node) {
154 //print("Add node @ %d\n", line);
155 if (this.node_lines_map.has_key(line)) {
158 this.node_lines.add(line);
159 this.node_lines_map.set(line, node);
163 public void setLine(int line, string type, string prop)
165 //GLib.debug("set prop %s (%s) to line %d", prop, type, line);
166 if (this.line_map.has_key(line)) {
167 if (this.line_map.get(line) != "e:" ) {
171 this.lines.add(line);
173 this.line_map.set(line, type + ":" + prop);
174 if (type == "e" || type == "p" ) {
176 if (prop == "" || !this.props.has_key(prop)) {
177 ///GLib.debug("cant find prop '%s'", prop);
181 var prope = this.props.get(prop);
182 if (prope != null && type =="p") {
183 prope.start_line = line;
185 if (prope != null && type =="e") {
186 prope.end_line = line;
190 if (type == "l" || type =="x") {
191 if (prop == "" || !this.listeners.has_key(prop)) {
192 //GLib.debug("cant find listener '%s'", prop);
196 var prope = this.listeners.get(prop);
197 if (prope != null && type =="l") {
198 prope.start_line = line;
200 if (prope != null && type =="x") {
201 prope.end_line = line;
209 //GLib.debug("setLine %d, %s", line, type + ":" + prop);
211 public void sortLines() {
212 //print("sortLines\n");
213 this.lines.sort((a,b) => {
214 return (int)a-(int)b;
216 this.node_lines.sort((a,b) => {
217 return (int)a-(int)b;
220 public Node? lineToNode(int line)
222 //print("Searching for line %d\n",line);
224 //foreach(int el in this.node_lines) {
225 //print("all lines %d\n", el);
229 foreach(int el in this.node_lines) {
230 //print("?match %d\n", el);
243 var ret = this.node_lines_map.get(l);
244 if (line > ret.line_end) {
247 //print("RETURNING NODE ON LINE %d", l);
254 var ret = this.node_lines_map.get(l);
255 if (line > ret.line_end) {
258 //print("RETURNING NODE ON LINE %d", l);
267 public NodeProp? lineToProp(int line)
270 for(var i= 0; i < this.propstore.get_n_items();i++) {
271 var p = (NodeProp) this.propstore.get_item(i);
272 GLib.debug("prop %s lines %d -> %d", p.name, p.start_line, p.end_line);
273 if (p.start_line > line) {
276 if (line > p.end_line) {
286 public bool getPropertyRange(string prop, out int start, out int end)
290 foreach(int el in this.lines) {
292 if (this.line_map.get(el) == prop) {
306 public void dumpProps(string indent = "")
308 print("%s:\n" , this.fqn());
309 foreach(int el in this.lines) {
310 print("%d: %s%s\n", el, indent, this.line_map.get(el));
312 foreach(Node n in this.items) {
313 n.dumpProps(indent + " ");
321 if (this.props.get("id") == null) {
322 return "uid-%d".printf(this.oid);
324 return this.props.get("id").val;
328 public bool hasChildren()
330 return this.items.size > 0;
332 public bool hasXnsType()
334 if (this.props.get("xns") != null && this.props.get("xtype") != null) {
341 public string FQN { // for sorting
342 owned get { return this.fqn(); }
348 if (!this.hasXnsType ()) {
351 return this.props.get("xns").val + "." + this.props.get("xtype").val;
354 public void setFqn(string name)
356 var ar = name.split(".");
357 var l = name.length - (ar[ar.length-1].length +1);
361 if (this.props.has_key("xtype")) {
362 this.props.get("xtype").val = ar[ar.length-1];
364 this.add_prop(new NodeProp.prop("xtype", "", ar[ar.length-1]));
366 if (this.props.has_key("xns")) {
367 this.props.get("xns").val = name.substring(0, l);
369 this.add_prop(new NodeProp.raw("xns", "", name.substring(0, l)));
373 //print("setFQN %s to %s\n", name , this.fqn());
377 // wrapper around get props that returns empty string if not found.
378 //overrides Glib.object.get (hence new)
379 public new string get(string key)
382 var v = this.props.get(key);
383 return v == null ? "" : v.val;
386 public NodeProp? get_prop(string key)
389 return this.props.get(key);
397 public bool has(string key)
399 return this.props.has_key(key);
406 if (this.parent == null) {
407 GLib.debug("remove - parent is null?");
410 var nlist = new Gee.ArrayList<Node>();
411 for (var i =0;i < this.parent.items.size; i++) {
412 if (this.parent.items.get(i) == this) {
415 nlist.add(this.parent.items.get(i));
418 if ( this.parent.childstore.find(this, out pos)) {
419 this.parent.childstore.remove(pos);
422 this.parent.items = nlist;
427 /* creates javascript based on the rules */
428 public Node? findProp(string n) {
429 for(var i=0;i< this.items.size;i++) {
430 var p = this.items.get(i).get("* prop");
435 return this.items.get(i);
445 static Json.Generator gen = null;
447 public string quoteString(string str)
449 if (Node.gen == null) {
450 Node.gen = new Json.Generator();
452 var n = new Json.Node(Json.NodeType.VALUE);
455 Node.gen.set_root (n);
456 return Node.gen.to_data (null);
459 public void loadFromJsonString(string str, int ver)
461 var pa = new Json.Parser();
463 pa.load_from_data(str);
464 } catch (GLib.Error e) {
465 GLib.debug("Error loading string?");
468 var new_node = pa.get_root();
469 var obj = new_node.get_object ();
471 this.loadFromJson(obj, ver);
476 public void loadFromJson(Json.Object obj, int version) {
478 obj.foreach_member((o , key, value) => {
480 if (key == "items") {
481 var ar = value.get_array();
482 ar.foreach_element( (are, ix, el) => {
483 var node = new Node();
485 node.loadFromJson(el.get_object(), version);
486 this.items.add(node);
487 this.childstore.append(node);
491 if (key == "listeners") {
492 var li = value.get_object();
493 li.foreach_member((lio , li_key, li_value) => {
494 this.add_prop(new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
495 //this.listeners.set(li_key, new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
502 var sval = this.jsonNodeAsString(value);
505 rkey = this.upgradeKey(key, sval);
507 var n = new NodeProp.from_json(rkey, sval);
521 // converts the array into a string with line breaks.
522 public string jsonNodeAsString(Json.Node node)
525 if (node.get_node_type() == Json.NodeType.ARRAY) {
526 var buffer = new GLib.StringBuilder();
527 var ar = node.get_array();
528 for (var i = 0; i < ar.get_length(); i++) {
530 buffer.append_c('\n');
532 buffer.append(ar.get_string_element(i));
536 // hopeflyu only type value..
537 var sv = Value (typeof (string));
538 var v = node.get_value();
544 // really old files...
546 public string upgradeKey(string key, string val)
549 if (key.length < 1) {
557 return "* " + key.substring(1);
562 if (key[0] == '.') { // v2 does not start with '.' ?
563 var bits = key.substring(1).split(":");
564 if (bits[0] == "signal") {
565 return "@" + string.joinv(" ", bits).substring(bits[0].length);
567 return "# " + string.joinv(" ", bits);
569 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
572 var bits = key.substring(1).split(":");
573 // two types '$' or '|' << for methods..
575 if (Regex.match_simple ("^function\\s*(", val.strip())) {
576 return "| " + key.substring(1);
580 if (Regex.match_simple ("^\\(", val.strip())) {
582 return "| " + string.joinv(" ", bits);
585 // guessing it's a property..
586 return "$ " + string.joinv(" ", bits);
597 public Node deepClone()
600 n.loadFromJson(this.toJsonObject(), 2);
604 public string toJsonString()
606 if (Node.gen == null) {
607 Node.gen = new Json.Generator();
611 var n = new Json.Node(Json.NodeType.OBJECT);
612 n.set_object(this.toJsonObject () );
613 Node.gen.set_root (n);
614 return Node.gen.to_data (null);
617 public void jsonObjectAddStringValue(Json.Object obj, string key, string v)
619 if (v.index_of_char('\n',0) < 0) {
620 obj.set_string_member(key,v);
623 var aro = new Json.Array();
624 var ar = v.split("\n");
625 for(var i =0;i < ar.length;i++) {
626 aro.add_string_element(ar[i]);
628 obj.set_array_member(key,aro);
631 public Json.Object toJsonObject()
633 var ret = new Json.Object();
636 if (this.listeners.size > 0) {
637 var li = new Json.Object();
638 ret.set_object_member("listeners", li);
639 var liter = this.listeners.map_iterator();
640 while (liter.next()) {
641 this.jsonObjectAddStringValue(li, liter.get_value().to_json_key(), liter.get_value().val);
645 if (this.props.size > 0 ) {
646 var iter = this.props.map_iterator();
647 while (iter.next()) {
648 this.jsonObjectsetMember(ret, iter.get_value().to_json_key(), iter.get_value().val);
651 if (this.items.size > 0) {
652 var ar = new Json.Array();
653 ret.set_array_member("items", ar);
656 for(var i =0;i < this.items.size;i++) {
657 ar.add_object_element(this.items.get(i).toJsonObject());
665 public void jsonObjectsetMember(Json.Object o, string key, string val) {
666 if (Lang.isBoolean(val)) {
667 o.set_boolean_member(key, val.down() == "false" ? false : true);
672 if (Lang.isNumber(val)) {
673 if (val.contains(".")) {
674 //print( "ADD " + key + "=" + val + " as a double?\n");
675 o.set_double_member(key, double.parse (val));
679 //print( "ADD " + key + "=" + val + " as a int?\n") ;
680 o.set_int_member(key,long.parse(val));
683 ///print( "ADD " + key + "=" + val + " as a string?\n");
684 this.jsonObjectAddStringValue(o,key,val);
685 //o.set_string_member(key,val);
690 public string nodeTipProp {
695 return this.nodeTip();
698 // fixme this needs to better handle 'user defined types etc..
699 public string nodeTip()
701 var ret = this.nodeTitle(true);
710 var keys = new Gee.ArrayList<string>();
711 foreach(var k in this.props.keys) {
715 return Posix.strcmp(a, b);
720 foreach(var pk in keys) {
722 var prop = this.props.get(pk);
723 var i = prop.name.strip();
726 val = val == null ? "" : val;
730 case RAW: // should they be the same?
732 props += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
733 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
734 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
742 funcs += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
743 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
744 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
748 case USER : // user defined.
750 uprops += "\n\t<b>" +
751 GLib.Markup.escape_text(i) +"</b> : " +
752 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
755 case SPECIAL : // * prop| args | ctor | init
759 GLib.Markup.escape_text(i) +"</b> : " +
760 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
763 case LISTENER : return ""; // always raw...
773 keys = new Gee.ArrayList<string>();
774 foreach(var k in this.listeners.keys) {
778 return Posix.strcmp(a, b);
782 foreach(var pk in keys) {
784 var prop = this.listeners.get(pk);
785 var i = prop.name.strip();
787 var val = prop.val.strip();
788 if (val == null || val.length < 1) {
791 listen += "\n\t<b>" +
792 GLib.Markup.escape_text(i) +"</b> : " +
793 GLib.Markup.escape_text(val.split("\n")[0]);
798 if (props.length > 0) {
799 ret+="\n\nProperties:" + props;
801 if (uprops.length > 0) {
802 ret+="\n\nUser defined Properties:" + uprops;
806 if (funcs.length > 0) {
807 ret+="\n\nMethods:" + funcs;
809 if (listen.length > 0) {
810 ret+="\n\nListeners:" + listen;
812 if (spec.length > 0) {
813 ret+="\n\nSpecial:" + spec;
820 public string nodeTitleProp {
825 return this.nodeTitle();
834 public string nodeTitle(bool for_tip = false)
838 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
839 //if (sr) txt.push('<s>');
841 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
843 //if (renderfull && c['|xns']) {
844 var fqn = this.fqn();
845 var fqn_ar = fqn.split(".");
846 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
848 if (fqn == "Roo.bootstrap.Element" && this.has("tag")) {
850 txt += GLib.Markup.escape_text(this.get("tag").up());
853 //if (c.xtype) { txt.push(c.xtype); }
855 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
856 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
857 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
860 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
861 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
862 if (this.has("html") && this.get("html").length > 0) {
863 var ht = this.get("html").split("\n");
865 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
867 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
870 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
871 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
872 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
873 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
874 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
875 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
876 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
877 // class is quite important on bootstrap..
878 if (this.has("cls")){ txt += ("<b>[cls=" + GLib.Markup.escape_text(this.get("cls")) + "]</b>"); }
881 if (fqn == "Roo.bootstrap.Link") {
882 txt += ("<b>href=" + (this.has("name") ? GLib.Markup.escape_text(this.get("name")) : "?" ) + "</b>");
883 if (this.has("fa")){ txt += ("<b>[fa=" + GLib.Markup.escape_text(this.get("fa")) + "]</b>"); }
888 // for flat classes...
889 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
890 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
893 //if (sr) txt.push('</s>');
894 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
896 // used by trees to display icons?
897 // needs more thought?!?
898 public string iconFilename {
903 var clsname = this.fqn();
905 var clsb = clsname.split(".");
906 var sub = clsb.length > 1 ? clsb[1].down() : "";
907 var fn = "/usr/share/glade/pixmaps/hicolor/16x16/actions/widget-gtk-" + sub + ".png";
908 //if (FileUtils.test (fn, FileTest.IS_REGULAR)) {
911 //return "/dev/null"; //???
917 public void insertAfter(Node child, Node after)
919 this.insertChild(this.items.index_of(after) + 1, child);
921 public void insertBefore(Node child, Node before)
923 this.insertChild(this.items.index_of(before), child);
926 public void insertChild(int pos, Node child)
928 this.items.insert(pos, child);
929 this.childstore.insert(pos, child);
932 public void appendChild(Node child)
934 this.items.add( child);
935 this.childstore.append(child);
943 previous we had listeners / and props
945 we really need to store this as flat array - keep it simple!?
957 public void loadProps(GLib.ListStore model)
960 // fixme sorting?? - no need to loop twice .. just use sorting.!
961 var oldstore = this.propstore;
962 this.propstore = model;
963 for(var i = 0; i < oldstore.n_items; i++ ) {
964 var it = (NodeProp) oldstore.get_item(i);
971 // used to replace propstore, so it does not get wiped by editing a node
972 public void dupeProps()
974 GLib.debug("dupeProps START");
975 var oldstore = this.propstore;
976 this.propstore = new GLib.ListStore(typeof(NodeProp));;
977 for(var i = 0; i < oldstore.n_items; i++ ) {
978 var it = (NodeProp) oldstore.get_item(i);
979 this.propstore.append(it);
981 GLib.debug("dupeProps END");
985 public void remove_prop(NodeProp prop)
988 if (!this.propstore.find(prop, out pos)) {
991 this.propstore.remove(pos);
992 this.updated_count++;
996 public bool has_prop_key(NodeProp prop)
998 for(var i = 0; i < this.propstore.n_items; i++ ) {
999 var it = (NodeProp) this.propstore.get_item(i);
1000 if (it.ptype == prop.ptype && it.to_index_key() == prop.to_index_key()) {
1012 public void add_prop(NodeProp prop)
1014 if (this.has_prop_key(prop) && !prop.to_index_key().has_suffix("[]")) {
1015 GLib.warning("duplicate key' %s'- can not add - call has_prop_key first", prop.to_index_key());
1019 this.propstore.append(prop);
1022 this.updated_count++;
1027 int props_updated_count = -1;
1028 Gee.HashMap<string,NodeProp> props_cache;
1030 public Gee.HashMap<string,NodeProp> props {
1032 if (this.updated_count == this.props_updated_count) {
1033 return this.props_cache;
1035 this.props_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1037 for(var i = 0; i < this.propstore.n_items; i++ ) {
1038 var it = (NodeProp) this.propstore.get_item(i);
1039 if (it.ptype != NodePropType.LISTENER) {
1040 // GLib.debug("props add key %s", it.to_index_key());
1041 this.props_cache.set( it.to_index_key() , it);
1044 this.props_updated_count = this.updated_count;
1045 return this.props_cache;
1048 GLib.error("do not set listerners direclty");
1052 int listeners_updated_count = -1;
1053 Gee.HashMap<string,NodeProp> listeners_cache;
1055 //private Gee.HashMap<string,NodeProp> _listeners; // the listeners..
1056 public Gee.HashMap<string,NodeProp> listeners {
1058 if (this.updated_count == this.listeners_updated_count) {
1059 return this.listeners_cache;
1062 this.listeners_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1064 for(var i = 0; i < this.propstore.n_items; i++ ) {
1065 var it = (NodeProp) this.propstore.get_item(i);
1066 if (it.ptype == NodePropType.LISTENER) {
1067 this.listeners_cache.set( it.to_index_key() , it);
1070 this.listeners_updated_count = this.updated_count;
1071 return this.listeners_cache;;
1074 GLib.error("do not set listerners direclty");
1077 private void sortProps ()
1080 this.propstore.sort( (a, b) => {
1082 return Posix.strcmp( ((NodeProp)a).to_sort_key(), ((NodeProp)b).to_sort_key());