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:23, 23 January 2014 (create as promised). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 = 'script-' + pageSuffix;

  var oppositeSuffixChoices = { 'css', 'js' };
  var oppositeSuffix = oppositeSuffixChoices[pageSuffix.length - 2];

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

  if ( sdoclnks.length == 2 ) {
    //2 links: both companion CSS/JS and documentation
    clss += ' script-hascompanion script-hasdoc script-has' + oppositeSuffix;
  }
  else if ( sdoclnks.length == 1 ) {
    if ( sdoclnks.hasClass( 'new' ) ) { //no companion CSS/JS nor documentation
      clss += ' script-nocompanion script-nodoc';
    }
    else {
      var myHref = sdoclnks.attr( 'href' );
      if ( /\/doc$/.test( myHref ) ) { //documentation only
        clss += ' script-nocompanion script-hasdoc';
      }
      else { //companion CSS/JS only
        clss += ' script-hascompanion script-nodoc script-has' + oppositeSuffix;
      }
    }
  }
  //else something went wrong

  sdoc.addClass( clss );
}

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