TreeAdapter.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // wrapped by build app
  2. define("dojox/wire/TreeAdapter", ["dijit","dojo","dojox","dojo/require!dojox/wire/CompositeWire"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.wire.TreeAdapter");
  4. dojo.require("dojox.wire.CompositeWire");
  5. dojo.declare("dojox.wire.TreeAdapter", dojox.wire.CompositeWire, {
  6. // summary:
  7. // A composite Wire for tree nodes
  8. // description:
  9. // This class has multiple child Wires for tree nodes, their title and
  10. // child nodes.
  11. // The root object for this class must be an array.
  12. // 'node' Wires in 'nodes' property is used to identify an object
  13. // representing a node.
  14. // 'title' Wires in 'nodes' property is used to get the title string
  15. // of a node.
  16. // 'children' Wires in 'nodes' property is used to iterate over child
  17. // node objects.
  18. // The node values are returned in an array as follows:
  19. // [
  20. // {title: title1,
  21. // children: [
  22. // {title: title2,
  23. // child: ...},
  24. // {title: title3,
  25. // child: ...},
  26. // ...
  27. // ]},
  28. // ...
  29. // ]
  30. // This class only supports getValue(), but not setValue().
  31. _wireClass: "dojox.wire.TreeAdapter",
  32. constructor: function(/*Object*/args){
  33. // summary:
  34. // Initialize properties
  35. // description:
  36. // If object properties ('node', 'title' and 'children') of array
  37. // elements specified in 'nodes' property are not Wires, Wires are
  38. // created from them as arguments, with 'parent' property set to
  39. // this Wire instance.
  40. // args:
  41. // Arguments to initialize properties
  42. // nodes:
  43. // An array containing objects for child Wires for node values
  44. this._initializeChildren(this.nodes);
  45. },
  46. _getValue: function(/*Array*/object){
  47. // summary:
  48. // Return an array of tree node values
  49. // description:
  50. // This method iterates over an array specified to 'object'
  51. // argument and calls getValue() method of 'node' Wires with each
  52. // element of the array to get object(s) that represetns nodes.
  53. // (If 'node' Wires are omitted, the array element is used for
  54. // further processing.)
  55. // Then, getValue() method of 'title' Wires are called to get
  56. // title strings for nodes.
  57. // (If 'title' Wires are omitted, the objects representing nodes
  58. // are used as title strings.)
  59. // And if an array of objects with 'node' and 'title' Wires is
  60. // specified to 'children', it is used to gather child nodes and
  61. // their title strings in the same way recursively.
  62. // Finally, an array of the top-level node objects are retuned.
  63. // object:
  64. // A root array
  65. // returns:
  66. // An array of tree node values
  67. if(!object || !this.nodes){
  68. return object; //Array
  69. }
  70. var array = object;
  71. if(!dojo.isArray(array)){
  72. array = [array];
  73. }
  74. var nodes = [];
  75. for(var i in array){
  76. for(var i2 in this.nodes){
  77. nodes = nodes.concat(this._getNodes(array[i], this.nodes[i2]));
  78. }
  79. }
  80. return nodes; //Array
  81. },
  82. _setValue: function(/*Array*/object, /*Array*/value){
  83. // summary:
  84. // Not supported
  85. throw new Error("Unsupported API: " + this._wireClass + "._setValue");
  86. },
  87. _initializeChildren: function(/*Array*/children){
  88. // summary:
  89. // Initialize child Wires
  90. // description:
  91. // If 'node' or 'title' properties of array elements specified in
  92. // 'children' argument are not Wires, Wires are created from them
  93. // as arguments, with 'parent' property set to this Wire instance.
  94. // If an array element has 'children' property, this method is
  95. // called recursively with it.
  96. // children:
  97. // An array of objects containing child Wires
  98. if(!children){
  99. return; //undefined
  100. }
  101. for(var i in children){
  102. var child = children[i];
  103. if(child.node){
  104. child.node.parent = this;
  105. if(!dojox.wire.isWire(child.node)){
  106. child.node = dojox.wire.create(child.node);
  107. }
  108. }
  109. if(child.title){
  110. child.title.parent = this;
  111. if(!dojox.wire.isWire(child.title)){
  112. child.title = dojox.wire.create(child.title);
  113. }
  114. }
  115. if(child.children){
  116. this._initializeChildren(child.children);
  117. }
  118. }
  119. },
  120. _getNodes: function(/*Object*/object, /*Object*/child){
  121. // summary:
  122. // Return an array of tree node values
  123. // description:
  124. // This method calls getValue() method of 'node' Wires with
  125. // 'object' argument to get object(s) that represents nodes.
  126. // (If 'node' Wires are omitted, 'object' is used for further
  127. // processing.)
  128. // Then, getValue() method of 'title' Wires are called to get
  129. // title strings for nodes.
  130. // (If 'title' Wires are omitted, the objects representing nodes
  131. // are used as title strings.)
  132. // And if an array of objects with 'node' and 'title' Wires is
  133. // specified to 'children', it is used to gather child nodes and
  134. // their title strings in the same way recursively.
  135. // Finally, an array of node objects are returned.
  136. // object:
  137. // An object
  138. // child:
  139. // An object with child Wires
  140. // returns:
  141. var array = null;
  142. if(child.node){
  143. array = child.node.getValue(object);
  144. if(!array){
  145. return [];
  146. }
  147. if(!dojo.isArray(array)){
  148. array = [array];
  149. }
  150. }else{
  151. array = [object];
  152. }
  153. var nodes = [];
  154. for(var i in array){
  155. object = array[i];
  156. var node = {};
  157. if(child.title){
  158. node.title = child.title.getValue(object);
  159. }else{
  160. node.title = object;
  161. }
  162. if(child.children){
  163. var children = [];
  164. for(var i2 in child.children){
  165. children = children.concat(this._getNodes(object, child.children[i2]));
  166. }
  167. if(children.length > 0){
  168. node.children = children;
  169. }
  170. }
  171. nodes.push(node);
  172. }
  173. return nodes; //Array
  174. }
  175. });
  176. });