Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / widgets / list / Sorter.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.list.Sorter
9  * @extends Ext.util.Observable
10  * <p>Supporting Class for Ext.list.ListView</p>
11  * @constructor
12  * @param {Object} config
13  */
14 Ext.list.Sorter = Ext.extend(Ext.util.Observable, {
15     /**
16      * @cfg {Array} sortClasses
17      * The CSS classes applied to a header when it is sorted. (defaults to <tt>["sort-asc", "sort-desc"]</tt>)
18      */
19     sortClasses : ["sort-asc", "sort-desc"],
20
21     constructor: function(config){
22         Ext.apply(this, config);
23         Ext.list.Sorter.superclass.constructor.call(this);
24     },
25
26     init : function(listView){
27         this.view = listView;
28         listView.on('render', this.initEvents, this);
29     },
30
31     initEvents : function(view){
32         view.mon(view.innerHd, 'click', this.onHdClick, this);
33         view.innerHd.setStyle('cursor', 'pointer');
34         view.mon(view.store, 'datachanged', this.updateSortState, this);
35         this.updateSortState.defer(10, this, [view.store]);
36     },
37
38     updateSortState : function(store){
39         var state = store.getSortState();
40         if(!state){
41             return;
42         }
43         this.sortState = state;
44         var cs = this.view.columns, sortColumn = -1;
45         for(var i = 0, len = cs.length; i < len; i++){
46             if(cs[i].dataIndex == state.field){
47                 sortColumn = i;
48                 break;
49             }
50         }
51         if(sortColumn != -1){
52             var sortDir = state.direction;
53             this.updateSortIcon(sortColumn, sortDir);
54         }
55     },
56
57     updateSortIcon : function(col, dir){
58         var sc = this.sortClasses;
59         var hds = this.view.innerHd.select('em').removeClass(sc);
60         hds.item(col).addClass(sc[dir == "DESC" ? 1 : 0]);
61     },
62
63     onHdClick : function(e){
64         var hd = e.getTarget('em', 3);
65         if(hd && !this.view.disableHeaders){
66             var index = this.view.findHeaderIndex(hd);
67             this.view.store.sort(this.view.columns[index].dataIndex);
68         }
69     }
70 });
71
72 // Backwards compatibility alias
73 Ext.ListView.Sorter = Ext.list.Sorter;