2150 |
mathias |
1 |
if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojo.data.util.sorter"] = true;
|
|
|
3 |
dojo.provide("dojo.data.util.sorter");
|
|
|
4 |
|
|
|
5 |
dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
|
|
|
6 |
/*anything*/ b){
|
|
|
7 |
// summary:
|
|
|
8 |
// Basic comparision function that compares if an item is greater or less than another item
|
|
|
9 |
// description:
|
|
|
10 |
// returns 1 if a > b, -1 if a < b, 0 if equal.
|
|
|
11 |
// undefined values are treated as larger values so that they're pushed to the end of the list.
|
|
|
12 |
|
|
|
13 |
var ret = 0;
|
|
|
14 |
if(a > b || typeof a === "undefined" || a === null){
|
|
|
15 |
ret = 1;
|
|
|
16 |
}else if(a < b || typeof b === "undefined" || b === null){
|
|
|
17 |
ret = -1;
|
|
|
18 |
}
|
|
|
19 |
return ret; //int, {-1,0,1}
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
|
|
|
23 |
/*dojo.data.core.Read*/ store){
|
|
|
24 |
// summary:
|
|
|
25 |
// Helper function to generate the sorting function based off the list of sort attributes.
|
|
|
26 |
// description:
|
|
|
27 |
// The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
|
|
|
28 |
// it will look in the mapping for comparisons function for the attributes. If one is found, it will
|
|
|
29 |
// use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
|
|
|
30 |
// Returns the sorting function for this particular list of attributes and sorting directions.
|
|
|
31 |
//
|
|
|
32 |
// sortSpec: array
|
|
|
33 |
// A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
|
|
|
34 |
// The objects should be formatted as follows:
|
|
|
35 |
// {
|
|
|
36 |
// attribute: "attributeName-string" || attribute,
|
|
|
37 |
// descending: true|false; // Default is false.
|
|
|
38 |
// }
|
|
|
39 |
// store: object
|
|
|
40 |
// The datastore object to look up item values from.
|
|
|
41 |
//
|
|
|
42 |
var sortFunctions=[];
|
|
|
43 |
|
|
|
44 |
function createSortFunction(attr, dir){
|
|
|
45 |
return function(itemA, itemB){
|
|
|
46 |
var a = store.getValue(itemA, attr);
|
|
|
47 |
var b = store.getValue(itemB, attr);
|
|
|
48 |
//See if we have a override for an attribute comparison.
|
|
|
49 |
var comparator = null;
|
|
|
50 |
if(store.comparatorMap){
|
|
|
51 |
if(typeof attr !== "string"){
|
|
|
52 |
attr = store.getIdentity(attr);
|
|
|
53 |
}
|
|
|
54 |
comparator = store.comparatorMap[attr]||dojo.data.util.sorter.basicComparator;
|
|
|
55 |
}
|
|
|
56 |
comparator = comparator||dojo.data.util.sorter.basicComparator;
|
|
|
57 |
return dir * comparator(a,b); //int
|
|
|
58 |
};
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
for(var i = 0; i < sortSpec.length; i++){
|
|
|
62 |
sortAttribute = sortSpec[i];
|
|
|
63 |
if(sortAttribute.attribute){
|
|
|
64 |
var direction = (sortAttribute.descending) ? -1 : 1;
|
|
|
65 |
sortFunctions.push(createSortFunction(sortAttribute.attribute, direction));
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return function(rowA, rowB){
|
|
|
70 |
var i=0;
|
|
|
71 |
while(i < sortFunctions.length){
|
|
|
72 |
var ret = sortFunctions[i++](rowA, rowB);
|
|
|
73 |
if(ret !== 0){
|
|
|
74 |
return ret;//int
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
return 0; //int
|
|
|
78 |
}; // Function
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
}
|