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,
@ -43,7 +43,7 @@ var require;
cfg = require; cfg = require;
} }
} }
/** /**
* Calls a method on a plugin. The obj object should have two property, * Calls a method on a plugin. The obj object should have two property,
* name: the name of the method to call on the plugin * name: the name of the method to call on the plugin
@ -65,7 +65,7 @@ var require;
req(["require/" + prefix], context.contextName); req(["require/" + prefix], context.contextName);
} }
} }
/** /**
* Convenience method to call main for a require.def call that was put on * Convenience method to call main for a require.def call that was put on
* hold in the defQueue. * hold in the defQueue.
@ -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,
@ -298,7 +328,7 @@ var require;
pluginPrefix = context.defPlugin[name]; pluginPrefix = context.defPlugin[name];
} }
//If module already defined for context, or already waiting to be //If module already defined for context, or already waiting to be
//evaluated, leave. //evaluated, leave.
waitingName = context.waiting[name]; waitingName = context.waiting[name];
@ -353,7 +383,7 @@ var require;
if (s.plugins.newContext) { if (s.plugins.newContext) {
s.plugins.newContext(newContext); s.plugins.newContext(newContext);
} }
context = s.contexts[contextName] = newContext; context = s.contexts[contextName] = newContext;
} }
@ -428,7 +458,7 @@ var require;
if (config.ready) { if (config.ready) {
req.ready(config.ready); req.ready(config.ready);
} }
//If it is just a config block, nothing else, //If it is just a config block, nothing else,
//then return. //then return.
if (!deps) { if (!deps) {
@ -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);
} }
} }
@ -499,7 +529,7 @@ var require;
args: [name, deps, callback, context] args: [name, deps, callback, context]
}); });
} }
//Hold on to the module until a script load or other adapter has finished //Hold on to the module until a script load or other adapter has finished
//evaluating the whole file. This helps when a file has more than one //evaluating the whole file. This helps when a file has more than one
//module in it -- dependencies are not traced and fetched until the whole //module in it -- dependencies are not traced and fetched until the whole
@ -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");
} }
}; };
@ -628,6 +660,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.
@ -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);
}; };
@ -786,7 +853,7 @@ var require;
} }
} }
}; };
req.isArray = function (it) { req.isArray = function (it) {
return ostring.call(it) === "[object Array]"; return ostring.call(it) === "[object Array]";
}; };
@ -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.
@ -920,7 +994,7 @@ var require;
* Splits a name into a possible plugin prefix and * Splits a name into a possible plugin prefix and
* the module name. If baseName is provided it will * the module name. If baseName is provided it will
* also normalize the name via require.normalizeName() * also normalize the name via require.normalizeName()
* *
* @param {String} name the module name * @param {String} name the module name
* @param {String} [baseName] base name that name is * @param {String} [baseName] base name that name is
* relative to. * relative to.
@ -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,11 +1091,10 @@ 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,
i, module, allDone, loads, loadArgs, err; i, module, allDone, loads, loadArgs, err;
//If already doing a checkLoaded call, //If already doing a checkLoaded call,
@ -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;
@ -1113,7 +1179,7 @@ var require;
if (pOrderDeps) { if (pOrderDeps) {
pOrderDeps(context); pOrderDeps(context);
} }
//Before defining the modules, give priority treatment to any modifiers //Before defining the modules, give priority treatment to any modifiers
//for modules that are already defined. //for modules that are already defined.
for (prop in modifiers) { for (prop in modifiers) {
@ -1123,7 +1189,7 @@ var require;
} }
} }
} }
//Define the modules, doing a depth first search. //Define the modules, doing a depth first search.
for (i = 0; (module = waiting[i]); i++) { for (i = 0; (module = waiting[i]); i++) {
req.exec(module, {}, waiting, context); req.exec(module, {}, waiting, context);
@ -1222,12 +1288,12 @@ var require;
/** /**
* Executes the modules in the correct order. * Executes the modules in the correct order.
* *
* @private * @private
*/ */
req.exec = function (module, traced, waiting, context) { req.exec = function (module, traced, waiting, context) {
//Some modules are just plain script files, abddo not have a formal //Some modules are just plain script files, abddo not have a formal
//module definition, //module definition,
if (!module) { if (!module) {
//Returning undefined for Spidermonky strict checking in Komodo //Returning undefined for Spidermonky strict checking in Komodo
return undefined; return undefined;
@ -1304,7 +1370,7 @@ var require;
//Execute modifiers, if they exist. //Execute modifiers, if they exist.
req.execModifiers(name, traced, waiting, context); req.execModifiers(name, traced, waiting, context);
return ret; return ret;
}; };
@ -1344,7 +1410,7 @@ var require;
delete modifiers[target]; delete modifiers[target];
} }
}; };
/** /**
* callback for script loads, used to check status of loading. * callback for script loads, used to check status of loading.
* *
@ -1465,11 +1531,11 @@ var require;
if (cfg.baseUrlMatch) { if (cfg.baseUrlMatch) {
rePkg = cfg.baseUrlMatch; rePkg = cfg.baseUrlMatch;
} else { } else {
rePkg = /(allplugins-|transportD-)?require\.js(\W|$)/i; rePkg = /(allplugins-)?require\.js(\W|$)/i;
} }
for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) { for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
@ -1534,12 +1600,27 @@ 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) {
s.readyCalls = []; if (callbacks.length) {
for (i = 0; (callback = callbacks[i]); i++) { s.readyCalls = [];
callback(); for (i = 0; (callback = callbacks[i]); i++) {
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;
}
}
} }
} }
}; };
@ -1600,7 +1681,7 @@ var require;
} }
} }
//****** END page load functionality **************** //****** END page load functionality ****************
//Set up default context. If require was a configuration object, use that as base config. //Set up default context. If require was a configuration object, use that as base config.
req(cfg); req(cfg);
@ -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 */
@ -1898,7 +1983,7 @@ var require;
if (loc === "_match") { if (loc === "_match") {
//Found default locale to use for the top-level bundle name. //Found default locale to use for the top-level bundle name.
defLoc = msWaiting[loc]; defLoc = msWaiting[loc];
} else if (msWaiting[loc] !== loc) { } else if (msWaiting[loc] !== loc) {
//A "best fit" locale, store it off to the end and handle //A "best fit" locale, store it off to the end and handle
//it at the end by just assigning the best fit value, since //it at the end by just assigning the best fit value, since
@ -1928,7 +2013,7 @@ var require;
//loop above so that the default locale bundle has been properly mixed //loop above so that the default locale bundle has been properly mixed
//together. //together.
context.defined[master] = context.defined[modulePrefix + "/" + defLoc + "/" + moduleSuffix]; context.defined[master] = context.defined[modulePrefix + "/" + defLoc + "/" + moduleSuffix];
//Handle any best fit locale definitions. //Handle any best fit locale definitions.
if (bestFit) { if (bestFit) {
for (loc in bestFit) { for (loc in bestFit) {
@ -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 */
@ -1993,7 +2078,7 @@ var require;
progIds = [progId]; // so faster next time progIds = [progId]; // so faster next time
break; break;
} }
} }
} }
if (!xhr) { if (!xhr) {
@ -2003,7 +2088,7 @@ var require;
return xhr; return xhr;
}; };
} }
if (!require.fetchText) { if (!require.fetchText) {
require.fetchText = function (url, callback) { require.fetchText = function (url, callback) {
var xhr = require.getXhr(); var xhr = require.getXhr();
@ -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 */
@ -2386,7 +2469,7 @@ var require;
/** /**
* Called when all modules have been loaded. Not needed for this plugin. * Called when all modules have been loaded. Not needed for this plugin.
* State is reset as part of scriptCacheCallback. * State is reset as part of scriptCacheCallback.
*/ */
orderDeps: function (context) { orderDeps: function (context) {
} }