Jump to content

User:PerfektesChaos/js/externalLinkProblem/talk/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/externalLinkProblem/talk/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
// Sub-module: Look for template on talk page, and trigger further action
// ResourceLoader:  compatible;
//                  dependencies: mediawiki.util
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <nowiki>
/* 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 Version    =  -2.8,
       ELP        =  "externalLinkProblem",
       Sub        =  "talk",
       Section    =  "#deadurl_",
       SubjectPg  =  "SubjectPg";
   if ( typeof mw.libs[ ELP ]  !==  "object"   ||   ! mw.libs[ ELP ] ) {
      mw.libs[ ELP ]  =  { };
   }
   mw.libs[ ELP ].type  =  ELP;
   ELP                  =  mw.libs[ ELP ];



   /*
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License as
    * published by the Free Software Foundation; either version 2 of the
    * License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program;
    * if not, write to the Free Software Foundation, Inc.,
    * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    * http://www.gnu.org/copyleft/gpl.html
    */



   if ( typeof ELP[ Sub ]  !==  "object" ) {
      ELP[ Sub ]  =  { };
   }
   ELP[ Sub ].vsn  =  Version;
   if ( typeof ELP[ Sub ].texts  !==  "object" ) {
      ELP[ Sub ].texts  =  {
         // 2015-11-23 PerfektesChaos@de.wikipedia
         "subjectPage":      {"en": "article",
                              "de": "Artikel"},
         "subjectPage-edit": {"en": "edit entire article source code",
                              "de": "Gesamten Artikelquelltext"
                                    + " bearbeiten"}
      };   // .talk.texts
   }



   function facilitated() {
      // Mark sub-module as ready
      // Uses:
      //    >  .signature
      //    >  Sub
      //    >  .type
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.hook()
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var rls, signature, sub;
      if ( typeof ELP.signature  ===  "string" ) {
         sub        =  "/" + Sub;
         signature  =  ELP.signature + sub;
         if ( mw.loader.getState( signature )  !==  "ready" ) {
            rls = { };
            rls[ signature ] = "ready";
            mw.loader.state( rls );
            mw.hook( ELP.type + sub + ".ready" ).fire();
         }
      }
   }   // facilitated()



   function fed( about ) {
      // Trigger GUI ELP.problems details
      // Precondition:
      //    about  -- referrerID (traceback)
      // Uses:
      //    >  .gui.fresh
      //    >  .talk.leader
      //    >  .problems
      //    .gui.fresh()
      // 2015-11-22 PerfektesChaos@de.wikipedia
      if ( typeof ELP.gui  ===  "object"
           &&     ELP.gui   &&
           typeof ELP.gui.fresh  ===  "function"   &&
           ( ELP.talk.leader  ||  ELP.problems === null ) ) {
         ELP.gui.fresh( about );
      }
   }   // fed()



   function feed() {
      // Equip current page with details or plain box
      // Uses:
      //    >  .talk.leader
      //    >  .raw.fire
      //    >< .config.lazy
      //    >< .config.low
      //     < .request
      //    .feed()
      //    (.raw.fire)
      //    (fed)
      // 2015-11-23 PerfektesChaos@de.wikipedia
      var launch  =  ELP.talk.leader,
          lazy, low;
      if ( typeof ELP.config.lazy  ===  "boolean" ) {
         lazy  =  ELP.config.lazy;
      } else {
         ELP.config.lazy  =  false;
      }
      if ( typeof ELP.config.low  ===  "boolean" ) {
         low  =  ELP.config.low;
      } else {
         ELP.config.low  =  false;
      }
      if ( ! low ) {
         launch  =  true;
         if ( typeof ELP.raw  ===  "object"
              &&     ELP.raw   &&
              typeof ELP.raw.fire  ===  "function" ) {
            ELP.raw.fire( fed );
         } else {
            ELP.feed( "raw",
                      function () {
                         ELP.raw.fire( fed );
                      } );
         }
         if ( ! lazy ) {
            ELP.feed( "extlinks", fed );
         }
      }
      if ( launch ) {
         ELP.request  =  "gui";
         ELP.feed( ELP.request, fed );
      }
   }   // feed()



   function fetched( arrived ) {
      // Postprocess after ajax request for talk page categories
      // Precondition:
      //    arrived  -- JSON result of ajax query
      // Uses:
      //     < .talk.id
      //    feed()
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var q;
      if ( typeof arrived  ===  "object"   &&
           typeof arrived.query  ===  "object"  ) {
         q  =  arrived.query;
         if ( typeof q.pageids  ===  "object"   &&
              typeof q.pages  ===  "object" ) {
            ELP.talk.id  =  q.pageids[ 0 ];
            q  =  q.pages[ ELP.talk.id ];
            if ( q   &&   typeof q.categories  ===  "object" ) {
               feed();
            }
         }
      }
   }   // fetched()



   function find() {
      // Check whether template is used on talk page
      // Precondition:
      //    Current page is subject page.
      //    "mediawiki.api"
      // Uses:
      //    >  .nsubject
      //    >  .project.template.scan
      //     < .envNS
      //     < .spot
      //     < .talk.stuff
      //    mw.config.get()
      //    mw.Api()
      //    (fetched)
      // Remark: May be used as event handler -- 'this' is not accessed
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var env  =  mw.config.get( [ "wgFormattedNamespaces",
                                   "wgTitle" ] ),
          q    =  new mw.Api(),
          w    =  { action:       "query",
                    indexpageids: true,
                    prop:         "categories"
                  };
      ELP.envNS       =  env.wgFormattedNamespaces;
      ELP.spot        =  env.wgTitle;
      ELP.talk.stuff  =  ELP.envNS[ ELP.nsubject + 1 ]
                         + ":" + ELP.spot;
      w.titles        =  ELP.talk.stuff.replace( /\s/g, "_" );
      w.clcategories  =  "category:" + ELP.project.template.scan;
      q.get( w ).done( fetched );
   }   // find()



   function fire() {
      // Start possible actions on current page
      // Precondition:
      //    Current page is a meaningful presentation
      //    User resources have been loaded
      // Uses:
      //    >  .talk.leader
      //    >  .config.lone
      //    >  .project.template.scan
      //    mw.config.get()
      //    mw.loader.using()
      //    jQuery.inArray()
      //    feed()
      //    (find)
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var lone;
      if ( typeof ELP.config.lone  ===  "boolean" ) {
         lone  =  ELP.config.lone;
      }
      if ( ELP.talk.leader ) {   // subject page
         if ( lone ) {
            lone  =  ( ELP.scope !== "view" );
         }
         if ( ! lone  &&  true ) {
            mw.loader.using( [ "mediawiki.api" ],
                             find );
         }
      } else if ( ! lone   &&
                  typeof ELP.project.template.scan  ===  "string"
                  &&     ELP.project.template.scan   &&
                  $.inArray( ELP.project.template.scan,
                             mw.config.get( "wgCategories" ) )  >=  0 ) {
         feed();
      }
   }   // fire()



   function flip( action ) {
      // Create headline button for talk page section
      // Precondition:
      //    action  -- false: view subject page,  true: edit
      // Postcondition:
      //    Returns jQuery link
      // Uses:
      //    >  SubjectPg
      //    >  .config.loose
      //    >  .type
      //    >  .talk.texts
      //    >  .title
      //    >< .gui.texts
      //    mw.util.getUrl()
      // 2016-04-25 PerfektesChaos@de.wikipedia
      var $r  =  $( "<a />" ),
          shift  =  "subjectPage",
          params, s;
      if ( ELP.config.loose ) {
         s  =  "_blank";
      } else {
         s  =  SubjectPg;
      }
      if ( action ) {
         params  =  { action: "edit" };
         shift   =  shift + "-edit";
      }
      $r.attr( { "href":   ELP.title.getUrl( params ),
                 "id":     ELP.type + "-" + shift,
                 "target": s } );
      $r.css( { "background-color": "#F0F0F0",
                "border-color":       "#E0E0E0"
                                    + " #E0E0E0"
                                    + " #707070"
                                    + " #707070",
                "border-style":     "solid",
                "border-width":     "2px",
                "display":          "inline-block",
                "font-size":        "small",
                "font-weight":      "normal",
                "line-height":      "1em",
                "margin-left":      "2em",
                "padding":          "0.3em",
                "text-decoration":  "none",
                "vertical-align":   "super",
                "white-space":      "nowrap" } );
      ELP.gui.texts[ shift ]  =  ELP.talk.texts[ shift ];
      $r.text( ELP.lang.fine( shift ) );
      return $r;
   }   // flip()



   function furnish() {
      // Insert headline buttons for talk page section
      // Uses:
      //    >  Section
      //    >  .talk.texts
      //    >  nsubject
      //    >< .spot
      //    >< .gui.texts
      //     < .title
      //    mw.Title()
      //    flip()
      // 2015-11-24 PerfektesChaos@de.wikipedia
      var $head;
      if ( typeof ELP.gui  ===  "object"
           &&     ELP.gui    &&
           typeof ELP.gui.texts  ===  "object"
           &&     ELP.gui.texts    &&
           typeof ELP.lang  ===  "object"
           &&     ELP.lang    &&
           typeof  ELP.lang.fine  ===  "function" ) {
         $head  =  $( Section );
         if ( $head.length ) {
            $head  =  $head.parent( ".mw-headline" );
            if ( $head.length ) {
//             $head  =  $head.parent();
               if ( typeof ELP.spot  !==  "string" ) {
                  ELP.spot  =  mw.config.get( "wgTitle" );
               }
               ELP.title  =  new mw.Title( ELP.spot, ELP.nsubject );
               $head.after( flip( false ) );
//             $head.append( flip( true, shift ) );
            }
         }
      }
   }   // furnish()



   ELP[ Sub ].fire  =  function () {
      // Start possible actions on current page
      // Precondition:
      //    Current page is meaningful
      // Uses:
      //    >  .config
      //    >  .project
      //    >  .project.template
      //    >< .request
      //     < .nsubject
      //     < .talk.leader
      //    facilitated()
      //    mw.loader.using()
      //    (fire)
      // Remark: May be used as event handler -- 'this' is not accessed
      // 2015-10-28 PerfektesChaos@de.wikipedia
      facilitated();
      if ( typeof ELP.config  ===  "object"
           &&     ELP.config    &&
           typeof ELP.project  ===  "object"
           &&     ELP.project    &&
           typeof ELP.project.template  ===  "object"
           &&     ELP.project.template    &&
           typeof ELP.request  ===  "object"
           &&     ELP.request    &&
           typeof ELP.request[ 0 ]  ===  "number" ) {
         ELP.nsubject     =  ELP.request[ 0 ];
         ELP.talk.leader  =  ELP.request[ 1 ];
         ELP.request      =  false;
         mw.loader.using( [ "mediawiki.util" ],
                          fire );
      }
   };   // .talk.fire()



   ELP.talk.former  =  function () {
      // Create list of disappeared URL for box
      // Postcondition:
      //    jQuery list to be appended to box, or not
      // Uses:
      //    >  .problems
      // 2015-11-27 PerfektesChaos@de.wikipedia
      var i, n, o, u, $li, $r;
      if ( typeof ELP.problems  ===  "object"
           &&     ELP.problems    &&
           typeof ELP.problems.length  ===  "number" ) {
         n  =  ELP.problems.length;
         if ( n ) {
            u  =  [ ];
            for ( i = 0;  i < n;  i++ ) {
               o  =  ELP.problems[ i ];
               if ( ! ( o.learnt || o.live ) ) {
                  u.push( o.url );
               }
            }   // for i
            if ( u.length ) {
               $r  =  $( "<ul>" );
               for ( i = 0;  i < u.length;  i++ ) {
                  $li  =  $( "<li>" );
//                $li.css( { "text-decoration": "line-through" } );
                  $li.text( u[ i ] );
                  $r.append( $li );
               }   // for i
            }
         }
      }
      return $r;
   };   // .talk.former()



   ELP.talk.furnish  =  function ( about ) {
      // Create link for box
      // Precondition:
      //    about  -- tooltip
      //    Called by existing .gui implementing .lang
      // Postcondition:
      //    Returns jQuery link
      // Uses:
      //    >  .template.stamp
      //    >  .talk.leader
      //    >  .config.loose
      //    >  .talk.id
      //    >< Section
      //    mw.util.getUrl()
      //    mw.loader.using()
      //    (furnish)
      // 2015-11-24 PerfektesChaos@de.wikipedia
      var $r  =  $( "<a />" ),
          s   =  Section;
      if ( typeof ELP.project.template.stamp  ===  "string"
           &&     ELP.project.template.stamp ) {
         Section  =  Section + ELP.project.template.stamp;
      }
      if ( ELP.talk.leader ) {   // subject page
         if ( ELP.config.loose ) {
            s  =  "_blank";
         } else {
            s  =  "ExternalL";
         }
         $r.attr( { "target": s,
                    "title":  about } );
         s  =  mw.util.getUrl( "Special:Redirect/page/" + ELP.talk.id );
      } else {
         s  =  "";
         mw.loader.using( [ "mediawiki.Title" ],
                          furnish );
      }
      $r.attr( "href",  s + Section );
      return $r;
   };   // .talk.furnish()



   function first() {
      // Initialize sub-module
      // Uses:
      //    >  Sub
      //    facilitated()
      //    .featuring()
      //    (.talk.fire)
      // 2015-10-22 PerfektesChaos@de.wikipedia
      facilitated();
      if ( typeof ELP.featuring  ===  "function" ) {
         ELP.featuring( ELP[ Sub ].fire );
      }
   }   // first()
   first();   // autorun
}( window.mediaWiki, window.jQuery ) );



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

/// EOF </nowiki>   externalLinkProblem/talk/d.js