Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / list / ListView.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.list.ListView\r
9  * @extends Ext.DataView\r
10  * <p>Ext.list.ListView is a fast and light-weight implentation of a\r
11  * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>\r
12  * <div class="mdetail-params"><ul>\r
13  * <li>resizable columns</li>\r
14  * <li>selectable</li>\r
15  * <li>column widths are initially proportioned by percentage based on the container\r
16  * width and number of columns</li>\r
17  * <li>uses templates to render the data in any required format</li>\r
18  * <li>no horizontal scrolling</li>\r
19  * <li>no editing</li>\r
20  * </ul></div>\r
21  * <p>Example usage:</p>\r
22  * <pre><code>\r
23 // consume JSON of this form:\r
24 {\r
25    "images":[\r
26       {\r
27          "name":"dance_fever.jpg",\r
28          "size":2067,\r
29          "lastmod":1236974993000,\r
30          "url":"images\/thumbs\/dance_fever.jpg"\r
31       },\r
32       {\r
33          "name":"zack_sink.jpg",\r
34          "size":2303,\r
35          "lastmod":1236974993000,\r
36          "url":"images\/thumbs\/zack_sink.jpg"\r
37       }\r
38    ]\r
39\r
40 var store = new Ext.data.JsonStore({\r
41     url: 'get-images.php',\r
42     root: 'images',\r
43     fields: [\r
44         'name', 'url',\r
45         {name:'size', type: 'float'},\r
46         {name:'lastmod', type:'date', dateFormat:'timestamp'}\r
47     ]\r
48 });\r
49 store.load();\r
50 \r
51 var listView = new Ext.list.ListView({\r
52     store: store,\r
53     multiSelect: true,\r
54     emptyText: 'No images to display',\r
55     reserveScrollOffset: true,\r
56     columns: [{\r
57         header: 'File',\r
58         width: .5,\r
59         dataIndex: 'name'\r
60     },{\r
61         header: 'Last Modified',\r
62         width: .35, \r
63         dataIndex: 'lastmod',\r
64         tpl: '{lastmod:date("m-d h:i a")}'\r
65     },{\r
66         header: 'Size',\r
67         dataIndex: 'size',\r
68         tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()\r
69         align: 'right'\r
70     }]\r
71 });\r
72 \r
73 // put it in a Panel so it looks pretty\r
74 var panel = new Ext.Panel({\r
75     id:'images-view',\r
76     width:425,\r
77     height:250,\r
78     collapsible:true,\r
79     layout:'fit',\r
80     title:'Simple ListView <i>(0 items selected)</i>',\r
81     items: listView\r
82 });\r
83 panel.render(document.body);\r
84 \r
85 // little bit of feedback\r
86 listView.on('selectionchange', function(view, nodes){\r
87     var l = nodes.length;\r
88     var s = l != 1 ? 's' : '';\r
89     panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');\r
90 });\r
91  * </code></pre>\r
92  * @constructor\r
93  * @param {Object} config\r
94  * @xtype listview\r
95  */\r
96 Ext.list.ListView = Ext.extend(Ext.DataView, {\r
97     /**\r
98      * Set this property to <tt>true</tt> to disable the header click handler disabling sort\r
99      * (defaults to <tt>false</tt>).\r
100      * @type Boolean\r
101      * @property disableHeaders\r
102      */\r
103     /**\r
104      * @cfg {Boolean} hideHeaders\r
105      * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so\r
106      * the {@link #internalTpl header row} will be shown).\r
107      */\r
108     /**\r
109      * @cfg {String} itemSelector\r
110      * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.\r
111      * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)\r
112      * that will be used to determine what nodes the ListView will be working with.   \r
113      */\r
114     itemSelector: 'dl',\r
115     /**\r
116      * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to\r
117      * <tt>'x-list-selected'</tt>). An example overriding the default styling:\r
118     <pre><code>\r
119     .x-list-selected {background-color: yellow;}\r
120     </code></pre>\r
121      * @type String\r
122      */\r
123     selectedClass:'x-list-selected',\r
124     /**\r
125      * @cfg {String} overClass The CSS class applied when over a row (defaults to\r
126      * <tt>'x-list-over'</tt>). An example overriding the default styling:\r
127     <pre><code>\r
128     .x-list-over {background-color: orange;}\r
129     </code></pre>\r
130      * @type String\r
131      */\r
132     overClass:'x-list-over',\r
133     /**\r
134      * @cfg {Boolean} reserveScrollOffset\r
135      * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>\r
136      * for 10 milliseconds.  Specify <tt>true</tt> to account for the configured\r
137      * <b><tt>{@link #scrollOffset}</tt></b> immediately.\r
138      */\r
139     /**\r
140      * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to\r
141      * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically\r
142      * calculated.\r
143      */\r
144     scrollOffset : undefined,\r
145     /**\r
146      * @cfg {Boolean/Object} columnResize\r
147      * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.ColumnResizer}\r
148      * to enable the columns to be resizable (defaults to <tt>true</tt>).\r
149      */\r
150     columnResize: true,\r
151     /**\r
152      * @cfg {Array} columns An array of column configuration objects, for example:\r
153      * <pre><code>\r
154 {\r
155     align: 'right',\r
156     dataIndex: 'size',\r
157     header: 'Size',\r
158     tpl: '{size:fileSize}',\r
159     width: .35\r
160 }\r
161      * </code></pre> \r
162      * Acceptable properties for each column configuration object are:\r
163      * <div class="mdetail-params"><ul>\r
164      * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property\r
165      * of the column. Defaults to <tt>'left'</tt>.</div></li>\r
166      * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.\r
167      * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>\r
168      * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.\r
169      * {@link Ext.grid.Column#header header} for details.</div></li>\r
170      * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the\r
171      * configuration string for {@link Ext.XTemplate}.  By default an {@link Ext.XTemplate}\r
172      * will be implicitly created using the <tt>dataIndex</tt>.</div></li>\r
173      * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width\r
174      * this column should be allocated.  Columns that have no width specified will be\r
175      * allocated with an equal percentage to fill 100% of the container width.  To easily take\r
176      * advantage of the full container width, leave the width of at least one column undefined.\r
177      * Note that if you do not want to take up the full width of the container, the width of\r
178      * every column needs to be explicitly defined.</div></li>\r
179      * </ul></div>\r
180      */\r
181     /**\r
182      * @cfg {Boolean/Object} columnSort\r
183      * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.Sorter}\r
184      * to enable the columns to be sortable (defaults to <tt>true</tt>).\r
185      */\r
186     columnSort: true,\r
187     /**\r
188      * @cfg {String/Array} internalTpl\r
189      * The template to be used for the header row.  See {@link #tpl} for more details.\r
190      */\r
191 \r
192     /*\r
193      * IE has issues when setting percentage based widths to 100%. Default to 99.\r
194      */\r
195     maxWidth: Ext.isIE ? 99 : 100,\r
196     \r
197     initComponent : function(){\r
198         if(this.columnResize){\r
199             this.colResizer = new Ext.list.ColumnResizer(this.colResizer);\r
200             this.colResizer.init(this);\r
201         }\r
202         if(this.columnSort){\r
203             this.colSorter = new Ext.list.Sorter(this.columnSort);\r
204             this.colSorter.init(this);\r
205         }\r
206         if(!this.internalTpl){\r
207             this.internalTpl = new Ext.XTemplate(\r
208                 '<div class="x-list-header"><div class="x-list-header-inner">',\r
209                     '<tpl for="columns">',\r
210                     '<div style="width:{[values.width*100]}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',\r
211                         '{header}',\r
212                     '</em></div>',\r
213                     '</tpl>',\r
214                     '<div class="x-clear"></div>',\r
215                 '</div></div>',\r
216                 '<div class="x-list-body"><div class="x-list-body-inner">',\r
217                 '</div></div>'\r
218             );\r
219         }\r
220         if(!this.tpl){\r
221             this.tpl = new Ext.XTemplate(\r
222                 '<tpl for="rows">',\r
223                     '<dl>',\r
224                         '<tpl for="parent.columns">',\r
225                         '<dt style="width:{[values.width*100]}%;text-align:{align};">',\r
226                         '<em unselectable="on"<tpl if="cls"> class="{cls}</tpl>">',\r
227                             '{[values.tpl.apply(parent)]}',\r
228                         '</em></dt>',\r
229                         '</tpl>',\r
230                         '<div class="x-clear"></div>',\r
231                     '</dl>',\r
232                 '</tpl>'\r
233             );\r
234         };\r
235         \r
236         var cs = this.columns, \r
237             allocatedWidth = 0, \r
238             colsWithWidth = 0, \r
239             len = cs.length, \r
240             columns = [];\r
241             \r
242         for(var i = 0; i < len; i++){\r
243             var c = cs[i];\r
244             if(!c.isColumn) {\r
245                 c.xtype = c.xtype ? (/^lv/.test(c.xtype) ? c.xtype : 'lv' + c.xtype) : 'lvcolumn';\r
246                 c = Ext.create(c);\r
247             }\r
248             if(c.width) {\r
249                 allocatedWidth += c.width*100;\r
250                 colsWithWidth++;\r
251             }\r
252             columns.push(c);\r
253         }\r
254         \r
255         cs = this.columns = columns;\r
256         \r
257         // auto calculate missing column widths\r
258         if(colsWithWidth < len){\r
259             var remaining = len - colsWithWidth;\r
260             if(allocatedWidth < this.maxWidth){\r
261                 var perCol = ((this.maxWidth-allocatedWidth) / remaining)/100;\r
262                 for(var j = 0; j < len; j++){\r
263                     var c = cs[j];\r
264                     if(!c.width){\r
265                         c.width = perCol;\r
266                     }\r
267                 }\r
268             }\r
269         }\r
270         Ext.list.ListView.superclass.initComponent.call(this);\r
271     },\r
272 \r
273     onRender : function(){\r
274         this.autoEl = {\r
275             cls: 'x-list-wrap'  \r
276         };\r
277         Ext.list.ListView.superclass.onRender.apply(this, arguments);\r
278 \r
279         this.internalTpl.overwrite(this.el, {columns: this.columns});\r
280         \r
281         this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);\r
282         this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);\r
283 \r
284         if(this.hideHeaders){\r
285             this.el.dom.firstChild.style.display = 'none';\r
286         }\r
287     },\r
288 \r
289     getTemplateTarget : function(){\r
290         return this.innerBody;\r
291     },\r
292 \r
293     /**\r
294      * <p>Function which can be overridden which returns the data object passed to this\r
295      * view's {@link #tpl template} to render the whole ListView. The returned object \r
296      * shall contain the following properties:</p>\r
297      * <div class="mdetail-params"><ul>\r
298      * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>\r
299      * <li><b>rows</b> : String<div class="sub-desc">See\r
300      * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>\r
301      * </ul></div>\r
302      * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.\r
303      * @param {Number} startIndex the index number of the Record being prepared for rendering.\r
304      * @return {Object} A data object containing properties to be processed by a repeating\r
305      * XTemplate as described above.\r
306      */\r
307     collectData : function(){\r
308         var rs = Ext.list.ListView.superclass.collectData.apply(this, arguments);\r
309         return {\r
310             columns: this.columns,\r
311             rows: rs\r
312         }\r
313     },\r
314 \r
315     verifyInternalSize : function(){\r
316         if(this.lastSize){\r
317             this.onResize(this.lastSize.width, this.lastSize.height);\r
318         }\r
319     },\r
320 \r
321     // private\r
322     onResize : function(w, h){\r
323         var bd = this.innerBody.dom;\r
324         var hd = this.innerHd.dom\r
325         if(!bd){\r
326             return;\r
327         }\r
328         var bdp = bd.parentNode;\r
329         if(Ext.isNumber(w)){\r
330             var sw = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth());\r
331             if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){\r
332                 bd.style.width = sw + 'px';\r
333                 hd.style.width = sw + 'px';\r
334             }else{\r
335                 bd.style.width = w + 'px';\r
336                 hd.style.width = w + 'px';\r
337                 setTimeout(function(){\r
338                     if((bdp.offsetWidth - bdp.clientWidth) > 10){\r
339                         bd.style.width = sw + 'px';\r
340                         hd.style.width = sw + 'px';\r
341                     }\r
342                 }, 10);\r
343             }\r
344         }\r
345         if(Ext.isNumber(h)){\r
346             bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';\r
347         }\r
348     },\r
349 \r
350     updateIndexes : function(){\r
351         Ext.list.ListView.superclass.updateIndexes.apply(this, arguments);\r
352         this.verifyInternalSize();\r
353     },\r
354 \r
355     findHeaderIndex : function(hd){\r
356         hd = hd.dom || hd;\r
357         var pn = hd.parentNode, cs = pn.parentNode.childNodes;\r
358         for(var i = 0, c; c = cs[i]; i++){\r
359             if(c == pn){\r
360                 return i;\r
361             }\r
362         }\r
363         return -1;\r
364     },\r
365 \r
366     setHdWidths : function(){\r
367         var els = this.innerHd.dom.getElementsByTagName('div');\r
368         for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){\r
369             els[i].style.width = (cs[i].width*100) + '%';\r
370         }\r
371     }\r
372 });\r
373 \r
374 Ext.reg('listview', Ext.list.ListView);\r
375 \r
376 // Backwards compatibility alias\r
377 Ext.ListView = Ext.list.ListView;