« MediaWiki:Common.js » : différence entre les versions

De Wikiromandie.org
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
performance
 
(6 versions intermédiaires par le même utilisateur non affichées)
Ligne 17 : Ligne 17 :
/* jshint strict:false, browser:true */
/* jshint strict:false, browser:true */


mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
// Modern ES6+ syntax is standard across all MediaWiki supported browsers
/* Begin of mw.loader.using callback */
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
    /* Begin of mw.loader.using callback */


/**
    /**
* Map addPortletLink to mw.util
    * Map addPortletLink to mw.util
* @deprecated: Use mw.util.addPortletLink instead.
    * @deprecated
*/
    */
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
    mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );


/**
    /**
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
    * Load JS and CSS by URL
* @rev 6
    */
*/
    var extraCSS = mw.util.getParamValue( 'withCSS' ),
var extraCSS = mw.util.getParamValue( 'withCSS' ),
        extraJS = mw.util.getParamValue( 'withJS' );
extraJS = mw.util.getParamValue( 'withJS' );


if ( extraCSS ) {
    if ( extraCSS ) {
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
        if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
            mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
} else {
        } else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
            mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
        }
}
    }


if ( extraJS ) {
    if ( extraJS ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
        if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
            mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
} else {
        } else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
            mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
        }
}
    }


/**
    /**
* Collapsible tables; reimplemented with mw-collapsible
    * Collapsible tables (shim for legacy .collapsible)
* Styling is also in place to avoid FOUC
    */
*
    mw.hook( 'wikipage.content' ).add( function ( $content ) {
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
        var $tables = $content.find( 'table.collapsible:not(.mw-collapsible)' );
* @version 3.0.0 (2018-05-20)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @author [[User:TheDJ]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core. Shimmable since MediaWiki 1.32
*
* @param {jQuery} $content
*/
function makeCollapsibleMwCollapsible( $content ) {
var $tables = $content
.find( 'table.collapsible:not(.mw-collapsible)' )
.addClass( 'mw-collapsible' );


$.each( $tables, function ( index, table ) {
        if ( !$tables.length ) { return; }
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
if ( $( table ).hasClass( 'collapsed' ) ) {
$( table ).addClass( 'mw-collapsed' );
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
}
} );
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );


/**
        $tables.addClass( 'mw-collapsible' ).each( function () {
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
            if ( $( this ).hasClass( 'collapsed' ) ) {
*
                $( this ).addClass( 'mw-collapsed' );
* Maintainers: TheDJ
            }
*/
        } );
function mwCollapsibleSetup( $collapsibleContent ) {
var $element,
$toggle,
autoCollapseThreshold = 2;
$.each( $collapsibleContent, function ( index, element ) {
$element = $( element );
if ( $element.hasClass( 'collapsible' ) ) {
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
}
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) ) {
if ( $element.parents( '.outercollapse' ).length > 0 ) {
$element.data( 'mw-collapsible' ).collapse();
}
}
// because of colored backgrounds, style the link in the text color
// to ensure accessible contrast
$toggle = $element.find( '.mw-collapsible-toggle' );
if ( $toggle.length ) {
// Make the toggle inherit text color (Updated for T333357 2023-04-29)
if ( $toggle.parent()[ 0 ].style.color ) {
$toggle.css( 'color', 'inherit' );
$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
}
}
} );
}


mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
        mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
            $tables.makeCollapsible();
        } );
    } );


/* End of mw.loader.using callback */
    /**
/* Wikidata SPARQL tables */
    * Support autocollapse, innercollapse, outercollapse
