aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/wire/DataWire.js
blob: ecb6223deb4fc45cbd698884c3c190d137f471f9 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
if(!dojo._hasResource["dojox.wire.DataWire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.wire.DataWire"] = true;
dojo.provide("dojox.wire.DataWire");

dojo.require("dojox.wire.Wire");

dojo.declare("dojox.wire.DataWire", dojox.wire.Wire, {
	//	summary:
	//		A Wire for item attributes of data stores
	//	description:
	//		This class accesses item attributes of data stores with a dotted
	//		notation of attribute names specified to 'attribute' property,
	//		using data APIs of a data store specified to 'dataStore' property.
	//		The root object for this class must be an item of the data store.
	//		Intermediate attribute names in the dotted notation specify
	//		attributes for child items, which are used for repeated calls to
	//		data APIs until reached to a descendant attribute.
	//		Attribute names may have an array index, such as "a[0]", to
	//		identify an array element of the attribute value.
	
	_wireClass: "dojox.wire.DataWire",

	constructor: function(/*Object*/args){
		//	summary:
		//		Initialize properties
		//	description:
		//		If 'dataStore' property is not specified, but 'parent' property
		//		is specified, 'dataStore' property is copied from the parent.
		//	args:
		//		Arguments to initialize properties
		//		dataStore:
		//			A data store
		//		attribute:
		//			A dotted notation to a descendant attribute
		if(!this.dataStore && this.parent){
			this.dataStore = this.parent.dataStore;
		}
	},
	_getValue: function(/*Object*/object){
		//	summary:
		//		Return an attribute value of an item
		//	description:
		//		This method uses a root item passed in 'object' argument and
		//		'attribute' property to call getValue() method of
		//		'dataStore'.
		//		If an attribute name have an array suffix ("[]"), getValues()
		//		method is called, instead.
		//		If an index is specified in the array suffix, an array element
		//		for the index is returned, instead of the array itself.
		//	object:
		//		A root item
		//	returns:
		//		A value found, otherwise 'undefined'
		if(!object || !this.attribute || !this.dataStore){
			return object; //Object
		}

		var value = object;
		var list = this.attribute.split('.');
		for(var i in list){
			value = this._getAttributeValue(value, list[i]);
			if(!value){
				return undefined; //undefined
			}
		}
		return value; //anything
	},

	_setValue: function(/*Object*/object, /*anything*/value){
		//	summary:
		//		Set an attribute value to an item
		//	description:
		//		This method uses a root item passed in 'object' argument and
		//		'attribute' property to identify an item.
		//		Then, setValue() method of 'dataStore' is called with a leaf
		//		attribute name and 'value' argument.
		//		If an attribute name have an array suffix ("[]"), setValues()
		//		method is called, instead.
		//		If an index is specified in the array suffix, an array element
		//		for the index is set to 'value', instead of the array itself.
		//	object:
		//		A root item
		//	value:
		//		A value to set
		//	returns:
		//		'object', or 'undefined' for invalid attribute
		if(!object || !this.attribute || !this.dataStore){
			return object; //Object
		}

		var item = object;
		var list = this.attribute.split('.');
		var last = list.length - 1;
		for(var i = 0; i < last; i++){
			item = this._getAttributeValue(item, list[i]);
			if(!item){
				return undefined; //undefined
			}
		}
		this._setAttributeValue(item, list[last], value);
		return object; //Object
	},

	_getAttributeValue: function(/*Object*/item, /*String*/attribute){
		//	summary:
		//		Return an attribute value of an item
		//	description:
		//		This method uses an item passed in 'item' argument and
		//		'attribute' argument to call getValue() method of 'dataStore'.
		//		If an attribute name have an array suffix ("[]"), getValues()
		//		method is called, instead.
		//		If an index is specified in the array suffix, an array element
		//		for the index is returned, instead of the array itself.
		//	item:
		//		An item
		//	attribute
		//		An attribute name
		//	returns:
		//		A value found, otherwise 'undefined'
		var value = undefined;
		var i1 = attribute.indexOf('[');
		if(i1 >= 0){
			var i2 = attribute.indexOf(']');
			var index = attribute.substring(i1 + 1, i2);
			attribute = attribute.substring(0, i1);
			var array = this.dataStore.getValues(item, attribute);
			if(array){
				if(!index){ // return array for "attribute[]"
					value = array;
				}else{
					value = array[index];
				}
			}
		}else{
			value = this.dataStore.getValue(item, attribute);
		}
		return value; //anything 
	},

	_setAttributeValue: function(/*Object*/item, /*String*/attribute, /*anything*/value){
		//	summary:
		//		Set an attribute value to an item
		//	description:
		//		This method uses an item passed in 'item' argument and
		//		'attribute' argument to call setValue() method of 'dataStore'
		//		with 'value' argument.
		//		If an attribute name have an array suffix ("[]"), setValues()
		//		method is called, instead.
		//		If an index is specified in the array suffix, an array element
		//		for the index is set to 'value', instead of the array itself.
		//	item:
		//		An item
		//	attribute:
		//		An attribute name
		//	value:
		//		A value to set
		var i1 = attribute.indexOf('[');
		if(i1 >= 0){
			var i2 = attribute.indexOf(']');
			var index = attribute.substring(i1 + 1, i2);
			attribute = attribute.substring(0, i1);
			var array = null;
			if(!index){ // replace whole array for "attribute[]"
				array = value;
			}else{
				array = this.dataStore.getValues(item, attribute);
				if(!array){
					array = [];
				}
				array[index] = value;
			}
			this.dataStore.setValues(item, attribute, array);
		}else{
			this.dataStore.setValue(item, attribute, value);
		}
	}
});

}