/hippo/src/main/webapp/ext/src/widgets/list/Sorter.js

http://hdbc.googlecode.com/ · JavaScript · 70 lines · 47 code · 6 blank · 17 comment · 9 complexity · 41d2ac2e390ce1363683d04425d2157e MD5 · raw file

  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. /**
  8. * @class Ext.ListView.Sorter
  9. * @extends Ext.util.Observable
  10. * <p>Supporting Class for Ext.ListView.</p>
  11. * @constructor
  12. * @param {Object} config
  13. */
  14. Ext.ListView.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. constructor: function(config){
  21. Ext.apply(this, config);
  22. Ext.ListView.Sorter.superclass.constructor.call(this);
  23. },
  24. init : function(listView){
  25. this.view = listView;
  26. listView.on('render', this.initEvents, this);
  27. },
  28. initEvents : function(view){
  29. view.mon(view.innerHd, 'click', this.onHdClick, this);
  30. view.innerHd.setStyle('cursor', 'pointer');
  31. view.mon(view.store, 'datachanged', this.updateSortState, this);
  32. this.updateSortState.defer(10, this, [view.store]);
  33. },
  34. updateSortState : function(store){
  35. var state = store.getSortState();
  36. if(!state){
  37. return;
  38. }
  39. this.sortState = state;
  40. var cs = this.view.columns, sortColumn = -1;
  41. for(var i = 0, len = cs.length; i < len; i++){
  42. if(cs[i].dataIndex == state.field){
  43. sortColumn = i;
  44. break;
  45. }
  46. }
  47. if(sortColumn != -1){
  48. var sortDir = state.direction;
  49. this.updateSortIcon(sortColumn, sortDir);
  50. }
  51. },
  52. updateSortIcon : function(col, dir){
  53. var sc = this.sortClasses;
  54. var hds = this.view.innerHd.select('em').removeClass(sc);
  55. hds.item(col).addClass(sc[dir == "DESC" ? 1 : 0]);
  56. },
  57. onHdClick : function(e){
  58. var hd = e.getTarget('em', 3);
  59. if(hd && !this.view.disableHeaders){
  60. var index = this.view.findHeaderIndex(hd);
  61. this.view.store.sort(this.view.columns[index].dataIndex);
  62. }
  63. }
  64. });