mw.hook( 'wikipage.content' ).add( function () {
    */
document.querySelectorAll( '.wikidata-sparql-table:not([data-loaded])' ).forEach( function ( el ) {
    mw.hook( 'wikipage.collapsibleContent' ).add( function ( $collapsibleContent ) {
el.setAttribute( 'data-loaded', '1' );
        var autoCollapseThreshold = 2,
var sparql = el.getAttribute( 'data-sparql' );
            hasOuter = $collapsibleContent.parents( '.outercollapse' ).length > 0;
var title  = el.getAttribute( 'data-title' );


el.textContent = '⏳ Loading…';
        $collapsibleContent.each( function () {
            var $element = $( this );


fetch( 'https://query.wikidata.org/sparql?format=json&query=' + encodeURIComponent( sparql ),
            if ( $element.hasClass( 'collapsible' ) ) {
{ headers: { Accept: 'application/json' } } )
                $element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
.then( function ( r ) { return r.json(); } )
            }
.then( function ( data ) {
var vars = data.head.vars;
var rows = data.results.bindings;
var html = '';


if ( title ) html += '<h3>' + title + '</h3>';
            if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
html += '<table class="wikitable sortable"><thead><tr>';
                $element.data( 'mw-collapsible' ).collapse();
vars.forEach( function ( v ) { html += '<th>' + v + '</th>'; } );
            } else if ( $element.hasClass( 'innercollapse' ) && hasOuter ) {
html += '</tr></thead><tbody>';
                $element.data( 'mw-collapsible' ).collapse();
rows.forEach( function ( row ) {
            }
html += '<tr>';
vars.forEach( function ( v ) {
var val = row[ v ] ? row[ v ].value : '';


if ( /^https?:\/\/.*\.(jpg|jpeg|png|svg|gif|webp)$/i.test( val ) ) {
            var $toggle = $element.find( '.mw-collapsible-toggle' );
// Wikimedia images: use the thumbnail API
            if ( $toggle.length && $toggle.parent()[0].style.color ) {
var thumb = val.replace(
                $toggle.css( 'color', 'inherit' ).find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
'https://commons.wikimedia.org/wiki/Special:FilePath/',
            }
'https://commons.wikimedia.org/wiki/Special:FilePath/'
        } );
);
    } );
html += '<td><img src="' + thumb + '?width=100" style="max-height:80px;" /></td>';
 
} else {
    /* End of mw.loader.using callback */
val = val.replace( /https?:\/\/www\.wikidata\.org\/entity\/(Q\d+)/,
 
'<a href="https://www.wikidata.org/wiki/$1">$1</a>' );
    /**
html += '<td>' + val + '</td>';
    * Wikidata SPARQL tables rendering
}
    */
} );
    var WIKIDATA_REGEX = /https?:\/\/www\.wikidata\.org\/entity\/(Q\d+)/g,
html += '</tr>';
        COMMONS_REGEX  = /^https?:\/\/commons\.wikimedia\.org\/wiki\/Special:FilePath\//i;
} );
 
html += '</tbody></table>';
    mw.hook( 'wikipage.content' ).add( function ( $content ) {
el.innerHTML = html;
        var tables = ($content ? $content[0] : document).querySelectorAll( '.wikidata-sparql-table:not([data-loaded])' );
} )
        if ( !tables.length ) { return; }
.catch( function () { el.textContent = '⚠️ Failed to load.'; } );
 
} );
        tables.forEach( function ( el ) {
} );
            el.setAttribute( 'data-loaded', '1' );
            var sparql = el.getAttribute( 'data-sparql' ),
                title  = el.getAttribute( 'data-title' );
 
            if ( !sparql ) { return; }
            el.textContent = '⏳ Loading…';
 
            fetch( 'https://query.wikidata.org/sparql?format=json&query=' + encodeURIComponent( sparql ), {
                headers: { Accept: 'application/json' }
            } )
            .then( function ( r ) { return r.json(); } )
            .then( function ( data ) {
                var vars = data.head.vars,
                    rows = data.results.bindings,
                    html = [],
                    vLen = vars.length;
 
                if ( title ) {
                    html.push( '<h3>', title, '</h3>' );
                }
 
                html.push( '<table class="wikitable sortable"><thead><tr>' );
                for ( var i = 0; i < vLen; i++ ) {
                    html.push( '<th>', vars[i].replace( /_/g, ' ' ), '<th>' );
                }
                html.push( '</tr></thead><tbody>' );
 
                for ( var r = 0, rLen = rows.length; r < rLen; r++ ) {
                    var row = rows[r];
                    html.push( '<tr>' );
                    for ( var c = 0; c < vLen; c++ ) {
                        var v = vars[c],
                            val = row[v] ? row[v].value : '';
 
                        if ( COMMONS_REGEX.test( val ) ) {
                            var thumb = val.replace( /^http:\/\//, 'https://' );
                            html.push( '<td><img src="', thumb, '?width=100" style="max-height:80px;" /></td>' );
                        } else {
                            val = val.replace( WIKIDATA_REGEX, '<a href="https://www.wikidata.org/wiki/$1" target="_blank" rel="noopener">$1</a>' );
                            html.push( '<td>', val, '</td>' );
                        }
                    }
                    html.push( '</tr>' );
                }
 
                html.push( '</tbody></table>' );
                el.innerHTML = html.join('');
 
                // Trigger MediaWiki table sorting module if loaded
                mw.hook( 'wikipage.content' ).fire( $( el ) );
            } )
            .catch( function () {
                el.textContent = '⚠️ Failed to load.';
            } );
        } );
    } );
} );
} );
/* DO NOT ADD CODE BELOW THIS LINE */
/* DO NOT ADD CODE BELOW THIS LINE */

Dernière version du 20 septembre 2026 à 16:10

/* Tout JavaScript présent ici sera exécuté par tous les utilisateurs à chaque chargement de page. */

/**
 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
 * loaded for all users on every wiki page. If possible create a gadget that is
 * enabled by default instead of adding it here (since gadgets are fully
 * optimized ResourceLoader modules with possibility to add dependencies etc.)
 *
 * Since Common.js isn't a gadget, there is no place to declare its
 * dependencies, so we have to lazy load them with mw.loader.using on demand and
 * then execute the rest in the callback. In most cases these dependencies will
 * be loaded (or loading) already and the callback will not be delayed. In case a
 * dependency hasn't arrived yet it'll make sure those are loaded before this.
 */

