Debian Codegraph @

debian/js/requirejs/order.js

raw symbols SCIP
1 /**
2 * @license RequireJS order 1.0.5 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
3 * Available via the MIT or new BSD license.
4 * see: http://github.com/jrburke/requirejs for details
5 */
6 /*jslint nomen: false, plusplus: false, strict: false */
7 /*global require: false, define: false, window: false, document: false,
8 setTimeout: false */
9
10 //Specify that requirejs optimizer should wrap this code in a closure that
11 //maps the namespaced requirejs API to non-namespaced local variables.
12 /*requirejs namespace: true */
13
14 (function () {
15
16 //Sadly necessary browser inference due to differences in the way
17 //that browsers load and execute dynamically inserted javascript
18 //and whether the script/cache method works when ordered execution is
19 //desired. Currently, Gecko and Opera do not load/fire onload for scripts with
20 //type="script/cache" but they execute injected scripts in order
21 //unless the 'async' flag is present.
22 //However, this is all changing in latest browsers implementing HTML5
23 //spec. With compliant browsers .async true by default, and
24 //if false, then it will execute in order. Favor that test first for forward
25 //compatibility.
26 var testScript = typeof document !== "undefined" &&
27 typeof window !== "undefined" &&
28 document.createElement("script"),
29
30 supportsInOrderExecution = testScript && (testScript.async ||
31 ((window.opera &&
32 Object.prototype.toString.call(window.opera) === "[object Opera]") ||
33 //If Firefox 2 does not have to be supported, then
34 //a better check may be:
35 //('mozIsLocallyAvailable' in window.navigator)
36 ("MozAppearance" in document.documentElement.style))),
37
38 //This test is true for IE browsers, which will load scripts but only
39 //execute them once the script is added to the DOM.
40 supportsLoadSeparateFromExecute = testScript &&
41 testScript.readyState === 'uninitialized',
42
43 readyRegExp = /^(complete|loaded)$/,
44 cacheWaiting = [],
45 cached = {},
46 scriptNodes = {},
47 scriptWaiting = [];
48
49 //Done with the test script.
50 testScript = null;
51
52 //Callback used by the type="script/cache" callback that indicates a script
53 //has finished downloading.
54 function scriptCacheCallback(evt) {
55 var node = evt.currentTarget || evt.srcElement, i,
56 moduleName, resource;
57
58 if (evt.type === "load" || readyRegExp.test(node.readyState)) {
59 //Pull out the name of the module and the context.
60 moduleName = node.getAttribute("data-requiremodule");
61
62 //Mark this cache request as loaded
63 cached[moduleName] = true;
64
65 //Find out how many ordered modules have loaded
66 for (i = 0; (resource = cacheWaiting[i]); i++) {
67 if (cached[resource.name]) {
68 resource.req([resource.name], resource.onLoad);
69 } else {
70 //Something in the ordered list is not loaded,
71 //so wait.
72 break;
73 }
74 }
75
76 //If just loaded some items, remove them from cacheWaiting.
77 if (i > 0) {
78 cacheWaiting.splice(0, i);
79 }
80
81 //Remove this script tag from the DOM
82 //Use a setTimeout for cleanup because some older IE versions vomit
83 //if removing a script node while it is being evaluated.
84 setTimeout(function () {
85 node.parentNode.removeChild(node);
86 }, 15);
87 }
88 }
89
90 /**
91 * Used for the IE case, where fetching is done by creating script element
92 * but not attaching it to the DOM. This function will be called when that
93 * happens so it can be determined when the node can be attached to the
94 * DOM to trigger its execution.
95 */
96 function onFetchOnly(node) {
97 var i, loadedNode, resourceName;
98
99 //Mark this script as loaded.
100 node.setAttribute('data-orderloaded', 'loaded');
101
102 //Cycle through waiting scripts. If the matching node for them
103 //is loaded, and is in the right order, add it to the DOM
104 //to execute the script.
105 for (i = 0; (resourceName = scriptWaiting[i]); i++) {
106 loadedNode = scriptNodes[resourceName];
107 if (loadedNode &&
108 loadedNode.getAttribute('data-orderloaded') === 'loaded') {
109 delete scriptNodes[resourceName];
110 require.addScriptToDom(loadedNode);
111 } else {
112 break;
113 }
114 }
115
116 //If just loaded some items, remove them from waiting.
117 if (i > 0) {
118 scriptWaiting.splice(0, i);
119 }
120 }
121
122 define({
123 version: '1.0.5',
124
125 load: function (name, req, onLoad, config) {
126 var hasToUrl = !!req.nameToUrl,
127 url, node, context;
128
129 //If no nameToUrl, then probably a build with a loader that
130 //does not support it, and all modules are inlined.
131 if (!hasToUrl) {
132 req([name], onLoad);
133 return;
134 }
135
136 url = req.nameToUrl(name, null);
137
138 //Make sure the async attribute is not set for any pathway involving
139 //this script.
140 require.s.skipAsync[url] = true;
141 if (supportsInOrderExecution || config.isBuild) {
142 //Just a normal script tag append, but without async attribute
143 //on the script.
144 req([name], onLoad);
145 } else if (supportsLoadSeparateFromExecute) {
146 //Just fetch the URL, but do not execute it yet. The
147 //non-standards IE case. Really not so nice because it is
148 //assuming and touching requrejs internals. OK though since
149 //ordered execution should go away after a long while.
150 context = require.s.contexts._;
151
152 if (!context.urlFetched[url] && !context.loaded[name]) {
153 //Indicate the script is being fetched.
154 context.urlFetched[url] = true;
155
156 //Stuff from require.load
157 require.resourcesReady(false);
158 context.scriptCount += 1;
159
160 //Fetch the script now, remember it.
161 node = require.attach(url, context, name, null, null, onFetchOnly);
162 scriptNodes[name] = node;
163 scriptWaiting.push(name);
164 }
165
166 //Do a normal require for it, once it loads, use it as return
167 //value.
168 req([name], onLoad);
169 } else {
170 //Credit to LABjs author Kyle Simpson for finding that scripts
171 //with type="script/cache" allow scripts to be downloaded into
172 //browser cache but not executed. Use that
173 //so that subsequent addition of a real type="text/javascript"
174 //tag will cause the scripts to be executed immediately in the
175 //correct order.
176 if (req.specified(name)) {
177 req([name], onLoad);
178 } else {
179 cacheWaiting.push({
180 name: name,
181 req: req,
182 onLoad: onLoad
183 });
184 require.attach(url, null, name, scriptCacheCallback, "script/cache");
185 }
186 }
187 }
188 });
189 }());