aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/wire/Wire.js
blob: f9dde1d2e2f5b2850f7d9226ced0254f8c86ecf1 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
if(!dojo._hasResource["dojox.wire.Wire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.wire.Wire"] = true;
dojo.provide("dojox.wire.Wire");

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

dojo.declare("dojox.wire.Wire", null, {
	//	summary:
	//		A default and base Wire to access an object property
	//	description:
	//		This class accesses a property of an object with a dotted notation
	//		specified to 'property' property, such as "a.b.c", which identifies
	//		a descendant property, "object.a.b.c".
	//		Property names in the dotted notation may have an array index, such
	//		as "a[0]", to identify an array element, literally, "object.a[0]".
	//		When a notation start with an array index, such as "[0].a", it
	//		specifies an array element of the root object (array),
	//		"object[0].a".
	//		This class also serves as a base class for other Wire classes,
	//		preparing a root object and converting a return value, so that
	//		sub-classes just can implement _getValue() and _setValue() called
	//		from getValue() and setValue() implemented by this calss.
	
	_wireClass: "dojox.wire.Wire",
	
	constructor: function(/*Object*/args){
		//	summary:
		//		Initialize properties
		//	description:
		//		If 'converter' property is specified and is a string for
		//		a converter class, an instanceof the converter class is
		//		created.
		//	args:
		//		Arguments to initialize properties
		//		object:
		//			A root object (or another Wire to access a root object)
		//		property:
		//			A dotted notation to a descendant property
		//		type:
		//			A type of the return value (for the source Wire)
		//		converter:
		//			A converter object (or class name) to convert the return
		//			value (for the source Wire)
		dojo.mixin(this, args);

		if(this.converter){
			if(dojo.isString(this.converter)){
				//First check the object tree for it.  Might be defined variable
				//name/global function (like a jsId, or just a function name).
				var convertObject = dojo.getObject(this.converter);
				if (dojo.isFunction(convertObject)){
					//We need to see if this is a pure function or an object constructor...
					try{
						var testObj = new convertObject();
						if(testObj && !dojo.isFunction(testObj["convert"])){
							//Looks like a 'pure' function...
							this.converter = {convert: convertObject};
						}else{
							this.converter = testObj;
						}
					}catch(e){
						//Do if this fails.	
					}
				}else if(dojo.isObject(convertObject)){
					//It's an object, like a jsId ... see if it has a convert function
					if(dojo.isFunction(convertObject["convert"])){
						this.converter = convertObject;
					}
				}

				//No object with that name (Converter is still a string), 
				//then look for a class that needs to be dynamically loaded...
				if (dojo.isString(this.converter)) {
					var converterClass = dojox.wire._getClass(this.converter);
					if(converterClass){
						this.converter = new converterClass();
					}else{
						this.converter = undefined;
					}
				}
			}else if(dojo.isFunction(this.converter)){
				this.converter = {convert: this.converter};
			}
		}
	},

	getValue: function(/*Object||Array*/defaultObject){
		//	summary:
		//		Return a value of an object
		//	description:
		//		This method first determins a root object as follows:
		//		1. If 'object' property specified,
		//		1.1 If 'object' is a Wire, its getValue() method is called to
		//	    	obtain a root object.
		//		1.2 Otherwise, use 'object' as a root object.
		//		2. Otherwise, use 'defaultObject' argument.
		//		3. If 'property' is specified, it is used to get a property
		//			value.
		//		Then, if a sub-class implements _getValue() method, it is
		//		called with the root object to get the return value.
		//		Otherwise, the root object (typically, a property valye) is
		//		used for the return value.
		//		Finally, if 'type' property is specified, the return value is
		//		converted to the specified primitive type ("string", "number",
		//		"boolean" and "array").
		//		If 'converter' property is specified, its convert() method is
		//		called to convert the value.
		//	defaultObject:
		//		A default root object
		//	returns:
		//		A value found
		var object = undefined;
		if(dojox.wire.isWire(this.object)){
			object = this.object.getValue(defaultObject);
		}else{
			object = (this.object || defaultObject);
		}

		if(this.property){
			var list = this.property.split('.');
			for(var i in list){
				if(!object){
					return object; //anything (null, undefined, etc)
				}
				object = this._getPropertyValue(object, list[i]);
			}
		}

		var value = undefined;
		if(this._getValue){
			value = this._getValue(object);
		}else{
			value = object;
		}

		if(value){
			if(this.type){
				if(this.type == "string"){
					value = value.toString();
				}else if(this.type == "number"){
					value = parseInt(value);
				}else if(this.type == "boolean"){
					value = (value != "false");
				}else if(this.type == "array"){
					if(!dojo.isArray(value)){
						value = [value];
					}
				}
			}
			if(this.converter && this.converter.convert){
				value = this.converter.convert(value, this); // optional "this" context
			}
		}
		return value; //anything
	},

	setValue: function(/*anything*/value, /*Object||Array*/defaultObject){
		//	summary:
		//		Set a value to an object
		//	description:
		//		This method first determins a root object as follows:
		//		1. If 'object' property specified,
		//		1.1 If 'object' is a Wire, its getValue() method is called to
		//	    	obtain a root object.
		//		1.2 Otherwise, use 'object' as a root object.
		//		2. Otherwise, use 'defaultObject' argument.
		//		3. If 'property' is specified, it is used to get a property
		//			value.
		//		Then, if a sub-class implements _setValue() method, it is
		//		called with the root object and 'value' argument to set
		//		the value.
		//		Otherwise, 'value' is set to a property specified with
		//		'property' property.
		//		If the root object is undefined and 'object' property is a Wire
		//		and a new object is created and returned by _setValue() it is
		//		set through 'object' (setValue() method).
		//	value:
		//		A value to set
		//	defaultObject:
		//		A default root object
		var object = undefined;
		if(dojox.wire.isWire(this.object)){
			object = this.object.getValue(defaultObject);
		}else{
			object = (this.object || defaultObject);
		}

		var property = undefined;
		if(this.property){
			if(!object){
				if(dojox.wire.isWire(this.object)){
					object = {};
					this.object.setValue(object, defaultObject);
				}else{
					throw new Error(this._wireClass + ".setValue(): invalid object");
				}
			}
			var list = this.property.split('.');
			var last = list.length - 1;
			for(var i = 0; i < last; i++){
				var p = list[i];
				var o = this._getPropertyValue(object, p);
				if(!o){
					o = {};
					this._setPropertyValue(object, p, o);
				}
				object = o;
			}
			property = list[last];
		}

		if(this._setValue){
			if(property){
				var o = this._getPropertyValue(object, property);
				if(!o){
					o = {};
					this._setPropertyValue(object, property, o);
				}
				object = o;
			}
			var newObject = this._setValue(object, value);
			if(!object && newObject){
				if(dojox.wire.isWire(this.object)){
					this.object.setValue(newObject, defaultObject);
				}else{
					throw new Error(this._wireClass + ".setValue(): invalid object");
				}
			}
		}else{
			if(property){
				this._setPropertyValue(object, property, value);
			}else{
				if(dojox.wire.isWire(this.object)){
					this.object.setValue(value, defaultObject);
				}else{
					throw new Error(this._wireClass + ".setValue(): invalid property");
				}
			}
		}
	},

	_getPropertyValue: function(/*Object||Array*/object, /*String*/property){
		//	summary:
		//		Return a property value of an object
		//	description:
		//		A value for 'property' of 'object' is returned.
		//		If 'property' ends with an array index, it is used to indentify
		//		an element of an array property.
		//		If 'object' implements getPropertyValue(), it is called with
		//		'property' to obtain the property value.
		//		If 'object' implements a getter for the property, it is called
		//		to obtain the property value.
		//	object:
		//		A default root object
		//	property:
		//		A property name
		//	returns:
		//		A value found, otherwise 'undefined'
		var value = undefined;
		var i1 = property.indexOf('[');
		if(i1 >= 0){
			var i2 = property.indexOf(']');
			var index = property.substring(i1 + 1, i2);
			var array = null;
			if(i1 === 0){ // object is array
				array = object;
			}else{
				property = property.substring(0, i1);
				array = this._getPropertyValue(object, property);
				if(array && !dojo.isArray(array)){
					array = [array];
				}
			}
			if(array){
				value = array[index];
			}
		}else if(object.getPropertyValue){
			value = object.getPropertyValue(property);
		}else{
			var getter = "get" + property.charAt(0).toUpperCase() + property.substring(1);
			if(object[getter]){
				value = object[getter]();
			}else{
				value = object[property];
			}
		}
		return value; //anything
	},

	_setPropertyValue: function(/*Object||Array*/object, /*String*/property, /*anything*/value){
		//	summary:
		//		Set a property value to an object
		//	description:
		//		'value' is set to 'property' of 'object'.
		//		If 'property' ends with an array index, it is used to indentify
		//		an element of an array property to set the value.
		//		If 'object' implements setPropertyValue(), it is called with
		//		'property' and 'value' to set the property value.
		//		If 'object' implements a setter for the property, it is called
		//		with 'value' to set the property value.
		//	object:
		//		An object
		//	property:
		//		A property name
		//	value:
		//		A value to set
		var i1 = property.indexOf('[');
		if(i1 >= 0){
			var i2 = property.indexOf(']');
			var index = property.substring(i1 + 1, i2);
			var array = null;
			if(i1 === 0){ // object is array
				array = object;
			}else{
				property = property.substring(0, i1);
				array = this._getPropertyValue(object, property);
				if(!array){
					array = [];
					this._setPropertyValue(object, property, array);
				}
			}
			array[index] = value;
		}else if(object.setPropertyValue){
			object.setPropertyValue(property, value);
		}else{
			var setter = "set" + property.charAt(0).toUpperCase() + property.substring(1);
			if(object[setter]){
				object[setter](value);
			}else{
				object[property] = value;
			}
		}
	}
});

}