[483] | 1 | define([ |
---|
| 2 | 'require', |
---|
| 3 | '../_base/array', |
---|
| 4 | './default!platform', |
---|
| 5 | './util' |
---|
| 6 | ], function(require, array, fallbackProvider, util){ |
---|
| 7 | var providers = []; |
---|
| 8 | |
---|
| 9 | function request(url, options){ |
---|
| 10 | var matchers = providers.slice(0), |
---|
| 11 | i = 0, |
---|
| 12 | matcher; |
---|
| 13 | |
---|
| 14 | while(matcher=matchers[i++]){ |
---|
| 15 | if(matcher(url, options)){ |
---|
| 16 | return matcher.request.call(null, url, options); |
---|
| 17 | } |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | return fallbackProvider.apply(null, arguments); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | function createMatcher(match, provider){ |
---|
| 24 | var matcher; |
---|
| 25 | |
---|
| 26 | if(provider){ |
---|
| 27 | if(match.test){ |
---|
| 28 | // RegExp |
---|
| 29 | matcher = function(url){ |
---|
| 30 | return match.test(url); |
---|
| 31 | }; |
---|
| 32 | }else if(match.apply && match.call){ |
---|
| 33 | matcher = function(){ |
---|
| 34 | return match.apply(null, arguments); |
---|
| 35 | }; |
---|
| 36 | }else{ |
---|
| 37 | matcher = function(url){ |
---|
| 38 | return url === match; |
---|
| 39 | }; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | matcher.request = provider; |
---|
| 43 | }else{ |
---|
| 44 | // If only one argument was passed, assume it is a provider function |
---|
| 45 | // to apply unconditionally to all URLs |
---|
| 46 | matcher = function(){ |
---|
| 47 | return true; |
---|
| 48 | }; |
---|
| 49 | |
---|
| 50 | matcher.request = match; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | return matcher; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | request.register = function(url, provider, first){ |
---|
| 57 | var matcher = createMatcher(url, provider); |
---|
| 58 | providers[(first ? 'unshift' : 'push')](matcher); |
---|
| 59 | |
---|
| 60 | return { |
---|
| 61 | remove: function(){ |
---|
| 62 | var idx; |
---|
| 63 | if(~(idx = array.indexOf(providers, matcher))){ |
---|
| 64 | providers.splice(idx, 1); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | }; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | request.load = function(id, parentRequire, loaded, config){ |
---|
| 71 | if(id){ |
---|
| 72 | // if there's an id, load and set the fallback provider |
---|
| 73 | require([id], function(fallback){ |
---|
| 74 | fallbackProvider = fallback; |
---|
| 75 | loaded(request); |
---|
| 76 | }); |
---|
| 77 | }else{ |
---|
| 78 | loaded(request); |
---|
| 79 | } |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | util.addCommonMethods(request); |
---|
| 83 | |
---|
| 84 | return request; |
---|
| 85 | }); |
---|