]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/argeo-ria-lib/jcr/class/org/argeo/jcr/ria/model/Node.js
Tweak nodes, provider and data model objects.
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / argeo-ria-lib / jcr / class / org / argeo / jcr / ria / model / Node.js
1 qx.Class.define("org.argeo.jcr.ria.model.Node", {
2 extend : org.argeo.jcr.ria.model.AbstractItem,
3
4 events : {
5 "clear" : "qx.event.type.Event",
6 "childNodeAdded" : "qx.event.type.Data",
7 "childNodeRemoved" : "qx.event.type.Data",
8 "childrenChanged" : "qx.event.type.Data",
9
10 "propertyAdded" : "qx.event.type.Data",
11 "propertyRemoved" : "qx.event.type.Data",
12 "propertiesChanged" : "qx.event.type.Data"
13 },
14 properties : {
15 loadState : {
16 check : "String",
17 init : "empty", // Can be "empty" => "loading" => "loaded"
18 event : "changeLoadState"
19 },
20 nodeProvider : {
21 check : "org.argeo.jcr.ria.provider.INodeProvider"
22 }
23 },
24
25 construct : function(nodeName, isRoot){
26 this.base(arguments);
27 this._children = {};
28 this._properties = {};
29 this.setName(nodeName);
30 if(isRoot){
31 this.setPath("");
32 this.setRoot(this);
33 }
34 },
35
36 members : {
37 _children : null,
38 _properties : null,
39
40 load : function(){
41 this.getNodeProvider().loadNode(this);
42 },
43
44 remove : function(){
45 if(this.itemIsRoot()) return;
46 this.getParent().removeChild(this.getName());
47 },
48
49 fromXmlString : function(xmlString){
50 var domDocument = qx.xml.Document.fromString(xmlString);
51 var root = domDocument.documentElement;
52 this.fromDomElement(root);
53 },
54
55 fromDomElement : function(domElement){
56 if(domElement.nodeType != 1) return;
57 for(var i=0;i<domElement.attributes.length;i++){
58 var att = domElement.attributes[i];
59 var property = new org.argeo.jcr.ria.model.Property(att.nodeName);
60 this.addProperty(property);
61 property.fromDomElement(att);
62 }
63 for(var i=0;i<domElement.childNodes.length;i++){
64 var child = domElement.childNodes[i];
65 if(child.nodeType != 1) continue;
66 var jcrChild = new org.argeo.jcr.ria.model.Node(child.nodeName);
67 this.addChild(jcrChild);
68 jcrChild.fromDomElement(child);
69 }
70 this.setLoadState("loaded");
71 },
72
73 toXmlString : function(recurse, childrenWriter){
74 var string = "<"+this.getName();
75 var props = this.getProperties();
76 for(var i=0;i<props.length;i++){
77 string += " " + props[i].toXmlString();
78 }
79 string += ">";
80 if(recurse){
81 var childs = this.getChildren();
82 var childrenString = "";
83 for(var j=0;j<childs.length;j++){
84 childrenString += childs[j].toXmlString(recurse, childrenWriter);
85 }
86 if(childrenWriter){
87 string += childrenWriter(childrenString);
88 }else{
89 string += childrenString;
90 }
91 }
92 string += "</"+this.getName()+">";
93 return string;
94 },
95
96 getChild : function(childName){
97 return this._children[childName];
98 },
99 getProperty : function(propertyName){
100 return this._properties[propertyName];
101 },
102 addChild : function(childNode){
103 this._children[childNode.getName()] = childNode;
104
105 childNode.setParent(this);
106 childNode.setRoot(this.getRoot());
107 childNode.setPath(this.getPath() + "/" + childNode.getName());
108
109 this.fireDataEvent("childNodeAdded", childNode);
110 this.fireDataEvent("childNodeChanged");
111 },
112 removeChild : function(childName){
113 delete(this._children[childName]);
114 this.fireDataEvent("childNodeRemoved", childName);
115 this.fireDataEvent("childNodeChanged");
116 },
117 addProperty : function(property){
118 this._properties[property.getName()] = property;
119
120 property.setParent(this);
121 property.setRoot(this.getRoot());
122 property.setPath(this.getPath() + "/" + property.getName());
123
124 this.fireDataEvent("propertyAdded", property);
125 this.fireDataEvent("propertiesChanged");
126 },
127 removeProperty : function(propertyName){
128 delete(this._properties[propertyName]);
129 this.fireDataEvent("propertyRemoved", propertyName);
130 this.fireDataEvent("propertiesChanged");
131 },
132 getChildren : function(){
133 return qx.lang.Object.getValues(this._children);
134 },
135 getProperties : function(){
136 return qx.lang.Object.getValues(this._properties);
137 },
138 getChildrenCount : function(){
139 return qx.lang.Object.getLength(this._children);
140 },
141 getPropertiesCount : function(){
142 return qx.lang.Object.getLength(this._properties);
143 }
144 }
145 });