Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***
  2.  * Filename:
  3.  *  vertnav.js
  4.  *
  5.  * Purpose:
  6.  *  Facilitate a vertical navigation menu with collapsable submenus.
  7.  *  This code will automatically detect which menu items have submenus
  8.  *  and handle them accordingly.
  9.  *
  10.  * Notes:
  11.  *  DON'T bother using this file to:
  12.  *      -change look/feel of menu: this is done from CSS script (ie: vertnav.css).
  13.  *      -add/remove/edit menu items: this is done within the HTML
  14.  *  
  15.  *  .
  16.  *  
  17.  *
  18.  */
  19.  
  20. /*
  21.  *
  22.  * CSS settings:
  23.  *  Normally never has to be touched, unless class names are changed
  24.  *  in the CSS or id is changed of the div wrapping the navigation <li>
  25.  *  tags(list items) in the HTML.
  26.  *  TO CHANGE LOOK/FEEL: simly edit the corresponding CSS class entries (not here)!!
  27.  *  
  28.  *  The navconfig_ settings should match with existing class entries in a linked stylesheet or CSS script
  29.  *
  30.  *
  31.  */
  32. var vertnavcfg_subshow_class = "subshow"; /* class used when toggled visible */
  33. var vertnavcfg_subhide_class = "subhide"; /* class used when toggled hidden */
  34. var vertnavcfg_hashiddensub_class = "hashiddensub"; /* class used when parent has a hidden submenu */
  35. var vertnavcfg_hasvisiblesub_class = "hasvisiblesub"; /* class used when parent has a visible submenu */
  36. var vertnavcfg_navlistdiv_id = "vertnavlist"; /* div wrapping the navigation <li> tags (list items) */
  37.  
  38. /*
  39.  *
  40.  * register event handler to initialize the navigation system on page load
  41.  *
  42.  */
  43. window.onload=vertnav_init;
  44.  
  45.  
  46. /*
  47.  *
  48.  * state handling variables
  49.  *
  50.  */
  51. var vertnavst_subclick = false;
  52.  
  53. /**
  54.  * Function:
  55.  *  toggleNav
  56.  *  
  57.  * Purpose:
  58.  *  for use on click event to
  59.  *  appropriately change the classes according to above configuration
  60.  *  typically to expand the menu to reveal a submenu
  61.  *
  62.  */
  63. function vertnav_toggle(what){
  64.     /*
  65.      * get submenu within the expected <li> calling this function (aka var what)
  66.      */
  67.     var subnav = what.getElementsByTagName("ul");
  68.     for(i=0;i<subnav.length;i++) { /*--loop thru 'submenu', typically should only be 1 iteration--*/
  69.         if (vertnavst_subclick) { /* if a submenu item is being clicked */
  70.             vertnavst_subclick = false;
  71.             return false;
  72.         }
  73.         if (subnav[i].className == vertnavcfg_subshow_class)
  74.         { /* if visible, make hidden */
  75.             subnav[i].className = vertnavcfg_subhide_class;
  76.             subnav[i].parentNode.className = vertnavcfg_hashiddensub_class;
  77.         }
  78.         else
  79.         { /* if hidden, make visible */
  80.             subnav[i].className = vertnavcfg_subshow_class;
  81.             subnav[i].parentNode.className = vertnavcfg_hasvisiblesub_class;
  82.         }
  83.     } /*--end 'submenu' loop--*/
  84. } /*--vertnav_toggle()--*/
  85.  
  86. /**
  87.  * Function:
  88.  *  initNav
  89.  *  
  90.  * Purpose:
  91.  *  for use on page load (or similar) to
  92.  *  appropriately change the classes according to above configuration
  93.  *
  94.  */
  95. function vertnav_init() {
  96.     /*
  97.      * hide all the nested ULs ('submenus') the #leftnavlist div
  98.      */
  99.     var subnav = document.getElementById(vertnavcfg_navlistdiv_id).getElementsByTagName("ul");
  100.     for (i = 0; i < subnav.length ; i++) { /*--loop thru all the 'submenus'--*/
  101.         subnav[i].className = vertnavcfg_subhide_class; /* change class to the one indicated as 'hidden' */
  102.         subnav[i].parentNode.className = vertnavcfg_hashiddensub_class; /* change class to one indicating the parent li has a 'hidden' submenu */
  103.         subnav[i].parentNode.onclick = function(){vertnav_toggle(this);} /* register toggleNav() as the onclick event handler for the parent li */
  104.         var subnavli = subnav[i].parentNode.getElementsByTagName("li"); /* get submenu entries */
  105.         for(j=0;j<subnavli.length;j++) { /* loop thru submenu entries */
  106.             subnavli[j].onclick = function() {vertnavst_subclick = true;} /* register state handling */
  107.         }
  108.     } /*--end 'submenus' loop--*/
  109. } /*--vertnav_init()--*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement