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