MediaWiki:Gadget-dataitemlinks.js: Difference between revisions

From OpenStreetMap Wiki
Jump to navigation Jump to search
(Moved sitelink editing link to toolbox)
Line 84: Line 84:
// Must have a single digit 0 without any other digits around it
// Must have a single digit 0 without any other digits around it
var noRefRe = RegExp('(^|[^0-9])0($|[^0-9])');
var noRefRe = RegExp('(^|[^0-9])0($|[^0-9])');
var noEditProps = {'P19':1, 'P16':1, 'P41':1, 'P10':1, 'P40':1, 'P43':1, 'P21':1}
var noEditProps = {'P19':1, 'P16':1, 'P52':1, 'P53':1, 'P41':1, 'P10':1, 'P40':1, 'P43':1, 'P21':1}
$("div.wikibase-statementview-references-container span.ui-toggler-label").each(function () {
$("div.wikibase-statementview-references-container span.ui-toggler-label").each(function () {
if (!noRefRe.test(this.textContent)) return; // Ignore if has non-0 ref count
if (!noRefRe.test(this.textContent)) return; // Ignore if has non-0 ref count

Revision as of 07:28, 30 April 2023

/**
 * Localize the sitelink at the top of the data item page.
 *
 * Dependencies: mediawiki.util
 */
$(function () {
	if (mw.config.get("wgPageContentModel") !== "wikibase-item" || mw.config.get("wgNamespaceNumber") != 120) return;
	
	var userLanguage = mw.config.get("wgUserLanguage");
	var localTitle = $("#P31 .wikibase-statementview-mainsnak .wb-monolingualtext-value:lang(" + userLanguage + ")").first().text();
	if (localTitle) {
		$(".wikibase-sitelinkview-page a")
			.attr("href", mw.util.getUrl(localTitle))
			.attr("title", localTitle)
			.text(localTitle);
	}
	
	// Link to [[Special:SetSiteLink]] in the toolbox since the sitelink editor is hidden by
	// https://github.com/nyurik/OsmWikibase/blob/7e280f5ec15ed2104436f18633a9e041dd5bb338/modules/OsmWikibase.less#L20
	mw.util.addPortletLink(
		"p-tb",
		mw.util.getUrl("Special:SetSiteLink/" + mw.config.get("wbEntityId") + "/wiki"),
		"Set item sitelink",
		"t-wb-setsitelink",
		"Set the item sitelink"
	);
});

/**
 * Shows local OSM image properties as images.
 *
 * Dependencies: mediawiki.util
 */
$(function () {
	if (mw.config.get('wgPageContentModel') !== 'wikibase-item') return;
	
	function wrapOsmFiles(prop) {
		var filter = '#' + prop + ' .wikibase-statementview-mainsnak .wikibase-snakview-variation-valuesnak';
		$(filter).html(function () {
			var text = this.textContent;
			var replacement = mw.html.element('a', {
					href: mw.util.getUrl('File:' + text),
					title: text,
				}, new mw.html.Raw(mw.html.element('img', {
					src: mw.util.getUrl('Special:Redirect/file/' + text, { width: 400, height: 120 }),
					alt: text,
				}) + '<br>' + text));
			return replacement;
		});
	}
		
	wrapOsmFiles('P28');
	wrapOsmFiles('P39');
});

/**
 * Links [[Property:P31]] statement values to the referenced wiki pages.
 *
 * Dependencies: mediawiki.util
 */
$(function () {
	if (mw.config.get("wgPageContentModel") !== "wikibase-item") return;
	
	$("#P31 .wikibase-statementview-mainsnak .wb-monolingualtext-value").wrapInner(function () {
		return mw.html.element("a", {
			href: mw.util.getUrl(this.textContent),
			title: this.textContent
		});
	});
});

/**
 * Hide references with 0 counts, and make UI more compact, hide edit links for a few properties
 *
 * Dependencies: mediawiki, jquery
 */
( function( $, mw ) {
  $(function () {
    if (!mw.config.exists('wbEntityId')) {
      return;
    }
    mw.hook('wikibase.entityPage.entityView.rendered').add(function () {

      // Must have a single digit 0 without any other digits around it
      var noRefRe = RegExp('(^|[^0-9])0($|[^0-9])');
      var noEditProps = {'P19':1, 'P16':1, 'P52':1, 'P53':1, 'P41':1, 'P10':1, 'P40':1, 'P43':1, 'P21':1}
      $("div.wikibase-statementview-references-container span.ui-toggler-label").each(function () {
        if (!noRefRe.test(this.textContent)) return; // Ignore if has non-0 ref count
        var $elemToHide = $(this).parent().parent().parent();
        var $elemToPad = $elemToHide.parent();
        var $topElement = $elemToPad.parent().parent().parent();
        var propId = $topElement[0].id;
        if (propId !== 'P6') {
          // Remove padding and hide ref section, except for the Status
          $elemToPad.css("padding-bottom", 0);
          $elemToHide.hide();
        }
        if (noEditProps[propId]) {
        	// Hide edit and 'add value' links for the permanent properties
        	$elemToPad.children('span.wikibase-edittoolbar-container').hide();
        	$('span.wikibase-toolbar-wrapper', $topElement).hide();
        }
      });
    });
  });
})( jQuery, mediaWiki );