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; in the form of function( value, options )
59 * where value is the data to be parsed and options is additional data passed through to the parser
61 'configuration': function( global
, parser
) {
66 var values
= global
=== true ? window
: {};
71 * Gets one or more values
73 * If called with no arguments, all values will be returned. If a parser is in use, no parsing will take
74 * place when calling with no arguments or calling with an array of names.
76 * @param {mixed} selection string name of value to get, array of string names of values to get, or object
77 * of name/option pairs
78 * @param {object} options optional set of options which are also passed to a parser if in use; only used
79 * when selection is a string
82 * // Value to use if key does not exist
86 this.get = function( selection
, options
) {
87 if ( typeof selection
=== 'object' ) {
89 for ( var s
in selection
) {
90 if ( selection
.hasOwnProperty( s
) ) {
91 if ( typeof s
=== 'string' ) {
92 return that
.get( values
[s
], selection
[s
] );
94 return that
.get( selection
[s
] );
99 } else if ( typeof selection
=== 'string' ) {
100 if ( typeof values
[selection
] === 'undefined' ) {
101 return typeof options
=== 'object' && 'fallback' in options
?
102 options
.fallback
: '<' + selection
+ '>';
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 "basic" string from a UNIX timestamp
234 function formatVersionNumber( timestamp
) {
235 function pad( a
, b
, c
) {
236 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
239 d
.setTime( timestamp
* 1000 );
241 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
242 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
247 * Recursively resolves dependencies and detects circular references
249 function recurse( module
, resolved
, unresolved
) {
250 unresolved
[unresolved
.length
] = module
;
251 // Resolves dynamic loader function and replaces it with it's own results
252 if ( typeof registry
[module
].dependencies
=== 'function' ) {
253 registry
[module
].dependencies
= registry
[module
].dependencies();
254 // Gaurantees the module's dependencies are always in an array
255 if ( typeof registry
[module
].dependencies
!== 'object' ) {
256 registry
[module
].dependencies
= [registry
[module
].dependencies
];
259 // Tracks down dependencies
260 for ( var n
= 0; n
< registry
[module
].dependencies
.length
; n
++ ) {
261 if ( resolved
.indexOf( registry
[module
].dependencies
[n
] ) === -1 ) {
262 if ( unresolved
.indexOf( registry
[module
].dependencies
[n
] ) !== -1 ) {
264 'Circular reference detected: ' + module
+ ' -> ' + registry
[module
].dependencies
[n
]
267 recurse( registry
[module
].dependencies
[n
], resolved
, unresolved
);
270 resolved
[resolved
.length
] = module
;
271 unresolved
.splice( unresolved
.indexOf( module
), 1 );
275 * Gets a list of modules names that a module dependencies in their proper dependency order
277 * @param mixed string module name or array of string module names
278 * @return list of dependencies
279 * @throws Error if circular reference is detected
281 function resolve( module
, resolved
, unresolved
) {
282 // Allow calling with an array of module names
283 if ( typeof module
=== 'object' ) {
285 for ( var m
= 0; m
< module
.length
; m
++ ) {
286 var dependencies
= resolve( module
[m
] );
287 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
288 modules
[modules
.length
] = dependencies
[n
];
292 } else if ( typeof module
=== 'string' ) {
293 // Undefined modules have no dependencies
294 if ( !( module
in registry
) ) {
298 recurse( module
, resolved
, [] );
301 throw new Error( 'Invalid module argument: ' + module
);
305 * Narrows a list of module names down to those matching a specific state. Possible states are 'undefined',
306 * 'registered', 'loading', 'loaded', or 'ready'
308 * @param mixed string or array of strings of module states to filter by
309 * @param array list of module names to filter (optional, all modules will be used by default)
310 * @return array list of filtered module names
312 function filter( states
, modules
) {
313 // Allow states to be given as a string
314 if ( typeof states
=== 'string' ) {
317 // If called without a list of modules, build and use a list of all modules
319 if ( typeof modules
=== 'undefined' ) {
321 for ( module
in registry
) {
322 modules
[modules
.length
] = module
;
325 // Build a list of modules which are in one of the specified states
326 for ( var s
= 0; s
< states
.length
; s
++ ) {
327 for ( var m
= 0; m
< modules
.length
; m
++ ) {
329 ( states
[s
] == 'undefined' && typeof registry
[modules
[m
]] === 'undefined' ) ||
330 ( typeof registry
[modules
[m
]] === 'object' && registry
[modules
[m
]].state
=== states
[s
] )
332 list
[list
.length
] = modules
[m
];
340 * Executes a loaded module, making it ready to use
342 * @param string module name to execute
344 function execute( module
) {
345 if ( typeof registry
[module
] === 'undefined' ) {
346 throw new Error( 'Module has not been registered yet: ' + module
);
347 } else if ( registry
[module
].state
=== 'registered' ) {
348 throw new Error( 'Module has not been requested from the server yet: ' + module
);
349 } else if ( registry
[module
].state
=== 'loading' ) {
350 throw new Error( 'Module has not completed loading yet: ' + module
);
351 } else if ( registry
[module
].state
=== 'ready' ) {
352 throw new Error( 'Module has already been loaded: ' + module
);
354 // Add style sheet to document
355 if ( typeof registry
[module
].style
=== 'string' && registry
[module
].style
.length
) {
356 $( 'head' ).append( '<style type="text/css">' + registry
[module
].style
+ '</style>' );
357 } else if ( typeof registry
[module
].style
=== 'object' && !( registry
[module
].style
instanceof Array
) ) {
358 for ( var media
in registry
[module
].style
) {
360 '<style type="text/css" media="' + media
+ '">' + registry
[module
].style
[media
] + '</style>'
364 // Add localizations to message system
365 if ( typeof registry
[module
].messages
=== 'object' ) {
366 mediaWiki
.msg
.set( registry
[module
].messages
);
370 registry
[module
].script();
371 registry
[module
].state
= 'ready';
372 // Run jobs who's dependencies have just been met
373 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
374 if ( filter( 'ready', jobs
[j
].dependencies
).compare( jobs
[j
].dependencies
) ) {
375 if ( typeof jobs
[j
].ready
=== 'function' ) {
382 // Execute modules who's dependencies have just been met
383 for ( r
in registry
) {
384 if ( registry
[r
].state
== 'loaded' ) {
385 if ( filter( ['ready'], registry
[r
].dependencies
).compare( registry
[r
].dependencies
) ) {
391 mediaWiki
.log( 'Exception thrown by ' + module
+ ': ' + e
.message
);
393 registry
[module
].state
= 'error';
394 // Run error callbacks of jobs affected by this condition
395 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
396 if ( jobs
[j
].dependencies
.indexOf( module
) !== -1 ) {
397 if ( typeof jobs
[j
].error
=== 'function' ) {
408 * Adds a dependencies to the queue with optional callbacks to be run when the dependencies are ready or fail
410 * @param mixed string moulde name or array of string module names
411 * @param function ready callback to execute when all dependencies are ready
412 * @param function error callback to execute when any dependency fails
414 function request( dependencies
, ready
, error
) {
415 // Allow calling by single module name
416 if ( typeof dependencies
=== 'string' ) {
417 dependencies
= [dependencies
];
418 if ( dependencies
[0] in registry
) {
419 for ( var n
= 0; n
< registry
[dependencies
[0]].dependencies
.length
; n
++ ) {
420 dependencies
[dependencies
.length
] = registry
[dependencies
[0]].dependencies
[n
];
424 // Add ready and error callbacks if they were given
425 if ( arguments
.length
> 1 ) {
426 jobs
[jobs
.length
] = {
427 'dependencies': filter( ['undefined', 'registered', 'loading', 'loaded'], dependencies
),
432 // Queue up any dependencies that are undefined or registered
433 dependencies
= filter( ['undefined', 'registered'], dependencies
);
434 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
435 if ( queue
.indexOf( dependencies
[n
] ) === -1 ) {
436 queue
[queue
.length
] = dependencies
[n
];
443 function sortQuery(o
) {
444 var sorted
= {}, key
, a
= [];
446 if ( o
.hasOwnProperty( key
) ) {
451 for ( key
= 0; key
< a
.length
; key
++ ) {
452 sorted
[a
[key
]] = o
[a
[key
]];
460 * Requests dependencies from server, loading and executing when things when ready.
462 this.work = function() {
463 // Appends a list of modules to the batch
464 for ( var q
= 0; q
< queue
.length
; q
++ ) {
465 // Only request modules which are undefined or registered
466 if ( !( queue
[q
] in registry
) || registry
[queue
[q
]].state
== 'registered' ) {
467 // Prevent duplicate entries
468 if ( batch
.indexOf( queue
[q
] ) === -1 ) {
469 batch
[batch
.length
] = queue
[q
];
470 // Mark registered modules as loading
471 if ( queue
[q
] in registry
) {
472 registry
[queue
[q
]].state
= 'loading';
477 // Clean up the queue
479 // After document ready, handle the batch
480 if ( !suspended
&& batch
.length
) {
481 // Always order modules alphabetically to help reduce cache misses for otherwise identical content
483 // Build a list of request parameters
485 'skin': mediaWiki
.config
.get( 'skin' ),
486 'lang': mediaWiki
.config
.get( 'wgUserLanguage' ),
487 'debug': mediaWiki
.config
.get( 'debug' )
489 // Extend request parameters with a list of modules in the batch
491 if ( base
.debug
== '1' ) {
492 for ( var b
= 0; b
< batch
.length
; b
++ ) {
493 requests
[requests
.length
] = $.extend(
494 { 'modules': batch
[b
], 'version': registry
[batch
[b
]].version
}, base
500 for ( var b
= 0; b
< batch
.length
; b
++ ) {
501 var group
= registry
[batch
[b
]].group
;
502 if ( !( group
in groups
) ) {
505 groups
[group
][groups
[group
].length
] = batch
[b
];
507 for ( var group
in groups
) {
508 // Calculate the highest timestamp
510 for ( var g
= 0; g
< groups
[group
].length
; g
++ ) {
511 if ( registry
[groups
[group
][g
]].version
> version
) {
512 version
= registry
[groups
[group
][g
]].version
;
515 requests
[requests
.length
] = $.extend(
516 { 'modules': groups
[group
].join( '|' ), 'version': formatVersionNumber( version
) }, base
520 // Clear the batch - this MUST happen before we append the script element to the body or it's
521 // possible that the script will be locally cached, instantly load, and work the batch again,
522 // all before we've cleared it causing each request to include modules which are already loaded
524 // Asynchronously append a script tag to the end of the body
527 for ( var r
= 0; r
< requests
.length
; r
++ ) {
528 requests
[r
] = sortQuery( requests
[r
] );
529 // Build out the HTML
530 var src
= mediaWiki
.config
.get( 'wgLoadScript' ) + '?' + $.param( requests
[r
] );
531 html
+= '<script type="text/javascript" src="' + src
+ '"></script>';
535 // Load asynchronously after doumument ready
537 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
539 document
.write( request() );
545 * Registers a module, letting the system know about it and it's dependencies. loader.js files contain calls
548 this.register = function( module
, version
, dependencies
, group
) {
549 // Allow multiple registration
550 if ( typeof module
=== 'object' ) {
551 for ( var m
= 0; m
< module
.length
; m
++ ) {
552 if ( typeof module
[m
] === 'string' ) {
553 that
.register( module
[m
] );
554 } else if ( typeof module
[m
] === 'object' ) {
555 that
.register
.apply( that
, module
[m
] );
561 if ( typeof module
!== 'string' ) {
562 throw new Error( 'module must be a string, not a ' + typeof module
);
564 if ( typeof registry
[module
] !== 'undefined' ) {
565 throw new Error( 'module already implemeneted: ' + module
);
567 // List the module as registered
569 'state': 'registered',
570 'group': typeof group
=== 'string' ? group
: null,
572 'version': typeof version
!== 'undefined' ? parseInt( version
) : 0
574 if ( typeof dependencies
=== 'string' ) {
575 // Allow dependencies to be given as a single module name
576 registry
[module
].dependencies
= [dependencies
];
577 } else if ( typeof dependencies
=== 'object' || typeof dependencies
=== 'function' ) {
578 // Allow dependencies to be given as an array of module names or a function which returns an array
579 registry
[module
].dependencies
= dependencies
;
584 * Implements a module, giving the system a course of action to take upon loading. Results of a request for
585 * one or more modules contain calls to this function.
587 this.implement = function( module
, script
, style
, localization
) {
588 // Automaically register module
589 if ( typeof registry
[module
] === 'undefined' ) {
590 that
.register( module
);
593 if ( typeof script
!== 'function' ) {
594 throw new Error( 'script must be a function, not a ' + typeof script
);
596 if ( typeof style
!== 'undefined' && typeof style
!== 'string' && typeof style
!== 'object' ) {
597 throw new Error( 'style must be a string or object, not a ' + typeof style
);
599 if ( typeof localization
!== 'undefined' && typeof localization
!== 'object' ) {
600 throw new Error( 'localization must be an object, not a ' + typeof localization
);
602 if ( typeof registry
[module
] !== 'undefined' && typeof registry
[module
].script
!== 'undefined' ) {
603 throw new Error( 'module already implemeneted: ' + module
);
605 // Mark module as loaded
606 registry
[module
].state
= 'loaded';
608 registry
[module
].script
= script
;
609 if ( typeof style
=== 'string' || typeof style
=== 'object' && !( style
instanceof Array
) ) {
610 registry
[module
].style
= style
;
612 if ( typeof localization
=== 'object' ) {
613 registry
[module
].messages
= localization
;
615 // Execute or queue callback
616 if ( filter( ['ready'], registry
[module
].dependencies
).compare( registry
[module
].dependencies
) ) {
624 * Executes a function as soon as one or more required modules are ready
626 * @param mixed string or array of strings of modules names the callback dependencies to be ready before
628 * @param function callback to execute when all dependencies are ready (optional)
629 * @param function callback to execute when if dependencies have a errors (optional)
631 this.using = function( dependencies
, ready
, error
) {
633 if ( typeof dependencies
!== 'object' && typeof dependencies
!== 'string' ) {
634 throw new Error( 'dependencies must be a string or an array, not a ' + typeof dependencies
)
636 // Allow calling with a single dependency as a string
637 if ( typeof dependencies
=== 'string' ) {
638 dependencies
= [dependencies
];
640 // Resolve entire dependency map
641 dependencies
= resolve( dependencies
);
642 // If all dependencies are met, execute ready immediately
643 if ( filter( ['ready'], dependencies
).compare( dependencies
) ) {
644 if ( typeof ready
=== 'function' ) {
648 // If any dependencies have errors execute error immediately
649 else if ( filter( ['error'], dependencies
).length
) {
650 if ( typeof error
=== 'function' ) {
654 // Since some dependencies are not yet ready, queue up a request
656 request( dependencies
, ready
, error
);
661 * Loads an external script or one or more modules for future use
663 * @param {mixed} modules either the name of a module, array of modules, or a URL of an external script or style
664 * @param {string} type mime-type to use if calling with a URL of an external script or style; acceptable values
665 * are "text/css" and "text/javascript"; if no type is provided, text/javascript is assumed
667 this.load = function( modules
, type
) {
669 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
670 throw new Error( 'dependencies must be a string or an array, not a ' + typeof dependencies
)
672 // Allow calling with an external script or single dependency as a string
673 if ( typeof modules
=== 'string' ) {
674 // Support adding arbitrary external scripts
675 if ( modules
.substr( 0, 7 ) == 'http://' || modules
.substr( 0, 8 ) == 'https://' ) {
676 if ( type
=== 'text/css' ) {
677 setTimeout( function() {
678 $( 'head' ).append( '<link rel="stylesheet" type="text/css" />' ).attr( 'href', modules
);
681 } else if ( type
=== 'text/javascript' || typeof type
=== 'undefined' ) {
682 setTimeout( function() {
683 $( 'body' ).append( '<script type="text/javascript"></script>' ).attr( 'src', modules
)
690 // Called with single module
693 // Resolve entire dependency map
694 modules
= resolve( modules
);
695 // If all modules are ready, nothing dependency be done
696 if ( filter( ['ready'], modules
).compare( modules
) ) {
699 // If any modules have errors return false
700 else if ( filter( ['error'], modules
).length
) {
703 // Since some modules are not yet ready, queue up a request
711 * Flushes the request queue and begin executing load requests on demand
713 this.go = function() {
719 * Changes the state of a module
721 * @param mixed module string module name or object of module name/state pairs
722 * @param string state string state name
724 this.state = function( module
, state
) {
725 if ( typeof module
=== 'object' ) {
726 for ( var m
in module
) {
727 that
.state( m
, module
[m
] );
731 if ( !( module
in registry
) ) {
732 that
.register( module
);
734 registry
[module
].state
= state
;
738 * Gets the version of a module
740 * @param string module name of module to get version for
742 this.version = function( module
) {
743 if ( module
in registry
&& 'version' in registry
[module
] ) {
744 return formatVersionNumber( registry
[module
].version
);
749 /* Cache document ready status */
751 $(document
).ready( function() { ready
= true; } );
754 /* Extension points */
761 /* Auto-register from pre-loaded startup scripts */
763 if ( typeof startUp
=== 'function' ) {
768 // Alias $j to jQuery for backwards compatibility