/// Gadget-annotationPair.js
// Connect related markers and annotation.
// [email protected] 2024
/// @license: CC-by-sa/4.0 GPLv3
// <nowiki>
/* global window: false */
/* jshint bitwise:true, curly:true, eqeqeq:true, latedef:true,
laxbreak:true,
nocomma:true, strict:true, undef:true, unused:true */
( function ( mw, $ ) {
"use strict";
var GLOBAL = { signature: "annotationPair",
signA: "a",
signM: "m",
k: 0,
recent: false
};
function flip( event ) {
// Handler for annotation click
// Precondition:
// event -- Event object
// event.data -- (Array)
// Uses:
// > GLOBAL.recent
var i, shift, stem;
if ( typeof event === "object" && event ) {
if ( typeof event.preventDefault === "function" ) {
event.preventDefault();
}
if ( typeof event.stopPropagation === "function" ) {
event.stopPropagation();
}
}
if ( GLOBAL.recent &&
event.data === GLOBAL.recent ) {
shift = GLOBAL.recent;
}
shift = shift || event.data;
stem = window.location.href;
i = stem.indexOf( "#" );
if ( i > 0 ) {
stem = stem.substring( 0, i );
}
window.location.href = stem + "#" + shift;
} // flip()
function fired( event ) {
// Handler for marker click
// Precondition:
// event -- Event object
// event.data -- (Array)
// Uses:
// < GLOBAL.recent
GLOBAL.recent = , event.data ];
} // fired()
function friends( apply ) {
// Process one group
// Precondition:
// apply -- object, mapping ID to Array of elements
// Uses:
// > GLOBAL.signA
// > GLOBAL.signM
// >< GLOBAL.k
// (flip)
// (fired)
var i, s, s0, set, si, sign, start, t, $a, $e;
GLOBAL.k++;
start = GLOBAL.k + "-";
for ( s in apply ) {
t = apply;
$e = t;
if ( $e &&
typeof t === "object" ) {
set = start + s;
sign = GLOBAL.sign + set + "-";
s0 = sign + "0";
si = sign + "1";
$a = $( "<a>" );
$a.addClass( GLOBAL.signA )
.attr( { "href": "#" } )
.on( "click",
,
flip );
$e.before( $a );
$e.detach();
$a.append( $e );
$a.parent().attr( { "id": s0 } );
s0 = "#" + s0;
for ( i = 1; i < t.length; i++ ) {
$e = t;
$a = $( "<a>" );
si = sign + i;
$a.addClass( GLOBAL.signM )
.attr( { "href": s0,
"id": si } )
.on( "click",
,
fired );
$e.before( $a );
$e.detach();
$a.append( $e );
} // for i
}
} // for s in apply
} // friends()
function full( $all ) {
// Process elements
// Precondition:
// $all -- all elements in scope
// Uses:
// > GLOBAL.signM
// > GLOBAL.signA
// friends()
var n = $all.length,
$e = $all.eq( 0 ),
start = ( $e.data( GLOBAL.signM ) ? GLOBAL.signM
: GLOBAL.signA ),
targets = { },
i, lead, loop, s, sign, t;
for ( i = 0; i < n; i++ ) {
$e = $all.eq( i );
sign = ( $e.data( GLOBAL.signM ) ? GLOBAL.signM
: GLOBAL.signA );
if ( sign === start ) {
if ( ! lead ) {
loop = false;
}
} else {
lead = false;
}
if ( ! loop ) {
if ( i ) {
friends( targets );
targets = { };
}
lead = true;
loop = true;
}
s = $e.data( sign );
if ( typeof s === "string" ) {
s = s.trim();
}
if ( s ) {
if ( typeof targets === "object" ) {
t = targets;
} else {
t = ;
}
if ( sign === GLOBAL.signA ) {
if ( ! t ) {
t = $e;
}
} else {
t.push( $e );
}
targets = t;
}
$e.attr( "data-" + sign, null );
} // for i
friends( targets );
} // full()
function furnish() {
// Start execution
// Precondition:
// DOM ready
// Uses:
// > GLOBAL.signature
// >< GLOBAL.signA
// >< GLOBAL.signM
// < GLOBAL.sign
// full()
var $got;
GLOBAL.sign = GLOBAL.signature.toLowerCase() + "-";
GLOBAL.signA = GLOBAL.sign + GLOBAL.signA;
GLOBAL.signM = GLOBAL.sign + GLOBAL.signM;
$got = $( ", " +
"" );
if ( $got.length ) {
full( $got );
}
} // furnish()
$( furnish );
}( window.mediaWiki, window.jQuery ) );
// </nowiki>