Jump to content

User:SoledadKabocha/scriptDocClassifier.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SoledadKabocha (talk | contribs) at 05:57, 23 January 2014 (logic was wrong because it doesn't account for the image being linked). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
function classifyScriptDoc( ) {
  if ( !document.getElementById( 'mw-script-doc' ) ) return;

  var clss = '';

  var pageSuffix = /\.(css|js)$/.exec( wgPageName );
  if ( !pageSuffix ) return;
  pageSuffix = pageSuffix[1];
  clss = 'wscript-' + pageSuffix;

  var oppositeSuffix = ( pageSuffix == 'js' ? 'css' : 'js' );

  var sdoc = $( '#mw-script-doc' );
  var sdoclnks = sdoc.find( 'a:not(.image)' );

  if ( sdoclnks.length == 2 ) {
    //2 links: both companion CSS/JS and documentation
    clss += ' wscript-hascompanion wscript-hasdoc wscript-has' + oppositeSuffix;
  }
  else if ( sdoclnks.length == 1 ) {
    //1 link: at most one of companion CSS/JS and documentation present
    if ( sdoclnks.hasClass( 'new' ) ) {
      //no companion CSS/JS nor documentation
      clss += ' wscript-nocompanion wscript-nodoc';
    }
    else {
      var myHref = sdoclnks.attr( 'href' );
      if ( /\.(css|js)$/.test( myHref ) ) {
        //companion CSS/JS only
        clss += ' wscript-hascompanion wscript-nodoc wscript-has' + oppositeSuffix;
      }
      else {
        //documentation only
        clss += ' wscript-nocompanion wscript-hasdoc';
      }
    }
  }
  //else something went wrong

  sdoc.addClass( clss );
}

if ( !window.scriptDocNoAutoClassify ) {
  $(document).ready( classifyScriptDoc );
}