User:Henning (WMDE)/AuthorityControl.js

From Wikidata
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.
/* AuthorityControl.js
 * Provides a link to various Authority Control tools (VIAF, GND, etc.) for Wikidata statements that
 * feature certain properties.
 *
 * Original gadget coded by [[User:Ricordisamoa]]
 */
( function( mw, $ ) {

var BASE_LANGUAGE = mw.config.get( 'wgUserLanguage' ).replace( /^([a-z]*).*$/g, '$1' );

var PROPERTIES = {
	212: mw.util.getUrl('Special:BookSources') + '/$1', // ISBN-13
	213: 'http://isni-url.oclc.nl/isni/$1', // ISNI
	214: '//viaf.org/viaf/$1/', // VIAF
	220: 'http://www-01.sil.org/iso639-3/documentation.asp?id=$1', // ISO 639-3
	225: '//species.wikimedia.org/wiki/$1', // taxon name
	227: 'http://d-nb.info/gnd/$1', // GND
	231: 'http://esis.jrc.ec.europa.eu/doc/IUCLID/data_sheets/$1.pdf', // CAS
	235: 'http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=search&db=pccompound&term=%22$1%22[InChIKey]', // InChIKey
	236: 'http://www.worldcat.org/search?fq=x0:jrnl&q=n2:$1', // ISSN
	243: 'http://worldcat.org/oclc/$1&lang=' + BASE_LANGUAGE, // OCLC
	244: 'http://lccn.loc.gov/$1', // LCCN
	245: 'http://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=$1', // ULAN
	247: 'http://nssdc.gsfc.nasa.gov/nmc/spacecraftDisplay.do?id=$1', // NSSDC ID
	268: 'http://catalogue.bnf.fr/ark:/12148/cb$1/PUBLIC', // BNF
	269: 'http://www.idref.fr/$1', // SUDOC
	270: 'http://opac.calis.edu.cn/aopac/ajsp/detail.jsp?actionfrom=1&actl=CAL++$1%23', // CALIS
	271: 'http://ci.nii.ac.jp/author/$1', // CiNii
	345: 'http://www.imdb.com/Name?$1', // IMDb
	347: 'http://www.culture.gouv.fr/public/mistral/joconde_fr?ACTION=CHERCHER&FIELD_1=REF&VALUE_1=$1', // Joconde
	349: 'http://id.ndl.go.jp/auth/ndlna/$1', // NDL
	350: 'http://www.rkd.nl/rkddb/dispatcher.aspx?action=search&database=ChoiceImages&search=priref=$1', // RKDimages
	351: 'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=retrieve&dopt=default&list_uids=$1', // Entrez Gene
	352: 'http://www.uniprot.org/uniprot/$1', // Uniprot
	354: 'http://www.genenames.org/data/hgnc_data.php?hgnc_id=$1', // HGNC
	356: 'http://dx.doi.org/$1', // DOI
	359: 'http://monumentenregister.cultureelerfgoed.nl/php/main.php?cAction=search&cOffset=1&cLimit=25&oOrder=ASC&cSubmit=1&sCompMonNr=$1', // Rijksmonument
	373: '//commons.wikimedia.org/wiki/Category:$1', // Commons category
	374: 'http://www.insee.fr/fr/bases-de-donnees/esl/comparateur.asp?codgeo=com-$1', // INSEE commune code
	377: 'http://www.n2yo.com/satellite/?s=$1', // SCN
	380: 'http://www.culture.gouv.fr/public/mistral/merimee_fr?ACTION=CHERCHER&FIELD_1=REF&VALUE_1=$1', // Mérimée
	381: '//toolserver.org/~erfgoed/api/api.php?action=search&format=html&srcountry=ch&srid=$1', // KGS identifier
	396: 'http://id.sbn.it/af/$1', // SBN
	402: 'http://www.openstreetmap.org/?relation=$1', // OpenStreetmap
	409: 'http://nla.gov.au/anbd.aut-an$1', // NLA
	424: '//$1.wikipedia.org', // Wikimedia language code
	428: 'http://www.ipni.org/ipni/advPlantNameSearch.do?find_includePublicationAuthors=on&find_includePublicationAuthors=off&find_includeBasionymAuthors=on&find_includeBasionymAuthors=off&find_rankToReturn=all&output_format=normal&find_authorAbbrev=$1', // botanist author abbreviation
	434: '//musicbrainz.org/artist/$1', // MusicBrainz Artist ID
	435: '//musicbrainz.org/work/$1', // MusicBrainz Work ID
	436: '//musicbrainz.org/release-group/$1', // MusicBrainz Release Group ID
	454: 'http://' + ( $.inArray( BASE_LANGUAGE, ['de', 'en', 'fr'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + '.structurae.de/structures/data/index.cfm?ID=$1', // Structurae ID
	455: 'http://www.emporis.com/' + ( $.inArray( BASE_LANGUAGE, ['de', 'en', 'fr'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + '/wm/bu/?id=$1', // Emporis ID
	464: 'http://www.legifrance.gouv.fr/UnTexteDeJorf.do?numjo=$1', // NOR (French law)
	476: 'http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=CELEX:$1:' + ( $.inArray( BASE_LANGUAGE, ['bg', 'es', 'cs', 'da', 'de', 'et', 'el', 'en', 'fr', 'ga', 'it', 'lv', 'lt', 'hu', 'mt', 'nl', 'pl', 'pt', 'ro', 'ro', 'sk', 'sl', 'fi', 'sv'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + ':NOT', // CELEX (European law)
	477: 'http://www.historicplaces.ca/' + ( $.inArray( BASE_LANGUAGE, ['en', 'fr'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + '/rep-reg/place-lieu.aspx?id=$1', // Historic Places (Canada)
	480: 'http://www.filmaffinity.com/' + ( $.inArray( BASE_LANGUAGE, ['en', 'es'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + '/film$1.html', // Filmaffinity
	481: 'http://www.culture.gouv.fr/public/mistral/palissy_fr?ACTION=CHERCHER&FIELD_1=REF&VALUE_1=$1', // Palissy
	486: 'http://www.nlm.nih.gov/cgi/mesh/2013/MB_cgi?field=uid&term=$1', // Mesh
	492: 'http://omim.org/entry/$1', // OMIM
	493: 'http://www.icd9data.com/getICD9Code.ashx?icd9=$1', // ICD 9
	494: 'http://apps.who.int/classifications/icd10/browse/2010/en#/$1', // ICD 10
	496: 'http://orcid.org/$1', // ORCID
	497: 'http://db1.ihp.sinica.edu.tw/cbdbc/cbdbkm' + ( $.inArray( BASE_LANGUAGE.substring(0,2), ['zh'] ) !== -1 ? '' : 'eng' ) + '?~~AAA$1', // China Biographical Database
	508: 'http://thes.bncf.firenze.sbn.it/termine.php?id=$1', //BNFC
	535: 'http://www.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=$1', // Find a Grave
	536: 'http://www.atpworldtour.com/Tennis/Players/$1.aspx', // ATP id
	539: 'http://www.culture.gouv.fr/public/mistral/museo_fr?ACTION=CHERCHER&FIELD_98=REF&VALUE_98=$1', // Museofile
	549: 'http://genealogy.math.ndsu.nodak.edu/id.php?id=$1', // MGP
	557: 'http://www.diseasesdatabase.com/ddb$1.htm', // DiseasesDB
	563: 'http://www.progenetix.org/cgi-bin/subsets.cgi?project=progenetix&subsetType=ICDM&ICDMcodes_m=$1', // ICD-O
	586: 'http://www.ipni.org/ipni/idAuthorSearch.do?id=$1', // IPNI author
	587: 'http://www.marinetraffic.com/ais/fr/shipdetails.aspx?MMSI=$1', // MMSI author
	590: 'http://geonames.usgs.gov/pls/gnispublic/f?p=gnispq:3:::NO::P3_FID:$1', // GNIS
	594: 'http://www.ensembl.org/Homo_sapiens/Gene/Summary?db=core;g=$1', // Ensembl ID
	597: 'http://www.wtatennis.com/players/player/$1', // WTA id
	599: 'http://www.itftennis.com/procircuit/players/player/profile.aspx?playerid=$1', // ITF id
	600: 'http://appdb.winehq.org/appview.php?iAppId=$1', // Wine AppDB-ID
	604: 'http://www.nlm.nih.gov/medlineplus/ency/article/$1.htm', // MedlinePlus
	627: 'http://www.iucnredlist.org/details/$1/0', // IUCN
	630: 'http://www.v2asp.paris.fr/commun/v2asp/v2/nomenclature_voies/Voieactu/$1.nom.htm', // Paris streets
	633: 'http://www.patrimoine-culturel.gouv.qc.ca/rpcq/detail.do?methode=consulter&id=$1&type=bien', // Quebec protected places
	638: 'http://www.rcsb.org/pdb/explore/explore.do?structureId=$1', // PDB ID
	640: 'http://www.culture.gouv.fr/public/mistral/leonore_fr?ACTION=CHERCHER&FIELD_1=COTE&VALUE_1=$1', // Léonore
	646: 'http://www.freebase.com$1', // Freebase ID (note that property values are supposed to start with slashes)
	648: 'http://openlibrary.org/books/$1', // Open Library
	650: 'http://www.rkd.nl/rkddb/dispatcher.aspx?action=search&database=ChoiceArtists&search=priref=$1', // RKDartists
	651: 'http://www.biografischportaal.nl/persoon/$1', // BPN
	652: 'http://fdasis.nlm.nih.gov/srs/srsdirect.jsp?regno=$1', // UNII
	653: 'http://www.ncbi.nlm.nih.gov/pubmedhealth/PMH$1', // PubMed Health
	661: 'http://www.chemspider.com/Chemical-Structure.$1', // Chemspider
	662: 'http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=$1', // PubChem ID
	665: 'http://www.kegg.jp/entry/$1', //  KEGG ID
	668: 'http://www.ncbi.nlm.nih.gov/books/$1', //  GeneReviews ID
	671: 'http://www.informatics.jax.org/marker/MGI:$1', // Mouse Gene Informatics ID
	673: 'http://emedicine.medscape.com/article/$1', // eMedicine
	675: 'http://books.google.com/books?id=$1', // Google Books identifier
	677: 'http://drusop.nature.cz/ost/chrobjekty/chrob_find/index.php?frame=1&h_kod=$1', // ÚSOP Code
	683: 'https://www.ebi.ac.uk/chebi/searchId.do?chebiId=CHEBI:$1', // ChEBI
	685: 'http://www.ncbi.nlm.nih.gov/taxonomy/$1', // NCBI Taxonomy ID
	686: 'http://amigo.geneontology.org/cgi-bin/amigo/term_details?term=GO:$1', // Gene Ontology ID
	687: 'http://biodiversitylibrary.org/page/$1', // BHL Page ID
	691: 'http://aut.nkp.cz/$1', // NK CR
	699: 'http://disease-ontology.org/term/DOID%3A$1', // diseases ontology ID
	701: 'http://dodis.ch/$1', // DODIS
	709: 'http://hsewsf.sedsh.gov.uk/hslive/hsstart?P_HBNUM=$1', // Historic Scotland ID
	715: 'http://www.drugbank.ca/drugs/DB$1', // Drugbank ID
	716: 'http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=$1', // JPL Small-Body Database
	717: 'http://newton.dm.unipi.it/neodys/index.php?pc=2.1.0&o=$1', // MP
	718: 'http://canmore.rcahms.gov.uk/en/site/$1/details/', // Canmore ID
	721: 'http://www.mosclassific.ru/mClass/okato_view.php?text=$1', // OKATO
	723: 'http://www.dbnl.org/auteurs/auteur.php?id=$1', // DBNL ID
	724: 'https://archive.org/details/$1', // Internet Archive ID
	727: 'http://www.europeana.eu/portal/record/$1.html', // Europeana ID
	731: 'http://www.bgr.de/app/litholex/gesamt_ausgabe_neu.php?id=$1', // Litholex ID
	732: 'http://www.bgs.ac.uk/Lexicon/lexicon.cfm?pub=$1', // BGX Lexicon ID
	733: 'http://www.dinoloket.nl/$1', // Dinoloket ID
	757: 'http://whc.unesco.org/' + ( $.inArray( BASE_LANGUAGE, ['en', 'fr'] ) !== -1 ? BASE_LANGUAGE : 'en' ) + '/list/$1', // UNESCO World Heritage
	758: 'http://www.kulturminnesok.no/kulturminnesok/kulturminne/?LOK_ID=$1', // Kulturminne
	759: 'https://hermis.alberta.ca/ARHP/Details.aspx?DeptID=1&ObjectID=$1', // Alberta Historic Places ID
	763: 'http://www.gov.pe.ca/hpo/app.php?nav=details&p=$1&lang=' + ( $.inArray( BASE_LANGUAGE.substring(0,2), ['fr'] ) !== -1 ? 'F' : 'E' ), // Prince Edward Island Historic Places ID
	764: 'http://classif.spb.ru/classificators/view/tmo.php?st=A&kr=1&kod=$1', // OKTMO
	781: 'http://www.sikart.ch/KuenstlerInnen.aspx?id=$1&lng=' + ( $.inArray( BASE_LANGUAGE, ['de', 'fr', 'it'] ) !== -1 ? BASE_LANGUAGE : 'en' ), // Sikart
	804: 'http://geonames.usgs.gov/pls/gnispublic/f?p=gnispq:5:::NO::P5_ANTAR_ID:$1', // GNIS Antarctica ID
	808: '//toolserver.org/~platonides/wlm/ids.php?bic=$1', // code Bien de Interés Cultural (BIC)
	809: 'http://protectedplanet.net/sites/$1', // WDPA ID
	815: 'http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=$1', // ITIS TSN
	818: 'http://arxiv.org/abs/$1', // arXiv
	819: 'http://adsabs.harvard.edu/abs/$1', // ADS bibcode
	821: 'http://www4.rncan.gc.ca/search-place-names/unique.php?id=$1', // CGNDB ID
	824: 'http://www.lpi.usra.edu/meteor/metbull.php?code=$1', // ADS bibcode
	827: 'http://www.bbc.co.uk/programmes/$1', // BBC Identifier
	829: 'http://oeis.org/$1', //  On-Line Encyclopedia of Integer Sequences
	830: 'http://eol.org/pages/$1', // EOL
	838: 'http://www.biolib.cz/en/taxon/id$1', // BioLib
	839: 'http://imslp.org/wiki/$1', // IMSLP ID
	842: 'http://paleodb.org/cgi-bin/bridge.pl?a=checkTaxonInfo&taxon_no=$1', // Paleobiology Database ID
	845: 'http://heritageapp.cyr.gov.sk.ca/public/index.cfm?action=propdetails&id=$1', // Saskatchewan ID
	846: 'http://data.gbif.org/species/$1', // GBIF-ID
	850: 'http://www.marinespecies.org/aphia.php?p=taxdetails&id=$1', // WoRMS-ID
	860: 'http://www.e-archiv.li/personDetail.aspx?persID=$1', // e-archiv.li
	861: 'http://www.premiershiprugby.com/matchcentre/fixtures/squads.php?player=$1&includeref=dynamic', // premiershiprugby.com
	863: 'https://inpho.cogs.indiana.edu/idea/$1.html', //  Indiana Philosophy Ontology project
	864: 'http://dl.acm.org/author_page.cfm?id=$1', // Association for Computing Machinery Digital Library author identifier
	865: 'http://bmlo.de/$1', // Bayerisches Musiker-Lexikon Online
	866: 'http://www.perlentaucher.de/autor/$1.html', // Perlentaucher
	867: 'http://recrutement.pole-emploi.fr/fichesrome/ficherome?codeRome=$1&domaine=Candidat', // Rome occupation code
	884: 'http://vwo.osm.rambler.ru/?page=findname&name=$1', // State Water Register Code (Russia)
	886: 'http://www.e-lir.ch/e-LIR___Lexicon.$1.450.0.html', // Lexicon istoric retic (LIR)
	888: 'http://www.jstor.org/stable/$1', // JSTOR
	889: 'http://www.ams.org/mathscinet-getitem?mr=$1', // ?Mathematical Reviews identifier
	892: 'http://tools.ietf.org/html/rfc$1', // Request for Comments number (IETF)
	893: 'http://ssrn.com/abstract=$1', // Social Science Research Network
	902: 'http://www.hls-dhs-dss.ch/textes/' + ( $.inArray( BASE_LANGUAGE, ['fr'] ) !== -1 ? 'f/F' : ( $.inArray( BASE_LANGUAGE, ['it'] ) !== -1 ? 'i/I' : 'd/D' )) + '$1.php', // HDS/HLS/DHS/DSS identifier
	905: 'http://port.' + ( $.inArray( BASE_LANGUAGE.substring(0,2), ['ro', 'cs' , 'sk', 'hr', 'sr'] ) !== -1 ? ( $.inArray( BASE_LANGUAGE, ['cs'] ) !== -1 ? 'cz' : ( $.inArray( BASE_LANGUAGE, ['sr'] ) !== -1 ? 'rs' : BASE_LANGUAGE ) ) : 'hu' ) + '/pls/fi/films.film_page?i_film_id=$1', // PORT-network identifier
	906: '//libris.kb.se/auth/$1', // Libris library catalog
	907: 'http://allgame.com/game.php?id=$1', // AMG Game ID
	909: 'https://eapps.ednet.ns.ca/HPIPublic/PropertyDisplay.aspx?Fid=$1', // Nova Scotia Register of Historic Places ID
	918: 'http://www5.' + ( $.inArray( BASE_LANGUAGE.substring(0,2), ['fr'] ) !== -1 ? 'rhdcc.gc.ca/CNP/Francais/CNP/2011/Profil' : 'hrsdc.gc.ca/noc/english/noc/2011/Profile' ) + '.aspx?val=7&val1=$1', // NOC Occupation Code
	920: 'http://id.sgcb.mcu.es/lem/ver/Autoridades/$1/concept', // Spanish subject headings for public libraries
	933: 'http://www.heritagefoundation.ca/property-search/property-details-page.aspx?id=$1', // heritagefoundation.ca ID
	935: '//commons.wikimedia.org/wiki/$1', // Commons gallery
	938: 'http://www.fishbase.org/Summary/speciesSummary.php?id=$1',  // Fishbase ID
	939: 'http://www.ksh.hu/apps/!cp.hnt2.telep?nn=$1', // Hungarian Central Statistical Office (KSH)
	947: 'http://aleph.rsl.ru/F?func=find-b&find_code=SYS&adjacent=Y&local_base=RSL11&request=$1', // Russian State Library identifier
	949: 'http://a20.libnet.ac.il/F?func=find-b&REQUEST=$1&find_code=SYS&local_base=NNL10', // NLI (Israel) identifier
	950: 'http://catalogo.bne.es/uhtbin/authoritybrowse.cgi?action=display&authority_id=$1', // BNE identifier
	959: 'http://www.departments.bucknell.edu/biology/resources/msw3/browse.asp?s=y&id=$1', // MSW species identifier
	960: 'http://www.tropicos.org/Name/$1',  //Tropicos taxon name identifier
	961: 'http://www.ipni.org/ipni/idPlantNameSearch.do?id=$1', // IPNI taxon name identifier
	962: 'http://www.mycobank.org/MycoTaxo.aspx?Link=T&Rec=$1', // MycoBank taxon name identifier
	966: '//musicbrainz.org/label/$1', // MusicBrainz Label ID
	982: '//musicbrainz.org/area/$1', // MusicBrainz Area ID
	1003: 'http://alephnew.bibnat.ro:8991/F?func=find-b&request=$1&find_code=SYS&adjacent=Y&local_base=NLR10', // NLR (Romania) identifier
	1004: '//musicbrainz.org/place/$1', // MusicBrainz Place ID
	1006: 'http://opc4.kb.nl/PPN?PPN=$1', // NTA name identifier
	1014: 'http://www.getty.edu/vow/AATFullDisplay?find=&logic=&note=&page=&subjectid=$1', // AAT
	1025: 'http://www.sudoc.fr/$1', // SUDOC catalog (editions only)
	1036: 'http://dewey.info/class/$1/about', // DDC
	1042: 'http://dispatch.opac.d-nb.de/DB=1.1/CMD?ACT=SRCHA&IKT=8506&TRM=$1', // ZDB identifier
	1045: 'http://www.assemblee-nationale.fr/sycomore/fiche.asp?num_dept=$1', // Sycomore
	1047: 'http://www.catholic-hierarchy.org/bishop/b$1.html' // Catholic Hierarchy
	// Note: Watch out to keep the trailing comma at the end of every entry other than the last!
};

mw.hook( 'wikipage.content' ).add( function() {
	if( mw.config.get( 'wbEntityType' ) !== 'item' ) {
		// Only item pages feature appropriate statements.
		return;
	}

	// The gadget should actually make use of a hook implemented in Wikibase. Since there is no
	// hook yet, there is no proper way to detect whether statementview widgets are initialized.
	try {
		$( ':wikibase-statementview' );
		initGadget();
	} catch( e ) {
		$( '.wb-entity' ).on( 'entityviewcreate', initGadget );
	}
} );

/**
 * Initializes the gadget.
 * This procedure needs to be perform as good as possible. jQuery selector usage should be limited
 * to a minimum.
 */
function initGadget() {
	$( ':wikibase-statementview' ).each( function() {
		var $statementview = $( this ),
			statementview = $.data( this, 'statementview' ),
			statement = statementview.value(),
			mainSnak = statement.getMainSnak(),
			mainSnakPropertyId = mainSnak.getPropertyId(),
			qualifiers = statement.getQualifiers(),
			references = statementview.getReferences(),
			// Do not look for referenceview DOM nodes before they are actually needed:
			$referenceviews = null;

		for( var numericPropertyId in PROPERTIES ) {
			var definedPropertyId = 'P' + numericPropertyId;

			if( mainSnakPropertyId === definedPropertyId ) {
				createLink(
					numericPropertyId,
					mainSnak.getValue().getValue(),
					statementview.$mainSnak,
					$statementview
				);
			}

			qualifiers.each( function( i, snak ) {
				if( snak.getPropertyId() === definedPropertyId ) {
					linkSnakview( snak, statementview.$qualifiers, $statementview );
				}
			} );

			for( var i in references ) {
				references[i].getSnaks().each( function( j, snak ) {
					if( snak.getPropertyId() === definedPropertyId ) {
						if( !$referenceviews ) {
							$referenceviews = $statementview.find( ':wikibase-referenceview' );
						}
						linkSnakview( snak, $referenceviews.eq( i ) );
					}
				} );
			}
		}
	} );
}

/**
 * Creates a link that is placed after a snak's input textarea.
 *
 * @param {number} numericPropertyId Refers to PROPERTIES.
 * @param {string} linkId The original string value that shall be used as id in the link.
 * @param {jQuery} $parent The node whose descendants shall be searched for the snak value
 *                 textarea.
 * @param {jQuery} $toolbarParent The node the "edit" toolbar corresponding to the value that shall
 *                 be linked is initialized on.
 */
function createLink( numericPropertyId, linkId, $parent, $toolbarParent ) {
	if( numericPropertyId === 213 ) {
		linkId = linkId.replace( / /g, '' );
	}

	var toolbar = $toolbarParent.data( 'edittoolbar' ).toolbar,
		$btnEdit = toolbar.editGroup.$btnEdit,
		$textarea = $parent.find( '.wb-snak-value .valueview-value textarea.valueview-input' ),
		linkTemplate = PROPERTIES[numericPropertyId];

	var $link = $( '<a/>' )
		.text( $textarea.val() )
		.attr( 'href', linkTemplate.replace( '$1', linkId ) )
		// Show the 'external link' icon:
		.addClass( 'external' );

	$textarea.hide().after( $link );

	// Using ".one" since, currently, link and textarea visibility are not toggled after leaving
	// edit mode anyhow:
	$btnEdit.one( 'click', function() {
		$link.hide();
		$textarea.show();
	} );
}

/**
 * Creates a link for the snakview featuring a specific snak.
 * Since the order of the snaks within a SnakList does not necessarily have to be the same than the
 * displayed/grouped snakviews, all snakviews need to be looped over to find out the snakview that
 * features a specific snak.
 *
 * @param {wikibase.Snak} snakToLink
 * @param {jQuery} $parent The node whose descendants will be searched for snaklistviews
 * @param {jQuery} [$toolbarParent] The node the "edit" toolbar corresponding to the value that
 *                 shall be linked is initialized on.
 */
function linkSnakview( snakToLink, $parent, $toolbarParent ) {
	$toolbarParent = $toolbarParent || $parent;

	$parent.find( '.wb-snakview' ).each( function() {
		var snak = $.data( this, 'wikibaseSnakview' ).snak();

		if( snak.equals( snakToLink ) ) {
			var numericPropertyId = parseInt( snak.getPropertyId().replace( /P/, '' ) );
			createLink( numericPropertyId, snak.getValue().getValue(), $( this ), $toolbarParent );
			return false;
		}
	} );
}

}( mediaWiki, jQuery ) );