User:JGirault (WMF)/common.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.
(function ( mw, $, module ) {

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

	if ( shouldExit ) {
		return;
	}

	module.dependencies = (function ( module ) {

		module.Helpers = (function ( module ) {

			module.GeoJSON = (function ( module ) {

				module.Point = (function ( mw, $ ) {
					var Feature = function () {
						this.latitude = null;
						this.longitude = null;
						this.props = {};
						this.customProps = {};
					};

					Feature.prototype.setLatLng = function ( latitude, longitude ) {
						this.latitude = latitude;
						this.longitude = longitude;
					};

					Feature.prototype.setTitle = function ( title ) {
						this._setProperty( 'title', title );
					};

					Feature.prototype.setDescription = function ( description ) {
						this._setProperty( 'description', description );
					};

					Feature.prototype.setMarkerColor = function ( hexcode ) {
						this._setProperty( 'marker-color', hexcode );
					};
					Feature.prototype.setMarkerSize = function ( size ) {
						this._setProperty( 'marker-size', size );
					};
					Feature.prototype.setMarkerSymbol = function ( symbol ) {
						this._setProperty( 'marker-symbol', symbol );
					};

					Feature.prototype.getProperties = function () {
						return $.extend( {}, this.props, this.customProps );
					};

					Feature.prototype._setProperty = function ( propName, propValue ) {
						this.customProps[ propName ] = propValue;
					}

					Feature.prototype._hasProperties = function () {
						return Object.keys( this.customProps ).length > 0 || Object.keys( this.props ).length > 0;
					};

					Feature.prototype.getGeoJSON = function () {
						var feature = this;
						var geoJson = {
							"type": "Feature",
							"geometry": {
								"type": "Point",
								"coordinates": [
									feature.longitude,
									feature.latitude
								]
							}
						};

						if ( this._hasProperties() ) {
							geoJson.properties = this.getProperties();
						}

						return geoJson;

					};

					return Feature;
				})( mediaWiki, jQuery );

				return module;

			})( {} );

			module.SinglePointMap = (function ( mw, $ ) {

				var Map = function ( container, latitude, longitude, options ) {
					this.container = container;
					this.latitude = latitude;
					this.longitude = longitude;
					this.options = options || {};
					this.map = null;
				};

				Map.prototype.draw = function () {
					var deferred = $.Deferred(),
						that = this;

					mw.loader.using( [ 'ext.kartographer.style', 'ext.kartographer.live' ], function () {

						var kartoLive = mw.loader.require( 'ext.kartographer.live' ),
							kartoLiveMap;

						kartoLiveMap = kartoLive.MWMap( that.container, {
							latitude: +that.latitude,
							longitude: +that.longitude,
							zoom: 11,
							// TODO: this should not be configured this way
							enableFullScreenButton: true,
							style: 'osm-intl',
							// TODO: this should not be required.
							maptagId: 0,
							// TODO: this should not be configured this way AT ALL.
							isMapframe: true
						} );

						// TODO: this should not be required

						// Bind the "ready" hook
						kartoLiveMap.ready( function ( map ) {

							var marker = new module.GeoJSON.Point();
							marker.setLatLng( that.latitude, that.longitude );
							if ( that.options.title ) {
								marker.setTitle( that.options.title );
							}
							if ( that.options.description ) {
								marker.setDescription( that.options.description );
							}
							marker.setMarkerColor( '3366cc' );

							kartoLiveMap.addDataLayer( marker.getGeoJSON() );
							deferred.resolve();
						} );
					} );

					return deferred.promise();
				};

				return Map;

			})( mediaWiki, jQuery, {} );


			module.Html = (function ( mw, $ ) {

				var Html = {
					createMapCard: function ( style ) {
						var computed;

						style = style || {};
						style.borderWidth = style.borderWidth || '1px';
						style.padding = style.padding || '.5em 1em';
						style.margin = style.margin || '.5em 1em';
						style.float = style.float || 'none';
						style.display = style.display || 'inline-block';

						computed = [
							'border: ' + style.borderWidth + ' solid #c8ccd1',
							'border-radius: 2px',
							'padding: ' + style.padding,
							'margin: ' + style.margin,
							'float: ' + style.float,
							'display: ' + style.display
						];

						return $( mw.html.element( 'div', {
							class: 'wd-map-card',
							style: computed.join( '; ' )
						} ) );
					},

					createMapContainer: function ( options ) {
						var computed;

						options = options || {};

						options.className = [ 'wd-map' ].concat( [ options.className || '' ] ).join( ' ' );
						options.width = options.width || '336px';
						options.height = options.height || '448px';

						computed = [
							'width: ' + options.width,
							'max-width: ' + options.width,
							'height: ' + options.height,
							'max-height: ' + options.height
						];

						return $( mw.html.element( 'div', {
							class: options.className,
							style: computed.join( '; ' )
						} ) );
					}
				};

				return Html;

			})( mediaWiki, jQuery, {} );

			return module;

		})( {} );

		return module;

	})( {} );


	module.scripts = (function ( module, dependencies ) {

		module.commonsHook = (function ( module, helpers ) {

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

				return {
					latitude: rawValue[ 0 ],
					longitude: rawValue[ 1 ],
					formattedCoords: formattedValue
				};
			}

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

				setTimeout( function () {

					var $snakviewValue,
						$mapCard,
						$map,
						map,
						mapData = {};

					$snakviewValue = $content.find( '.commons-file-information-table a.external' ).first();
					if (!$snakviewValue[0]) {
						return;
					}

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

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

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

					mapData = getMapData( $snakviewValue )

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

					map.draw();
				}, 0 );
			} );

			return module;

		})( {}, dependencies.Helpers );

		return module;

	})( {}, module.dependencies );

})( mediaWiki, jQuery, {} );