User:Jgirault/Kartodeveloper/scripts/wikidata.js

(function ( mw, $ ) {

var shouldExit = mw.config.get( 'wgServer' ) !== '//www.wikidata.org' || typeof mw.config.get( 'wbEntity' ) === 'undefined' || !mw.config.exists( 'wbEntityId' );

if ( shouldExit ) { return; }

mw.hook( 'wikipage.content' ).add( function ( $content ) {

mw.hook( 'wikipage.kartodeveloper' ).add( function ( kartodeveloper ) {			function parseGeohackUrl( url ) {				var matches = url.match( /params=([\-\+]?\d+\.?\d*)_N_([\-\+]?\d+\.?\d*)_E_/ );				if ( matches ) {					matches.shift;					return matches;				}			}

function getContentTitle( $content ) { return $content.find( '.wikibase-labelview-text' ).first.text; }

function getMapData( $snakviewValue ) {

var rawValue = parseGeohackUrl( $snakviewValue.attr( 'href' ) ); var formattedValue = $snakviewValue.text;

if ( !rawValue ) { return; }				return { latitude: rawValue[ 0 ], longitude: rawValue[ 1 ], formattedCoords: formattedValue };			}			setTimeout( function {

var $prop = $content.find( '#P625' ), $snakviewValue, $mapCard, $map, map, mapData = {};

if ( !$prop[ 0 ] ) { return; }

$snakviewValue = $prop.find( '.wikibase-statementview-mainsnak .wikibase-snakview-value a' );

$mapCard = kartodeveloper.helpers.HTML.createMapCard( {					padding: '0',					margin: '0 0 1em 0',					float: 'none',					display: 'block',					borderWidth: '0'				} );

$map = kartodeveloper.helpers.HTML.createMapContainer( {					width: '320px',					height: '240px',					className: 'wd-map-P625'				} );

$snakviewValue.before( $mapCard.append( $map ) );

$.extend( mapData, getMapData( $snakviewValue ), { title: getContentTitle( $content ) } )

map = new kartodeveloper.helpers.SinglePointMap(					$map[ 0 ],					mapData.latitude,					mapData.longitude,					{						title: mapData.title,						description: mapData.formattedCoords					}				);

map.draw; }, 0 );

} );	} );

mw.loader.load( 'https://www.mediawiki.org/w/index.php?title=User:Jgirault/Kartodeveloper.js&action=raw&ctype=text/javascript' ); })( mediaWiki, jQuery );