Wire.js 11 KB

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