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;
123 if (this.parent != null) {
124 this.parent.updated_count++;
128 } // changes to this trigger updates on the tree..
132 this.items = new Gee.ArrayList<Node>();
133 //this._props = new Gee.HashMap<string,NodeProp>();
134 //this._listeners = new Gee.HashMap<string,NodeProp>(); // Nodeprop can include line numbers..
135 this.propstore = new GLib.ListStore(typeof(NodeProp)); // Nodeprop can include line numbers..
137 this.xvala_xcls = "";
140 this.line_start = -1;
142 this.lines = new Gee.ArrayList<int>();
143 this.line_map = new Gee.HashMap<int,string>();
144 this.node_lines = new Gee.ArrayList<int>();
145 this.node_lines_map = new Gee.HashMap<int,Node>();
146 this.childstore = new GLib.ListStore( typeof(Node));
147 this.oid = uid_count++;
151 public Gee.ArrayList<Node> readItems()
153 return this.items; // note should not modify add/remove from this directly..
156 public void setNodeLine(int line, Node node) {
157 //print("Add node @ %d\n", line);
158 if (this.node_lines_map.has_key(line)) {
161 this.node_lines.add(line);
162 this.node_lines_map.set(line, node);
166 public void setLine(int line, string type, string prop)
168 //GLib.debug("set prop %s (%s) to line %d", prop, type, line);
169 if (this.line_map.has_key(line)) {
170 if (this.line_map.get(line) != "e:" ) {
174 this.lines.add(line);
176 this.line_map.set(line, type + ":" + prop);
177 if (type == "e" || type == "p" ) {
179 if (prop == "" || !this.props.has_key(prop)) {
180 ///GLib.debug("cant find prop '%s'", prop);
184 var prope = this.props.get(prop);
185 if (prope != null && type =="p") {
186 prope.start_line = line;
188 if (prope != null && type =="e") {
189 prope.end_line = line;
193 if (type == "l" || type =="x") {
194 if (prop == "" || !this.listeners.has_key(prop)) {
195 //GLib.debug("cant find listener '%s'", prop);
199 var prope = this.listeners.get(prop);
200 if (prope != null && type =="l") {
201 prope.start_line = line;
203 if (prope != null && type =="x") {
204 prope.end_line = line;
212 //GLib.debug("setLine %d, %s", line, type + ":" + prop);
214 public void sortLines() {
215 //print("sortLines\n");
216 this.lines.sort((a,b) => {
217 return (int)a-(int)b;
219 this.node_lines.sort((a,b) => {
220 return (int)a-(int)b;
223 public Node? lineToNode(int line)
225 //print("Searching for line %d\n",line);
227 //foreach(int el in this.node_lines) {
228 //print("all lines %d\n", el);
232 foreach(int el in this.node_lines) {
233 //print("?match %d\n", el);
246 var ret = this.node_lines_map.get(l);
247 if (line > ret.line_end) {
250 //print("RETURNING NODE ON LINE %d", l);
257 var ret = this.node_lines_map.get(l);
258 if (line > ret.line_end) {
261 //print("RETURNING NODE ON LINE %d", l);
270 public NodeProp? lineToProp(int line)
273 for(var i= 0; i < this.propstore.get_n_items();i++) {
274 var p = (NodeProp) this.propstore.get_item(i);
275 GLib.debug("prop %s lines %d -> %d", p.name, p.start_line, p.end_line);
276 if (p.start_line > line) {
279 if (line > p.end_line) {
289 public bool getPropertyRange(string prop, out int start, out int end)
293 foreach(int el in this.lines) {
295 if (this.line_map.get(el) == prop) {
309 public void dumpProps(string indent = "")
311 print("%s:\n" , this.fqn());
312 foreach(int el in this.lines) {
313 print("%d: %s%s\n", el, indent, this.line_map.get(el));
315 foreach(Node n in this.items) {
316 n.dumpProps(indent + " ");
324 if (this.props.get("id") == null) {
325 return "uid-%d".printf(this.oid);
327 return this.props.get("id").val;
331 public bool hasChildren()
333 return this.items.size > 0;
335 public bool hasXnsType()
337 if (this.props.get("xns") != null && this.props.get("xtype") != null) {
344 public string FQN { // for sorting
345 owned get { return this.fqn(); }
351 if (!this.hasXnsType ()) {
354 return this.props.get("xns").val + "." + this.props.get("xtype").val;
357 public void setFqn(string name)
359 var ar = name.split(".");
360 var l = name.length - (ar[ar.length-1].length +1);
364 if (this.props.has_key("xtype")) {
365 this.props.get("xtype").val = ar[ar.length-1];
367 this.add_prop(new NodeProp.prop("xtype", "", ar[ar.length-1]));
369 if (this.props.has_key("xns")) {
370 this.props.get("xns").val = name.substring(0, l);
372 this.add_prop(new NodeProp.raw("xns", "", name.substring(0, l)));
376 //print("setFQN %s to %s\n", name , this.fqn());
380 // wrapper around get props that returns empty string if not found.
381 //overrides Glib.object.get (hence new)
382 public new string get(string key)
385 var v = this.props.get(key);
386 return v == null ? "" : v.val;
389 public NodeProp? get_prop(string key)
392 return this.props.get(key);
400 public bool has(string key)
402 return this.props.has_key(key);
409 if (this.parent == null) {
410 GLib.debug("remove - parent is null?");
413 var nlist = new Gee.ArrayList<Node>();
414 for (var i =0;i < this.parent.items.size; i++) {
415 if (this.parent.items.get(i) == this) {
418 nlist.add(this.parent.items.get(i));
421 if ( this.parent.childstore.find(this, out pos)) {
422 this.parent.childstore.remove(pos);
425 this.parent.items = nlist;
430 /* creates javascript based on the rules */
431 public Node? findProp(string n) {
432 for(var i=0;i< this.items.size;i++) {
433 var p = this.items.get(i).get("* prop");
438 return this.items.get(i);
448 static Json.Generator gen = null;
450 public string quoteString(string str)
452 if (Node.gen == null) {
453 Node.gen = new Json.Generator();
455 var n = new Json.Node(Json.NodeType.VALUE);
458 Node.gen.set_root (n);
459 return Node.gen.to_data (null);
462 public void loadFromJsonString(string str, int ver)
464 var pa = new Json.Parser();
466 pa.load_from_data(str);
467 } catch (GLib.Error e) {
468 GLib.debug("Error loading string?");
471 var new_node = pa.get_root();
472 var obj = new_node.get_object ();
474 this.loadFromJson(obj, ver);
479 public void loadFromJson(Json.Object obj, int version) {
481 obj.foreach_member((o , key, value) => {
483 if (key == "items") {
484 var ar = value.get_array();
485 ar.foreach_element( (are, ix, el) => {
486 var node = new Node();
488 node.loadFromJson(el.get_object(), version);
489 this.items.add(node);
490 this.childstore.append(node);
494 if (key == "listeners") {
495 var li = value.get_object();
496 li.foreach_member((lio , li_key, li_value) => {
497 this.add_prop(new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
498 //this.listeners.set(li_key, new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
505 var sval = this.jsonNodeAsString(value);
508 rkey = this.upgradeKey(key, sval);
510 var n = new NodeProp.from_json(rkey, sval);
524 // converts the array into a string with line breaks.
525 public string jsonNodeAsString(Json.Node node)
528 if (node.get_node_type() == Json.NodeType.ARRAY) {
529 var buffer = new GLib.StringBuilder();
530 var ar = node.get_array();
531 for (var i = 0; i < ar.get_length(); i++) {
533 buffer.append_c('\n');
535 buffer.append(ar.get_string_element(i));
539 // hopeflyu only type value..
540 var sv = Value (typeof (string));
541 var v = node.get_value();
547 // really old files...
549 public string upgradeKey(string key, string val)
552 if (key.length < 1) {
560 return "* " + key.substring(1);
565 if (key[0] == '.') { // v2 does not start with '.' ?
566 var bits = key.substring(1).split(":");
567 if (bits[0] == "signal") {
568 return "@" + string.joinv(" ", bits).substring(bits[0].length);
570 return "# " + string.joinv(" ", bits);
572 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
575 var bits = key.substring(1).split(":");
576 // two types '$' or '|' << for methods..
578 if (Regex.match_simple ("^function\\s*(", val.strip())) {
579 return "| " + key.substring(1);
583 if (Regex.match_simple ("^\\(", val.strip())) {
585 return "| " + string.joinv(" ", bits);
588 // guessing it's a property..
589 return "$ " + string.joinv(" ", bits);
600 public Node deepClone()
603 n.loadFromJson(this.toJsonObject(), 2);
607 public string toJsonString()
609 if (Node.gen == null) {
610 Node.gen = new Json.Generator();
614 var n = new Json.Node(Json.NodeType.OBJECT);
615 n.set_object(this.toJsonObject () );
616 Node.gen.set_root (n);
617 return Node.gen.to_data (null);
620 public void jsonObjectAddStringValue(Json.Object obj, string key, string v)
622 if (v.index_of_char('\n',0) < 0) {
623 obj.set_string_member(key,v);
626 var aro = new Json.Array();
627 var ar = v.split("\n");
628 for(var i =0;i < ar.length;i++) {
629 aro.add_string_element(ar[i]);
631 obj.set_array_member(key,aro);
634 public Json.Object toJsonObject()
636 var ret = new Json.Object();
639 if (this.listeners.size > 0) {
640 var li = new Json.Object();
641 ret.set_object_member("listeners", li);
642 var liter = this.listeners.map_iterator();
643 while (liter.next()) {
644 this.jsonObjectAddStringValue(li, liter.get_value().to_json_key(), liter.get_value().val);
648 if (this.props.size > 0 ) {
649 var iter = this.props.map_iterator();
650 while (iter.next()) {
651 this.jsonObjectsetMember(ret, iter.get_value().to_json_key(), iter.get_value().val);
654 if (this.items.size > 0) {
655 var ar = new Json.Array();
656 ret.set_array_member("items", ar);
659 for(var i =0;i < this.items.size;i++) {
660 ar.add_object_element(this.items.get(i).toJsonObject());
668 public void jsonObjectsetMember(Json.Object o, string key, string val) {
669 if (Lang.isBoolean(val)) {
670 o.set_boolean_member(key, val.down() == "false" ? false : true);
675 if (Lang.isNumber(val)) {
676 if (val.contains(".")) {
677 //print( "ADD " + key + "=" + val + " as a double?\n");
678 o.set_double_member(key, double.parse (val));
682 //print( "ADD " + key + "=" + val + " as a int?\n") ;
683 o.set_int_member(key,long.parse(val));
686 ///print( "ADD " + key + "=" + val + " as a string?\n");
687 this.jsonObjectAddStringValue(o,key,val);
688 //o.set_string_member(key,val);
693 public string nodeTipProp {
698 return this.nodeTip();
701 // fixme this needs to better handle 'user defined types etc..
702 public string nodeTip()
704 var ret = this.nodeTitle(true);
713 var keys = new Gee.ArrayList<string>();
714 foreach(var k in this.props.keys) {
718 return Posix.strcmp(a, b);
723 foreach(var pk in keys) {
725 var prop = this.props.get(pk);
726 var i = prop.name.strip();
729 val = val == null ? "" : val;
733 case RAW: // should they be the same?
735 props += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
736 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
737 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
745 funcs += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
746 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
747 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
751 case USER : // user defined.
753 uprops += "\n\t<b>" +
754 GLib.Markup.escape_text(i) +"</b> : " +
755 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
758 case SPECIAL : // * prop| args | ctor | init
762 GLib.Markup.escape_text(i) +"</b> : " +
763 GLib.Markup.escape_text(val == "" ? "" : val.split("\n")[0]);
766 case LISTENER : return ""; // always raw...
776 keys = new Gee.ArrayList<string>();
777 foreach(var k in this.listeners.keys) {
781 return Posix.strcmp(a, b);
785 foreach(var pk in keys) {
787 var prop = this.listeners.get(pk);
788 var i = prop.name.strip();
790 var val = prop.val.strip();
791 if (val == null || val.length < 1) {
794 listen += "\n\t<b>" +
795 GLib.Markup.escape_text(i) +"</b> : " +
796 GLib.Markup.escape_text(val.split("\n")[0]);
801 if (props.length > 0) {
802 ret+="\n\nProperties:" + props;
804 if (uprops.length > 0) {
805 ret+="\n\nUser defined Properties:" + uprops;
809 if (funcs.length > 0) {
810 ret+="\n\nMethods:" + funcs;
812 if (listen.length > 0) {
813 ret+="\n\nListeners:" + listen;
815 if (spec.length > 0) {
816 ret+="\n\nSpecial:" + spec;
823 public string nodeTitleProp {
828 return this.nodeTitle();
837 public string nodeTitle(bool for_tip = false)
841 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
842 //if (sr) txt.push('<s>');
844 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
846 //if (renderfull && c['|xns']) {
847 var fqn = this.fqn();
848 var fqn_ar = fqn.split(".");
849 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
851 if (fqn == "Roo.bootstrap.Element" && this.has("tag")) {
853 txt += GLib.Markup.escape_text(this.get("tag").up());
856 //if (c.xtype) { txt.push(c.xtype); }
858 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
859 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
860 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
863 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
864 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
865 if (this.has("html") && this.get("html").length > 0) {
866 var ht = this.get("html").split("\n");
868 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
870 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
873 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
874 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
875 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
876 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
877 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
878 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
879 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
880 // class is quite important on bootstrap..
881 if (this.has("cls")){ txt += ("<b>[cls=" + GLib.Markup.escape_text(this.get("cls")) + "]</b>"); }
884 if (fqn == "Roo.bootstrap.Link") {
885 txt += ("<b>href=" + (this.has("name") ? GLib.Markup.escape_text(this.get("name")) : "?" ) + "</b>");
886 if (this.has("fa")){ txt += ("<b>[fa=" + GLib.Markup.escape_text(this.get("fa")) + "]</b>"); }
891 // for flat classes...
892 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
893 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
896 //if (sr) txt.push('</s>');
897 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
899 // used by trees to display icons?
900 // needs more thought?!?
901 public string iconFilename {
906 var clsname = this.fqn();
908 var clsb = clsname.split(".");
909 var sub = clsb.length > 1 ? clsb[1].down() : "";
910 var fn = "/usr/share/glade/pixmaps/hicolor/16x16/actions/widget-gtk-" + sub + ".png";
911 //if (FileUtils.test (fn, FileTest.IS_REGULAR)) {
914 //return "/dev/null"; //???
920 public void insertAfter(Node child, Node after)
922 this.insertChild(this.items.index_of(after) + 1, child);
924 public void insertBefore(Node child, Node before)
926 this.insertChild(this.items.index_of(before), child);
929 public void insertChild(int pos, Node child)
931 this.items.insert(pos, child);
932 this.childstore.insert(pos, child);
935 public void appendChild(Node child)
937 this.items.add( child);
938 this.childstore.append(child);
946 previous we had listeners / and props
948 we really need to store this as flat array - keep it simple!?
960 public void loadProps(GLib.ListStore model)
963 // fixme sorting?? - no need to loop twice .. just use sorting.!
964 var oldstore = this.propstore;
965 this.propstore = model;
966 for(var i = 0; i < oldstore.n_items; i++ ) {
967 var it = (NodeProp) oldstore.get_item(i);
974 // used to replace propstore, so it does not get wiped by editing a node
975 public void dupeProps()
977 GLib.debug("dupeProps START");
978 var oldstore = this.propstore;
979 this.propstore = new GLib.ListStore(typeof(NodeProp));;
980 for(var i = 0; i < oldstore.n_items; i++ ) {
981 var it = (NodeProp) oldstore.get_item(i);
982 this.propstore.append(it);
984 GLib.debug("dupeProps END");
988 public void remove_prop(NodeProp prop)
991 if (!this.propstore.find(prop, out pos)) {
994 this.propstore.remove(pos);
995 this.updated_count++;
999 public bool has_prop_key(NodeProp prop)
1001 for(var i = 0; i < this.propstore.n_items; i++ ) {
1002 var it = (NodeProp) this.propstore.get_item(i);
1003 if (it.ptype == prop.ptype && it.to_index_key() == prop.to_index_key()) {
1015 public void add_prop(NodeProp prop)
1017 if (this.has_prop_key(prop) && !prop.to_index_key().has_suffix("[]")) {
1018 GLib.warning("duplicate key' %s'- can not add - call has_prop_key first", prop.to_index_key());
1022 this.propstore.append(prop);
1025 this.updated_count++;
1030 int props_updated_count = -1;
1031 Gee.HashMap<string,NodeProp> props_cache;
1033 public Gee.HashMap<string,NodeProp> props {
1035 if (this.updated_count == this.props_updated_count) {
1036 return this.props_cache;
1038 this.props_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1040 for(var i = 0; i < this.propstore.n_items; i++ ) {
1041 var it = (NodeProp) this.propstore.get_item(i);
1042 if (it.ptype != NodePropType.LISTENER) {
1043 // GLib.debug("props add key %s", it.to_index_key());
1044 this.props_cache.set( it.to_index_key() , it);
1047 this.props_updated_count = this.updated_count;
1048 return this.props_cache;
1051 GLib.error("do not set listerners direclty");
1055 int listeners_updated_count = -1;
1056 Gee.HashMap<string,NodeProp> listeners_cache;
1058 //private Gee.HashMap<string,NodeProp> _listeners; // the listeners..
1059 public Gee.HashMap<string,NodeProp> listeners {
1061 if (this.updated_count == this.listeners_updated_count) {
1062 return this.listeners_cache;
1065 this.listeners_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1067 for(var i = 0; i < this.propstore.n_items; i++ ) {
1068 var it = (NodeProp) this.propstore.get_item(i);
1069 if (it.ptype == NodePropType.LISTENER) {
1070 this.listeners_cache.set( it.to_index_key() , it);
1073 this.listeners_updated_count = this.updated_count;
1074 return this.listeners_cache;;
1077 GLib.error("do not set listerners direclty");
1080 private void sortProps ()
1083 this.propstore.sort( (a, b) => {
1085 return Posix.strcmp( ((NodeProp)a).to_sort_key(), ((NodeProp)b).to_sort_key());