-
- var currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase ),
- currReqBaseLength = $.param( currReqBase ).length,
- moduleMap = {},
- // We may need to split up the request to honor the query string length limit,
- // so build it piece by piece.
- l = currReqBaseLength + 9; // '&modules='.length == 9
-
- moduleMap = {}; // { prefix: [ suffixes ] }
-
- for ( var i = 0; i < modules.length; i++ ) {
- // Determine how many bytes this module would add to the query string
- var lastDotIndex = modules[i].lastIndexOf( '.' ),
- // Note that these substr() calls work even if lastDotIndex == -1
- prefix = modules[i].substr( 0, lastDotIndex ),
- suffix = modules[i].substr( lastDotIndex + 1 ),
- bytesAdded = prefix in moduleMap
- ? suffix.length + 3 // '%2C'.length == 3
- : modules[i].length + 3; // '%7C'.length == 3
-
- // If the request would become too long, create a new one,
- // but don't create empty requests
- if ( maxQueryLength > 0 && !$.isEmptyObject( moduleMap ) && l + bytesAdded > maxQueryLength ) {
- // This request would become too long, create a new one
- // and fire off the old one
- doRequest( moduleMap, currReqBase, sourceLoadScript );
- moduleMap = {};
- l = currReqBaseLength + 9;
+ // Validate input
+ if ( typeof module !== 'string' ) {
+ throw new Error( 'module must be a string, not a ' + typeof module );
+ }
+ if ( registry[module] !== undefined ) {
+ throw new Error( 'module already registered: ' + module );
+ }
+ // List the module as registered
+ registry[module] = {
+ 'version': version !== undefined ? parseInt( version, 10 ) : 0,
+ 'dependencies': [],
+ 'group': typeof group === 'string' ? group : null,
+ 'source': typeof source === 'string' ? source: 'local',
+ 'state': 'registered'
+ };
+ if ( typeof dependencies === 'string' ) {
+ // Allow dependencies to be given as a single module name
+ registry[module].dependencies = [dependencies];
+ } else if ( typeof dependencies === 'object' || $.isFunction( dependencies ) ) {
+ // Allow dependencies to be given as an array of module names
+ // or a function which returns an array
+ registry[module].dependencies = dependencies;
+ }
+ },
+
+ /**
+ * Implements a module, giving the system a course of action to take
+ * upon loading. Results of a request for one or more modules contain
+ * calls to this function.
+ *
+ * All arguments are required.
+ *
+ * @param module String: Name of module
+ * @param script Mixed: Function of module code or String of URL to be used as the src
+ * attribute when adding a script element to the body
+ * @param style Object: Object of CSS strings keyed by media-type or Object of lists of URLs
+ * keyed by media-type
+ * @param msgs Object: List of key/value pairs to be passed through mw.messages.set
+ */
+ implement: function ( module, script, style, msgs ) {
+ // Validate input
+ if ( typeof module !== 'string' ) {
+ throw new Error( 'module must be a string, not a ' + typeof module );
+ }
+ if ( !$.isFunction( script ) && !$.isArray( script ) ) {
+ throw new Error( 'script must be a function or an array, not a ' + typeof script );
+ }
+ if ( !$.isPlainObject( style ) ) {
+ throw new Error( 'style must be an object, not a ' + typeof style );
+ }
+ if ( !$.isPlainObject( msgs ) ) {
+ throw new Error( 'msgs must be an object, not a ' + typeof msgs );
+ }
+ // Automatically register module
+ if ( registry[module] === undefined ) {
+ mw.loader.register( module );
+ }
+ // Check for duplicate implementation
+ if ( registry[module] !== undefined && registry[module].script !== undefined ) {
+ throw new Error( 'module already implemented: ' + module );
+ }
+ // Mark module as loaded
+ registry[module].state = 'loaded';
+ // Attach components
+ registry[module].script = script;
+ registry[module].style = style;
+ registry[module].messages = msgs;
+ // Execute or queue callback
+ if ( compare(
+ filter( ['ready'], registry[module].dependencies ),
+ registry[module].dependencies ) )
+ {
+ execute( module );
+ }
+ },
+
+ /**
+ * Executes a function as soon as one or more required modules are ready
+ *
+ * @param dependencies {String|Array} Module name or array of modules names the callback
+ * dependends on to be ready before executing
+ * @param ready {Function} callback to execute when all dependencies are ready (optional)
+ * @param error {Function} callback to execute when if dependencies have a errors (optional)
+ */
+ using: function ( dependencies, ready, error ) {
+ var tod = typeof dependencies;
+ // Validate input
+ if ( tod !== 'object' && tod !== 'string' ) {
+ throw new Error( 'dependencies must be a string or an array, not a ' + tod );
+ }
+ // Allow calling with a single dependency as a string
+ if ( tod === 'string' ) {
+ dependencies = [dependencies];
+ }
+ // Resolve entire dependency map
+ dependencies = resolve( dependencies );
+ // If all dependencies are met, execute ready immediately
+ if ( compare( filter( ['ready'], dependencies ), dependencies ) ) {
+ if ( $.isFunction( ready ) ) {
+ ready();