4fbb9e60ade7e1f90702446187bd5e835ad3cc27.svn-base 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.JsonView
  8. * @extends Ext.View Shortcut class to create a JSON + {@link Ext.Updater}
  9. * template view. Usage:
  10. *
  11. * <pre><code>
  12. * var view = new Ext.JsonView(&quot;my-element&quot;,
  13. * '&lt;div id=&quot;{id}&quot;&gt;{foo} - {bar}&lt;/div&gt;', // auto create template
  14. * {
  15. * multiSelect : true,
  16. * jsonRoot : &quot;data&quot;
  17. * });
  18. *
  19. * // listen for node click?
  20. * view.on(&quot;click&quot;, function(vw, index, node, e) {
  21. * alert('Node &quot;' + node.id + '&quot; at index: ' + index + &quot; was clicked.&quot;);
  22. * });
  23. *
  24. * // direct load of JSON data
  25. * view.load(&quot;foobar.php&quot;);
  26. *
  27. * // Example from my blog list
  28. * var tpl = new Ext.Template('&lt;div class=&quot;entry&quot;&gt;'
  29. * + '&lt;a class=&quot;entry-title&quot; href=&quot;{link}&quot;&gt;{title}&lt;/a&gt;'
  30. * + &quot;&lt;h4&gt;{date} by {author} | {comments} Comments&lt;/h4&gt;{description}&quot;
  31. * + &quot;&lt;/div&gt;&lt;hr /&gt;&quot;);
  32. *
  33. * var moreView = new Ext.JsonView(&quot;entry-list&quot;, tpl, {
  34. * jsonRoot : &quot;posts&quot;
  35. * });
  36. * moreView.on(&quot;beforerender&quot;, this.sortEntries, this);
  37. * moreView.load({
  38. * url : &quot;/blog/get-posts.php&quot;,
  39. * params : &quot;allposts=true&quot;,
  40. * text : &quot;Loading Blog Entries...&quot;
  41. * });
  42. * </code></pre>
  43. *
  44. * @constructor Create a new JsonView
  45. * @param {Mixed}
  46. * container The container element where the view is to be rendered.
  47. * @param {Template}
  48. * tpl The rendering template
  49. * @param {Object}
  50. * config The config object
  51. */
  52. Ext.JsonView = function(container, tpl, config) {
  53. Ext.JsonView.superclass.constructor.call(this, container, tpl, config);
  54. var um = this.el.getUpdater();
  55. um.setRenderer(this);
  56. um.on("update", this.onLoad, this);
  57. um.on("failure", this.onLoadException, this);
  58. /**
  59. * @event beforerender Fires before rendering of the downloaded JSON data.
  60. * @param {Ext.JsonView}
  61. * this
  62. * @param {Object}
  63. * data The JSON data loaded
  64. */
  65. /**
  66. * @event load Fires when data is loaded.
  67. * @param {Ext.JsonView}
  68. * this
  69. * @param {Object}
  70. * data The JSON data loaded
  71. * @param {Object}
  72. * response The raw Connect response object
  73. */
  74. /**
  75. * @event loadexception Fires when loading fails.
  76. * @param {Ext.JsonView}
  77. * this
  78. * @param {Object}
  79. * response The raw Connect response object
  80. */
  81. this.addEvents({
  82. 'beforerender' : true,
  83. 'load' : true,
  84. 'loadexception' : true
  85. });
  86. };
  87. Ext.extend(Ext.JsonView, Ext.View, {
  88. /**
  89. * The root property in the loaded JSON object that contains the
  90. * data
  91. *
  92. * @type {String}
  93. */
  94. jsonRoot : "",
  95. /**
  96. * Refreshes the view.
  97. */
  98. refresh : function() {
  99. this.clearSelections();
  100. this.el.update("");
  101. var html = [];
  102. var o = this.jsonData;
  103. if (o && o.length > 0) {
  104. for (var i = 0, len = o.length; i < len; i++) {
  105. var data = this.prepareData(o[i], i, o);
  106. html[html.length] = this.tpl.apply(data);
  107. }
  108. } else {
  109. html.push(this.emptyText);
  110. }
  111. this.el.update(html.join(""));
  112. this.nodes = this.el.dom.childNodes;
  113. this.updateIndexes(0);
  114. },
  115. /**
  116. * Performs an async HTTP request, and loads the JSON from the
  117. * response. If <i>params</i> are specified it uses POST, otherwise
  118. * it uses GET.
  119. *
  120. * @param {Object/String/Function}
  121. * url The URL for this request, or a function to call to
  122. * get the URL, or a config object containing any of the
  123. * following options:
  124. *
  125. * <pre><code>
  126. * view.load({
  127. * url : &quot;your-url.php&quot;,
  128. * params : {
  129. * param1 : &quot;foo&quot;,
  130. * param2 : &quot;bar&quot;
  131. * }, // or a URL encoded string
  132. * callback : yourFunction,
  133. * scope : yourObject, //(optional scope)
  134. * discardUrl : false,
  135. * nocache : false,
  136. * text : &quot;Loading...&quot;,
  137. * timeout : 30,
  138. * scripts : false
  139. * });
  140. * </code></pre>
  141. *
  142. * The only required property is <i>url</i>. The
  143. * optional properties <i>nocache</i>, <i>text</i> and
  144. * <i>scripts</i> are shorthand for <i>disableCaching</i>,
  145. * <i>indicatorText</i> and <i>loadScripts</i> and are
  146. * used to set their associated property on this Updater
  147. * instance.
  148. * @param {String/Object}
  149. * params (optional) The parameters to pass, as either a
  150. * URL encoded string "param1=1&amp;param2=2" or an
  151. * object {param1: 1, param2: 2}
  152. * @param {Function}
  153. * callback (optional) Callback when transaction is
  154. * complete - called with signature (oElement, bSuccess)
  155. * @param {Boolean}
  156. * discardUrl (optional) By default when you execute an
  157. * update the defaultUrl is changed to the last used URL.
  158. * If true, it will not store the URL.
  159. */
  160. load : function() {
  161. var um = this.el.getUpdater();
  162. um.update.apply(um, arguments);
  163. },
  164. render : function(el, response) {
  165. this.clearSelections();
  166. this.el.update("");
  167. var o;
  168. try {
  169. o = Ext.util.JSON.decode(response.responseText);
  170. if (this.jsonRoot) {
  171. o = eval("o." + this.jsonRoot);
  172. }
  173. } catch (e) {
  174. }
  175. /**
  176. * The current JSON data or null
  177. */
  178. this.jsonData = o;
  179. this.beforeRender();
  180. this.refresh();
  181. },
  182. /**
  183. * Get the number of records in the current JSON dataset
  184. *
  185. * @return {Number}
  186. */
  187. getCount : function() {
  188. return this.jsonData ? this.jsonData.length : 0;
  189. },
  190. /**
  191. * Returns the JSON object for the specified node(s)
  192. *
  193. * @param {HTMLElement/Array}
  194. * node The node or an array of nodes
  195. * @return {Object/Array} If you pass in an array, you get an array
  196. * back, otherwise you get the JSON object for the node
  197. */
  198. getNodeData : function(node) {
  199. if (node instanceof Array) {
  200. var data = [];
  201. for (var i = 0, len = node.length; i < len; i++) {
  202. data.push(this.getNodeData(node[i]));
  203. }
  204. return data;
  205. }
  206. return this.jsonData[this.indexOf(node)] || null;
  207. },
  208. beforeRender : function() {
  209. this.snapshot = this.jsonData;
  210. if (this.sortInfo) {
  211. this.sort.apply(this, this.sortInfo);
  212. }
  213. this.fireEvent("beforerender", this, this.jsonData);
  214. },
  215. onLoad : function(el, o) {
  216. this.fireEvent("load", this, this.jsonData, o);
  217. },
  218. onLoadException : function(el, o) {
  219. this.fireEvent("loadexception", this, o);
  220. },
  221. /**
  222. * Filter the data by a specific property.
  223. *
  224. * @param {String}
  225. * property A property on your JSON objects
  226. * @param {String/RegExp}
  227. * value Either string that the property values should
  228. * start with, or a RegExp to test against the property
  229. */
  230. filter : function(property, value) {
  231. if (this.jsonData) {
  232. var data = [];
  233. var ss = this.snapshot;
  234. if (typeof value == "string") {
  235. var vlen = value.length;
  236. if (vlen == 0) {
  237. this.clearFilter();
  238. return;
  239. }
  240. value = value.toLowerCase();
  241. for (var i = 0, len = ss.length; i < len; i++) {
  242. var o = ss[i];
  243. if (o[property].substr(0, vlen).toLowerCase() == value) {
  244. data.push(o);
  245. }
  246. }
  247. } else if (value.exec) { // regex?
  248. for (var i = 0, len = ss.length; i < len; i++) {
  249. var o = ss[i];
  250. if (value.test(o[property])) {
  251. data.push(o);
  252. }
  253. }
  254. } else {
  255. return;
  256. }
  257. this.jsonData = data;
  258. this.refresh();
  259. }
  260. },
  261. /**
  262. * Filter by a function. The passed function will be called with
  263. * each object in the current dataset. If the function returns true
  264. * the value is kept, otherwise it is filtered.
  265. *
  266. * @param {Function}
  267. * fn
  268. * @param {Object}
  269. * scope (optional) The scope of the function (defaults
  270. * to this JsonView)
  271. */
  272. filterBy : function(fn, scope) {
  273. if (this.jsonData) {
  274. var data = [];
  275. var ss = this.snapshot;
  276. for (var i = 0, len = ss.length; i < len; i++) {
  277. var o = ss[i];
  278. if (fn.call(scope || this, o)) {
  279. data.push(o);
  280. }
  281. }
  282. this.jsonData = data;
  283. this.refresh();
  284. }
  285. },
  286. /**
  287. * Clears the current filter.
  288. */
  289. clearFilter : function() {
  290. if (this.snapshot && this.jsonData != this.snapshot) {
  291. this.jsonData = this.snapshot;
  292. this.refresh();
  293. }
  294. },
  295. /**
  296. * Sorts the data for this view and refreshes it.
  297. *
  298. * @param {String}
  299. * property A property on your JSON objects to sort on
  300. * @param {String}
  301. * direction (optional) "desc" or "asc" (defaults to
  302. * "asc")
  303. * @param {Function}
  304. * sortType (optional) A function to call to convert the
  305. * data to a sortable value.
  306. */
  307. sort : function(property, dir, sortType) {
  308. this.sortInfo = Array.prototype.slice.call(arguments, 0);
  309. if (this.jsonData) {
  310. var p = property;
  311. var dsc = dir && dir.toLowerCase() == "desc";
  312. var f = function(o1, o2) {
  313. var v1 = sortType ? sortType(o1[p]) : o1[p];
  314. var v2 = sortType ? sortType(o2[p]) : o2[p];;
  315. if (v1 < v2) {
  316. return dsc ? +1 : -1;
  317. } else if (v1 > v2) {
  318. return dsc ? -1 : +1;
  319. } else {
  320. return 0;
  321. }
  322. };
  323. this.jsonData.sort(f);
  324. this.refresh();
  325. if (this.jsonData != this.snapshot) {
  326. this.snapshot.sort(f);
  327. }
  328. }
  329. }
  330. });