TreeAdapter.js 5.7 KB

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