MediaWiki:Common.js

From imedwiki
Revision as of 11:16, 20 September 2021 by Christoph Holtermann (talk | contribs) (import from imedwiki de)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. */
/* Das folgende JavaScript wird für alle Benutzer geladen. */

/** Helper functions for maps checkbox selector */
/* Steuerung von Maps über Checkboxen */

/* return filename from url - omit path and all that comes before */
function getFilenameFromUrl(urlString) {
	url = new URL(urlString);
	filename = url.pathname.split('/').pop();
	return filename;
}

/* compare url to filename, used to check in checkbox for maps */ 
function compareImgUrl(url, name) {
	nameEscaped = name.replace(/ /g,"_");
	filename = decodeURIComponent(getFilenameFromUrl(url));
	return filename == nameEscaped;
}

/* get all map markers that match provided name of image */
function getAllMarkersBySrcName(name) {
	markers = $(".leaflet-marker-icon");
	returnArray = [];
	for (var i = 0; i < markers.length; i++) {
	    marker = markers[i];
		if (marker.src && compareImgUrl(marker.src, name)) returnArray.push(marker);
	}
	return returnArray;
}

/* enable or disable markers depending on checkbox value */
function checkboxOnChange() {
	fileName = this.value;
	checkedState = this.checked;
	markers = getAllMarkersBySrcName(fileName);
	for (var i = 0; i < markers.length; i++) {
	    marker = markers[i];
		if (checkedState) $(marker).removeClass("nodisplay"); else $(marker).addClass("nodisplay");
	}
}

function checkboxPropagate(checkbox) {
	fileName = checkbox.value;
	checkedState = checkbox.checked;
	markers = getAllMarkersBySrcName(fileName);
	for (var i = 0; i < markers.length; i++) {
	    marker = markers[i];
		if (checkedState) $(marker).removeClass("nodisplay"); else $(marker).addClass("nodisplay");
	}
}

function checkboxesAllPropagate() {
	checkboxes = $('#checkbox input');
    for (var i = 0; i < checkboxes.length; i++) {
    	checkbox = checkboxes[i];
    	checkboxPropagate(checkbox);
    }
}

$('#checkbox input').attr('change', undefined).change(checkboxOnChange);
$("#map_leaflet_1").on('click', function() { checkboxesAllPropagate(); /* console.log("click"); */ });
$("#map_leaflet_1").on('wheel', function() { checkboxesAllPropagate(); /* console.log("wheel"); */ });
/* $("#map_leaflet_1").on('transitionstart', function() { checkboxesAllPropagate(); console.log("transitionstart");}); */
$("#map_leaflet_1").on('transitionrun', function() { checkboxesAllPropagate(); /* console.log("transitionrun"); */ });
/* $("#map_leaflet_1").on('transitionend', function() { checkboxesAllPropagate(); console.log("transitionend")}); */

/* this structure is assumed (as provided by Vorlage:AufKarteZeigen):
<span class=".Kartenlink"><a href="URL#divId">LINKTEXT</a><span class="coords"><span class="coordvalues">lat,long</span></span></span>
The map has to be enclosed by a div with id divId. It works as link target on page and to identify the map.
*/
$(".Kartenlink a").on('click', function() { 
	divId = $(this).attr("href").split('#').pop(); 
	for (var i = 0; i < mapsLeafletList.length; i++) {
      if ($(mapsLeafletList[i][0]).parents("#"+divId).length) {
      	coords = $(this).parent().find("span.coordvalues").html();
      	var latLng=L.latLng(coords.split(","));
      	mapsLeafletList[i].map.flyTo(latLng, 16);
      }
    } 
});

/* dealing with interwikilinks */

var interwikiSuffixes = Array();

/* this function returns all interwikilinks 
return jQuery results */
function getAllInterwikiLinks() {
	interwikilinks = $("#content a.extiw");
	return interwikilinks;
}

/* return destination of interwikilink

extract information from link title (i.e. "wikipedia:Hauptseite"). 
Title gets split by : and the first part is returned */
function getInterwikiDest(link) {
	return link.title.split(":")[0];
}

function getInterwikiLinkSuffix(interwiki) {
	if (interwiki in interwikiSuffixes) return interwikiSuffixes[interwiki];
	
	textObject = JSON.parse(
    	$.ajax({
        	url:mw.util.wikiScript('api'),
        	data: { action: 'parse', text: '{{Vorlage:InterwikilinkWikipedia}}', format: 'json', contentmodel: 'wikitext'},
        	async:false
    	}).responseText
    );
    text = textObject.parse.text["*"];
    $suffix = $(text).find("p").html();
    
    if (!(interwiki in interwikiSuffixes)) interwikiSuffixes[interwiki] = $suffix;
    
    return $suffix;
}

/* append suffix to interwikilink */
function appendInterwikiLinkSuffix(link){
	dest = getInterwikiDest(link);
	interwikiLinkSuffix =getInterwikiLinkSuffix(dest);
	link.innerHTML += interwikiLinkSuffix;
}

/* allInterwikiLinksAppendSuffix 

append link suffix to all interwikilinks on page in content area */
function allInterwikiLinksAppendSuffix() {
	interwikiLinks = getAllInterwikiLinks();
	for (var i = 0; i < interwikiLinks.length; i++) {
		interwikiLink = interwikiLinks[i];
		appendInterwikiLinkSuffix(interwikiLink);
	}
}

allInterwikiLinksAppendSuffix();