Skip to content

Instantly share code, notes, and snippets.

@mattidupre
Created December 6, 2012 20:17
Show Gist options
  • Save mattidupre/4227955 to your computer and use it in GitHub Desktop.
Save mattidupre/4227955 to your computer and use it in GitHub Desktop.
SharePoint Nested Menu Script
var initMenu = function () {
// Script compares document url with each menu url such that only those submenus that are children of the current page are toggled visible.
// Compares both subfolders (Pages/folder/item.aspx) and subsites (site/Pages/folder/item.aspx) with url.
// Hide all second-level menus.
$('#DhssZenNav li.static ul').attr('style', 'display: none;');
// Define regular expression.
var myRegExp = new RegExp('(?:/|.*?//?.*?/)(?:pub/|home/)*((?:.*?/)*).*?.aspx');
// note that ?: indicates an ignored block, (not passed to result array).
// Breakdown:
// (?:/|.*?//?.*?/) => [ignored] / [or] http://, localhost/, etc
// .*?/ => [ignored] alaska.gov/, www.alaska.gov/
// (?:pub/|home/)* => [ignored] pub/home/, pub/, home/
// (?:.*?/)*) => [$1] division/Pages/subfolder/anothersubfolder/
// .*?.aspx => [ignored] default.aspx, test.aspx
// Extract the folder uri from page URL and apply regular expression.
var pageUri = myRegExp.exec(document.URL);
pageUri = (pageUri && pageUri.length > 1) ? pageUri[1] : null;
// If regular expression returns nothing or the site root, exit.
if (!pageUri || pageUri === 'Pages/') return;
// Iterate through all links in the menu.
$('#DhssZenNav li.static > a').each(function (key, el) {
// Extract folder uri from each link.
var result = myRegExp.exec($(el).attr('href'));
var linkUri = (result && result.length > 1) ? result[1] : null;
if (!linkUri) return true; // Continue
// Trim page uri to the length of the link uri, compare, and add the appropriate class on match.
if (linkUri && pageUri.substr(0, linkUri.length) == linkUri) {
$(el).parents('li').addClass('selected');
};
});
// Add "selected" class to selected link's associated menu and show.
$('#DhssZenNav li.static.selected > ul').addClass('selected').attr('style', '');
// Add "active" class to selected link's parent menus and show.
$('#DhssZenNav li.static.selected').parents('ul').addClass('active').attr('style', '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment