MediaWiki:Gadget-wikidata-list.js

Frå Wikipedia – det frie oppslagsverket

Merk: Etter lagring vil det kanskje vera naudsynt at nettlesaren slettar mellomlageret sitt for at endringane skal tre i kraft.

  • Firefox og Safari: Haldt nede Shift medan du klikkar på Oppdater, eller trykk anten Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Trykk Ctrl-Shift-R (⌘-Shift-R på Mac)
  • Internet Explorer og Edge: Haldt nede Ctrl medan du klikkar Oppdater, eller trykk Ctrl-F5
  • Opera: Tøm mellomlageret i Verktøy → Innstillingar
// These are i18n fallbacks that can be overridden locally. Do not override them
// in this page as that will block further updates without additional work.
// The value for gadget-wikidata-link-item-text, that is "item", should be kept
// short as it placed vertically. Typically it should be 4 – 5 characters.
// The *-href keys should not be localized.
var fallbacks = {
	"gadget-wikidata-link-category": "Wikidata list",
	"gadget-wikidata-link-claim-title": "Go to property $2 at $1 on Wikidata",
	"gadget-wikidata-link-claim-href": "https://www.wikidata.org/wiki/$1#$2",
	"gadget-wikidata-link-claim-text": "claim",
	"gadget-wikidata-link-item-title": "Go to item $1 on Wikidata",
	"gadget-wikidata-link-item-href": "https://www.wikidata.org/wiki/$1",
	"gadget-wikidata-link-item-text": "item"
};

var extractPatterns = function( obj, pattern ) {
	"use strict";
	if ( obj === null ) {
		return null;
	}
	var cls = obj.attr( 'class' );
	if ( cls === null ) {
		return null;
	}
	var matches = pattern.exec( cls );
	if ( matches === null ) {
		return null;
	}
	if (matches.length !== 2 ) {
		return null;
	}
	return matches[1];
};

jQuery.fn.itemId = function() {
	"use strict";
	var id = extractPatterns( $(this[0]), /^.*?wd_(q\d+).*$/ );
	return id ? id.toUpperCase() : null;
};

jQuery.fn.propId = function() {
	"use strict";
	var id = extractPatterns( $(this[0]), /^.*?wd_(p\d+|label|desc).*$/ );
	return id ? id.toUpperCase() : null;
};

var buildClaimHandle = function( $td, qid, prop ) {
	"use strict";
	var $text = $( "<span>" );
	var text = mw.message( "gadget-wikidata-link-claim-text" );
	if ( text.exists() ) {
		$text.text( text.escaped() );
	}
	var $link = $( "<a>" )
		.append( $text );
	var href = mw.message( "gadget-wikidata-link-claim-href", qid, prop );
	if ( href.exists() ) {
		$link.attr( "href", href.escaped() );
	}
	var title = mw.message( "gadget-wikidata-link-claim-title", qid, prop );
	if ( title.exists() ) {
		$link.attr( "title", title.escaped() );
	}
	var $div = $("<div>")
		.addClass( "wd-link" )
		.addClass( "wd-claim-link" )
		.append( $link );
	$td.css( {
		position:"relative"
	} );
	$div.css( {
		top: "0.2em",
		right: "0.2em",
		position:"absolute",
		display: "none"
	} );
	$td.prepend( $div );
	$td.hover(
		function() { $div.fadeIn( "fast" ); },
		function() { $div.fadeOut( "fast" ); }
	);
};

var buildItemHandle = function( $tr, qid ) {
	"use strict";
	var $text = $( "<span>" );
	var text = mw.message( "gadget-wikidata-link-item-text" );
	if ( text.exists() ) {
		$text.text( text.escaped() );
	}
	var $link = $( "<a>" )
		.append( $text );
	var href = mw.message( "gadget-wikidata-link-item-href",
		qid.toUpperCase() );
	if ( href.exists() ) {
		$link.attr( "href", href.escaped() );
	}
	var title = mw.message( "gadget-wikidata-link-item-title",
		qid.toUpperCase() );
	if ( title.exists() ) {
		$link.attr( "title", title.escaped() );
	}
	var $div = $("<div>")
		.addClass( "wd-link" )
		.addClass( "wd-item-link" )
		.append( $link );
	var $td = $tr.children( ":first" );
	var width = $td.outerWidth();
	$td.css( {
		position:"relative"
	} );
	$div.css( {
		top: 0,
		bottom: 0,
		right: width,
		position:"absolute",
		display: "none"
	} );
	$td.prepend( $div );
	$tr.hover(
		function() { $div.fadeIn( "fast" ); },
		function() { $div.fadeOut( "fast" ); }
	);
};

$.when( mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ) )
.fail( function() {
	"use strict";
	mw.log( "WDlink: ", "failing in setup" );
} )
.then( function() {
	var category = mw.message( "gadget-wikidata-link-category" ).escaped();
	if ( !["view"].includes( mw.config.get( "wgAction" ) )
		|| mw.util.getParamValue( "type" )
		|| mw.util.getParamValue( "diff" )
		|| mw.util.getParamValue( "redirect" )
		|| mw.util.getParamValue( "veaction" )
	) {
		mw.log( "WDlink: ", "terminate early due to limitations" );
		return Promise.reject( new Error( "terminate early due to limitations" ) );
	}
} )
.then( function() {
	"use strict";

	// make sure we have fallbacks
	$.each( fallbacks, function( key, message ) {
		mw.messages.set( key, message );
	} );

	// load local overrides
	return new mw.Api().getMessages( Object.keys( fallbacks ), {
		amlang: mw.config.get( "wgUserLanguage" )
	} )
	.then( function( messages ) {
		var msgs = {};
		jQuery.extend( msgs, fallbacks, messages );
		$.each( msgs, function( key, message ) {
			mw.messages.set( key, message );
		} );
	} );
} )
.then( function() {
	// this must run after the messages are localized
	var category = mw.message( 'gadget-wikidata-link-category' ).escaped();
	if ( !mw.config.get( 'wgCategories' ).includes( category ) ) {
		mw.log( 'WDlink: ', 'terminate late due to limitations' );
		return Promise.reject( new Error( 'terminate late due to limitations' ) );
	}
} )
.then( function() {
	"use strict";
	mw.log( "WDlink: ", "try to activate" );
	$(".wd_can_edit tr").each( function() {
		var $tr = $( this );
		var item = $tr.itemId();
		if ( item === null ) {
			return;
		}
		$tr.hover(
			function() {
				// remove initial handler
				$( this ).off( "mouseenter mouseleave" );
				// build the handle for the row we're hovering above
				buildItemHandle( $tr, item );
				// show the handle, it will be removed if we're moving out
				$( ".wd-item-link", this ).fadeIn( "fast" );
			},
			function() {}
		);
		$tr.children( "td" ).each( function() {
			var $td = $( this );
			var prop = $td.propId();
			if ( prop === null ) {
				return;
			}
			prop = prop.replace( /^(?:LABEL|DESC)$/, "mw-content-text");
			$td.hover(
				function() {
					// remove initial handler
					$( this ).off( "mouseenter mouseleave" );
					// build the handle for the row we're hovering above
					buildClaimHandle( $td, item, prop );
					// show the handle, it will be removed if we're moving out
					$( ".wd-claim-link", this ).fadeIn( "fast" );
				},
				function() {}
			);
		} );
	} );
} );