2 * JavaScript backwards-compatibility alternatives and other convenience functions
6 trimLeft : function( str
) {
7 return str
== null ? '' : str
.toString().replace( /^\s+/, '' );
9 trimRight : function( str
) {
11 '' : str
.toString().replace( /\s+$/, '' );
13 ucFirst : function( str
) {
14 return str
.substr( 0, 1 ).toUpperCase() + str
.substr( 1, str
.length
);
16 escapeRE : function( str
) {
17 return str
.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
19 isEmpty : function( v
) {
21 if ( v
=== "" || v
=== 0 || v
=== "0" || v
=== null
22 || v
=== false || typeof v
=== 'undefined' )
26 // the for-loop could potentially contain prototypes
27 // to avoid that we check it's length first
28 if ( v
.length
=== 0 ) {
31 if ( typeof v
=== 'object' ) {
39 compareArray : function( arrThis
, arrAgainst
) {
40 if ( arrThis
.length
!= arrAgainst
.length
) {
43 for ( var i
= 0; i
< arrThis
.length
; i
++ ) {
44 if ( arrThis
[i
] instanceof Array
) {
45 if ( !$.compareArray( arrThis
[i
], arrAgainst
[i
] ) ) {
48 } else if ( arrThis
[i
] !== arrAgainst
[i
] ) {
54 compareObject : function( objectA
, objectB
) {
56 // Do a simple check if the types match
57 if ( typeof( objectA
) == typeof( objectB
) ) {
59 // Only loop over the contents if it really is an object
60 if ( typeof( objectA
) == 'object' ) {
61 // If they are aliases of the same object (ie. mw and mediaWiki) return now
62 if ( objectA
=== objectB
) {
65 // Iterate over each property
66 for ( var prop
in objectA
) {
67 // Check if this property is also present in the other object
68 if ( prop
in objectB
) {
69 // Compare the types of the properties
70 var type
= typeof( objectA
[prop
] );
71 if ( type
== typeof( objectB
[prop
] ) ) {
72 // Recursively check objects inside this one
75 if ( !$.compareObject( objectA
[prop
], objectB
[prop
] ) ) {
80 // Functions need to be strings to compare them properly
81 if ( objectA
[prop
].toString() !== objectB
[prop
].toString() ) {
87 if ( objectA
[prop
] !== objectB
[prop
] ) {
99 // Check for properties in B but not in A
100 // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
101 // ...than incrementing a count variable in the above and below loops
102 // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
103 for ( var prop
in objectB
) {
104 if ( !( prop
in objectA
) ) {
118 * Core MediaWiki JavaScript Library
122 window
.mediaWiki
= new ( function( $ ) {
126 // This will not change until we are 100% ready to turn off legacy globals
127 var LEGACY_GLOBALS
= true;
129 /* Private Members */
131 // List of messages that have been requested to be loaded
132 var messageQueue
= {};
137 * An object which allows single and multiple get/set/exists functionality
138 * on a list of key / value pairs.
140 * @param {boolean} global Whether to get/set/exists values on the window
141 * object or a private object
143 function Map( global
) {
144 this.values
= ( global
=== true ) ? window
: {};
148 * Gets the value of a key, or a list of key/value pairs for an array of keys.
150 * If called with no arguments, all values will be returned.
152 * @param selection mixed Key or array of keys to get values for
153 * @param fallback mixed Value to use in case key(s) do not exist (optional)
155 Map
.prototype.get = function( selection
, fallback
) {
156 if ( typeof selection
=== 'object' ) {
157 selection
= $.makeArray( selection
);
159 for ( var i
= 0; i
< selection
.length
; i
++ ) {
160 results
[selection
[i
]] = this.get( selection
[i
], fallback
);
163 } else if ( typeof selection
=== 'string' ) {
164 if ( typeof this.values
[selection
] === 'undefined' ) {
165 if ( typeof fallback
!== 'undefined' ) {
170 return this.values
[selection
];
176 * Sets one or multiple key/value pairs.
178 * @param selection mixed Key or object of key/value pairs to set
179 * @param value mixed Value to set (optional, only in use when key is a string)
181 Map
.prototype.set = function( selection
, value
) {
182 if ( typeof selection
=== 'object' ) {
183 for ( var s
in selection
) {
184 this.values
[s
] = selection
[s
];
186 } else if ( typeof selection
=== 'string' && typeof value
!== 'undefined' ) {
187 this.values
[selection
] = value
;
192 * Checks if one or multiple keys exist.
194 * @param selection mixed Key or array of keys to check
195 * @return boolean Existence of key(s)
197 Map
.prototype.exists = function( selection
) {
198 if ( typeof keys
=== 'object' ) {
199 for ( var s
= 0; s
< selection
.length
; s
++ ) {
200 if ( !( selection
[s
] in this.values
) ) {
206 return selection
in this.values
;
211 * Message object, similar to Message in PHP
213 function Message( map
, key
, parameters
) {
214 this.format
= 'parse';
217 this.parameters
= typeof parameters
=== 'undefined' ? [] : $.makeArray( parameters
);
221 * Appends parameters for replacement
223 * @param parameters mixed First in a list of variadic arguments to append as message parameters
225 Message
.prototype.params = function( parameters
) {
226 for ( var i
= 0; i
< parameters
.length
; i
++ ) {
227 this.parameters
[this.parameters
.length
] = parameters
[i
];
233 * Converts message object to it's string form based on the state of format
235 * @return {string} String form of message
237 Message
.prototype.toString = function() {
238 if ( !this.map
.exists( this.key
) ) {
239 // Return <key> if key does not exist
240 return '<' + this.key
+ '>';
242 var text
= this.map
.get( this.key
);
243 var parameters
= this.parameters
;
244 text
= text
.replace( /\$(\d+)/g, function( string
, match
) {
245 var index
= parseInt( match
, 10 ) - 1;
246 return index
in parameters
? parameters
[index
] : '$' + match
;
248 /* This should be fixed up when we have a parser
249 if ( this.format === 'parse' && 'language' in mediaWiki ) {
250 text = mediaWiki.language.parse( text );
257 * Changes format to parse and converts message to string
259 * @return {string} String form of parsed message
261 Message
.prototype.parse = function() {
262 this.format
= 'parse';
263 return this.toString();
267 * Changes format to plain and converts message to string
269 * @return {string} String form of plain message
271 Message
.prototype.plain = function() {
272 this.format
= 'plain';
273 return this.toString();
277 * Checks if message exists
279 * @return {string} String form of parsed message
281 Message
.prototype.exists = function() {
282 return this.map
.exists( this.key
);
290 /* Private Members */
296 this.options
= new Map();
301 * Generates a random user session ID (32 alpha-numeric characters).
303 * This information would potentially be stored in a cookie to identify a user during a
304 * session. It's uniqueness should not be depended on.
306 * @return string random set of 32 alpha-numeric characters
308 function generateSessionId() {
310 var seed
= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
311 for ( var i
= 0, r
; i
< 32; i
++ ) {
312 r
= Math
.floor( Math
.random() * seed
.length
);
313 id
+= seed
.substring( r
, r
+ 1 );
319 * Gets the current user's name.
321 * @return mixed user name string or null if users is anonymous
323 this.name = function() {
324 return mediaWiki
.config
.get( 'wgUserName' );
328 * Gets the current user's name or a random session ID automatically generated and kept in
331 * Do not use this method before the first call to mediaWiki.loader.go(), it depends on
332 * jquery.cookie, which is added to the first payload just after mediaWiki is defined, but
333 * won't be loaded until the first call to go().
335 * @return string user name or random session ID
337 this.sessionId = function () {
338 var name
= that
.name();
342 var sessionId
= $.cookie( 'mediaWiki.user.sessionId' );
343 if ( typeof sessionId
== 'undefined' || sessionId
== null ) {
344 sessionId
= generateSessionId();
346 // Set cookie if not set, or renew it if already set
347 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { 'expires': 30, 'path': '/' } );
355 * Dummy function which in debug mode can be replaced with a function that
356 * does something clever
358 this.log = function() { };
361 * Make the Map-class publicly available
366 * List of configuration values
368 * In legacy mode the values this object wraps will be in the global space
370 this.config
= new this.Map( LEGACY_GLOBALS
);
373 * Information about the current user
375 this.user
= new User();
378 * Localization system
380 this.messages
= new this.Map();
385 * Gets a message object, similar to wfMessage()
387 * @param key string Key of message to get
388 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
391 this.message = function( key
, parameters
) {
392 // Support variadic arguments
393 if ( typeof parameters
!== 'undefined' ) {
394 parameters
= $.makeArray( arguments
);
399 return new Message( mediaWiki
.messages
, key
, parameters
);
403 * Gets a message string, similar to wfMsg()
405 * @param key string Key of message to get
406 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
409 this.msg = function( key
, parameters
) {
410 return mediaWiki
.message
.apply( mediaWiki
.message
, arguments
).toString();
414 * Client-side module loader which integrates with the MediaWiki ResourceLoader
416 this.loader
= new ( function() {
418 /* Private Members */
421 * Mapping of registered modules
423 * The jquery module is pre-registered, because it must have already
424 * been provided for this object to have been built, and in debug mode
425 * jquery would have been provided through a unique loader request,
426 * making it impossible to hold back registration of jquery until after
432 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
433 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
434 * 'script': function() {},
435 * 'style': 'css code string',
436 * 'messages': { 'key': 'value' },
437 * 'version': ############## (unix timestamp)
442 // List of modules which will be loaded as when ready
444 // List of modules to be loaded
446 // List of callback functions waiting for modules to be ready to be called
448 // Flag indicating that requests should be suspended
449 var suspended
= true;
450 // Flag inidicating that document ready has occured
453 /* Private Methods */
455 function compare( a
, b
) {
456 if ( a
.length
!= b
.length
) {
459 for ( var i
= 0; i
< b
.length
; i
++ ) {
460 if ( $.isArray( a
[i
] ) ) {
461 if ( !compare( a
[i
], b
[i
] ) ) {
465 if ( a
[i
] !== b
[i
] ) {
473 * Generates an ISO8601 "basic" string from a UNIX timestamp
475 function formatVersionNumber( timestamp
) {
476 function pad( a
, b
, c
) {
477 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
480 d
.setTime( timestamp
* 1000 );
482 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
483 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
488 * Recursively resolves dependencies and detects circular references
490 function recurse( module
, resolved
, unresolved
) {
491 if ( typeof registry
[module
] === 'undefined' ) {
492 throw new Error( 'Unknown dependency: ' + module
);
494 // Resolves dynamic loader function and replaces it with its own results
495 if ( typeof registry
[module
].dependencies
=== 'function' ) {
496 registry
[module
].dependencies
= registry
[module
].dependencies();
497 // Ensures the module's dependencies are always in an array
498 if ( typeof registry
[module
].dependencies
!== 'object' ) {
499 registry
[module
].dependencies
= [registry
[module
].dependencies
];
502 // Tracks down dependencies
503 for ( var n
= 0; n
< registry
[module
].dependencies
.length
; n
++ ) {
504 if ( $.inArray( registry
[module
].dependencies
[n
], resolved
) === -1 ) {
505 if ( $.inArray( registry
[module
].dependencies
[n
], unresolved
) !== -1 ) {
507 'Circular reference detected: ' + module
+
508 ' -> ' + registry
[module
].dependencies
[n
]
511 recurse( registry
[module
].dependencies
[n
], resolved
, unresolved
);
514 resolved
[resolved
.length
] = module
;
515 unresolved
.splice( $.inArray( module
, unresolved
), 1 );
519 * Gets a list of module names that a module depends on in their proper dependency order
521 * @param module string module name or array of string module names
522 * @return list of dependencies
523 * @throws Error if circular reference is detected
525 function resolve( module
, resolved
, unresolved
) {
526 // Allow calling with an array of module names
527 if ( typeof module
=== 'object' ) {
529 for ( var m
= 0; m
< module
.length
; m
++ ) {
530 var dependencies
= resolve( module
[m
] );
531 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
532 modules
[modules
.length
] = dependencies
[n
];
536 } else if ( typeof module
=== 'string' ) {
537 // Undefined modules have no dependencies
538 if ( !( module
in registry
) ) {
542 recurse( module
, resolved
, [] );
545 throw new Error( 'Invalid module argument: ' + module
);
549 * Narrows a list of module names down to those matching a specific
550 * state. Possible states are 'undefined', 'registered', 'loading',
551 * 'loaded', or 'ready'
553 * @param states string or array of strings of module states to filter by
554 * @param modules array list of module names to filter (optional, all modules
555 * will be used by default)
556 * @return array list of filtered module names
558 function filter( states
, modules
) {
559 // Allow states to be given as a string
560 if ( typeof states
=== 'string' ) {
563 // If called without a list of modules, build and use a list of all modules
565 if ( typeof modules
=== 'undefined' ) {
567 for ( module
in registry
) {
568 modules
[modules
.length
] = module
;
571 // Build a list of modules which are in one of the specified states
572 for ( var s
= 0; s
< states
.length
; s
++ ) {
573 for ( var m
= 0; m
< modules
.length
; m
++ ) {
574 if ( typeof registry
[modules
[m
]] === 'undefined' ) {
575 // Module does not exist
576 if ( states
[s
] == 'undefined' ) {
578 list
[list
.length
] = modules
[m
];
581 // Module exists, check state
582 if ( registry
[modules
[m
]].state
=== states
[s
] ) {
584 list
[list
.length
] = modules
[m
];
593 * Executes a loaded module, making it ready to use
595 * @param module string module name to execute
597 function execute( module
) {
598 if ( typeof registry
[module
] === 'undefined' ) {
599 throw new Error( 'Module has not been registered yet: ' + module
);
600 } else if ( registry
[module
].state
=== 'registered' ) {
601 throw new Error( 'Module has not been requested from the server yet: ' + module
);
602 } else if ( registry
[module
].state
=== 'loading' ) {
603 throw new Error( 'Module has not completed loading yet: ' + module
);
604 } else if ( registry
[module
].state
=== 'ready' ) {
605 throw new Error( 'Module has already been loaded: ' + module
);
607 // Add style sheet to document
608 if ( typeof registry
[module
].style
=== 'string' && registry
[module
].style
.length
) {
610 .append( mediaWiki
.html
.element( 'style',
611 { type
: "text/css" },
612 new mediaWiki
.html
.Cdata( registry
[module
].style
)
614 } else if ( typeof registry
[module
].style
=== 'object'
615 && !( registry
[module
].style
instanceof Array
) )
617 for ( var media
in registry
[module
].style
) {
618 $( 'head' ).append( mediaWiki
.html
.element( 'style',
619 { type
: 'text/css', media
: media
},
620 new mediaWiki
.html
.Cdata( registry
[module
].style
[media
] )
624 // Add localizations to message system
625 if ( typeof registry
[module
].messages
=== 'object' ) {
626 mediaWiki
.messages
.set( registry
[module
].messages
);
630 registry
[module
].script();
631 registry
[module
].state
= 'ready';
632 // Run jobs who's dependencies have just been met
633 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
635 filter( 'ready', jobs
[j
].dependencies
),
636 jobs
[j
].dependencies
) )
638 if ( typeof jobs
[j
].ready
=== 'function' ) {
645 // Execute modules who's dependencies have just been met
646 for ( r
in registry
) {
647 if ( registry
[r
].state
== 'loaded' ) {
649 filter( ['ready'], registry
[r
].dependencies
),
650 registry
[r
].dependencies
) )
657 mediaWiki
.log( 'Exception thrown by ' + module
+ ': ' + e
.message
);
659 registry
[module
].state
= 'error';
660 // Run error callbacks of jobs affected by this condition
661 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
662 if ( $.inArray( module
, jobs
[j
].dependencies
) !== -1 ) {
663 if ( typeof jobs
[j
].error
=== 'function' ) {
674 * Adds a dependencies to the queue with optional callbacks to be run
675 * when the dependencies are ready or fail
677 * @param dependencies string module name or array of string module names
678 * @param ready function callback to execute when all dependencies are ready
679 * @param error function callback to execute when any dependency fails
681 function request( dependencies
, ready
, error
) {
682 // Allow calling by single module name
683 if ( typeof dependencies
=== 'string' ) {
684 dependencies
= [dependencies
];
685 if ( dependencies
[0] in registry
) {
686 for ( var n
= 0; n
< registry
[dependencies
[0]].dependencies
.length
; n
++ ) {
687 dependencies
[dependencies
.length
] =
688 registry
[dependencies
[0]].dependencies
[n
];
692 // Add ready and error callbacks if they were given
693 if ( arguments
.length
> 1 ) {
694 jobs
[jobs
.length
] = {
695 'dependencies': filter(
696 ['undefined', 'registered', 'loading', 'loaded'],
702 // Queue up any dependencies that are undefined or registered
703 dependencies
= filter( ['undefined', 'registered'], dependencies
);
704 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
705 if ( $.inArray( dependencies
[n
], queue
) === -1 ) {
706 queue
[queue
.length
] = dependencies
[n
];
710 mediaWiki
.loader
.work();
713 function sortQuery(o
) {
714 var sorted
= {}, key
, a
= [];
716 if ( o
.hasOwnProperty( key
) ) {
721 for ( key
= 0; key
< a
.length
; key
++ ) {
722 sorted
[a
[key
]] = o
[a
[key
]];
730 * Requests dependencies from server, loading and executing when things when ready.
732 this.work = function() {
733 // Appends a list of modules to the batch
734 for ( var q
= 0; q
< queue
.length
; q
++ ) {
735 // Only request modules which are undefined or registered
736 if ( !( queue
[q
] in registry
) || registry
[queue
[q
]].state
== 'registered' ) {
737 // Prevent duplicate entries
738 if ( $.inArray( queue
[q
], batch
) === -1 ) {
739 batch
[batch
.length
] = queue
[q
];
740 // Mark registered modules as loading
741 if ( queue
[q
] in registry
) {
742 registry
[queue
[q
]].state
= 'loading';
747 // Clean up the queue
749 // After document ready, handle the batch
750 if ( !suspended
&& batch
.length
) {
751 // Always order modules alphabetically to help reduce cache
752 // misses for otherwise identical content
754 // Build a list of request parameters
756 'skin': mediaWiki
.config
.get( 'skin' ),
757 'lang': mediaWiki
.config
.get( 'wgUserLanguage' ),
758 'debug': mediaWiki
.config
.get( 'debug' )
760 // Extend request parameters with a list of modules in the batch
764 for ( var b
= 0; b
< batch
.length
; b
++ ) {
765 var group
= registry
[batch
[b
]].group
;
766 if ( !( group
in groups
) ) {
769 groups
[group
][groups
[group
].length
] = batch
[b
];
771 for ( var group
in groups
) {
772 // Calculate the highest timestamp
774 for ( var g
= 0; g
< groups
[group
].length
; g
++ ) {
775 if ( registry
[groups
[group
][g
]].version
> version
) {
776 version
= registry
[groups
[group
][g
]].version
;
779 requests
[requests
.length
] = $.extend(
780 { 'modules': groups
[group
].join( '|' ), 'version': formatVersionNumber( version
) }, base
783 // Clear the batch - this MUST happen before we append the
784 // script element to the body or it's possible that the script
785 // will be locally cached, instantly load, and work the batch
786 // again, all before we've cleared it causing each request to
787 // include modules which are already loaded
789 // Asynchronously append a script tag to the end of the body
792 for ( var r
= 0; r
< requests
.length
; r
++ ) {
793 requests
[r
] = sortQuery( requests
[r
] );
794 // Build out the HTML
795 var src
= mediaWiki
.config
.get( 'wgLoadScript' ) + '?' + $.param( requests
[r
] );
796 html
+= mediaWiki
.html
.element( 'script',
797 { type
: 'text/javascript', src
: src
}, '' );
801 // Load asynchronously after doumument ready
803 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
805 document
.write( request() );
811 * Registers a module, letting the system know about it and its
812 * dependencies. loader.js files contain calls to this function.
814 this.register = function( module
, version
, dependencies
, group
) {
815 // Allow multiple registration
816 if ( typeof module
=== 'object' ) {
817 for ( var m
= 0; m
< module
.length
; m
++ ) {
818 if ( typeof module
[m
] === 'string' ) {
819 mediaWiki
.loader
.register( module
[m
] );
820 } else if ( typeof module
[m
] === 'object' ) {
821 mediaWiki
.loader
.register
.apply( mediaWiki
.loader
, module
[m
] );
827 if ( typeof module
!== 'string' ) {
828 throw new Error( 'module must be a string, not a ' + typeof module
);
830 if ( typeof registry
[module
] !== 'undefined' ) {
831 throw new Error( 'module already implemeneted: ' + module
);
833 // List the module as registered
835 'state': 'registered',
836 'group': typeof group
=== 'string' ? group
: null,
838 'version': typeof version
!== 'undefined' ? parseInt( version
) : 0
840 if ( typeof dependencies
=== 'string' ) {
841 // Allow dependencies to be given as a single module name
842 registry
[module
].dependencies
= [dependencies
];
843 } else if ( typeof dependencies
=== 'object' || typeof dependencies
=== 'function' ) {
844 // Allow dependencies to be given as an array of module names
845 // or a function which returns an array
846 registry
[module
].dependencies
= dependencies
;
851 * Implements a module, giving the system a course of action to take
852 * upon loading. Results of a request for one or more modules contain
853 * calls to this function.
855 this.implement = function( module
, script
, style
, localization
) {
856 // Automatically register module
857 if ( typeof registry
[module
] === 'undefined' ) {
858 mediaWiki
.loader
.register( module
);
861 if ( typeof script
!== 'function' ) {
862 throw new Error( 'script must be a function, not a ' + typeof script
);
864 if ( typeof style
!== 'undefined'
865 && typeof style
!== 'string'
866 && typeof style
!== 'object' )
868 throw new Error( 'style must be a string or object, not a ' + typeof style
);
870 if ( typeof localization
!== 'undefined'
871 && typeof localization
!== 'object' )
873 throw new Error( 'localization must be an object, not a ' + typeof localization
);
875 if ( typeof registry
[module
] !== 'undefined'
876 && typeof registry
[module
].script
!== 'undefined' )
878 throw new Error( 'module already implemeneted: ' + module
);
880 // Mark module as loaded
881 registry
[module
].state
= 'loaded';
883 registry
[module
].script
= script
;
884 if ( typeof style
=== 'string'
885 || typeof style
=== 'object' && !( style
instanceof Array
) )
887 registry
[module
].style
= style
;
889 if ( typeof localization
=== 'object' ) {
890 registry
[module
].messages
= localization
;
892 // Execute or queue callback
894 filter( ['ready'], registry
[module
].dependencies
),
895 registry
[module
].dependencies
) )
904 * Executes a function as soon as one or more required modules are ready
906 * @param dependencies string or array of strings of modules names the callback
907 * dependencies to be ready before
909 * @param ready function callback to execute when all dependencies are ready (optional)
910 * @param error function callback to execute when if dependencies have a errors (optional)
912 this.using = function( dependencies
, ready
, error
) {
914 if ( typeof dependencies
!== 'object' && typeof dependencies
!== 'string' ) {
915 throw new Error( 'dependencies must be a string or an array, not a ' +
916 typeof dependencies
)
918 // Allow calling with a single dependency as a string
919 if ( typeof dependencies
=== 'string' ) {
920 dependencies
= [dependencies
];
922 // Resolve entire dependency map
923 dependencies
= resolve( dependencies
);
924 // If all dependencies are met, execute ready immediately
925 if ( compare( filter( ['ready'], dependencies
), dependencies
) ) {
926 if ( typeof ready
=== 'function' ) {
930 // If any dependencies have errors execute error immediately
931 else if ( filter( ['error'], dependencies
).length
) {
932 if ( typeof error
=== 'function' ) {
936 // Since some dependencies are not yet ready, queue up a request
938 request( dependencies
, ready
, error
);
943 * Loads an external script or one or more modules for future use
945 * @param modules mixed either the name of a module, array of modules,
946 * or a URL of an external script or style
947 * @param type string mime-type to use if calling with a URL of an
948 * external script or style; acceptable values are "text/css" and
949 * "text/javascript"; if no type is provided, text/javascript is
952 this.load = function( modules
, type
) {
954 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
955 throw new Error( 'dependencies must be a string or an array, not a ' +
956 typeof dependencies
)
958 // Allow calling with an external script or single dependency as a string
959 if ( typeof modules
=== 'string' ) {
960 // Support adding arbitrary external scripts
961 if ( modules
.substr( 0, 7 ) == 'http://'
962 || modules
.substr( 0, 8 ) == 'https://' )
964 if ( type
=== 'text/css' ) {
966 .append( $( '<link rel="stylesheet" type="text/css" />' )
967 .attr( 'href', modules
) );
969 } else if ( type
=== 'text/javascript' || typeof type
=== 'undefined' ) {
970 var script
= mediaWiki
.html
.element( 'script',
971 { type
: 'text/javascript', src
: modules
}, '' );
973 $( 'body' ).append( script
);
975 document
.write( script
);
982 // Called with single module
985 // Resolve entire dependency map
986 modules
= resolve( modules
);
987 // If all modules are ready, nothing dependency be done
988 if ( compare( filter( ['ready'], modules
), modules
) ) {
991 // If any modules have errors return false
992 else if ( filter( ['error'], modules
).length
) {
995 // Since some modules are not yet ready, queue up a request
1003 * Flushes the request queue and begin executing load requests on demand
1005 this.go = function() {
1007 mediaWiki
.loader
.work();
1011 * Changes the state of a module
1013 * @param module string module name or object of module name/state pairs
1014 * @param state string state name
1016 this.state = function( module
, state
) {
1017 if ( typeof module
=== 'object' ) {
1018 for ( var m
in module
) {
1019 mediaWiki
.loader
.state( m
, module
[m
] );
1023 if ( !( module
in registry
) ) {
1024 mediaWiki
.loader
.register( module
);
1026 registry
[module
].state
= state
;
1030 * Gets the version of a module
1032 * @param module string name of module to get version for
1034 this.version = function( module
) {
1035 if ( module
in registry
&& 'version' in registry
[module
] ) {
1036 return formatVersionNumber( registry
[module
].version
);
1041 /* Cache document ready status */
1043 $(document
).ready( function() { ready
= true; } );
1046 /** HTML construction helper functions */
1047 this.html
= new ( function () {
1048 function escapeCallback( s
) {
1064 * Escape a string for HTML. Converts special characters to HTML entities.
1065 * @param s The string to escape
1067 this.escape = function( s
) {
1068 return s
.replace( /['"<>&]/g, escapeCallback
);
1072 * Wrapper object for raw HTML passed to mediaWiki.html.element().
1074 this.Raw = function( value
) {
1079 * Wrapper object for CDATA element contents passed to mediaWiki.html.element()
1081 this.Cdata = function( value
) {
1086 * Create an HTML element string, with safe escaping.
1088 * @param name The tag name.
1089 * @param attrs An object with members mapping element names to values
1090 * @param contents The contents of the element. May be either:
1091 * - string: The string is escaped.
1092 * - null or undefined: The short closing form is used, e.g. <br/>.
1093 * - this.Raw: The value attribute is included without escaping.
1094 * - this.Cdata: The value attribute is included, and an exception is
1095 * thrown if it contains an illegal ETAGO delimiter.
1096 * See http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2
1099 * var h = mediaWiki.html;
1100 * return h.element( 'div', {},
1101 * new h.Raw( h.element( 'img', {src: '<'} ) ) );
1102 * Returns <div><img src="<"/></div>
1104 this.element = function( name
, attrs
, contents
) {
1106 for ( attrName
in attrs
) {
1107 s
+= ' ' + attrName
+ '="' + this.escape( attrs
[attrName
] ) + '"';
1109 if ( typeof contents
== 'undefined' || contents
=== null ) {
1116 if (typeof contents
=== 'string') {
1118 s
+= this.escape( contents
);
1119 } else if ( contents
instanceof this.Raw
) {
1120 // Raw HTML inclusion
1121 s
+= contents
.value
;
1122 } else if ( contents
instanceof this.Cdata
) {
1124 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
1125 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
1127 s
+= contents
.value
;
1129 throw new Error( 'mw.html.element: Invalid type of contents' );
1131 s
+= '</' + name
+ '>';
1137 /* Extension points */
1143 /* Auto-register from pre-loaded startup scripts */
1145 if ( typeof startUp
=== 'function' ) {
1150 // Add jQuery Cookie to initial payload (used in mediaWiki.user)
1151 mediaWiki
.loader
.load( 'jquery.cookie' );
1153 // Alias $j to jQuery for backwards compatibility
1155 window
.mw
= mediaWiki
;