summaryrefslogtreecommitdiff
path: root/includes/js/dojox/wire/ml/Data.js
blob: 71ab0adf6052960c28af4d756cf94337adc2aa1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
if(!dojo._hasResource["dojox.wire.ml.Data"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.wire.ml.Data"] = true;
dojo.provide("dojox.wire.ml.Data");
dojo.provide("dojox.wire.ml.DataProperty");

dojo.require("dijit._Widget");
dojo.require("dijit._Container");
dojo.require("dojox.wire.ml.util");

dojo.declare("dojox.wire.ml.Data", [dijit._Widget, dijit._Container], {
	//	summary:
	//		A widget for a data object
	//	description:
	//		This widget represents an object with '_properties' property.
	//		If child 'DataProperty' widgets exist, they are used to initialize
	//		propertiy values of '_properties' object.

	startup: function(){
		//	summary:
		//		Call _initializeProperties()
		//	description:
		//		See _initializeProperties().
		this._initializeProperties();
	},

	_initializeProperties: function(/*Boolean*/reset){
		//	summary:
		//		Initialize a data object
		//	description:
		//		If this widget has child DataProperty widgets, their getValue()
		//		methods are called and set the return value to a property
		//		specified by 'name' attribute of the child widgets.
		//	reset:
		//		A boolean to reset current properties
		if(!this._properties || reset){
			this._properties = {};
		}
		var children = this.getChildren();
		for(var i in children){
			var child = children[i];
			if((child instanceof dojox.wire.ml.DataProperty) && child.name){
				this.setPropertyValue(child.name, child.getValue());
			}
		}
	},

	getPropertyValue: function(/*String*/property){
		//	summary:
		//		Return a property value
		//	description:
		//		This method returns the value of a property, specified with
		//		'property' argument, in '_properties' object.
		//	property:
		//		A property name
		//	returns:
		//		A property value
		return this._properties[property]; //anything
	},

	setPropertyValue: function(/*String*/property, /*anything*/value){
		//	summary:
		//		Store a property value
		//	description:
		//		This method stores 'value' as a property, specified with
		//		'property' argument, in '_properties' object.
		//	property:
		//		A property name
		//	value:
		//		A property value
		this._properties[property] = value;
	}
});

dojo.declare("dojox.wire.ml.DataProperty", [dijit._Widget, dijit._Container], {
	//	summary:
	//		A widget to define a data property
	//	description:
	//		Attributes of this widget are used to add a property to the parent
	//		Data widget.
	//		'type' attribute specifies one of "string", "number", "boolean",
	//		"array", "object" and "element" (DOM Element)
	//		(default to "string").
	//		If 'type' is "array" or "object", child DataProperty widgets are
	//		used to initialize the array elements or the object properties.
	//	name:
	//		A property name
	//	type:
	//		A property type name
	//	value:
	//		A property value
	name: "",
	type: "",
	value: "",

	getValue: function(){
		//	summary:
		//		Returns a property value
		//	description:
		//		If 'type' is specified, 'value' attribute is converted to
		//		the specified type and returned.
		//		Otherwise, 'value' attribute is returned as is.
		//	returns:
		//		A property value
		var value = this.value;
		if(this.type){
			if(this.type == "number"){
				value = parseInt(value);
			}else if(this.type == "boolean"){
				value = (value == "true");
			}else if(this.type == "array"){
				value = [];
				var children = this.getChildren();
				for(var i in children){
					var child = children[i];
					if(child instanceof dojox.wire.ml.DataProperty){
						value.push(child.getValue());
					}
				}
			}else if(this.type == "object"){
				value = {};
				var children = this.getChildren();
				for(var i in children){
					var child = children[i];
					if((child instanceof dojox.wire.ml.DataProperty) && child.name){
						value[child.name] = child.getValue();
					}
				}
			}else if(this.type == "element"){
				value = new dojox.wire.ml.XmlElement(value);
				var children = this.getChildren();
				for(var i in children){
					var child = children[i];
					if((child instanceof dojox.wire.ml.DataProperty) && child.name){
						value.setPropertyValue(child.name, child.getValue());
					}
				}
			}
		}
		return value; //anything
	}
});

}