Difference between revisions of "MediaWiki:NavigationTabs.js"

From DigiNet Wiki
Jump to: navigation, search
(Created page with "// Navigate Tabs. Allows for lots of information to be displayed on a page in a more compact form. // Maintained by User:Darklama function Navigate_Tabs() { function ...")
 
(No difference)

Latest revision as of 07:54, 13 July 2015

// Navigate Tabs. Allows for lots of information to be displayed on a page in a more compact form.
 // Maintained by [[User:Darklama]]
 
function Navigate_Tabs()
{
  function select_tab(id, update) {
    if (!id) return;
    var contents = document.getElementById(id.substring(1));
    if (contents && hasClass(contents, 'contents') && hasClass(contents.parentNode, "tabnav")) {
      for (var tabs = contents.parentNode.firstChild; tabs != null; tabs = tabs.nextSibling) {
        if (hasClass(tabs, "tabs")) {
          var links = tabs.getElementsByTagName("a");
          for (var i = 0; link = links[i]; i++) {
            if (hasClass(link.parentNode, "selected"))
              link.parentNode.className = link.parentNode.className.replace(/(\s|^)selected(\s|$)/g, "");
            if (link.hash && link.hash == id)
              link.parentNode.className += (link.parentNode.className ? " " : "") + "selected";
          }
        } else if (hasClass(tabs, "contents"))
          tabs.style.display = "none";
        }
      }
      contents.style.display = "block";
 
      if (update) {
        if (get_query_string(window.location.search, "tab"))
          window.location.replace(window.location.toString.replace(/([&?])tab=[^&]*?/, "$1tab="+encodeURIComponent(id)));
        else if (window.location.search)
          window.location.replace(window.location.toString + '&tab=' + encodeURIComponent(id));
        else
          window.location.replace(window.location.toString + '?tab=' + encodeURIComponent(id));
      }
    }
  }
 
  function clicked_tab(e)
  {
    var target;
    if (!e) e = window.event;
    if (e.target) target = e.target;
    else if (e.srcElement) target = e.srcElement;
    else return;
    if (target.nodeType && target.nodeType == 3)
      target = target.parentNode;
    if (target.nodeType && (target.tagName.toLowerCase() != 'a' || !target.hash))
      return;
    if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
 
    select_tab(target.hash ? target.hash : target, true)
  }
 
  if (window.location.search) {
    var tab = get_query_string(window.location.search, 'tab');
    if (tab) select_tab(tab);
  }
 
  var page = document.getElementById("bodyContent") || document.getElementById("article") || document.getElementById("content");
  if (page) {
    var tree = page.getElementsByTagName("*");
    for (var i=0; node=tree[i]; i++) {
      if (hasClass(node, 'navtabs')) {
        node.onclick = clicked_tab;
      } else if (!window.location.search && node.nodeName.toLowerCase() == 'a' && node.hash) {
        var pNode = (hasClass(node.parentNode, 'selected') ? node.parentNode.parentNode : null);
 
        pNode = ((pNode && hasClass(pNode, 'tabs')) ? pNode.parentNode : null);
        pNode = ((pNode && hasClass(pNode, 'navtabs')) ? true : null);
 
        if (pNode) select_tab(node.hash);
      }
    }
  }
}
 
addOnloadHook(Navigate_Tabs);