Improve query parsing

This commit is contained in:
Alex Dima 2016-08-24 11:04:07 +02:00
parent e76aa16381
commit 346cb83091

View file

@ -1,5 +1,29 @@
(function() { (function() {
function parseLoaderOptions() {
function parseQueryString() {
var str = window.location.search;
str = str.replace(/^\?/, '');
var pieces = str.split(/&/);
var result = {};
pieces.forEach(function(piece) {
var config = piece.split(/=/);
result[config[0]] = config[1];
});
return result;
}
var overwrites = parseQueryString();
var result = {};
result['editor'] = overwrites['editor'] || 'npm';
METADATA.PLUGINS.map(function(plugin) {
result[plugin.name] = overwrites[plugin.name] || 'npm';
});
return result;
}
var LOADER_OPTS = parseLoaderOptions();
// console.log(JSON.stringify(LOADER_OPTS, null, '\t'));
self.loadDevEditor = function() { self.loadDevEditor = function() {
return (getQueryStringValue('editor') === 'dev'); return (getQueryStringValue('editor') === 'dev');
} }
@ -8,29 +32,21 @@
return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
} }
function resolveCorePath(core) {
if (loadDevEditor()) { function resolvePath(paths, selectedPath) {
return core.paths.dev; if (selectedPath === 'npm') {
return '/monaco-editor/' + paths[selectedPath];
} else { } else {
return '/monaco-editor/' + core.paths.npm; return paths[selectedPath];
} }
} }
self.RESOLVED_CORE_PATH = resolvePath(METADATA.CORE.paths, LOADER_OPTS['editor']);
function resolvePluginPath(plugin) {
if (plugin.paths.dev && getQueryStringValue(plugin.name) === 'dev') {
return plugin.paths.dev;
} else {
return '/monaco-editor/' + plugin.paths.npm;
}
}
self.RESOLVED_CORE_PATH = resolveCorePath(METADATA.CORE);
var RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) { var RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) {
return { return {
name: plugin.name, name: plugin.name,
contrib: plugin.contrib, contrib: plugin.contrib,
modulePrefix: plugin.modulePrefix, modulePrefix: plugin.modulePrefix,
path: resolvePluginPath(plugin) path: resolvePath(plugin.paths, LOADER_OPTS[plugin.name])
}; };
}); });
self.METADATA = null; self.METADATA = null;