Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
/// Gadget-annotationPair.js
// Connect related markers and annotation.
// PerfektesChaos@de.wikipedia 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) [ pair name, fallback id ]
      // 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[ 0 ]  ===  GLOBAL.recent[ 0 ] ) {
         shift = GLOBAL.recent[ 1 ];
      }
      shift = shift  ||  event.data[ 1 ];
      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) [ pair name, back id ]
      // Uses:
      //     < GLOBAL.recent
      GLOBAL.recent = [ event.data[ 0 ],  event.data[ 1 ] ];
   }   // 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[ s ];
         $e =  t[ 0 ];
         if ( $e  &&
              typeof t[ 1 ]  ===  "object" ) {
            set  = start + s;
            sign = GLOBAL.sign + set + "-";
            s0   = sign + "0";
            si   = sign + "1";
            $a   = $( "<a>" );
            $a.addClass( GLOBAL.signA )
              .attr( { "href": "#",
                       "id":   s0 } )
              .on( "click",
                   [ set, si ],
                   flip );
            $e.before( $a );
            $e.detach();
            $a.append( $e );
            s0 = "#" + s0;
            for ( i = 1;  i < t.length;  i++ ) {
               $e = t[ i ];
               $a = $( "<a>" );
               si = sign + i;
               $a.addClass( GLOBAL.signM )
                 .attr( { "href": s0,
                          "id":   si } )
                 .on( "click",
                      [ set, si ],
                      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[ s ]  ===  "object" ) {
               t = targets[ s ];
            } else {
               t = [ false ];
            }
            if ( sign === GLOBAL.signA ) {
               if ( ! t[ 0 ] ) {
                  t[ 0 ] = $e;
               }
            } else {
               t.push( $e );
            }
            targets[ s ] = 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 = $( "[ data-" + GLOBAL.signA + "], " +
                "[ data-" + GLOBAL.signM + "]" );
      if ( $got.length ) {
         full( $got );
      }
   }   // furnish()



   $( furnish );
}( window.mediaWiki, window.jQuery ) );
// </nowiki>