JGirault has uploaded a new change for review. https://gerrit.wikimedia.org/r/294940
Change subject: Make <maplink> a real link so users can open it in a new tab/window. ...................................................................... Make <maplink> a real link so users can open it in a new tab/window. Bug: T137910 Change-Id: I48f60846107f1a313cab509e7fc73910a88f62cd --- M modules/kartographer.MapDialog.js M modules/kartographer.js M styles/kartographer.less 3 files changed, 77 insertions(+), 28 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Kartographer refs/changes/40/294940/1 diff --git a/modules/kartographer.MapDialog.js b/modules/kartographer.MapDialog.js index 798f144..2bb774c 100644 --- a/modules/kartographer.MapDialog.js +++ b/modules/kartographer.MapDialog.js @@ -53,8 +53,8 @@ // Check whether it is the same map. if ( existing && - typeof existing.articleMapId === 'number' && - existing.articleMapId === mapData.articleMapId ) { + typeof existing.maptagId === 'number' && + existing.maptagId === mapData.maptagId ) { fullScreenState = mapData.fullScreenState; extendedData = {}; @@ -149,7 +149,7 @@ this.map.setView( new L.LatLng( extendedData.latitude, extendedData.longitude ), extendedData.zoom, true ); } - if ( typeof mapData.articleMapId === 'number' ) { + if ( typeof mapData.maptagId === 'number' ) { this.map.on( 'moveend', this.onMapMove, this ); } diff --git a/modules/kartographer.js b/modules/kartographer.js index 206ca96..371791a 100644 --- a/modules/kartographer.js +++ b/modules/kartographer.js @@ -42,6 +42,13 @@ */ mw.kartographer.maps = []; + /** + * References the maplinks of the page. + * + * @type {HTMLElement[]} + */ + mw.kartographer.maplinks = []; + mw.kartographer.FullScreenControl = L.Control.extend( { options: { // Do not switch for RTL because zoom also stays in place @@ -280,8 +287,8 @@ if ( mapData instanceof L.Map ) { map = mapData; mapData = getMapData( $( map.getContainer() ).closest( '.mw-kartographer-interactive' ) ); - } else if ( $.type( mapData.articleMapId ) === 'number' ) { - map = mw.kartographer.maps[ mapData.articleMapId ]; + } else if ( mapData && mapData.isMapframe ) { + map = mw.kartographer.maps[ mapData.maptagId ]; } $.extend( dialogData, mapData, { @@ -312,7 +319,7 @@ /** * Formats the full screen route of the map, such as: - * `/map/:articleMapId(/:zoom/:longitude/:latitude)` + * `/map/:maptagId(/:zoom/:longitude/:latitude)` * * The hash will contain the portion between parenthesis if and only if * one of these 3 values differs from the initial setting. @@ -323,10 +330,13 @@ * @return {string} The route to open the map in full screen mode. */ mw.kartographer.getMapHash = function ( data, map ) { - var hash = '/map/' + data.articleMapId, + + var hash = '/' + ( data.isMapframe ? 'map' : 'maplink' ), mapPosition, newHash, initialHash = getScaleCoords( data.zoom, data.latitude, data.longitude ).join( '/' ); + + hash += '/' + data.maptagId; if ( map ) { mapPosition = getMapPosition( map ); @@ -388,24 +398,49 @@ */ function getMapData( element ) { var $el = $( element ), - articleMapId = null; + maptagId = null; // Prevent users from adding map divs directly via wikitext if ( $el.attr( 'mw-data' ) !== 'interface' ) { return null; } - if ( $.type( $el.data( 'article-map-id' ) ) !== 'undefined' ) { - articleMapId = +$el.data( 'article-map-id' ); + if ( $.type( $el.data( 'maptag-id' ) ) !== 'undefined' ) { + maptagId = +$el.data( 'maptag-id' ); } return { - articleMapId: articleMapId, + isMapframe: $el.hasClass( 'mw-kartographer-interactive' ), + maptagId: maptagId, latitude: +$el.data( 'lat' ), longitude: +$el.data( 'lon' ), zoom: +$el.data( 'zoom' ), style: $el.data( 'style' ), overlays: $el.data( 'overlays' ) }; + } + + /** + * Formats the fullscreen state object based on route attributes. + * + * @param {string|number} [zoom] + * @param {string|number} [latitude] + * @param {string|number} [longitude] + * @return {Object} Full screen state + * @return {number} [return.zoom] Zoom if between 0 and 18. + * @return {number} [return.latitude] + * @return {number} [return.longitude] + * @private + */ + function getFullScreenState( zoom, latitude, longitude ) { + var obj = {}; + if ( zoom !== undefined && zoom >= 0 && zoom <= 18 ) { + obj.zoom = +zoom; + } + if ( longitude !== undefined ) { + obj.latitude = +latitude; + obj.longitude = +longitude; + } + return obj; } /** @@ -533,12 +568,11 @@ // Some links might be displayed outside of $content, so we need to // search outside. This is an anti-pattern and should be improved... // Meanwhile #content is better than searching the full document. - $( '#content' ).on( 'click', '.mw-kartographer-link', function () { - var data = getMapData( this ); + $( '.mw-kartographer-link', '#content' ).each( function ( index ) { + mw.kartographer.maplinks[ index ] = this; - if ( data ) { - mw.kartographer.openFullscreenMap( data ); - } + $( this ).data( 'maptag-id', index ); + this.href = '#' + '/maplink/' + index; } ); L.Map.mergeOptions( { @@ -553,7 +587,7 @@ container = this, $container = $( this ); - $container.data( 'article-map-id', index ); + $container.data( 'maptag-id', index ); data = getMapData( container ); if ( data ) { @@ -593,23 +627,35 @@ // Allow customizations of interactive maps in article. mw.hook( 'wikipage.maps' ).fire( mapsInArticle, false /* isFullScreen */ ); - // Opens map in full screen. #/map(/:zoom)(/:latitude)(/:longitude) + // Opens a map in full screen. #/map(/:zoom)(/:latitude)(/:longitude) // Examples: // #/map/0 // #/map/0/5 // #/map/0/16/-122.4006/37.7873 - router.route( /map\/([0-9]+)(?:\/([0-9]+))?(?:\/([\-\+]?\d+\.?\d{0,5})?\/([\-\+]?\d+\.?\d{0,5})?)?/, function ( mapId, zoom, latitude, longitude ) { - var map = mw.kartographer.maps[ mapId ], - fullScreenState = {}; + router.route( /map\/([0-9]+)(?:\/([0-9]+))?(?:\/([\-\+]?\d+\.?\d{0,5})?\/([\-\+]?\d+\.?\d{0,5})?)?/, function ( maptagId, zoom, latitude, longitude ) { + var map = mw.kartographer.maps[ maptagId ]; + if ( !map ) { + router.navigate( '' ); + return; + } + mw.kartographer.openFullscreenMap( map, getFullScreenState( zoom, latitude, longitude ) ); + } ); - if ( zoom !== undefined && zoom >= 0 && zoom <= 18 ) { - fullScreenState.zoom = +zoom; + // Opens a maplink in full screen. #/maplink(/:zoom)(/:latitude)(/:longitude) + // Examples: + // #/maplink/0 + // #/maplink/0/5 + // #/maplink/0/16/-122.4006/37.7873 + router.route( /maplink\/([0-9]+)(?:\/([0-9]+))?(?:\/([\-\+]?\d+\.?\d{0,5})?\/([\-\+]?\d+\.?\d{0,5})?)?/, function ( maptagId, zoom, latitude, longitude ) { + var link = mw.kartographer.maplinks[ maptagId ], + data; + + if ( !link ) { + router.navigate( '' ); + return; } - if ( longitude !== undefined ) { - fullScreenState.latitude = +latitude; - fullScreenState.longitude = +longitude; - } - mw.kartographer.openFullscreenMap( map, fullScreenState ); + data = getMapData( link ); + mw.kartographer.openFullscreenMap( data, getFullScreenState( zoom, latitude, longitude ) ); } ); // Check if we need to open a map in full screen. diff --git a/styles/kartographer.less b/styles/kartographer.less index 9abf55b..705b4c0 100644 --- a/styles/kartographer.less +++ b/styles/kartographer.less @@ -53,6 +53,9 @@ line-height: 1; text-align: center; } +a.mw-kartographer-autostyled:visited { + color: #fff; +} .leaflet-bar a { background-position: center center; -- To view, visit https://gerrit.wikimedia.org/r/294940 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I48f60846107f1a313cab509e7fc73910a88f62cd Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Kartographer Gerrit-Branch: master Gerrit-Owner: JGirault <julien.inbox.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits