lfr-amd-loader
Version:
AMD Loader with support for combo URL and conditional loading
157 lines (116 loc) • 4.8 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: url-builder.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: url-builder.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
// External protocols regex, supports:
// "http", "https", "//" and "www."
var REGEX_EXTERNAL_PROTOCOLS = /^https?:\/\/|\/\/|www\./;
/**
* Creates an instance of URLBuilder class.
*
* @constructor
* @param {object} - instance of {@link ConfigParser} object.
*/
function URLBuilder(configParser) {
this._configParser = configParser;
}
URLBuilder.prototype = {
constructor: URLBuilder,
/**
* Returns a list of URLs from provided list of modules.
*
* @param {array} modules List of modules for which URLs should be created.
* @return {array} List of URLs.
*/
build: function (modules) {
var buffer = [];
var result = [];
var registeredModules = this._configParser.getModules();
var config = this._configParser.getConfig();
var basePath = config.basePath;
/* istanbul ignore else */
if (basePath.charAt(basePath.length - 1) !== '/') {
basePath += '/';
}
for (var i = 0; i < modules.length; i++) {
var module = registeredModules[modules[i]];
// If module has fullPath, individual URL have to be created.
if (module.fullPath) {
result.push(module.fullPath);
} else {
var path = this._getModulePath(module);
// If the URL starts with external protocol, individual URL shall be created.
if (REGEX_EXTERNAL_PROTOCOLS.test(path)) {
result.push(path);
// If combine is disabled, create individual URL based on config URL and module path.
} else if (!config.combine) {
result.push(config.url + basePath + path);
} else {
// If combine is true and module does not have full path, it will be collected
// in a buffer to be loaded among with other modules from combo loader.
buffer.push(path);
}
}
module.requested = true;
}
// Add to the result all modules, which have to be combined.
if (buffer.length) {
result.push(config.url + basePath + buffer.join('&' + basePath));
buffer.length = 0;
}
return result;
},
/**
* Returns the path for a module. If module has property path, it will be returned directly. Otherwise,
* the name of module will be used and extension .js will be added to module name if omitted.
*
* @protected
* @param {object} module The module which path should be returned.
* @return {string} Module path.
*/
_getModulePath: function (module) {
var path = module.path || module.name;
var paths = this._configParser.getConfig().paths;
for (var key in paths) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(paths, key)) {
if (path === key || path.indexOf(key + '/') === 0) {
path = paths[key] + path.substring(key.length);
}
}
}
if (!REGEX_EXTERNAL_PROTOCOLS.test(path) && path.indexOf('.js') !== path.length - 3) {
path += '.js';
}
return path;
}
};</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>