diff options
author | mensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2008-11-13 09:49:11 +0000 |
---|---|---|
committer | mensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2008-11-13 09:49:11 +0000 |
commit | e44a7e37b6c7b5961adaffc62b9042b8d442938e (patch) | |
tree | 95b67c356e93163467db2451f2b8cce84ed5d582 /includes/js/dojo/data/util/sorter.js | |
parent | a62b9742ee5e28bcec6872d88f50f25b820914f6 (diff) | |
download | semanticscuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.tar.gz semanticscuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.tar.bz2 |
New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'includes/js/dojo/data/util/sorter.js')
-rw-r--r-- | includes/js/dojo/data/util/sorter.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/includes/js/dojo/data/util/sorter.js b/includes/js/dojo/data/util/sorter.js new file mode 100644 index 0000000..e13fedc --- /dev/null +++ b/includes/js/dojo/data/util/sorter.js @@ -0,0 +1,81 @@ +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 +}; + +} |