1,751 bytes added
, 4 years ago
// required modules: mediawiki.api, jquery.wikibase.wbtooltip
( function ( mw, $, wb ) {
// only works for items
if ( !mw.config.exists( 'wbEntityId' ) ) {
return;
}
/* T222491, hovercards were uninstalled
// disable when hovercards are activated
if ( mw.user.options.get( 'popups' ) === "1" ) {
return;
}
*/
function init() {
var api = new mw.Api(),
userLang = mw.config.get( 'wgUserLanguage' ),
ids = [],
anchors = {},
$tooltip = $( '<span>' )
.appendTo( 'body' );
$( '.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 ) {
return;
}
api.get( {
action: 'wbgetentities',
ids: ids,
props: [ 'descriptions' ],
languages: userLang,
languagefallback: 1,
} )
.then( function ( data ) {
$.each( data.entities || {}, function ( id, entity ) {
if ( !entity.descriptions[ userLang ] || !anchors[ id ] ) {
return;
}
$.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();
} );
} );
} );
} );
}
$( init );
} ( mediaWiki, jQuery, wikibase ) );