Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / widgets / list / ListView.js
1 /*!
2  * Ext JS Library 3.0.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.ListView\r
9  * @extends Ext.DataView\r
10  * <p>Ext.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.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.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>19</tt> pixels)\r
142      */\r
143     scrollOffset : 19,\r
144     /**\r
145      * @cfg {Boolean/Object} columnResize\r
146      * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.ColumnResizer}\r
147      * to enable the columns to be resizable (defaults to <tt>true</tt>).\r
148      */\r
149     columnResize: true,\r
150     /**\r
151      * @cfg {Array} columns An array of column configuration objects, for example:\r
152      * <pre><code>\r
153 {\r
154     align: 'right',\r
155     dataIndex: 'size',\r
156     header: 'Size',\r
157     tpl: '{size:fileSize}',\r
158     width: .35\r
159 }\r
160      * </code></pre> \r
161      * Acceptable properties for each column configuration object are:\r
162      * <div class="mdetail-params"><ul>\r
163      * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property\r
164      * of the column. Defaults to <tt>'left'</tt>.</div></li>\r
165      * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.\r
166      * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>\r
167      * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.\r
168      * {@link Ext.grid.Column#header header} for details.</div></li>\r
169      * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the\r
170      * configuration string for {@link Ext.XTemplate}.  By default an {@link Ext.XTemplate}\r
171      * will be implicitly created using the <tt>dataIndex</tt>.</div></li>\r
172      * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width\r
173      * this column should be allocated.  Columns that have no width specified will be\r
174      * allocated with an equal percentage to fill 100% of the container width.  To easily take\r
175      * advantage of the full container width, leave the width of at least one column undefined.\r
176      * Note that if you do not want to take up the full width of the container, the width of\r
177      * every column needs to be explicitly defined.</div></li>\r
178      * </ul></div>\r
179      */\r
180     /**\r
181      * @cfg {Boolean/Object} columnSort\r
182      * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.Sorter}\r
183      * to enable the columns to be sortable (defaults to <tt>true</tt>).\r
184      */\r
185     columnSort: true,\r
186     /**\r
187      * @cfg {String/Array} internalTpl\r
188      * The template to be used for the header row.  See {@link #tpl} for more details.\r
189      */\r
190 \r
191     initComponent : function(){\r
192         if(this.columnResize){\r
193             this.colResizer = new Ext.ListView.ColumnResizer(this.colResizer);\r
194             this.colResizer.init(this);\r
195         }\r
196         if(this.columnSort){\r
197             this.colSorter = new Ext.ListView.Sorter(this.columnSort);\r
198             this.colSorter.init(this);\r
199         }\r
200         if(!this.internalTpl){\r
201             this.internalTpl = new Ext.XTemplate(\r
202                 '<div class="x-list-header"><div class="x-list-header-inner">',\r
203                     '<tpl for="columns">',\r
204                     '<div style="width:{width}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',\r
205                         '{header}',\r
206                     '</em></div>',\r
207                     '</tpl>',\r
208                     '<div class="x-clear"></div>',\r
209                 '</div></div>',\r
210                 '<div class="x-list-body"><div class="x-list-body-inner">',\r
211                 '</div></div>'\r
212             );\r
213         }\r
214         if(!this.tpl){\r
215             this.tpl = new Ext.XTemplate(\r
216                 '<tpl for="rows">',\r
217                     '<dl>',\r
218                         '<tpl for="parent.columns">',\r
219                         '<dt style="width:{width}%;text-align:{align};"><em unselectable="on">',\r
220                             '{[values.tpl.apply(parent)]}',\r
221                         '</em></dt>',\r
222                         '</tpl>',\r
223                         '<div class="x-clear"></div>',\r
224                     '</dl>',\r
225                 '</tpl>'\r
226             );\r
227         };\r
228         var cs = this.columns, allocatedWidth = 0, colsWithWidth = 0, len = cs.length;\r
229         for(var i = 0; i < len; i++){\r
230             var c = cs[i];\r
231             if(!c.tpl){\r
232                 c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');\r
233             }else if(Ext.isString(c.tpl)){\r
234                 c.tpl = new Ext.XTemplate(c.tpl);\r
235             }\r
236             c.align = c.align || 'left';\r
237             if(Ext.isNumber(c.width)){\r
238                 c.width *= 100;\r
239                 allocatedWidth += c.width;\r
240                 colsWithWidth++;\r
241             }\r
242         }\r
243         // auto calculate missing column widths\r
244         if(colsWithWidth < len){\r
245             var remaining = len - colsWithWidth;\r
246             if(allocatedWidth < 100){\r
247                 var perCol = ((100-allocatedWidth) / remaining);\r
248                 for(var j = 0; j < len; j++){\r
249                     var c = cs[j];\r
250                     if(!Ext.isNumber(c.width)){\r
251                         c.width = perCol;\r
252                     }\r
253                 }\r
254             }\r
255         }\r
256         Ext.ListView.superclass.initComponent.call(this);\r
257     },\r
258 \r
259     onRender : function(){\r
260         Ext.ListView.superclass.onRender.apply(this, arguments);\r
261 \r
262         this.internalTpl.overwrite(this.el, {columns: this.columns});\r
263         \r
264         this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);\r
265         this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);\r
266 \r
267         if(this.hideHeaders){\r
268             this.el.dom.firstChild.style.display = 'none';\r
269         }\r
270     },\r
271 \r
272     getTemplateTarget : function(){\r
273         return this.innerBody;\r
274     },\r
275 \r
276     /**\r
277      * <p>Function which can be overridden which returns the data object passed to this\r
278      * view's {@link #tpl template} to render the whole ListView. The returned object \r
279      * shall contain the following properties:</p>\r
280      * <div class="mdetail-params"><ul>\r
281      * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>\r
282      * <li><b>rows</b> : String<div class="sub-desc">See\r
283      * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>\r
284      * </ul></div>\r
285      * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.\r
286      * @param {Number} startIndex the index number of the Record being prepared for rendering.\r
287      * @return {Object} A data object containing properties to be processed by a repeating\r
288      * XTemplate as described above.\r
289      */\r
290     collectData : function(){\r
291         var rs = Ext.ListView.superclass.collectData.apply(this, arguments);\r
292         return {\r
293             columns: this.columns,\r
294             rows: rs\r
295         }\r
296     },\r
297 \r
298     verifyInternalSize : function(){\r
299         if(this.lastSize){\r
300             this.onResize(this.lastSize.width, this.lastSize.height);\r
301         }\r
302     },\r
303 \r
304     // private\r
305     onResize : function(w, h){\r
306         var bd = this.innerBody.dom;\r
307         var hd = this.innerHd.dom\r
308         if(!bd){\r
309             return;\r
310         }\r
311         var bdp = bd.parentNode;\r
312         if(Ext.isNumber(w)){\r
313             var sw = w - this.scrollOffset;\r
314             if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){\r
315                 bd.style.width = sw + 'px';\r
316                 hd.style.width = sw + 'px';\r
317             }else{\r
318                 bd.style.width = w + 'px';\r
319                 hd.style.width = w + 'px';\r
320                 setTimeout(function(){\r
321                     if((bdp.offsetWidth - bdp.clientWidth) > 10){\r
322                         bd.style.width = sw + 'px';\r
323                         hd.style.width = sw + 'px';\r
324                     }\r
325                 }, 10);\r
326             }\r
327         }\r
328         if(Ext.isNumber(h == 'number')){\r
329             bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';\r
330         }\r
331     },\r
332 \r
333     updateIndexes : function(){\r
334         Ext.ListView.superclass.updateIndexes.apply(this, arguments);\r
335         this.verifyInternalSize();\r
336     },\r
337 \r
338     findHeaderIndex : function(hd){\r
339         hd = hd.dom || hd;\r
340         var pn = hd.parentNode, cs = pn.parentNode.childNodes;\r
341         for(var i = 0, c; c = cs[i]; i++){\r
342             if(c == pn){\r
343                 return i;\r
344             }\r
345         }\r
346         return -1;\r
347     },\r
348 \r
349     setHdWidths : function(){\r
350         var els = this.innerHd.dom.getElementsByTagName('div');\r
351         for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){\r
352             els[i].style.width = cs[i].width + '%';\r
353         }\r
354     }\r
355 });\r
356 \r
357 Ext.reg('listview', Ext.ListView);