Jump to content

User:PerfektesChaos/js/prettyPageHeading/d.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/// Gadget-prettyPageHeading.js
//  Format page header nice if namespace and/or subpage
/// 2018-08-25 PerfektesChaos@de.wikipedia
//  ResourceLoader: compatible;
//    dependencies: NONE
/// Fingerprint:    #0#0#
//  Documentation:  [[w:en:User:PerfektesChaos/js/prettyPageHeading]]
/// <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 Version   = -1.4,
       Sign      = "prettyPageHeading",
       Shift     = "0.25em",
       Shop      = "w:en:User:PerfektesChaos/js/",
       Config, Nnsp, Rooms, Store;



   function $flip( $assembly, apply, ahead, above ) {
      // prepend one shifted text element
      // Precondition:
      //    $assembly  -- title
      //    apply      -- substring
      //    ahead      -- margin attribute
      //    above      -- link items on top
      // Uses:
      //    >  Sign
      //    >  Shift
      //    >  Store
      // 2015-12-26 PerfektesChaos@de.wikipedia
      var $el = $( above ? "<a>" : "<span>" );
      if ( above ) {
         $el.attr( "href",  Store + encodeURI( above ) );
      }
      $assembly.prepend( $el.attr( "class", Sign )
                            .css( ahead, Shift )
                            .text( apply ) );
   }   // $flip()



   function fiat() {
      // Replace page header by new title
      // Precondition:
      //    DOM.ready
      // Uses:
      //    >  Nnsp
      //    >< Store
      //    >< Rooms
      //    mw.config.get()
      //    $flip()
      // 2018-04-22 PerfektesChaos@de.wikipedia
      var $h1 = $( "#firstHeading,#section_0" ),
          env, i, k, ns, parts, plus,
          shift, space, story, superior, support,
          $ch, $el, $space;
      if ( $h1.length ) {
         $h1 = $h1.eq( 0 );
      } else {
          $h1 = $( "<h1>" );
      }
      $ch = $h1.children();
      ns  = $ch.length;
      for ( i = ns - 1;  i >= 0;  i-- ) {
         $el = $ch.eq( i );
         if ( $el.get( 0 ).nodeName.toUpperCase()  ===  "MARK" ) {
            // ignore temporary markup
            ns = i;
         } else if ( $el.hasClass( "title-shortlink-container" )  ||
                     $el.hasClass( "title-shortlink-icon" ) ) {
            plus = plus  ||  [ ];
            plus.push( $el.detach() );
            ns = i;
         } else {
            break;
         }
      }   // for i--
      if ( ! ns ) {
         env = mw.config.get( [ "wgContentLanguage",
                                "wgFormattedNamespaces",
                                "wgUserLanguage" ] );
         if ( Store ) {
            space = env.wgFormattedNamespaces[ Nnsp + "" ];
            Store = Store  +  encodeURI( space )  +  ":";
            $( ".subpages" ).hide();
         }
         // No DISPLAYTITLE surprise
         if ( $( "html" ).attr( "dir" )  ===  "rtl" ) {
            shift = "margin-right";
         } else {
            shift = "margin-left";
         }
         story = $h1.text();   // DISPLAYTITLE
         $h1.empty();
         ns    = story.indexOf( ":" ) + 1;
         space = story.substr( 0, ns );
         parts = story.substr( ns ).split( "/" );
         for ( i = parts.length - 1;  i >= 0;  i-- ) {
            $flip( $h1, parts[ i ], shift, superior );
            if ( i ) {
               $flip( $h1, "/", shift );
            }
            if ( Store ) {
               superior = "";
               for ( k = 0;  k < i;  k++ ) {
                  superior = superior  +  ( k ? "/" : "" )
                                       +  parts[ k ];
               }   // for k++
            }
         }   // for i--
         $space = $( "<span>" );
         $space.text( space );
         if ( Nnsp <= 15  &&
              env.wgUserLanguage !== env.wgContentLanguage ) {
            if ( Nnsp < 0 ) {
               support = "Special";
            } else {
               Rooms   = [ "(main)",
                           "User",
                           "Project",
                           "File",
                           "MediaWiki",
                           "Template",
                           "Help",
                           "Category" ];
               i       = Nnsp % 2;
               ns      = ( Nnsp - i )  /  2;
               support = Rooms[ ns ];
               if ( i ) {
                  support = support + " Talk";
               }
            }
            if ( support !== space ) {
               $space.attr( "title", support );
            }
         }
         $h1.prepend( $space );
         if ( plus ) {
            for ( i = plus.length - 1;  i >= 0;  i-- ) {
               $h1.append( plus[ i ] );
            }   // for i--
         }
      }
   }   // fiat()



   function fire() {
      // Autorun on load (once)
      // Uses:
      //    >  Sign
      //    >  Version
      //    >  Shop
      //     < Nnsp
      //     < Config
      //     < Store
      //     < Shift
      //    mw.config.get()
      //    (fiat)
      // 2018-08-25 PerfektesChaos@de.wikipedia
      var signature = "ext.gadget." + Sign,
          rls;
      if ( mw.loader.getState( signature )  !==  "ready" ) {
         rls = { };
         rls[ signature ] = "ready";
         mw.loader.state( rls );
         Nnsp = mw.config.get( "wgNamespaceNumber" );
         if ( Nnsp > 0 ) {
            if ( typeof mw.libs[ Sign ]  ===  "object" ) {
               Config = mw.libs[ Sign ];
               if ( Config ) {
                  Config.vsn = Version;
                  Config.doc = "[[" + Shop + Sign + "]]";
                  if ( typeof Config.link  ===  "boolean"
                       &&     Config.link ) {
                     Store = "/wiki/";
                  }
                  if ( typeof Config.shift  ===  "string" ) {
                     Shift = Config.shift;
                  }
               }
            }
            $( fiat );
         }
      }
   }   // fire()



   fire();
}( window.mediaWiki, window.jQuery ) );

/// EOF </nowiki>   prettyPageHeading.js