2 * JavaScript backwards-compatibility and support
5 // Make calling .indexOf() on an array work on older browsers
6 if ( typeof Array.prototype.indexOf === 'undefined' ) {
7 Array.prototype.indexOf = function( needle ) {
8 for ( var i = 0; i < this.length; i++ ) {
9 if ( this[i] === needle ) {
16 // Add array comparison functionality
17 if ( typeof Array.prototype.compare === 'undefined' ) {
18 Array.prototype.compare = function( against ) {
19 if ( this.length != against.length ) {
22 for ( var i = 0; i < against.length; i++ ) {
23 if ( this[i].compare ) {
24 if ( !this[i].compare( against[i] ) ) {
28 if ( this[i] !== against[i] ) {
37 * Core MediaWiki JavaScript Library
40 window.mediaWiki = new ( function( $ ) {
44 // This will not change until we are 100% ready to turn off legacy globals
45 var LEGACY_GLOBALS = true;
55 * An object which allows single and multiple get/set/exists functionality on a list of key / value pairs
57 * @param {boolean} global whether to get/set/exists values on the window object or a private object
58 * @param {function} parser function to perform extra processing before while getting a value which accepts
59 * value and options parameters where value is a string to be parsed and options is an object of options for the
62 'configuration': function( global, parser ) {
67 var values = global === true ? window : {};
72 * Gets one or more values
74 * If called with no arguments, all values will be returned. If a parser is in use, no parsing will take
75 * place when calling with no arguments or calling with an array of names.
77 * @param {mixed} selection string name of value to get, array of string names of values to get, or object
78 * of name/option pairs
79 * @param {object} options optional set of options which are also passed to a parser if in use; only used
80 * when selection is a string
83 * // Value to use if key does not exist
87 this.get = function( selection, options ) {
88 if ( typeof selection === 'object' ) {
90 for ( s in selection ) {
91 if ( selection.hasOwnProperty( s ) ) {
92 if ( typeof s === 'string' ) {
93 return that.get( values[s], selection[s] );
95 return that.get( selection[s] );
100 } else if ( typeof selection === 'string' ) {
101 if ( typeof values[selection] === 'undefined' ) {
102 return 'fallback' in options !== 'undefined' ? options.fallback : null;
104 if ( typeof parser === 'function' ) {
105 return parser( values[selection], options );
107 return values[selection];
116 * Sets one or multiple configuration values using a key and a value or an object of keys and values
118 * @param {mixed} key string of name by which value will be made accessible, or object of name/value pairs
119 * @param {mixed} value optional value to set, only in use when key is a string
121 this.set = function( selection, value ) {
122 if ( typeof selection === 'object' ) {
123 for ( var s in selection ) {
124 values[s] = selection[s];
126 } else if ( typeof selection === 'string' && typeof value !== 'undefined' ) {
127 values[selection] = value;
132 * Checks if one or multiple configuration fields exist
134 this.exists = function( selection ) {
135 if ( typeof keys === 'object' ) {
136 for ( var s = 0; s < selection.length; s++ ) {
137 if ( !( selection[s] in values ) ) {
143 return selection in values;
152 * Dummy function which in debug mode can be replaced with a function that does something clever
154 this.log = function() { };
157 * List of configuration values
159 * In legacy mode the values this object wraps will be in the global space
161 this.config = new this.prototypes.configuration( LEGACY_GLOBALS );
164 * Information about the current user
166 this.user = new ( function() {
170 this.options = new that.prototypes.configuration();
174 * Basic parser, can be replaced with something more robust
176 this.parser = function( text, options ) {
177 if ( typeof options === 'object' && typeof options.parameters === 'object' ) {
178 for ( var p = 0; p < options.parameters.length; p++ ) {
179 text = text.replace( '\$' + ( parseInt( p ) + 1 ), options.parameters[p] );
186 * Localization system
188 this.msg = new that.prototypes.configuration( false, this.parser );
191 * Client-side module loader which integrates with the MediaWiki ResourceLoader
193 this.loader = new ( function() {
195 /* Private Members */
199 * Mapping of registered modules
201 * The jquery module is pre-registered, because it must have already been provided for this object to have
202 * been built, and in debug mode jquery would have been provided through a unique loader request, making it
203 * impossible to hold back registration of jquery until after mediawiki.
208 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
209 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
210 * 'script': function() {},
211 * 'style': 'css code string',
212 * 'messages': { 'key': 'value' },
213 * 'version': ############## (unix timestamp)
218 // List of modules which will be loaded as when ready
220 // List of modules to be loaded
222 // List of callback functions waiting for modules to be ready to be called
224 // Flag indicating that requests should be suspended
225 var suspended = true;
226 // Flag inidicating that document ready has occured
229 /* Private Methods */
232 * Generates an ISO8601 string from a UNIX timestamp
234 function formatVersionNumber( timestamp ) {
235 var date = new Date();
236 date.setTime( timestamp * 1000 );
238 return n < 10 ? '0' + n : n
241 return n < 10 ? '00' + n : ( n < 100 ? '0' + n : n );
243 return date.getUTCFullYear() + '-' +
244 pad1( date.getUTCMonth() + 1 ) + '-' +
245 pad1( date.getUTCDate() ) + 'T' +
246 pad1( date.getUTCHours() ) + ':' +
247 pad1( date.getUTCMinutes() ) + ':' +
248 pad1( date.getUTCSeconds() ) +
253 * Recursively resolves dependencies and detects circular references
255 function recurse( module, resolved, unresolved ) {
256 unresolved[unresolved.length] = module;
257 // Resolves dynamic loader function and replaces it with it's own results
258 if ( typeof registry[module].dependencies === 'function' ) {
259 registry[module].dependencies = registry[module].dependencies();
260 // Gaurantees the module's dependencies are always in an array
261 if ( typeof registry[module].dependencies !== 'object' ) {
262 registry[module].dependencies = [registry[module].dependencies];
265 // Tracks down dependencies
266 for ( var n = 0; n < registry[module].dependencies.length; n++ ) {
267 if ( resolved.indexOf( registry[module].dependencies[n] ) === -1 ) {
268 if ( unresolved.indexOf( registry[module].dependencies[n] ) !== -1 ) {
270 'Circular reference detected: ' + module + ' -> ' + registry[module].dependencies[n]
273 recurse( registry[module].dependencies[n], resolved, unresolved );
276 resolved[resolved.length] = module;
277 unresolved.splice( unresolved.indexOf( module ), 1 );
281 * Gets a list of modules names that a module dependencies in their proper dependency order
283 * @param mixed string module name or array of string module names
284 * @return list of dependencies
285 * @throws Error if circular reference is detected
287 function resolve( module, resolved, unresolved ) {
288 // Allow calling with an array of module names
289 if ( typeof module === 'object' ) {
291 for ( var m = 0; m < module.length; m++ ) {
292 var dependencies = resolve( module[m] );
293 for ( var n = 0; n < dependencies.length; n++ ) {
294 modules[modules.length] = dependencies[n];
298 } else if ( typeof module === 'string' ) {
299 // Undefined modules have no dependencies
300 if ( !( module in registry ) ) {
304 recurse( module, resolved, [] );
307 throw new Error( 'Invalid module argument: ' + module );
311 * Narrows a list of module names down to those matching a specific state. Possible states are 'undefined',
312 * 'registered', 'loading', 'loaded', or 'ready'
314 * @param mixed string or array of strings of module states to filter by
315 * @param array list of module names to filter (optional, all modules will be used by default)
316 * @return array list of filtered module names
318 function filter( states, modules ) {
319 // Allow states to be given as a string
320 if ( typeof states === 'string' ) {
323 // If called without a list of modules, build and use a list of all modules
325 if ( typeof modules === 'undefined' ) {
327 for ( module in registry ) {
328 modules[modules.length] = module;
331 // Build a list of modules which are in one of the specified states
332 for ( var s = 0; s < states.length; s++ ) {
333 for ( var m = 0; m < modules.length; m++ ) {
335 ( states[s] == 'undefined' && typeof registry[modules[m]] === 'undefined' ) ||
336 ( typeof registry[modules[m]] === 'object' && registry[modules[m]].state === states[s] )
338 list[list.length] = modules[m];
346 * Executes a loaded module, making it ready to use
348 * @param string module name to execute
350 function execute( module ) {
351 if ( typeof registry[module] === 'undefined' ) {
352 throw new Error( 'Module has not been registered yet: ' + module );
353 } else if ( registry[module].state === 'registered' ) {
354 throw new Error( 'Module has not been requested from the server yet: ' + module );
355 } else if ( registry[module].state === 'loading' ) {
356 throw new Error( 'Module has not completed loading yet: ' + module );
357 } else if ( registry[module].state === 'ready' ) {
358 throw new Error( 'Module has already been loaded: ' + module );
360 // Add style sheet to document
361 if ( typeof registry[module].style === 'string' && registry[module].style.length ) {
362 $( 'head' ).append( '<style type="text/css">' + registry[module].style + '</style>' );
363 } else if ( typeof registry[module].style === 'object' && !( registry[module].style instanceof Array ) ) {
364 for ( var media in registry[module].style ) {
366 '<style type="text/css" media="' + media + '">' + registry[module].style[media] + '</style>'
370 // Add localizations to message system
371 if ( typeof registry[module].messages === 'object' ) {
372 mediaWiki.msg.set( registry[module].messages );
376 registry[module].script();
377 registry[module].state = 'ready';
378 // Run jobs who's dependencies have just been met
379 for ( var j = 0; j < jobs.length; j++ ) {
380 if ( filter( 'ready', jobs[j].dependencies ).compare( jobs[j].dependencies ) ) {
381 if ( typeof jobs[j].ready === 'function' ) {
388 // Execute modules who's dependencies have just been met
389 for ( r in registry ) {
390 if ( registry[r].state == 'loaded' ) {
391 if ( filter( ['ready'], registry[r].dependencies ).compare( registry[r].dependencies ) ) {
397 mediaWiki.log( 'Exception thrown by ' + module + ': ' + e.message );
399 registry[module].state = 'error';
400 // Run error callbacks of jobs affected by this condition
401 for ( var j = 0; j < jobs.length; j++ ) {
402 if ( jobs[j].dependencies.indexOf( module ) !== -1 ) {
403 if ( typeof jobs[j].error === 'function' ) {
414 * Adds a dependencies to the queue with optional callbacks to be run when the dependencies are ready or fail
416 * @param mixed string moulde name or array of string module names
417 * @param function ready callback to execute when all dependencies are ready
418 * @param function error callback to execute when any dependency fails
420 function request( dependencies, ready, error ) {
421 // Allow calling by single module name
422 if ( typeof dependencies === 'string' ) {
423 dependencies = [dependencies];
424 if ( dependencies[0] in registry ) {
425 for ( var n = 0; n < registry[dependencies[0]].dependencies.length; n++ ) {
426 dependencies[dependencies.length] = registry[dependencies[0]].dependencies[n];
430 // Add ready and error callbacks if they were given
431 if ( arguments.length > 1 ) {
432 jobs[jobs.length] = {
433 'dependencies': filter( ['undefined', 'registered', 'loading', 'loaded'], dependencies ),
438 // Queue up any dependencies that are undefined or registered
439 dependencies = filter( ['undefined', 'registered'], dependencies );
440 for ( var n = 0; n < dependencies.length; n++ ) {
441 if ( queue.indexOf( dependencies[n] ) === -1 ) {
442 queue[queue.length] = dependencies[n];
449 function sortQuery(o) {
450 var sorted = {}, key, a = [];
452 if ( o.hasOwnProperty( key ) ) {
457 for ( key = 0; key < a.length; key++ ) {
458 sorted[a[key]] = o[a[key]];
466 * Requests dependencies from server, loading and executing when things when ready.
468 this.work = function() {
469 // Appends a list of modules to the batch
470 for ( var q = 0; q < queue.length; q++ ) {
471 // Only request modules which are undefined or registered
472 if ( !( queue[q] in registry ) || registry[queue[q]].state == 'registered' ) {
473 // Prevent duplicate entries
474 if ( batch.indexOf( queue[q] ) === -1 ) {
475 batch[batch.length] = queue[q];
476 // Mark registered modules as loading
477 if ( queue[q] in registry ) {
478 registry[queue[q]].state = 'loading';
483 // Clean up the queue
485 // After document ready, handle the batch
486 if ( !suspended && batch.length ) {
487 // Always order modules alphabetically to help reduce cache misses for otherwise identical content
489 // Build a list of request parameters
491 'skin': mediaWiki.config.get( 'skin' ),
492 'lang': mediaWiki.config.get( 'wgUserLanguage' ),
493 'debug': mediaWiki.config.get( 'debug' )
495 // Extend request parameters with a list of modules in the batch
497 if ( base.debug == '1' ) {
498 for ( var b = 0; b < batch.length; b++ ) {
499 requests[requests.length] = $.extend(
500 { 'modules': batch[b], 'version': registry[batch[b]].version }, base
504 // Calculate the highest timestamp
506 for ( var b = 0; b < batch.length; b++ ) {
507 if ( registry[batch[b]].version > version ) {
508 version = registry[batch[b]].version;
511 requests[requests.length] = $.extend(
512 { 'modules': batch.join( '|' ), 'version': formatVersionNumber( version ) }, base
515 // Clear the batch - this MUST happen before we append the script element to the body or it's
516 // possible that the script will be locally cached, instantly load, and work the batch again,
517 // all before we've cleared it causing each request to include modules which are already loaded
519 // Asynchronously append a script tag to the end of the body
522 for ( var r = 0; r < requests.length; r++ ) {
523 requests[r] = sortQuery( requests[r] );
524 // Build out the HTML
525 var src = mediaWiki.config.get( 'wgLoadScript' ) + '?' + $.param( requests[r] );
526 html += '<script type="text/javascript" src="' + src + '"></script>';
530 // Load asynchronously after doumument ready
532 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
534 document.write( request() );
540 * Registers a module, letting the system know about it and it's dependencies. loader.js files contain calls
543 this.register = function( module, version, dependencies, status ) {
544 // Allow multiple registration
545 if ( typeof module === 'object' ) {
546 for ( var m = 0; m < module.length; m++ ) {
547 if ( typeof module[m] === 'string' ) {
548 that.register( module[m] );
549 } else if ( typeof module[m] === 'object' ) {
550 that.register.apply( that, module[m] );
556 if ( typeof module !== 'string' ) {
557 throw new Error( 'module must be a string, not a ' + typeof module );
559 if ( typeof registry[module] !== 'undefined' && typeof status === 'undefined' ) {
560 throw new Error( 'module already implemeneted: ' + module );
562 // List the module as registered
564 'state': typeof status === 'string' ? status : 'registered',
566 'version': typeof version !== 'undefined' ? parseInt( version ) : 0
568 if ( typeof dependencies === 'string' ) {
569 // Allow dependencies to be given as a single module name
570 registry[module].dependencies = [dependencies];
571 } else if ( typeof dependencies === 'object' || typeof dependencies === 'function' ) {
572 // Allow dependencies to be given as an array of module names or a function which returns an array
573 registry[module].dependencies = dependencies;
578 * Implements a module, giving the system a course of action to take upon loading. Results of a request for
579 * one or more modules contain calls to this function.
581 this.implement = function( module, script, style, localization ) {
582 // Automaically register module
583 if ( typeof registry[module] === 'undefined' ) {
584 that.register( module );
587 if ( typeof script !== 'function' ) {
588 throw new Error( 'script must be a function, not a ' + typeof script );
590 if ( typeof style !== 'undefined' && typeof style !== 'string' && typeof style !== 'object' ) {
591 throw new Error( 'style must be a string or object, not a ' + typeof style );
593 if ( typeof localization !== 'undefined' && typeof localization !== 'object' ) {
594 throw new Error( 'localization must be an object, not a ' + typeof localization );
596 if ( typeof registry[module] !== 'undefined' && typeof registry[module].script !== 'undefined' ) {
597 throw new Error( 'module already implemeneted: ' + module );
599 // Mark module as loaded
600 registry[module].state = 'loaded';
602 registry[module].script = script;
603 if ( typeof style === 'string' || typeof style === 'object' && !( style instanceof Array ) ) {
604 registry[module].style = style;
606 if ( typeof localization === 'object' ) {
607 registry[module].messages = localization;
609 // Execute or queue callback
610 if ( filter( ['ready'], registry[module].dependencies ).compare( registry[module].dependencies ) ) {
618 * Executes a function as soon as one or more required modules are ready
620 * @param mixed string or array of strings of modules names the callback dependencies to be ready before
622 * @param function callback to execute when all dependencies are ready (optional)
623 * @param function callback to execute when if dependencies have a errors (optional)
625 this.using = function( dependencies, ready, error ) {
627 if ( typeof dependencies !== 'object' && typeof dependencies !== 'string' ) {
628 throw new Error( 'dependencies must be a string or an array, not a ' + typeof dependencies )
630 // Allow calling with a single dependency as a string
631 if ( typeof dependencies === 'string' ) {
632 dependencies = [dependencies];
634 // Resolve entire dependency map
635 dependencies = resolve( dependencies );
636 // If all dependencies are met, execute ready immediately
637 if ( filter( ['ready'], dependencies ).compare( dependencies ) ) {
638 if ( typeof ready !== 'function' ) {
642 // If any dependencies have errors execute error immediately
643 else if ( filter( ['error'], dependencies ).length ) {
644 if ( typeof error === 'function' ) {
648 // Since some dependencies are not yet ready, queue up a request
650 request( dependencies, ready, error );
655 * Loads an external script or one or more modules for future use
657 * @param {mixed} modules either the name of a module, array of modules, or a URL of an external script or style
658 * @param {string} type mime-type to use if calling with a URL of an external script or style; acceptable values
659 * are "text/css" and "text/javascript"; if no type is provided, text/javascript is assumed
661 this.load = function( modules, type ) {
663 if ( typeof modules !== 'object' && typeof modules !== 'string' ) {
664 throw new Error( 'dependencies must be a string or an array, not a ' + typeof dependencies )
666 // Allow calling with an external script or single dependency as a string
667 if ( typeof modules === 'string' ) {
668 // Support adding arbitrary external scripts
669 if ( modules.substr( 0, 7 ) == 'http://' || modules.substr( 0, 8 ) == 'https://' ) {
670 if ( type === 'text/css' ) {
671 setTimeout( function() {
672 $( 'head' ).append( '<link rel="stylesheet" type="text/css" href="' + modules + '" />' );
675 } else if ( type === 'text/javascript' || typeof type === 'undefined' ) {
676 setTimeout( function() {
677 $( 'body' ).append( '<script type="text/javascript" src="' + modules + '"></script>' );
684 // Called with single module
687 // Resolve entire dependency map
688 modules = resolve( modules );
689 // If all modules are ready, nothing dependency be done
690 if ( filter( ['ready'], modules ).compare( modules ) ) {
693 // If any modules have errors return false
694 else if ( filter( ['error'], modules ).length ) {
697 // Since some modules are not yet ready, queue up a request
705 * Flushes the request queue and begin executing load requests on demand
707 this.go = function() {
713 * Changes the state of a module
715 * @param mixed module string module name or object of module name/state pairs
716 * @param string state string state name
718 this.state = function( module, state ) {
719 if ( typeof module === 'object' ) {
720 for ( var m in module ) {
721 that.state( m, module[m] );
725 if ( module in registry ) {
726 registry[module].state = state;
730 /* Cache document ready status */
732 $(document).ready( function() { ready = true; } );
735 /* Extension points */
743 /* Auto-register from pre-loaded startup scripts */
745 if ( typeof window['startUp'] === 'function' ) {
747 delete window['startUp'];