]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/data/util/sorter.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / data / util / sorter.js
CommitLineData
2f01fe57
AD
1/*
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5*/
6
7
a089699c
AD
8if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dojo.data.util.sorter"] = true;
2f01fe57 10dojo.provide("dojo.data.util.sorter");
a089699c
AD
11
12dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
13 /*anything*/ b){
14 // summary:
15 // Basic comparision function that compares if an item is greater or less than another item
16 // description:
17 // returns 1 if a > b, -1 if a < b, 0 if equal.
18 // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
19 // And compared to each other, null is equivalent to undefined.
20
21 //null is a problematic compare, so if null, we set to undefined.
22 //Makes the check logic simple, compact, and consistent
23 //And (null == undefined) === true, so the check later against null
24 //works for undefined and is less bytes.
25 var r = -1;
26 if(a === null){
27 a = undefined;
28 }
29 if(b === null){
30 b = undefined;
31 }
32 if(a == b){
33 r = 0;
34 }else if(a > b || a == null){
35 r = 1;
36 }
37 return r; //int {-1,0,1}
2f01fe57 38};
a089699c
AD
39
40dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
41 /*dojo.data.core.Read*/ store){
42 // summary:
43 // Helper function to generate the sorting function based off the list of sort attributes.
44 // description:
45 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
46 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
47 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
48 // Returns the sorting function for this particular list of attributes and sorting directions.
49 //
50 // sortSpec: array
51 // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
52 // The objects should be formatted as follows:
53 // {
54 // attribute: "attributeName-string" || attribute,
55 // descending: true|false; // Default is false.
56 // }
57 // store: object
58 // The datastore object to look up item values from.
59 //
60 var sortFunctions=[];
61
62 function createSortFunction(attr, dir, comp, s){
63 //Passing in comp and s (comparator and store), makes this
64 //function much faster.
65 return function(itemA, itemB){
66 var a = s.getValue(itemA, attr);
67 var b = s.getValue(itemB, attr);
68 return dir * comp(a,b); //int
69 };
70 }
71 var sortAttribute;
72 var map = store.comparatorMap;
73 var bc = dojo.data.util.sorter.basicComparator;
74 for(var i = 0; i < sortSpec.length; i++){
75 sortAttribute = sortSpec[i];
76 var attr = sortAttribute.attribute;
77 if(attr){
78 var dir = (sortAttribute.descending) ? -1 : 1;
79 var comp = bc;
80 if(map){
81 if(typeof attr !== "string" && ("toString" in attr)){
82 attr = attr.toString();
83 }
84 comp = map[attr] || bc;
85 }
86 sortFunctions.push(createSortFunction(attr,
87 dir, comp, store));
88 }
89 }
90 return function(rowA, rowB){
91 var i=0;
92 while(i < sortFunctions.length){
93 var ret = sortFunctions[i++](rowA, rowB);
94 if(ret !== 0){
95 return ret;//int
96 }
97 }
98 return 0; //int
99 }; // Function
2f01fe57 100};
a089699c 101
2f01fe57 102}