MediaWiki

Difference between revisions of "Common.js"

Line 34: Line 34:
 
//Add an audio player to the audio records links in the wikibase items
 
//Add an audio player to the audio records links in the wikibase items
 
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
 
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
$( '#P3 a.extiw' ).each( function() {
+
 
    var $node = $( this ),
+
function playButton( audioUrl ) {
        $audio = $( '<audio>' )
+
var button = new OO.ui.ButtonWidget( {
            .attr( 'src', BASE_FILE_URL + $node.text() )
+
framed: false,
             .attr( 'controls', true );
+
icon: 'play',
    $node.after( $audio );
+
title: 'play'
} );
+
} );
 +
button.on( 'click', function() {
 +
var audio = new Audio( audioUrl );
 +
audio.play();
 +
} );
 +
 
 +
return button.$element;
 +
}
 +
 
 +
if ( $( '#P3 a.extiw' ).length > 0 ) {
 +
    mw.loader.using( [ 'oojs-ui-widgets', 'oojs-ui.styles.icons-media' ], function() {
 +
        $( '#P3 a.extiw' ).each( function() {
 +
             var $node = $( this );
 +
            $node.before( playButton( BASE_FILE_URL + $node.text() ) );
 +
        } );
 +
    } );
 +
}

Revision as of 10:14, 26 May 2018

// Replace Wikidata IDs with their [label, description]
if ( $( '.wb-external-id' ).length > 0 ) {
	mw.loader.using( 'mediawiki.ForeignApi', function() {
		$( '.wb-external-id' ).each( function() {
			if ( $( this ).attr( 'href' ).lastIndexOf( 'https://www.wikidata.org', 0 ) === 0 ) {
				var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', {
						anonymous: true,
						parameters: { 'origin': '*' },
						ajax: { timeout: 10000 }
					} ),
					lang = mw.config.get( 'wgUserLanguage' ),
					node = $( this );
				wikidataApi.get( {
					'action': 'wbgetentities',
					'format': 'json',
					'ids': node.text(),
					'props': 'labels|descriptions',
					'languages': lang,
					'languagefallback': 1,
					'origin': '*'
				} ).then( function( data ) {
                                        
					var entity = data.entities[ node.text() ],
						label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ),
						description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' );
					
					node.html( label + '<br>' + description )
				} );
			}
		} );
	} );
}

//Add an audio player to the audio records links in the wikibase items
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';

function playButton( audioUrl ) {
	var button = new OO.ui.ButtonWidget( {
		framed: false,
		icon: 'play',
		title: 'play'
	} );
	button.on( 'click', function() {
		var audio = new Audio( audioUrl );
		audio.play();
	} );

	return button.$element;
}

if ( $( '#P3 a.extiw' ).length > 0 ) {
    mw.loader.using( [ 'oojs-ui-widgets', 'oojs-ui.styles.icons-media' ], function() {
        $( '#P3 a.extiw' ).each( function() {
            var $node = $( this );
            $node.before( playButton( BASE_FILE_URL + $node.text() ) );
        } );
    } );
}