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