Jump to content

User:PerfektesChaos/js/refNames/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.
/// User:PerfektesChaos/js/refNames/d.js
/// 2022-07-27 PerfektesChaos@de.wikipedia
//  Show and edit <ref name="..."> identifiers
//  ResourceLoader:  compatible;
//    dependencies: user.options,
//                  mediawiki.API, mediawiki.storage, mediawiki.util
/// Fingerprint: #0#0#
/// License: CC-by-sa/4.0
/// <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,
       Signature = "refNames",
       THIS      = { doc:      { site:    "w:en",
                                 support: "User:PerfektesChaos/js/"
                                          + Signature },
                     launch:  false,
                     learn:   false,
                     maxage:  43200,
                     rooms:   [ 0, 2, 4 ],
                     shoot:   false,
                     site:    "w:en",
                     spaces:  false,
                     store:   "User:PerfektesChaos/js/",
                     strange: false,
                     sub:     "core",
                     using:   [ "mediawiki.util" ],
                     warn:    false
                   },
       PREGO     = { app:       false,
                     live:      false,
                     maxage:    604816,
                     pars:      [ [ "spaces",  "string" ],
                                  [ "strange", "string" ],
                                  [ "shoot",   "string" ] ],
                     signature: "preferencesGadgetOptions",
                     site:      "w:en",
                     store:     "User:PerfektesChaos/js/",
                     sub:       "/r.js" },
       REPOS     = { };



   function features( apply ) {
      // Config hook has been fired
      // Precondition:
      //    apply  -- hook payload
      // Uses:
      //    >  THIS.learn
      //    >  THIS.nsn
      //     < THIS.rooms
      //     < THIS.trigger
      //     < THIS.warn
      // 2022-07-01 PerfektesChaos@de.wikipedia
      var i, n, rooms, s;
      if ( typeof apply  ===  "object"
           &&     apply ) {
         if ( typeof apply.rooms  ===  "object"
              &&     apply.rooms   &&
              typeof apply.rooms.length  ===  "number" ) {
            rooms = [ ];
            for ( i = 0;  i < apply.rooms.length;  i++ ) {
               n = apply.rooms[ i ];
               if ( typeof n  ===  "number" ) {
                  n = Math.floor( n );
                  if ( n >= 0 ) {
                     rooms.push( n );
                  }
               }
            }   // for i
            if ( rooms.length ) {
               rooms.sort();
               THIS.rooms = rooms;
            }
         } else if ( typeof apply.rooms  ===  "string" ) {
            THIS.spaces = "*";
         }
         if ( THIS.learn   &&
              typeof apply.trigger  ===  "object"
              &&     apply.trigger   &&
              typeof apply.trigger.length  ===  "number" ) {
            for ( i = 0;  i < apply.trigger.length;  i++ ) {
               if ( apply.trigger[ i ] === THIS.nsn ) {
                  THIS.launch = true;
                  break;   // for i
               }
            }   // for i
         }
         if ( typeof apply.warn  ===  "string" ) {
            apply.warn = apply.warn.trim();
            if ( apply.warn ) {
               THIS.warn = THIS.warn  ||  [ ];
               THIS.warn.push( apply.warn );
            }
         } else if ( typeof apply.warn  ===  "object"
                     &&     apply.warn   &&
                     typeof apply.warn.length  ===  "number" ) {
            for ( i = 0;  i < apply.warn.length;  i++ ) {
               s = apply.warn[ i ];
               if ( typeof s  ===  "string" ) {
                  s = s.trim();
                  if ( s ) {
                     THIS.warn = THIS.warn  ||  [ ];
                     THIS.warn.push( s );
                  }
               }
            }   // for i
         }
      }
   }   // features()



   function feed( access ) {
      // Load package component
      // Precondition:
      //    access  -- string, with subpage name, defaults to THIS.sub
      // Uses:
      //    >  THIS.site
      //    >  THIS.store
      //    >  Signature
      //    >  THIS.maxage
      //    >  THIS.sub
      //    >  THIS.signature
      //    >  Version
      //     < THIS.serial
      //    mw.loader.getState()
      //    mw.loader.state()
      //    REPOS.fire()
      // 2022-07-01 PerfektesChaos@de.wikipedia
      var sign  = THIS.signature + ".",
          store = THIS.store + Signature,
          rls;
      if ( access   &&  typeof access  ===  "string" ) {
         sign  = sign + access;
         store = store + "/" + access;
      } else {
         REPOS.fire( THIS.site,
                     store + ".css",
                     false,
                     { action: "raw",
                       ctype:  "text/css",
                       bcache: 1,
                       maxage: THIS.maxage } );
         THIS.serial = Version + "";
         mw.hook( Signature + "." + THIS.sub ).fire( THIS );
         sign  = sign + THIS.sub;
         store = store + "/" + THIS.sub;
      }
      if ( ! mw.loader.getState( sign ) ) {
         rls = { };
         rls[ sign ] = "loading";
         mw.loader.state( rls );
         REPOS.fire( THIS.site,
                     store,
                     ( Version > 0 ? "/r.js" : "/d.js" ),
                     { action: "raw",
                       ctype:  "text/javascript",
                       bcache: 1,
                       maxage: THIS.maxage } );
      }
   }   // feed()



   function first() {
      // Autorun on loading
      // Uses:
      //    >  Signature
      //    >  PREGO.signature
      //    >  THIS.doc.site
      //    >  THIS.doc.support
      //    >  Version
      //     < THIS.signature
      //     < THIS.nsn
      //     < THIS.learn
      //     < PREGO.live
      //     < THIS.doc.server
      //     < THIS.pub
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.config.get()
      //    mw.hook()
      //    REPOS.foundation()
      //    PREGO.feed()
      //    (features)
      //    (PREGO.features)
      // 2022-07-01 PerfektesChaos@de.wikipedia
      var env, i, live, rls, s;
      THIS.signature = "ext.gadget." + Signature;
      if ( mw.loader.getState( THIS.signature )  !==  "ready" ) {
         rls = { };
         rls[ THIS.signature ] = "ready";
         mw.loader.state( rls );
         env = mw.config.get( [ "wgAction",
                                "wgNamespaceNumber" ] );
         THIS.nsn = env.wgNamespaceNumber;
         switch ( env.wgAction ) {
            case "edit":
            case "submit":
               live       = true;
               THIS.learn = true;
               break;
            case "view":
               if ( THIS.nsn === -1 ) {
                  if ( mw.config.get( "wgCanonicalSpecialPageName" )
                                                     ===  "Blankpage" ) {
                     s = mw.config.get( "wgTitle" );
                     i = s.indexOf( "/" + PREGO.signature );
                     if ( i > 1 ) {
                        PREGO.live = true;
                     }
                  }
               } else {
                  live = true;
               }
               break;
         }   // switch wgAction
         if ( live ) {
            live = ( mw.config.get( "wgPageContentModel" )
                                                       ===  "wikitext" );
         }
         if ( live || PREGO.live ) {
            mw.hook( Signature + ".config" ).add( features );
            THIS.doc.server = REPOS.foundation( THIS.doc.site, "" );
            PREGO.feed();
            mw.hook( PREGO.signature + ".fetch" ).fire( Signature,
                                                        PREGO.features );
         }
         THIS.pub = { doc:  "[[" + THIS.doc.site + ":"
                                 + THIS.doc.support + "]]",
                      type: Signature,
                      vsn:  Version };
         mw.hook( Signature + ".ready" ).fire( THIS.pub );
      }
   }   // first()



   PREGO.features = function ( applied ) {
      // User options available
      // Precondition:
      //    applied  -- object, may be null, with values
      // Uses:
      //    >  PREGO.live
      //    >  PREGO.pars
      //    >  PREGO.signature
      //    >  Signature
      //    >  THIS.nsn
      //    >  THIS.rooms
      //    >  THIS.learn
      //    >  THIS.using
      //    >< THIS.shoot
      //    >< THIS.spaces
      //    >< THIS.strange
      //    >< THIS.warn
      //     < THIS.launch
      //    mw.hook()
      //    feed()
      //    mw.loader.using()
      //    (feed)
      // 2022-07-26 PerfektesChaos@de.wikipedia
      var re = new RegExp( "\\s+" ),
          i, live, n, s, v;
      if ( typeof applied  ===  "object"
           &&     applied ) {
         n = PREGO.pars.length;
         for ( i = 0;  i < n;  i++ ) {
            v = PREGO.pars[ i ];
            s = v[ 0 ];
            if ( typeof applied[ s ]  ===  v[ 1 ] ) {
               THIS[ s ] = applied[ s ];
            }
         }   // for i
      }
      if ( THIS.shoot ) {
         THIS.shoot = THIS.shoot.trim()
                                .replace( re, " " );
      }
      if ( THIS.spaces ) {
         THIS.spaces = THIS.spaces.trim()
                                  .replace( re, " " );
      } else {
         n = false;
         THIS.rooms.sort();
         for ( i = 0;  i < THIS.rooms.length;  i++ ) {
            if ( THIS.rooms[ i ] !== n ) {
               n           = THIS.rooms[ i ];
               THIS.spaces = ( THIS.spaces  ?  THIS.spaces + " "  :  "" )
                             +   n;
            }
         }   // for i
      }
      if ( THIS.strange ) {
         THIS.strange = THIS.strange.trim();
      }
      if ( PREGO.live ) {
         THIS.prego = { pars:      PREGO.pars,
                        signature: PREGO.signature };
         mw.hook( Signature + ".prego" ).fire( THIS );
         feed( "prego" );
      } else {
         if ( THIS.spaces === "*" ) {
            live = true;
         } else if ( THIS.spaces  &&  THIS.spaces !== "-" ) {
            v = THIS.spaces.split( " " );
            s = THIS.nsn + "";
            for ( i = 0;  i < v.length;  i++ ) {
               if ( s === v[ i ] ) {
                  live = true;
                  break;   // for i
               }
            }   // for i
         }
         if ( live && THIS.learn && THIS.shoot ) {
            v = THIS.shoot.split( " " );
            s = THIS.nsn + "";
            for ( i = 0;  i < v.length;  i++ ) {
               if ( s === v[ i ] ) {
                  THIS.launch = true;
                  break;   // for i
               }
            }   // for i
         }
         if ( THIS.strange === "-" ) {
            THIS.strange = false;
         }
         if ( THIS.strange ) {
            THIS.warn = THIS.warn  ||  [ ];
            THIS.warn.push( THIS.strange );
         }
         if ( live ) {
            mw.loader.using( THIS.using, feed );
         }
      }
   };   // PREGO.features()



   PREGO.feed = function () {
      // Provide PREGO
      // Uses:
      //    >  PREGO.signature
      //    >  PREGO.site
      //    >  PREGO.store
      //    >  PREGO.sub
      //    >  PREGO.maxage
      //    mw.loader.getState()
      //    mw.loader.state()
      //    REPOS.fire()
      // 2018-09-03 PerfektesChaos@de.wikipedia
      var sign = "ext.gadget." + PREGO.signature,
          rls;
      if ( ! mw.loader.getState( sign ) ) {
         rls = { };
         rls[ sign ] = "loading";
         mw.loader.state( rls );
         REPOS.fire( PREGO.site,
                     PREGO.store + PREGO.signature + PREGO.sub,
                     false,
                     { action: "raw",
                       ctype:  "text/javascript",
                       bcache: 1,
                       maxage: PREGO.maxage } );
      }
   };   // PREGO.feed()



   REPOS.fire = function ( at, access, append, alter ) {
      // Load from external URL
      // Precondition:
      //    at      -- Wikimedia Foundation site code, or not
      //    access  -- string, with basic page name
      //    append  -- string, with subpage, or not
      //    alter   -- parameter object, or MIME string, or not
      // Uses:
      //    >< REPOS.requests
      //    REPOS.foundation()
      //    mw.loader.load()
      // 2018-03-21 PerfektesChaos@de.wikipedia
      var source, syntax;
      if ( typeof REPOS.requests  !==  "object" ) {
         REPOS.requests = { };
      }
      if ( typeof REPOS.requests[ access ]  !==  "boolean" ) {
         REPOS.requests[ access ] = true;
         if ( append ) {
            source = access + append;
         } else {
            source = access;
         }
         if ( at ) {
            source = REPOS.foundation( at, source, alter );
            if ( typeof alter  ===  "object"
                 &&     alter   &&
                 typeof alter.ctype  ===  "string" ) {
               syntax = alter.ctype;
            }
         } else {
            syntax = alter;
         }
         mw.loader.load( source, syntax );
      }
   };   // REPOS.fire()



   REPOS.foundation = function ( at, access, alter ) {
      // Create URL within Wikimedia Foundation
      // Precondition:
      //    at      -- site code, or not
      //    access  -- string with page name
      //    alter   -- parameter object, or not
      // Postcondition:
      //    Returns full URL
      // 2018-03-21 PerfektesChaos@de.wikipedia
      var s = access,
          r = encodeURI( s );
      if ( typeof alter  ===  "object"
           &&     alter ) {
         r = "/w/index.php?title=" + r;
         if ( access.substr( -3 ) === ".js" ) {
            alter.ctype = "text/javascript";
         } else if ( access.substr( -4 ) === ".css" ) {
            alter.ctype = "text/css";
         }
         alter.action = "raw";
         for ( s in alter ) {
            r = r + "&" + s + "=" + encodeURI( alter[ s ] );
         }   // for s in alter
      } else {
         r = "/wiki/" + r;
      }
      if ( typeof at  ===  "string"
           &&     at ) {
         switch ( at ) {
            case "meta":
               r = "meta.wikimedia.org" + r;
               break;
            case "mw":
               r = "www.mediawiki.org" + r;
               break;
            case "w:en":
               r = "en.wikipedia.org" + r;
               break;
            default:
               r = window.location.host + r;
         }   // switch at
         r = "https://" + r;
      }
      return r;
   };   // REPOS.foundation()



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



// Emacs
// Local Variables:
// coding: utf-8-dos
// fill-column: 80
// End:

/// EOF </nowiki>   refNames/d.js