source: Dev/branches/rest-dojo-ui/client/util/build/plugins/has.js @ 274

Last change on this file since 274 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.4 KB
Line 
1define(["dojo/regexp"], function(dojoRegExp) {
2        return {
3                start:function(
4                        id,
5                        referenceModule,
6                        bc
7                ) {
8                        var
9                                getHasPluginDependency= function(){
10                                        var hasPlugin= bc.amdResources["dojo/has"];
11                                        if(!hasPlugin){
12                                                bc.log("dojoHasMissingPlugin");
13                                                return [];
14                                        }else{
15                                                return [hasPlugin];
16                                        }
17                                },
18
19                                has = function(featureId) {
20                                        var value = bc.staticHasFeatures[featureId];
21                                        return (value===undefined || value==-1) ? undefined : value;
22                                },
23
24                                tokens = id.match(/[\?:]|[^:\?]*/g),
25
26                                i = 0,
27
28                                get = function(skip){
29                                        var operator, term = tokens[i++];
30                                        if(term == ":"){
31                                                // empty string module name; therefore, no dependency
32                                                return "";
33                                        }else{
34                                                // postfixed with a ? means it is a feature to branch on, the term is the name of the feature
35                                                if(tokens[i++] == "?"){
36                                                        var hasResult= has(term);
37                                                        if(hasResult===undefined){
38                                                                return undefined;
39                                                        }else if(!skip && hasResult){
40                                                                // matched the feature, get the first value from the options
41                                                                return get();
42                                                        }else{
43                                                                // did not match, get the second value, passing over the first
44                                                                get(true);
45                                                                return get(skip);
46                                                        }
47                                                }
48                                                // a module
49                                                return term===undefined ? "" : term;
50                                        }
51                                },
52
53                                resolvedId = get();
54
55                        // we only need the plugin if we need to resolve at run time
56                        if(resolvedId===undefined){
57                                bc.log("dojoHasUnresolvedMid", ["plugin resource id", id, "reference module id", referenceModule && referenceModule.mid]);
58                                return getHasPluginDependency();
59                        }
60
61                        var regex= new RegExp("((dojo\\/)|([./]+))has\\!" + dojoRegExp.escapeString(id));
62                        if(!resolvedId){
63                                // replace the unneeded module with a module that's guaranteed available
64                                // this keeps the module order, and therefore, argument order to the factory correct
65                                referenceModule.text= referenceModule.text.replace(regex, "require");
66                                return [];
67                        }else{
68                                var
69                                        moduleInfo= bc.getSrcModuleInfo(resolvedId, referenceModule),
70                                        module= bc.amdResources[moduleInfo.mid];
71                                if(module){
72                                        referenceModule.text= referenceModule.text.replace(regex, resolvedId);
73                                        return [module];
74                                }else{
75                                        bc.log("dojoHasMissingMid", ["plugin resource id", id, "resolved plugin resource id", moduleInfo.mid, "reference module id", referenceModule && referenceModule.mid]);
76                                        return getHasPluginDependency();
77                                }
78                        }
79                }
80        };
81});
Note: See TracBrowser for help on using the repository browser.