// Custom JS Goes HERE $(document).ready(function (){ /** * Create a tree out of a flat path recursively. * @param {[type]} * @param {[type]} * @param {[type]} * @return {[type]} */ // Now place each item in the tree function treeize(pages) { var t = { leaves: [], branches: [] } for (i in pages) { var urlArr = pages[i].url.split('/'); var currLevel = urlArr.shift(); var pointer = t; // For some reason the GH Pages jekyll detects style.css as a page. if (urlArr[0] === "assets") continue // Now loop until we're deep enough while (urlArr.length > 1) { var key = urlArr[0].length == 0 ? "EMPTY" : urlArr[0]; key = key.replace(/_/g," "); var newDir = null; var thebranch = pointer.branches.filter(function(br){ return br.key == key }) if (thebranch.length > 0){ newDir = thebranch[0]; } else { newDir = { key: key, leaves: [], branches: [] } pointer.branches.push(newDir); } pointer = newDir; currLevel = urlArr.shift(); } if (urlArr[0] == "index.html" || urlArr[0] == ""){ pointer.title = pages[i].title; pointer.index = pages[i]; } else{ pointer.title = pointer.key; pointer.leaves.push(pages[i]); } } traverseandsort(t) return t } /** * recursively sort a tree by weight then by alphabet * @param {[type]} * @return {[type]} */ function traverseandsort(t) { // Now sort branches at this level t.branches.sort(function(a,b) { a.weight = a.index && a.index.weight ? parseInt(a.index.weight) : 999; b.weight = b.index && b.index.weight ? parseInt(b.index.weight) : 999; if (a.weight == b.weight) { return a.title < b.title ? -1 : (a.title > b.title ? 1 : 0); } else { return a.weight < b.weight ? -1 : (a.weight > b.weight ? 1 : 0); } return true; }); // Sort the leaves by weight and then by title t.leaves.sort(function(a, b) { a.weight = parseInt(a.weight) || 999; b.weight = parseInt(b.weight) || 999; if (a.weight == b.weight) { return a.title < b.title ? -1 : (a.title > b.title ? 1 : 0); } else { return a.weight < b.weight ? -1 : (a.weight > b.weight ? 1 : 0); } }); // Now go find more branches to sort their leaves for (br in t.branches) { traverseandsort(t.branches[br]) } } /** * Turn a tree structure from SiteSettings.topmenu into a foundation topbar * @param {[type]} * @return {[type]} */ function topbarize() { // The Mobile menu $topbarContainer = $('
') var tree = SiteSettings.topmenu // Otherwise we get a proper menu $mobilediv = $(''); $mobilediv.append($('')) $mobilediv.append($(' ')) $topbarContainer.append($mobilediv) var $topbar = $(''); var $topbarleft = $(' ') var $topbarright = $(''); $topbar.append($topbarleft); function menutraverse(t, first) { if (!first) first = false; // First time round var $mUL = $(' '); if (first){ var $mUL = $(' '); } function urlize(item){ var newurl = "#"; var target = ""; var title = item.title; if (item.url){ // Is the URL absolute or relative? if (item.url.indexOf("http") != 0) { newurl = NAVHome + '/' + item.url; } else{ newurl = item.url; target = 'target="_blank"'; } } return $('' + title + ''); } // Loop over the immediate children for (cind in t) { // Now go find more branches to sort their leaves m656 if (t[cind].children && t[cind].children.length > 0){ var $li = $(''); $li.append(urlize(t[cind])) $li.append(menutraverse(t[cind].children)); $mUL.append($li); } else { var $mLi = $(''); $mLi.append(urlize(t[cind])); $mUL.append($mLi); } } return $mUL; } // Only build the menu if it's specified in the config.yaml file if (tree){ $topbarright.append(menutraverse(tree, true)); $topbar.append($topbarright); } $topbarContainer.append($topbar); return $topbarContainer; } // Extract the url parameter from the name you want function getUrlParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^]*)'); var results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); }; /** * Turn a tree structure from treeize into a foundation sidebar accordion * @param {[type]} * @return {[type]} */ function accordionize(t, $mUL) { if (!SiteSettings.sideMenu) return // The first time we have to build the ul if (!$mUL) { $mUL = $(' '); // If we've elected to have a home item then use it try { if (SiteSettings.sideMenu.homeItem === true){ $li = $('