User:PerfektesChaos/js/pageLinkHelper/d.js

This is an old revision of this page, as edited by PerfektesChaos (talk | contribs) at 19:17, 6 December 2014 (-1.0). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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-05 PerfektesChaos@de.wikipedia
/*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.0,
       Env, PLH;



   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-05 PerfektesChaos@de.wikipedia
      var sign = "pageLinkHelperDifflink",
          $div = $( "#" + sign ),
          got, i, id1, id2, 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 ) {
               if ( id2 ) {
                  i = parseInt( id2, 10 );
                  if ( ! isNaN( i ) ) {
                     id1 = parseInt( id1, 10 );
                     if ( id1 > i ) {
                        id2 = id1;
                        id1 = i;
                     }
                  }
               }
            }
            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" } );
            $div.append( $span );
            $container.append( $div );
         }
      }
   }   // flip()



   function fresh() {
      // Provide purge link
      // Precondition:
      //    DOM.ready
      //    mediawiki.util loaded
      //    user resources loaded
      // Uses:
      //    >  VERSION;
      //    >< PLH
      //    >< Env
      //    mw.config.get()
      //    mw.util.getUrl()
      //    mw.util.addPortletLink()
      // 2014-12-05 PerfektesChaos@de.wikipedia
      var say   = "PURGE this page from server cache",
          seed  = null,
          slot  = "p-cactions",
          show  = "Purge cache",
          start = "ca",
          got, re, swap;
      if ( typeof mw.libs.pageLinkHelper === "object" ) {
         PLH = mw.libs.pageLinkHelper;
         if ( PLH ) {
            PLH.vsn = VERSION;
            switch ( typeof PLH.purge ) {
               case "boolean" :
                  if ( PLH.purge ) {
                     seed = false;
                  } else {
                     slot = false;
                  }
                  break;
               case "string" :
                  seed = PLH.purge;
                  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 PLH.purge
         }
      }
      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:
      //    DOM.ready
      //    mediawiki.util loaded
      // Uses:
      //    >  history
      //    >  ___location
      //    >  document
      //    >< Env
      //    mw.util.getUrl()
      //    history.___pushState()
      //    history.___replaceState()
      // 2014-12-05 PerfektesChaos@de.wikipedia
      var cause, show, last, 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 ) {
            cause  = { caused: "pageLinkHelper" };
            showed = window.document.title;
            state  = window.___location.href;
            show   = "#REDIRECT " + Env.wgRedirectedFrom;
            window.history.___replaceState( cause, show, swap );
            window.history.___pushState( cause, showed, state );
         }
      }
   }   // from()



   function fire() {
      // Autorun on load
      // Precondition:
      //    mediawiki.util loaded
      // Uses:
      //     < Env
      //    mw.config.get()
      //    mw.util.getParamValue()
      //    mw.loader.using()
      //    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" ) {
         mw.loader.using( [ "user" ],
                          fresh );
         if ( Env.wgRedirectedFrom ) {
            from();
         }
      }
   }   // fire()



   if ( mw.loader.state( "ext.gadget.pageLinkHelper" )  !==  "ready" ) {
      mw.loader.state( "ext.gadget.pageLinkHelper", "ready" );
      mw.hook( "wikipage.content" ).add( fire );   // don't hurry
   }
}( window.mediaWiki, window.jQuery ) );