lfr-amd-loader
Version:
AMD Loader with support for combo URL and conditional loading
237 lines (193 loc) • 7.17 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: config-parser.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: config-parser.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* Creates an instance of ConfigurationParser class.
*
* @constructor
* @param {object=} - The configuration object to be parsed.
*/
function ConfigParser(config) {
this._config = {};
this._modules = {};
this._conditionalModules = {};
this._parseConfig(config);
}
ConfigParser.prototype = {
constructor: ConfigParser,
/**
* Adds a module to the configuration.
*
* @param {object} module The module which should be added to the configuration. Should have the following
* properties:
* <ul>
* <strong>Obligatory properties</strong>:
* <li>name (String) The name of the module</li>
* <li>dependencies (Array) The modules from which it depends</li>
* </ul>
*
* <strong>Optional parameters:</strong>
* The same as those which config parameter of {@link Loader#define} method accepts.
*/
addModule: function (module) {
// Module might be added via configuration or when it arrives from the server.
// If it arrives from the server, it will have already a definition. In this case,
// we will overwrite the existing properties with those, provided from the module definition.
// Otherwise, we will just add it to the map.
var moduleDefinition = this._modules[module.name];
if (moduleDefinition) {
for (var key in module) {
if (hasOwnProperty.call(module, key)) {
moduleDefinition[key] = module[key];
}
}
} else {
this._modules[module.name] = module;
}
this._registerConditionalModule(module);
},
/**
* Returns the current configuration.
*
* @return {object} The current configuration.
*/
getConfig: function () {
return this._config;
},
/**
* Returns map with all currently registered conditional modules and their triggers.
*
* @return {object} Map with all currently registered conditional modules.
*/
getConditionalModules: function () {
return this._conditionalModules;
},
/**
* Returns map with all currently registered modules.
*
* @return {object} Map with all currently registered modules.
*/
getModules: function () {
return this._modules;
},
/**
* Maps module names to their aliases. Example:
* __CONFIG__.maps = {
* liferay: 'liferay@1.0.0'
* }
*
* When someone does require('liferay/html/js/ac.es',...),
* if the module 'liferay/html/js/ac.es' is not defined,
* then a corresponding alias will be searched. If found, the name will be replaced,
* so it will look like user did require('liferay@1.0.0/html/js/ac.es',...).
*
* @protected
* @param {array|string} module The module which have to be mapped or array of modules.
* @return {array|string} The mapped module or array of mapped modules.
*/
mapModule: function(module) {
var modules;
if (Array.isArray(module)) {
modules = module;
} else {
modules = [module];
}
for (var i = 0; i < modules.length; i++) {
var tmpModule = modules[i];
for (var alias in this._config.maps) {
/* istanbul ignore else */
if (hasOwnProperty.call(this._config.maps, alias)) {
if (tmpModule === alias || tmpModule.indexOf(alias + '/') === 0) {
tmpModule = this._config.maps[alias] + tmpModule.substring(alias.length);
modules[i] = tmpModule;
break;
}
}
}
}
return Array.isArray(module) ? modules : modules[0];
},
/**
* Parses configuration object.
*
* @protected
* @param {object} config Configuration object to be parsed.
* @return {object} The created configuration
*/
_parseConfig: function (config) {
for (var key in config) { /* istanbul ignore else */
if (hasOwnProperty.call(config, key)) {
if (key === 'modules') {
this._parseModules(config[key]);
} else {
this._config[key] = config[key];
}
}
}
return this._config;
},
/**
* Parses a provided modules configuration.
*
* @protected
* @param {object} modules Map of modules to be parsed.
* @return {object} Map of parsed modules.
*/
_parseModules: function (modules) {
for (var key in modules) { /* istanbul ignore else */
if (hasOwnProperty.call(modules, key)) {
var module = modules[key];
module.name = key;
this.addModule(module);
}
}
return this._modules;
},
/**
* Registers conditional module to the configuration.
*
* @protected
* @param {object} module Module object
*/
_registerConditionalModule: function (module) {
// Create HashMap of all modules, which have conditional modules, as an Array.
if (module.condition) {
var existingModules = this._conditionalModules[module.condition.trigger];
if (!existingModules) {
this._conditionalModules[module.condition.trigger] = existingModules = [];
}
existingModules.push(module.name);
}
}
};</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="ConfigParser.html">ConfigParser</a></li><li><a href="DependencyBuilder.html">DependencyBuilder</a></li><li><a href="EventEmitter.html">EventEmitter</a></li><li><a href="Loader.html">Loader</a></li><li><a href="URLBuilder.html">URLBuilder</a></li></ul><h3>Events</h3><ul><li><a href="Loader.html#event:moduleRegister">moduleRegister</a></li></ul>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Tue Jun 16 2015 17:51:36 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>