]> git.pond.sub.org Git - eow/blobdiff - static/dojo-release-1.1.1/dojo/data/util/sorter.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / data / util / sorter.js
diff --git a/static/dojo-release-1.1.1/dojo/data/util/sorter.js b/static/dojo-release-1.1.1/dojo/data/util/sorter.js
new file mode 100644 (file)
index 0000000..e13fedc
--- /dev/null
@@ -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
+};
+
+}