Jump to content

User:PerfektesChaos/js/keyboardMapper/config.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/keyboardMapper/config.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
//  Establish keyboard mapper for particular pages and forms
// ResourceLoader:  compatible;
//    dependencies: None (mediawiki.util)
/// 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.2,
       Signature = "keyboardMapper",
       CNF       = { bib:       mw.libs,
                     divert:    [ "meta", "alt", "ctrl", "shift" ],
                     doc:       "w:en:PerfektesChaos/js/" + Signature,
                     $meta:     false,
                     transform: null,
                     winLogo:   "1/19/"
                                + "Windows logo - 2002–2012 (Black).svg"
                   },
       PREGO     = { signature: "preferencesGadgetOptions" };



   CNF.style = "." + Signature + "-key {"
               +   "background-color: #F7F7F7;"
               +   "background-image:"
               +               " linear-gradient(rgba(255,255,255,.4),e:"
               +                                "rgba(127,127,127,.2));"
               +   "border-color: #DDDDDD #AAAAAA #888888 #CCCCCC;"
               +   "border-radius: 2px;"
               +   "border-style: solid;"
               +   "border-width: 1px;"
               +   "padding: 0 .4em;"
               +   "text-shadow: 0 1px rgba(255,255,255,.5);"
               + "}";



   CNF.texts = {
      // 2017-07-12 PerfektesChaos@de.wikipedia
      "alt":      {"en": "Alt"},
      "ctrl":     {"en": "Ctrl",
                   "de": "Strg"},
      "desc":     {"en": "Divert keystrokes"
                         + " to insert specified text"
                         + " or trigger other activities.",
                   "de": "Tastendrücke umlenken,"
                         + " um anderen Text einzufügen"
                         + " oder sonstige Aktionen auszuführen."},
      "domain":   {"en": "en.wikipedia.org",
                   "de": "de.wikipedia.org"},
      "empty":    {"en": "Currently no specification.",
                   "de": "Zurzeit nichts spezifiziert."},
      "fun":      {"en": "function",
                   "de": "Funktion"},
      "headKey":  {"en": "Keys",
                   "de": "Tasten"},
      "headTrans":{"en": "Result",
                   "de": "Ergibt"},
      "headWhat": {"en": "Remarks",
                   "de": "Anmerkungen"},
      "numPad":   {"en": "Num<br>pad",
                   "de": "Ziffern-<br>block"},
      "meta":     {"en": "Meta"},
      "reflectC": {"en": "physical name",
                   "de": "physischer Name"},
      "reflectK": {"en": "Input",
                   "de": "Eingabe"},
      "shift":    {"en": "Shift",
                   "de": "Umschalt"},
      "space":    {"en": "Space",
                   "de": "Leer"},
      "stdSeg":   {"en": "Standard<br>board",
                   "de": "Standard-<br>bereich"},
      "version":  {"en": "Version:"}
   };   // CNF.texts



   PREGO.feed = function () {
      // Provide PREGO
      // Uses:
      //    >  PREGO.signature
      //    mw.loader.getState()
      //    mw.loader.load()
      // 2017-08-08 PerfektesChaos@de.wikipedia
      var s = "ext.gadget." + PREGO.signature;
      if ( mw.loader.getState( s )  !==  "ready" ) {
         mw.loader.load( "https://en.wikipedia.org"
                         + "/w/index.php?title="
                         + "User:PerfektesChaos/js/"
                         + PREGO.signature + "/r.js"
                         + "&bcache=1&maxage=604811"
                         + "&action=raw"
                         + "&ctype=text/javascript" );
      }
   };   // PREGO.feed()



   PREGO.foreign = function ( apply ) {
      // Translate local text segment
      // Precondition:
      //    apply  -- string, with ID
      // Postcondition:
      //    Returns string, with translation
      // Uses:
      //    >  CNF.texts
      //    PREGO.app.translation()
      // 2017-07-12 PerfektesChaos@de.wikipedia
      return PREGO.app.translation( CNF.texts[ apply ] );
   };   // PREGO.foreign()



   function face( event ) {
      // Handler for keyboard event
      // Precondition:
      //    event  -- DOM event
      // Uses:
      //    >  CNF.$reflectK
      //    >  CNF.$reflectC
      // 2017-07-12 PerfektesChaos@de.wikipedia
      if ( typeof event.key  ===  "string" ) {
         CNF.$reflectK.text( event.key );
      } else {
         CNF.$reflectK.text( " " );
      }
      if ( typeof event.code  ===  "string" ) {
         CNF.$reflectC.text( event.code );
      } else {
         CNF.$reflectC.html( "&#160;" );
      }
   }   // face()



   function $face( assign, adjust ) {
      // Create key
      // Precondition:
      //    assign  -- string, with label
      //    adjust  -- number, wit style; 3=dense, else key
      // Postcondition:
      //    Returns jQuery element
      // Uses:
      //    >  CNF.winLogo
      //    >  Signature
      //    >< CNF.$meta
      //    PREGO.foreign()
      // 2017-07-12 PerfektesChaos@de.wikipedia
      var s      = assign,
          script = "inherit",
          $r     = $( "<kbd>" ),
          src, system;
      if ( s === " " ) {
         s = PREGO.foreign( "space" );
      }
      if ( s.length === 1 ) {
         script = "monospace,Courier";
         $r.css( { "font-size": "111%" } );
      } else if ( adjust === 2 ) {
         if ( assign === "meta" ) {
            if ( ! CNF.$meta ) {
               system = ( window.navigator.platform || "" );
               if ( ! system.indexOf( "Win" ) ) {
                  src = CNF.winLogo.replace( new RegExp( " ", "g" ),
                                             "_" );
                  src = "https://upload.wikimedia.org/"
                        + "wikipedia/commons/" + encodeURI( src );
                  CNF.$meta = $( "<img>" );
                  CNF.$meta.attr( { "src":    src,
                                    "height": "13px" } );
               } else if ( system.indexOf( "Mac" )  >=  0  ) {
                  CNF.$meta = String.fromCharCode( 0x2318 );
               } else {
                  CNF.$meta = PREGO.foreign( "meta" );
               }
            }
            if ( typeof CNF.$meta  ===  "string" ) {
               s = CNF.$meta;
            } else {
               $r.append( CNF.$meta.clone() );
               s = false;
            }
         } else {
            s = PREGO.foreign( assign );
         }
      }
      if ( adjust === 3 ) {
         $r = $( "<span>" );
         $r.css( { "display":        "inline-block",
                   "font-size":      "72%",
                   "line-height":    "1em",
                   "padding-left":   "2px",
                   "padding-right":  "2px",
                   "vertical-align": "sub" } )
           .html( s );
         s = false;
      } else {
         $r.addClass( Signature + "-key" )
           .css( { "font-family": script } );
      }
      if ( s ) {
         $r.text( s );
      }
      return $r;
   }   // $face()



   function $facet( apply ) {
      // Create keymap table row
      // Precondition:
      //    apply  -- keymap entry
      // Postcondition:
      //    Returns jQuery table row
      // Uses:
      //    $face()
      //    PREGO.foreign()
      //    PREGO.app.translation()
      // 2017-07-12 PerfektesChaos@de.wikipedia
      var $r  = $( "<tr>" ),
          $td = $( "<td>" ),
          i, k, s, seq, $e;
      $td.append( $face( apply.single, 1 ) )
         .css( { "background-color":  "#FFFFFF",
                 "white-space":       "nowrap" } );
      if ( typeof apply.num  ===  "boolean" ) {
         seq = ( apply.num ? "numPad" : "stdSeg" );
         $td.prepend( $face( PREGO.foreign( seq ),  3 ) );
      }
      if ( typeof apply.second  ===  "string" ) {
         $e = $( "<span>" );
         $e.text( "+" );
         $td.prepend( $e )
            .prepend( $face( apply.second, 2 ) );
      }
      $r.append( $td );
      $td = $( "<td>" );
      switch ( typeof apply.transfer ) {
         case "function":
            $td.css( { "font-style":  "italic" } )
               .html( PREGO.foreign( "fun" ) );
            break;
         case "string":
            $e = $( "<code>" );
            $e.text( apply.transfer );
            $td.append( $e )
               .css( { "background-color":  "#FFFFFF" } );
            if ( apply.transfer.length <= 7 ) {
               seq = "U";
               for ( i = 0;  i < apply.transfer.length;  i++ ) {
                  k = apply.transfer.charCodeAt( i );
                  s = Number( k ).toString( 16 )
                                 .toUpperCase();
                  if ( k < 65536 ) {
                     s = "000" + s;
                     if ( k > 15 ) {
                        s = s.substr( s.length - 4 );
                     }
                  }
                  seq = seq + "+" + s;
               }   // for i
               $e  = $( "<span>" );
               $e.text( seq );
               $td.append( $( "<br>" ) )
                  .append( $e );
            }
            break;
      }   // switch typeof apply.transfer
      $r.append( $td );
      switch ( typeof apply.say ) {
         case "object":
            s = PREGO.app.translation( apply.say );
            break;
         case "string":
            s = apply.say;
            break;
         default:
            s = false;
      }   // switch typeof apply.say
      if ( s ) {
         $td = $( "<td>" );
         $td.html( s );
         $r.append( $td );
      }
      return $r;
   }   // $facet()



   function factory() {
      // Details on current keymap
      // Uses:
      //    >  CNF.bib.transform
      //    >  CNF.bib.reverse
      //    >  CNF.bib.divert
      //    >  CNF.$current
      //    >< CNF.style
      //    mw.util.addCSS()
      //    PREGO.foreign()
      //    $facet()
      // 2017-07-12 PerfektesChaos@de.wikipedia
      var defs   = { },
          order  = [ ],
          $table = $( "<table>" ),
          $th    = $( "<th>" ),
          $tr    = $( "<tr>" ),
          i, k, o, part, s, serial, sign, single, sub;
      if ( CNF.style ) {
         mw.util.addCSS( CNF.style );
      }
      for ( sign in CNF.bib.transform ) {
         single = sign;
         if ( single.length === 1 ) {
            k = 0;
         } else if ( single.substr( 0, 6 ) === "Numpad" ) {
            k      = 3;
            single = single.substr( 6 );
            if ( typeof CNF.bib.reverse[ single ]  ===  "string" ) {
               single = CNF.bib.reverse[ single ];
            }
         } else if ( single.substr( 0, 5 ) === "Digit" ) {
            k      = 1;
            single = single.substr( 0, 5 );
         } else  if ( typeof CNF.bib.reverse[ single ]  ===  "string" ) {
            single = CNF.bib.reverse[ single ];
            k = 1;
         } else {
            k = 0;
         }
         if ( single.length === 1 ) {
            serial = "0000000" + single.charCodeAt( 0 );
            serial = serial.substr( serial.length - 7 );
         } else {
            serial = single;
         }
         serial = serial + "." + k + ".";
         part   = CNF.bib.transform[ sign ];
         for ( sub in part ) {
            o = part[ sub ];
            o.single = single;
            if ( k === 3 ) {
               o.num = true;
            } else if ( k === 1 ) {
               o.num = false;
            }
            s = serial;
            for ( i = 0;  i < CNF.bib.divert.length;  i++ ) {
               if ( CNF.bib.divert[ i ]  === sub ) {
                  o.second = sub;
                  s        = s + i;
                  break;   // for i
               }
            }   // for i
            defs[ s ] = o;
            order.push( s );
         }   // for sub in part
         order.sort();
      }   // for sign in CNF.bib.transform
      $table.addClass( "wikitable" );
      $th.html( PREGO.foreign( "headKey" ) );
      $tr.append( $th );
      $th = $( "<th>" );
      $th.html( PREGO.foreign( "headTrans" ) );
      $tr.append( $th );
      $th = $( "<th>" );
      $th.html( PREGO.foreign( "headWhat" ) );
      $tr.append( $th );
      $table.append( $tr );
      for ( k = 0;  k < order.length;  k++ ) {
         $table.append(  $facet( defs[ order[ k ] ] ) );
      }   // for k
      CNF.$current.append( $table );
   }   // factory()



   function fashion() {
      // New keymap arrived
      // Uses:
      //    >  CNF.$current
      //    >  CNF.bib.transform
      //    >  CNF.$empty
      //    mw.loader.using()
      //    (factory)
      // 2017-07-12 PerfektesChaos@de.wikipedia
      CNF.$current.empty();
      if ( typeof CNF.bib.transform  ===  "object"
           &&     CNF.bib.transform ) {
         mw.loader.using( [ "mediawiki.util" ],
                          factory );
      } else {
         CNF.$current.append( CNF.$empty.clone() );
      }
   }   // fashion()



   function father() {
      // Head appliction has been loaded
      // Uses:
      //    >  Signature
      //    >  CNF.$about
      //    >< CNF.bib
      //    PREGO.foreign()
      //    mw.hook()
      //    (fashion)
      // 2017-07-12 PerfektesChaos@de.wikipedia
      var $e;
      if ( typeof CNF.bib[ Signature ]  ===  "object"
           &&     CNF.bib[ Signature ] ) {
         CNF.bib = CNF.bib[ Signature ];
         $e = $( "<span>" );
         $e.css( { "margin-right":  "2em" } )
           .html( PREGO.foreign( "version" ) );
         CNF.$about.append( $e );
         $e = $( "<code>" );
         $e.text( CNF.bib.vsn );
         CNF.$about.append( $e );
         mw.hook( Signature + ".updated" ).add( fashion );
      }
   }   // father()



   function features( application ) {
      // Equip page with docpage and intro
      // Precondition:
      //    application  -- PREGO object
      //    DOM ready
      //    PREGO available
      // Uses:
      //    >  CNF.$doc
      //    >  Signature
      //    >  CNF.$current
      //    >  CNF.$reflectR
      //     < PREGO.app
      //     < PREGO.$empty
      //     < CNF.$reflectK
      //     < CNF.$reflectC
      //    PREGO.foreign()
      //    mw.hook()
      //    (father)
      //    (face)
      // 2017-07-12 PerfektesChaos@de.wikipedia
      var $e;
      if ( typeof application  ===  "object"
           &&     application   &&
           typeof application.translation  ===  "function" ) {
         PREGO.app = application;
         $e = $( "<div>" );
         $e.css( { "font-size":  "111%" } )
           .html( PREGO.foreign( "desc" ) );
         CNF.$doc.append( $e );
         $e = $( "<a>" );
         $e.attr( { href:   "https://"
                            + PREGO.foreign( "domain" )
                            + "/wiki/"
                            + "User:PerfektesChaos/js/"
                            + Signature,
                    target: Signature } )
           .text( Signature + "@PerfektesChaos" );
         CNF.$doc.append( $e );
         CNF.$empty = $( "<span>" );
         CNF.$empty.css( { "font-style":  "italic" } )
                   .html( PREGO.foreign( "empty" ) );
         CNF.$current.append( CNF.$empty );
         $e = $( "<th>" );
         $e.html( PREGO.foreign( "reflectK" ) );
         CNF.$reflectR.append( $e );
         $e = $( "<th>" );
         $e.html( PREGO.foreign( "reflectC" ) );
         CNF.$reflectR.append( $e );
         mw.hook( Signature + ".ready" ).add( father );
         window.addEventListener( "keydown", face, true );
      }
   }   // features()



   function fire() {
      // Initialize page
      // Precondition:
      //    DOM ready
      // Uses:
      //    >  Signature
      //    >  Version
      //    >  PREGO.signature
      //     < CNF.$about
      //     < CNF.$doc
      //     < CNF.$current
      //     < CNF.$reflectR
      //     < CNF.$reflectK
      //     < CNF.$reflectC
      //    mw.hook()
      //    (features)
      // 2017-08-08 PerfektesChaos@de.wikipedia
      var $v  = $( "head" ),
          $e  = $v.find( "title" ),
          $tr = $( "<tr>" );
      $e.remove();
      $e = $( "<title>" );
      $e.text( Signature );
      $v.prepend( $e );
      $v = $( "#firstHeading,#section_0" );
      if ( ! $v.length ) {
         $v = $( "h1" );
      }
      $v.eq( 0 ).text( Signature );
      $v = mw.util.$content.find( "#mw-content-text" );
      $v.empty();
      $e = $( "<div>" );
      $e.attr( { "id":  Signature + "-doc" } );
      $v.append( $e );
      CNF.$doc = $e;
      $e = $( "<div>" );
      $e.attr( { "id":  Signature + "-about" } );
      $v.append( $e );
      CNF.$about = $e;
      $v.append( $( "<hr>" ) );
      $e = $( "<div>" );
      $e.attr( { "id":  Signature + "-current" } );
      $v.append( $e );
      CNF.$current = $e;
      $v.append( $( "<hr>" ) );
      $e = $( "<table>" );
      CNF.$reflectR = $( "<tr>" );
      $e.addClass( "wikitable" )
        .append( CNF.$reflectR );
      $tr = $( "<tr>" );
      CNF.$reflectK = $( "<td>" );
      CNF.$reflectK.html( "&#160;" );
      $tr.append( CNF.$reflectK );
      CNF.$reflectC = $( "<td>" );
      $tr.append( CNF.$reflectC );
      $e.append( $tr );
      $v.append( $e )
        .append( $( "<hr>" ) );
      $e = $( "<div>" );
      $e.css( { "font-size":  "smaller" } )
        .text( Version );
      $v.append( $e );
      mw.hook( PREGO.signature + ".ready" ).add( features );
   }   // fire()



   function first() {
      // Autorun on loading
      // Uses:
      //    >  Signature
      //    >  PREGO
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.config.get()
      //    PREGO.feed()
      //    (fire)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var signature = "ext.gadget." + Signature + "/config",
          env, rls;
      if ( mw.loader.getState( signature )  !==  "ready" ) {
         rls = { };
         rls[ signature ] = "ready";
         mw.loader.state( rls );
         env = mw.config.get( [ "wgTitle",
                                "wgCanonicalSpecialPageName" ] );
         if ( env.wgCanonicalSpecialPageName === "Blankpage"  &&
              env.wgTitle.indexOf( "/" + Signature )  >  0 ) {
            PREGO.feed();
            $( fire );
         }
      }
   }   // first()



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



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

/// EOF </nowiki>   keyboardMapper/config.js