/* global mw, $ */
/* jshint strict:false, browser:true */

// Modern ES6+ syntax is standard across all MediaWiki supported browsers
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
    /* Begin of mw.loader.using callback */

    /**
     * Map addPortletLink to mw.util
     * @deprecated
     */
    mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );

    /**
     * Load JS and CSS by URL
     */
    var extraCSS = mw.util.getParamValue( 'withCSS' ),
        extraJS  = mw.util.getParamValue( 'withJS' );

    if ( extraCSS ) {
        if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
            mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
        } else {
            mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
        }
    }

    if ( extraJS ) {
        if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
            mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
        } else {
            mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
        }
    }

    /**
     * Collapsible tables (shim for legacy .collapsible)
     */
    mw.hook( 'wikipage.content' ).add( function ( $content ) {
        var $tables = $content.find( 'table.collapsible:not(.mw-collapsible)' );

        if ( !$tables.length ) { return; }

        $tables.addClass( 'mw-collapsible' ).each( function () {
            if ( $( this ).hasClass( 'collapsed' ) ) {
                $( this ).addClass( 'mw-collapsed' );
            }
        } );

        mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
            $tables.makeCollapsible();
        } );
    } );

    /**
     * Support autocollapse, innercollapse, outercollapse
     */
    mw.hook( 'wikipage.collapsibleContent' ).add( function ( $collapsibleContent ) {
        var autoCollapseThreshold = 2,
            hasOuter = $collapsibleContent.parents( '.outercollapse' ).length > 0;

        $collapsibleContent.each( function () {
            var $element = $( this );

            if ( $element.hasClass( 'collapsible' ) ) {
                $element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
            }

            if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
                $element.data( 'mw-collapsible' ).collapse();
            } else if ( $element.hasClass( 'innercollapse' ) && hasOuter ) {
                $element.data( 'mw-collapsible' ).collapse();
            }

            var $toggle = $element.find( '.mw-collapsible-toggle' );
            if ( $toggle.length && $toggle.parent()[0].style.color ) {
                $toggle.css( 'color', 'inherit' ).find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
            }
        } );
    } );

    /* End of mw.loader.using callback */

    /**
     * Wikidata SPARQL tables rendering
     */
    var WIKIDATA_REGEX = /https?:\/\/www\.wikidata\.org\/entity\/(Q\d+)/g,
        COMMONS_REGEX  = /^https?:\/\/commons\.wikimedia\.org\/wiki\/Special:FilePath\//i;

    mw.hook( 'wikipage.content' ).add( function ( $content ) {
        var tables = ($content ? $content[0] : document).querySelectorAll( '.wikidata-sparql-table:not([data-loaded])' );
        if ( !tables.length ) { return; }

        tables.forEach( function ( el ) {
            el.setAttribute( 'data-loaded', '1' );
            var sparql = el.getAttribute( 'data-sparql' ),
                title  = el.getAttribute( 'data-title' );

            if ( !sparql ) { return; }
            el.textContent = '⏳ Loading…';

            fetch( 'https://query.wikidata.org/sparql?format=json&query=' + encodeURIComponent( sparql ), {
                headers: { Accept: 'application/json' }
            } )
            .then( function ( r ) { return r.json(); } )
            .then( function ( data ) {
                var vars = data.head.vars,
                    rows = data.results.bindings,
                    html = [],
                    vLen = vars.length;

                if ( title ) {
                    html.push( '<h3>', title, '</h3>' );
                }

                html.push( '<table class="wikitable sortable"><thead><tr>' );
                for ( var i = 0; i < vLen; i++ ) {
                    html.push( '<th>', vars[i].replace( /_/g, ' ' ), '<th>' );
                }
                html.push( '</tr></thead><tbody>' );

                for ( var r = 0, rLen = rows.length; r < rLen; r++ ) {
                    var row = rows[r];
                    html.push( '<tr>' );
                    for ( var c = 0; c < vLen; c++ ) {
                        var v = vars[c],
                            val = row[v] ? row[v].value : '';

                        if ( COMMONS_REGEX.test( val ) ) {
                            var thumb = val.replace( /^http:\/\//, 'https://' );
                            html.push( '<td><img src="', thumb, '?width=100" style="max-height:80px;" /></td>' );
                        } else {
                            val = val.replace( WIKIDATA_REGEX, '<a href="https://www.wikidata.org/wiki/$1" target="_blank" rel="noopener">$1</a>' );
                            html.push( '<td>', val, '</td>' );
                        }
                    }
                    html.push( '</tr>' );
                }

                html.push( '</tbody></table>' );
                el.innerHTML = html.join('');

                // Trigger MediaWiki table sorting module if loaded
                mw.hook( 'wikipage.content' ).fire( $( el ) );
            } )
            .catch( function () {
                el.textContent = '⚠️ Failed to load.';
            } );
        } );
    } );
} );

/* DO NOT ADD CODE BELOW THIS LINE */