jenkins-bot has submitted this change and it was merged. Change subject: Use Extension:GeoData coordinates as default if available ......................................................................
Use Extension:GeoData coordinates as default if available Also move lat/lng/zoom validation to setView so we only have to do it in once place. Change-Id: Iae535369cd93c3764d3fe1ce6714c7c8b1167f19 --- M modules/box/Map.js M modules/mapframe/mapframe.js M modules/maplink/maplink.js M modules/ve-maps/ve.ce.MWMapsNode.js M modules/ve-maps/ve.ui.MWMapsDialog.js 5 files changed, 65 insertions(+), 168 deletions(-) Approvals: JGirault: Looks good to me, approved jenkins-bot: Verified diff --git a/modules/box/Map.js b/modules/box/Map.js index 1cad6aa..1fc520f 100644 --- a/modules/box/Map.js +++ b/modules/box/Map.js @@ -350,7 +350,7 @@ * Sets the initial center and zoom of the map, and optionally calls * {@link #setView} to reposition the map. * - * @param {L.LatLng|Array} [center] + * @param {L.LatLng|number[]} [center] * @param {number} [zoom] * @param {boolean} [setView=false] * @chainable @@ -358,9 +358,15 @@ initView: function ( center, zoom, setView ) { setView = setView === false ? false : true; - if ( center ) { - center = L.latLng( center ); + if ( Array.isArray( center ) ) { + if ( !isNaN( center[ 0 ] ) && !isNaN( center[ 1 ] ) ) { + center = L.latLng( center ); + } else { + center = undefined; + } } + + zoom = isNaN( zoom ) ? this.options.fallbackZoom : zoom; this._initialPosition = { center: center, zoom: zoom @@ -559,7 +565,7 @@ * fourth parameter to decide whether to update the container's data * attribute with the calculated values (for performance). * - * @param {L.LatLng|Array|string} [center] Map center. + * @param {L.LatLng|number[]|string} [center] Map center. * @param {number} [zoom] * @param {Object} [options] See [L.Map#setView](https://www.mapbox.com/mapbox.js/api/v2.3.0/l-map-class/) * documentation for the full list of options. @@ -570,8 +576,14 @@ var maxBounds, initial = this.getInitialMapPosition(); + if ( Array.isArray( center ) ) { + if ( !isNaN( center[ 0 ] ) && !isNaN( center[ 1 ] ) ) { + center = L.latLng( center ); + } else { + center = undefined; + } + } if ( center ) { - center = L.latLng( center ); zoom = isNaN( zoom ) ? this.options.fallbackZoom : zoom; L.Map.prototype.setView.call( this, center, zoom, options ); } else { diff --git a/modules/mapframe/mapframe.js b/modules/mapframe/mapframe.js index 1a40c51..df683c8 100644 --- a/modules/mapframe/mapframe.js +++ b/modules/mapframe/mapframe.js @@ -52,38 +52,6 @@ } /** - * Formats center if valid. - * - * @param {string|number} latitude - * @param {string|number} longitude - * @return {Array|undefined} - * @private - */ - function validCenter( latitude, longitude ) { - latitude = +latitude; - longitude = +longitude; - - if ( !isNaN( latitude ) && !isNaN( longitude ) ) { - return [ latitude, longitude ]; - } - } - - /** - * Formats zoom if valid. - * - * @param {string|number} zoom - * @return {number|undefined} - * @private - */ - function validZoom( zoom ) { - zoom = +zoom; - - if ( !isNaN( zoom ) ) { - return zoom; - } - } - - /** * This code will be executed once the article is rendered and ready. * * @ignore @@ -111,8 +79,8 @@ map = kartobox.map( { container: container, - center: validCenter( data.latitude, data.longitude ), - zoom: validZoom( data.zoom ), + center: [ data.latitude, data.longitude ], + zoom: data.zoom, fullScreenRoute: '/map/' + index, allowFullScreen: true, dataGroups: data.overlays @@ -160,8 +128,8 @@ } map.openFullScreen( { - center: validCenter( latitude, longitude ), - zoom: validZoom( zoom ) + center: [ +latitude, +longitude ], + zoom: +zoom } ); } ); diff --git a/modules/maplink/maplink.js b/modules/maplink/maplink.js index 6068e08..8f1361d 100644 --- a/modules/maplink/maplink.js +++ b/modules/maplink/maplink.js @@ -52,38 +52,6 @@ } /** - * Formats center if valid. - * - * @param {string|number} latitude - * @param {string|number} longitude - * @return {Array|undefined} - * @private - */ - function validCenter( latitude, longitude ) { - latitude = +latitude; - longitude = +longitude; - - if ( !isNaN( latitude ) && !isNaN( longitude ) ) { - return [ latitude, longitude ]; - } - } - - /** - * Formats zoom if valid. - * - * @param {string|number} zoom - * @return {number|undefined} - * @private - */ - function validZoom( zoom ) { - zoom = +zoom; - - if ( !isNaN( zoom ) ) { - return zoom; - } - } - - /** * This code will be executed once the article is rendered and ready. * * @ignore @@ -103,8 +71,8 @@ maplinks[ index ] = kartobox.link( { container: this, - center: data.latitude && data.latitude ? [ data.latitude, data.longitude ] : 'auto', - zoom: data.zoom || 'auto', + center: [ data.latitude, data.longitude ], + zoom: data.zoom, dataGroups: data.overlays, fullScreenRoute: '/maplink/' + index } ); @@ -130,8 +98,8 @@ } link.openFullScreen( { - center: validCenter( latitude, longitude ), - zoom: validZoom( zoom ) + center: [ +latitude, +longitude ], + zoom: +zoom } ); } ); diff --git a/modules/ve-maps/ve.ce.MWMapsNode.js b/modules/ve-maps/ve.ce.MWMapsNode.js index b0632dc..b96ee5d 100644 --- a/modules/ve-maps/ve.ce.MWMapsNode.js +++ b/modules/ve-maps/ve.ce.MWMapsNode.js @@ -74,7 +74,7 @@ ve.ce.MWMapsNode.prototype.requiresInteractive = function () { var mwData = this.model.getAttribute( 'mw' ); - return mwData.body.extsrc; + return mwData.body.extsrc || isNaN( mwData.attrs.latitude ) || isNaN( mwData.attrs.zoom ); }; /** @@ -133,37 +133,6 @@ .addClass( alignClasses[ align ] ) .css( this.model.getCurrentDimensions() ); }; -/** - * Formats center if valid. - * - * @param {string|number} latitude - * @param {string|number} longitude - * @return {Array|undefined} - * @private - */ -function validCenter( latitude, longitude ) { - latitude = +latitude; - longitude = +longitude; - - if ( !isNaN( latitude ) && !isNaN( longitude ) ) { - return [ latitude, longitude ]; - } -} - -/** - * Formats zoom if valid. - * - * @param {string|number} zoom - * @return {number|undefined} - * @private - */ -function validZoom( zoom ) { - zoom = +zoom; - - if ( !isNaN( zoom ) ) { - return zoom; - } -} /** * Setup an interactive map @@ -171,18 +140,16 @@ ve.ce.MWMapsNode.prototype.setupMap = function () { var mwData = this.model.getAttribute( 'mw' ), mwAttrs = mwData && mwData.attrs, - zoom = validZoom( +mwAttrs.zoom ), - center = validCenter( mwAttrs.latitude, mwAttrs.longitude ), node = this; - node.map = kartobox.map( { - container: node.$element[ 0 ], - center: center, - zoom: zoom + this.map = kartobox.map( { + container: this.$element[ 0 ], + center: [ +mwAttrs.latitude, +mwAttrs.longitude ], + zoom: +mwAttrs.zoom // TODO: Support style editing } ); - node.map.on( 'layeradd', node.updateMapPosition, node ); - node.map.doWhenReady( function ( ) { + this.map.on( 'layeradd', this.updateMapPosition, this ); + this.map.doWhenReady( function ( ) { node.updateGeoJson(); // Disable interaction @@ -202,7 +169,7 @@ geoJson = mwData && mwData.body.extsrc; if ( geoJson !== this.geoJson ) { - kartoEditing.updateKartographerLayer( this.map, mwData && mwData.body.extsrc ); + kartoEditing.updateKartographerLayer( this.map, mwData && mwData.body.extsrc ).then( this.updateMapPosition.bind( this ) ); this.geoJson = geoJson; } }; @@ -216,7 +183,7 @@ updatedData = mwData && mwData.attrs, current; - if ( !validCenter( mapData.latitude, mapData.longitude ) || !updatedData ) { + if ( !updatedData ) { // auto calculate the position this.map.setView( null, mapData.zoom ); current = this.map.getMapPosition(); @@ -225,6 +192,7 @@ mwData.attrs.longitude = mapData.longitude = current.center.lng.toString(); mwData.attrs.zoom = mapData.zoom = current.zoom.toString(); } else if ( + isNaN( updatedData.latitude ) || isNaN( updatedData.longitude ) || isNaN( updatedData.zoom ) || mapData.latitude !== updatedData.latitude || mapData.longitude !== updatedData.longitude || mapData.zoom !== updatedData.zoom @@ -290,7 +258,10 @@ * Handle focus events */ ve.ce.MWMapsNode.prototype.onMapFocus = function () { - $( '<img>' ).attr( 'src', this.model.getUrl( 1000, 1000 ) ); + if ( !this.requiresInteractive() ) { + // Preload larger static map for resizing + $( '<img>' ).attr( 'src', this.model.getUrl( 1000, 1000 ) ); + } }; /* Registration */ diff --git a/modules/ve-maps/ve.ui.MWMapsDialog.js b/modules/ve-maps/ve.ui.MWMapsDialog.js index 3269f33..c0c781c 100644 --- a/modules/ve-maps/ve.ui.MWMapsDialog.js +++ b/modules/ve-maps/ve.ui.MWMapsDialog.js @@ -123,7 +123,7 @@ this.updateSize(); if ( center ) { - this.map.setView( center ); + this.map.setView( center, this.map.getZoom() ); } this.map.invalidateSize(); this.updateActions(); @@ -304,19 +304,13 @@ // if geojson and no center, we need the map to automatically // position itself when the feature layer is added. - if ( dialog.input.getValue() && !mapPosition.center ) { + if ( + dialog.input.getValue() && + ( !mapPosition.center || isNaN( mapPosition.center[ 0 ] ) || isNaN( mapPosition.center[ 1 ] ) ) + ) { dialog.map.on( 'layeradd', function () { - var mwData = dialog.selectedNode && dialog.selectedNode.getAttribute( 'mw' ), - mwAttrs = mwData && mwData.attrs || {}, - position; - dialog.map.setView( null, mapPosition.zoom ); - position = dialog.map.getMapPosition(); - - // update attributes with current position - mwAttrs.latitude = position.center.lat; - mwAttrs.longitude = position.center.lng; - mwAttrs.zoom = position.zoom; + dialog.updateActions(); } ); } @@ -357,37 +351,6 @@ } ); } ); }; -/** - * Formats center if valid. - * - * @param {string|number} latitude - * @param {string|number} longitude - * @return {Array|undefined} - * @private - */ -function validCenter( latitude, longitude ) { - latitude = +latitude; - longitude = +longitude; - - if ( !isNaN( latitude ) && !isNaN( longitude ) ) { - return [ latitude, longitude ]; - } -} - -/** - * Formats zoom if valid. - * - * @param {string|number} zoom - * @return {number|undefined} - * @private - */ -function validZoom( zoom ) { - zoom = +zoom; - - if ( !isNaN( zoom ) ) { - return zoom; - } -} /** * Get the initial map position (coordinates and zoom level) @@ -395,13 +358,28 @@ * @return {Object} Object containing latitude, longitude and zoom */ ve.ui.MWMapsDialog.prototype.getInitialMapPosition = function () { - var mwData = this.selectedNode && this.selectedNode.getAttribute( 'mw' ), - mwAttrs = mwData && mwData.attrs || {}, - center = validCenter( mwAttrs.latitude, mwAttrs.longitude ), - zoom = validZoom( mwAttrs.zoom ); + var latitude, longitude, zoom, + pageCoords = mw.config.get( 'wgCoordinates' ), + mwData = this.selectedNode && this.selectedNode.getAttribute( 'mw' ), + mwAttrs = mwData && mwData.attrs; + + if ( mwAttrs && mwAttrs.zoom ) { + latitude = +mwAttrs.latitude; + longitude = +mwAttrs.longitude; + zoom = +mwAttrs.zoom; + } else if ( pageCoords ) { + // Use page coordinates if Extension:GeoData is available + latitude = pageCoords.lat; + longitude = pageCoords.lon; + zoom = 5; + } else if ( !mwAttrs || !mwAttrs.extsrc ) { + latitude = 30; + longitude = 0; + zoom = 2; + } return { - center: center, + center: [ latitude, longitude ], zoom: zoom }; }; -- To view, visit https://gerrit.wikimedia.org/r/302081 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Iae535369cd93c3764d3fe1ce6714c7c8b1167f19 Gerrit-PatchSet: 8 Gerrit-Project: mediawiki/extensions/Kartographer Gerrit-Branch: master Gerrit-Owner: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: JGirault <julien.inbox.w...@gmail.com> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: Yurik <yu...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits