Jump to content

User:PerfektesChaos/js/idResolver/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/idResolver/d.js
/// 2018-09-04 PerfektesChaos@de.wikipedia
// Run ID resolving alternatives on HTML page
// ResourceLoader:  compatible;
//    dependencies: "user", "mediawiki.util",
//                  "oojs", "oojs-ui-core", "oojs-ui-widgets"
/// Documentation:  [[w:en:User:PerfektesChaos/js/idResolver]]
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <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.7,
       IDA     = "idResolver",
       IDEA    = { load: true,
                   sub:  "/core",
                   type: IDA };
   if ( typeof mw.libs[ IDA ]  !==  "object"
        ||   ! mw.libs[ IDA ] ) {
      mw.libs[ IDA ] = { };
   }
   IDA = mw.libs[ IDA ];
   if ( typeof IDA.vsn  ===  "string" ) {
      IDA.vsn = "*" + VERSION + " " + IDA.vsn;
   } else {
      IDA.vsn = "" + VERSION;
   }
   IDA.type      = IDEA.type;
   IDA.signature = "ext.gadget." + IDA.type;
   IDA.support   =  "User:PerfektesChaos/js/" + IDA.type;
   IDA.doc       = "[[w:en:" + IDA.support + "]]";



   function facilitate( activate ) {
      // Callback from core completion
      // Precondition:
      //    activate  -- function, to run core business
      // Uses:
      //    >  IDA
      //    >  IDEA.cfg
      // 2016-05-01 PerfektesChaos@de.wikipedia
      activate( IDA, IDEA.cfg );
   }   // facilitate()



   function faculty( after ) {
      // Run Special:Gadgets form
      // Precondition:
      //    after  -- form initialization function
      // Uses:
      //    >  IDEA.prego
      //    >  IDEA.cfg
      // 2016-05-01 PerfektesChaos@de.wikipedia
      after( IDEA.prego, IDEA.cfg );
   }   // faculty()



   function features( assign ) {
      // Assign set of user options
      // Precondition:
      //    assign  -- object with assignments
      // Uses:
      //    >  IDEA.pars
      //     < IDEA.rooms
      //     < IDEA.cfg
      //     < IDEA.opt
      // 2016-09-01 PerfektesChaos@de.wikipedia
      var part, s;
      for ( s in assign ) {
         if ( s === "rooms" ) {
            if ( typeof assign.rooms  ===  "object" ) {
               part = assign.rooms;
               if ( part    &&
                    typeof part.length  ===  "number"
                    &&     part.length ) {
                  IDEA.rooms = part;
               }
            }
         } else if ( s === "types" ) {
            if ( typeof assign.types  ===  "object"
                 &&     assign.types ) {
               IDEA.cfg.types = assign.types;
            }
         } else if ( typeof IDEA.pars[ s ]  ===  "undefined" ) {
            IDEA.cfg[ s ] = assign[ s ];
         } else if ( typeof assign[ s ]  ===  IDEA.pars[ s ] ) {
            IDEA.opt[ s ] = assign[ s ];
         }
      }   // for s in assign
   }   // features()



   function featuring( auxilary ) {
      // Appreciate user options
      // Precondition:
      //    auxilary  -- preferencesGadgetOptions library object
      // Postcondition:
      //    Returns true if page is unconditionally subject for equipment
      // Uses:
      //    >  IDA.opt
      //    >  IDA.type
      //    >< IDEA.opt
      //    >< IDEA.rooms
      //     < IDEA.cfg
      //     < IDEA.lazy
      //     < IDEA.lock
      //    features()
      //    mw.hook()
      //    mw.libs.preferencesGadgetOptions.fetch()
      // 2017-02-06 PerfektesChaos@de.wikipedia
      var i, s, v, vals;
      IDEA.cfg  = { make: 0 };
      IDEA.opt  = { spaces: false };
      IDEA.pars = { launch:  "boolean",
                    layer:   "boolean",
                    lazy:    "boolean",
                    light:   "boolean",
                    linking: "boolean",
                    lock:    "boolean",
                    make:    "number",
                    seen:    "string",
                    spaces:  "string",
                    debug:   "boolean" };
      for ( s in IDEA.pars ) {
         IDEA.opt[ s ] = false;
      }   // for s in pars
      if ( typeof IDA.opt  ===  "object"
           &&     IDA.opt ) {
         features( IDA.opt );
      }
      mw.hook( IDA.type + ".before" ).add( features );
      if ( typeof auxilary  ===  "object"
           &&     auxilary ) {
         if ( typeof auxilary.fetch  ===  "function" ) {
            vals = auxilary.fetch( IDA.type );
            if ( vals   &&   typeof vals  ===  "object" ) {
               for ( s in IDEA.pars ) {
                  if ( typeof vals[ s ]  ===  IDEA.pars[ s ] ) {
                     IDEA.opt[ s ] = vals[ s ];
                  }
               }   // for s in pars
               if ( IDEA.opt.spaces ) {
                  if ( typeof IDEA.rooms  !==  "object" ) {
                     IDEA.rooms = [ ];
                  }
                  vals = IDEA.opt.spaces.split( " " );
                  for ( i = 0;  i < vals.length;  i++ ) {
                     v = parseInt( vals[ i ], 10 );
                     if ( ! isNaN( v ) ) {
                        IDEA.rooms.push( Math.floor( v ) );
                     }
                  }
               }
            }
         }
      }
      IDEA.lazy  = IDEA.opt.lazy;
      IDEA.lock  = IDEA.opt.lock;
      IDEA.cfg.layer   = IDEA.opt.layer;
      IDEA.cfg.light   = IDEA.opt.light;
      IDEA.cfg.linking = IDEA.opt.linking;
      if ( typeof IDEA.opt.make  ===  "number"
           &&     IDEA.opt.make >= 0 ) {
         IDEA.cfg.make = Math.floor( IDEA.opt.make );
      }
      return IDEA.opt.launch;
   }   // featuring()



   function feed( assigned, access, after, age ) {
      // Run subscript
      // Precondition:
      //    assigned  -- signature
      //    access    -- script
      //    after     -- callback
      //    age       -- maxage, or false
      // Uses:
      //    >  VERSION
      //    >  IDEA.opt.debug
      //    >< IDEA.level
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.loader.load()
      //    mw.hook()
      // 2018-09-04 PerfektesChaos@de.wikipedia
      var maxage, rls;
      if ( ! mw.loader.getState( assigned ) ) {
         rls = { };
         rls[ assigned ] = "loading";
         mw.loader.state( rls );
         if ( typeof IDEA.level  !==  "boolean" ) {
            IDEA.level = ( VERSION > 0 );
            if ( IDEA.level   &&
                 typeof IDEA.opt  ===  "object"
                 &&     IDEA.opt    &&
                 typeof IDEA.opt.debug  === "boolean"
                 &&     IDEA.opt.debug ) {
               IDEA.level = false;
            }
         }
         maxage = ( age   ?   age
                          :   ( IDEA.level  ?  86400  :  1000 ) );
         mw.loader.load( "https://en.wikipedia.org"
                         + "/w/index.php?title="
                         + "User:PerfektesChaos/js/"
                         + access + "/"
                         + ( IDEA.level  ?  "r"  :  "d" )   +   ".js"
                         + "&bcache=1&maxage=" + maxage
                         + "&action=raw"
                         + "&ctype=text/javascript",
                         "text/javascript" );
      }
      if ( after ) {
         mw.hook( access + ".ready" ).add( after );
      }
   }   // feed()



   function fetch( activity ) {
      // Run core
      // Precondition:
      //    activity  -- event
      // Uses:
      //    >  IDA.signature
      //    >  IDEA.sub
      //    >  IDA.type
      //    >< IDEA.load
      //    feed()
      //    (facilitate)
      // 2016-11-11 PerfektesChaos@de.wikipedia
      if ( typeof activity  ===  "object"   &&   activity ) {
         if ( typeof activity.preventDefault  ===  "function" ) {
            activity.preventDefault();
         }
         if ( typeof activity.stopPropagation  ===  "function" ) {
            activity.stopPropagation();
         }
      }
      if ( IDEA.load ) {
         IDEA.load = false;
         feed( IDA.signature + IDEA.sub,
               IDA.type + IDEA.sub,
               facilitate );
      }
   }   // fetch()



   function figure( auxilary ) {
      // Prepare Special:Gadgets
      // Precondition:
      //    auxilary  -- preferencesGadgetOptions library object
      // Uses:
      //    >  IDA.type
      //     < IDEA.prego
      //    featuring()
      //    (faculty)
      // 2016-05-01 PerfektesChaos@de.wikipedia
      IDEA.prego = auxilary;
      featuring( auxilary );
      mw.hook( IDA.type + "/prego.ready" ).add( faculty );
   }   // figure()



   function fill() {
      // Provide trigger for manual start of analysis
      // Precondition:
      //    document ready
      //    "mediawiki.util"
      // Uses:
      //    >  IDA.type
      //    >  IDA.vsn
      //    mw.util.addPortletLink()
      //    (fetch)
      // 2016-05-01 PerfektesChaos@de.wikipedia
      var portlet  = mw.util.addPortletLink( "p-tb",
                                             "#",
                                             IDA.type,
                                             "t-" + IDA.type ),
          $portlet = $( portlet );
      $portlet.click( fetch );
      $portlet.attr( { title:  IDA.type + " " + IDA.vsn } );
   }   // fill()



   function filter( auxilary ) {
      // Check user options for meaningful usage
      // Precondition:
      //    auxilary  -- preferencesGadgetOptions library object
      // Uses:
      //    >  IDEA.nsn
      //    >  IDEA.rooms
      //    >  IDEA.lazy
      //    >  IDEA.lock
      //    mw.config.get()
      //    featuring()
      //    mw.loader.using()
      //    fetch()
      //    mw.hook()
      //    (fill)
      // 2016-10-20 PerfektesChaos@de.wikipedia
      var env    = mw.config.get( [ "wgTitle",
                                    "wgUserName",
                                    "wgWikibaseItemId" ] ),
          launch = featuring( auxilary );
      if ( ! launch ) {
         switch ( IDEA.nsn ) {
            case 0:
               launch = true;
               break;
            case 2:
               launch = ( ! env.wgTitle.indexOf( env.wgUserName
                                                 + "/" ) );
               break;
            case 4:
               if ( typeof env.wgWikibaseItemId  ===  "string"   &&
                    env.wgWikibaseItemId.toUpperCase() === "Q3938" ) {
                  launch = true;
               }
               break;
         }   // switch envNamespaceNumber
      }
      if ( ! launch   &&
           IDEA.rooms   &&
           $.inArray( IDEA.nsn, IDEA.rooms )  >=  0 ) {
         launch = true;
      }
      if ( launch ) {
         if ( IDEA.lazy ) {
            mw.loader.using( [ "mediawiki.util" ],
                             function () {
                                $( fill );
                             } );
         } else if ( ! IDEA.lock ) {
            fetch();
         } else {
            mw.hook( IDA.type + ".fire" ).fire( fetch );
         }
      }
   }   // filter()



   function fire() {
      // Intialize application, check for meaningful usage
      // Uses:
      //    >  IDA.signature
      //    >  IDA.type
      //     < IDEA.nsn
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.config.get()
      //    feed()
      //    (figure)
      //    (filter)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var callback, env, rls, scope;
      if ( mw.loader.getState( IDA.signature )  !==  "ready" ) {
         rls = { };
         rls[ IDA.signature ] = "ready";
         mw.loader.state( rls );
         env   = mw.config.get( [ "wgAction",
                                  "wgCanonicalSpecialPageName",
                                  "wgNamespaceNumber",
                                  "wgPageContentModel" ] );
         scope = "|view|submit|edit|";
         if ( env.wgNamespaceNumber < 0 ) {
            if ( env.wgCanonicalSpecialPageName === "Blankpage" ) {
               //                                   "Gadgets"
               feed( IDA.signature + "/prego",
                     IDA.type + "/prego",
                     false );
               callback = figure;
            }
         } else if ( scope.indexOf( env.wgAction ) > 0   &&
                     env.wgPageContentModel === "wikitext" ) {
            IDEA.nsn = env.wgNamespaceNumber;
            callback = filter;
         }
         if ( callback ) {
            feed( "ext.gadget.preferencesGadgetOptions",
                  "preferencesGadgetOptions",
                  callback,
                  86410 );
         }
      }
   }   // fire()



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

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