Skip to content

Instantly share code, notes, and snippets.

@jayzeng
Created April 13, 2011 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayzeng/918224 to your computer and use it in GitHub Desktop.
Save jayzeng/918224 to your computer and use it in GitHub Desktop.
var require, define;
(function () {
function getInteractiveScript() {
var a, b, c;
if (interactiveScript && interactiveScript.readyState === "interactive") return interactiveScript;
a = document.getElementsByTagName("script");
for (b = a.length - 1; b > -1 && (c = a[b]); b--) if (c.readyState === "interactive") return interactiveScript = c;
return null
}
function newContext(contextName) {
function loadPaused(a) {
a.prefix && a.name.indexOf("__$p") === 0 && defined[a.prefix] && (a = makeModuleMap(a.originalName, a.parentMap));
var b = a.prefix,
c = a.fullName;
!specified[c] && !loaded[c] && (specified[c] = !0, b ? defined[b] ? callPlugin(b, a) : (pluginsQueue[b] || (pluginsQueue[b] = [], (managerCallbacks[b] || (managerCallbacks[b] = [])).push({
onDep: function (a, c) {
if (a === b) {
var d, e, f = pluginsQueue[b];
for (d = 0; d < f.length; d++) e = f[d], callPlugin(b, makeModuleMap(e.originalName, e.parentMap));
delete pluginsQueue[b]
}
}
})), pluginsQueue[b].push(a)) : req.load(context, c, a.url))
}
function callPlugin(pluginName, dep) {
var name = dep.name,
fullName = dep.fullName,
load;
fullName in defined || fullName in loaded || (plugins[pluginName] || (plugins[pluginName] = defined[pluginName]), loaded[fullName] || (loaded[fullName] = !1), load = function (a) {
require.onPluginLoad && require.onPluginLoad(context, pluginName, name, a), execManager({
prefix: dep.prefix,
name: dep.name,
fullName: dep.fullName,
callback: function () {
return a
}
}), loaded[fullName] = !0
}, load.fromText = function (moduleName, text) {
var hasInteractive = useInteractive;
context.loaded[moduleName] = !1, context.scriptCount += 1, hasInteractive && (useInteractive = !1), eval(text), hasInteractive && (useInteractive = !0), context.completeLoad(moduleName)
}, plugins[pluginName].load(name, makeRequire(dep.parentMap, !0), load, config))
}
function checkLoaded() {
var a = config.waitSeconds * 1e3,
b = a && context.startTime + a < (new Date).getTime(),
c = "",
d = !1,
e = !1,
f, g, h;
if (context.pausedCount > 0) return undefined;
if (config.priorityWait) if (isPriorityDone()) resume();
else return undefined;
for (f in loaded) if (!(f in empty)) {
d = !0;
if (!loaded[f]) if (b) c += f + " ";
else {
e = !0;
break
}
}
if (!d && !context.waitCount) return undefined;
if (b && c) {
g = new Error("require.js load timeout for modules: " + c), g.requireType = "timeout", g.requireModules = c;
return req.onError(g)
}
if (e || context.scriptCount) {
(isBrowser || isWebWorker) && setTimeout(checkLoaded, 50);
return undefined
}
if (context.waitCount) {
for (i = 0; h = waitAry[i]; i++) forceExec(h, {});
checkLoaded();
return undefined
}
req.checkReadyState();
return undefined
}
function forceExec(a, b) {
if (a.isDone) return undefined;
var c = a.fullName,
d = a.depArray,
e, f;
if (c) {
if (b[c]) return defined[c];
b[c] = !0
}
for (f = 0; f < d.length; f++) e = d[f], e && (!a.deps[e] && waiting[e] && a.onDep(e, forceExec(waiting[e], b)));
return c ? defined[c] : undefined
}
function jQueryCheck(a) {
if (!context.jQuery) {
var b = a || (typeof jQuery !== "undefined" ? jQuery : null);
b && "readyWait" in b && (context.jQuery = b, callDefMain(["jquery", [], function () {
return jQuery
}]), context.scriptCount && (b.readyWait += 1, context.jQueryIncremented = !0))
}
}
function callDefMain(a) {
main.apply(null, a), loaded[a[0]] = !0
}
function main(a, b, c, d) {
var e = makeModuleMap(a, d),
f = e.name,
g = e.fullName,
h = {},
i = {
waitId: f || reqWaitIdPrefix + waitIdCounter++,
depCount: 0,
depMax: 0,
prefix: e.prefix,
name: f,
fullName: g,
deps: {},
depArray: b,
callback: c,
onDep: function (a, b) {
a in i.deps || (i.deps[a] = b, i.depCount += 1, i.depCount === i.depMax && execManager(i))
}
},
j, k, l, m;
if (g) {
if (g in defined || loaded[g] === !0) return;
specified[g] = !0, loaded[g] = !0, context.jQueryDef = g === "jquery"
}
for (j = 0; j < b.length; j++) k = b[j], k && (k = makeModuleMap(k, f ? e : d), l = k.fullName, b[j] = l, l === "require" ? i.deps[l] = makeRequire(e) : l === "exports" ? (i.deps[l] = defined[g] = {}, i.usingExports = !0) : l === "module" ? (i.cjsModule = m = i.deps[l] = {
id: f,
uri: f ? context.nameToUrl(f, null, d) : undefined
}, m.setExports = makeSetExports(m)) : l in defined && !(l in waiting) ? i.deps[l] = defined[l] : h[l] || (i.depMax += 1, queueDependency(k), (managerCallbacks[l] || (managerCallbacks[l] = [])).push(i), h[l] = !0));
i.depCount === i.depMax ? execManager(i) : (waiting[i.waitId] = i, waitAry.push(i), context.waitCount += 1)
}
function execManager(a) {
var b, c, d, e = a.callback,
f = a.fullName,
g = [],
h = a.depArray;
if (e && isFunction(e)) {
if (h) for (b = 0; b < h.length; b++) g.push(a.deps[h[b]]);
c = req.execCb(f, a.callback, g);
if (f) if (!a.usingExports || c !== undefined || a.cjsModule && "exports" in a.cjsModule) if (a.cjsModule && "exports" in a.cjsModule) c = defined[f] = a.cjsModule.exports;
else {
if (f in defined && !a.usingExports) return req.onError(new Error(f + " has already been defined"));
defined[f] = c
} else c = defined[f]
} else f && (c = defined[f] = e);
if (f) {
d = managerCallbacks[f];
if (d) {
for (b = 0; b < d.length; b++) d[b].onDep(f, c);
delete managerCallbacks[f]
}
}
waiting[a.waitId] && (delete waiting[a.waitId], a.isDone = !0, context.waitCount -= 1, context.waitCount === 0 && (waitAry = []));
return undefined
}
function queueDependency(a) {
var b = a.prefix,
c = a.fullName;
specified[c] || c in defined || (b && !plugins[b] && (plugins[b] = undefined, (normalizedWaiting[b] || (normalizedWaiting[b] = [])).push(a), (managerCallbacks[b] || (managerCallbacks[b] = [])).push({
onDep: function (a, c) {
a === b && updateNormalizedNames(b)
}
}), queueDependency(makeModuleMap(b))), context.paused.push(a))
}
function updateNormalizedNames(a) {
var b, c, d, e, f, g, h, i, j, k, l = normalizedWaiting[a];
if (l) for (g = 0; c = l[g]; g++) {
b = c.fullName, d = makeModuleMap(c.originalName, c.parentMap), e = d.fullName, f = managerCallbacks[b] || [], k = managerCallbacks[e];
if (e !== b) {
b in specified && (delete specified[b], specified[e] = !0), k ? managerCallbacks[e] = k.concat(f) : managerCallbacks[e] = f, delete managerCallbacks[b];
for (h = 0; h < f.length; h++) {
j = f[h].depArray;
for (i = 0; i < j.length; i++) j[i] === b && (j[i] = e)
}
}
}
delete normalizedWaiting[a]
}
function makeRequire(a, b) {
var c = makeContextModuleFunc(context.require, a, b);
mixin(c, {
nameToUrl: makeContextModuleFunc(context.nameToUrl, a),
toUrl: makeContextModuleFunc(context.toUrl, a),
isDefined: makeContextModuleFunc(context.isDefined, a),
ready: req.ready,
isBrowser: req.isBrowser
}), req.paths && (c.paths = req.paths);
return c
}
function makeContextModuleFunc(a, b, c) {
return function () {
var d = [].concat(aps.call(arguments, 0)),
e;
c && isFunction(e = d[d.length - 1]) && (e.__requireJsBuild = !0), d.push(b);
return a.apply(null, d)
}
}
function makeSetExports(a) {
return function (b) {
a.exports = b
}
}
function isPriorityDone() {
var a = !0,
b = config.priorityWait,
c, d;
if (b) {
for (d = 0; c = b[d]; d++) if (!loaded[c]) {
a = !1;
break
}
a && delete config.priorityWait
}
return a
}
function makeModuleMap(a, b) {
var c = a ? a.indexOf("!") : -1,
d = null,
e = b ? b.name : null,
f = a,
g, h, i;
c !== -1 && (d = a.substring(0, c), a = a.substring(c + 1, a.length)), d && (d = normalize(d, e)), a && (d ? (i = defined[d], i ? i.normalize ? g = i.normalize(a, function (a) {
return normalize(a, e)
}) : g = normalize(a, e) : g = "__$p" + e + "@" + a) : g = normalize(a, e), h = urlMap[g], h || (req.toModuleUrl ? h = req.toModuleUrl(context, a, b) : h = context.nameToUrl(a, null, b), urlMap[g] = h));
return {
prefix: d,
name: g,
parentMap: b,
url: h,
originalName: f,
fullName: d ? d + "!" + g : g
}
}
function normalize(a, b) {
var c, d;
a.charAt(0) === "." && (b && (config.pkgs[b] ? b = [b] : (b = b.split("/"), b = b.slice(0, b.length - 1)), a = b.concat(a.split("/")), trimDots(a), d = config.pkgs[c = a[0]], a = a.join("/"), d && a === c + "/" + d.main && (a = c)));
return a
}
function trimDots(a) {
var b, c;
for (b = 0; c = a[b]; b++) if (c === ".") a.splice(b, 1), b -= 1;
else if (c === "..") if (b !== 1 || a[2] !== ".." && a[0] !== "..") b > 0 && (a.splice(b - 1, 2), b -= 2);
else break
}
var context, resume, config = {
waitSeconds: 7,
baseUrl: s.baseUrl || "./",
paths: {},
pkgs: {}
},
defQueue = [],
specified = {
require: !0,
exports: !0,
module: !0
},
urlMap = {},
defined = {},
loaded = {},
waiting = {},
waitAry = [],
waitIdCounter = 0,
managerCallbacks = {},
plugins = {},
pluginsQueue = {},
resumeDepth = 0,
normalizedWaiting = {};
resume = function () {
var a, b, c;
resumeDepth += 1, context.scriptCount <= 0 && (context.scriptCount = 0);
while (defQueue.length) {
a = defQueue.shift();
if (a[0] === null) return req.onError(new Error("Mismatched anonymous require.def modules"));
callDefMain(a)
}
if (!config.priorityWait || isPriorityDone()) while (context.paused.length) {
c = context.paused, context.pausedCount += c.length, context.paused = [];
for (b = 0; a = c[b]; b++) loadPaused(a);
context.startTime = (new Date).getTime(), context.pausedCount -= c.length
}
resumeDepth === 1 && checkLoaded(), resumeDepth -= 1;
return undefined
}, context = {
contextName: contextName,
config: config,
defQueue: defQueue,
waiting: waiting,
waitCount: 0,
specified: specified,
loaded: loaded,
urlMap: urlMap,
scriptCount: 0,
urlFetched: {},
defined: defined,
paused: [],
pausedCount: 0,
plugins: plugins,
managerCallbacks: managerCallbacks,
makeModuleMap: makeModuleMap,
normalize: normalize,
configure: function (a) {
var b, c, d, e, f, g;
a.baseUrl && (a.baseUrl.charAt(a.baseUrl.length - 1) !== "/" && (a.baseUrl += "/")), b = config.paths, d = config.packages, e = config.pkgs, mixin(config, a, !0);
if (a.paths) {
for (c in a.paths) c in empty || (b[c] = a.paths[c]);
config.paths = b
}
f = a.packagePaths;
if (f || a.packages) {
if (f) for (c in f) c in empty || configurePackageDir(e, f[c], c);
a.packages && configurePackageDir(e, a.packages), config.pkgs = e
}
a.priority && (g = context.requireWait, context.requireWait = !1, context.takeGlobalQueue(), resume(), context.require(a.priority), resume(), context.requireWait = g, config.priorityWait = a.priority), (a.deps || a.callback) && context.require(a.deps || [], a.callback), a.ready && req.ready(a.ready)
},
isDefined: function (a, b) {
return makeModuleMap(a, b).fullName in defined
},
require: function (a, b, c) {
var d, e, f;
if (typeof a === "string") {
if (req.get) return req.get(context, a, b);
d = a, c = b, f = makeModuleMap(d, c), e = defined[f.fullName];
if (e === undefined) return req.onError(new Error("require: module name '" + f.fullName + "' has not been loaded yet for context: " + contextName));
return e
}
main(null, a, b, c);
if (!context.requireWait) while (!context.scriptCount && context.paused.length) resume();
return undefined
},
takeGlobalQueue: function () {
globalDefQueue.length && (apsp.apply(context.defQueue, [context.defQueue.length - 1, 0].concat(globalDefQueue)), globalDefQueue = [])
},
completeLoad: function (a) {
var b;
context.takeGlobalQueue();
while (defQueue.length) {
b = defQueue.shift();
if (b[0] === null) {
b[0] = a;
break
}
if (b[0] === a) break;
callDefMain(b), b = null
}
b ? callDefMain(b) : callDefMain([a, [], a === "jquery" && typeof jQuery !== "undefined" ?
function () {
return jQuery
} : null]), loaded[a] = !0, jQueryCheck(), req.isAsync && (context.scriptCount -= 1), resume(), req.isAsync || (context.scriptCount -= 1)
},
toUrl: function (a, b) {
var c = a.lastIndexOf("."),
d = null;
c !== -1 && (d = a.substring(c, a.length), a = a.substring(0, c));
return context.nameToUrl(a, d, b)
},
nameToUrl: function (a, b, c) {
var d, e, f, g, h, i, j, k, l = context.config;
if (a.indexOf("./") === 0 || a.indexOf("../") === 0) h = c && c.url ? c.url.split("/") : [], h.length && h.pop(), h = h.concat(a.split("/")), trimDots(h), k = h.join("/") + (b ? b : req.jsExtRegExp.test(a) ? "" : ".js");
else {
a = normalize(a, c);
if (req.jsExtRegExp.test(a)) k = a + (b ? b : "");
else {
d = l.paths, e = l.pkgs, h = a.split("/");
for (i = h.length; i > 0; i--) {
j = h.slice(0, i).join("/");
if (d[j]) {
h.splice(0, i, d[j]);
break
}
if (f = e[j]) {
a === f.name ? g = f.location + "/" + f.main : g = f.location + "/" + f.lib, h.splice(0, i, g);
break
}
}
k = h.join("/") + (b || ".js"), k = (k.charAt(0) === "/" || k.match(/^\w+:/) ? "" : l.baseUrl) + k
}
}
return l.urlArgs ? k + ((k.indexOf("?") === -1 ? "?" : "&") + l.urlArgs) : k
}
}, context.jQueryCheck = jQueryCheck, context.resume = resume;
return context
}
function configurePackageDir(a, b, c) {
var d, e, f;
for (d = 0; f = b[d]; d++) f = typeof f === "string" ? {
name: f
} : f, e = f.location, c && (!e || e.indexOf("/") !== 0 && e.indexOf(":") === -1) && (e = c + "/" + (e || f.name)), a[f.name] = {
name: f.name,
location: e || f.name,
lib: f.lib || "lib",
main: (f.main || "lib/main").replace(currDirRegExp, "").replace(jsSuffixRegExp, "")
}
}
function mixin(a, b, c) {
for (var d in b)!(d in empty) && (!(d in a) || c) && (a[d] = b[d]);
return req
}
function isArray(a) {
return ostring.call(a) === "[object Array]"
}
function isFunction(a) {
return ostring.call(a) === "[object Function]"
}
var version = "0.24.0",
commentRegExp = /(\/\*([\s\S]*?)\*\/|\/\/(.*)$)/mg,
cjsRequireRegExp = /require\(["']([^'"\s]+)["']\)/g,
currDirRegExp = /^\.\//,
jsSuffixRegExp = /\.js$/,
ostring = Object.prototype.toString,
ap = Array.prototype,
aps = ap.slice,
apsp = ap.splice,
isBrowser = typeof window !== "undefined" && navigator && document,
isWebWorker = !isBrowser && typeof importScripts !== "undefined",
readyRegExp = isBrowser && navigator.platform === "PLAYSTATION 3" ? /^complete$/ : /^(complete|loaded)$/,
defContextName = "_",
isOpera = typeof opera !== "undefined" && opera.toString() === "[object Opera]",
reqWaitIdPrefix = "_r@@",
empty = {},
contexts = {},
globalDefQueue = [],
interactiveScript = null,
isDone = !1,
useInteractive = !1,
req, cfg = {},
currentlyAddingScript, s, head, baseElement, scripts, script, src, subPath, mainScript, dataMain, i, scrollIntervalId, setReadyState, ctx;
if (typeof require !== "undefined") {
if (isFunction(require)) return;
cfg = require
}
req = require = function (a, b) {
var c = defContextName,
d, e;
!isArray(a) && typeof a !== "string" && (e = a, isArray(b) ? (a = b, b = arguments[2]) : a = []), e && e.context && (c = e.context), d = contexts[c] || (contexts[c] = newContext(c)), e && d.configure(e);
return d.require(a, b)
}, req.version = version, req.isArray = isArray, req.isFunction = isFunction, req.mixin = mixin, req.jsExtRegExp = /^\/|:|\?|\.js$/, s = req.s = {
contexts: contexts,
skipAsync: {},
isPageLoaded: !isBrowser,
readyCalls: []
}, req.isAsync = req.isBrowser = isBrowser, isBrowser && (head = s.head = document.getElementsByTagName("head")[0], baseElement = document.getElementsByTagName("base")[0], baseElement && (head = s.head = baseElement.parentNode)), req.onError = function (a) {
throw a
}, req.load = function (a, b, c) {
var d = a.contextName,
e = a.urlFetched,
f = a.loaded;
isDone = !1, f[b] || (f[b] = !1), e[c] || (a.scriptCount += 1, req.attach(c, d, b), e[c] = !0, a.jQuery && !a.jQueryIncremented && (a.jQuery.readyWait += 1, a.jQueryIncremented = !0))
}, define = req.def = function (a, b, c) {
var d, e;
typeof a !== "string" && (c = b, b = a, a = null), req.isArray(b) || (c = b, b = []), !a && !b.length && req.isFunction(c) && (c.length && (c.toString().replace(commentRegExp, "").replace(cjsRequireRegExp, function (a, c) {
b.push(c)
}), b = ["require", "exports", "module"].concat(b)));
if (useInteractive) {
d = currentlyAddingScript || getInteractiveScript();
if (!d) return req.onError(new Error("ERROR: No matching script interactive for " + c));
a || (a = d.getAttribute("data-requiremodule")), e = contexts[d.getAttribute("data-requirecontext")]
}(e ? e.defQueue : globalDefQueue).push([a, b, c]);
return undefined
}, define.amd = {
multiversion: !0,
plugins: !0
}, req.execCb = function (a, b, c) {
return b.apply(null, c)
}, req.onScriptLoad = function (a) {
var b = a.currentTarget || a.srcElement,
c, d, e;
if (a.type === "load" || readyRegExp.test(b.readyState)) interactiveScript = null, c = b.getAttribute("data-requirecontext"), d = b.getAttribute("data-requiremodule"), e = contexts[c], contexts[c].completeLoad(d), b.detachEvent && !isOpera ? b.detachEvent("onreadystatechange", req.onScriptLoad) : b.removeEventListener("load", req.onScriptLoad, !1)
}, req.attach = function (a, b, c, d, e) {
var f, g, h;
if (isBrowser) {
d = d || req.onScriptLoad, f = document.createElement("script"), f.type = e || "text/javascript", f.charset = "utf-8", f.async = !s.skipAsync[a], f.setAttribute("data-requirecontext", b), f.setAttribute("data-requiremodule", c), f.attachEvent && !isOpera ? (useInteractive = !0, f.attachEvent("onreadystatechange", d)) : f.addEventListener("load", d, !1), f.src = a, currentlyAddingScript = f, baseElement ? head.insertBefore(f, baseElement) : head.appendChild(f), currentlyAddingScript = null;
return f
}
isWebWorker && (h = contexts[b], g = h.loaded, g[c] = !1, importScripts(a), h.completeLoad(c));
return null
};
if (isBrowser) {
scripts = document.getElementsByTagName("script");
for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
head || (head = script.parentNode);
if (dataMain = script.getAttribute("data-main")) {
cfg.baseUrl || (src = dataMain.split("/"), mainScript = src.pop(), subPath = src.length ? src.join("/") + "/" : "./", cfg.baseUrl = subPath, dataMain = mainScript.replace(jsSuffixRegExp, "")), cfg.deps = cfg.deps ? cfg.deps.concat(dataMain) : [dataMain];
break
}
}
}
s.baseUrl = cfg.baseUrl, req.pageLoaded = function () {
s.isPageLoaded || (s.isPageLoaded = !0, scrollIntervalId && clearInterval(scrollIntervalId), setReadyState && (document.readyState = "complete"), req.callReady())
}, req.checkReadyState = function () {
var a = s.contexts,
b;
for (b in a) if (!(b in empty)) if (a[b].waitCount) return;
s.isDone = !0, req.callReady()
}, req.callReady = function () {
var a = s.readyCalls,
b, c, d, e, f;
if (s.isPageLoaded && s.isDone) {
if (a.length) {
s.readyCalls = [];
for (b = 0; c = a[b]; b++) c()
}
d = s.contexts;
for (f in d) f in empty || (e = d[f], e.jQueryIncremented && (e.jQuery.ready(!0), e.jQueryIncremented = !1))
}
}, req.ready = function (a) {
s.isPageLoaded && s.isDone ? a() : s.readyCalls.push(a);
return req
}, isBrowser && (document.addEventListener ? (document.addEventListener("DOMContentLoaded", req.pageLoaded, !1), window.addEventListener("load", req.pageLoaded, !1), document.readyState || (setReadyState = !0, document.readyState = "loading")) : window.attachEvent && (window.attachEvent("onload", req.pageLoaded), self === self.top && (scrollIntervalId = setInterval(function () {
try {
document.body && (document.documentElement.doScroll("left"), req.pageLoaded())
} catch (a) {}
}, 30))), document.readyState === "complete" && req.pageLoaded()), req(cfg), req.isAsync && typeof setTimeout !== "undefined" && (ctx = s.contexts[cfg.context || defContextName], ctx.requireWait = !0, setTimeout(function () {
ctx.requireWait = !1, ctx.takeGlobalQueue(), ctx.jQueryCheck(), ctx.scriptCount || ctx.resume(), req.checkReadyState()
}, 0))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment