Jump to content

User:PerfektesChaos/js/logidTooltip/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/logidTooltip/d.js
//  Pop up logid link on certain special pages
/// 2020-08-01 PerfektesChaos@de.wikipedia
//  ResourceLoader: compatible;
//    dependencies: oojs, oojs-ui-core, oojs-ui-widgets
/// Fingerprint:    #0#0#
/// Documentation:  [[w:en:User:PerfektesChaos/js/logidTooltip]]
/// @license:       CC-by-sa/4.0 GPLv3
/// <nowiki>
/* global window: false                                                */
/* jshint forin: false,
          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.3,
       LIT     = { signature: "logidTooltip",
                   source:    "0f/Arrow Blue UpperRight 001.svg",
                   search:    "&action=edit&redlink=1",
                   selector:  "data-mw-logid",
                   npx:       16,
                   $body:     false,
                   $img:      false
                 },
       OO;



   // -------------------------------------------------------------------



   function filepath( assign ) {
      // Configure icon file ID
      // Precondition:
      //    assign   -- string with file upload path
      // Uses:
      //    >< LIT.reFile
      //     < LIT.source
      // 2019-10-01 PerfektesChaos@de.wikipedia
      var s = "[a-z0-9]";
      if ( typeof assign  ===  "string" ) {
         if ( typeof LIT.reFile  !==  "object" ) {
            LIT.reFile = new RegExp( "^" + s + s +
                                     "/[^a-z][^/#<>{}]+\\.[a-zA-Z]*$" );
         }
         if ( LIT.reFile.test( assign ) ) {
            LIT.source = assign;
         }
      }
   }   // filepath()



   function fire() {
      // Initialize
      // Uses:
      //    >  LIT.signature
      //    >  Version
      //    >  LIT.search
      //     < LIT.vsn
      //     < LIT.doc
      //     < LIT.signed
      //     < LIT.special
      //    mw.loader.getState()
      //    mw.config.get()
      //    mw.hook()
      //    (filepath)
      //    (LIT.furnish)
      // 2020-08-01 PerfektesChaos@de.wikipedia
      var signature = "ext.gadget." + LIT.signature,
          env, launch, rls;
      if ( mw.loader.getState( signature ) !== "ready" ) {
         rls = { };
         rls[ signature ] = "ready";
         mw.loader.state( rls );
         LIT.vsn = Version;
         LIT.doc = "w:en:User:PerfektesChaos/js/" + LIT.signature;
         LIT.doc = "[[" + LIT.doc + "]]";
         env = mw.config.get( [ "wgCanonicalSpecialPageName",
                                "wgFormattedNamespaces" ] );
         switch ( env.wgCanonicalSpecialPageName ) {
            case "Log":
            case "Recentchanges":
            case "Watchlist":
               launch = true;
               break;
            default:
               launch = ( window.location.search.indexOf( LIT.search )
                                                                 >=  0 );
         }   // switch wgCanonicalSpecialPageName
         if ( launch ) {
            LIT.signed  = LIT.signature + "-handle";
            LIT.special = env.wgFormattedNamespaces[ "-1" ];
            mw.hook( LIT.signature + "-icon" ).add( filepath );
            mw.hook( "wikipage.content" ).add( LIT.furnish );
         }
      }
   }   // fire()



   LIT.$factory = function( assigned, $anchor ) {
      // Create popup
      // Precondition:
      //    assigned   -- string, with logid
      //    $anchor    -- element to be attached to
      //    OOjs-ui ready
      // Postcondition:
      //    Return popup
      // Uses:
      //    >  LIT.special
      //    >  LIT.signature
      //    >  OO
      //    >  .$body
      //    OO.ui.PopupWidget()
      // 2019-10-01 PerfektesChaos@de.wikipedia
      var illusive = 0.75,
//        margin   = 10,
          offset   = $anchor.offset(),
          options  = { anchor:              true,
                       padded:              true,
                       position:            "below",
                       verticalPosition:    "below",
                       $floatableContainer: $anchor },
          shift    = LIT.special + ":Redirect/logid/" + assigned,
          $a       = $( "<a>" ),
          r;
      options.width = ( 12 + assigned.length * 0.7 ) + "em";
      if ( offset.left  <  0.5 * window.innerWidth ) {
         options.align              = "forwards";
         options.horizontalPosition = "start";
      } else {
         options.align              = "backwards";
         options.horizontalPosition = "end";
      }
      $a.addClass( LIT.signature + "-text plainlinks" )
        .attr( { "dir":   "ltr",
                 "href":  "/wiki/" + shift,
                 "id":    LIT.signature + "-text-" + assigned,
                 "lang": "en" } )
        .css( { "font-family":  "sans-serif",
                "font-size":    "medium",
                "font-style":   "normal",
                "font-variant": "normal",
                "font-weight":  "normal",
                "white-space":  "nowrap" } )
        .text( "[[" + shift + "]]" );
      options.$content = $a;
      r = new OO.ui.PopupWidget( options );
      r.$floatable.attr( { "role": "tooltip" } )
                  .css( { "opacity":    illusive,
                          "padding":    "2px",
                          "text-align": "center" } );
      LIT.$body.append( r.$floatable );
      return r;
   };   // .$factory()



   LIT.fiat = function() {
      // Deal with log entry
      // Precondition:
      //    this   -- LI DOM element
      // Uses:
      //    >  LIT.signed
      //    >  LIT.selector
      //    >  LIT.ltr
      //    >  LIT.source
      //    >  LIT.npx
      //    >< LIT.$img
      //    (LIT.fired)
      // 2019-10-01 PerfektesChaos@de.wikipedia
      var $li = $( this ),
          id, re, src, $e;
      if ( ! $li.find( "." + LIT.signed ).length ) {
         id = $li.attr( LIT.selector );
         if ( id ) {
            if ( ! LIT.$img ) {
               re         = new RegExp( " +", "g" );
               LIT.source = LIT.source.substr( 0, 1 )  +  "/"
                            +  LIT.source.replace( re, "_" );
               src        = "https://upload.wikimedia.org/"
                            + "wikipedia/commons/thumb/"
                            + LIT.source + "/"
                            + LIT.npx + "px-"
                            + LIT.source.substr( 5 ) + ".png";
               LIT.$img   = $( "<img>" ).attr( { src:    src,
                                                 height: LIT.npx + "px",
                                                 width:  LIT.npx + "px" }
                                             );
            }
            $e = LIT.$img.clone()
                         .addClass( LIT.signed )
                         .attr( "title", "logidTooltip " + id )
                         .css( "padding-"
                               +  ( LIT.ltr ? "left" : "right" ),
                               "6px" )
                         .click( function () {   LIT.fired( id, $e );
                                             } );
            $li.append( $e );
         }
      }
   };   // .fiat()



   LIT.fired = function( assigned, $anchor ) {
      // Icon has been clicked
      // Precondition:
      //    assigned   -- logid
      //    $anchor    -- element to be attached to
      // Uses:
      //    >< .$body
      //    LIT.flip()
      //    mw.loader.using()
      //    (LIT.flip)
      // 2019-10-01 PerfektesChaos@de.wikipedia
      if ( LIT.$body ) {
         LIT.flip( assigned, $anchor );
      } else {
         LIT.$body = $( "body" );
         mw.loader.using( [ "oojs",
                            "oojs-ui-core",
                            "oojs-ui-widgets" ],
                          function () {
                             LIT.flip( assigned, $anchor );
                          }
                        );
      }
   };   // .fired()



   LIT.flip = function( assigned, $anchor ) {
      // Process icon click
      // Precondition:
      //    assigned   -- logid
      //    $anchor    -- element to be attached to
      //    OOjs-ui ready
      // Uses:
      //    >< OO
      //     < LIT.popups
      //    LIT.$factory()
      // 2019-10-01 PerfektesChaos@de.wikipedia
      var entry, popup;
      if ( ! OO ) {
         OO = window.OO;
         LIT.popups = { };
      }
      if ( typeof LIT.popups[ assigned ]  ===  "object" ) {
         entry  = LIT.popups[ assigned ];
         popup = entry[ 0 ];
      } else {
         popup = LIT.$factory( assigned, $anchor );
         entry  = [ popup, false ];
         LIT.popups[ assigned ] = entry;
      }
      entry[ 1 ] = ! entry[ 1 ];
      popup.toggle( entry[ 1 ] );
   };   // .flip()



   LIT.furnish = function( $area ) {
      // Content area arrived
      // Precondition:
      //    $area   -- jQuery content area
      // Uses:
      //    >  LIT.selector
      //    >< LIT.ltr
      //    mw.hook()
      //    (LIT.fiat)
      // 2020-01-01 PerfektesChaos@de.wikipedia
      var $entries = $area.find( "li[" + LIT.selector + "]" );
      if ( $entries.length ) {
         if ( typeof LIT.ltr  !==  "boolean" ) {
            LIT.ltr = ( $( "html" ).attr( "dir" )  !==  "rtl" );
         }
         $entries.each( LIT.fiat );
      }
   };   // .furnish()



   // -------------------------------------------------------------------



   fire();
}( window.mediaWiki, window.jQuery ) );
/// EOF </nowiki>   logidTooltip/d.js