]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/store/util/SimpleQueryEngine.js.uncompressed.js
make precache_headlines_idle() start slower
[tt-rss.git] / lib / dojo / store / util / SimpleQueryEngine.js.uncompressed.js
CommitLineData
1354d172
AD
1define("dojo/store/util/SimpleQueryEngine", ["../../_base/array"], function(arrayUtil) {
2 // module:
3 // dojo/store/util/SimpleQueryEngine
4 // summary:
5 // The module defines a simple filtering query engine for object stores.
6
7return function(query, options){
8 // summary:
9 // Simple query engine that matches using filter functions, named filter
10 // functions or objects by name-value on a query object hash
11 //
12 // description:
13 // The SimpleQueryEngine provides a way of getting a QueryResults through
14 // the use of a simple object hash as a filter. The hash will be used to
15 // match properties on data objects with the corresponding value given. In
16 // other words, only exact matches will be returned.
17 //
18 // This function can be used as a template for more complex query engines;
19 // for example, an engine can be created that accepts an object hash that
20 // contains filtering functions, or a string that gets evaluated, etc.
21 //
22 // When creating a new dojo.store, simply set the store's queryEngine
23 // field as a reference to this function.
24 //
25 // query: Object
26 // An object hash with fields that may match fields of items in the store.
27 // Values in the hash will be compared by normal == operator, but regular expressions
28 // or any object that provides a test() method are also supported and can be
29 // used to match strings by more complex expressions
30 // (and then the regex's or object's test() method will be used to match values).
31 //
32 // options: dojo.store.util.SimpleQueryEngine.__queryOptions?
33 // An object that contains optional information such as sort, start, and count.
34 //
35 // returns: Function
36 // A function that caches the passed query under the field "matches". See any
37 // of the "query" methods on dojo.stores.
38 //
39 // example:
40 // Define a store with a reference to this engine, and set up a query method.
41 //
42 // | var myStore = function(options){
43 // | // ...more properties here
44 // | this.queryEngine = dojo.store.util.SimpleQueryEngine;
45 // | // define our query method
46 // | this.query = function(query, options){
47 // | return dojo.store.util.QueryResults(this.queryEngine(query, options)(this.data));
48 // | };
49 // | };
50
51 // create our matching query function
52 switch(typeof query){
53 default:
54 throw new Error("Can not query with a " + typeof query);
55 case "object": case "undefined":
56 var queryObject = query;
57 query = function(object){
58 for(var key in queryObject){
59 var required = queryObject[key];
60 if(required && required.test){
61 if(!required.test(object[key])){
62 return false;
63 }
64 }else if(required != object[key]){
65 return false;
66 }
67 }
68 return true;
69 };
70 break;
71 case "string":
72 // named query
73 if(!this[query]){
74 throw new Error("No filter function " + query + " was found in store");
75 }
76 query = this[query];
77 // fall through
78 case "function":
79 // fall through
80 }
81 function execute(array){
82 // execute the whole query, first we filter
83 var results = arrayUtil.filter(array, query);
84 // next we sort
85 if(options && options.sort){
86 results.sort(function(a, b){
87 for(var sort, i=0; sort = options.sort[i]; i++){
88 var aValue = a[sort.attribute];
89 var bValue = b[sort.attribute];
90 if (aValue != bValue) {
91 return !!sort.descending == aValue > bValue ? -1 : 1;
92 }
93 }
94 return 0;
95 });
96 }
97 // now we paginate
98 if(options && (options.start || options.count)){
99 var total = results.length;
100 results = results.slice(options.start || 0, (options.start || 0) + (options.count || Infinity));
101 results.total = total;
102 }
103 return results;
104 }
105 execute.matches = query;
106 return execute;
107};
108});