| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
( function () { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
function isNamedReference( id ) { |
| 16 |
|
| 17 |
return /^cite_ref-\D/.test( id ); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
function isReusedNamedReference( id, $content ) { |
| 26 |
if ( !isNamedReference( id ) ) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
return id.slice( -2 ) !== '-0' || |
| 32 |
$content.find( '.references a[href="#' + $.escapeSelector( id.slice( 0, -1 ) ) + '1"]' ).length; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
function makeUpArrowLink( $backlinkWrapper ) { |
| 40 |
let textNode = $backlinkWrapper[ 0 ].firstChild; |
| 41 |
const accessibilityLabel = mw.msg( 'cite_references_link_accessibility_back_label' ); |
| 42 |
const $upArrowLink = $( '<a>' ) |
| 43 |
.addClass( 'mw-cite-up-arrow-backlink' ) |
| 44 |
.attr( 'aria-label', accessibilityLabel ) |
| 45 |
.attr( 'title', accessibilityLabel ); |
| 46 |
|
| 47 |
if ( !textNode ) { |
| 48 |
return $upArrowLink; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
while ( textNode.firstChild ) { |
| 53 |
textNode = textNode.firstChild; |
| 54 |
} |
| 55 |
|
| 56 |
if ( textNode.nodeType !== Node.TEXT_NODE || textNode.data.trim() === '' ) { |
| 57 |
return $upArrowLink; |
| 58 |
} |
| 59 |
|
| 60 |
const upArrow = textNode.data.trim(); |
| 61 |
e. |
| 62 |
textNode.data = textNode.data.replace( upArrow, '' ); |
| 63 |
|
| 64 |
ly |
| 65 |
|
| 66 |
$backlinkWrapper.prepend( |
| 67 |
$( '<span>' ) |
| 68 |
.addClass( 'mw-cite-up-arrow' ) |
| 69 |
.text( upArrow ), |
| 70 |
$upArrowLink |
| 71 |
.text( upArrow ) |
| 72 |
); |
| 73 |
|
| 74 |
return $upArrowLink; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
function updateUpArrowLink( $backlink ) { |
| 81 |
|
| 82 |
const $backlinkWrapper = $backlink.closest( '.mw-cite-backlink, li' ); |
| 83 |
let $upArrowLink = $backlinkWrapper.find( '.mw-cite-up-arrow-backlink' ); |
| 84 |
|
| 85 |
if ( !$upArrowLink.length && $backlinkWrapper.length ) { |
| 86 |
$upArrowLink = makeUpArrowLink( $backlinkWrapper ); |
| 87 |
} |
| 88 |
|
| 89 |
$upArrowLink.attr( 'href', $backlink.attr( 'href' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
mw.hook( 'wikipage.content' ).add( ( $content ) => { |
| 93 |
|
| 94 |
$content.find( '.reference[id] > a' ).on( 'click', function () { |
| 95 |
const id = $( this ).parent().attr( 'id' ); |
| 96 |
|
| 97 |
$content.find( '.mw-cite-targeted-backlink' ).removeClass( 'mw-cite-targeted-backlink' ); |
| 98 |
|
| 99 |
|
| 100 |
if ( !isReusedNamedReference( id, $content ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
const $backlink = $content.find( '.references a[href="#' + $.escapeSelector( id ) + '"]:not(.mw-cite-up-arrow-backlink)' ) |
| 106 |
.first() |
| 107 |
.addClass( 'mw-cite-targeted-backlink' ); |
| 108 |
|
| 109 |
if ( $backlink.length ) { |
| 110 |
updateUpArrowLink( $backlink ); |
| 111 |
} |
| 112 |
} ); |
| 113 |
} ); |
| 114 |
}() ); |