util.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.wire.ml.util"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire.ml.util"] = true;
  8. dojo.provide("dojox.wire.ml.util");
  9. dojo.require("dojox.xml.parser");
  10. dojo.require("dojox.wire.Wire");
  11. dojox.wire.ml._getValue = function(/*String*/source, /*Array*/args){
  12. // summary:
  13. // Return a value
  14. // description:
  15. // This method obtains an object by an ID of a widget or an DOM
  16. // element.
  17. // If 'source' specifies a dotted notation to its property, a Wire is
  18. // used to get the object property.
  19. // If 'source' starts with "arguments", 'args' is used as a root
  20. // object for the Wire.
  21. // source:
  22. // A string to specify an object and its property
  23. // args:
  24. // An optional arguments array
  25. // returns:
  26. // A value
  27. if(!source){
  28. return undefined; //undefined
  29. }
  30. var property = undefined;
  31. if(args && source.length >= 9 && source.substring(0, 9) == "arguments"){
  32. property = source.substring(9);
  33. return new dojox.wire.Wire({property: property}).getValue(args);
  34. }
  35. var i = source.indexOf('.');
  36. if(i >= 0){
  37. property = source.substring(i + 1);
  38. source = source.substring(0, i);
  39. }
  40. var object = (dijit.byId(source) || dojo.byId(source) || dojo.getObject(source));
  41. if(!object){
  42. return undefined; //undefined
  43. }
  44. if(!property){
  45. return object; //Object
  46. }else{
  47. return new dojox.wire.Wire({object: object, property: property}).getValue(); //anything
  48. }
  49. };
  50. dojox.wire.ml._setValue = function(/*String*/target, /*anything*/value){
  51. // summary:
  52. // Store a value
  53. // description:
  54. // This method stores a value by an ID of a widget or an DOM
  55. // element with a dotted notation to its property, using a Wire.
  56. // target:
  57. // A string to specify an object and its property
  58. // value:
  59. // A value
  60. if(!target){
  61. return; //undefined
  62. }
  63. var i = target.indexOf('.');
  64. if(i < 0){
  65. return; //undefined
  66. }
  67. var object = this._getValue(target.substring(0, i));
  68. if(!object){
  69. return; //undefined
  70. }
  71. var property = target.substring(i + 1);
  72. var wire = new dojox.wire.Wire({object: object, property: property}).setValue(value);
  73. };
  74. dojo.declare("dojox.wire.ml.XmlElement", null, {
  75. // summary:
  76. // An object wrapping an XML element
  77. // description:
  78. // This class represents an XML element.
  79. constructor: function(/*Element||String*/element){
  80. // summary:
  81. // Initialize with an XML element or a tag name
  82. // element:
  83. // An XML element or a tag name
  84. if(dojo.isString(element)){
  85. element = this._getDocument().createElement(element);
  86. }
  87. this.element = element;
  88. },
  89. getPropertyValue: function(/*String*/property){
  90. // summary:
  91. // Return a property value
  92. // description:
  93. // If 'property' starts with '@', the attribute value is returned.
  94. // If 'property' specifies "text()", the value of the first child
  95. // text is returned.
  96. // Otherwise, child elements of the tag name specified with
  97. // 'property' are returned.
  98. // property:
  99. // A property name
  100. // returns:
  101. // A property value
  102. var value = undefined;
  103. if(!this.element){
  104. return value; //undefined
  105. }
  106. if(!property){
  107. return value; //undefined
  108. }
  109. if(property.charAt(0) == '@'){
  110. var attribute = property.substring(1);
  111. value = this.element.getAttribute(attribute);
  112. }else if(property == "text()"){
  113. var text = this.element.firstChild;
  114. if(text){
  115. value = text.nodeValue;
  116. }
  117. }else{ // child elements
  118. var elements = [];
  119. for(var i = 0; i < this.element.childNodes.length; i++){
  120. var child = this.element.childNodes[i];
  121. if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == property){
  122. elements.push(new dojox.wire.ml.XmlElement(child));
  123. }
  124. }
  125. if(elements.length > 0){
  126. if(elements.length === 1){
  127. value = elements[0];
  128. }else{
  129. value = elements;
  130. }
  131. }
  132. }
  133. return value; //String||Array||XmlElement
  134. },
  135. setPropertyValue: function(/*String*/property, /*String||Array||XmlElement*/value){
  136. // summary:
  137. // Store a property value
  138. // description:
  139. // If 'property' starts with '@', 'value' is set to the attribute.
  140. // If 'property' specifies "text()", 'value' is set as the first
  141. // child text.
  142. // If 'value' is a string, a child element of the tag name
  143. // specified with 'property' is created and 'value' is set as
  144. // the first child text of the child element.
  145. // Otherwise, 'value' is set to as child elements.
  146. // property:
  147. // A property name
  148. // value:
  149. // A property value
  150. var i;
  151. var text;
  152. if(!this.element){
  153. return; //undefined
  154. }
  155. if(!property){
  156. return; //undefined
  157. }
  158. if(property.charAt(0) == '@'){
  159. var attribute = property.substring(1);
  160. if(value){
  161. this.element.setAttribute(attribute, value);
  162. }else{
  163. this.element.removeAttribute(attribute);
  164. }
  165. }else if(property == "text()"){
  166. while(this.element.firstChild){
  167. this.element.removeChild(this.element.firstChild);
  168. }
  169. if(value){
  170. text = this._getDocument().createTextNode(value);
  171. this.element.appendChild(text);
  172. }
  173. }else{ // child elements
  174. var nextChild = null;
  175. var child;
  176. for(i = this.element.childNodes.length - 1; i >= 0; i--){
  177. child = this.element.childNodes[i];
  178. if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == property){
  179. if(!nextChild){
  180. nextChild = child.nextSibling;
  181. }
  182. this.element.removeChild(child);
  183. }
  184. }
  185. if(value){
  186. if(dojo.isArray(value)){
  187. for(i in value){
  188. var e = value[i];
  189. if(e.element){
  190. this.element.insertBefore(e.element, nextChild);
  191. }
  192. }
  193. }else if(value instanceof dojox.wire.ml.XmlElement){
  194. if(value.element){
  195. this.element.insertBefore(value.element, nextChild);
  196. }
  197. }else{ // assume string
  198. child = this._getDocument().createElement(property);
  199. text = this._getDocument().createTextNode(value);
  200. child.appendChild(text);
  201. this.element.insertBefore(child, nextChild);
  202. }
  203. }
  204. }
  205. },
  206. toString: function(){
  207. // summary:
  208. // Return a value of the first text child of the element
  209. // description:
  210. // A value of the first text child of the element is returned.
  211. // returns:
  212. // A value of the first text child of the element
  213. var s = "";
  214. if(this.element){
  215. var text = this.element.firstChild;
  216. if(text){
  217. s = text.nodeValue;
  218. }
  219. }
  220. return s; //String
  221. },
  222. toObject: function(){
  223. // summary:
  224. // Return an object representation of the element
  225. // description:
  226. // An object with properties for child elements, attributes and
  227. // text is returned.
  228. // returns:
  229. // An object representation of the element
  230. if(!this.element){
  231. return null; //null
  232. }
  233. var text = "";
  234. var obj = {};
  235. var elements = 0;
  236. var i;
  237. for(i = 0; i < this.element.childNodes.length; i++){
  238. var child = this.element.childNodes[i];
  239. if(child.nodeType === 1 /* ELEMENT_NODE */){
  240. elements++;
  241. var o = new dojox.wire.ml.XmlElement(child).toObject();
  242. var name = child.nodeName;
  243. var p = obj[name];
  244. if(!p){
  245. obj[name] = o;
  246. }else if(dojo.isArray(p)){
  247. p.push(o);
  248. }else{
  249. obj[name] = [p, o]; // make them array
  250. }
  251. }else if(child.nodeType === 3 /* TEXT_NODE */ ||
  252. child.nodeType === 4 /* CDATA_SECTION_NODE */){
  253. text += child.nodeValue;
  254. }
  255. }
  256. var attributes = 0;
  257. if(this.element.nodeType === 1 /* ELEMENT_NODE */){
  258. attributes = this.element.attributes.length;
  259. for(i = 0; i < attributes; i++){
  260. var attr = this.element.attributes[i];
  261. obj["@" + attr.nodeName] = attr.nodeValue;
  262. }
  263. }
  264. if(elements === 0){
  265. if(attributes === 0){
  266. // text only
  267. return text; //String
  268. }
  269. // text with attributes
  270. obj["text()"] = text;
  271. }
  272. // else ignore text
  273. return obj; //Object
  274. },
  275. _getDocument: function(){
  276. // summary:
  277. // Return a DOM document
  278. // description:
  279. // If 'element' is specified, a DOM document of the element is
  280. // returned.
  281. // Otherwise, a DOM document is created.
  282. // returns:
  283. // A DOM document
  284. if(this.element){
  285. return (this.element.nodeType == 9 /* DOCUMENT_NODE */ ?
  286. this.element : this.element.ownerDocument); //Document
  287. }else{
  288. return dojox.xml.parser.parse(); //Document
  289. }
  290. }
  291. });
  292. }