User:Jgirault/Kartodeveloper/scripts/commons.js

(function ( mw, $ ) {

var shouldExit = mw.config.get( 'wgServer' ) !== '//commons.wikimedia.org' || mw.config.get( 'wgCanonicalNamespace' ) !== 'File';

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 $snakviewValue, $mapCard, $map, map, mapData = {};

$content.find( '.commons-file-information-table a.external' ).each( function {					var $this = $( this );					mapData = getMapData( $this ); 						if ( !mapData ) {						return;					} else {						$snakviewValue = $this;						return false;					}				}); if ( !$snakviewValue ) { return; }

$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-commons'				} );

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

map = new kartodeveloper.helpers.SinglePointMap(					$map[ 0 ],					mapData.latitude,					mapData.longitude,					{						title: 'Object location',						description: 'lat=' + mapData.latitude + ' | lon=' + mapData.longitude					}				);

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 );