Jump to content

User:PerfektesChaos/js/jsonDebug/scout/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/jsonDebug/scout/d.js
//  Show LintErrors analysis live
/// 2018-08-24 PerfektesChaos@de.wikipedia
//  ResourceLoader:  compatible;
//    dependencies: user, mw.API
/// 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.1,
       Signature = "scout",
       APP   = { listen:  false,
                 loading: false,
                 longer:  false,
                 nsn:     false,
                 reClass: false,
                 reTag:   false,
                 rooms:   false,
                 sel:     false,
                 sign:    false },
       JSOND = { sign:  "jsonDebug",
                 site:  "w:en",
                 store: "User:PerfektesChaos/js/",
                 sel:   "json-code-lint",
                 src:   "div,pre",
                 using: null },
       REPOS = { requests:  false };



   APP.feed = function ( area ) {
      // Namespace analysis
      // Precondition:
      //    area  -- namespace
      // Uses:
      //    >  APP.nsn
      //    >< APP.listen
      //    >< APP.rooms
      //     < APP.reSpace
      //    mw.config.get()
      //    APP.finder()
      // 2018-03-11 PerfektesChaos@de.wikipedia
      var nsn, s;
      if ( ! APP.listen ) {
         switch ( typeof area ) {
            case "number":
               nsn = area;
               break;
            case "string":
               if ( ! APP.rooms ) {
                  APP.rooms   = mw.config.get( "wgNamespaceIds" );
                  APP.reSpace = new RegExp( "\\s+" );
               }
               s = area.trim().replace( APP.reSpace, "_" ).toLowerCase();
               if ( typeof APP.rooms[ s ]  ===  "number" ) {
                  nsn = APP.rooms[ s ];
               }
               break;
         }   // switch typeof area
         if ( typeof nsn  ===  "number" ) {
            APP.listen = ( APP.nsn === nsn );
            if ( APP.listen ) {
               APP.finder();
            }
         }
      }
   };   // APP.feed()



   APP.fetch = function () {
      // Load real tool
      // Uses:
      //    >  APP.longer
      //    >  JSOND.site
      //    >  JSOND.store
      //    >  JSOND.sign
      //    REPOS.fire()
      // 2018-03-11 PerfektesChaos@de.wikipedia
      var p, suffix;
      p = { bcache: 1 };
      if ( APP.longer ) {
         p.maxage = 100;
         suffix = "/d.js";
      } else {
         p.maxage = 604800;
         suffix = "/r.js";
      }
      REPOS.fire( JSOND.site,
                  JSOND.store + JSOND.sign,
                  suffix,
                  p );
   };   // APP.fetch()



   APP.find = function ( $area ) {
      // Scan this content
      // Precondition:
      //    $area  -- jQuery element of content area
      //    (DOM ready)
      // Uses:
      //    >  JSOND.sel
      //    >  JSOND.src
      //    >< APP.sel
      //     < APP.loading
      //    APP.fetch()
      // 2018-03-11 PerfektesChaos@de.wikipedia
      if ( ! APP.sel ) {
         APP.sel = "." + JSOND.sel;
      }
      if ( $area.find( APP.sel ).filter( JSOND.src ).length ) {
         APP.loading = true;
         APP.fetch();
      }
   };   // APP.find()



   APP.finder = function () {
      // Scan current page
      // Uses:
      //    mw.hook()
      //    (APP.find)
      // 2018-03-11 PerfektesChaos@de.wikipedia
      mw.hook( "wikipage.content" ).add( APP.find );
   };   // APP.finder()



   APP.fire = function ( areas, assert ) {
      // Namespace request
      // Precondition:
      //    areas   -- namespaces
      //    assert  -- true for debuggung
      // Uses:
      //    >  APP.listen
      //     < APP.longer
      //    APP.feed()
      // 2018-03-11 PerfektesChaos@de.wikipedia
      var i;
      if ( typeof assert  ===  "boolean" ) {
         APP.longer = assert;
      }
      if ( ! APP.listen ) {
         if ( typeof areas  ===  "object" ) {
            if ( areas   &&   typeof areas.length  ===  "number" ) {
               for ( i = 0;  i < areas.length;  i++ ) {
                  APP.feed( areas[ i ] );
               }   // for i
            }
         } else {
            APP.feed( areas );
         }
      }
   };   // APP.fire()



   APP.first = function () {
      // Autorun on loading
      // Uses:
      //    >  JSOND.sign
      //    >  Signature
      //    >  Version
      //    >< APP.sign
      //     < APP.signature
      //     < APP.nsn
      //    mw.loader.getState()
      //    mw.loader.state()
      //    APP.fetch()
      //    mw.hook()
      //    (APP.fire)
      //    (APP.further)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var env, rls;
      if ( ! APP.sign ) {
         APP.sign      = JSOND.sign + "." + Signature;
         APP.signature = "ext.gadget." + APP.sign;
      }
      if ( mw.loader.getState( APP.signature )  !==  "ready" ) {
         rls = { };
         rls[ APP.signature ] = "ready";
         mw.loader.state( rls );
         mw.hook( APP.sign + ".ready" ).fire( { type: APP.sign,
                                                vsn:  Version } );
         env = mw.config.get( [ "wgPageContentModel",
                                "wgNamespaceNumber" ] );
         if ( env.wgPageContentModel.toLowerCase().indexOf( "json" )
              >=  0 ) {
            APP.fetch();
         } else {
            APP.nsn = env.wgNamespaceNumber;
            mw.hook( JSOND.sign + ".namespaces" ).add( APP.fire );
            mw.hook( JSOND.sign + ".selectors" ).add( APP.further );
         }
      }
   };   // APP.first()



   APP.further = function ( add ) {
      // Selector request
      // Precondition:
      //    add   -- selectors object
      // Uses:
      //    >  APP.loading
      //    >  JSOND.sel
      //    >  APP.listen
      //    >< APP.sel
      //    >< APP.reClass
      //    >< APP.reTag
      //    >< JSOND.src
      //    APP.finder()
      // 2018-03-11 PerfektesChaos@de.wikipedia
      var i, s;
      if ( ! APP.loading   &&
           typeof add  ===  "object"
           &&     add ) {
         if ( typeof add.classes  ===  "object"
              &&     add.classes   &&
              typeof add.classes.length  ===  "number" ) {
            if ( ! APP.reClass ) {
               APP.reClass = new RegExp( "^[^ .#,]+$" );
            }
            if ( ! APP.sel ) {
               APP.sel = "." + JSOND.sel;
            }
            for ( i = 0;  i < add.classes.length;  i++ ) {
               s = add.classes[ i ];
               if ( APP.reClass.test( s ) ) {
                  APP.sel = APP.sel + ",." + s;
               }
            }   // for i
         }
         if ( typeof add.tags  ===  "object"
              &&     add.tags   &&
              typeof add.tags.length  ===  "number" ) {
            if ( ! APP.reTag ) {
               APP.reTag = new RegExp( "^[a-zA-Z]+$" );
            }
            for ( i = 0;  i < add.tags.length;  i++ ) {
               s = add.tags[ i ];
               if ( APP.reTag.test( s ) ) {
                  JSOND.src = JSOND.src + "," + s;
               }
            }   // for i
         }
         if ( APP.listen ) {
            APP.finder();
         }
      }
   };   // APP.further()



   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-11 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-11 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()



   APP.first();
}( window.mediaWiki ) );



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

/// EOF </nowiki>   jsonDebug/scout/d.js