Open main menu

UESPWiki β

UESPWiki:Javascript/reorderCategories.js

< UESPWiki:Javascript

//

//

  var _catFunction = function(sectionName)
  {
    var categoryList = document.getElementById( sectionName );
    if (null != categoryList)
    {
      var catRE=new RegExp(/<[^>]*>/g);
      var strRE=new RegExp(/^[^-]*-/);
      var catList = categoryList.innerHTML.split(/ \| /);
      var first = catList[0].split(": ");
      catList[0] = first[1];
      catList.sort(function(a,b){var A=a.toUpperCase().replace(catRE, ""); var B=b.toUpperCase().replace(catRE, ""); return ((A<B)?-1:(A==B)?0:1);});
      var colLen = catList.length
      var numCols = 1;
      if (colLen > 8)
      {
        colLen = Math.floor((catList.length+2) / 3);
        numCols = 3
      } else if (colLen > 4)
      {
        colLen = Math.floor((catList.length+1) / 2);
        numCols = 2;
      }

      var catTable = document.createElement("table");
      for (var i=0; i<colLen; i++)
      {
        var row = catTable.insertRow(-1);
        for (var r=0; (i+r<catList.length); r+=colLen)
        {
          var cell = row.insertCell(-1);
          cell.innerHTML = catList[i+r];
          var iText = cell.childNodes[0].childNodes[0];
          if (document.all)
          {
            if (iText.innerText.indexOf("-") != -1) iText.innerHTML = iText.innerText.replace(strRE, "");
          } else {
            if (iText.text.indexOf("-") != -1) iText.innerHTML = iText.text.replace(strRE, "");
          }
        }
      }
      categoryList.innerHTML = first[0];
      categoryList.appendChild(catTable);
    }
  }

  function reorderCategories()
  {
    _catFunction("mw-normal-catlinks");
    _catFunction("mw-hidden-catlinks");
  }

  $(reorderCategories);

//