d0f6062ca5f9d3ff9d4a74215c24fc3ee36012dd.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.tree.TreeLoader
  8. * @extends Ext.util.Observable A TreeLoader provides for lazy loading of an
  9. * {@link Ext.tree.TreeNode}'s child nodes from a specified URL. The
  10. * response must be a JavaScript Array definition whose elements are
  11. * node definition objects. eg:
  12. *
  13. * <pre><code>
  14. * [{
  15. * id : 1,
  16. * text : 'A leaf Node',
  17. * leaf : true
  18. * }, {
  19. * id : 2,
  20. * text : 'A folder Node',
  21. * children : [{
  22. * id : 3,
  23. * text : 'A child Node',
  24. * leaf : true
  25. * }]
  26. * }]
  27. * </code></pre>
  28. *
  29. * <br>
  30. * <br>
  31. * A server request is sent, and child nodes are loaded only when a
  32. * node is expanded. The loading node's id is passed to the server
  33. * under the parameter name "node" to enable the server to produce the
  34. * correct child nodes. <br>
  35. * <br>
  36. * To pass extra parameters, an event handler may be attached to the
  37. * "beforeload" event, and the parameters specified in the TreeLoader's
  38. * baseParams property:
  39. *
  40. * <pre><code>
  41. * myTreeLoader.on(&quot;beforeload&quot;, function(treeLoader, node) {
  42. * this.baseParams.category = node.attributes.category;
  43. * }, this);
  44. * </code></pre>< This would pass an HTTP parameter called "category" to the
  45. * server containing the value of the Node's "category" attribute.
  46. * @constructor Creates a new Treeloader.
  47. * @param {Object}
  48. * config A config object containing config properties.
  49. */
  50. Ext.tree.TreeLoader = function(config) {
  51. this.baseParams = {};
  52. this.requestMethod = "POST";
  53. Ext.apply(this, config);
  54. this.addEvents(
  55. /**
  56. * @event beforeload Fires before a network request is made to
  57. * retrieve the Json text which specifies a node's children.
  58. * @param {Object}
  59. * This TreeLoader object.
  60. * @param {Object}
  61. * node The {@link Ext.tree.TreeNode} object being
  62. * loaded.
  63. * @param {Object}
  64. * callback The callback function specified in the
  65. * {@link #load} call.
  66. */
  67. "beforeload",
  68. /**
  69. * @event load Fires when the node has been successfuly loaded.
  70. * @param {Object}
  71. * This TreeLoader object.
  72. * @param {Object}
  73. * node The {@link Ext.tree.TreeNode} object being
  74. * loaded.
  75. * @param {Object}
  76. * response The response object containing the data from
  77. * the server.
  78. */
  79. "load",
  80. /**
  81. * @event loadexception Fires if the network request failed.
  82. * @param {Object}
  83. * This TreeLoader object.
  84. * @param {Object}
  85. * node The {@link Ext.tree.TreeNode} object being
  86. * loaded.
  87. * @param {Object}
  88. * response The response object containing the data from
  89. * the server.
  90. */
  91. "loadexception");
  92. Ext.tree.TreeLoader.superclass.constructor.call(this);
  93. };
  94. Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
  95. /**
  96. * @cfg {String} dataUrl The URL from which to request a Json string
  97. * which specifies an array of node definition objects
  98. * representing the child nodes to be loaded.
  99. */
  100. /**
  101. * @cfg {String} requestMethod The HTTP request method for loading
  102. * data (defaults to 'POST').
  103. */
  104. /**
  105. * @cfg {String} url Equivalent to {@link #dataUrl}.
  106. */
  107. /**
  108. * @cfg {Boolean} preloadChildren If set to true, the loader
  109. * recursively loads "children" attributes when doing the first
  110. * load on nodes.
  111. */
  112. /**
  113. * @cfg {Object} baseParams (optional) An object containing
  114. * properties which specify HTTP parameters to be passed to
  115. * each request for child nodes.
  116. */
  117. /**
  118. * @cfg {Object} baseAttrs (optional) An object containing
  119. * attributes to be added to all nodes created by this loader.
  120. * If the attributes sent by the server have an attribute in
  121. * this object, they take priority.
  122. */
  123. /**
  124. * @cfg {Object} uiProviders (optional) An object containing
  125. * properties which specify custom {@link Ext.tree.TreeNodeUI}
  126. * implementations. If the optional <i>uiProvider</i>
  127. * attribute of a returned child node is a string rather than a
  128. * reference to a TreeNodeUI implementation, this that string
  129. * value is used as a property name in the uiProviders object.
  130. */
  131. uiProviders : {},
  132. /**
  133. * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove
  134. * previously existing child nodes before loading.
  135. */
  136. clearOnLoad : true,
  137. /**
  138. * Load an {@link Ext.tree.TreeNode} from the URL specified in the
  139. * constructor. This is called automatically when a node is
  140. * expanded, but may be used to reload a node (or append new
  141. * children if the {@link #clearOnLoad} option is false.)
  142. *
  143. * @param {Ext.tree.TreeNode}
  144. * node
  145. * @param {Function}
  146. * callback
  147. */
  148. load : function(node, callback) {
  149. if (this.clearOnLoad) {
  150. while (node.firstChild) {
  151. node.removeChild(node.firstChild);
  152. }
  153. }
  154. if (this.doPreload(node)) { // preloaded json children
  155. if (typeof callback == "function") {
  156. callback();
  157. }
  158. } else if (this.dataUrl || this.url) {
  159. this.requestData(node, callback);
  160. }
  161. },
  162. doPreload : function(node) {
  163. if (node.attributes.children) {
  164. if (node.childNodes.length < 1) { // preloaded?
  165. var cs = node.attributes.children;
  166. node.beginUpdate();
  167. for (var i = 0, len = cs.length; i < len; i++) {
  168. var cn = node.appendChild(this.createNode(cs[i]));
  169. if (this.preloadChildren) {
  170. this.doPreload(cn);
  171. }
  172. }
  173. node.endUpdate();
  174. }
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. },
  180. getParams : function(node) {
  181. var buf = [], bp = this.baseParams;
  182. for (var key in bp) {
  183. if (typeof bp[key] != "function") {
  184. buf.push(encodeURIComponent(key), "=",
  185. encodeURIComponent(bp[key]), "&");
  186. }
  187. }
  188. buf.push("node=", encodeURIComponent(node.id));
  189. return buf.join("");
  190. },
  191. requestData : function(node, callback) {
  192. if (this.fireEvent("beforeload", this, node, callback) !== false) {
  193. this.transId = Ext.Ajax.request({
  194. method : this.requestMethod,
  195. url : this.dataUrl || this.url,
  196. success : this.handleResponse,
  197. failure : this.handleFailure,
  198. scope : this,
  199. argument : {
  200. callback : callback,
  201. node : node
  202. },
  203. params : this.getParams(node)
  204. });
  205. } else {
  206. // if the load is cancelled, make sure we notify
  207. // the node that we are done
  208. if (typeof callback == "function") {
  209. callback();
  210. }
  211. }
  212. },
  213. isLoading : function() {
  214. return this.transId ? true : false;
  215. },
  216. abort : function() {
  217. if (this.isLoading()) {
  218. Ext.Ajax.abort(this.transId);
  219. }
  220. },
  221. /**
  222. * Override this function for custom TreeNode node implementation
  223. */
  224. createNode : function(attr) {
  225. // apply baseAttrs, nice idea Corey!
  226. if (this.baseAttrs) {
  227. Ext.applyIf(attr, this.baseAttrs);
  228. }
  229. if (this.applyLoader !== false) {
  230. attr.loader = this;
  231. }
  232. if (typeof attr.uiProvider == 'string') {
  233. attr.uiProvider = this.uiProviders[attr.uiProvider]
  234. || eval(attr.uiProvider);
  235. }
  236. return (attr.leaf
  237. ? new Ext.tree.TreeNode(attr)
  238. : new Ext.tree.AsyncTreeNode(attr));
  239. },
  240. processResponse : function(response, node, callback) {
  241. var json = response.responseText;
  242. try {
  243. var o = eval("(" + json + ")");
  244. node.beginUpdate();
  245. for (var i = 0, len = o.length; i < len; i++) {
  246. var n = this.createNode(o[i]);
  247. if (n) {
  248. node.appendChild(n);
  249. }
  250. }
  251. node.endUpdate();
  252. if (typeof callback == "function") {
  253. callback(this, node);
  254. }
  255. } catch (e) {
  256. this.handleFailure(response);
  257. }
  258. },
  259. handleResponse : function(response) {
  260. this.transId = false;
  261. var a = response.argument;
  262. this.processResponse(response, a.node, a.callback);
  263. this.fireEvent("load", this, a.node, response);
  264. },
  265. handleFailure : function(response) {
  266. this.transId = false;
  267. var a = response.argument;
  268. this.fireEvent("loadexception", this, a.node, response);
  269. if (typeof a.callback == "function") {
  270. a.callback(this, a.node);
  271. }
  272. }
  273. });