Last active
March 5, 2024 16:34
-
-
Save mbstacy/2f1eb65ba1c2ffab14097531b846b076 to your computer and use it in GitHub Desktop.
Apigee Portal Anchor Links and Overview Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
// Add to Apigee Portal Assets | |
// check if Header is in the Nav bar | |
function navParent(elem) { | |
var nav=false; | |
while(elem.parentNode && elem.parentNode.nodeName.toLowerCase() != 'body') { | |
elem = elem.parentNode; | |
if(elem.tagName=="NAV"){nav=true;} | |
} | |
return nav; | |
} | |
// create ids on headings | |
function headingids(){ | |
document.querySelectorAll("h2,h3,h4,h5,h6").forEach(heading => { | |
const text = heading.innerText.replace(/[^a-zA-Z ]/g, "").replace(/ /g, "-").replace(/^-/, ""); | |
heading.id = text; | |
if (!navParent(heading)){ | |
if (!heading.classList.contains("no-link")){ | |
if (!heading.classList.contains("anchor-links")){ | |
if (location.pathname.substring(location.pathname.lastIndexOf('/')+1)==='overview'){ | |
heading.classList.add("anchor-links") | |
heading.insertAdjacentHTML('beforeend', '<a aria-label="Link to this heading" class="docs-markdown-a" title="Link to this heading" aria-describedby="font-icons-with-css" href="' + location.pathname + '#' + text +'"><span class="mat-icon notranslate material-icons mat-icon-no-color" role="img" aria-hidden="true" style="margin-left:5px;margin-bottom:8px;vertical-align:middle;">link</span></a>'); | |
} | |
} | |
}; | |
} | |
}); | |
} | |
// this function will go to the hash when the page loads | |
// why doesn't it do this by default? because angular has routing rules | |
function gotohash(){ | |
if(window.location.hash !== ""){ | |
try { | |
document.querySelector(location.hash).scrollIntoView(); | |
termhash=".menu-" + location.hash.replace("#",'') | |
if(document.querySelector(termhash)){ | |
if(document.querySelector(".mat-list-item-active")!== null){ | |
document.querySelector(".mat-list-item-active").classList.remove("mat-list-item-active"); | |
} | |
document.querySelector(termhash).classList.add("mat-list-item-active"); | |
document.querySelector(termhash).focus(); | |
} | |
} | |
catch(err) { | |
console.log(err.message); | |
} | |
}else{ | |
if (location.pathname.substring(location.pathname.lastIndexOf('/')+1)==='overview'){ | |
document.querySelectorAll(".mat-list-item-active").forEach((el) => { | |
el.classList.remove("mat-list-item-active"); | |
}); | |
document.querySelector(".mat-list-item").classList.add("mat-list-item-active"); | |
} | |
} | |
} | |
// this function will force a scroll to the URI fragment | |
// Note: there is a parallel (not in the bubble order of this) click event happening | |
// presumably triggered by angular routing rules that we have no visibility on -- | |
// the best way I could find to get around that was to wrap the scroll in a timeout | |
function hashchangechange(){ | |
window.onhashchange = function(event) { | |
window.setTimeout(() => { | |
element = document.querySelector(location.hash); | |
if(element !== null){ | |
element.scrollIntoView(); | |
termhash=".menu-" + location.hash.replace("#",''); | |
if(document.querySelector(termhash)){ | |
if(document.querySelector(".mat-list-item-active")!== null){ | |
document.querySelectorAll(".mat-list-item-active").forEach((el) => { | |
el.classList.remove("mat-list-item-active"); | |
}); | |
} | |
document.querySelector(termhash).classList.add("mat-list-item-active"); | |
} | |
}else{ | |
if(document.querySelector(".mat-list-item-active")!== null){ | |
document.querySelectorAll(".mat-list-item-active").forEach((el) => { | |
el.classList.remove("mat-list-item-active"); | |
}); | |
} | |
document.querySelector(".mat-list-item").classList.add("mat-list-item-active") | |
} | |
}, 0); | |
}; | |
} | |
// this function will push in some navigation links on the left navigation based on | |
// the class ".header-nav-menu" | |
function navMenuFromClass(){ | |
var parent_nav = document.querySelector("mat-nav-list"); | |
setTimeout(function(){ | |
overvw=document.querySelector(".mat-list-item"); | |
overvw.setAttribute("onclick","gotohash();return true;"); | |
}, 300); | |
if (document.querySelector("mat-nav-list").children.length <=1){ | |
document.querySelectorAll(".header-nav-menu").forEach((el) => { | |
if(el.id !== null){ | |
// we just copy the existing element | |
var element = document.querySelector("nav-entry").cloneNode(true); | |
if(element !== null && parent_nav !== null){ | |
const href = window.location.pathname + "#" + el.id; | |
const a = element.querySelector("a"); | |
a.classList.remove("mat-focus-indicator"); | |
a.classList.remove("mat-list-item-active"); | |
a.classList.add("menu-" + el.id); | |
a.href = href; | |
a.setAttribute("onclick","goOverview();return true;" ) | |
element.querySelector(".nav-entry-text").innerText = el.innerText.replace(/link$/,'') | |
parent_nav.appendChild(element); | |
} | |
} | |
}); | |
} | |
} | |
// goOverview prevents page reload. Click known route('overview') and then perform anchor link. | |
function goOverview(){ | |
document.querySelector(".mat-list-item").click(); | |
} | |
headingids(); | |
hashchangechange(); | |
navMenuFromClass(); | |
gotohash(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
// Add to Apigee Portal settings custom scripts | |
<script> | |
window.portal = {}; | |
window.portal.pageEventListeners = { | |
onLoad: (path) => { | |
// `if` or `else if` depending on your configuration | |
if(location.pathname.includes('docs')){ | |
var script = document.createElement("script"); | |
script.src = "/files/anchorLinks.js"; | |
document.body.appendChild(script); | |
} | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment