Jump to content

User:PerfektesChaos/js/catTreeToggling/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.
// PerfektesChaos/js/catTreeToggling.js
/// Toggle all CategoryTree elements
/// 2024-02-27 PerfektesChaos@de.wikipedia
/// Documentation:  [[w:en:User:PerfektesChaos/js/catTreeToggling]]
/// Fingerprint:    #0#0#
/// @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.1,
       Signature = "catTreeToggling",
       THIS      = { ct:  { sect: ".CategoryTreeSection",
                            sel:  ".CategoryTreeToggleHandlerAttached",
                            stta: "aria-expanded",
                            sttc: "false",
                            stte: "true"
                          },
                     doc: { site:    "w:en",
                            support: "User:PerfektesChaos/js/"
                                     + Signature },
                     gui: { box: { "background-color": "#D0D0D0",
                                   "border-color":     "#808080",
                                   "border-radius":    "6px",
                                   "border-style":     "solid",
                                   "border-width":     "3px",
                                   "display":          "inline-block",
                                   "margin-bottom":    "1em",
                                   "margin-left":      "1em",
                                   "margin-right":     "1em",
                                   "padding":          "0.3em" },
                            clp: [ "54",
                                   "Blue_Fire.svg",
                                   "collapse" ],
                            exp: [ "6d",
                                   "Blue_Fire_upsidedown.svg",
                                   "expand" ],
                            img: 18
                              }
                   };



   function $file( address, appearance ) {
      // Create <img>
      // Precondition:
      //    address  -- Array, with image location
      //                       [ 0 ]  -- string, with 2 hex storage path
      //                       [ 1 ]  -- string, with signifcant ID
      //    appearance  -- number, with pixel size
      // 2022-07-01 PerfektesChaos@de.wikipedia
      var $r = $( "<img>" );
      $r.attr( { "src": "https://upload.wikimedia.org/"
                        + "wikipedia/commons/thumb/"
                        + address[ 0 ].substr( 0, 1 ) + "/"
                        + address[ 0 ] + "/"
                        + address[ 1 ]
                        + "/" + appearance + "px-"
                        + address[ 1 ] + ".png" } );
      return $r;
   }   // $file()



   function fake( ask ) {
      // Execute toggling
      // Precondition:
      //    ask  -- string, toggle "expand" or "collapsed" elements
      // Uses:
      //    >  .$ct
      //    >  .ct.sel
      //    >  .ct.stta
      //  2023-02-01 PerfektesChaos@de.wikipedia
      var $do = THIS.$ct.find( THIS.ct.sel + ":visible" ),
          i, $h;
      for ( i = $do.length - 1;  i >= 0;  i-- ) {
         $h = $do.eq( i );
         if ( $h.attr( THIS.ct.stta ) === ask ) {
            $h.click();
         }
      }   // for i--
   }   // fake()



   function feed() {
      // Expansion requested
      // Uses:
      //    >  .ct.sttc
      //    fake()
      // 2023-02-01 PerfektesChaos@de.wikipedia
      fake( THIS.ct.sttc );
   }   // feed()



   function fewer() {
      // Collapsing requested
      // Uses:
      //    >  .ct.stte
      //    fake()
      // 2023-02-01 PerfektesChaos@de.wikipedia
      fake( THIS.ct.stte );
   }   // fewer()



   function fiat( $area ) {
      // Create and apply GUI element
      // Precondition:
      //    $area  -- content
      // Uses:
      //    >  .gui.clp
      //    >  .gui.img
      //    >  .gui.exp
      //    >  Signature
      //    >  .sel
      //    >  Version
      //    >  .gui.box
      //    >< .ltr
      //    $file()
      //    (feed)
      //    (fewer)
      // 2023-02-01 PerfektesChaos@de.wikipedia
      var $e   = $( "<div>" ),
          hdls = [ feed, fewer ],
          pics = [ "exp", "clp" ],
          i, s, $img;
      if ( typeof THIS.ltr  !==  "boolean" ) {
         THIS.ltr = ( $( "html" ).attr( "dir" )  !==  "rtl" );
      }
      for ( i = 0;  i < 2;  i++ ) {
         s    = pics[ i ];
         $img = $file( THIS.gui[ s ],  THIS.gui.img );
         $img.attr( { id:    THIS.sel  +  s.substr( 0, 1 ),
                      role:  "button",
                      title: THIS.gui[ s ][ 2 ] } )
             .click( hdls[ i ] )
             .css( { "cursor": "pointer" } );
         $e.append( $img );
      }   // for i
      $e.attr( { id:    THIS.sel + "box",
                 title: Signature + " " + Version } )
        .css( THIS.gui.box )
        .css( "float",  ( THIS.ltr ? "right" : "left" ) );
      $area.prepend( $e );
   }   // fiat()



   function fire( $area ) {
      // DOM ready
      // Precondition:
      //    $area  -- content
      // Uses:
      //    >  .ct.sect
      //    >  Signature
      //    >< .sel
      //     < .$ct
      //     < .sign
      //    fiat()
      // 2023-02-01 PerfektesChaos@de.wikipedia
      THIS.$ct = $area.find( THIS.ct.sect );
      if ( THIS.$ct.length ) {
         if ( typeof THIS.sel  !==  "string" ) {
            THIS.sign = Signature.toLowerCase();
            THIS.sel  = "gadget-" + THIS.sign + "-";
         }
         fiat( $area );
      }
   }   // fire()



   function first() {
      // Autorun on loading
      // Uses:
      //    >  Signature
      //    >  Version
      //    >  .doc.site
      //    >  .doc.support
      //     < .signature
      //     < .pub
      //    (fire)
      // 2023-02-01 PerfektesChaos@de.wikipedia
      var env, rls, launch;
      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",
                                "wgCanonicalSpecialPageName",
                                "wgNamespaceNumber",
                                "wgPageContentModel" ] );
         switch ( env.wgAction ) {
            case "edit":
            case "submit":
            case "view":
               if ( env.wgPageContentModel ===  "wikitext" ) {
                  if ( env.wgNamespaceNumber < -1 ) {
                     launch = ( env.wgCanonicalSpecialPageName
                                                    === "CategoryTree" );
                  } else {
                     launch = true;
                  }
               }
               break;
         }   // switch wgAction
         if ( launch ) {
            mw.hook( "wikipage.content" ).add( fire );
         }
         THIS.pub = { doc:  "[[" + THIS.doc.site + ":"
                                 + THIS.doc.support + "]]",
                      type: Signature,
                      vsn:  Version };
         mw.hook( Signature + ".ready" ).fire( THIS.pub );
      }
   }   // first()



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



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



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

/// EOF </nowiki>   catTreeToggling.js