You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
14 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-tree-TreeLoader-method-constructor'><span id='Ext-tree-TreeLoader'>/**
  19. </span></span> * @class Ext.tree.TreeLoader
  20. * @extends Ext.util.Observable
  21. * A TreeLoader provides for lazy loading of an {@link Ext.tree.TreeNode}'s child
  22. * nodes from a specified URL. The response must be a JavaScript Array definition
  23. * whose elements are node definition objects. e.g.:
  24. * &lt;pre&gt;&lt;code&gt;
  25. [{
  26. id: 1,
  27. text: 'A leaf Node',
  28. leaf: true
  29. },{
  30. id: 2,
  31. text: 'A folder Node',
  32. children: [{
  33. id: 3,
  34. text: 'A child Node',
  35. leaf: true
  36. }]
  37. }]
  38. &lt;/code&gt;&lt;/pre&gt;
  39. * &lt;br&gt;&lt;br&gt;
  40. * A server request is sent, and child nodes are loaded only when a node is expanded.
  41. * The loading node's id is passed to the server under the parameter name &quot;node&quot; to
  42. * enable the server to produce the correct child nodes.
  43. * &lt;br&gt;&lt;br&gt;
  44. * To pass extra parameters, an event handler may be attached to the &quot;beforeload&quot;
  45. * event, and the parameters specified in the TreeLoader's baseParams property:
  46. * &lt;pre&gt;&lt;code&gt;
  47. myTreeLoader.on(&quot;beforeload&quot;, function(treeLoader, node) {
  48. this.baseParams.category = node.attributes.category;
  49. }, this);
  50. &lt;/code&gt;&lt;/pre&gt;
  51. * This would pass an HTTP parameter called &quot;category&quot; to the server containing
  52. * the value of the Node's &quot;category&quot; attribute.
  53. * @constructor
  54. * Creates a new Treeloader.
  55. * @param {Object} config A config object containing config properties.
  56. */
  57. Ext.tree.TreeLoader = function(config){
  58. this.baseParams = {};
  59. Ext.apply(this, config);
  60. this.addEvents(
  61. <span id='Ext-tree-TreeLoader-event-beforeload'> /**
  62. </span> * @event beforeload
  63. * Fires before a network request is made to retrieve the Json text which specifies a node's children.
  64. * @param {Object} This TreeLoader object.
  65. * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
  66. * @param {Object} callback The callback function specified in the {@link #load} call.
  67. */
  68. &quot;beforeload&quot;,
  69. <span id='Ext-tree-TreeLoader-event-load'> /**
  70. </span> * @event load
  71. * Fires when the node has been successfuly loaded.
  72. * @param {Object} This TreeLoader object.
  73. * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
  74. * @param {Object} response The response object containing the data from the server.
  75. */
  76. &quot;load&quot;,
  77. <span id='Ext-tree-TreeLoader-event-loadexception'> /**
  78. </span> * @event loadexception
  79. * Fires if the network request failed.
  80. * @param {Object} This TreeLoader object.
  81. * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
  82. * @param {Object} response The response object containing the data from the server.
  83. */
  84. &quot;loadexception&quot;
  85. );
  86. Ext.tree.TreeLoader.superclass.constructor.call(this);
  87. if(Ext.isString(this.paramOrder)){
  88. this.paramOrder = this.paramOrder.split(/[\s,|]/);
  89. }
  90. };
  91. Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
  92. <span id='Ext-tree-TreeLoader-cfg-dataUrl'> /**
  93. </span> * @cfg {String} dataUrl The URL from which to request a Json string which
  94. * specifies an array of node definition objects representing the child nodes
  95. * to be loaded.
  96. */
  97. <span id='Ext-tree-TreeLoader-cfg-requestMethod'> /**
  98. </span> * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
  99. */
  100. <span id='Ext-tree-TreeLoader-cfg-url'> /**
  101. </span> * @cfg {String} url Equivalent to {@link #dataUrl}.
  102. */
  103. <span id='Ext-tree-TreeLoader-cfg-preloadChildren'> /**
  104. </span> * @cfg {Boolean} preloadChildren If set to true, the loader recursively loads &quot;children&quot; attributes when doing the first load on nodes.
  105. */
  106. <span id='Ext-tree-TreeLoader-cfg-baseParams'> /**
  107. </span> * @cfg {Object} baseParams (optional) An object containing properties which
  108. * specify HTTP parameters to be passed to each request for child nodes.
  109. */
  110. <span id='Ext-tree-TreeLoader-cfg-baseAttrs'> /**
  111. </span> * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
  112. * created by this loader. If the attributes sent by the server have an attribute in this object,
  113. * they take priority.
  114. */
  115. <span id='Ext-tree-TreeLoader-cfg-uiProviders'> /**
  116. </span> * @cfg {Object} uiProviders (optional) An object containing properties which
  117. * specify custom {@link Ext.tree.TreeNodeUI} implementations. If the optional
  118. * &lt;i&gt;uiProvider&lt;/i&gt; attribute of a returned child node is a string rather
  119. * than a reference to a TreeNodeUI implementation, then that string value
  120. * is used as a property name in the uiProviders object.
  121. */
  122. uiProviders : {},
  123. <span id='Ext-tree-TreeLoader-cfg-clearOnLoad'> /**
  124. </span> * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
  125. * child nodes before loading.
  126. */
  127. clearOnLoad : true,
  128. <span id='Ext-tree-TreeLoader-cfg-paramOrder'> /**
  129. </span> * @cfg {Array/String} paramOrder Defaults to &lt;tt&gt;undefined&lt;/tt&gt;. Only used when using directFn.
  130. * Specifies the params in the order in which they must be passed to the server-side Direct method
  131. * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
  132. * comma, or pipe. For example,
  133. * any of the following would be acceptable:&lt;pre&gt;&lt;code&gt;
  134. nodeParameter: 'node',
  135. paramOrder: ['param1','param2','param3']
  136. paramOrder: 'node param1 param2 param3'
  137. paramOrder: 'param1,node,param2,param3'
  138. paramOrder: 'param1|param2|param|node'
  139. &lt;/code&gt;&lt;/pre&gt;
  140. */
  141. paramOrder: undefined,
  142. <span id='Ext-tree-TreeLoader-cfg-paramsAsHash'> /**
  143. </span> * @cfg {Boolean} paramsAsHash Only used when using directFn.
  144. * Send parameters as a collection of named arguments (defaults to &lt;tt&gt;false&lt;/tt&gt;). Providing a
  145. * &lt;tt&gt;{@link #paramOrder}&lt;/tt&gt; nullifies this configuration.
  146. */
  147. paramsAsHash: false,
  148. <span id='Ext-tree-TreeLoader-cfg-nodeParameter'> /**
  149. </span> * @cfg {String} nodeParameter The name of the parameter sent to the server which contains
  150. * the identifier of the node. Defaults to &lt;tt&gt;'node'&lt;/tt&gt;.
  151. */
  152. nodeParameter: 'node',
  153. <span id='Ext-tree-TreeLoader-cfg-directFn'> /**
  154. </span> * @cfg {Function} directFn
  155. * Function to call when executing a request.
  156. */
  157. directFn : undefined,
  158. <span id='Ext-tree-TreeLoader-method-load'> /**
  159. </span> * Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor.
  160. * This is called automatically when a node is expanded, but may be used to reload
  161. * a node (or append new children if the {@link #clearOnLoad} option is false.)
  162. * @param {Ext.tree.TreeNode} node
  163. * @param {Function} callback Function to call after the node has been loaded. The
  164. * function is passed the TreeNode which was requested to be loaded.
  165. * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the callback is executed.
  166. * defaults to the loaded TreeNode.
  167. */
  168. load : function(node, callback, scope){
  169. if(this.clearOnLoad){
  170. while(node.firstChild){
  171. node.removeChild(node.firstChild);
  172. }
  173. }
  174. if(this.doPreload(node)){ // preloaded json children
  175. this.runCallback(callback, scope || node, [node]);
  176. }else if(this.directFn || this.dataUrl || this.url){
  177. this.requestData(node, callback, scope || node);
  178. }
  179. },
  180. doPreload : function(node){
  181. if(node.attributes.children){
  182. if(node.childNodes.length &lt; 1){ // preloaded?
  183. var cs = node.attributes.children;
  184. node.beginUpdate();
  185. for(var i = 0, len = cs.length; i &lt; len; i++){
  186. var cn = node.appendChild(this.createNode(cs[i]));
  187. if(this.preloadChildren){
  188. this.doPreload(cn);
  189. }
  190. }
  191. node.endUpdate();
  192. }
  193. return true;
  194. }
  195. return false;
  196. },
  197. getParams: function(node){
  198. var bp = Ext.apply({}, this.baseParams),
  199. np = this.nodeParameter,
  200. po = this.paramOrder;
  201. np &amp;&amp; (bp[ np ] = node.id);
  202. if(this.directFn){
  203. var buf = [node.id];
  204. if(po){
  205. // reset 'buf' if the nodeParameter was included in paramOrder
  206. if(np &amp;&amp; po.indexOf(np) &gt; -1){
  207. buf = [];
  208. }
  209. for(var i = 0, len = po.length; i &lt; len; i++){
  210. buf.push(bp[ po[i] ]);
  211. }
  212. }else if(this.paramsAsHash){
  213. buf = [bp];
  214. }
  215. return buf;
  216. }else{
  217. return bp;
  218. }
  219. },
  220. requestData : function(node, callback, scope){
  221. if(this.fireEvent(&quot;beforeload&quot;, this, node, callback) !== false){
  222. if(this.directFn){
  223. var args = this.getParams(node);
  224. args.push(this.processDirectResponse.createDelegate(this, [{callback: callback, node: node, scope: scope}], true));
  225. this.directFn.apply(window, args);
  226. }else{
  227. this.transId = Ext.Ajax.request({
  228. method:this.requestMethod,
  229. url: this.dataUrl||this.url,
  230. success: this.handleResponse,
  231. failure: this.handleFailure,
  232. scope: this,
  233. argument: {callback: callback, node: node, scope: scope},
  234. params: this.getParams(node)
  235. });
  236. }
  237. }else{
  238. // if the load is cancelled, make sure we notify
  239. // the node that we are done
  240. this.runCallback(callback, scope || node, []);
  241. }
  242. },
  243. processDirectResponse: function(result, response, args){
  244. if(response.status){
  245. this.handleResponse({
  246. responseData: Ext.isArray(result) ? result : null,
  247. responseText: result,
  248. argument: args
  249. });
  250. }else{
  251. this.handleFailure({
  252. argument: args
  253. });
  254. }
  255. },
  256. // private
  257. runCallback: function(cb, scope, args){
  258. if(Ext.isFunction(cb)){
  259. cb.apply(scope, args);
  260. }
  261. },
  262. isLoading : function(){
  263. return !!this.transId;
  264. },
  265. abort : function(){
  266. if(this.isLoading()){
  267. Ext.Ajax.abort(this.transId);
  268. }
  269. },
  270. <span id='Ext-tree-TreeLoader-method-createNode'> /**
  271. </span> * &lt;p&gt;Override this function for custom TreeNode node implementation, or to
  272. * modify the attributes at creation time.&lt;/p&gt;
  273. * Example:&lt;pre&gt;&lt;code&gt;
  274. new Ext.tree.TreePanel({
  275. ...
  276. loader: new Ext.tree.TreeLoader({
  277. url: 'dataUrl',
  278. createNode: function(attr) {
  279. // Allow consolidation consignments to have
  280. // consignments dropped into them.
  281. if (attr.isConsolidation) {
  282. attr.iconCls = 'x-consol',
  283. attr.allowDrop = true;
  284. }
  285. return Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
  286. }
  287. }),
  288. ...
  289. });
  290. &lt;/code&gt;&lt;/pre&gt;
  291. * @param attr {Object} The attributes from which to create the new node.
  292. */
  293. createNode : function(attr){
  294. // apply baseAttrs, nice idea Corey!
  295. if(this.baseAttrs){
  296. Ext.applyIf(attr, this.baseAttrs);
  297. }
  298. if(this.applyLoader !== false &amp;&amp; !attr.loader){
  299. attr.loader = this;
  300. }
  301. if(Ext.isString(attr.uiProvider)){
  302. attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
  303. }
  304. if(attr.nodeType){
  305. return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr);
  306. }else{
  307. return attr.leaf ?
  308. new Ext.tree.TreeNode(attr) :
  309. new Ext.tree.AsyncTreeNode(attr);
  310. }
  311. },
  312. processResponse : function(response, node, callback, scope){
  313. var json = response.responseText;
  314. try {
  315. var o = response.responseData || Ext.decode(json);
  316. node.beginUpdate();
  317. for(var i = 0, len = o.length; i &lt; len; i++){
  318. var n = this.createNode(o[i]);
  319. if(n){
  320. node.appendChild(n);
  321. }
  322. }
  323. node.endUpdate();
  324. this.runCallback(callback, scope || node, [node]);
  325. }catch(e){
  326. this.handleFailure(response);
  327. }
  328. },
  329. handleResponse : function(response){
  330. this.transId = false;
  331. var a = response.argument;
  332. this.processResponse(response, a.node, a.callback, a.scope);
  333. this.fireEvent(&quot;load&quot;, this, a.node, response);
  334. },
  335. handleFailure : function(response){
  336. this.transId = false;
  337. var a = response.argument;
  338. this.fireEvent(&quot;loadexception&quot;, this, a.node, response);
  339. this.runCallback(a.callback, a.scope || a.node, [a.node]);
  340. },
  341. destroy : function(){
  342. this.abort();
  343. this.purgeListeners();
  344. }
  345. });</pre>
  346. </body>
  347. </html>