util.js 8.0 KB

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