]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/util/sorter.js.uncompressed.js
modify dojo rebuild script to remove uncompressed files
[tt-rss.git] / lib / dojo / data / util / sorter.js.uncompressed.js
1 define("dojo/data/util/sorter", ["../../_base/lang"], function(lang){
2 // module:
3 // dojo/data/util/sorter
4 // summary:
5 // TODOC
6
7 var sorter = {};
8 lang.setObject("dojo.data.util.sorter", sorter);
9
10 sorter.basicComparator = function( /*anything*/ a,
11 /*anything*/ b){
12 // summary:
13 // Basic comparison function that compares if an item is greater or less than another item
14 // description:
15 // returns 1 if a > b, -1 if a < b, 0 if equal.
16 // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
17 // And compared to each other, null is equivalent to undefined.
18
19 //null is a problematic compare, so if null, we set to undefined.
20 //Makes the check logic simple, compact, and consistent
21 //And (null == undefined) === true, so the check later against null
22 //works for undefined and is less bytes.
23 var r = -1;
24 if(a === null){
25 a = undefined;
26 }
27 if(b === null){
28 b = undefined;
29 }
30 if(a == b){
31 r = 0;
32 }else if(a > b || a == null){
33 r = 1;
34 }
35 return r; //int {-1,0,1}
36 };
37
38 sorter.createSortFunction = function( /* attributes[] */sortSpec, /*dojo/data/api/Read*/ store){
39 // summary:
40 // Helper function to generate the sorting function based off the list of sort attributes.
41 // description:
42 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
43 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
44 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
45 // Returns the sorting function for this particular list of attributes and sorting directions.
46 // sortSpec:
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:
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 });