User:PerfektesChaos/js/pageLinkHelper/d.js

This is an old revision of this page, as edited by PerfektesChaos (talk | contribs) at 19:15, 7 December 2014 (-1.2). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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-pageLinkHelper.js
//  Minor link helpers
// ResourceLoader:  compatible;  dependencies: user, mediawiki.util
/// 2014-12-07 PerfektesChaos@de.wikipedia
/// Fingerprint: #0#0#
/// <nowiki>
/*jshint bitwise:true, curly:true, eqeqeq:true, forin:false,
         latedef:true, laxbreak:true, strict:true, trailing:true,
         undef:true, unused:true, white:false                          */
/*global window:false                                                  */
( function ( mw, $ ) {
   "use strict";
   var VERSION = -1.2,
       Env, PLH;



   function feature( applied ) {
      // Check for user option
      // Precondition:
      //    applied  -- option keyword
      //    module 'user' loaded
      // Postcondition:
      //    Returns value, or undefined
      // Uses:
      //    >  VERSION
      //    >< PLH
      // 2014-12-07 PerfektesChaos@de.wikipedia
      var r;   // = undefined
      if ( typeof mw.libs.pageLinkHelper === "object" ) {
         PLH = mw.libs.pageLinkHelper;
         if ( PLH ) {
            PLH.vsn = VERSION;
            if ( typeof PLH[ applied ]  !==  "undefined" ) {
               r = PLH[ applied ];
            }
         }
      }
      return r;
   }   // feature()



   function flip() {
      // Equip diffpage with wikilink for c&p
      // Precondition:
      //    DOM.ready
      //    mediawiki.util loaded
      // Uses:
      //    >  Env
      //    >  ___location
      //    mw.util.getParamValue()
      //    mw.config.get()
      // 2014-12-06 PerfektesChaos@de.wikipedia
      var sign = "pageLinkHelperDifflink",
          $div = $( "#" + sign ),
         got, i, id1, id2, j, re, show, $container, $span;
      if ( ! $div.length ) {
         $container = $( "#contentSub" );
         if ( $container.length ) {
            if ( Env.wgCanonicalSpecialPageName ) {
               // 2014 redirected to &diff=
               re  = /\/wiki\/[^:]+:[^\/]+\/(\d+)(?:\/(\.+))?$/;
               got = re.exec( window.___location.pathname );
               if ( got ) {
                  id1 = got[ 1 ];
                  id2 = got[ 2 ];
               }
            } else {
               id1 = mw.util.getParamValue( "diff" );
               id2 = mw.util.getParamValue( "oldid" );
            }
            if ( id1 ) {
               i = parseInt( id1, 10 );
               if ( id2 ) {
                  if ( isNaN( i ) ) {
                     i   = id1;
                     id1 = id2;
                     id2 = i;
                  } else {
                     j = parseInt( id2, 10 );
                     if ( ! isNaN( j ) ) {
                        if ( i > j ) {
                           id2 = i;
                           id1 = j;
                        }
                     }
                  }
                  if ( id2 === "prev" ) {
                     id2 = false;
                  }
               } else if ( isNaN( i ) ) {
                  id1 = false;
               }
            }
            if ( id1 ) {
               show  = "[["
                       + mw.config.get( "wgFormattedNamespaces" )[ "-1" ]
                       + ":Diff/" + id1
                       + ( id2  ?  "/" + id2  :  "" )
                       + "]]";
               $span = $( "<code>" );
               $span.text( show );
               $div = $( "<div>" );
               $div.attr( { "id": sign } );
               $div.css( { "float":       "right",
                           "margin-top" : "1em" } );
               $div.append( $span );
               $container.append( $div );
            }
         }
      }
   }   // flip()



   function fresh() {
      // Provide purge link
      // Precondition:
      //    DOM.ready
      //    mediawiki.util loaded
      //    user resources loaded
      // Uses:
      //    >< Env
      //    feature()
      //    mw.config.get()
      //    mw.util.getUrl()
      //    mw.util.addPortletLink()
      // 2014-12-07 PerfektesChaos@de.wikipedia
      var opt   = feature( "purge" ),
          say   = "PURGE this page from server cache",
          seed  = null,
          slot  = "p-cactions",
          show  = "Purge cache",
          start = "ca",
          got, re, swap;
      switch ( typeof opt ) {
         case "boolean" :
            if ( opt ) {
               seed = false;
            } else {
               slot = false;
            }
            break;
         case "string" :
            seed = opt;
            re   = /^([a-z]+)-(.+)$/;
            got  = re.exec( seed );
            if ( got ) {
               switch ( got[ 1 ] ) {
                  case "p" :
                     slot = seed;
                     seed = false;
                     switch ( got[ 2 ] ) {
                        case "navigation" :
                           start = "n";
                           break;
                        case "tb" :
                           start = "t";
                           break;
                     }   // switch got.2
                     break;
                  case "ca" :
                     break;
                  case "n" :
                     slot = "p-navigation";
                     break;
                  case "t" :
                     slot = "p-tb";
                     break;
               }   // switch got.1
            }
            break;
      }   // switch typeof opt
      if ( slot  &&  seed === null ) {
         Env.skin = mw.config.get( "skin" );
         if ( Env.skin !== "vector" ) {
            slot = false;
         }
      }
      if ( slot ) {
         Env.wgPageName = mw.config.get( "wgPageName" );
         swap = mw.util.getUrl( Env.wgPageName,
                                { action: "purge" } );
         if ( seed ) {
            seed = "#" + seed;
         }
         mw.util.addPortletLink( slot,
                                 swap,
                                 show,
                                 start + "-purge",
                                 say,
                                 null,
                                 seed );
      }
   }   // fresh()



   function from() {
      // Redirected from somewhere
      // Precondition:
      //    mediawiki.util loaded
      // Uses:
      //    >  history
      //    >  ___location
      //    >  document
      //    >< Env
      //    mw.util.getUrl()
      //    feature()
      //    history.___pushState()
      //    history.___replaceState()
      // 2014-12-07 PerfektesChaos@de.wikipedia
      var caused, opts, show, last, legacy, showed, state, swap;
      if ( typeof window.history.___pushState === "function"  &&
           typeof window.history.___replaceState === "function" ) {
         swap = mw.util.getUrl( Env.wgRedirectedFrom,
                                { redirect: "no" } );
         if ( window.history.previous ) {
            // should actually not cause wgRedirectedFrom, but avoid
            last = ( window.history.previous.indexOf( swap )  >=  0 );
         }
         if ( ! last ) {
            opts = feature( "redirect" );
            if ( opts   &&
                 typeof opts  ===  "object"   &&
                 typeof opts.legacy  ===  "boolean" ) {
               legacy = opts.legacy;
            }
            caused = { caused: "pageLinkHelper" };
            showed = window.document.title;
            if ( legacy ) {
               state = mw.util.getUrl( Env.wgRedirectedFrom );
            } else {
               state = window.___location.href;
            }
            show = "#REDIRECT " + Env.wgRedirectedFrom;
            window.history.___replaceState( caused, show, swap );
            window.history.___pushState( caused, showed, state );
         }
      }
   }   // from()



   function fire() {
      // Autorun on load
      // Precondition:
      //    mediawiki.util loaded
      // Uses:
      //     < Env
      //    mw.config.get()
      //    mw.util.getParamValue()
      //    flip()
      //    fresh()
      //    from()
      // 2014-12-05 PerfektesChaos@de.wikipedia
      Env = mw.config.get( [ "wgAction",
                             "wgCanonicalSpecialPageName",
                             "wgRedirectedFrom" ] );
      if ( mw.util.getParamValue( "diff" )  ||
           Env.wgCanonicalSpecialPageName === "Diff" ) {
         flip();
      } else if ( ! Env.wgCanonicalSpecialPageName
                  &&  Env.wgAction === "view" ) {
         fresh();
         if ( Env.wgRedirectedFrom ) {
            from();
         }
      }
   }   // fire()



   if ( mw.loader.state( "ext.gadget.pageLinkHelper" )  !==  "ready" ) {
      mw.loader.state( "ext.gadget.pageLinkHelper", "ready" );
      mw.loader.using( [ "mediawiki.util",
                         "user" ],
                       function () {
                          $( fire );   // don't hurry
                       } );
   }
}( window.mediaWiki, window.jQuery ) );
/// EOF </nowiki>   pageLinkHelper.js