Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / data / SortTypes.js
1 /**
2  * @class Ext.data.SortTypes
3  * This class defines a series of static methods that are used on a
4  * {@link Ext.data.Field} for performing sorting. The methods cast the 
5  * underlying values into a data type that is appropriate for sorting on
6  * that particular field.  If a {@link Ext.data.Field#type} is specified, 
7  * the sortType will be set to a sane default if the sortType is not 
8  * explicitly defined on the field. The sortType will make any necessary
9  * modifications to the value and return it.
10  * <ul>
11  * <li><b>asText</b> - Removes any tags and converts the value to a string</li>
12  * <li><b>asUCText</b> - Removes any tags and converts the value to an uppercase string</li>
13  * <li><b>asUCText</b> - Converts the value to an uppercase string</li>
14  * <li><b>asDate</b> - Converts the value into Unix epoch time</li>
15  * <li><b>asFloat</b> - Converts the value to a floating point number</li>
16  * <li><b>asInt</b> - Converts the value to an integer number</li>
17  * </ul>
18  * <p>
19  * It is also possible to create a custom sortType that can be used throughout
20  * an application.
21  * <pre><code>
22 Ext.apply(Ext.data.SortTypes, {
23     asPerson: function(person){
24         // expects an object with a first and last name property
25         return person.lastName.toUpperCase() + person.firstName.toLowerCase();
26     }    
27 });
28
29 Ext.define('Employee', {
30     extend: 'Ext.data.Model',
31     fields: [{
32         name: 'person',
33         sortType: 'asPerson'
34     }, {
35         name: 'salary',
36         type: 'float' // sortType set to asFloat
37     }]
38 });
39  * </code></pre>
40  * </p>
41  * @singleton
42  * @docauthor Evan Trimboli <evan@sencha.com>
43  */
44 Ext.define('Ext.data.SortTypes', {
45     
46     singleton: true,
47     
48     /**
49      * Default sort that does nothing
50      * @param {Mixed} s The value being converted
51      * @return {Mixed} The comparison value
52      */
53     none : function(s) {
54         return s;
55     },
56
57     /**
58      * The regular expression used to strip tags
59      * @type {RegExp}
60      * @property
61      */
62     stripTagsRE : /<\/?[^>]+>/gi,
63
64     /**
65      * Strips all HTML tags to sort on text only
66      * @param {Mixed} s The value being converted
67      * @return {String} The comparison value
68      */
69     asText : function(s) {
70         return String(s).replace(this.stripTagsRE, "");
71     },
72
73     /**
74      * Strips all HTML tags to sort on text only - Case insensitive
75      * @param {Mixed} s The value being converted
76      * @return {String} The comparison value
77      */
78     asUCText : function(s) {
79         return String(s).toUpperCase().replace(this.stripTagsRE, "");
80     },
81
82     /**
83      * Case insensitive string
84      * @param {Mixed} s The value being converted
85      * @return {String} The comparison value
86      */
87     asUCString : function(s) {
88         return String(s).toUpperCase();
89     },
90
91     /**
92      * Date sorting
93      * @param {Mixed} s The value being converted
94      * @return {Number} The comparison value
95      */
96     asDate : function(s) {
97         if(!s){
98             return 0;
99         }
100         if(Ext.isDate(s)){
101             return s.getTime();
102         }
103         return Date.parse(String(s));
104     },
105
106     /**
107      * Float sorting
108      * @param {Mixed} s The value being converted
109      * @return {Float} The comparison value
110      */
111     asFloat : function(s) {
112         var val = parseFloat(String(s).replace(/,/g, ""));
113         return isNaN(val) ? 0 : val;
114     },
115
116     /**
117      * Integer sorting
118      * @param {Mixed} s The value being converted
119      * @return {Number} The comparison value
120      */
121     asInt : function(s) {
122         var val = parseInt(String(s).replace(/,/g, ""), 10);
123         return isNaN(val) ? 0 : val;
124     }
125 });