User:Jgirault/Kartodeveloper/scripts/search.js

(function ( mw, $ ) {

var shouldExit = !(mw.config.get( 'searchTerm' ));

if ( shouldExit ) { return; }

mw.hook( 'wikipage.content' ).add( function ( $content ) {		mw.hook( 'wikipage.kartodeveloper' ).add( function ( kartodeveloper ) { $.ajax( {				dataType: 'jsonp',				url: '/w/api.php?action=query&format=json&prop=coordinates&generator=search&gsrsearch=' + encodeURIComponent( mw.config.get( 'searchTerm' ) ),				error: function ( jqXHR, textStatus, errorThrown ) {					console.log( 'error:', arguments );				},				success: function ( response ) {					var key, marker, items;					if ( !response.hasOwnProperty( 'query' ) ) {						// nothing found;						return;					}					items = response[ 'query' ][ 'pages' ];					var markers = [];					for ( key in items ) {						markers[ items[ key ].index - 1 ] = [							items[ key ][ 'coordinates' ][ 0 ].lat,							items[ key ][ 'coordinates' ][ 0 ].lon,							items[ key ].title						];					}					var $mapCard,						$map,						map;					$mapCard = kartodeveloper.helpers.HTML.createMapCard( { padding: '0', margin: '0 0 1em 0', float: 'none', display: 'block', borderWidth: '0' } );					$map = kartodeveloper.helpers.HTML.createMapContainer( { width: '100%', height: '180px', className: 'wp-search-results-map' } );					$content.find( 'div.searchresults' ).prepend( $mapCard.append( $map ) );					map = new kartodeveloper.helpers.MultiplePointMap( $map[ 0 ], markers );					map.draw;				}			} ); } );	} );

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