MediaWiki:Gadget-dataitemlinks.js

From OpenStreetMap Wiki
Jump to navigation Jump to search

Note: After publishing, 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: Press Ctrl-F5.
/**
 * 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"
	);
	
	// Merge the wiki sitelink with the label in the page title.
	// Custom OSM Wikibase code applies a style to the sitelink list that’s incompatible with Vector 2022.
	// https://github.com/nyurik/OsmWikibase/blob/7e280f5ec15ed2104436f18633a9e041dd5bb338/modules/OsmWikibase.less#L13-L15
	var sitelink = $(".wikibase-sitelinkview-link-wiki .wikibase-sitelinkview-page a");
	var titleLabel = $(".wikibase-title-label span[lang]");
	if (titleLabel.length === 0) titleLabel = $(".wikibase-title-label");
	var labelLink = $("<a>")
		.attr("href", sitelink.attr("href"))
		.attr("title", sitelink.attr("title"));
	titleLabel.wrapInner(labelLink);
	$(".wikibase-sitelinkgrouplistview").hide();
});

/**
 * 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 );