]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/data/util/sorter.js.uncompressed.js
update dojo to 1.7.3
[tt-rss.git] / lib / dojo / data / util / sorter.js.uncompressed.js
1 define("dojo/data/util/sorter", ["dojo/_base/lang"], function(lang) {
2         // module:
3         //              dojo/data/util/sorter
4         // summary:
5         //              TODOC
6
7 var sorter = lang.getObject("dojo.data.util.sorter", true);
8
9 sorter.basicComparator = function(      /*anything*/ a,
10                                                                                                         /*anything*/ b){
11         //      summary:
12         //              Basic comparision function that compares if an item is greater or less than another item
13         //      description:
14         //              returns 1 if a > b, -1 if a < b, 0 if equal.
15         //              'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
16         //              And compared to each other, null is equivalent to undefined.
17
18         //null is a problematic compare, so if null, we set to undefined.
19         //Makes the check logic simple, compact, and consistent
20         //And (null == undefined) === true, so the check later against null
21         //works for undefined and is less bytes.
22         var r = -1;
23         if(a === null){
24                 a = undefined;
25         }
26         if(b === null){
27                 b = undefined;
28         }
29         if(a == b){
30                 r = 0;
31         }else if(a > b || a == null){
32                 r = 1;
33         }
34         return r; //int {-1,0,1}
35 };
36
37 sorter.createSortFunction = function(   /* attributes array */sortSpec, /*dojo.data.core.Read*/ store){
38         //      summary:
39         //              Helper function to generate the sorting function based off the list of sort attributes.
40         //      description:
41         //              The sort function creation will look for a property on the store called 'comparatorMap'.  If it exists
42         //              it will look in the mapping for comparisons function for the attributes.  If one is found, it will
43         //              use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
44         //              Returns the sorting function for this particular list of attributes and sorting directions.
45         //
46         //      sortSpec: array
47         //              A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
48         //              The objects should be formatted as follows:
49         //              {
50         //                      attribute: "attributeName-string" || attribute,
51         //                      descending: true|false;   // Default is false.
52         //              }
53         //      store: object
54         //              The datastore object to look up item values from.
55         //
56         var sortFunctions=[];
57
58         function createSortFunction(attr, dir, comp, s){
59                 //Passing in comp and s (comparator and store), makes this
60                 //function much faster.
61                 return function(itemA, itemB){
62                         var a = s.getValue(itemA, attr);
63                         var b = s.getValue(itemB, attr);
64                         return dir * comp(a,b); //int
65                 };
66         }
67         var sortAttribute;
68         var map = store.comparatorMap;
69         var bc = sorter.basicComparator;
70         for(var i = 0; i < sortSpec.length; i++){
71                 sortAttribute = sortSpec[i];
72                 var attr = sortAttribute.attribute;
73                 if(attr){
74                         var dir = (sortAttribute.descending) ? -1 : 1;
75                         var comp = bc;
76                         if(map){
77                                 if(typeof attr !== "string" && ("toString" in attr)){
78                                          attr = attr.toString();
79                                 }
80                                 comp = map[attr] || bc;
81                         }
82                         sortFunctions.push(createSortFunction(attr,
83                                 dir, comp, store));
84                 }
85         }
86         return function(rowA, rowB){
87                 var i=0;
88                 while(i < sortFunctions.length){
89                         var ret = sortFunctions[i++](rowA, rowB);
90                         if(ret !== 0){
91                                 return ret;//int
92                         }
93                 }
94                 return 0; //int
95         }; // Function
96 };
97
98 return sorter;
99 });