MediaWiki:Gadget-Descriptions.js: Difference between revisions

From Resistance in Belgium
No edit summary
No edit summary
Line 1: Line 1:
// required modules: mediawiki.api, jquery.wikibase.wbtooltip
// Required modules: mediawiki.api, jquery.wikibase.wbtooltip


( function ( mw, $, wb ) {
( function ( mw, $, wb ) {


// only works for items
    // only works for items
if ( !mw.config.exists( 'wbEntityId' ) ) {
    if ( !mw.config.exists( 'wbEntityId' ) ) {
return;
        return;
}
    }


/* T222491, hovercards were uninstalled
    function init() {
// disable when hovercards are activated
        console.log('Gadget is running');
if ( mw.user.options.get( 'popups' ) === "1" ) {
return;
}
*/


function init() {
        var api = new mw.Api(),
console.log('Gadget is running');
            userLang = mw.config.get( 'wgUserLanguage' ) || 'en',
var api = new mw.Api(),
            ids = [],
userLang = mw.config.get( 'wgUserLanguage' ) || 'en',
            anchors = {},
ids = [],
            $tooltip = $( '<span>' ).appendTo( 'body' );
anchors = {},
$tooltip = $( '<span>' )
.appendTo( 'body' );


$( '.wikibase-statementgroupview a[href^="/wiki/"]' )
        // Sélectionner tous les liens avec des entités dans les statements
.each( function () {
        $( '.wikibase-statementgroupview a[href^="/wiki/"]' ).each( function () {
var $anchor = $( this ),
            var $anchor = $( this ),
id = $anchor.attr( 'href' ).replace( /.*Item:(Q\d+)/, '$1' ).replace( /.*Property:(P\d+)/, '$1' );
                id = $anchor.attr( 'href' ).replace( /.*Item:(Q\d+)/, '$1' ).replace( /.*Property:(P\d+)/, '$1' );
if ( id.indexOf( 'Q' ) !== 0 && id.indexOf( 'P' ) !== 0 ) {
           
return;
            if ( id.indexOf( 'Q' ) !== 0 && id.indexOf( 'P' ) !== 0 ) {
}
                return;
            }


ids.push( id );
            ids.push( id );
if ( anchors[ id ] ) {
            if ( anchors[ id ] ) {
anchors[ id ].push( $anchor );
                anchors[ id ].push( $anchor );
} else {
            } else {
anchors[ id ] = [ $anchor ];
                anchors[ id ] = [ $anchor ];
}
            }
} );
        });


if ( ids.length === 0 ) {
        if ( ids.length === 0 ) {
return;
            console.log('No entity IDs found.');
}
            return;
        }


api.get( {
        // Appel à l'API pour obtenir les descriptions des entités
action: 'wbgetentities',
        api.get({
ids: ids,
            action: 'wbgetentities',
props: [ 'descriptions' ],
            ids: ids,
languages: userLang,
            props: [ 'descriptions' ],
languagefallback: 1,
            languages: userLang,
} )
            languagefallback: 1,
.then( function ( data ) {
        })
  console.log('API response for anonymous users:', data);
        .done( function ( data ) {
            console.log('API response for anonymous users:', data);


$.each( data.entities || {}, function ( id, entity ) {
            $.each( data.entities || {}, function ( id, entity ) {
if ( !entity.descriptions[ userLang ] || !anchors[ id ] ) {
                if ( !entity.descriptions[ userLang ] || !anchors[ id ] ) {
return;
                    return;
}
                }


$.each( anchors[ id ], function ( i, $anchor ) {
                // Appliquer les tooltips sur chaque lien
$anchor.hover( function () {
                $.each( anchors[ id ], function ( i, $anchor ) {
$tooltip
                    $anchor.hover( function () {
.wbtooltip( {
                        $tooltip.wbtooltip({
content: $( '<span>' ).text( entity.descriptions[ userLang ].value ),
                            content: $( '<span>' ).text( entity.descriptions[ userLang ].value ),
permanent: true,
                            permanent: true,
gravity: 'nw',
                            gravity: 'nw',
$anchor: $anchor
                            $anchor: $anchor
} )
                        }).data( 'wbtooltip' ).show();
.data( 'wbtooltip' ).show();
                    }, function () {
}, function () {
                        $tooltip.data( 'wbtooltip' ).hide();
$tooltip.data( 'wbtooltip' ).hide();
                    });
} );
                });
} );
            });
} );
        })
} );
        .fail( function ( error ) {
}
            console.log('Error fetching entity descriptions:', error);
        });
    }


$( init );
    // Charger les dépendances avant d'exécuter le script
    mw.loader.using(['mediawiki.api', 'jquery.wikibase.wbtooltip'], function () {
        $( init );  // Initialiser une fois les modules chargés
    });


} ( mediaWiki, jQuery, wikibase ) );
} ( mediaWiki, jQuery, wikibase ) );

Revision as of 20:40, 9 October 2024

// Required modules: mediawiki.api, jquery.wikibase.wbtooltip

( function ( mw, $, wb ) {

    // only works for items
    if ( !mw.config.exists( 'wbEntityId' ) ) {
        return;
    }

    function init() {
        console.log('Gadget is running');

        var api = new mw.Api(),
            userLang = mw.config.get( 'wgUserLanguage' ) || 'en',
            ids = [],
            anchors = {},
            $tooltip = $( '<span>' ).appendTo( 'body' );

        // Sélectionner tous les liens avec des entités dans les statements
        $( '.wikibase-statementgroupview a[href^="/wiki/"]' ).each( function () {
            var $anchor = $( this ),
                id = $anchor.attr( 'href' ).replace( /.*Item:(Q\d+)/, '$1' ).replace( /.*Property:(P\d+)/, '$1' );
            
            if ( id.indexOf( 'Q' ) !== 0 && id.indexOf( 'P' ) !== 0 ) {
                return;
            }

            ids.push( id );
            if ( anchors[ id ] ) {
                anchors[ id ].push( $anchor );
            } else {
                anchors[ id ] = [ $anchor ];
            }
        });

        if ( ids.length === 0 ) {
            console.log('No entity IDs found.');
            return;
        }

        // Appel à l'API pour obtenir les descriptions des entités
        api.get({
            action: 'wbgetentities',
            ids: ids,
            props: [ 'descriptions' ],
            languages: userLang,
            languagefallback: 1,
        })
        .done( function ( data ) {
            console.log('API response for anonymous users:', data);

            $.each( data.entities || {}, function ( id, entity ) {
                if ( !entity.descriptions[ userLang ] || !anchors[ id ] ) {
                    return;
                }

                // Appliquer les tooltips sur chaque lien
                $.each( anchors[ id ], function ( i, $anchor ) {
                    $anchor.hover( function () {
                        $tooltip.wbtooltip({
                            content: $( '<span>' ).text( entity.descriptions[ userLang ].value ),
                            permanent: true,
                            gravity: 'nw',
                            $anchor: $anchor
                        }).data( 'wbtooltip' ).show();
                    }, function () {
                        $tooltip.data( 'wbtooltip' ).hide();
                    });
                });
            });
        })
        .fail( function ( error ) {
            console.log('Error fetching entity descriptions:', error);
        });
    }

    // Charger les dépendances avant d'exécuter le script
    mw.loader.using(['mediawiki.api', 'jquery.wikibase.wbtooltip'], function () {
        $( init );  // Initialiser une fois les modules chargés
    });

} ( mediaWiki, jQuery, wikibase ) );