e44a7e37b6
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f
81 lines
2.8 KiB
JavaScript
81 lines
2.8 KiB
JavaScript
if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
dojo._hasResource["dojo.data.util.sorter"] = true;
|
|
dojo.provide("dojo.data.util.sorter");
|
|
|
|
dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
|
|
/*anything*/ b){
|
|
// summary:
|
|
// Basic comparision function that compares if an item is greater or less than another item
|
|
// description:
|
|
// returns 1 if a > b, -1 if a < b, 0 if equal.
|
|
// undefined values are treated as larger values so that they're pushed to the end of the list.
|
|
|
|
var ret = 0;
|
|
if(a > b || typeof a === "undefined" || a === null){
|
|
ret = 1;
|
|
}else if(a < b || typeof b === "undefined" || b === null){
|
|
ret = -1;
|
|
}
|
|
return ret; //int, {-1,0,1}
|
|
};
|
|
|
|
dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
|
|
/*dojo.data.core.Read*/ store){
|
|
// summary:
|
|
// Helper function to generate the sorting function based off the list of sort attributes.
|
|
// description:
|
|
// The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
|
|
// it will look in the mapping for comparisons function for the attributes. If one is found, it will
|
|
// use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
|
|
// Returns the sorting function for this particular list of attributes and sorting directions.
|
|
//
|
|
// sortSpec: array
|
|
// A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
|
|
// The objects should be formatted as follows:
|
|
// {
|
|
// attribute: "attributeName-string" || attribute,
|
|
// descending: true|false; // Default is false.
|
|
// }
|
|
// store: object
|
|
// The datastore object to look up item values from.
|
|
//
|
|
var sortFunctions=[];
|
|
|
|
function createSortFunction(attr, dir){
|
|
return function(itemA, itemB){
|
|
var a = store.getValue(itemA, attr);
|
|
var b = store.getValue(itemB, attr);
|
|
//See if we have a override for an attribute comparison.
|
|
var comparator = null;
|
|
if(store.comparatorMap){
|
|
if(typeof attr !== "string"){
|
|
attr = store.getIdentity(attr);
|
|
}
|
|
comparator = store.comparatorMap[attr]||dojo.data.util.sorter.basicComparator;
|
|
}
|
|
comparator = comparator||dojo.data.util.sorter.basicComparator;
|
|
return dir * comparator(a,b); //int
|
|
};
|
|
}
|
|
|
|
for(var i = 0; i < sortSpec.length; i++){
|
|
sortAttribute = sortSpec[i];
|
|
if(sortAttribute.attribute){
|
|
var direction = (sortAttribute.descending) ? -1 : 1;
|
|
sortFunctions.push(createSortFunction(sortAttribute.attribute, direction));
|
|
}
|
|
}
|
|
|
|
return function(rowA, rowB){
|
|
var i=0;
|
|
while(i < sortFunctions.length){
|
|
var ret = sortFunctions[i++](rowA, rowB);
|
|
if(ret !== 0){
|
|
return ret;//int
|
|
}
|
|
}
|
|
return 0; //int
|
|
}; // Function
|
|
};
|
|
|
|
}
|