Skip to content

Instantly share code, notes, and snippets.

@bcls
Last active September 27, 2017 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcls/03fd73ec5be8869d45616ba8944877b1 to your computer and use it in GitHub Desktop.
Save bcls/03fd73ec5be8869d45616ba8944877b1 to your computer and use it in GitHub Desktop.
Add items to Drupal left (in-page) menu #drupal #javascript
// create navigation for page sections
function createInPageNavMenu() {
var sideNavList = document.querySelector('.bc-ipnav-block ul'),
// get reference to Related Topics item
lastLI = sideNavList.lastChild,
i,
max = navLabel.length,
aEl,
liEl,
txt;
for (i = 0; i < max; i++) {
liEl = document.createElement('li');
aEl = document.createElement('a');
aEl.setAttribute('href', '#' + navLabel[i].link);
txt = document.createTextNode(navLabel[i].text);
aEl.appendChild(txt);
liEl.appendChild(aEl);
sideNavList.insertBefore(liEl, lastLI);
}
}
// create array of new nav objects
function createInPageNav() {
var navObj = {},
h2s = document.querySelectorAll('section.bcls-section h2'),
i, index,
iMax = h2s.length;
// set initial visibilities
for (i = 0; i < iMax; i++) {
index = i;
// in this case there's already one item at the top of the menu
// so skipping that
if (index > 0) {
$this = h2s[i];
navObj = {};
navObj.link = $this.getAttribute("id");
navObj.text = $this.innerHTML;
navLabel.push(navObj);
}
}
// bclslog('navLabel', navLabel);
// only create the nav widget if there is more than one item
if (navLabel.length > 1) {
// create in-page nav menu
createInPageNavMenu();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment