User:PerfektesChaos/js/resultListSort/d.js

/// mw:User:PerfektesChaos/js/resultListSort/?.js /// 2012-07-13 PerfektesChaos@de.wikipedia // Sort result lists as of "Whatlinkshere" or "Search" special page. // ResourceLoader: compatible; dependencies: user, mediawiki.util /// Fingerprint: #0#0# /// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA) /// /* jshint curly:true, eqeqeq:true, undef:true, white:false            */ /* global window:false, document:false, jQuery:false, mediaWiki:false */ /*jslint plusplus: true, vars: true, white: true                      */ /*globals document, jQuery, mediaWiki, window                         */

( function ( mw, $ ) {  "use strict";   if ( typeof mw.libs.resultListSort !== "object" ) {      mw.libs.resultListSort  =  {  opt:  { }  };   }   mw.libs.resultListSort.vsn   =  -1.0;   mw.libs.resultListSort.type  =  "resultListSort";   if ( typeof mw.libs.resultListSort.opt !== "object" ) {      mw.libs.resultListSort.opt  =  { };   }

/*   * 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 */

// Requires: JavaScript 1.3 //          ECMA        262-3 § 11.8.5  (string comparison operators) //          MediaWiki   1.18 (mw.libs, jQuery core)

mw.libs.resultListSort.fair =  function ( $access, aligned ) { // Make sortable element from jQuery list item // Precondition: //   $access  -- jQuery list item //   aligned  -- mode (structure) //               1  -- direct child //               2  -- div.mw-search-result-heading // Postcondition: //   Returns   [ $access, ns, title, talkmod ] // Uses: //   this //   >  .joint //   >  .reNS //   >  .space2num //   >  .opt.group //   >  .opt.casing //   .fetch // 2012-07-13 PerfektesChaos@de.wikipedia var g;     var m   =  0; var n  =  0; var p;     var s   =  this.fetch( $access, aligned ); if ( s ) { if ( this.joint ) { s =  s.substr( this.joint ); }        g  =  this.reNS.exec( s ); if ( g ) { p =  g[ 1 ]; n =  this.space2num[ p ]; s =  s.substr( p.length + 1 ); if ( mw.libs.resultListSort.opt.group ) { m =  n % 2; n =  ( n - m )  /  2; }        }         if ( mw.libs.resultListSort.opt.casing ) { s =  s.toLowerCase; }     } else { s =  ""; }     return  [ $access, n, s, m ]; };  // .fair

mw.libs.resultListSort.fetch =  function ( $access, aligned ) { // Retrieve URL from jQuery list item // Precondition: //   $access  -- jQuery list item //   aligned  -- mode (structure) //               1  -- direct child //               2  -- div.mw-search-result-heading // Postcondition: //   Returns URL string // 2012-07-13 PerfektesChaos@de.wikipedia var $d =  false; var r  =  false; switch ( aligned ) { case 1 : $d =  $access; break; case 2 : $d =  $access.children( ".mw-search-result-heading" ); break; }  // switch aligned if ( $d ) { if ( $d.length ) { r =  $d.children( "a" ).attr( "href" ); }     }      return  r;   };   // .fetch

mw.libs.resultListSort.fiat =  function ( $alike, aligned ) { // Ensure namespace RegExp and list objects // Precondition: //   $alike  -- jQuery list item example // Postcondition: //   configuration has been set // Uses: //   this //   >< .reNS //   >< .space2num //   >< .start //    < .joint //   mw.config.get //   .fetch // Requires: JavaScript 1.3  charCodeAt // 2012-07-13 PerfektesChaos@de.wikipedia var re, s;     if ( ! this.reNS ) { this.space2num =  mw.config.get( "wgNamespaceIds" ); re             =  ""; for ( s in this.space2num ) { if ( this.space2num.hasOwnProperty( s ) ) { if ( this.space2num[ s ] ) { re =  re  +  ( re ? "|" : "" )  +  s;               } }        }   // for s in this.space2num this.reNS =  new RegExp( "^(" + re + "):",  "i" ); }     this.joint  =  0; if ( $alike ) { if ( ! this.start ) { s          =  mw.config.get( "wgArticlePath" ); this.start =  s.replace( /\$1/, "" ); }        s  =  this.fetch( $alike, aligned ); if ( s ) { if ( s.charCodeAt(0) === 47 ) {  // '/' if ( s.charCodeAt(1) === 47 ) {  // '/' re =  "//" + window.location.host + this.start; } else { re =  this.start; }           } else { re =  window.location.protocol + "//" + window.location.host + this.start; }           if ( s.indexOf( re )  ===  0 ) { this.joint =  re.length; }        }      }   };   // .fiat

mw.libs.resultListSort.find =  function ( album, ask ) { // Is element matching? // Precondition: //   album  -- collection or single item //   ask    -- particular element // Postcondition: //   Returns false iff not matching // 2012-07-13 PerfektesChaos@de.wikipedia var i;     var r  =  false; switch ( typeof album ) { case "number" : case "string" : r =  ( album === ask ); break; case "object" : if ( album ) { if ( typeof album.length ===  "number" ) {   // Array for ( i = 0; i < album.length;  i++ ) { r =  ( album[i] === ask ); if ( r ) { break;  // for i                     } }  // for i               } else { r =  album[ ask ]; }           }            break; }  // switch typeof album return r;   };   // .find

mw.libs.resultListSort.fire =  function  { // Start possible actions on current page // Precondition: //   Current page is special page // Uses: //   >  .opt.auto //   >  .opt.portlet //   >  .specialPage //   .find //   .fresh //   .furnish // Remark: May be used as event handler -- 'this' is not accessed // 2012-07-13 PerfektesChaos@de.wikipedia var RLS =  mw.libs.resultListSort; if ( RLS.opt.auto ) { if ( RLS.find( RLS.opt.auto, RLS.specialPage ) ) { RLS.fresh; }     }      if ( RLS.opt.portlet === undefined ) { RLS.furnish; } else if ( RLS.opt.portlet ) { if ( RLS.find( RLS.opt.portlet, RLS.specialPage ) ) { RLS.furnish; }     }   };   // .fire

mw.libs.resultListSort.first =  function ( arg1, arg2 ) { // Compare two list elements //   element:  [ *, ns, title, talkmod, ... ]     // Precondition: //   arg1  -- first element //   arg2  -- second element // Postcondition: //   Returns number  -1 :  arg2 -> arg1 //                    0 :  arg1 == arg2 //                    1 :  arg1 -> arg2 // 2012-07-13 PerfektesChaos@de.wikipedia var r =  arg1[ 1 ]  -  arg2[ 1 ]; if ( ! r ) { if ( arg1[ 2 ] ===  arg2[ 2 ] ) { r =  arg1[ 3 ]  -  arg2[ 3 ]; } else if ( arg1[ 2 ] <  arg2[ 2 ] ) { r =  -1; } else { r =  1; }     }      return  r;   };   // .first

mw.libs.resultListSort.fresh =  function  { // Launch sorting process on current page // Uses: //   >  .specialPage //   >  .reNS //   >  mw.util.$content //   jQuery.find //   jQuery.children //   .fiat //   .fair //   (.first) // Remark: May be used as event handler -- 'this' is not accessed // 2012-07-13 PerfektesChaos@de.wikipedia var g, i, k, n, $lis; var $ul =  false; switch ( mw.libs.resultListSort.specialPage ) { case "Whatlinkshere" : k   =  1; $ul =  "#mw-whatlinkshere-list"; break; case "Search" : k   =  2; $ul =  ".mw-search-results"; break; }  // switch typeof album if ( $ul ) { $ul =  mw.util.$content.find( $ul ); if ( $ul.length ) { $lis =  $ul.children( "li" ); n    =  $lis.length; if ( n > 1 ) { g =  new Array( n ); mw.libs.resultListSort.fiat( $lis.eq( 0 ), k ); for ( i = 0; i < n;  i++ ) { g[ i ] =  mw.libs.resultListSort.fair( $lis.eq( i ),                                                          k ); }  // for i               g.sort( mw.libs.resultListSort.first ); $lis.detach; for ( i = 0; i < n;  i++ ) { $ul.append( g[ i ][ 0 ] ); }  // for i            } }     }   };   // .fresh

mw.libs.resultListSort.furnish =  function  { // Equip current page with portlet link // Uses: //   mw.util.addPortletLink //   (.fresh) // 2012-07-13 PerfektesChaos@de.wikipedia var show =  "Sort results"; switch ( mw.config.get( "wgUserLanguage" ) ) { case "de" : case "de-at" : case "de-ch" : case "de-formal" : case "als" : case "nds" : show =  "Sortiere Ergebnisse"; break; }  // switch wgUserLanguage mw.util.addPortletLink( "p-cactions",                             "javascript:"                               + "mw.libs.resultListSort.fresh;"                               + "void(0);",                              show,                              "ca-sort",                              show,                              null,                              "ca-sort",                              "#ca-history" ); };  // .furnish

if ( ! mw.libs.resultListSort.loaded ) { mw.libs.resultListSort.loaded =  true; if ( mw.config.get( "wgNamespaceNumber" ) ===  -1 ) { $( document ).ready( function {               mw.libs.resultListSort.specialPage  =                           mw.config.get( "wgCanonicalSpecialPageName" );               mw.loader.using( [ "user", "mediawiki.util" ], mw.libs.resultListSort.fire );                                         } );   // .ready }  }   mw.loader.state( "ext.gadget.resultListSort", "ready" ); }( mediaWiki, jQuery ) );

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

/// EOF  resultListSort/?.js