MediaWiki:Geocode Users.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// <nowiki>
(function($, mw) {

	if (!/^Commons:Geocode_Users/.test(mw.config.get('wgPageName'))) return;

	// Register dependency
	if (null === mw.loader.getState('mediawiki.commons.geoedit')) mw.loader.implement('mediawiki.commons.geoedit', [
				"//commons.wikimedia.org/w/index.php?title=MediaWiki:GeoEdit.js&action=raw&ctype=text/javascript"
		], { /*no styles*/ }, { /*no messages*/ });

	var $geocodeusermap = $('#geocodeusermap'),
		$geoEdit;

	if (0 === $geocodeusermap.length) return;


	var to,
		$input,
		__geoValueChanged = function(d) {
			var val = '{{User location|' +
				d.topleft.lat + '|' +
				d.topleft.lon +
				'}}';

			$input.val(val);
		},
		_geoValueChanged = function(e, d) {
			clearTimeout(to);
			to = setTimeout(function() {
				__geoValueChanged(d);
			}, 50);
		};


	mw.loader.using(['mediawiki.commons.geoedit'], function() {
		$geocodeusermap.text('').css('border', 'none');
		$input = $('<input type="text" size="100" readonly="readonly"></input>').click(function() {
			$(this).select();
		}).appendTo($geocodeusermap);
		$geoEdit = mw.libs.geoMarker
			.$getUI({
				type: 'marker'
			})
			.init()
			.appendTo($geocodeusermap)
			.on('geoValue', _geoValueChanged);
	});

}(jQuery, mediaWiki));
// </nowiki>