MediaWiki:Gadget-MarkerUtilities.js

Aus Wikivoyage

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
//<nowiki>
/**	Gadget-MarkerUtilities.js, v2.0, 2023-06-03

	support for markers, vCards/listings, indicators, TOC
	original authors: DerFussi, Roland Unger

	Support of desktop and mobile views
	License: GPL-2.0+, CC-by-sa 3.0
*/
 
( function ( $, mw ) {
	'use strict';

	/**
		Replacing copy-marker placeholders (Template:Marker Kopie)
	*/
	function replaceMarkerPlaceholders() {
		$( '.voy-copy-marker' ).each( function() {
			const $this = $( this ),
				att = $this.attr( 'data-copy-marker-attribute' ),
				cont = $this.attr( 'data-copy-marker-content' ),
				markers = $( '.vcard[' + att + '="' + cont + '"]:not(.voy-without-marker)' ),
				bgColor = markers.attr( 'data-color' ),
				marker = markers.find( 'a.mw-kartographer-maplink' );
			if ( marker.length ) {
				const lat = marker.attr( 'data-lat' ),
					lon = marker.attr( 'data-lon' ),
					zoom = marker.attr( 'data-zoom' ),
					isInverse = marker.closest( '.listing-map' ).hasClass( 'listing-map-inverse' ),
					number = marker.first().text();
				$this.css( { 'background-color': bgColor, display: 'inline-block' } )
					.append( $( '<span class="mw-kartographer-maplink mw-kartographer-autostyled' +
					( isInverse ? ' listing-map-inverse' : '' ) +
					'">' + number + '</span>' )
						.attr( { 'data-lat': lat, 'data-lon': lon, 'data-zoom': zoom } )
						.click(
							{ attribute : att, content : cont }, 
							function( event ) { 
								$( '.vcard[' + event.data.attribute + '="' + event.data.content +
								'"]:not(.voy-without-marker) a.mw-kartographer-maplink' )
									.first().trigger( 'click' ); 
							}
						)
					);
				$this.closest( '.vcard' ).attr( 'data-color', bgColor );
			}
		});
	}

	/**
		Adds "listing-inline" and "listing-with-siblings" classes to
		<span class="vCard"> listings if necessary
	*/
	function addVcardMaintenanceClasses() {
		var ignoreTags = [ 'link', 'style', 'ul' ];
		$( 'span.vCard' ).each( function() {
			var count = 0,
				countAll = 0,
				$this = $( this ),
				isInline = $this.hasClass( 'listing-inline' ),
				siblings = $this.parent().contents();
			for ( var i = 0; i < siblings.length; i++ ) {
				if ( ( siblings[ i ].nodeType === Node.TEXT_NODE && 
					siblings[ i ].nodeValue.trim() !== '' ) ||
					( siblings[ i ].nodeType === Node.ELEMENT_NODE &&
					!ignoreTags.includes( siblings[ i ].nodeName.toLowerCase() ) ) ) {
					count++;
					countAll++;
				}
				// text before vCard will not prevent display of full edit/info buttons
				if ( $this.is( siblings[ i ] ) ) {
					count = 1;
				}
			}
			if ( count === 1 ) {
				$this.removeClass( "listing-inline" );
			}
			if ( count > 1 ) {
				$this.addClass( 'listing-inline' );
			}
			if ( countAll > 1 && !isInline ) {
				$this.addClass( 'listing-with-siblings' );
			}
		});
	}

	/**
		Support of Skype links. Skype protocol is not supported by the
		Mediawiki software, and the link is to be added later
	*/
	function addSkypeLinks() {
		$( '.listing-skype-link' ).each( function() {
			var p = $( this ),
				t = p.text();
			p.html( mw.format( '<a href="skype:$1">$2</a>', t, t.replace( /\?.*$/, '' ) ) );
		});
	}

	/**
		add voy-user-login class to body tag if user is logged-in
		to show Wikidata icon after markers
	*/
	function addUserLoginClass() {
		if ( mw.config.get( 'wgUserName' ) ) {
			$( 'body' ).addClass( 'voy-user-login' );
		}
	}

	/**
		Add class for figure captions if they contain markers to show
		marker tooltips correctly
	*/
	function addFigCaptionClass() {
		$( 'figcaption:has(span.Marker)' ).addClass( 'with-Marker' );
	}

	/**
		Move indicators in Vector 2022 skin
	*/
	function moveIndicators() {
		$( 'body.skin-vector-2022 .mw-indicators' ).insertAfter( '#p-lang-btn' );
	}

	/**
		Workaround TOC and jumps to anchors in URL
	*/
	function jumpToHash( $content ) {
		mw.loader.using( 'user.options', function () {
			var hash = window.location.hash.slice( 1 ),
				toc = $( '#content #toc' );

			if ( hash !== '' && toc.length ) {
				if ( mw.user.options.get( 'toc-floated' ) ) {
					toc.addClass( 'tocFloat' );
				}
				if ( !mw.user.options.get( 'toc-expand' ) ) {
					$( '#content #toc .toclevel-2' ).hide();
				}
				window.location.href = '#' + hash;
			}
		});
	}

	replaceMarkerPlaceholders();
	addVcardMaintenanceClasses();
	addSkypeLinks();
	addUserLoginClass();
	addFigCaptionClass();
	moveIndicators();
	mw.hook( 'wikipage.content' ).add( jumpToHash );

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