2018-11-15 17:32:38 +00:00
|
|
|
/**
|
|
|
|
* @author Thiemo Kreuz
|
|
|
|
*/
|
|
|
|
( function () {
|
|
|
|
'use strict';
|
|
|
|
|
2019-02-20 15:04:33 +00:00
|
|
|
/**
|
|
|
|
* Checks if the ID uses a composite format that does not only consist of a sequential number,
|
|
|
|
* as specified in "cite_reference_link_key_with_num".
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isNamedReference( id ) {
|
|
|
|
var prefix = mw.msg( 'cite_reference_link_prefix' );
|
|
|
|
|
|
|
|
// Note: This assumes IDs start with the prefix; this is guaranteed by the parser function
|
|
|
|
return /\D/.test( id.slice( prefix.length ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {jQuery} $content
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isReusedNamedReference( id, $content ) {
|
|
|
|
if ( !isNamedReference( id ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either the ID is already a reuse, or at least one reuse exists somewhere else on the page
|
|
|
|
return id.slice( -2 ) !== '-0' ||
|
|
|
|
$content.find( '.references a[href="#' + $.escapeSelector( id.slice( 0, -1 ) ) + '1"]' ).length;
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:03:02 +00:00
|
|
|
/**
|
|
|
|
* @param {jQuery} $backlinkWrapper
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
function makeUpArrowLink( $backlinkWrapper ) {
|
2021-11-03 12:28:17 +00:00
|
|
|
var textNode = $backlinkWrapper[ 0 ].firstChild,
|
2018-12-11 14:18:08 +00:00
|
|
|
accessibilityLabel = mw.msg( 'cite_references_link_accessibility_back_label' ),
|
|
|
|
$upArrowLink = $( '<a>' )
|
|
|
|
.addClass( 'mw-cite-up-arrow-backlink' )
|
|
|
|
.attr( 'aria-label', accessibilityLabel )
|
|
|
|
.attr( 'title', accessibilityLabel );
|
2018-11-30 14:03:02 +00:00
|
|
|
|
|
|
|
if ( !textNode ) {
|
|
|
|
return $upArrowLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip additional, custom HTML wrappers, if any.
|
|
|
|
while ( textNode.firstChild ) {
|
|
|
|
textNode = textNode.firstChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( textNode.nodeType !== Node.TEXT_NODE || textNode.data.trim() === '' ) {
|
|
|
|
return $upArrowLink;
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
var upArrow = textNode.data.trim();
|
2018-11-30 14:03:02 +00:00
|
|
|
// The text node typically contains "↑ ", and we need to keep the space.
|
|
|
|
textNode.data = textNode.data.replace( upArrow, '' );
|
|
|
|
|
|
|
|
// Create a plain text and a clickable "↑". CSS :target selectors make sure only
|
|
|
|
// one is visible at a time.
|
|
|
|
$backlinkWrapper.prepend(
|
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 'mw-cite-up-arrow' )
|
|
|
|
.text( upArrow ),
|
|
|
|
$upArrowLink
|
2018-12-11 14:18:08 +00:00
|
|
|
.text( upArrow )
|
2018-11-30 14:03:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $upArrowLink;
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:18:08 +00:00
|
|
|
/**
|
|
|
|
* @param {jQuery} $backlink
|
|
|
|
*/
|
|
|
|
function updateUpArrowLink( $backlink ) {
|
|
|
|
// It's convenient to stop at the class name, but it's not guaranteed to be there.
|
|
|
|
var $backlinkWrapper = $backlink.closest( '.mw-cite-backlink, li' ),
|
|
|
|
$upArrowLink = $backlinkWrapper.find( '.mw-cite-up-arrow-backlink' );
|
|
|
|
|
|
|
|
if ( !$upArrowLink.length && $backlinkWrapper.length ) {
|
|
|
|
$upArrowLink = makeUpArrowLink( $backlinkWrapper );
|
|
|
|
}
|
|
|
|
|
|
|
|
$upArrowLink.attr( 'href', $backlink.attr( 'href' ) );
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:32:38 +00:00
|
|
|
mw.hook( 'wikipage.content' ).add( function ( $content ) {
|
|
|
|
// We are going to use the ID in the code below, so better be sure one is there.
|
2019-01-08 23:06:43 +00:00
|
|
|
$content.find( '.reference[id] > a' ).on( 'click', function () {
|
2021-11-03 12:28:17 +00:00
|
|
|
var id = $( this ).parent().attr( 'id' );
|
2018-11-15 17:32:38 +00:00
|
|
|
|
2020-06-09 10:27:52 +00:00
|
|
|
$content.find( '.mw-cite-targeted-backlink' ).removeClass( 'mw-cite-targeted-backlink' );
|
2018-12-11 14:18:08 +00:00
|
|
|
|
2018-11-28 10:56:27 +00:00
|
|
|
// Bail out if there is not at least a second backlink ("cite_references_link_many").
|
2019-02-20 15:04:33 +00:00
|
|
|
if ( !isReusedNamedReference( id, $content ) ) {
|
2018-11-28 10:56:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The :not() skips the duplicate link created below. Relevant when double clicking.
|
2021-11-03 12:28:17 +00:00
|
|
|
var $backlink = $content.find( '.references a[href="#' + $.escapeSelector( id ) + '"]:not(.mw-cite-up-arrow-backlink)' )
|
2018-11-28 10:56:27 +00:00
|
|
|
.first()
|
2020-06-09 10:27:52 +00:00
|
|
|
.addClass( 'mw-cite-targeted-backlink' );
|
2018-11-19 09:25:21 +00:00
|
|
|
|
2018-12-11 14:18:08 +00:00
|
|
|
if ( $backlink.length ) {
|
|
|
|
updateUpArrowLink( $backlink );
|
2018-11-19 09:25:21 +00:00
|
|
|
}
|
2018-11-15 17:32:38 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}() );
|