2150 |
mathias |
1 |
if(!dojo._hasResource["dojo.AdapterRegistry"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojo.AdapterRegistry"] = true;
|
|
|
3 |
dojo.provide("dojo.AdapterRegistry");
|
|
|
4 |
|
|
|
5 |
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 |
dojo.extend(dojo.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: Remove a named adapter from the registry
|
|
|
84 |
|
|
|
85 |
// FIXME: this is kind of a dumb way to handle this. On a large
|
|
|
86 |
// registry this will be slow-ish and we can use the name as a lookup
|
|
|
87 |
// should we choose to trade memory for speed.
|
|
|
88 |
for(var i = 0; i < this.pairs.length; i++){
|
|
|
89 |
var pair = this.pairs[i];
|
|
|
90 |
if(pair[0] == name){
|
|
|
91 |
this.pairs.splice(i, 1);
|
|
|
92 |
return true;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
return false;
|
|
|
96 |
}
|
|
|
97 |
});
|
|
|
98 |
|
|
|
99 |
}
|