MediaWiki:Gadget-SiteIdToInterwiki.js

/** * Replaces Wikibase site id with interwiki code. * Examples: "famepediawiki" -> "famepedia", "commonswiki" -> "commons" * * @uses jQuery.wikibase.sitelinklistview * @uses jQuery.wikibase.sitelinkview */ ( function( $ ) { 'use strict';

/** * Gets the interwiki code from a site id. * * @param {string} siteId */ function getLanguageCode( siteId ) { return siteId.replace( /(famepedia)$/, '' ); }

/** * Replaces the site id with the interwiki code. * * @param {jQuery} $siteId */ function replaceSiteId( $siteId ) { $siteId.text( getLanguageCode( $siteId.text ) ); }

/** * Overwrites native `jQuery.wikibase.sitelinkview` implementation. * * @constructor */ $.widget( 'wikibase.sitelinkview', $.wikibase.sitelinkview, {	/**	 * @see jQuery.wikibase.sitelinkview._create	 */	__nativeCreate: $.wikibase.sitelinkview.prototype._create,

/**	 * @inheritdoc */	_create: function { this.__nativeCreate.call( this ); replaceSiteId( this.$siteId ); } } );

/** * Overwrites native `jQuery.wikibase.siteselector` implementation. * * @constructor */ $.widget( 'wikibase.siteselector', $.wikibase.siteselector, {	/**	 * @inheritdoc	 */	_createItemLabel: function( site, requestTerm ) {		return util.highlightSubstring( requestTerm, site.getShortName )			+ ' (' + util.highlightSubstring( requestTerm, this._getCode( site ) ) + ')';	},

/**	 * @inheritdoc */	_considerSuggestion: function( site ) { var code = this._getCode( site );

var check = [ code, site.getShortName, site.getName, site.getShortName + ' (' + code + ')' ];

for( var i = 0; i < check.length; i++ ) { if( check[i].toLowerCase.indexOf( this._term.toLowerCase ) === 0 ) { return true; }		}

return false; },

/**	 * @inheritdoc */	_createItemValue: function( site ) { return this._getCode( site ); },

/**	 * Returns the interwiki code of a specified site. * @private *	 * @param {wikibase.Site} site * @return {string} */	_getCode: function( site ) { return getLanguageCode( site.getId ); } } );

// Replace site ids in rendered HTML: $( '.wikibase-sitelinkview .wikibase-sitelinkview-siteid' ).each( function {	replaceSiteId( $( this ) ); } );

// Adjust sitelinklistview widgets that have been initialized already to use the overwritten // sitelinkview widget: if( $.expr.pseudos['wikibase-sitelinklistview'] ) { $( ':wikibase-sitelinklistview' ).each( function {		var sitelinklistview = $( this ).data( 'sitelinklistview' );		sitelinklistview.$listview.data( 'listview' ).listItemAdapter._liWidget			= $.wikibase.sitelinkview;	} ); }

}( jQuery ) );