All files modules.js

72% Statements 18/25
50% Branches 4/8
66.67% Functions 4/6
72% Lines 18/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                            2x 2x     1x                       5x           5x 2x   3x   5x 5x 5x             5x                         5x   5x 1x   4x   4x                                                               1x   1x  
/**
 * Class for managing modules
 *
 * A module in this context is essentially a Javascript class (not to be confused with
 * ResourceLoader modules).
 *
 * @class ModuleLoader
 * @extends OO.EventEmitter
 */
function ModuleLoader() {
	/**
	 * @property {Object} register of defined modules
	 * @private
	 */
	this._register = {};
	OO.EventEmitter.call( this );
}
 
ModuleLoader.prototype = {
	/**
	 * Require (import) a module previously defined using define().
	 * Searches core module registry using mw.loader.require before consulting
	 * its own local registry. This method is deprecated, please do not use.
	 * @memberof ModuleLoader
	 * @instance
	 * @param {string} id Required module id.
	 * @return {Object} Required module, can be any JavaScript object.
	 */
	require: function ( id ) {
		var module, args,
			registry = this._register;
 
		/**
		 * @return {Object} Module
		 */
		function localRequire() {
			if ( !Object.hasOwnProperty.call( registry, id ) ) {
				throw new Error( 'MobileFrontend Module not found: ' + id );
			}
			return registry[ id ];
		}
		args = id.split( '/' );
		try {
			module = mw.loader.require( args[0] );
			if ( module[ args[1] ] ) {
				return module[ args[1] ];
			} else {
				return localRequire();
			}
		} catch ( e ) {
			return localRequire();
		}
	},
 
	/**
	 * Define a module which can be later required (imported) using require().
	 * @memberof ModuleLoader
	 * @instance
	 * @param {string} id Defined module id.
	 * @param {Object} obj Defined module body, can be any JavaScript object.
	 * @return {Object}
	 */
	define: function ( id, obj ) {
		var self = this;
 
		if ( Object.hasOwnProperty.call( this._register, id ) ) {
			throw new Error( 'Module already exists: ' + id );
		}
		this._register[ id ] = obj;
		// return an object of additionally functions to do with the registered module
		return {
			/**
			 * @see ModuleLoader#deprecate
			 * @param {string} deprecatedId Defined module id, which is deprecated.
			 * @ignore
			 */
			deprecate: function ( deprecatedId ) {
				self.deprecate( deprecatedId, obj, id );
			}
		};
	},
 
	/**
	 * Deprecate a module and give an replacement (if there is any).
	 * @memberof ModuleLoader
	 * @instance
	 * @param {string} id Defined module id, which is deprecated.
	 * @param {Object} obj Defined module body, can be any JavaScript object.
	 * @param {string} [replacement] Give an optional replacement for this module (which
	 * needs to be already defined!)
	 */
	deprecate: function ( id, obj, replacement ) {
		var msg;
		if ( replacement ) {
			// add an alternative for this module, if any given
			msg = 'Use ' + replacement + ' instead.';
		}
		// register it as a deprecated one
		mw.log.deprecate( this._register, id, obj, msg );
	}
};
 
OO.mixinClass( ModuleLoader, OO.EventEmitter );
 
module.exports = ModuleLoader;