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();
345 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { 'expires': 30, 'path': '/' } );
354 * Dummy function which in debug mode can be replaced with a function that
355 * does something clever
357 this.log = function() { };
360 * Make the Map-class publicly available
365 * List of configuration values
367 * In legacy mode the values this object wraps will be in the global space
369 this.config
= new this.Map( LEGACY_GLOBALS
);
372 * Information about the current user
374 this.user
= new User();
377 * Localization system
379 this.messages
= new this.Map();
384 * Gets a message object, similar to wfMessage()
386 * @param key string Key of message to get
387 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
390 this.message = function( key
, parameters
) {
391 // Support variadic arguments
392 if ( typeof parameters
!== 'undefined' ) {
393 parameters
= $.makeArray( arguments
);
398 return new Message( mediaWiki
.messages
, key
, parameters
);
402 * Gets a message string, similar to wfMsg()
404 * @param key string Key of message to get
405 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
408 this.msg = function( key
, parameters
) {
409 return mediaWiki
.message
.apply( mediaWiki
.message
, arguments
).toString();
413 * Client-side module loader which integrates with the MediaWiki ResourceLoader
415 this.loader
= new ( function() {
417 /* Private Members */
420 * Mapping of registered modules
422 * The jquery module is pre-registered, because it must have already
423 * been provided for this object to have been built, and in debug mode
424 * jquery would have been provided through a unique loader request,
425 * making it impossible to hold back registration of jquery until after
431 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
432 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
433 * 'script': function() {},
434 * 'style': 'css code string',
435 * 'messages': { 'key': 'value' },
436 * 'version': ############## (unix timestamp)
441 // List of modules which will be loaded as when ready
443 // List of modules to be loaded
445 // List of callback functions waiting for modules to be ready to be called
447 // Flag indicating that requests should be suspended
448 var suspended
= true;
449 // Flag inidicating that document ready has occured
452 /* Private Methods */
454 function compare( a
, b
) {
455 if ( a
.length
!= b
.length
) {
458 for ( var i
= 0; i
< b
.length
; i
++ ) {
459 if ( $.isArray( a
[i
] ) ) {
460 if ( !compare( a
[i
], b
[i
] ) ) {
464 if ( a
[i
] !== b
[i
] ) {
472 * Generates an ISO8601 "basic" string from a UNIX timestamp
474 function formatVersionNumber( timestamp
) {
475 function pad( a
, b
, c
) {
476 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
479 d
.setTime( timestamp
* 1000 );
481 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
482 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
487 * Recursively resolves dependencies and detects circular references
489 function recurse( module
, resolved
, unresolved
) {
490 if ( typeof registry
[module
] === 'undefined' ) {
491 throw new Error( 'Unknown dependency: ' + module
);
493 // Resolves dynamic loader function and replaces it with its own results
494 if ( typeof registry
[module
].dependencies
=== 'function' ) {
495 registry
[module
].dependencies
= registry
[module
].dependencies();
496 // Ensures the module's dependencies are always in an array
497 if ( typeof registry
[module
].dependencies
!== 'object' ) {
498 registry
[module
].dependencies
= [registry
[module
].dependencies
];
501 // Tracks down dependencies
502 for ( var n
= 0; n
< registry
[module
].dependencies
.length
; n
++ ) {
503 if ( $.inArray( registry
[module
].dependencies
[n
], resolved
) === -1 ) {
504 if ( $.inArray( registry
[module
].dependencies
[n
], unresolved
) !== -1 ) {
506 'Circular reference detected: ' + module
+
507 ' -> ' + registry
[module
].dependencies
[n
]
510 recurse( registry
[module
].dependencies
[n
], resolved
, unresolved
);
513 resolved
[resolved
.length
] = module
;
514 unresolved
.splice( $.inArray( module
, unresolved
), 1 );
518 * Gets a list of module names that a module depends on in their proper dependency order
520 * @param module string module name or array of string module names
521 * @return list of dependencies
522 * @throws Error if circular reference is detected
524 function resolve( module
, resolved
, unresolved
) {
525 // Allow calling with an array of module names
526 if ( typeof module
=== 'object' ) {
528 for ( var m
= 0; m
< module
.length
; m
++ ) {
529 var dependencies
= resolve( module
[m
] );
530 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
531 modules
[modules
.length
] = dependencies
[n
];
535 } else if ( typeof module
=== 'string' ) {
536 // Undefined modules have no dependencies
537 if ( !( module
in registry
) ) {
541 recurse( module
, resolved
, [] );
544 throw new Error( 'Invalid module argument: ' + module
);
548 * Narrows a list of module names down to those matching a specific
549 * state. Possible states are 'undefined', 'registered', 'loading',
550 * 'loaded', or 'ready'
552 * @param states string or array of strings of module states to filter by
553 * @param modules array list of module names to filter (optional, all modules
554 * will be used by default)
555 * @return array list of filtered module names
557 function filter( states
, modules
) {
558 // Allow states to be given as a string
559 if ( typeof states
=== 'string' ) {
562 // If called without a list of modules, build and use a list of all modules
564 if ( typeof modules
=== 'undefined' ) {
566 for ( module
in registry
) {
567 modules
[modules
.length
] = module
;
570 // Build a list of modules which are in one of the specified states
571 for ( var s
= 0; s
< states
.length
; s
++ ) {
572 for ( var m
= 0; m
< modules
.length
; m
++ ) {
573 if ( typeof registry
[modules
[m
]] === 'undefined' ) {
574 // Module does not exist
575 if ( states
[s
] == 'undefined' ) {
577 list
[list
.length
] = modules
[m
];
580 // Module exists, check state
581 if ( registry
[modules
[m
]].state
=== states
[s
] ) {
583 list
[list
.length
] = modules
[m
];
592 * Executes a loaded module, making it ready to use
594 * @param module string module name to execute
596 function execute( module
) {
597 if ( typeof registry
[module
] === 'undefined' ) {
598 throw new Error( 'Module has not been registered yet: ' + module
);
599 } else if ( registry
[module
].state
=== 'registered' ) {
600 throw new Error( 'Module has not been requested from the server yet: ' + module
);
601 } else if ( registry
[module
].state
=== 'loading' ) {
602 throw new Error( 'Module has not completed loading yet: ' + module
);
603 } else if ( registry
[module
].state
=== 'ready' ) {
604 throw new Error( 'Module has already been loaded: ' + module
);
606 // Add style sheet to document
607 if ( typeof registry
[module
].style
=== 'string' && registry
[module
].style
.length
) {
609 .append( mediaWiki
.html
.element( 'style',
610 { type
: "text/css" },
611 new mediaWiki
.html
.Cdata( registry
[module
].style
)
613 } else if ( typeof registry
[module
].style
=== 'object'
614 && !( registry
[module
].style
instanceof Array
) )
616 for ( var media
in registry
[module
].style
) {
617 $( 'head' ).append( mediaWiki
.html
.element( 'style',
618 { type
: 'text/css', media
: media
},
619 new mediaWiki
.html
.Cdata( registry
[module
].style
[media
] )
623 // Add localizations to message system
624 if ( typeof registry
[module
].messages
=== 'object' ) {
625 mediaWiki
.messages
.set( registry
[module
].messages
);
629 registry
[module
].script();
630 registry
[module
].state
= 'ready';
631 // Run jobs who's dependencies have just been met
632 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
634 filter( 'ready', jobs
[j
].dependencies
),
635 jobs
[j
].dependencies
) )
637 if ( typeof jobs
[j
].ready
=== 'function' ) {
644 // Execute modules who's dependencies have just been met
645 for ( r
in registry
) {
646 if ( registry
[r
].state
== 'loaded' ) {
648 filter( ['ready'], registry
[r
].dependencies
),
649 registry
[r
].dependencies
) )
656 mediaWiki
.log( 'Exception thrown by ' + module
+ ': ' + e
.message
);
658 registry
[module
].state
= 'error';
659 // Run error callbacks of jobs affected by this condition
660 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
661 if ( $.inArray( module
, jobs
[j
].dependencies
) !== -1 ) {
662 if ( typeof jobs
[j
].error
=== 'function' ) {
673 * Adds a dependencies to the queue with optional callbacks to be run
674 * when the dependencies are ready or fail
676 * @param dependencies string module name or array of string module names
677 * @param ready function callback to execute when all dependencies are ready
678 * @param error function callback to execute when any dependency fails
680 function request( dependencies
, ready
, error
) {
681 // Allow calling by single module name
682 if ( typeof dependencies
=== 'string' ) {
683 dependencies
= [dependencies
];
684 if ( dependencies
[0] in registry
) {
685 for ( var n
= 0; n
< registry
[dependencies
[0]].dependencies
.length
; n
++ ) {
686 dependencies
[dependencies
.length
] =
687 registry
[dependencies
[0]].dependencies
[n
];
691 // Add ready and error callbacks if they were given
692 if ( arguments
.length
> 1 ) {
693 jobs
[jobs
.length
] = {
694 'dependencies': filter(
695 ['undefined', 'registered', 'loading', 'loaded'],
701 // Queue up any dependencies that are undefined or registered
702 dependencies
= filter( ['undefined', 'registered'], dependencies
);
703 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
704 if ( $.inArray( dependencies
[n
], queue
) === -1 ) {
705 queue
[queue
.length
] = dependencies
[n
];
709 mediaWiki
.loader
.work();
712 function sortQuery(o
) {
713 var sorted
= {}, key
, a
= [];
715 if ( o
.hasOwnProperty( key
) ) {
720 for ( key
= 0; key
< a
.length
; key
++ ) {
721 sorted
[a
[key
]] = o
[a
[key
]];
729 * Requests dependencies from server, loading and executing when things when ready.
731 this.work = function() {
732 // Appends a list of modules to the batch
733 for ( var q
= 0; q
< queue
.length
; q
++ ) {
734 // Only request modules which are undefined or registered
735 if ( !( queue
[q
] in registry
) || registry
[queue
[q
]].state
== 'registered' ) {
736 // Prevent duplicate entries
737 if ( $.inArray( queue
[q
], batch
) === -1 ) {
738 batch
[batch
.length
] = queue
[q
];
739 // Mark registered modules as loading
740 if ( queue
[q
] in registry
) {
741 registry
[queue
[q
]].state
= 'loading';
746 // Clean up the queue
748 // After document ready, handle the batch
749 if ( !suspended
&& batch
.length
) {
750 // Always order modules alphabetically to help reduce cache
751 // misses for otherwise identical content
753 // Build a list of request parameters
755 'skin': mediaWiki
.config
.get( 'skin' ),
756 'lang': mediaWiki
.config
.get( 'wgUserLanguage' ),
757 'debug': mediaWiki
.config
.get( 'debug' )
759 // Extend request parameters with a list of modules in the batch
763 for ( var b
= 0; b
< batch
.length
; b
++ ) {
764 var group
= registry
[batch
[b
]].group
;
765 if ( !( group
in groups
) ) {
768 groups
[group
][groups
[group
].length
] = batch
[b
];
770 for ( var group
in groups
) {
771 // Calculate the highest timestamp
773 for ( var g
= 0; g
< groups
[group
].length
; g
++ ) {
774 if ( registry
[groups
[group
][g
]].version
> version
) {
775 version
= registry
[groups
[group
][g
]].version
;
778 requests
[requests
.length
] = $.extend(
779 { 'modules': groups
[group
].join( '|' ), 'version': formatVersionNumber( version
) }, base
782 // Clear the batch - this MUST happen before we append the
783 // script element to the body or it's possible that the script
784 // will be locally cached, instantly load, and work the batch
785 // again, all before we've cleared it causing each request to
786 // include modules which are already loaded
788 // Asynchronously append a script tag to the end of the body
791 for ( var r
= 0; r
< requests
.length
; r
++ ) {
792 requests
[r
] = sortQuery( requests
[r
] );
793 // Build out the HTML
794 var src
= mediaWiki
.config
.get( 'wgLoadScript' ) + '?' + $.param( requests
[r
] );
795 html
+= mediaWiki
.html
.element( 'script',
796 { type
: 'text/javascript', src
: src
}, '' );
800 // Load asynchronously after doumument ready
802 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
804 document
.write( request() );
810 * Registers a module, letting the system know about it and its
811 * dependencies. loader.js files contain calls to this function.
813 this.register = function( module
, version
, dependencies
, group
) {
814 // Allow multiple registration
815 if ( typeof module
=== 'object' ) {
816 for ( var m
= 0; m
< module
.length
; m
++ ) {
817 if ( typeof module
[m
] === 'string' ) {
818 mediaWiki
.loader
.register( module
[m
] );
819 } else if ( typeof module
[m
] === 'object' ) {
820 mediaWiki
.loader
.register
.apply( mediaWiki
.loader
, module
[m
] );
826 if ( typeof module
!== 'string' ) {
827 throw new Error( 'module must be a string, not a ' + typeof module
);
829 if ( typeof registry
[module
] !== 'undefined' ) {
830 throw new Error( 'module already implemeneted: ' + module
);
832 // List the module as registered
834 'state': 'registered',
835 'group': typeof group
=== 'string' ? group
: null,
837 'version': typeof version
!== 'undefined' ? parseInt( version
) : 0
839 if ( typeof dependencies
=== 'string' ) {
840 // Allow dependencies to be given as a single module name
841 registry
[module
].dependencies
= [dependencies
];
842 } else if ( typeof dependencies
=== 'object' || typeof dependencies
=== 'function' ) {
843 // Allow dependencies to be given as an array of module names
844 // or a function which returns an array
845 registry
[module
].dependencies
= dependencies
;
850 * Implements a module, giving the system a course of action to take
851 * upon loading. Results of a request for one or more modules contain
852 * calls to this function.
854 this.implement = function( module
, script
, style
, localization
) {
855 // Automatically register module
856 if ( typeof registry
[module
] === 'undefined' ) {
857 mediaWiki
.loader
.register( module
);
860 if ( typeof script
!== 'function' ) {
861 throw new Error( 'script must be a function, not a ' + typeof script
);
863 if ( typeof style
!== 'undefined'
864 && typeof style
!== 'string'
865 && typeof style
!== 'object' )
867 throw new Error( 'style must be a string or object, not a ' + typeof style
);
869 if ( typeof localization
!== 'undefined'
870 && typeof localization
!== 'object' )
872 throw new Error( 'localization must be an object, not a ' + typeof localization
);
874 if ( typeof registry
[module
] !== 'undefined'
875 && typeof registry
[module
].script
!== 'undefined' )
877 throw new Error( 'module already implemeneted: ' + module
);
879 // Mark module as loaded
880 registry
[module
].state
= 'loaded';
882 registry
[module
].script
= script
;
883 if ( typeof style
=== 'string'
884 || typeof style
=== 'object' && !( style
instanceof Array
) )
886 registry
[module
].style
= style
;
888 if ( typeof localization
=== 'object' ) {
889 registry
[module
].messages
= localization
;
891 // Execute or queue callback
893 filter( ['ready'], registry
[module
].dependencies
),
894 registry
[module
].dependencies
) )
903 * Executes a function as soon as one or more required modules are ready
905 * @param dependencies string or array of strings of modules names the callback
906 * dependencies to be ready before
908 * @param ready function callback to execute when all dependencies are ready (optional)
909 * @param error function callback to execute when if dependencies have a errors (optional)
911 this.using = function( dependencies
, ready
, error
) {
913 if ( typeof dependencies
!== 'object' && typeof dependencies
!== 'string' ) {
914 throw new Error( 'dependencies must be a string or an array, not a ' +
915 typeof dependencies
)
917 // Allow calling with a single dependency as a string
918 if ( typeof dependencies
=== 'string' ) {
919 dependencies
= [dependencies
];
921 // Resolve entire dependency map
922 dependencies
= resolve( dependencies
);
923 // If all dependencies are met, execute ready immediately
924 if ( compare( filter( ['ready'], dependencies
), dependencies
) ) {
925 if ( typeof ready
=== 'function' ) {
929 // If any dependencies have errors execute error immediately
930 else if ( filter( ['error'], dependencies
).length
) {
931 if ( typeof error
=== 'function' ) {
935 // Since some dependencies are not yet ready, queue up a request
937 request( dependencies
, ready
, error
);
942 * Loads an external script or one or more modules for future use
944 * @param modules mixed either the name of a module, array of modules,
945 * or a URL of an external script or style
946 * @param type string mime-type to use if calling with a URL of an
947 * external script or style; acceptable values are "text/css" and
948 * "text/javascript"; if no type is provided, text/javascript is
951 this.load = function( modules
, type
) {
953 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
954 throw new Error( 'dependencies must be a string or an array, not a ' +
955 typeof dependencies
)
957 // Allow calling with an external script or single dependency as a string
958 if ( typeof modules
=== 'string' ) {
959 // Support adding arbitrary external scripts
960 if ( modules
.substr( 0, 7 ) == 'http://'
961 || modules
.substr( 0, 8 ) == 'https://' )
963 if ( type
=== 'text/css' ) {
965 .append( $( '<link rel="stylesheet" type="text/css" />' )
966 .attr( 'href', modules
) );
968 } else if ( type
=== 'text/javascript' || typeof type
=== 'undefined' ) {
969 var script
= mediaWiki
.html
.element( 'script',
970 { type
: 'text/javascript', src
: modules
}, '' );
972 $( 'body' ).append( script
);
974 document
.write( script
);
981 // Called with single module
984 // Resolve entire dependency map
985 modules
= resolve( modules
);
986 // If all modules are ready, nothing dependency be done
987 if ( compare( filter( ['ready'], modules
), modules
) ) {
990 // If any modules have errors return false
991 else if ( filter( ['error'], modules
).length
) {
994 // Since some modules are not yet ready, queue up a request
1002 * Flushes the request queue and begin executing load requests on demand
1004 this.go = function() {
1006 mediaWiki
.loader
.work();
1010 * Changes the state of a module
1012 * @param module string module name or object of module name/state pairs
1013 * @param state string state name
1015 this.state = function( module
, state
) {
1016 if ( typeof module
=== 'object' ) {
1017 for ( var m
in module
) {
1018 mediaWiki
.loader
.state( m
, module
[m
] );
1022 if ( !( module
in registry
) ) {
1023 mediaWiki
.loader
.register( module
);
1025 registry
[module
].state
= state
;
1029 * Gets the version of a module
1031 * @param module string name of module to get version for
1033 this.version = function( module
) {
1034 if ( module
in registry
&& 'version' in registry
[module
] ) {
1035 return formatVersionNumber( registry
[module
].version
);
1040 /* Cache document ready status */
1042 $(document
).ready( function() { ready
= true; } );
1045 /** HTML construction helper functions */
1046 this.html
= new ( function () {
1047 function escapeCallback( s
) {
1063 * Escape a string for HTML. Converts special characters to HTML entities.
1064 * @param s The string to escape
1066 this.escape = function( s
) {
1067 return s
.replace( /['"<>&]/g, escapeCallback
);
1071 * Wrapper object for raw HTML passed to mediaWiki.html.element().
1073 this.Raw = function( value
) {
1078 * Wrapper object for CDATA element contents passed to mediaWiki.html.element()
1080 this.Cdata = function( value
) {
1085 * Create an HTML element string, with safe escaping.
1087 * @param name The tag name.
1088 * @param attrs An object with members mapping element names to values
1089 * @param contents The contents of the element. May be either:
1090 * - string: The string is escaped.
1091 * - null or undefined: The short closing form is used, e.g. <br/>.
1092 * - this.Raw: The value attribute is included without escaping.
1093 * - this.Cdata: The value attribute is included, and an exception is
1094 * thrown if it contains an illegal ETAGO delimiter.
1095 * See http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2
1098 * var h = mediaWiki.html;
1099 * return h.element( 'div', {},
1100 * new h.Raw( h.element( 'img', {src: '<'} ) ) );
1101 * Returns <div><img src="<"/></div>
1103 this.element = function( name
, attrs
, contents
) {
1105 for ( attrName
in attrs
) {
1106 s
+= ' ' + attrName
+ '="' + this.escape( attrs
[attrName
] ) + '"';
1108 if ( typeof contents
== 'undefined' || contents
=== null ) {
1115 if (typeof contents
=== 'string') {
1117 s
+= this.escape( contents
);
1118 } else if ( contents
instanceof this.Raw
) {
1119 // Raw HTML inclusion
1120 s
+= contents
.value
;
1121 } else if ( contents
instanceof this.Cdata
) {
1123 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
1124 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
1126 s
+= contents
.value
;
1128 throw new Error( 'mw.html.element: Invalid type of contents' );
1130 s
+= '</' + name
+ '>';
1136 /* Extension points */
1142 /* Auto-register from pre-loaded startup scripts */
1144 if ( typeof startUp
=== 'function' ) {
1149 // Add jQuery Cookie to initial payload (used in mediaWiki.user)
1150 mediaWiki
.loader
.load( 'jquery.cookie' );
1152 // Alias $j to jQuery for backwards compatibility
1154 window
.mw
= mediaWiki
;