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.

1552 lines
53 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.DataView
  17. * @extends Ext.BoxComponent
  18. * A mechanism for displaying data using custom layout templates and formatting. DataView uses an {@link Ext.XTemplate}
  19. * as its internal templating mechanism, and is bound to an {@link Ext.data.Store}
  20. * so that as the data in the store changes the view is automatically updated to reflect the changes. The view also
  21. * provides built-in behavior for many common events that can occur for its contained items including click, doubleclick,
  22. * mouseover, mouseout, etc. as well as a built-in selection model. <b>In order to use these features, an {@link #itemSelector}
  23. * config must be provided for the DataView to determine what nodes it will be working with.</b>
  24. *
  25. * <p>The example below binds a DataView to a {@link Ext.data.Store} and renders it into an {@link Ext.Panel}.</p>
  26. * <pre><code>
  27. var store = new Ext.data.JsonStore({
  28. url: 'get-images.php',
  29. root: 'images',
  30. fields: [
  31. 'name', 'url',
  32. {name:'size', type: 'float'},
  33. {name:'lastmod', type:'date', dateFormat:'timestamp'}
  34. ]
  35. });
  36. store.load();
  37. var tpl = new Ext.XTemplate(
  38. '&lt;tpl for="."&gt;',
  39. '&lt;div class="thumb-wrap" id="{name}"&gt;',
  40. '&lt;div class="thumb"&gt;&lt;img src="{url}" title="{name}"&gt;&lt;/div&gt;',
  41. '&lt;span class="x-editable"&gt;{shortName}&lt;/span&gt;&lt;/div&gt;',
  42. '&lt;/tpl&gt;',
  43. '&lt;div class="x-clear"&gt;&lt;/div&gt;'
  44. );
  45. var panel = new Ext.Panel({
  46. id:'images-view',
  47. frame:true,
  48. width:535,
  49. autoHeight:true,
  50. collapsible:true,
  51. layout:'fit',
  52. title:'Simple DataView',
  53. items: new Ext.DataView({
  54. store: store,
  55. tpl: tpl,
  56. autoHeight:true,
  57. multiSelect: true,
  58. overClass:'x-view-over',
  59. itemSelector:'div.thumb-wrap',
  60. emptyText: 'No images to display'
  61. })
  62. });
  63. panel.render(document.body);
  64. </code></pre>
  65. * @constructor
  66. * Create a new DataView
  67. * @param {Object} config The config object
  68. * @xtype dataview
  69. */
  70. Ext.DataView = Ext.extend(Ext.BoxComponent, {
  71. /**
  72. * @cfg {String/Array} tpl
  73. * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
  74. * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
  75. */
  76. /**
  77. * @cfg {Ext.data.Store} store
  78. * The {@link Ext.data.Store} to bind this DataView to.
  79. */
  80. /**
  81. * @cfg {String} itemSelector
  82. * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
  83. * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
  84. * working with.
  85. */
  86. /**
  87. * @cfg {Boolean} multiSelect
  88. * True to allow selection of more than one item at a time, false to allow selection of only a single item
  89. * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
  90. */
  91. /**
  92. * @cfg {Boolean} singleSelect
  93. * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
  94. * Note that if {@link #multiSelect} = true, this value will be ignored.
  95. */
  96. /**
  97. * @cfg {Boolean} simpleSelect
  98. * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
  99. * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
  100. */
  101. /**
  102. * @cfg {String} overClass
  103. * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
  104. */
  105. /**
  106. * @cfg {String} loadingText
  107. * A string to display during data load operations (defaults to undefined). If specified, this text will be
  108. * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
  109. * contents will continue to display normally until the new data is loaded and the contents are replaced.
  110. */
  111. /**
  112. * @cfg {String} selectedClass
  113. * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
  114. */
  115. selectedClass : "x-view-selected",
  116. /**
  117. * @cfg {String} emptyText
  118. * The text to display in the view when there is no data to display (defaults to '').
  119. */
  120. emptyText : "",
  121. /**
  122. * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
  123. */
  124. deferEmptyText: true,
  125. /**
  126. * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
  127. */
  128. trackOver: false,
  129. /**
  130. * @cfg {Boolean} blockRefresh Set this to true to ignore datachanged events on the bound store. This is useful if
  131. * you wish to provide custom transition animations via a plugin (defaults to false)
  132. */
  133. blockRefresh: false,
  134. //private
  135. last: false,
  136. // private
  137. initComponent : function(){
  138. Ext.DataView.superclass.initComponent.call(this);
  139. if(Ext.isString(this.tpl) || Ext.isArray(this.tpl)){
  140. this.tpl = new Ext.XTemplate(this.tpl);
  141. }
  142. this.addEvents(
  143. /**
  144. * @event beforeclick
  145. * Fires before a click is processed. Returns false to cancel the default action.
  146. * @param {Ext.DataView} this
  147. * @param {Number} index The index of the target node
  148. * @param {HTMLElement} node The target node
  149. * @param {Ext.EventObject} e The raw event object
  150. */
  151. "beforeclick",
  152. /**
  153. * @event click
  154. * Fires when a template node is clicked.
  155. * @param {Ext.DataView} this
  156. * @param {Number} index The index of the target node
  157. * @param {HTMLElement} node The target node
  158. * @param {Ext.EventObject} e The raw event object
  159. */
  160. "click",
  161. /**
  162. * @event mouseenter
  163. * Fires when the mouse enters a template node. trackOver:true or an overClass must be set to enable this event.
  164. * @param {Ext.DataView} this
  165. * @param {Number} index The index of the target node
  166. * @param {HTMLElement} node The target node
  167. * @param {Ext.EventObject} e The raw event object
  168. */
  169. "mouseenter",
  170. /**
  171. * @event mouseleave
  172. * Fires when the mouse leaves a template node. trackOver:true or an overClass must be set to enable this event.
  173. * @param {Ext.DataView} this
  174. * @param {Number} index The index of the target node
  175. * @param {HTMLElement} node The target node
  176. * @param {Ext.EventObject} e The raw event object
  177. */
  178. "mouseleave",
  179. /**
  180. * @event containerclick
  181. * Fires when a click occurs and it is not on a template node.
  182. * @param {Ext.DataView} this
  183. * @param {Ext.EventObject} e The raw event object
  184. */
  185. "containerclick",
  186. /**
  187. * @event dblclick
  188. * Fires when a template node is double clicked.
  189. * @param {Ext.DataView} this
  190. * @param {Number} index The index of the target node
  191. * @param {HTMLElement} node The target node
  192. * @param {Ext.EventObject} e The raw event object
  193. */
  194. "dblclick",
  195. /**
  196. * @event contextmenu
  197. * Fires when a template node is right clicked.
  198. * @param {Ext.DataView} this
  199. * @param {Number} index The index of the target node
  200. * @param {HTMLElement} node The target node
  201. * @param {Ext.EventObject} e The raw event object
  202. */
  203. "contextmenu",
  204. /**
  205. * @event containercontextmenu
  206. * Fires when a right click occurs that is not on a template node.
  207. * @param {Ext.DataView} this
  208. * @param {Ext.EventObject} e The raw event object
  209. */
  210. "containercontextmenu",
  211. /**
  212. * @event selectionchange
  213. * Fires when the selected nodes change.
  214. * @param {Ext.DataView} this
  215. * @param {Array} selections Array of the selected nodes
  216. */
  217. "selectionchange",
  218. /**
  219. * @event beforeselect
  220. * Fires before a selection is made. If any handlers return false, the selection is cancelled.
  221. * @param {Ext.DataView} this
  222. * @param {HTMLElement} node The node to be selected
  223. * @param {Array} selections Array of currently selected nodes
  224. */
  225. "beforeselect"
  226. );
  227. this.store = Ext.StoreMgr.lookup(this.store);
  228. this.all = new Ext.CompositeElementLite();
  229. this.selected = new Ext.CompositeElementLite();
  230. },
  231. // private
  232. afterRender : function(){
  233. Ext.DataView.superclass.afterRender.call(this);
  234. this.mon(this.getTemplateTarget(), {
  235. "click": this.onClick,
  236. "dblclick": this.onDblClick,
  237. "contextmenu": this.onContextMenu,
  238. scope:this
  239. });
  240. if(this.overClass || this.trackOver){
  241. this.mon(this.getTemplateTarget(), {
  242. "mouseover": this.onMouseOver,
  243. "mouseout": this.onMouseOut,
  244. scope:this
  245. });
  246. }
  247. if(this.store){
  248. this.bindStore(this.store, true);
  249. }
  250. },
  251. /**
  252. * Refreshes the view by reloading the data from the store and re-rendering the template.
  253. */
  254. refresh : function() {
  255. this.clearSelections(false, true);
  256. var el = this.getTemplateTarget(),
  257. records = this.store.getRange();
  258. el.update('');
  259. if(records.length < 1){
  260. if(!this.deferEmptyText || this.hasSkippedEmptyText){
  261. el.update(this.emptyText);
  262. }
  263. this.all.clear();
  264. }else{
  265. this.tpl.overwrite(el, this.collectData(records, 0));
  266. this.all.fill(Ext.query(this.itemSelector, el.dom));
  267. this.updateIndexes(0);
  268. }
  269. this.hasSkippedEmptyText = true;
  270. },
  271. getTemplateTarget: function(){
  272. return this.el;
  273. },
  274. /**
  275. * Function which can be overridden to provide custom formatting for each Record that is used by this
  276. * DataView's {@link #tpl template} to render each node.
  277. * @param {Array/Object} data The raw data object that was used to create the Record.
  278. * @param {Number} recordIndex the index number of the Record being prepared for rendering.
  279. * @param {Record} record The Record being prepared for rendering.
  280. * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
  281. * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
  282. */
  283. prepareData : function(data){
  284. return data;
  285. },
  286. /**
  287. * <p>Function which can be overridden which returns the data object passed to this
  288. * DataView's {@link #tpl template} to render the whole DataView.</p>
  289. * <p>This is usually an Array of data objects, each element of which is processed by an
  290. * {@link Ext.XTemplate XTemplate} which uses <tt>'&lt;tpl for="."&gt;'</tt> to iterate over its supplied
  291. * data object as an Array. However, <i>named</i> properties may be placed into the data object to
  292. * provide non-repeating data such as headings, totals etc.</p>
  293. * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
  294. * @param {Number} startIndex the index number of the Record being prepared for rendering.
  295. * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
  296. * contain <i>named</i> properties.
  297. */
  298. collectData : function(records, startIndex){
  299. var r = [],
  300. i = 0,
  301. len = records.length;
  302. for(; i < len; i++){
  303. r[r.length] = this.prepareData(records[i].data, startIndex + i, records[i]);
  304. }
  305. return r;
  306. },
  307. // private
  308. bufferRender : function(records, index){
  309. var div = document.createElement('div');
  310. this.tpl.overwrite(div, this.collectData(records, index));
  311. return Ext.query(this.itemSelector, div);
  312. },
  313. // private
  314. onUpdate : function(ds, record){
  315. var index = this.store.indexOf(record);
  316. if(index > -1){
  317. var sel = this.isSelected(index),
  318. original = this.all.elements[index],
  319. node = this.bufferRender([record], index)[0];
  320. this.all.replaceElement(index, node, true);
  321. if(sel){
  322. this.selected.replaceElement(original, node);
  323. this.all.item(index).addClass(this.selectedClass);
  324. }
  325. this.updateIndexes(index, index);
  326. }
  327. },
  328. // private
  329. onAdd : function(ds, records, index){
  330. if(this.all.getCount() === 0){
  331. this.refresh();
  332. return;
  333. }
  334. var nodes = this.bufferRender(records, index), n, a = this.all.elements;
  335. if(index < this.all.getCount()){
  336. n = this.all.item(index).insertSibling(nodes, 'before', true);
  337. a.splice.apply(a, [index, 0].concat(nodes));
  338. }else{
  339. n = this.all.last().insertSibling(nodes, 'after', true);
  340. a.push.apply(a, nodes);
  341. }
  342. this.updateIndexes(index);
  343. },
  344. // private
  345. onRemove : function(ds, record, index){
  346. this.deselect(index);
  347. this.all.removeElement(index, true);
  348. this.updateIndexes(index);
  349. if (this.store.getCount() === 0){
  350. this.refresh();
  351. }
  352. },
  353. /**
  354. * Refreshes an individual node's data from the store.
  355. * @param {Number} index The item's data index in the store
  356. */
  357. refreshNode : function(index){
  358. this.onUpdate(this.store, this.store.getAt(index));
  359. },
  360. // private
  361. updateIndexes : function(startIndex, endIndex){
  362. var ns = this.all.elements;
  363. startIndex = startIndex || 0;
  364. endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
  365. for(var i = startIndex; i <= endIndex; i++){
  366. ns[i].viewIndex = i;
  367. }
  368. },
  369. /**
  370. * Returns the store associated with this DataView.
  371. * @return {Ext.data.Store} The store
  372. */
  373. getStore : function(){
  374. return this.store;
  375. },
  376. /**
  377. * Changes the data store bound to this view and refreshes it.
  378. * @param {Store} store The store to bind to this view
  379. */
  380. bindStore : function(store, initial){
  381. if(!initial && this.store){
  382. if(store !== this.store && this.store.autoDestroy){
  383. this.store.destroy();
  384. }else{
  385. this.store.un("beforeload", this.onBeforeLoad, this);
  386. this.store.un("datachanged", this.onDataChanged, this);
  387. this.store.un("add", this.onAdd, this);
  388. this.store.un("remove", this.onRemove, this);
  389. this.store.un("update", this.onUpdate, this);
  390. this.store.un("clear", this.refresh, this);
  391. }
  392. if(!store){
  393. this.store = null;
  394. }
  395. }
  396. if(store){
  397. store = Ext.StoreMgr.lookup(store);
  398. store.on({
  399. scope: this,
  400. beforeload: this.onBeforeLoad,
  401. datachanged: this.onDataChanged,
  402. add: this.onAdd,
  403. remove: this.onRemove,
  404. update: this.onUpdate,
  405. clear: this.refresh
  406. });
  407. }
  408. this.store = store;
  409. if(store){
  410. this.refresh();
  411. }
  412. },
  413. /**
  414. * @private
  415. * Calls this.refresh if this.blockRefresh is not true
  416. */
  417. onDataChanged: function() {
  418. if (this.blockRefresh !== true) {
  419. this.refresh.apply(this, arguments);
  420. }
  421. },
  422. /**
  423. * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
  424. * @param {HTMLElement} node
  425. * @return {HTMLElement} The template node
  426. */
  427. findItemFromChild : function(node){
  428. return Ext.fly(node).findParent(this.itemSelector, this.getTemplateTarget());
  429. },
  430. // private
  431. onClick : function(e){
  432. var item = e.getTarget(this.itemSelector, this.getTemplateTarget()),
  433. index;
  434. if(item){
  435. index = this.indexOf(item);
  436. if(this.onItemClick(item, index, e) !== false){
  437. this.fireEvent("click", this, index, item, e);
  438. }
  439. }else{
  440. if(this.fireEvent("containerclick", this, e) !== false){
  441. this.onContainerClick(e);
  442. }
  443. }
  444. },
  445. onContainerClick : function(e){
  446. this.clearSelections();
  447. },
  448. // private
  449. onContextMenu : function(e){
  450. var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
  451. if(item){
  452. this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
  453. }else{
  454. this.fireEvent("containercontextmenu", this, e);
  455. }
  456. },
  457. // private
  458. onDblClick : function(e){
  459. var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
  460. if(item){
  461. this.fireEvent("dblclick", this, this.indexOf(item), item, e);
  462. }
  463. },
  464. // private
  465. onMouseOver : function(e){
  466. var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
  467. if(item && item !== this.lastItem){
  468. this.lastItem = item;
  469. Ext.fly(item).addClass(this.overClass);
  470. this.fireEvent("mouseenter", this, this.indexOf(item), item, e);
  471. }
  472. },
  473. // private
  474. onMouseOut : function(e){
  475. if(this.lastItem){
  476. if(!e.within(this.lastItem, true, true)){
  477. Ext.fly(this.lastItem).removeClass(this.overClass);
  478. this.fireEvent("mouseleave", this, this.indexOf(this.lastItem), this.lastItem, e);
  479. delete this.lastItem;
  480. }
  481. }
  482. },
  483. // private
  484. onItemClick : function(item, index, e){
  485. if(this.fireEvent("beforeclick", this, index, item, e) === false){
  486. return false;
  487. }
  488. if(this.multiSelect){
  489. this.doMultiSelection(item, index, e);
  490. e.preventDefault();
  491. }else if(this.singleSelect){
  492. this.doSingleSelection(item, index, e);
  493. e.preventDefault();
  494. }
  495. return true;
  496. },
  497. // private
  498. doSingleSelection : function(item, index, e){
  499. if(e.ctrlKey && this.isSelected(index)){
  500. this.deselect(index);
  501. }else{
  502. this.select(index, false);
  503. }
  504. },
  505. // private
  506. doMultiSelection : function(item, index, e){
  507. if(e.shiftKey && this.last !== false){
  508. var last = this.last;
  509. this.selectRange(last, index, e.ctrlKey);
  510. this.last = last; // reset the last
  511. }else{
  512. if((e.ctrlKey||this.simpleSelect) && this.isSelected(index)){
  513. this.deselect(index);
  514. }else{
  515. this.select(index, e.ctrlKey || e.shiftKey || this.simpleSelect);
  516. }
  517. }
  518. },
  519. /**
  520. * Gets the number of selected nodes.
  521. * @return {Number} The node count
  522. */
  523. getSelectionCount : function(){
  524. return this.selected.getCount();
  525. },
  526. /**
  527. * Gets the currently selected nodes.
  528. * @return {Array} An array of HTMLElements
  529. */
  530. getSelectedNodes : function(){
  531. return this.selected.elements;
  532. },
  533. /**
  534. * Gets the indexes of the selected nodes.
  535. * @return {Array} An array of numeric indexes
  536. */
  537. getSelectedIndexes : function(){
  538. var indexes = [],
  539. selected = this.selected.elements,
  540. i = 0,
  541. len = selected.length;
  542. for(; i < len; i++){
  543. indexes.push(selected[i].viewIndex);
  544. }
  545. return indexes;
  546. },
  547. /**
  548. * Gets an array of the selected records
  549. * @return {Array} An array of {@link Ext.data.Record} objects
  550. */
  551. getSelectedRecords : function(){
  552. return this.getRecords(this.selected.elements);
  553. },
  554. /**
  555. * Gets an array of the records from an array of nodes
  556. * @param {Array} nodes The nodes to evaluate
  557. * @return {Array} records The {@link Ext.data.Record} objects
  558. */
  559. getRecords : function(nodes){
  560. var records = [],
  561. i = 0,
  562. len = nodes.length;
  563. for(; i < len; i++){
  564. records[records.length] = this.store.getAt(nodes[i].viewIndex);
  565. }
  566. return records;
  567. },
  568. /**
  569. * Gets a record from a node
  570. * @param {HTMLElement} node The node to evaluate
  571. * @return {Record} record The {@link Ext.data.Record} object
  572. */
  573. getRecord : function(node){
  574. return this.store.getAt(node.viewIndex);
  575. },
  576. /**
  577. * Clears all selections.
  578. * @param {Boolean} suppressEvent (optional) True to skip firing of the selectionchange event
  579. */
  580. clearSelections : function(suppressEvent, skipUpdate){
  581. if((this.multiSelect || this.singleSelect) && this.selected.getCount() > 0){
  582. if(!skipUpdate){
  583. this.selected.removeClass(this.selectedClass);
  584. }
  585. this.selected.clear();
  586. this.last = false;
  587. if(!suppressEvent){
  588. this.fireEvent("selectionchange", this, this.selected.elements);
  589. }
  590. }
  591. },
  592. /**
  593. * Returns true if the passed node is selected, else false.
  594. * @param {HTMLElement/Number/Ext.data.Record} node The node, node index or record to check
  595. * @return {Boolean} True if selected, else false
  596. */
  597. isSelected : function(node){
  598. return this.selected.contains(this.getNode(node));
  599. },
  600. /**
  601. * Deselects a node.
  602. * @param {HTMLElement/Number/Record} node The node, node index or record to deselect
  603. */
  604. deselect : function(node){
  605. if(this.isSelected(node)){
  606. node = this.getNode(node);
  607. this.selected.removeElement(node);
  608. if(this.last == node.viewIndex){
  609. this.last = false;
  610. }
  611. Ext.fly(node).removeClass(this.selectedClass);
  612. this.fireEvent("selectionchange", this, this.selected.elements);
  613. }
  614. },
  615. /**
  616. * Selects a set of nodes.
  617. * @param {Array/HTMLElement/String/Number/Ext.data.Record} nodeInfo An HTMLElement template node, index of a template node,
  618. * id of a template node, record associated with a node or an array of any of those to select
  619. * @param {Boolean} keepExisting (optional) true to keep existing selections
  620. * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
  621. */
  622. select : function(nodeInfo, keepExisting, suppressEvent){
  623. if(Ext.isArray(nodeInfo)){
  624. if(!keepExisting){
  625. this.clearSelections(true);
  626. }
  627. for(var i = 0, len = nodeInfo.length; i < len; i++){
  628. this.select(nodeInfo[i], true, true);
  629. }
  630. if(!suppressEvent){
  631. this.fireEvent("selectionchange", this, this.selected.elements);
  632. }
  633. } else{
  634. var node = this.getNode(nodeInfo);
  635. if(!keepExisting){
  636. this.clearSelections(true);
  637. }
  638. if(node && !this.isSelected(node)){
  639. if(this.fireEvent("beforeselect", this, node, this.selected.elements) !== false){
  640. Ext.fly(node).addClass(this.selectedClass);
  641. this.selected.add(node);
  642. this.last = node.viewIndex;
  643. if(!suppressEvent){
  644. this.fireEvent("selectionchange", this, this.selected.elements);
  645. }
  646. }
  647. }
  648. }
  649. },
  650. /**
  651. * Selects a range of nodes. All nodes between start and end are selected.
  652. * @param {Number} start The index of the first node in the range
  653. * @param {Number} end The index of the last node in the range
  654. * @param {Boolean} keepExisting (optional) True to retain existing selections
  655. */
  656. selectRange : function(start, end, keepExisting){
  657. if(!keepExisting){
  658. this.clearSelections(true);
  659. }
  660. this.select(this.getNodes(start, end), true);
  661. },
  662. /**
  663. * Gets a template node.
  664. * @param {HTMLElement/String/Number/Ext.data.Record} nodeInfo An HTMLElement template node, index of a template node,
  665. * the id of a template node or the record associated with the node.
  666. * @return {HTMLElement} The node or null if it wasn't found
  667. */
  668. getNode : function(nodeInfo){
  669. if(Ext.isString(nodeInfo)){
  670. return document.getElementById(nodeInfo);
  671. }else if(Ext.isNumber(nodeInfo)){
  672. return this.all.elements[nodeInfo];
  673. }else if(nodeInfo instanceof Ext.data.Record){
  674. var idx = this.store.indexOf(nodeInfo);
  675. return this.all.elements[idx];
  676. }
  677. return nodeInfo;
  678. },
  679. /**
  680. * Gets a range nodes.
  681. * @param {Number} start (optional) The index of the first node in the range
  682. * @param {Number} end (optional) The index of the last node in the range
  683. * @return {Array} An array of nodes
  684. */
  685. getNodes : function(start, end){
  686. var ns = this.all.elements,
  687. nodes = [],
  688. i;
  689. start = start || 0;
  690. end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
  691. if(start <= end){
  692. for(i = start; i <= end && ns[i]; i++){
  693. nodes.push(ns[i]);
  694. }
  695. } else{
  696. for(i = start; i >= end && ns[i]; i--){
  697. nodes.push(ns[i]);
  698. }
  699. }
  700. return nodes;
  701. },
  702. /**
  703. * Finds the index of the passed node.
  704. * @param {HTMLElement/String/Number/Record} nodeInfo An HTMLElement template node, index of a template node, the id of a template node
  705. * or a record associated with a node.
  706. * @return {Number} The index of the node or -1
  707. */
  708. indexOf : function(node){
  709. node = this.getNode(node);
  710. if(Ext.isNumber(node.viewIndex)){
  711. return node.viewIndex;
  712. }
  713. return this.all.indexOf(node);
  714. },
  715. // private
  716. onBeforeLoad : function(){
  717. if(this.loadingText){
  718. this.clearSelections(false, true);
  719. this.getTemplateTarget().update('<div class="loading-indicator">'+this.loadingText+'</div>');
  720. this.all.clear();
  721. }
  722. },
  723. onDestroy : function(){
  724. this.all.clear();
  725. this.selected.clear();
  726. Ext.DataView.superclass.onDestroy.call(this);
  727. this.bindStore(null);
  728. }
  729. });
  730. /**
  731. * Changes the data store bound to this view and refreshes it. (deprecated in favor of bindStore)
  732. * @param {Store} store The store to bind to this view
  733. */
  734. Ext.DataView.prototype.setStore = Ext.DataView.prototype.bindStore;
  735. Ext.reg('dataview', Ext.DataView);
  736. /**
  737. * @class Ext.list.ListView
  738. * @extends Ext.DataView
  739. * <p>Ext.list.ListView is a fast and light-weight implentation of a
  740. * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
  741. * <div class="mdetail-params"><ul>
  742. * <li>resizable columns</li>
  743. * <li>selectable</li>
  744. * <li>column widths are initially proportioned by percentage based on the container
  745. * width and number of columns</li>
  746. * <li>uses templates to render the data in any required format</li>
  747. * <li>no horizontal scrolling</li>
  748. * <li>no editing</li>
  749. * </ul></div>
  750. * <p>Example usage:</p>
  751. * <pre><code>
  752. // consume JSON of this form:
  753. {
  754. "images":[
  755. {
  756. "name":"dance_fever.jpg",
  757. "size":2067,
  758. "lastmod":1236974993000,
  759. "url":"images\/thumbs\/dance_fever.jpg"
  760. },
  761. {
  762. "name":"zack_sink.jpg",
  763. "size":2303,
  764. "lastmod":1236974993000,
  765. "url":"images\/thumbs\/zack_sink.jpg"
  766. }
  767. ]
  768. }
  769. var store = new Ext.data.JsonStore({
  770. url: 'get-images.php',
  771. root: 'images',
  772. fields: [
  773. 'name', 'url',
  774. {name:'size', type: 'float'},
  775. {name:'lastmod', type:'date', dateFormat:'timestamp'}
  776. ]
  777. });
  778. store.load();
  779. var listView = new Ext.list.ListView({
  780. store: store,
  781. multiSelect: true,
  782. emptyText: 'No images to display',
  783. reserveScrollOffset: true,
  784. columns: [{
  785. header: 'File',
  786. width: .5,
  787. dataIndex: 'name'
  788. },{
  789. header: 'Last Modified',
  790. width: .35,
  791. dataIndex: 'lastmod',
  792. tpl: '{lastmod:date("m-d h:i a")}'
  793. },{
  794. header: 'Size',
  795. dataIndex: 'size',
  796. tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
  797. align: 'right'
  798. }]
  799. });
  800. // put it in a Panel so it looks pretty
  801. var panel = new Ext.Panel({
  802. id:'images-view',
  803. width:425,
  804. height:250,
  805. collapsible:true,
  806. layout:'fit',
  807. title:'Simple ListView <i>(0 items selected)</i>',
  808. items: listView
  809. });
  810. panel.render(document.body);
  811. // little bit of feedback
  812. listView.on('selectionchange', function(view, nodes){
  813. var l = nodes.length;
  814. var s = l != 1 ? 's' : '';
  815. panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
  816. });
  817. * </code></pre>
  818. * @constructor
  819. * @param {Object} config
  820. * @xtype listview
  821. */
  822. Ext.list.ListView = Ext.extend(Ext.DataView, {
  823. /**
  824. * Set this property to <tt>true</tt> to disable the header click handler disabling sort
  825. * (defaults to <tt>false</tt>).
  826. * @type Boolean
  827. * @property disableHeaders
  828. */
  829. /**
  830. * @cfg {Boolean} hideHeaders
  831. * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
  832. * the {@link #internalTpl header row} will be shown).
  833. */
  834. /**
  835. * @cfg {String} itemSelector
  836. * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
  837. * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
  838. * that will be used to determine what nodes the ListView will be working with.
  839. */
  840. itemSelector: 'dl',
  841. /**
  842. * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
  843. * <tt>'x-list-selected'</tt>). An example overriding the default styling:
  844. <pre><code>
  845. .x-list-selected {background-color: yellow;}
  846. </code></pre>
  847. * @type String
  848. */
  849. selectedClass:'x-list-selected',
  850. /**
  851. * @cfg {String} overClass The CSS class applied when over a row (defaults to
  852. * <tt>'x-list-over'</tt>). An example overriding the default styling:
  853. <pre><code>
  854. .x-list-over {background-color: orange;}
  855. </code></pre>
  856. * @type String
  857. */
  858. overClass:'x-list-over',
  859. /**
  860. * @cfg {Boolean} reserveScrollOffset
  861. * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
  862. * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
  863. * <b><tt>{@link #scrollOffset}</tt></b> immediately.
  864. */
  865. /**
  866. * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
  867. * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically
  868. * calculated.
  869. */
  870. scrollOffset : undefined,
  871. /**
  872. * @cfg {Boolean/Object} columnResize
  873. * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.ColumnResizer}
  874. * to enable the columns to be resizable (defaults to <tt>true</tt>).
  875. */
  876. columnResize: true,
  877. /**
  878. * @cfg {Array} columns An array of column configuration objects, for example:
  879. * <pre><code>
  880. {
  881. align: 'right',
  882. dataIndex: 'size',
  883. header: 'Size',
  884. tpl: '{size:fileSize}',
  885. width: .35
  886. }
  887. * </code></pre>
  888. * Acceptable properties for each column configuration object are:
  889. * <div class="mdetail-params"><ul>
  890. * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property
  891. * of the column. Defaults to <tt>'left'</tt>.</div></li>
  892. * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
  893. * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>
  894. * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
  895. * {@link Ext.grid.Column#header header} for details.</div></li>
  896. * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the
  897. * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
  898. * will be implicitly created using the <tt>dataIndex</tt>.</div></li>
  899. * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width
  900. * this column should be allocated. Columns that have no width specified will be
  901. * allocated with an equal percentage to fill 100% of the container width. To easily take
  902. * advantage of the full container width, leave the width of at least one column undefined.
  903. * Note that if you do not want to take up the full width of the container, the width of
  904. * every column needs to be explicitly defined.</div></li>
  905. * </ul></div>
  906. */
  907. /**
  908. * @cfg {Boolean/Object} columnSort
  909. * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.Sorter}
  910. * to enable the columns to be sortable (defaults to <tt>true</tt>).
  911. */
  912. columnSort: true,
  913. /**
  914. * @cfg {String/Array} internalTpl
  915. * The template to be used for the header row. See {@link #tpl} for more details.
  916. */
  917. /*
  918. * IE has issues when setting percentage based widths to 100%. Default to 99.
  919. */
  920. maxColumnWidth: Ext.isIE9m ? 99 : 100,
  921. initComponent : function(){
  922. if(this.columnResize){
  923. this.colResizer = new Ext.list.ColumnResizer(this.colResizer);
  924. this.colResizer.init(this);
  925. }
  926. if(this.columnSort){
  927. this.colSorter = new Ext.list.Sorter(this.columnSort);
  928. this.colSorter.init(this);
  929. }
  930. if(!this.internalTpl){
  931. this.internalTpl = new Ext.XTemplate(
  932. '<div class="x-list-header"><div class="x-list-header-inner">',
  933. '<tpl for="columns">',
  934. '<div style="width:{[values.width*100]}%;text-align:{align};"><em class="x-unselectable" unselectable="on" id="',this.id, '-xlhd-{#}">',
  935. '{header}',
  936. '</em></div>',
  937. '</tpl>',
  938. '<div class="x-clear"></div>',
  939. '</div></div>',
  940. '<div class="x-list-body"><div class="x-list-body-inner">',
  941. '</div></div>'
  942. );
  943. }
  944. if(!this.tpl){
  945. this.tpl = new Ext.XTemplate(
  946. '<tpl for="rows">',
  947. '<dl>',
  948. '<tpl for="parent.columns">',
  949. '<dt style="width:{[values.width*100]}%;text-align:{align};">',
  950. '<em unselectable="on"<tpl if="cls"> class="{cls}</tpl>">',
  951. '{[values.tpl.apply(parent)]}',
  952. '</em></dt>',
  953. '</tpl>',
  954. '<div class="x-clear"></div>',
  955. '</dl>',
  956. '</tpl>'
  957. );
  958. };
  959. var cs = this.columns,
  960. allocatedWidth = 0,
  961. colsWithWidth = 0,
  962. len = cs.length,
  963. columns = [];
  964. for(var i = 0; i < len; i++){
  965. var c = cs[i];
  966. if(!c.isColumn) {
  967. c.xtype = c.xtype ? (/^lv/.test(c.xtype) ? c.xtype : 'lv' + c.xtype) : 'lvcolumn';
  968. c = Ext.create(c);
  969. }
  970. if(c.width) {
  971. allocatedWidth += c.width*100;
  972. if(allocatedWidth > this.maxColumnWidth){
  973. c.width -= (allocatedWidth - this.maxColumnWidth) / 100;
  974. }
  975. colsWithWidth++;
  976. }
  977. columns.push(c);
  978. }
  979. cs = this.columns = columns;
  980. // auto calculate missing column widths
  981. if(colsWithWidth < len){
  982. var remaining = len - colsWithWidth;
  983. if(allocatedWidth < this.maxColumnWidth){
  984. var perCol = ((this.maxColumnWidth-allocatedWidth) / remaining)/100;
  985. for(var j = 0; j < len; j++){
  986. var c = cs[j];
  987. if(!c.width){
  988. c.width = perCol;
  989. }
  990. }
  991. }
  992. }
  993. Ext.list.ListView.superclass.initComponent.call(this);
  994. },
  995. onRender : function(){
  996. this.autoEl = {
  997. cls: 'x-list-wrap'
  998. };
  999. Ext.list.ListView.superclass.onRender.apply(this, arguments);
  1000. this.internalTpl.overwrite(this.el, {columns: this.columns});
  1001. this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
  1002. this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
  1003. if(this.hideHeaders){
  1004. this.el.dom.firstChild.style.display = 'none';
  1005. }
  1006. },
  1007. getTemplateTarget : function(){
  1008. return this.innerBody;
  1009. },
  1010. /**
  1011. * <p>Function which can be overridden which returns the data object passed to this
  1012. * view's {@link #tpl template} to render the whole ListView. The returned object
  1013. * shall contain the following properties:</p>
  1014. * <div class="mdetail-params"><ul>
  1015. * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
  1016. * <li><b>rows</b> : String<div class="sub-desc">See
  1017. * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
  1018. * </ul></div>
  1019. * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
  1020. * @param {Number} startIndex the index number of the Record being prepared for rendering.
  1021. * @return {Object} A data object containing properties to be processed by a repeating
  1022. * XTemplate as described above.
  1023. */
  1024. collectData : function(){
  1025. var rs = Ext.list.ListView.superclass.collectData.apply(this, arguments);
  1026. return {
  1027. columns: this.columns,
  1028. rows: rs
  1029. };
  1030. },
  1031. verifyInternalSize : function(){
  1032. if(this.lastSize){
  1033. this.onResize(this.lastSize.width, this.lastSize.height);
  1034. }
  1035. },
  1036. // private
  1037. onResize : function(w, h){
  1038. var body = this.innerBody.dom,
  1039. header = this.innerHd.dom,
  1040. scrollWidth = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth()) + 'px',
  1041. parentNode;
  1042. if(!body){
  1043. return;
  1044. }
  1045. parentNode = body.parentNode;
  1046. if(Ext.isNumber(w)){
  1047. if(this.reserveScrollOffset || ((parentNode.offsetWidth - parentNode.clientWidth) > 10)){
  1048. body.style.width = scrollWidth;
  1049. header.style.width = scrollWidth;
  1050. }else{
  1051. body.style.width = w + 'px';
  1052. header.style.width = w + 'px';
  1053. setTimeout(function(){
  1054. if((parentNode.offsetWidth - parentNode.clientWidth) > 10){
  1055. body.style.width = scrollWidth;
  1056. header.style.width = scrollWidth;
  1057. }
  1058. }, 10);
  1059. }
  1060. }
  1061. if(Ext.isNumber(h)){
  1062. parentNode.style.height = Math.max(0, h - header.parentNode.offsetHeight) + 'px';
  1063. }
  1064. },
  1065. updateIndexes : function(){
  1066. Ext.list.ListView.superclass.updateIndexes.apply(this, arguments);
  1067. this.verifyInternalSize();
  1068. },
  1069. findHeaderIndex : function(header){
  1070. header = header.dom || header;
  1071. var parentNode = header.parentNode,
  1072. children = parentNode.parentNode.childNodes,
  1073. i = 0,
  1074. c;
  1075. for(; c = children[i]; i++){
  1076. if(c == parentNode){
  1077. return i;
  1078. }
  1079. }
  1080. return -1;
  1081. },
  1082. setHdWidths : function(){
  1083. var els = this.innerHd.dom.getElementsByTagName('div'),
  1084. i = 0,
  1085. columns = this.columns,
  1086. len = columns.length;
  1087. for(; i < len; i++){
  1088. els[i].style.width = (columns[i].width*100) + '%';
  1089. }
  1090. }
  1091. });
  1092. Ext.reg('listview', Ext.list.ListView);
  1093. // Backwards compatibility alias
  1094. Ext.ListView = Ext.list.ListView;/**
  1095. * @class Ext.list.Column
  1096. * <p>This class encapsulates column configuration data to be used in the initialization of a
  1097. * {@link Ext.list.ListView ListView}.</p>
  1098. * <p>While subclasses are provided to render data in different ways, this class renders a passed
  1099. * data field unchanged and is usually used for textual columns.</p>
  1100. */
  1101. Ext.list.Column = Ext.extend(Object, {
  1102. /**
  1103. * @private
  1104. * @cfg {Boolean} isColumn
  1105. * Used by ListView constructor method to avoid reprocessing a Column
  1106. * if <code>isColumn</code> is not set ListView will recreate a new Ext.list.Column
  1107. * Defaults to true.
  1108. */
  1109. isColumn: true,
  1110. /**
  1111. * @cfg {String} align
  1112. * Set the CSS text-align property of the column. Defaults to <tt>'left'</tt>.
  1113. */
  1114. align: 'left',
  1115. /**
  1116. * @cfg {String} header Optional. The header text to be used as innerHTML
  1117. * (html tags are accepted) to display in the ListView. <b>Note</b>: to
  1118. * have a clickable header with no text displayed use <tt>'&#160;'</tt>.
  1119. */
  1120. header: '',
  1121. /**
  1122. * @cfg {Number} width Optional. Percentage of the container width
  1123. * this column should be allocated. Columns that have no width specified will be
  1124. * allocated with an equal percentage to fill 100% of the container width. To easily take
  1125. * advantage of the full container width, leave the width of at least one column undefined.
  1126. * Note that if you do not want to take up the full width of the container, the width of
  1127. * every column needs to be explicitly defined.
  1128. */
  1129. width: null,
  1130. /**
  1131. * @cfg {String} cls Optional. This option can be used to add a CSS class to the cell of each
  1132. * row for this column.
  1133. */
  1134. cls: '',
  1135. /**
  1136. * @cfg {String} tpl Optional. Specify a string to pass as the
  1137. * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
  1138. * will be implicitly created using the <tt>dataIndex</tt>.
  1139. */
  1140. /**
  1141. * @cfg {String} dataIndex <p><b>Required</b>. The name of the field in the
  1142. * ListViews's {@link Ext.data.Store}'s {@link Ext.data.Record} definition from
  1143. * which to draw the column's value.</p>
  1144. */
  1145. constructor : function(c){
  1146. if(!c.tpl){
  1147. c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');
  1148. }
  1149. else if(Ext.isString(c.tpl)){
  1150. c.tpl = new Ext.XTemplate(c.tpl);
  1151. }
  1152. Ext.apply(this, c);
  1153. }
  1154. });
  1155. Ext.reg('lvcolumn', Ext.list.Column);
  1156. /**
  1157. * @class Ext.list.NumberColumn
  1158. * @extends Ext.list.Column
  1159. * <p>A Column definition class which renders a numeric data field according to a {@link #format} string. See the
  1160. * {@link Ext.list.Column#xtype xtype} config option of {@link Ext.list.Column} for more details.</p>
  1161. */
  1162. Ext.list.NumberColumn = Ext.extend(Ext.list.Column, {
  1163. /**
  1164. * @cfg {String} format
  1165. * A formatting string as used by {@link Ext.util.Format#number} to format a numeric value for this Column
  1166. * (defaults to <tt>'0,000.00'</tt>).
  1167. */
  1168. format: '0,000.00',
  1169. constructor : function(c) {
  1170. c.tpl = c.tpl || new Ext.XTemplate('{' + c.dataIndex + ':number("' + (c.format || this.format) + '")}');
  1171. Ext.list.NumberColumn.superclass.constructor.call(this, c);
  1172. }
  1173. });
  1174. Ext.reg('lvnumbercolumn', Ext.list.NumberColumn);
  1175. /**
  1176. * @class Ext.list.DateColumn
  1177. * @extends Ext.list.Column
  1178. * <p>A Column definition class which renders a passed date according to the default locale, or a configured
  1179. * {@link #format}. See the {@link Ext.list.Column#xtype xtype} config option of {@link Ext.list.Column}
  1180. * for more details.</p>
  1181. */
  1182. Ext.list.DateColumn = Ext.extend(Ext.list.Column, {
  1183. format: 'm/d/Y',
  1184. constructor : function(c) {
  1185. c.tpl = c.tpl || new Ext.XTemplate('{' + c.dataIndex + ':date("' + (c.format || this.format) + '")}');
  1186. Ext.list.DateColumn.superclass.constructor.call(this, c);
  1187. }
  1188. });
  1189. Ext.reg('lvdatecolumn', Ext.list.DateColumn);
  1190. /**
  1191. * @class Ext.list.BooleanColumn
  1192. * @extends Ext.list.Column
  1193. * <p>A Column definition class which renders boolean data fields. See the {@link Ext.list.Column#xtype xtype}
  1194. * config option of {@link Ext.list.Column} for more details.</p>
  1195. */
  1196. Ext.list.BooleanColumn = Ext.extend(Ext.list.Column, {
  1197. /**
  1198. * @cfg {String} trueText
  1199. * The string returned by the renderer when the column value is not falsey (defaults to <tt>'true'</tt>).
  1200. */
  1201. trueText: 'true',
  1202. /**
  1203. * @cfg {String} falseText
  1204. * The string returned by the renderer when the column value is falsey (but not undefined) (defaults to
  1205. * <tt>'false'</tt>).
  1206. */
  1207. falseText: 'false',
  1208. /**
  1209. * @cfg {String} undefinedText
  1210. * The string returned by the renderer when the column value is undefined (defaults to <tt>'&#160;'</tt>).
  1211. */
  1212. undefinedText: '&#160;',
  1213. constructor : function(c) {
  1214. c.tpl = c.tpl || new Ext.XTemplate('{' + c.dataIndex + ':this.format}');
  1215. var t = this.trueText, f = this.falseText, u = this.undefinedText;
  1216. c.tpl.format = function(v){
  1217. if(v === undefined){
  1218. return u;
  1219. }
  1220. if(!v || v === 'false'){
  1221. return f;
  1222. }
  1223. return t;
  1224. };
  1225. Ext.list.DateColumn.superclass.constructor.call(this, c);
  1226. }
  1227. });
  1228. Ext.reg('lvbooleancolumn', Ext.list.BooleanColumn);/**
  1229. * @class Ext.list.ColumnResizer
  1230. * @extends Ext.util.Observable
  1231. * <p>Supporting Class for Ext.list.ListView</p>
  1232. * @constructor
  1233. * @param {Object} config
  1234. */
  1235. Ext.list.ColumnResizer = Ext.extend(Ext.util.Observable, {
  1236. /**
  1237. * @cfg {Number} minPct The minimum percentage to allot for any column (defaults to <tt>.05</tt>)
  1238. */
  1239. minPct: .05,
  1240. constructor: function(config){
  1241. Ext.apply(this, config);
  1242. Ext.list.ColumnResizer.superclass.constructor.call(this);
  1243. },
  1244. init : function(listView){
  1245. this.view = listView;
  1246. listView.on('render', this.initEvents, this);
  1247. },
  1248. initEvents : function(view){
  1249. view.mon(view.innerHd, 'mousemove', this.handleHdMove, this);
  1250. this.tracker = new Ext.dd.DragTracker({
  1251. onBeforeStart: this.onBeforeStart.createDelegate(this),
  1252. onStart: this.onStart.createDelegate(this),
  1253. onDrag: this.onDrag.createDelegate(this),
  1254. onEnd: this.onEnd.createDelegate(this),
  1255. tolerance: 3,
  1256. autoStart: 300
  1257. });
  1258. this.tracker.initEl(view.innerHd);
  1259. view.on('beforedestroy', this.tracker.destroy, this.tracker);
  1260. },
  1261. handleHdMove : function(e, t){
  1262. var handleWidth = 5,
  1263. x = e.getPageX(),
  1264. header = e.getTarget('em', 3, true);
  1265. if(header){
  1266. var region = header.getRegion(),
  1267. style = header.dom.style,
  1268. parentNode = header.dom.parentNode;
  1269. if(x - region.left <= handleWidth && parentNode != parentNode.parentNode.firstChild){
  1270. this.activeHd = Ext.get(parentNode.previousSibling.firstChild);
  1271. style.cursor = Ext.isWebKit ? 'e-resize' : 'col-resize';
  1272. } else if(region.right - x <= handleWidth && parentNode != parentNode.parentNode.lastChild.previousSibling){
  1273. this.activeHd = header;
  1274. style.cursor = Ext.isWebKit ? 'w-resize' : 'col-resize';
  1275. } else{
  1276. delete this.activeHd;
  1277. style.cursor = '';
  1278. }
  1279. }
  1280. },
  1281. onBeforeStart : function(e){
  1282. this.dragHd = this.activeHd;
  1283. return !!this.dragHd;
  1284. },
  1285. onStart: function(e){
  1286. var me = this,
  1287. view = me.view,
  1288. dragHeader = me.dragHd,
  1289. x = me.tracker.getXY()[0];
  1290. me.proxy = view.el.createChild({cls:'x-list-resizer'});
  1291. me.dragX = dragHeader.getX();
  1292. me.headerIndex = view.findHeaderIndex(dragHeader);
  1293. me.headersDisabled = view.disableHeaders;
  1294. view.disableHeaders = true;
  1295. me.proxy.setHeight(view.el.getHeight());
  1296. me.proxy.setX(me.dragX);
  1297. me.proxy.setWidth(x - me.dragX);
  1298. this.setBoundaries();
  1299. },
  1300. // Sets up the boundaries for the drag/drop operation
  1301. setBoundaries: function(relativeX){
  1302. var view = this.view,
  1303. headerIndex = this.headerIndex,
  1304. width = view.innerHd.getWidth(),
  1305. relativeX = view.innerHd.getX(),
  1306. minWidth = Math.ceil(width * this.minPct),
  1307. maxWidth = width - minWidth,
  1308. numColumns = view.columns.length,
  1309. headers = view.innerHd.select('em', true),
  1310. minX = minWidth + relativeX,
  1311. maxX = maxWidth + relativeX,
  1312. header;
  1313. if (numColumns == 2) {
  1314. this.minX = minX;
  1315. this.maxX = maxX;
  1316. }else{
  1317. header = headers.item(headerIndex + 2);
  1318. this.minX = headers.item(headerIndex).getX() + minWidth;
  1319. this.maxX = header ? header.getX() - minWidth : maxX;
  1320. if (headerIndex == 0) {
  1321. // First
  1322. this.minX = minX;
  1323. } else if (headerIndex == numColumns - 2) {
  1324. // Last
  1325. this.maxX = maxX;
  1326. }
  1327. }
  1328. },
  1329. onDrag: function(e){
  1330. var me = this,
  1331. cursorX = me.tracker.getXY()[0].constrain(me.minX, me.maxX);
  1332. me.proxy.setWidth(cursorX - this.dragX);
  1333. },
  1334. onEnd: function(e){
  1335. /* calculate desired width by measuring proxy and then remove it */
  1336. var newWidth = this.proxy.getWidth(),
  1337. index = this.headerIndex,
  1338. view = this.view,
  1339. columns = view.columns,
  1340. width = view.innerHd.getWidth(),
  1341. newPercent = Math.ceil(newWidth * view.maxColumnWidth / width) / 100,
  1342. disabled = this.headersDisabled,
  1343. headerCol = columns[index],
  1344. otherCol = columns[index + 1],
  1345. totalPercent = headerCol.width + otherCol.width;
  1346. this.proxy.remove();
  1347. headerCol.width = newPercent;
  1348. otherCol.width = totalPercent - newPercent;
  1349. delete this.dragHd;
  1350. view.setHdWidths();
  1351. view.refresh();
  1352. setTimeout(function(){
  1353. view.disableHeaders = disabled;
  1354. }, 100);
  1355. }
  1356. });
  1357. // Backwards compatibility alias
  1358. Ext.ListView.ColumnResizer = Ext.list.ColumnResizer;/**
  1359. * @class Ext.list.Sorter
  1360. * @extends Ext.util.Observable
  1361. * <p>Supporting Class for Ext.list.ListView</p>
  1362. * @constructor
  1363. * @param {Object} config
  1364. */
  1365. Ext.list.Sorter = Ext.extend(Ext.util.Observable, {
  1366. /**
  1367. * @cfg {Array} sortClasses
  1368. * The CSS classes applied to a header when it is sorted. (defaults to <tt>["sort-asc", "sort-desc"]</tt>)
  1369. */
  1370. sortClasses : ["sort-asc", "sort-desc"],
  1371. constructor: function(config){
  1372. Ext.apply(this, config);
  1373. Ext.list.Sorter.superclass.constructor.call(this);
  1374. },
  1375. init : function(listView){
  1376. this.view = listView;
  1377. listView.on('render', this.initEvents, this);
  1378. },
  1379. initEvents : function(view){
  1380. view.mon(view.innerHd, 'click', this.onHdClick, this);
  1381. view.innerHd.setStyle('cursor', 'pointer');
  1382. view.mon(view.store, 'datachanged', this.updateSortState, this);
  1383. this.updateSortState.defer(10, this, [view.store]);
  1384. },
  1385. updateSortState : function(store){
  1386. var state = store.getSortState();
  1387. if(!state){
  1388. return;
  1389. }
  1390. this.sortState = state;
  1391. var cs = this.view.columns, sortColumn = -1;
  1392. for(var i = 0, len = cs.length; i < len; i++){
  1393. if(cs[i].dataIndex == state.field){
  1394. sortColumn = i;
  1395. break;
  1396. }
  1397. }
  1398. if(sortColumn != -1){
  1399. var sortDir = state.direction;
  1400. this.updateSortIcon(sortColumn, sortDir);
  1401. }
  1402. },
  1403. updateSortIcon : function(col, dir){
  1404. var sc = this.sortClasses;
  1405. var hds = this.view.innerHd.select('em').removeClass(sc);
  1406. hds.item(col).addClass(sc[dir == "DESC" ? 1 : 0]);
  1407. },
  1408. onHdClick : function(e){
  1409. var hd = e.getTarget('em', 3);
  1410. if(hd && !this.view.disableHeaders){
  1411. var index = this.view.findHeaderIndex(hd);
  1412. this.view.store.sort(this.view.columns[index].dataIndex);
  1413. }
  1414. }
  1415. });
  1416. // Backwards compatibility alias
  1417. Ext.ListView.Sorter = Ext.list.Sorter;