Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Sorter.html
index c30d305..161e812 100644 (file)
+<!DOCTYPE html>
 <html>
 <head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>The source code</title>
-    <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
-    <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
+  <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
+  <script type="text/javascript" src="../prettify/prettify.js"></script>
+  <style type="text/css">
+    .highlight { display: block; background-color: #ddd; }
+  </style>
+  <script type="text/javascript">
+    function highlight() {
+      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
+    }
+  </script>
 </head>
-<body  onload="prettyPrint();">
-    <pre class="prettyprint lang-js">/*!
- * Ext JS Library 3.0.3
- * Copyright(c) 2006-2009 Ext JS, LLC
- * licensing@extjs.com
- * http://www.extjs.com/license
+<body onload="prettyPrint(); highlight();">
+  <pre class="prettyprint lang-js"><span id='Ext-util-Sorter'>/**
+</span> * @class Ext.util.Sorter
+ * @extends Object
+ * Represents a single sorter that can be applied to a Store
  */
-<div id="cls-Ext.ListView.Sorter"></div>/**\r
- * @class Ext.ListView.Sorter\r
- * @extends Ext.util.Observable\r
- * <p>Supporting Class for Ext.ListView.</p>\r
- * @constructor\r
- * @param {Object} config\r
- */\r
-Ext.ListView.Sorter = Ext.extend(Ext.util.Observable, {\r
-    <div id="cfg-Ext.ListView.Sorter-sortClasses"></div>/**\r
-     * @cfg {Array} sortClasses\r
-     * The CSS classes applied to a header when it is sorted. (defaults to <tt>["sort-asc", "sort-desc"]</tt>)\r
-     */\r
-    sortClasses : ["sort-asc", "sort-desc"],\r
-\r
-    constructor: function(config){\r
-        Ext.apply(this, config);\r
-        Ext.ListView.Sorter.superclass.constructor.call(this);\r
-    },\r
-\r
-    init : function(listView){\r
-        this.view = listView;\r
-        listView.on('render', this.initEvents, this);\r
-    },\r
-\r
-    initEvents : function(view){\r
-        view.mon(view.innerHd, 'click', this.onHdClick, this);\r
-        view.innerHd.setStyle('cursor', 'pointer');\r
-        view.mon(view.store, 'datachanged', this.updateSortState, this);\r
-        this.updateSortState.defer(10, this, [view.store]);\r
-    },\r
-\r
-    updateSortState : function(store){\r
-        var state = store.getSortState();\r
-        if(!state){\r
-            return;\r
-        }\r
-        this.sortState = state;\r
-        var cs = this.view.columns, sortColumn = -1;\r
-        for(var i = 0, len = cs.length; i < len; i++){\r
-            if(cs[i].dataIndex == state.field){\r
-                sortColumn = i;\r
-                break;\r
-            }\r
-        }\r
-        if(sortColumn != -1){\r
-            var sortDir = state.direction;\r
-            this.updateSortIcon(sortColumn, sortDir);\r
-        }\r
-    },\r
-\r
-    updateSortIcon : function(col, dir){\r
-        var sc = this.sortClasses;\r
-        var hds = this.view.innerHd.select('em').removeClass(sc);\r
-        hds.item(col).addClass(sc[dir == "DESC" ? 1 : 0]);\r
-    },\r
-\r
-    onHdClick : function(e){\r
-        var hd = e.getTarget('em', 3);\r
-        if(hd && !this.view.disableHeaders){\r
-            var index = this.view.findHeaderIndex(hd);\r
-            this.view.store.sort(this.view.columns[index].dataIndex);\r
-        }\r
-    }\r
+Ext.define('Ext.util.Sorter', {
+
+<span id='Ext-util-Sorter-cfg-property'>    /**
+</span>     * @cfg {String} property The property to sort by. Required unless {@link #sorter} is provided
+     */
+    
+<span id='Ext-util-Sorter-cfg-sorterFn'>    /**
+</span>     * @cfg {Function} sorterFn A specific sorter function to execute. Can be passed instead of {@link #property}
+     */
+    
+<span id='Ext-util-Sorter-cfg-root'>    /**
+</span>     * @cfg {String} root Optional root property. This is mostly useful when sorting a Store, in which case we set the
+     * root to 'data' to make the filter pull the {@link #property} out of the data object of each item
+     */
+    
+<span id='Ext-util-Sorter-cfg-transform'>    /**
+</span>     * @cfg {Function} transform A function that will be run on each value before
+     * it is compared in the sorter. The function will receive a single argument,
+     * the value.
+     */
+    
+<span id='Ext-util-Sorter-cfg-direction'>    /**
+</span>     * @cfg {String} direction The direction to sort by. Defaults to ASC
+     */
+    direction: &quot;ASC&quot;,
+    
+    constructor: function(config) {
+        var me = this;
+        
+        Ext.apply(me, config);
+        
+        //&lt;debug&gt;
+        if (me.property == undefined &amp;&amp; me.sorterFn == undefined) {
+            Ext.Error.raise(&quot;A Sorter requires either a property or a sorter function&quot;);
+        }
+        //&lt;/debug&gt;
+        
+        me.updateSortFunction();
+    },
+    
+<span id='Ext-util-Sorter-method-createSortFunction'>    /**
+</span>     * @private
+     * Creates and returns a function which sorts an array by the given property and direction
+     * @return {Function} A function which sorts by the property/direction combination provided
+     */
+    createSortFunction: function(sorterFn) {
+        var me        = this,
+            property  = me.property,
+            direction = me.direction || &quot;ASC&quot;,
+            modifier  = direction.toUpperCase() == &quot;DESC&quot; ? -1 : 1;
+        
+        //create a comparison function. Takes 2 objects, returns 1 if object 1 is greater,
+        //-1 if object 2 is greater or 0 if they are equal
+        return function(o1, o2) {
+            return modifier * sorterFn.call(me, o1, o2);
+        };
+    },
+    
+<span id='Ext-util-Sorter-method-defaultSorterFn'>    /**
+</span>     * @private
+     * Basic default sorter function that just compares the defined property of each object
+     */
+    defaultSorterFn: function(o1, o2) {
+        var me = this,
+            transform = me.transform,
+            v1 = me.getRoot(o1)[me.property],
+            v2 = me.getRoot(o2)[me.property];
+            
+        if (transform) {
+            v1 = transform(v1);
+            v2 = transform(v2);
+        }
+
+        return v1 &gt; v2 ? 1 : (v1 &lt; v2 ? -1 : 0);
+    },
+    
+<span id='Ext-util-Sorter-method-getRoot'>    /**
+</span>     * @private
+     * Returns the root property of the given item, based on the configured {@link #root} property
+     * @param {Object} item The item
+     * @return {Object} The root property of the object
+     */
+    getRoot: function(item) {
+        return this.root == undefined ? item : item[this.root];
+    },
+    
+    // @TODO: Add docs for these three methods
+    setDirection: function(direction) {
+        var me = this;
+        me.direction = direction;
+        me.updateSortFunction();
+    },
+    
+    toggle: function() {
+        var me = this;
+        me.direction = Ext.String.toggle(me.direction, &quot;ASC&quot;, &quot;DESC&quot;);
+        me.updateSortFunction();
+    },
+    
+    updateSortFunction: function() {
+        var me = this;
+        me.sort = me.createSortFunction(me.sorterFn || me.defaultSorterFn);
+    }
 });</pre>
 </body>
-</html>
\ No newline at end of file
+</html>