MediaWiki:Common.js

From SABR Encyclopedia

(Difference between revisions)
Jump to: navigation, search
GarverP01 (Talk | contribs)
(Created page with '- Any JavaScript here will be loaded for all users on every page load.: /* Test if an element has a certain class ************************************** * * Description: Use…')

Latest revision as of 20:12, 15 June 2010

/* Any JavaScript here will be loaded for all users on every page load. */
/* Test if an element has a certain class **************************************
 *
 * Description: Uses regular expressions and caching for better performance.
 * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
 * Copied from wp:MediaWiki:Common.js by Philip J. Rayment, 24 March 2009.
 */

var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();

/** Collapsible tables and divs *************************
 *
 * Description: Allows tables and divs to be collapsed, showing only the header.
 * Written by: Philip J. Rayment, heavily adapted from wp:MediaWiki.Common.js code for collapsible tables and navboxes.
 * Further improvement by Nx.
 *
 */


var autoCollapse = 3;
var collapseCaption = "hide";
var expandCaption = "show";

function collapseBox(index)
{
   var Button = document.getElementById("collapseButton"+index);
   var Box = document.getElementById("collapsibleBox"+index);

   if (!Box || !Button)
   { return false; }

   var disp = "";
   if (Button.firstChild.data == collapseCaption)
   {
      Button.firstChild.data = expandCaption;
      disp="none";
   }
   else
   {
      Button.firstChild.data = collapseCaption;
      if (Box.tagName != "TABLE")
      { disp="block"; }
   }

   if (Box.tagName == "TABLE")
   {
      var Rows = Box.rows;
      for (var i=1; i<Rows.length; i++)
        {
           if (disp == "none")
           { Rows[i].style.display = disp; }
           else
           { Rows[i].style.display = Rows[0].style.display; }
        }
   } 
   else //<div>
   {
       /* Only works with divs */
       var Divs = Box.getElementsByTagName("div");
       for (var i=1; i<Divs.length; i++)
       { 
          if (Divs[i] && Divs[i].style)
          { Divs[i].style.display = disp; }
       }

   }
      
} // function collapseBox

function createCollapseButtonsFromArray(Boxes)
{
   for (var i = 0; i < Boxes.length; i++ )
   {
         Boxes[i].setAttribute("id", "collapsibleBox"+i);
         if (Boxes[i].tagName == "TABLE")
         { var Header = Boxes[i].rows[0].cells[0]; } //first cell of first row.
         else
         { //locate first inner <div>
            var Header = Boxes[i].firstChild;
            if (Header.tagName != "DIV") {Header = Header.nextSibling;}
         }

         var Button     = document.createElement("span");
         var ButtonLink = document.createElement("a");
         var ButtonText = document.createTextNode(collapseCaption);

         Button.className = "collapseButton";
//         ButtonLink.style.color = Header.style.color;
         ButtonLink.setAttribute("id","collapseButton"+i);
         ButtonLink.setAttribute("href","javascript:javascript:collapseBox(" +i + ");");
         ButtonLink.appendChild(ButtonText);

         Button.appendChild(document.createTextNode("[") );
         Button.appendChild(ButtonLink);
         Button.appendChild(document.createTextNode("]") );

         if (Boxes[i].tagName == "TABLE")
         { Header.insertBefore(Button,Header.firstChild); }
         else
         { Header.insertBefore(Button,Header.childNodes[0]); }
   }
}

function createCollapseButtons()
{
   var Boxes = getElementsByClassName(document.getElementById("bodyContent"),"*","collapsible");
   if (Boxes.length > 0)
   {
      createCollapseButtonsFromArray(Boxes);
      for (var i=0; i<Boxes.length; i++)
      {
         if (hasClass(Boxes[i],"collapsed") || Boxes.length >= autoCollapse && hasClass(Boxes[i],"autocollapse"))
         { collapseBox(i); }
      }
   }
}

addOnloadHook(createCollapseButtons);

function unhide(divID) 
{
  var item = document.getElementById(divID);
  if (item) 
  {
    item.className=(item.className=='hidden')?'unhidden':'hidden';
  }
}
Personal tools