update requireJS

This commit is contained in:
Fabian Jakobs 2010-11-09 09:24:06 +01:00
commit dce8d5dbf3

View file

@ -1,19 +1,19 @@
/** vim: et:ts=4:sw=4:sts=4 /** vim: et:ts=4:sw=4:sts=4
* @license RequireJS Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. * @license RequireJS Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
//laxbreak is true to allow build pragmas to change some statements. //laxbreak is true to allow build pragmas to change some statements.
/*jslint plusplus: false, nomen: false, laxbreak: true, regexp: false */ /*jslint plusplus: false, nomen: false, laxbreak: true, regexp: false */
/*global window: false, document: false, navigator: false, /*global window: false, document: false, navigator: false,
setTimeout: false, traceDeps: true, clearInterval: false, self: false, setTimeout: false, traceDeps: true, clearInterval: false, self: false,
setInterval: false, importScripts: false */ setInterval: false, importScripts: false, jQuery: false */
var require; var require, define;
(function () { (function () {
//Change this version number for each release. //Change this version number for each release.
var version = "0.14.2", var version = "0.14.5",
empty = {}, s, empty = {}, s,
i, defContextName = "_", contextLoads = [], i, defContextName = "_", contextLoads = [],
scripts, script, rePkg, src, m, dataMain, cfg = {}, setReadyState, scripts, script, rePkg, src, m, dataMain, cfg = {}, setReadyState,
@ -105,6 +105,27 @@ var require;
} }
} }
/**
* Determine if priority loading is done. If so clear the priorityWait
*/
function isPriorityDone(context) {
var priorityDone = true,
priorityWait = context.config.priorityWait,
priorityName, i;
if (priorityWait) {
for (i = 0; (priorityName = priorityWait[i]); i++) {
if (!context.loaded[priorityName]) {
priorityDone = false;
break;
}
}
if (priorityDone) {
delete context.config.priorityWait;
}
}
return priorityDone;
}
/** /**
* Resumes tracing of dependencies and then checks if everything is loaded. * Resumes tracing of dependencies and then checks if everything is loaded.
*/ */
@ -128,7 +149,7 @@ var require;
} }
//Skip the resume if current context is in priority wait. //Skip the resume if current context is in priority wait.
if (s.contexts[s.ctxName].config.priorityWait) { if (context.config.priorityWait && !isPriorityDone(context)) {
return; return;
} }
@ -167,12 +188,14 @@ var require;
// Adjust args if there are dependencies // Adjust args if there are dependencies
deps = callback; deps = callback;
callback = contextName; callback = contextName;
contextName = arguments[3]; contextName = relModuleName;
relModuleName = arguments[4];
} else { } else {
deps = []; deps = [];
} }
} }
main(null, deps, callback, config, contextName);
main(null, deps, callback, config, contextName, relModuleName);
//If the require call does not trigger anything new to load, //If the require call does not trigger anything new to load,
//then resume the dependency processing. Context will be undefined //then resume the dependency processing. Context will be undefined
@ -210,7 +233,7 @@ var require;
* return a value to define the module corresponding to the first argument's * return a value to define the module corresponding to the first argument's
* name. * name.
*/ */
req.def = function (name, deps, callback, contextName) { define = req.def = function (name, deps, callback, contextName) {
var i, scripts, script, node = currentlyAddingScript; var i, scripts, script, node = currentlyAddingScript;
//Allow for anonymous functions //Allow for anonymous functions
@ -267,6 +290,13 @@ var require;
name = node.getAttribute("data-requiremodule"); name = node.getAttribute("data-requiremodule");
} }
if (typeof name === 'string') {
//Do not try to auto-register a jquery later.
//Do this work here and in main, since for IE/useInteractive, this function
//is the earliest touch-point.
s.contexts[s.ctxName].jQueryDef = (name === "jquery");
}
//Always save off evaluating the def call until the script onload handler. //Always save off evaluating the def call until the script onload handler.
//This allows multiple modules to be in a file without prematurely //This allows multiple modules to be in a file without prematurely
//tracing dependencies, and allows for anonymous module support, //tracing dependencies, and allows for anonymous module support,
@ -275,7 +305,7 @@ var require;
defQueue.push([name, deps, callback, null, contextName]); defQueue.push([name, deps, callback, null, contextName]);
}; };
main = function (name, deps, callback, config, contextName) { main = function (name, deps, callback, config, contextName, relModuleName) {
//Grab the context, or create a new one for the given context name. //Grab the context, or create a new one for the given context name.
var context, newContext, loaded, pluginPrefix, var context, newContext, loaded, pluginPrefix,
canSetContext, prop, newLength, outDeps, mods, paths, index, i, canSetContext, prop, newLength, outDeps, mods, paths, index, i,
@ -443,7 +473,7 @@ var require;
outDeps = deps; outDeps = deps;
deps = []; deps = [];
for (i = 0; i < outDeps.length; i++) { for (i = 0; i < outDeps.length; i++) {
deps[i] = req.splitPrefix(outDeps[i], name, context); deps[i] = req.splitPrefix(outDeps[i], (name || relModuleName), context);
} }
} }
@ -510,8 +540,10 @@ var require;
//as part of a layer, where onScriptLoad is not fired //as part of a layer, where onScriptLoad is not fired
//for those cases. Do this after the inline define and //for those cases. Do this after the inline define and
//dependency tracing is done. //dependency tracing is done.
//Also check if auto-registry of jQuery needs to be skipped.
if (name) { if (name) {
context.loaded[name] = true; context.loaded[name] = true;
context.jQueryDef = (name === "jquery");
} }
}; };
@ -629,6 +661,36 @@ var require;
return req; return req;
}; };
/**
* As of jQuery 1.4.3, it supports a readyWait property that will hold off
* calling jQuery ready callbacks until all scripts are loaded. Be sure
* to track it if readyWait is available. Also, since jQuery 1.4.3 does
* not register as a module, need to do some global inference checking.
* Even if it does register as a module, not guaranteed to be the precise
* name of the global. If a jQuery is tracked for this context, then go
* ahead and register it as a module too, if not already in process.
*/
function jQueryCheck(context, jqCandidate) {
if (!context.jQuery) {
var $ = jqCandidate || (typeof jQuery !== "undefined" ? jQuery : null);
if ($ && "readyWait" in $) {
context.jQuery = $;
//Manually create a "jquery" module entry if not one already
//or in process.
if (!context.defined.jquery && !context.jQueryDef) {
context.defined.jquery = $;
}
//Make sure
if (context.scriptCount) {
$.readyWait += 1;
context.jQueryIncremented = true;
}
}
}
}
/** /**
* Internal method used by environment adapters to complete a load event. * Internal method used by environment adapters to complete a load event.
* A load event could be a script load or just a load pass from a synchronous * A load event could be a script load or just a load pass from a synchronous
@ -662,6 +724,11 @@ var require;
//for traditional browser scripts. //for traditional browser scripts.
context.loaded[moduleName] = true; context.loaded[moduleName] = true;
//If a global jQuery is defined, check for it. Need to do it here
//instead of main() since stock jQuery does not register as
//a module via define.
jQueryCheck(context);
context.scriptCount -= 1; context.scriptCount -= 1;
resume(context); resume(context);
}; };
@ -861,13 +928,20 @@ var require;
context.scriptCount += 1; context.scriptCount += 1;
req.attach(url, contextName, moduleName); req.attach(url, contextName, moduleName);
urlFetched[url] = true; urlFetched[url] = true;
//If tracking a jQuery, then make sure its readyWait
//is incremented to prevent its ready callbacks from
//triggering too soon.
if (context.jQuery && !context.jQueryIncremented) {
context.jQuery.readyWait += 1;
context.jQueryIncremented = true;
}
} }
} }
}; };
req.jsExtRegExp = /\.js$/; req.jsExtRegExp = /\.js$/;
/** /**
* Given a relative module name, like ./something, normalize it to * Given a relative module name, like ./something, normalize it to
* a real name that can be mapped to a path. * a real name that can be mapped to a path.
@ -965,9 +1039,7 @@ var require;
//Just a plain path, not module name lookup, so just return it. //Just a plain path, not module name lookup, so just return it.
//Add extension if it is included. This is a bit wonky, only non-.js things pass //Add extension if it is included. This is a bit wonky, only non-.js things pass
//an extension, this method probably needs to be reworked. //an extension, this method probably needs to be reworked.
return moduleName + (ext ? ext : ""); url = moduleName + (ext ? ext : "");
} else if (moduleName.charAt(0) === ".") {
return req.onError(new Error("require.nameToUrl does not handle relative module names (ones that start with '.' or '..')"));
} else { } else {
//A module that needs to be converted to a path. //A module that needs to be converted to a path.
paths = config.paths; paths = config.paths;
@ -999,8 +1071,11 @@ var require;
//Join the path parts together, then figure out if baseUrl is needed. //Join the path parts together, then figure out if baseUrl is needed.
url = syms.join("/") + (ext || ".js"); url = syms.join("/") + (ext || ".js");
return ((url.charAt(0) === '/' || url.match(/^\w+:/)) ? "" : config.baseUrl) + url; url = (url.charAt(0) === '/' || url.match(/^\w+:/) ? "" : config.baseUrl) + url;
} }
return config.urlArgs ? url +
((url.indexOf('?') === -1 ? '?' : '&') +
config.urlArgs) : url;
}; };
/** /**
@ -1016,8 +1091,7 @@ var require;
expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(), expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
loaded, defined = context.defined, loaded, defined = context.defined,
modifiers = context.modifiers, waiting, noLoads = "", modifiers = context.modifiers, waiting, noLoads = "",
hasLoadedProp = false, stillLoading = false, prop, priorityDone, hasLoadedProp = false, stillLoading = false, prop,
priorityName,
pIsWaiting = s.plugins.isWaiting, pOrderDeps = s.plugins.orderDeps, pIsWaiting = s.plugins.isWaiting, pOrderDeps = s.plugins.orderDeps,
@ -1032,17 +1106,9 @@ var require;
//Determine if priority loading is done. If so clear the priority. If //Determine if priority loading is done. If so clear the priority. If
//not, then do not check //not, then do not check
if (context.config.priorityWait) { if (context.config.priorityWait) {
priorityDone = true; if (isPriorityDone(context)) {
for (i = 0; (priorityName = context.config.priorityWait[i]); i++) { //Call resume, since it could have
if (!context.loaded[priorityName]) {
priorityDone = false;
break;
}
}
if (priorityDone) {
//Clean up priority and call resume, since it could have
//some waiting dependencies to trace. //some waiting dependencies to trace.
delete context.config.priorityWait;
resume(context); resume(context);
} else { } else {
return; return;
@ -1468,7 +1534,7 @@ var require;
rePkg = /(allplugins-|transportD-)?require\.js(\W|$)/i; rePkg = /(allplugins-)?require\.js(\W|$)/i;
} }
@ -1534,14 +1600,29 @@ var require;
* you can define this method to call your page ready code instead. * you can define this method to call your page ready code instead.
*/ */
req.callReady = function () { req.callReady = function () {
var callbacks = s.readyCalls, i, callback; var callbacks = s.readyCalls, i, callback, contexts, context, prop;
if (s.isPageLoaded && s.isDone && callbacks.length) { if (s.isPageLoaded && s.isDone) {
if (callbacks.length) {
s.readyCalls = []; s.readyCalls = [];
for (i = 0; (callback = callbacks[i]); i++) { for (i = 0; (callback = callbacks[i]); i++) {
callback(); callback();
} }
} }
//If jQuery with readyWait is being tracked, updated its
//readyWait count.
contexts = s.contexts;
for (prop in contexts) {
if (!(prop in empty)) {
context = contexts[prop];
if (context.jQueryIncremented) {
context.jQuery.readyWait -= 1;
context.jQueryIncremented = false;
}
}
}
}
}; };
/** /**
@ -1610,14 +1691,18 @@ var require;
//which seems odd to do on the server. //which seems odd to do on the server.
if (typeof setTimeout !== "undefined") { if (typeof setTimeout !== "undefined") {
setTimeout(function () { setTimeout(function () {
resume(s.contexts[(cfg.context || defContextName)]); var ctx = s.contexts[(cfg.context || defContextName)];
//Allow for jQuery to be loaded/already in the page, and if jQuery 1.4.3,
//make sure to hold onto it for readyWait triggering.
jQueryCheck(ctx);
resume(ctx);
}, 0); }, 0);
} }
}()); }());
/** /**
* @license RequireJS i18n Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. * @license RequireJS i18n Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
/*jslint regexp: false, nomen: false, plusplus: false */ /*jslint regexp: false, nomen: false, plusplus: false */
@ -1942,8 +2027,8 @@ var require;
}); });
}()); }());
/** /**
* @license RequireJS text Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. * @license RequireJS text Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
/*jslint regexp: false, nomen: false, plusplus: false */ /*jslint regexp: false, nomen: false, plusplus: false */
@ -2102,7 +2187,6 @@ var require;
require.fetchText(url, function (text) { require.fetchText(url, function (text) {
context.text[key] = text; context.text[key] = text;
context.loaded[name] = true; context.loaded[name] = true;
require.checkLoaded(contextName);
}); });
} }
}, },
@ -2138,8 +2222,8 @@ var require;
}); });
}()); }());
/** /**
* @license RequireJS jsonp Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. * @license RequireJS jsonp Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
/*jslint nomen: false, plusplus: false */ /*jslint nomen: false, plusplus: false */
@ -2195,7 +2279,6 @@ var require;
require._jsonp[funcName] = function (value) { require._jsonp[funcName] = function (value) {
data.value = value; data.value = value;
context.loaded[name] = true; context.loaded[name] = true;
require.checkLoaded(contextName);
//Use a setTimeout for cleanup because some older IE versions vomit //Use a setTimeout for cleanup because some older IE versions vomit
//if removing a script node while it is being evaluated. //if removing a script node while it is being evaluated.
setTimeout(function () { setTimeout(function () {
@ -2261,8 +2344,8 @@ var require;
}); });
}()); }());
/** /**
* @license RequireJS order Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. * @license RequireJS order Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
/*jslint nomen: false, plusplus: false */ /*jslint nomen: false, plusplus: false */