Removed debugging code accidentally left in.
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.js
1 /*
2 * JavaScript backwards-compatibility and support
3 */
4
5 // Implementation of string trimming functionality introduced natively in JavaScript 1.8.1
6 if ( typeof String.prototype.trim === 'undefined' ) {
7 // Add removing trailing and leading whitespace functionality cross-browser
8 // See also: http://blog.stevenlevithan.com/archives/faster-trim-javascript
9 String.prototype.trim = function() {
10 return this.replace( /^\s+|\s+$/g, '' );
11 };
12 }
13 if ( typeof String.prototype.trimLeft === 'undefined' ) {
14 String.prototype.trimLeft = function() {
15 return this.replace( /^\s\s*/, "" );
16 };
17 }
18 if ( typeof String.prototype.trimRight === 'undefined' ) {
19 String.prototype.trimRight = function() {
20 return this.replace(/\s\s*$/, "");
21 };
22 }
23
24 /*
25 * Prototype enhancements
26 */
27
28 // Capitalize the first character of the given string
29 if ( typeof String.prototype.ucFirst === 'undefined' ) {
30 String.prototype.ucFirst = function() {
31 return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
32 };
33 }
34
35 // Escape all RegExp special characters such that the result can be safely used
36 // in a RegExp as a literal.
37 if ( typeof String.prototype.escapeRE === 'undefined' ) {
38 String.prototype.escapeRE = function() {
39 return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1");
40 };
41 }
42
43 /*
44 * Core MediaWiki JavaScript Library
45 */
46
47 // Attach to window
48 window.mediaWiki = new ( function( $ ) {
49
50 /* Constants */
51
52 // This will not change until we are 100% ready to turn off legacy globals
53 var LEGACY_GLOBALS = true;
54
55 /* Private Members */
56
57 // List of messages that have been requested to be loaded
58 var messageQueue = {};
59
60 /* Prototypes */
61
62 /**
63 * An object which allows single and multiple get/set/exists functionality
64 * on a list of key / value pairs.
65 *
66 * @param {boolean} global Whether to get/set/exists values on the window
67 * object or a private object
68 */
69 function Map( global ) {
70 this.values = ( global === true ) ? window : {};
71 };
72
73 /**
74 * Gets the value of a key, or a list of key/value pairs for an array of keys.
75 *
76 * If called with no arguments, all values will be returned.
77 *
78 * @param {mixed} selection Key or array of keys to get values for
79 * @param {mixed} fallback Value to use in case key(s) do not exist (optional)
80 */
81 Map.prototype.get = function( selection, fallback ) {
82 if ( typeof selection === 'object' ) {
83 selection = $.makeArray( selection );
84 var results = {};
85 for ( var i = 0; i < selection.length; i++ ) {
86 results[selection[i]] = this.get( selection[i], fallback );
87 }
88 return results;
89 } else if ( typeof selection === 'string' ) {
90 if ( typeof this.values[selection] === 'undefined' ) {
91 if ( typeof fallback !== 'undefined' ) {
92 return fallback;
93 }
94 return null;
95 }
96 return this.values[selection];
97 }
98 return this.values;
99 };
100
101 /**
102 * Sets one or multiple key/value pairs.
103 *
104 * @param {mixed} selection Key or object of key/value pairs to set
105 * @param {mixed} value Value to set (optional, only in use when key is a string)
106 */
107 Map.prototype.set = function( selection, value ) {
108 if ( typeof selection === 'object' ) {
109 for ( var s in selection ) {
110 this.values[s] = selection[s];
111 }
112 } else if ( typeof selection === 'string' && typeof value !== 'undefined' ) {
113 this.values[selection] = value;
114 }
115 };
116
117 /**
118 * Checks if one or multiple keys exist.
119 *
120 * @param {mixed} key Key or array of keys to check
121 * @return {boolean} Existence of key(s)
122 */
123 Map.prototype.exists = function( selection ) {
124 if ( typeof keys === 'object' ) {
125 for ( var s = 0; s < selection.length; s++ ) {
126 if ( !( selection[s] in this.values ) ) {
127 return false;
128 }
129 }
130 return true;
131 } else {
132 return selection in this.values;
133 }
134 };
135
136 /**
137 * Message object, similar to Message in PHP
138 */
139 function Message( map, key, parameters ) {
140 this.format = 'parse';
141 this.map = map;
142 this.key = key;
143 this.parameters = typeof parameters === 'undefined' ? [] : $.makeArray( parameters );
144 };
145
146 /**
147 * Appends parameters for replacement
148 *
149 * @param {mixed} args First in a list of variadic arguments to append as message parameters
150 */
151 Message.prototype.params = function( parameters ) {
152 for ( var i = 0; i < parameters.length; i++ ) {
153 this.parameters[this.parameters.length] = parameters[i];
154 }
155 return this;
156 };
157
158 /**
159 * Converts message object to it's string form based on the state of format
160 *
161 * @return {string} String form of message
162 */
163 Message.prototype.toString = function() {
164 if ( !this.map.exists( this.key ) ) {
165 // Return <key> if key does not exist
166 return '<' + this.key + '>';
167 }
168 var text = this.map.get( this.key );
169 var parameters = this.parameters;
170 text = text.replace( /\$(\d+)/g, function( string, match ) {
171 var index = parseInt( match, 10 ) - 1;
172 return index in parameters ? parameters[index] : '$' + match;
173 } );
174 /* This should be fixed up when we have a parser
175 if ( this.format === 'parse' && 'language' in mediaWiki ) {
176 text = mediaWiki.language.parse( text );
177 }
178 */
179 return text;
180 };
181
182 /**
183 * Changes format to parse and converts message to string
184 *
185 * @return {string} String form of parsed message
186 */
187 Message.prototype.parse = function() {
188 this.format = 'parse';
189 return this.toString();
190 };
191
192 /**
193 * Changes format to plain and converts message to string
194 *
195 * @return {string} String form of plain message
196 */
197 Message.prototype.plain = function() {
198 this.format = 'plain';
199 return this.toString();
200 };
201
202 /**
203 * Checks if message exists
204 *
205 * @return {string} String form of parsed message
206 */
207 Message.prototype.exists = function() {
208 return this.map.exists( this.key );
209 };
210
211 /**
212 * User object
213 */
214 function User() {
215 this.options = new Map();
216 }
217
218 /* Public Members */
219
220 /*
221 * Dummy function which in debug mode can be replaced with a function that
222 * does something clever
223 */
224 this.log = function() { };
225
226 /*
227 * List of configuration values
228 *
229 * In legacy mode the values this object wraps will be in the global space
230 */
231 this.config = new Map( LEGACY_GLOBALS );
232
233 /*
234 * Information about the current user
235 */
236 this.user = new User();
237
238 /*
239 * Localization system
240 */
241 this.messages = new Map();
242
243 /* Public Methods */
244
245 /**
246 * Gets a message object, similar to wfMessage()
247 *
248 * @param {string} key Key of message to get
249 * @param {mixed} params First argument in a list of variadic arguments, each a parameter for $
250 * replacement
251 */
252 this.message = function( key, parameters ) {
253 // Support variadic arguments
254 if ( typeof parameters !== 'undefined' ) {
255 parameters = $.makeArray( arguments );
256 parameters.shift();
257 } else {
258 parameters = [];
259 }
260 return new Message( mediaWiki.messages, key, parameters );
261 };
262
263 /**
264 * Gets a message string, similar to wfMsg()
265 *
266 * @param {string} key Key of message to get
267 * @param {mixed} params First argument in a list of variadic arguments, each a parameter for $
268 * replacement
269 */
270 this.msg = function( key, parameters ) {
271 return mediaWiki.message.apply( mediaWiki.message, arguments ).toString();
272 };
273
274 /**
275 * Client-side module loader which integrates with the MediaWiki ResourceLoader
276 */
277 this.loader = new ( function() {
278
279 /* Private Members */
280
281 /**
282 * Mapping of registered modules
283 *
284 * The jquery module is pre-registered, because it must have already
285 * been provided for this object to have been built, and in debug mode
286 * jquery would have been provided through a unique loader request,
287 * making it impossible to hold back registration of jquery until after
288 * mediawiki.
289 *
290 * Format:
291 * {
292 * 'moduleName': {
293 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
294 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
295 * 'script': function() {},
296 * 'style': 'css code string',
297 * 'messages': { 'key': 'value' },
298 * 'version': ############## (unix timestamp)
299 * }
300 * }
301 */
302 var registry = {};
303 // List of modules which will be loaded as when ready
304 var batch = [];
305 // List of modules to be loaded
306 var queue = [];
307 // List of callback functions waiting for modules to be ready to be called
308 var jobs = [];
309 // Flag indicating that requests should be suspended
310 var suspended = true;
311 // Flag inidicating that document ready has occured
312 var ready = false;
313
314 /* Private Methods */
315
316 function compare( a, b ) {
317 if ( a.length != b.length ) {
318 return false;
319 }
320 for ( var i = 0; i < b.length; i++ ) {
321 if ( $.isArray( a[i] ) ) {
322 if ( !compare( a[i], b[i] ) ) {
323 return false;
324 }
325 }
326 if ( a[i] !== b[i] ) {
327 return false;
328 }
329 }
330 return true;
331 };
332
333 /**
334 * Generates an ISO8601 "basic" string from a UNIX timestamp
335 */
336 function formatVersionNumber( timestamp ) {
337 function pad( a, b, c ) {
338 return [a < 10 ? '0' + a : a, b < 10 ? '0' + b : b, c < 10 ? '0' + c : c].join( '' );
339 }
340 var d = new Date()
341 d.setTime( timestamp * 1000 );
342 return [
343 pad( d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate() ), 'T',
344 pad( d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds() ), 'Z'
345 ].join( '' );
346 }
347
348 /**
349 * Recursively resolves dependencies and detects circular references
350 */
351 function recurse( module, resolved, unresolved ) {
352 if ( typeof registry[module] === 'undefined' ) {
353 throw new Error( 'Unknown dependency: ' + module );
354 }
355 // Resolves dynamic loader function and replaces it with its own results
356 if ( typeof registry[module].dependencies === 'function' ) {
357 registry[module].dependencies = registry[module].dependencies();
358 // Ensures the module's dependencies are always in an array
359 if ( typeof registry[module].dependencies !== 'object' ) {
360 registry[module].dependencies = [registry[module].dependencies];
361 }
362 }
363 // Tracks down dependencies
364 for ( var n = 0; n < registry[module].dependencies.length; n++ ) {
365 if ( $.inArray( registry[module].dependencies[n], resolved ) === -1 ) {
366 if ( $.inArray( registry[module].dependencies[n], unresolved ) !== -1 ) {
367 throw new Error(
368 'Circular reference detected: ' + module +
369 ' -> ' + registry[module].dependencies[n]
370 );
371 }
372 recurse( registry[module].dependencies[n], resolved, unresolved );
373 }
374 }
375 resolved[resolved.length] = module;
376 unresolved.splice( $.inArray( module, unresolved ), 1 );
377 }
378
379 /**
380 * Gets a list of module names that a module depends on in their proper dependency order
381 *
382 * @param mixed string module name or array of string module names
383 * @return list of dependencies
384 * @throws Error if circular reference is detected
385 */
386 function resolve( module, resolved, unresolved ) {
387 // Allow calling with an array of module names
388 if ( typeof module === 'object' ) {
389 var modules = [];
390 for ( var m = 0; m < module.length; m++ ) {
391 var dependencies = resolve( module[m] );
392 for ( var n = 0; n < dependencies.length; n++ ) {
393 modules[modules.length] = dependencies[n];
394 }
395 }
396 return modules;
397 } else if ( typeof module === 'string' ) {
398 // Undefined modules have no dependencies
399 if ( !( module in registry ) ) {
400 return [];
401 }
402 var resolved = [];
403 recurse( module, resolved, [] );
404 return resolved;
405 }
406 throw new Error( 'Invalid module argument: ' + module );
407 };
408
409 /**
410 * Narrows a list of module names down to those matching a specific
411 * state. Possible states are 'undefined', 'registered', 'loading',
412 * 'loaded', or 'ready'
413 *
414 * @param mixed string or array of strings of module states to filter by
415 * @param array list of module names to filter (optional, all modules
416 * will be used by default)
417 * @return array list of filtered module names
418 */
419 function filter( states, modules ) {
420 // Allow states to be given as a string
421 if ( typeof states === 'string' ) {
422 states = [states];
423 }
424 // If called without a list of modules, build and use a list of all modules
425 var list = [];
426 if ( typeof modules === 'undefined' ) {
427 modules = [];
428 for ( module in registry ) {
429 modules[modules.length] = module;
430 }
431 }
432 // Build a list of modules which are in one of the specified states
433 for ( var s = 0; s < states.length; s++ ) {
434 for ( var m = 0; m < modules.length; m++ ) {
435 if ( typeof registry[modules[m]] === 'undefined' ) {
436 // Module does not exist
437 if ( states[s] == 'undefined' ) {
438 // OK, undefined
439 list[list.length] = modules[m];
440 }
441 } else {
442 // Module exists, check state
443 if ( registry[modules[m]].state === states[s] ) {
444 // OK, correct state
445 list[list.length] = modules[m];
446 }
447 }
448 }
449 }
450 return list;
451 }
452
453 /**
454 * Executes a loaded module, making it ready to use
455 *
456 * @param string module name to execute
457 */
458 function execute( module ) {
459 if ( typeof registry[module] === 'undefined' ) {
460 throw new Error( 'Module has not been registered yet: ' + module );
461 } else if ( registry[module].state === 'registered' ) {
462 throw new Error( 'Module has not been requested from the server yet: ' + module );
463 } else if ( registry[module].state === 'loading' ) {
464 throw new Error( 'Module has not completed loading yet: ' + module );
465 } else if ( registry[module].state === 'ready' ) {
466 throw new Error( 'Module has already been loaded: ' + module );
467 }
468 // Add style sheet to document
469 if ( typeof registry[module].style === 'string' && registry[module].style.length ) {
470 $( 'head' )
471 .append( '<style type="text/css">' + registry[module].style + '</style>' );
472 } else if ( typeof registry[module].style === 'object'
473 && !( registry[module].style instanceof Array ) )
474 {
475 for ( var media in registry[module].style ) {
476 $( 'head' ).append(
477 '<style type="text/css" media="' + media + '">' +
478 registry[module].style[media] +
479 '</style>'
480 );
481 }
482 }
483 // Add localizations to message system
484 if ( typeof registry[module].messages === 'object' ) {
485 mediaWiki.messages.set( registry[module].messages );
486 }
487 // Execute script
488 try {
489 registry[module].script();
490 registry[module].state = 'ready';
491 // Run jobs who's dependencies have just been met
492 for ( var j = 0; j < jobs.length; j++ ) {
493 if ( compare(
494 filter( 'ready', jobs[j].dependencies ),
495 jobs[j].dependencies ) )
496 {
497 if ( typeof jobs[j].ready === 'function' ) {
498 jobs[j].ready();
499 }
500 jobs.splice( j, 1 );
501 j--;
502 }
503 }
504 // Execute modules who's dependencies have just been met
505 for ( r in registry ) {
506 if ( registry[r].state == 'loaded' ) {
507 if ( compare(
508 filter( ['ready'], registry[r].dependencies ),
509 registry[r].dependencies ) )
510 {
511 execute( r );
512 }
513 }
514 }
515 } catch ( e ) {
516 mediaWiki.log( 'Exception thrown by ' + module + ': ' + e.message );
517 mediaWiki.log( e );
518 registry[module].state = 'error';
519 // Run error callbacks of jobs affected by this condition
520 for ( var j = 0; j < jobs.length; j++ ) {
521 if ( $.inArray( module, jobs[j].dependencies ) !== -1 ) {
522 if ( typeof jobs[j].error === 'function' ) {
523 jobs[j].error();
524 }
525 jobs.splice( j, 1 );
526 j--;
527 }
528 }
529 }
530 }
531
532 /**
533 * Adds a dependencies to the queue with optional callbacks to be run
534 * when the dependencies are ready or fail
535 *
536 * @param mixed string moulde name or array of string module names
537 * @param function ready callback to execute when all dependencies are ready
538 * @param function error callback to execute when any dependency fails
539 */
540 function request( dependencies, ready, error ) {
541 // Allow calling by single module name
542 if ( typeof dependencies === 'string' ) {
543 dependencies = [dependencies];
544 if ( dependencies[0] in registry ) {
545 for ( var n = 0; n < registry[dependencies[0]].dependencies.length; n++ ) {
546 dependencies[dependencies.length] =
547 registry[dependencies[0]].dependencies[n];
548 }
549 }
550 }
551 // Add ready and error callbacks if they were given
552 if ( arguments.length > 1 ) {
553 jobs[jobs.length] = {
554 'dependencies': filter(
555 ['undefined', 'registered', 'loading', 'loaded'],
556 dependencies ),
557 'ready': ready,
558 'error': error
559 };
560 }
561 // Queue up any dependencies that are undefined or registered
562 dependencies = filter( ['undefined', 'registered'], dependencies );
563 for ( var n = 0; n < dependencies.length; n++ ) {
564 if ( $.inArray( dependencies[n], queue ) === -1 ) {
565 queue[queue.length] = dependencies[n];
566 }
567 }
568 // Work the queue
569 mediaWiki.loader.work();
570 }
571
572 function sortQuery(o) {
573 var sorted = {}, key, a = [];
574 for ( key in o ) {
575 if ( o.hasOwnProperty( key ) ) {
576 a.push( key );
577 }
578 }
579 a.sort();
580 for ( key = 0; key < a.length; key++ ) {
581 sorted[a[key]] = o[a[key]];
582 }
583 return sorted;
584 }
585
586 /* Public Methods */
587
588 /**
589 * Requests dependencies from server, loading and executing when things when ready.
590 */
591 this.work = function() {
592 // Appends a list of modules to the batch
593 for ( var q = 0; q < queue.length; q++ ) {
594 // Only request modules which are undefined or registered
595 if ( !( queue[q] in registry ) || registry[queue[q]].state == 'registered' ) {
596 // Prevent duplicate entries
597 if ( $.inArray( queue[q], batch ) === -1 ) {
598 batch[batch.length] = queue[q];
599 // Mark registered modules as loading
600 if ( queue[q] in registry ) {
601 registry[queue[q]].state = 'loading';
602 }
603 }
604 }
605 }
606 // Clean up the queue
607 queue = [];
608 // After document ready, handle the batch
609 if ( !suspended && batch.length ) {
610 // Always order modules alphabetically to help reduce cache
611 // misses for otherwise identical content
612 batch.sort();
613 // Build a list of request parameters
614 var base = {
615 'skin': mediaWiki.config.get( 'skin' ),
616 'lang': mediaWiki.config.get( 'wgUserLanguage' ),
617 'debug': mediaWiki.config.get( 'debug' )
618 };
619 // Extend request parameters with a list of modules in the batch
620 var requests = [];
621 // Split into groups
622 var groups = {};
623 for ( var b = 0; b < batch.length; b++ ) {
624 var group = registry[batch[b]].group;
625 if ( !( group in groups ) ) {
626 groups[group] = [];
627 }
628 groups[group][groups[group].length] = batch[b];
629 }
630 for ( var group in groups ) {
631 // Calculate the highest timestamp
632 var version = 0;
633 for ( var g = 0; g < groups[group].length; g++ ) {
634 if ( registry[groups[group][g]].version > version ) {
635 version = registry[groups[group][g]].version;
636 }
637 }
638 requests[requests.length] = $.extend(
639 { 'modules': groups[group].join( '|' ), 'version': formatVersionNumber( version ) }, base
640 );
641 }
642 // Clear the batch - this MUST happen before we append the
643 // script element to the body or it's possible that the script
644 // will be locally cached, instantly load, and work the batch
645 // again, all before we've cleared it causing each request to
646 // include modules which are already loaded
647 batch = [];
648 // Asynchronously append a script tag to the end of the body
649 function request() {
650 var html = '';
651 for ( var r = 0; r < requests.length; r++ ) {
652 requests[r] = sortQuery( requests[r] );
653 // Build out the HTML
654 var src = mediaWiki.config.get( 'wgLoadScript' ) + '?' + $.param( requests[r] );
655 html += '<script type="text/javascript" src="' + src + '"></script>';
656 }
657 return html;
658 }
659 // Load asynchronously after doumument ready
660 if ( ready ) {
661 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
662 } else {
663 document.write( request() );
664 }
665 }
666 };
667
668 /**
669 * Registers a module, letting the system know about it and its
670 * dependencies. loader.js files contain calls to this function.
671 */
672 this.register = function( module, version, dependencies, group ) {
673 // Allow multiple registration
674 if ( typeof module === 'object' ) {
675 for ( var m = 0; m < module.length; m++ ) {
676 if ( typeof module[m] === 'string' ) {
677 mediaWiki.loader.register( module[m] );
678 } else if ( typeof module[m] === 'object' ) {
679 mediaWiki.loader.register.apply( mediaWiki.loader, module[m] );
680 }
681 }
682 return;
683 }
684 // Validate input
685 if ( typeof module !== 'string' ) {
686 throw new Error( 'module must be a string, not a ' + typeof module );
687 }
688 if ( typeof registry[module] !== 'undefined' ) {
689 throw new Error( 'module already implemeneted: ' + module );
690 }
691 // List the module as registered
692 registry[module] = {
693 'state': 'registered',
694 'group': typeof group === 'string' ? group : null,
695 'dependencies': [],
696 'version': typeof version !== 'undefined' ? parseInt( version ) : 0
697 };
698 if ( typeof dependencies === 'string' ) {
699 // Allow dependencies to be given as a single module name
700 registry[module].dependencies = [dependencies];
701 } else if ( typeof dependencies === 'object' || typeof dependencies === 'function' ) {
702 // Allow dependencies to be given as an array of module names
703 // or a function which returns an array
704 registry[module].dependencies = dependencies;
705 }
706 };
707
708 /**
709 * Implements a module, giving the system a course of action to take
710 * upon loading. Results of a request for one or more modules contain
711 * calls to this function.
712 */
713 this.implement = function( module, script, style, localization ) {
714 // Automaically register module
715 if ( typeof registry[module] === 'undefined' ) {
716 mediaWiki.loader.register( module );
717 }
718 // Validate input
719 if ( typeof script !== 'function' ) {
720 throw new Error( 'script must be a function, not a ' + typeof script );
721 }
722 if ( typeof style !== 'undefined'
723 && typeof style !== 'string'
724 && typeof style !== 'object' )
725 {
726 throw new Error( 'style must be a string or object, not a ' + typeof style );
727 }
728 if ( typeof localization !== 'undefined'
729 && typeof localization !== 'object' )
730 {
731 throw new Error( 'localization must be an object, not a ' + typeof localization );
732 }
733 if ( typeof registry[module] !== 'undefined'
734 && typeof registry[module].script !== 'undefined' )
735 {
736 throw new Error( 'module already implemeneted: ' + module );
737 }
738 // Mark module as loaded
739 registry[module].state = 'loaded';
740 // Attach components
741 registry[module].script = script;
742 if ( typeof style === 'string'
743 || typeof style === 'object' && !( style instanceof Array ) )
744 {
745 registry[module].style = style;
746 }
747 if ( typeof localization === 'object' ) {
748 registry[module].messages = localization;
749 }
750 // Execute or queue callback
751 if ( compare(
752 filter( ['ready'], registry[module].dependencies ),
753 registry[module].dependencies ) )
754 {
755 execute( module );
756 } else {
757 request( module );
758 }
759 };
760
761 /**
762 * Executes a function as soon as one or more required modules are ready
763 *
764 * @param mixed string or array of strings of modules names the callback
765 * dependencies to be ready before
766 * executing
767 * @param function callback to execute when all dependencies are ready (optional)
768 * @param function callback to execute when if dependencies have a errors (optional)
769 */
770 this.using = function( dependencies, ready, error ) {
771 // Validate input
772 if ( typeof dependencies !== 'object' && typeof dependencies !== 'string' ) {
773 throw new Error( 'dependencies must be a string or an array, not a ' +
774 typeof dependencies )
775 }
776 // Allow calling with a single dependency as a string
777 if ( typeof dependencies === 'string' ) {
778 dependencies = [dependencies];
779 }
780 // Resolve entire dependency map
781 dependencies = resolve( dependencies );
782 // If all dependencies are met, execute ready immediately
783 if ( compare( filter( ['ready'], dependencies ), dependencies ) ) {
784 if ( typeof ready === 'function' ) {
785 ready();
786 }
787 }
788 // If any dependencies have errors execute error immediately
789 else if ( filter( ['error'], dependencies ).length ) {
790 if ( typeof error === 'function' ) {
791 error();
792 }
793 }
794 // Since some dependencies are not yet ready, queue up a request
795 else {
796 request( dependencies, ready, error );
797 }
798 };
799
800 /**
801 * Loads an external script or one or more modules for future use
802 *
803 * @param {mixed} modules either the name of a module, array of modules,
804 * or a URL of an external script or style
805 * @param {string} type mime-type to use if calling with a URL of an
806 * external script or style; acceptable values are "text/css" and
807 * "text/javascript"; if no type is provided, text/javascript is
808 * assumed
809 */
810 this.load = function( modules, type ) {
811 // Validate input
812 if ( typeof modules !== 'object' && typeof modules !== 'string' ) {
813 throw new Error( 'dependencies must be a string or an array, not a ' +
814 typeof dependencies )
815 }
816 // Allow calling with an external script or single dependency as a string
817 if ( typeof modules === 'string' ) {
818 // Support adding arbitrary external scripts
819 if ( modules.substr( 0, 7 ) == 'http://'
820 || modules.substr( 0, 8 ) == 'https://' )
821 {
822 if ( type === 'text/css' ) {
823 $( 'head' )
824 .append( $( '<link rel="stylesheet" type="text/css" />' )
825 .attr( 'href', modules ) );
826 return true;
827 } else if ( type === 'text/javascript' || typeof type === 'undefined' ) {
828 var script = '<script type="text/javascript" src="' + modules + '"></script>';
829 if ( ready ) {
830 $( 'body' ).append( script );
831 } else {
832 document.write( script );
833 }
834 return true;
835 }
836 // Unknown type
837 return false;
838 }
839 // Called with single module
840 modules = [modules];
841 }
842 // Resolve entire dependency map
843 modules = resolve( modules );
844 // If all modules are ready, nothing dependency be done
845 if ( compare( filter( ['ready'], modules ), modules ) ) {
846 return true;
847 }
848 // If any modules have errors return false
849 else if ( filter( ['error'], modules ).length ) {
850 return false;
851 }
852 // Since some modules are not yet ready, queue up a request
853 else {
854 request( modules );
855 return true;
856 }
857 };
858
859 /**
860 * Flushes the request queue and begin executing load requests on demand
861 */
862 this.go = function() {
863 suspended = false;
864 mediaWiki.loader.work();
865 };
866
867 /**
868 * Changes the state of a module
869 *
870 * @param mixed module string module name or object of module name/state pairs
871 * @param string state string state name
872 */
873 this.state = function( module, state ) {
874 if ( typeof module === 'object' ) {
875 for ( var m in module ) {
876 mediaWiki.loader.state( m, module[m] );
877 }
878 return;
879 }
880 if ( !( module in registry ) ) {
881 mediaWiki.loader.register( module );
882 }
883 registry[module].state = state;
884 };
885
886 /**
887 * Gets the version of a module
888 *
889 * @param string module name of module to get version for
890 */
891 this.version = function( module ) {
892 if ( module in registry && 'version' in registry[module] ) {
893 return formatVersionNumber( registry[module].version );
894 }
895 return null;
896 }
897
898 /* Cache document ready status */
899
900 $(document).ready( function() { ready = true; } );
901 } )();
902
903 /* Extension points */
904
905 this.legacy = {};
906
907 } )( jQuery );
908
909 /* Auto-register from pre-loaded startup scripts */
910
911 if ( typeof startUp === 'function' ) {
912 startUp();
913 delete startUp;
914 }
915
916 // Alias $j to jQuery for backwards compatibility
917 window.$j = jQuery;
918 window.mw = mediaWiki;