2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.wire.Wire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.wire.Wire"] = true;
|
|
|
3 |
dojo.provide("dojox.wire.Wire");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.wire._base");
|
|
|
6 |
|
|
|
7 |
dojo.declare("dojox.wire.Wire", null, {
|
|
|
8 |
// summary:
|
|
|
9 |
// A default and base Wire to access an object property
|
|
|
10 |
// description:
|
|
|
11 |
// This class accesses a property of an object with a dotted notation
|
|
|
12 |
// specified to 'property' property, such as "a.b.c", which identifies
|
|
|
13 |
// a descendant property, "object.a.b.c".
|
|
|
14 |
// Property names in the dotted notation may have an array index, such
|
|
|
15 |
// as "a[0]", to identify an array element, literally, "object.a[0]".
|
|
|
16 |
// When a notation start with an array index, such as "[0].a", it
|
|
|
17 |
// specifies an array element of the root object (array),
|
|
|
18 |
// "object[0].a".
|
|
|
19 |
// This class also serves as a base class for other Wire classes,
|
|
|
20 |
// preparing a root object and converting a return value, so that
|
|
|
21 |
// sub-classes just can implement _getValue() and _setValue() called
|
|
|
22 |
// from getValue() and setValue() implemented by this calss.
|
|
|
23 |
|
|
|
24 |
_wireClass: "dojox.wire.Wire",
|
|
|
25 |
|
|
|
26 |
constructor: function(/*Object*/args){
|
|
|
27 |
// summary:
|
|
|
28 |
// Initialize properties
|
|
|
29 |
// description:
|
|
|
30 |
// If 'converter' property is specified and is a string for
|
|
|
31 |
// a converter class, an instanceof the converter class is
|
|
|
32 |
// created.
|
|
|
33 |
// args:
|
|
|
34 |
// Arguments to initialize properties
|
|
|
35 |
// object:
|
|
|
36 |
// A root object (or another Wire to access a root object)
|
|
|
37 |
// property:
|
|
|
38 |
// A dotted notation to a descendant property
|
|
|
39 |
// type:
|
|
|
40 |
// A type of the return value (for the source Wire)
|
|
|
41 |
// converter:
|
|
|
42 |
// A converter object (or class name) to convert the return
|
|
|
43 |
// value (for the source Wire)
|
|
|
44 |
dojo.mixin(this, args);
|
|
|
45 |
|
|
|
46 |
if(this.converter){
|
|
|
47 |
if(dojo.isString(this.converter)){
|
|
|
48 |
//First check the object tree for it. Might be defined variable
|
|
|
49 |
//name/global function (like a jsId, or just a function name).
|
|
|
50 |
var convertObject = dojo.getObject(this.converter);
|
|
|
51 |
if (dojo.isFunction(convertObject)){
|
|
|
52 |
//We need to see if this is a pure function or an object constructor...
|
|
|
53 |
try{
|
|
|
54 |
var testObj = new convertObject();
|
|
|
55 |
if(testObj && !dojo.isFunction(testObj["convert"])){
|
|
|
56 |
//Looks like a 'pure' function...
|
|
|
57 |
this.converter = {convert: convertObject};
|
|
|
58 |
}else{
|
|
|
59 |
this.converter = testObj;
|
|
|
60 |
}
|
|
|
61 |
}catch(e){
|
|
|
62 |
//Do if this fails.
|
|
|
63 |
}
|
|
|
64 |
}else if(dojo.isObject(convertObject)){
|
|
|
65 |
//It's an object, like a jsId ... see if it has a convert function
|
|
|
66 |
if(dojo.isFunction(convertObject["convert"])){
|
|
|
67 |
this.converter = convertObject;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
//No object with that name (Converter is still a string),
|
|
|
72 |
//then look for a class that needs to be dynamically loaded...
|
|
|
73 |
if (dojo.isString(this.converter)) {
|
|
|
74 |
var converterClass = dojox.wire._getClass(this.converter);
|
|
|
75 |
if(converterClass){
|
|
|
76 |
this.converter = new converterClass();
|
|
|
77 |
}else{
|
|
|
78 |
this.converter = undefined;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
}else if(dojo.isFunction(this.converter)){
|
|
|
82 |
this.converter = {convert: this.converter};
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
},
|
|
|
86 |
|
|
|
87 |
getValue: function(/*Object||Array*/defaultObject){
|
|
|
88 |
// summary:
|
|
|
89 |
// Return a value of an object
|
|
|
90 |
// description:
|
|
|
91 |
// This method first determins a root object as follows:
|
|
|
92 |
// 1. If 'object' property specified,
|
|
|
93 |
// 1.1 If 'object' is a Wire, its getValue() method is called to
|
|
|
94 |
// obtain a root object.
|
|
|
95 |
// 1.2 Otherwise, use 'object' as a root object.
|
|
|
96 |
// 2. Otherwise, use 'defaultObject' argument.
|
|
|
97 |
// 3. If 'property' is specified, it is used to get a property
|
|
|
98 |
// value.
|
|
|
99 |
// Then, if a sub-class implements _getValue() method, it is
|
|
|
100 |
// called with the root object to get the return value.
|
|
|
101 |
// Otherwise, the root object (typically, a property valye) is
|
|
|
102 |
// used for the return value.
|
|
|
103 |
// Finally, if 'type' property is specified, the return value is
|
|
|
104 |
// converted to the specified primitive type ("string", "number",
|
|
|
105 |
// "boolean" and "array").
|
|
|
106 |
// If 'converter' property is specified, its convert() method is
|
|
|
107 |
// called to convert the value.
|
|
|
108 |
// defaultObject:
|
|
|
109 |
// A default root object
|
|
|
110 |
// returns:
|
|
|
111 |
// A value found
|
|
|
112 |
var object = undefined;
|
|
|
113 |
if(dojox.wire.isWire(this.object)){
|
|
|
114 |
object = this.object.getValue(defaultObject);
|
|
|
115 |
}else{
|
|
|
116 |
object = (this.object || defaultObject);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if(this.property){
|
|
|
120 |
var list = this.property.split('.');
|
|
|
121 |
for(var i in list){
|
|
|
122 |
if(!object){
|
|
|
123 |
return object; //anything (null, undefined, etc)
|
|
|
124 |
}
|
|
|
125 |
object = this._getPropertyValue(object, list[i]);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
var value = undefined;
|
|
|
130 |
if(this._getValue){
|
|
|
131 |
value = this._getValue(object);
|
|
|
132 |
}else{
|
|
|
133 |
value = object;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
if(value){
|
|
|
137 |
if(this.type){
|
|
|
138 |
if(this.type == "string"){
|
|
|
139 |
value = value.toString();
|
|
|
140 |
}else if(this.type == "number"){
|
|
|
141 |
value = parseInt(value);
|
|
|
142 |
}else if(this.type == "boolean"){
|
|
|
143 |
value = (value != "false");
|
|
|
144 |
}else if(this.type == "array"){
|
|
|
145 |
if(!dojo.isArray(value)){
|
|
|
146 |
value = [value];
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
if(this.converter && this.converter.convert){
|
|
|
151 |
value = this.converter.convert(value, this); // optional "this" context
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
return value; //anything
|
|
|
155 |
},
|
|
|
156 |
|
|
|
157 |
setValue: function(/*anything*/value, /*Object||Array*/defaultObject){
|
|
|
158 |
// summary:
|
|
|
159 |
// Set a value to an object
|
|
|
160 |
// description:
|
|
|
161 |
// This method first determins a root object as follows:
|
|
|
162 |
// 1. If 'object' property specified,
|
|
|
163 |
// 1.1 If 'object' is a Wire, its getValue() method is called to
|
|
|
164 |
// obtain a root object.
|
|
|
165 |
// 1.2 Otherwise, use 'object' as a root object.
|
|
|
166 |
// 2. Otherwise, use 'defaultObject' argument.
|
|
|
167 |
// 3. If 'property' is specified, it is used to get a property
|
|
|
168 |
// value.
|
|
|
169 |
// Then, if a sub-class implements _setValue() method, it is
|
|
|
170 |
// called with the root object and 'value' argument to set
|
|
|
171 |
// the value.
|
|
|
172 |
// Otherwise, 'value' is set to a property specified with
|
|
|
173 |
// 'property' property.
|
|
|
174 |
// If the root object is undefined and 'object' property is a Wire
|
|
|
175 |
// and a new object is created and returned by _setValue() it is
|
|
|
176 |
// set through 'object' (setValue() method).
|
|
|
177 |
// value:
|
|
|
178 |
// A value to set
|
|
|
179 |
// defaultObject:
|
|
|
180 |
// A default root object
|
|
|
181 |
var object = undefined;
|
|
|
182 |
if(dojox.wire.isWire(this.object)){
|
|
|
183 |
object = this.object.getValue(defaultObject);
|
|
|
184 |
}else{
|
|
|
185 |
object = (this.object || defaultObject);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
var property = undefined;
|
|
|
189 |
if(this.property){
|
|
|
190 |
if(!object){
|
|
|
191 |
if(dojox.wire.isWire(this.object)){
|
|
|
192 |
object = {};
|
|
|
193 |
this.object.setValue(object, defaultObject);
|
|
|
194 |
}else{
|
|
|
195 |
throw new Error(this._wireClass + ".setValue(): invalid object");
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
var list = this.property.split('.');
|
|
|
199 |
var last = list.length - 1;
|
|
|
200 |
for(var i = 0; i < last; i++){
|
|
|
201 |
var p = list[i];
|
|
|
202 |
var o = this._getPropertyValue(object, p);
|
|
|
203 |
if(!o){
|
|
|
204 |
o = {};
|
|
|
205 |
this._setPropertyValue(object, p, o);
|
|
|
206 |
}
|
|
|
207 |
object = o;
|
|
|
208 |
}
|
|
|
209 |
property = list[last];
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
if(this._setValue){
|
|
|
213 |
if(property){
|
|
|
214 |
var o = this._getPropertyValue(object, property);
|
|
|
215 |
if(!o){
|
|
|
216 |
o = {};
|
|
|
217 |
this._setPropertyValue(object, property, o);
|
|
|
218 |
}
|
|
|
219 |
object = o;
|
|
|
220 |
}
|
|
|
221 |
var newObject = this._setValue(object, value);
|
|
|
222 |
if(!object && newObject){
|
|
|
223 |
if(dojox.wire.isWire(this.object)){
|
|
|
224 |
this.object.setValue(newObject, defaultObject);
|
|
|
225 |
}else{
|
|
|
226 |
throw new Error(this._wireClass + ".setValue(): invalid object");
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
}else{
|
|
|
230 |
if(property){
|
|
|
231 |
this._setPropertyValue(object, property, value);
|
|
|
232 |
}else{
|
|
|
233 |
if(dojox.wire.isWire(this.object)){
|
|
|
234 |
this.object.setValue(value, defaultObject);
|
|
|
235 |
}else{
|
|
|
236 |
throw new Error(this._wireClass + ".setValue(): invalid property");
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
},
|
|
|
241 |
|
|
|
242 |
_getPropertyValue: function(/*Object||Array*/object, /*String*/property){
|
|
|
243 |
// summary:
|
|
|
244 |
// Return a property value of an object
|
|
|
245 |
// description:
|
|
|
246 |
// A value for 'property' of 'object' is returned.
|
|
|
247 |
// If 'property' ends with an array index, it is used to indentify
|
|
|
248 |
// an element of an array property.
|
|
|
249 |
// If 'object' implements getPropertyValue(), it is called with
|
|
|
250 |
// 'property' to obtain the property value.
|
|
|
251 |
// If 'object' implements a getter for the property, it is called
|
|
|
252 |
// to obtain the property value.
|
|
|
253 |
// object:
|
|
|
254 |
// A default root object
|
|
|
255 |
// property:
|
|
|
256 |
// A property name
|
|
|
257 |
// returns:
|
|
|
258 |
// A value found, otherwise 'undefined'
|
|
|
259 |
var value = undefined;
|
|
|
260 |
var i1 = property.indexOf('[');
|
|
|
261 |
if(i1 >= 0){
|
|
|
262 |
var i2 = property.indexOf(']');
|
|
|
263 |
var index = property.substring(i1 + 1, i2);
|
|
|
264 |
var array = null;
|
|
|
265 |
if(i1 === 0){ // object is array
|
|
|
266 |
array = object;
|
|
|
267 |
}else{
|
|
|
268 |
property = property.substring(0, i1);
|
|
|
269 |
array = this._getPropertyValue(object, property);
|
|
|
270 |
if(array && !dojo.isArray(array)){
|
|
|
271 |
array = [array];
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
if(array){
|
|
|
275 |
value = array[index];
|
|
|
276 |
}
|
|
|
277 |
}else if(object.getPropertyValue){
|
|
|
278 |
value = object.getPropertyValue(property);
|
|
|
279 |
}else{
|
|
|
280 |
var getter = "get" + property.charAt(0).toUpperCase() + property.substring(1);
|
|
|
281 |
if(object[getter]){
|
|
|
282 |
value = object[getter]();
|
|
|
283 |
}else{
|
|
|
284 |
value = object[property];
|
|
|
285 |
}
|
|
|
286 |
}
|
|
|
287 |
return value; //anything
|
|
|
288 |
},
|
|
|
289 |
|
|
|
290 |
_setPropertyValue: function(/*Object||Array*/object, /*String*/property, /*anything*/value){
|
|
|
291 |
// summary:
|
|
|
292 |
// Set a property value to an object
|
|
|
293 |
// description:
|
|
|
294 |
// 'value' is set to 'property' of 'object'.
|
|
|
295 |
// If 'property' ends with an array index, it is used to indentify
|
|
|
296 |
// an element of an array property to set the value.
|
|
|
297 |
// If 'object' implements setPropertyValue(), it is called with
|
|
|
298 |
// 'property' and 'value' to set the property value.
|
|
|
299 |
// If 'object' implements a setter for the property, it is called
|
|
|
300 |
// with 'value' to set the property value.
|
|
|
301 |
// object:
|
|
|
302 |
// An object
|
|
|
303 |
// property:
|
|
|
304 |
// A property name
|
|
|
305 |
// value:
|
|
|
306 |
// A value to set
|
|
|
307 |
var i1 = property.indexOf('[');
|
|
|
308 |
if(i1 >= 0){
|
|
|
309 |
var i2 = property.indexOf(']');
|
|
|
310 |
var index = property.substring(i1 + 1, i2);
|
|
|
311 |
var array = null;
|
|
|
312 |
if(i1 === 0){ // object is array
|
|
|
313 |
array = object;
|
|
|
314 |
}else{
|
|
|
315 |
property = property.substring(0, i1);
|
|
|
316 |
array = this._getPropertyValue(object, property);
|
|
|
317 |
if(!array){
|
|
|
318 |
array = [];
|
|
|
319 |
this._setPropertyValue(object, property, array);
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
array[index] = value;
|
|
|
323 |
}else if(object.setPropertyValue){
|
|
|
324 |
object.setPropertyValue(property, value);
|
|
|
325 |
}else{
|
|
|
326 |
var setter = "set" + property.charAt(0).toUpperCase() + property.substring(1);
|
|
|
327 |
if(object[setter]){
|
|
|
328 |
object[setter](value);
|
|
|
329 |
}else{
|
|
|
330 |
object[property] = value;
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
});
|
|
|
335 |
|
|
|
336 |
}
|