discourse/app/assets/javascripts/discourse-loader.js

218 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-07-06 02:14:30 +08:00
var define, requirejs;
(function() {
2015-05-12 01:16:44 +08:00
var MOVED_MODULES = {
"discourse/views/list/post-count-or-badges": "discourse/raw-views/list/post-count-or-badges",
"discourse/views/list/posts-count-column" : "discourse/raw-views/list/posts-count-column",
"discourse/views/list/visited-line" : "discourse/raw-views/list/visited-line",
"discourse/views/topic-list-header-column" : "discourse/raw-views/topic-list-header-column",
"discourse/views/topic-status" : "discourse/raw-views/topic-status"
};
2015-05-12 01:16:44 +08:00
var _isArray;
if (!Array.isArray) {
_isArray = function (x) {
return Object.prototype.toString.call(x) === "[object Array]";
};
} else {
_isArray = Array.isArray;
}
var registry = {};
var seen = {};
var FAILED = false;
2015-05-12 01:16:44 +08:00
var uuid = 0;
function tryFinally(tryable, finalizer) {
try {
return tryable();
} finally {
finalizer();
}
}
function unsupportedModule(length) {
throw new Error("an unsupported module was defined, expected `define(name, deps, module)` instead got: `" + length + "` arguments to define`");
}
var defaultDeps = ['require', 'exports', 'module'];
function Module(name, deps, callback, exports) {
this.id = uuid++;
this.name = name;
this.deps = !deps.length && callback.length ? defaultDeps : deps;
this.exports = exports || { };
this.callback = callback;
this.state = undefined;
this._require = undefined;
}
Module.prototype.makeRequire = function() {
var name = this.name;
return this._require || (this._require = function(dep) {
2017-07-06 02:14:30 +08:00
return requirejs(resolve(dep, name));
2015-05-12 01:16:44 +08:00
});
};
2015-05-12 01:16:44 +08:00
define = function(name, deps, callback) {
2015-05-12 01:16:44 +08:00
if (arguments.length < 2) {
unsupportedModule(arguments.length);
}
if (!_isArray(deps)) {
callback = deps;
deps = [];
}
registry[name] = new Module(name, deps, callback);
};
// we don't support all of AMD
// define.amd = {};
// we will support petals...
define.petal = { };
function Alias(path) {
this.name = path;
}
define.alias = function(path) {
return new Alias(path);
};
function reify(mod, name, rseen) {
2015-05-12 01:16:44 +08:00
var deps = mod.deps;
var length = deps.length;
var reified = new Array(length);
var dep;
2015-05-12 01:16:44 +08:00
// TODO: new Module
// TODO: seen refactor
var module = { };
for (var i = 0, l = length; i < l; i++) {
dep = deps[i];
if (dep === 'exports') {
module.exports = reified[i] = rseen;
2015-05-12 01:16:44 +08:00
} else if (dep === 'require') {
reified[i] = mod.makeRequire();
} else if (dep === 'module') {
mod.exports = rseen;
2015-05-12 01:16:44 +08:00
module = reified[i] = mod;
} else {
2015-05-12 01:16:44 +08:00
reified[i] = requireFrom(resolve(dep, name), name);
}
}
return {
deps: reified,
2015-05-12 01:16:44 +08:00
module: module
};
}
2015-05-12 01:16:44 +08:00
function requireFrom(name, origin) {
2015-05-12 01:16:44 +08:00
var mod = registry[name];
if (!mod) {
var moved = MOVED_MODULES[name];
if (moved) {
console.warn("DEPRECATION: `" + name + "` was moved to `" + moved + "`");
}
mod = registry[moved];
}
2015-05-12 01:16:44 +08:00
if (!mod) {
throw new Error('Could not find module `' + name + '` imported from `' + origin + '`');
}
2017-07-06 02:14:30 +08:00
return requirejs(name);
2015-05-12 01:16:44 +08:00
}
2015-05-12 01:16:44 +08:00
function missingModule(name) {
throw new Error('Could not find module ' + name);
}
2016-11-16 01:10:18 +08:00
requirejs = require = function(name) {
2015-05-12 01:16:44 +08:00
if (MOVED_MODULES[name]) {
name = MOVED_MODULES[name];
}
var mod = registry[name];
2015-05-12 01:16:44 +08:00
if (mod && mod.callback instanceof Alias) {
mod = registry[mod.callback.name];
}
2015-05-12 01:16:44 +08:00
if (!mod) { missingModule(name); }
if (mod.state !== FAILED &&
seen.hasOwnProperty(name)) {
return seen[name];
}
var reified;
var module;
var loaded = false;
seen[name] = { }; // placeholder for run-time cycles
2015-05-12 01:16:44 +08:00
tryFinally(function() {
reified = reify(mod, name, seen[name]);
module = mod.callback.apply(this, reified.deps);
loaded = true;
2015-05-12 01:16:44 +08:00
}, function() {
if (!loaded) {
2015-05-12 01:16:44 +08:00
mod.state = FAILED;
}
2015-05-12 01:16:44 +08:00
});
var obj;
if (module === undefined && reified.module.exports) {
obj = reified.module.exports;
} else {
obj = seen[name] = module;
}
if (obj !== null &&
(typeof obj === 'object' || typeof obj === 'function') &&
obj['default'] === undefined) {
obj['default'] = obj;
}
2015-05-12 01:16:44 +08:00
return (seen[name] = obj);
};
2016-11-16 01:10:18 +08:00
window.requireModule = requirejs;
function resolve(child, name) {
if (child.charAt(0) !== '.') { return child; }
var parts = child.split('/');
var nameParts = name.split('/');
2015-05-12 01:16:44 +08:00
var parentBase = nameParts.slice(0, -1);
for (var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
2015-05-12 01:16:44 +08:00
if (part === '..') {
if (parentBase.length === 0) {
throw new Error('Cannot access parent module of root');
}
parentBase.pop();
} else if (part === '.') {
continue;
} else { parentBase.push(part); }
}
return parentBase.join('/');
}
requirejs.entries = requirejs._eak_seen = registry;
2015-05-12 01:16:44 +08:00
requirejs.clear = function() {
requirejs.entries = requirejs._eak_seen = registry = {};
seen = {};
};
})();