2 * Provides a {@link jQuery} plugins that manage spinners.
4 * To use these jQuery plugins, load the `jquery.spinner` module with {@link mw.loader}.
7 * mw.loader.using( 'jquery.spinner' ).then( () => {
8 * $( '#bodyContent' ).injectSpinner();
11 * @module jquery.spinner
16 * Default options for new spinners,
17 * stored outside the function to share between calls.
19 * @type {module:jquery.spinner~SpinnerOpts}
28 * @typedef {Object} module:jquery.spinner~SpinnerOpts Options for {@link module:jquery.spinner.$.fn.injectSpinner injectSpinner}.
29 * @property {string} [id] If given, spinner will be given an id of "mw-spinner-{id}".
30 * @property {'small'|'large'} [size='small'] 'small' or 'large' for a 20-pixel or 32-pixel spinner.
31 * @property {'inline'|'block'} [type='inline'] 'inline' or 'block'. Inline creates an inline-block with
32 * width and height equal to spinner size. Block is a block-level element with width 100%,
33 * height equal to spinner size.
38 * Create a spinner element
40 * The argument is an object with options used to construct the spinner (see below).
42 * It is a good practice to keep a reference to the created spinner to be able to remove it later.
43 * Alternatively, one can use the 'id' option and {@link module:jquery.spinner.removeSpinner removeSpinner}
44 * (but make sure to choose an id that's unlikely to cause conflicts, e.g. with extensions, gadgets or user scripts).
48 * - .mw-spinner for every spinner
49 * - .mw-spinner-small / .mw-spinner-large for size
50 * - .mw-spinner-block / .mw-spinner-inline for display types
53 * // Create a large spinner reserving all available horizontal space.
54 * const $spinner = $.createSpinner( { size: 'large', type: 'block' } );
55 * // Insert above page content.
56 * $( '#mw-content-text' ).prepend( $spinner );
58 * // Place a small inline spinner next to the "Save" button
59 * const $spinner = $.createSpinner( { size: 'small', type: 'inline' } );
60 * // Alternatively, just `$.createSpinner();` as these are the default options.
61 * $( '#wpSave' ).after( $spinner );
63 * // The following two are equivalent:
64 * $.createSpinner( 'magic' );
65 * $.createSpinner( { id: 'magic' } );
67 * @memberof module:jquery.spinner
70 * @param {module:jquery.spinner~SpinnerOpts|string} [opts] Options. If a string is given, it will be treated as the value
71 * of the {@link module:mediawiki.jqueryMsg~SpinnerOpts#id} option.
74 createSpinner: ( opts ) => {
75 if ( typeof opts === 'string' ) {
81 opts = Object.assign( {}, defaults, opts );
83 const $spinner = $( '<div>' ).addClass( 'mw-spinner' );
84 if ( opts.id !== undefined ) {
85 $spinner.attr( 'id', 'mw-spinner-' + opts.id );
89 .addClass( opts.size === 'large' ? 'mw-spinner-large' : 'mw-spinner-small' )
90 .addClass( opts.type === 'block' ? 'mw-spinner-block' : 'mw-spinner-inline' );
92 const $container = $( '<div>' ).addClass( 'mw-spinner-container' ).appendTo( $spinner );
93 for ( let i = 0; i < 12; i++ ) {
94 $container.append( $( '<div>' ) );
101 * Remove a spinner element
103 * @memberof module:jquery.spinner
105 * @param {string} id Id of the spinner, as passed to {@link module:jquery.spinner.createSpinner createSpinner}
106 * @return {jQuery} The (now detached) spinner element
108 removeSpinner: ( id ) => $( '#mw-spinner-' + id ).remove()
112 * Inject a spinner after each element in the collection.
114 * Inserts spinner as siblings (not children) of the target elements.
115 * Collection contents remain unchanged.
117 * @memberof module:jquery.spinner
118 * @param {module:jquery.spinner~SpinnerOpts|string} [opts] Options. If a string is given, it will be treated as the value
119 * of the {@link module:jquery.spinner~SpinnerOpts SpinnerOpts id} option.
122 $.fn.injectSpinner = function ( opts ) {
123 return this.after( $.createSpinner( opts ) );
128 * @mixes jQuery.plugin.spinner