Jump to content

User:PerfektesChaos/js/watchCategories/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/watchCategories/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
//  Display member count of some (maintenance) categories on watchlist
// ResourceLoader:  compatible;
//    dependencies: user, mediawiki.api, mediawiki.util
//    namespaces:   -1
//    doc:          w:en:PerfektesChaos/js/watchCategories
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <nowiki>
/* jshint forin:false                                                  */
/* global window:false                                                 */
/* jshint bitwise:true, curly:true, eqeqeq:true, latedef:true,
          laxbreak:true,
          nocomma:true, strict:true, undef:true, unused:true           */



( function ( mw, $ ) {
   "use strict";
   var WatCat   =  "watchCategories",
       Version  =  -1.3;
   if ( typeof mw.libs[ WatCat ]  !==  "object"
        ||   ! mw.libs[ WatCat ] ) {
      mw.libs[ WatCat ] = { };
   }
   mw.libs[ WatCat ].type  =  WatCat;
   WatCat      =  mw.libs[ WatCat ];
   WatCat.doc  =  "[[w:en:User:PerfektesChaos/js/" + WatCat + "]]";
   WatCat.vsn  =  Version;



   // Requires: JavaScript  1.3



   function fire() {
      // Intialize application, check for meaningful usage
      // Uses:
      //    >  .type
      //    >  .cats
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.loader.using()
      //    feed()
      //    (WatCat.fire)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var signature = "ext.gadget." + WatCat.type,
          rls;
      if ( mw.loader.getState( signature ) !==  "ready" ) {
         rls = { };
         rls[ signature ] = "ready";
         mw.loader.state( rls );
         if ( WatCat.cats ) {
            mw.loader.using( [ "user",
                               "mediawiki.api",
                               "mediawiki.util" ],
                             WatCat.fire );
         }
      }
   }   // fire()



   WatCat.factory  =  function ( already ) {
      // Return single object
      // Precondition:
      //    already  -- object with user defined data
      // Postcondition:
      //    Returns object
      // Uses:
      //    mw.util.$content
      // 2013-05-31 PerfektesChaos@de.wikipedia
      var r, s, v;
      if ( already.cat ) {
         r  =  { };
         for ( s in already ) {
            v  =  already[ s ];
            if ( v ) {
               r[ s ]  =  v;
            }
         }   // for s in r
         r.cat  =  r.cat.replace( /_/g, " " );
         if ( typeof( r.min ) !== "number" ) {
            r.min  =  1;
         }
         if ( typeof( r.ns ) !== "number" ) {
            r.ns  =  -1;
         }
         if ( r.ns === -1  &&  typeof( r.title ) !== "string" ) {
            r.title  =  "Watchlist";
         }
         if ( typeof( r.append ) !== "string"  &&
              typeof( r.before ) !== "string" ) {
            if ( r.ns === -1  &&  r.title === "Watchlist" ) {
               r.before  =  "#mw-content-text";
            } else {
               r.before  =  mw.util.$content;
            }
         }
         if ( typeof( r.style1 ) !== "string" ) {
            r.style1  =     "border: solid 2px #FF0000;"
                         + " padding: 3px;"
                         + " color: #FF0000;";
         }
         if ( r.min < 1  &&  typeof( r.style0 ) !== "string" ) {
            r.style0  =     "border: solid 1px #008000;"
                         + " padding: 3px;"
                         + " color: #008000;";
         }
      }
      return r;
   };   // .factory()



   WatCat.find  =  function () {
      // Launch API query
      // Uses:
      //    this
      //    >  .q
      //     < .reSp
      //     < .space
      //    .finder()
      // 2013-05-31 PerfektesChaos@de.wikipedia
      var e, i, s;
      var n   =  this.q.length;
      var sP  =  false;
      var sT  =  false;
      for ( i = n - 1;  i >= 0;  i-- ) {
         e  =  this.q[ i ];
         if ( e.ns === this.nsN  &&  e.title === this.title ) {
            switch ( typeof( e.cat ) ) {
               case "string" :
                  s  =  "Category:" + e.cat;
                  if ( sT ) {
                     sT  =  sT + "|" + s;
                  } else {
                     sT  =  s;
                  }
                  break;
               case "number" :
                  e.curid  =  e.cat;
                  delete e.cat;
                  if ( sP ) {
                     sP  =  sP + "|";
                  } else {
                     sP  =  "";
                  }
                  sP  =  sP + e.curid;
                  break;
            }   // switch typeof e.cat
         } else {
            this.q[ i ]  =  false;
         }
      }   // for i--
      if ( sP || sT ) {
         this.reSp   =  new RegExp( " ", "g" );
         this.space  =  mw.config.get( "wgFormattedNamespaces" )[ 14 ];
      }
      if ( sP ) {
         this.finder( sP, false );
      }
      if ( sT ) {
         sT  =  sT.replace( this.reSp, "_" );
         this.finder( sT, true );
      }
   };   // .find()



   WatCat.finder  =  function ( ask, assigned ) {
      // Launch API query
      // Precondition:
      //    ask       -- pipe separated page list
      //    assigned  -- true: titles;  false: pageids
      // Uses:
      //    this
      //    (.found)
      // 2013-05-31 PerfektesChaos@de.wikipedia
      var a  =  new mw.Api();
      var o  =  { action: "query",
                  prop:   "categoryinfo" };
      if ( assigned ) {
         o.titles  =  ask;
      } else {
         o.pageids  =  ask;
      }
      a.get( o ).done( this.found );
   };   // .finder()



   WatCat.fire  =  function () {
      // Check whether anything to do
      // Precondition:
      //    .cats present
      // Uses:
      //    >  .cats
      //     < .nsN
      //     < .title
      //     < .q
      //    mw.config.get()
      //    .factory()
      //    .find()
      // Remark: Used as event handler -- 'this' is not WatCat
      // 2013-05-31 PerfektesChaos@de.wikipedia
      var e, i, n,
          launch  =  false;
      WatCat.nsN  =  mw.config.get( "wgNamespaceNumber" );
      if ( WatCat.nsN === -1 ) {
         WatCat.title  =  mw.config.get( "wgCanonicalSpecialPageName" );
      }
      switch ( typeof( WatCat.cats ) ) {
         case "string" :
         case "number" :
            if ( WatCat.nsN === -1 ) {
               if ( WatCat.title === "Watchlist" ) {
                  WatCat.q  =  [ WatCat.factory( { cat: WatCat.cats } )
                               ];
                  launch    =  true;
               }
            }
            break;
         case "object" :
            n  =  WatCat.cats.length;
            if ( typeof( n ) === "number" ) {
               if ( WatCat.nsN !== -1 ) {
                  WatCat.title = mw.config.get( "wgTitle" );
               }
               WatCat.q  =  [ ];
               for ( i = 0;  i < n;  i++ ) {
                  e  =  WatCat.cats[ i ];
                  switch ( typeof( e ) ) {
                     case "string" :
                     case "number" :
                        e  =  WatCat.factory( { cat: e } );
                        break;
                     case "object" :
                        e  =  WatCat.factory( e );
                        break;
                     default :
                        e  =  false;
                  }   // switch typeof e
                  if ( e ) {
                     if ( e.ns === WatCat.nsN ) {
                        if ( e.title === WatCat.title ) {
                           WatCat.q.push( e );
                           launch  =  true;
                        }
                     }
                  }
               }   // for i
            }
            break;
      }   // switch typeof .cats
      if ( launch ) {
         WatCat.find();
      }
   };   // .fire()



   WatCat.flag  =  function ( adhere, about, amount, alter ) {
      // Mark detected "common-error" in Module transclusion
      // Precondition:
      //    adhere  -- configuration object
      //    about   -- category title
      //    amount  -- number of pages in category
      //    alter   -- iff string: different link title
      // Uses:
      //    this
      //    >  .type
      //    >  .reSp
      //    .furnish()
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var s;
      if ( amount >= adhere.min ) {
         s  =  "<div style='margin-top: 5px; margin-bottom: 5px;'>"
               +  "<span "
                    + ( adhere.id  ?  "id='" + adhere.id + "' "
                                   :  "class='" + WatCat.type + "' " )
                    + "style='"
                          +  ( amount ? adhere.style1 : adhere.style0 )
                          +  "'>"
               +  "<a href='/wiki/Category:"
                                       +  about.replace( this.reSp, "_" )
                       +  "'>"
                           +  ( typeof( alter ) === "string" ?  alter
                                                             :  about )
                + "</a>"
               +  ( amount ?  " #<strong>" + amount + "</strong>"
                           :  "" )
               +  "</span></div>";
         if ( adhere.append ) {
            this.furnish( s, adhere.append, true );
         } else if ( adhere.before ) {
            this.furnish( s, adhere.before, false );
         }
      }
   };   // .flag()



   WatCat.found  =  function ( arrived ) {
      // Show API success on pagesincategory
      // Precondition:
      //    arrived  -- JSON result of ajax query
      // Uses:
      //    >  .q
      //    >  .space
      //    .flag()
      // Remark: Used as event handler -- 'this' is not WatCat
      // 2013-12-25 PerfektesChaos@de.wikipedia
      var e, g, i, p, s,
          got  =  arrived.query,
          j    =  WatCat.space.length + 1,
          n    =  WatCat.q.length;
      if ( got ) {
         got  =  got.pages;
         if ( got ) {
            for ( g in got ) {
               p  =  got[ g ];
               for ( i = 0;  i < n;  i++ ) {
                  e  =  WatCat.q[ i ];
                  if ( p && e ) {
                     if ( p.title ) {
                        s  =  p.title.substr( j );
                        if ( typeof( p.pageid ) !== "number" ) {
                           p.pageid  =  -2;
                        }
                        if ( p.pageid === e.curid  ||  s === e.cat ) {
                           p  =  p.categoryinfo;
                           if ( p ) {
                              WatCat.flag( e, s, p.pages, e.text );
                           }
                        }
                     }
                  }
               }   // for i
            }   // for g in got
         }
      }
   };   // .found()



   WatCat.furnish  =  function ( apply, adjacent, append ) {
      // Insert element content
      // Precondition:
      //    apply     -- string with content to be inserted
      //    adjacent  -- element; selector string or jQuery
      //    append    -- true: append;  false: before
      // Uses:
      //    mw.util.$content
      //    jQuery()
      //    jQuery().append()
      //    jQuery().before()
      // 2013-05-31 PerfektesChaos@de.wikipedia
      var $e;
      var $i  =  $( apply );
      switch ( typeof( adjacent ) ) {
         case "string" :
            $e  =  $( adjacent );
            break;
         case "object" :
            $e  =  adjacent;
            break;
         default:
            $e  =  false;
      }   // switch typeof adjacent
      if ( ! $e.length ) {
         $e  =  mw.util.$content;
      }
      if ( append ) {
         $e.append( $i );
      } else {
         $e.before( $i );
      }
   };   // .furnish()



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



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

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