]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/AdapterRegistry.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dojo / AdapterRegistry.js
1 /*
2         Copyright (c) 2004-2011, 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
8 if(!dojo._hasResource["dojo.AdapterRegistry"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.AdapterRegistry"] = true;
10 dojo.provide("dojo.AdapterRegistry");
11
12
13 dojo.AdapterRegistry = function(/*Boolean?*/ returnWrappers){
14         //      summary:
15         //              A registry to make contextual calling/searching easier.
16         //      description:
17         //              Objects of this class keep list of arrays in the form [name, check,
18         //              wrap, directReturn] that are used to determine what the contextual
19         //              result of a set of checked arguments is. All check/wrap functions
20         //              in this registry should be of the same arity.
21         //      example:
22         //      |       // create a new registry
23         //      |       var reg = new dojo.AdapterRegistry();
24         //      |       reg.register("handleString",
25         //      |               dojo.isString,
26         //      |               function(str){
27         //      |                       // do something with the string here
28         //      |               }
29         //      |       );
30         //      |       reg.register("handleArr",
31         //      |               dojo.isArray,
32         //      |               function(arr){
33         //      |                       // do something with the array here
34         //      |               }
35         //      |       );
36         //      |
37         //      |       // now we can pass reg.match() *either* an array or a string and
38         //      |       // the value we pass will get handled by the right function
39         //      |       reg.match("someValue"); // will call the first function
40         //      |       reg.match(["someValue"]); // will call the second
41
42         this.pairs = [];
43         this.returnWrappers = returnWrappers || false; // Boolean
44 };
45
46 dojo.extend(dojo.AdapterRegistry, {
47         register: function(/*String*/ name, /*Function*/ check, /*Function*/ wrap, /*Boolean?*/ directReturn, /*Boolean?*/ override){
48                 //      summary:
49                 //              register a check function to determine if the wrap function or
50                 //              object gets selected
51                 //      name:
52                 //              a way to identify this matcher.
53                 //      check:
54                 //              a function that arguments are passed to from the adapter's
55                 //              match() function.  The check function should return true if the
56                 //              given arguments are appropriate for the wrap function.
57                 //      directReturn:
58                 //              If directReturn is true, the value passed in for wrap will be
59                 //              returned instead of being called. Alternately, the
60                 //              AdapterRegistry can be set globally to "return not call" using
61                 //              the returnWrappers property. Either way, this behavior allows
62                 //              the registry to act as a "search" function instead of a
63                 //              function interception library.
64                 //      override:
65                 //              If override is given and true, the check function will be given
66                 //              highest priority. Otherwise, it will be the lowest priority
67                 //              adapter.
68                 this.pairs[((override) ? "unshift" : "push")]([name, check, wrap, directReturn]);
69         },
70
71         match: function(/* ... */){
72                 // summary:
73                 //              Find an adapter for the given arguments. If no suitable adapter
74                 //              is found, throws an exception. match() accepts any number of
75                 //              arguments, all of which are passed to all matching functions
76                 //              from the registered pairs.
77                 for(var i = 0; i < this.pairs.length; i++){
78                         var pair = this.pairs[i];
79                         if(pair[1].apply(this, arguments)){
80                                 if((pair[3])||(this.returnWrappers)){
81                                         return pair[2];
82                                 }else{
83                                         return pair[2].apply(this, arguments);
84                                 }
85                         }
86                 }
87                 throw new Error("No match found");
88         },
89
90         unregister: function(name){
91                 // summary: Remove a named adapter from the registry
92
93                 // FIXME: this is kind of a dumb way to handle this. On a large
94                 // registry this will be slow-ish and we can use the name as a lookup
95                 // should we choose to trade memory for speed.
96                 for(var i = 0; i < this.pairs.length; i++){
97                         var pair = this.pairs[i];
98                         if(pair[0] == name){
99                                 this.pairs.splice(i, 1);
100                                 return true;
101                         }
102                 }
103                 return false;
104         }
105 });
106
107 }