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