Jump to content

User:Aude/nearby.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
( function ( mw, $ ) {

function setMessages() {
	switch ( mw.config.get( 'wgUserLanguage' ) ) {
		default:
		case 'en':
			mw.messages.set( {
				'gadget-nearby': 'Nearby'
			} );
			break;
	}
}

function init() {
	var action = mw.config.get( 'wgAction' ),
	    $indicators,
	    $nearbyEl;
	
	if ( action === 'view' ) {
		if ( mw.config.get( 'wgNamespaceNumber' ) != 0 ) {
			return;
		}
		
		if ( document.getElementById( 'coordinates' ) == null ) {
			return;
		}
		
		if ( mw.config.get( 'skin') == 'minerva' ) {
			$indicators = $( '#page-actions' );
			$nearbyEl = $( '<li>' );
		} else {
			$indicators = $( '.mw-indicators' ).first();
			$nearbyEl = $( '<div>' );
		}

		if ( $indicators.find( '#mw-indicator-nearby' ).length > 0 ) {
			return;
		}

		setMessages();

		var nearbyText = mw.msg( 'gadget-nearby' );

		$nearbyEl
			.addClass( 'mw-indicator mw-indicator-nearby' )
			.attr( {
				id: 'mw-indicator-nearby',
				title: nearbyText,
				alt: nearbyText
			} )
			.appendTo( $indicators )
			.click( function( e ) {
				var url =  window.location.origin + '/wiki/Special:Nearby#/page/' + mw.config.get( 'wgPageName' );
				window.location.replace( url );
			} );
	}
}

$( init );

}( mediaWiki, jQuery ) );