( function () { var container, menu, links, i, len; container = document.getelementbyid('site-navigation'); if (!container) { return; } menu = container.getelementsbytagname('ul')[0]; menu.setattribute('aria-expanded', 'false'); if (-1 === menu.classname.indexof('nav-menu')) { menu.classname += ' nav-menu'; } // get all the link elements within the menu. links = menu.getelementsbytagname('a'); // each time a menu link is focused or blurred, toggle focus. for (i = 0, len = links.length; i < len; i++) { links[i].addeventlistener('focus', togglefocus, true); links[i].addeventlistener('blur', togglefocus, true); } /** * sets or removes .focus class on an element. */ function togglefocus() { var self = this; // move up through the ancestors of the current link until we hit .nav-menu. while (-1 === self.classname.indexof('nav-menu')) { // on li elements toggle the class .focus. if ('li' === self.tagname.tolowercase()) { if (-1 !== self.classname.indexof('focus')) { self.classname = self.classname.replace(' focus', ''); } else { self.classname += ' focus'; } } self = self.parentelement; } } /** * toggles `focus` class to allow submenu access on tablets. */ ( function (container) { var touchstartfn, i, parentlink = container.queryselectorall('.menu-item-has-children > a, .page_item_has_children > a'); if ('ontouchstart' in window) { touchstartfn = function (e) { var menuitem = this.parentnode, i; if (!menuitem.classlist.contains('focus')) { e.preventdefault(); for (i = 0; i < menuitem.parentnode.children.length; ++i) { if (menuitem === menuitem.parentnode.children[i]) { continue; } menuitem.parentnode.children[i].classlist.remove('focus'); } menuitem.classlist.add('focus'); } else { menuitem.classlist.remove('focus'); } }; for (i = 0; i < parentlink.length; ++i) { parentlink[i].addeventlistener('touchstart', touchstartfn, false); } } }(container) ); }() ); /** * fixes menu out of viewport */ ( function () { var i; var container = document.getelementbyid('site-navigation'), elwithchildren = document.queryselectorall('.tg-primary-menu li.menu-item-has-children, .tg-primary-menu li.page_item_has_children'), elcount = elwithchildren.length; function elementinviewport(el) { var top = el.offsettop, left = el.offsetleft, width = el.offsetwidth, height = el.offsetheight; while (el.offsetparent) { el = el.offsetparent; top += el.offsettop; left += el.offsetleft; } return ( top >= window.pageyoffset && left >= window.pagexoffset && ( top + height ) <= ( window.pageyoffset + window.innerheight ) && ( left + width ) <= ( window.pagexoffset + window.innerwidth ) ); } // end: function elementinviewport( el ) { // loop through li having sub menu. for (i = 0; i < elcount; i++) { // on mouse enter. elwithchildren[i].addeventlistener('mouseenter', function (ev) { var li = ev.currenttarget; if (li) { var submenu = li.queryselectorall('.sub-menu, .children')[0]; if (submenu) { var w = window, d = document, e = d.documentelement, g = d.getelementsbytagname('body')[0], x = w.innerwidth || e.clientwidth || g.clientwidth, y = w.innerheight || e.clientheight || g.clientheight; if (!elementinviewport(submenu)) { submenu.classlist.add('tg-edge'); } } } }, false); // on mouse leave. elwithchildren[i].addeventlistener('mouseleave', function (ev) { var li = ev.currenttarget; if (li) { var sub = li.queryselectorall('.sub-menu, .children')[0]; sub.classlist.remove('tg-edge'); if (sub.classlist.contains('tg-edge')) { sub.classlist.remove('tg-edge'); } } }, false); } // end: for ( i in elwithchildren ) { } )(); /** * keep menu items on one line. */ ( function () { // get required elements. var mainwrapper = document.queryselector('.tg-site-header .tg-header-container'), branding = document.queryselector('.tg-site-header .site-branding'), navigation = document.getelementbyid('site-navigation'), mainwidth = mainwrapper.offsetwidth, brandwidth = branding.offsetwidth, navwidth = navigation.offsetwidth, isextra = (brandwidth + navwidth) > mainwidth, more = navigation.getelementsbyclassname('tg-menu-extras-wrap')[0]; // return if no excess menu items. if (!navigation.classlist.contains('tg-extra-menus')) { return; } function dimension(el) { var elwidth; if (document.all) {// ie. elwidth = el.currentstyle.width + parseint(el.currentstyle.marginleft, 10) + parseint(el.currentstyle.marginright, 10) + parseint(el.currentstyle.paddingleft, 10) + parseint(el.currentstyle.paddingright, 10); } else { elwidth = parseint(document.defaultview.getcomputedstyle(el, '').getpropertyvalue('width')) + parseint(document.defaultview.getcomputedstyle(el, '').getpropertyvalue('margin-left')) + parseint(document.defaultview.getcomputedstyle(el, '').getpropertyvalue('padding-left')) + parseint(document.defaultview.getcomputedstyle(el, '').getpropertyvalue('padding-right')) + parseint(document.defaultview.getcomputedstyle(el, '').getpropertyvalue('margin-right')); } return elwidth; } // if menu excesses. if (!isextra) { more.parentnode.removechild(more); } else { var widthtobe, search, cart, button, searchwidth, cartwidth, buttonwidth, morewidth; widthtobe = mainwidth - brandwidth; search = navigation.getelementsbyclassname('tg-menu-item-search')[0]; cart = navigation.getelementsbyclassname('tg-menu-item-cart')[0]; button = navigation.getelementsbyclassname('tg-header-button-wrap')[0]; searchwidth = search ? dimension(search) : 0; cartwidth = cart ? dimension(cart) : 0; buttonwidth = button ? dimension(button) : 0; morewidth = more ? dimension(more) : 0; newnavwidth = widthtobe - (searchwidth + cartwidth + buttonwidth + morewidth); navigation.style.visibility = 'none'; navigation.style.width = newnavwidth + 'px'; // returns first children of a node. function getchildnodes(node) { var children = new array(); for (var child in node.childnodes) { if (1 === node.childnodes[child].nodetype) { children.push(node.childnodes[child]); } } return children; } var navul = navigation.getelementsbyclassname('nav-menu')[0], navli = getchildnodes(navul); // get lis. function offset(el) { var rect = el.getboundingclientrect(), scrollleft = window.pagexoffset || document.documentelement.scrollleft, scrolltop = window.pageyoffset || document.documentelement.scrolltop; return {top: rect.top + scrolltop, left: rect.left + scrollleft} } var extrali = []; for (var licount = 0; licount < navli.length; licount++) { var initialpos, li, postop; li = navli[licount]; postop = offset(li).top; if (0 === licount) { initialpos = postop; } if (postop > initialpos) { if (!li.classlist.contains('tg-menu-item-search') && !li.classlist.contains('tg-menu-item-cart') && !li.classlist.contains('tg-header-button-wrap') && !li.classlist.contains('tg-menu-extras-wrap')) { extrali.push(li); } } } var newnavwidth = newnavwidth + (searchwidth + cartwidth + buttonwidth + morewidth) - 2, extrawrap = document.getelementbyid('tg-menu-extras'); navigation.style.width = newnavwidth + 'px'; if (null !== extrawrap) { extrali.foreach(function (item, index, arr) { extrawrap.appendchild(item); }); } } }() );