TreeAdapter.js 6.2 KB

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