Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 1 | (function () { |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 2 | |
| 3 | // Init functions |
| 4 | header(); |
| 5 | nav(); |
| 6 | |
| 7 | // Load logo |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 8 | function loadLogo() { |
| 9 | $('.logo__wrapper').load('logo.html', function () { |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 10 | |
| 11 | // Grab logo if has ID or not |
| 12 | var logoID = document.getElementById("header__logo"); |
| 13 | var logo = document.querySelectorAll("img, svg"); |
| 14 | |
| 15 | // Has ID - call header height |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 16 | if (logoID && logoID !== null) { |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 17 | getHeaderHeight(); |
| 18 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 19 | // If logo exists but no ID - call header height |
| 20 | } else if (logo !== null && logo.length == 1) { |
| 21 | $('img, svg').on('load', function () { |
| 22 | getHeaderHeight(); |
| 23 | }); |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 24 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 25 | // If no logo at all - call header height |
| 26 | } else { |
| 27 | getHeaderHeight(); |
| 28 | } |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 29 | }); |
| 30 | } |
| 31 | |
| 32 | // Get header height |
| 33 | function getHeaderHeight() { |
| 34 | // Get header height after logo is loaded |
| 35 | var header = document.getElementById("header__wrapper"); |
| 36 | var headerHeight = header.offsetHeight; |
| 37 | |
| 38 | // Add body padding to compensate for fixed header |
| 39 | document.body.style.paddingTop = headerHeight + 'px'; |
| 40 | |
| 41 | nav(headerHeight); |
| 42 | } |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 43 | |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 44 | // Include header |
| 45 | function header() { |
| 46 | $('#header__wrapper').load('header.html', function () { |
| 47 | |
| 48 | // include logo into header |
| 49 | loadLogo(); |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | // load navigation - pass in header height |
| 54 | function nav(height) { |
| 55 | $('#navigation').load('navigation.html', function (headerHeight) { |
| 56 | |
| 57 | var nav = document.getElementById("nav__top-level"); |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 58 | var subnav = document.getElementsByClassName("nav__second-level"); |
| 59 | var navBtn = document.querySelectorAll('#nav__top-level button'); |
| 60 | |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 61 | |
| 62 | // Bump down nav to compensate for fixed header |
| 63 | nav.style.top = height + 'px'; |
| 64 | |
| 65 | // Bump second level nav down for fixed header |
| 66 | for (var i = 0; i < subnav.length; i++) { |
| 67 | subnav[i].style.top = height + 'px'; |
| 68 | } |
| 69 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 70 | //Loop over nav buttons |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 71 | for (var i = 0, len = navBtn.length; i < len; i++) { |
| 72 | |
| 73 | // Click event for each nav button |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 74 | navBtn[i].addEventListener('click', function () { |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 75 | var parent = $(this).parents("#navigation"); |
| 76 | var btnId = $(this).attr("class").match(/btn[\w-]*\b/); |
| 77 | var subnavClass = $('.nav__second-level.' + btnId); |
| 78 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 79 | //Remove opened class from buttons |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 80 | parent.find('.opened').removeClass('opened'); |
| 81 | |
| 82 | // Add opened class to clicked button |
| 83 | this.classList.add("opened"); |
| 84 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 85 | //Close all sub panels and remove opened class |
| 86 | parent.find('.nav__second-level').css("display", "none").removeClass('opened'); |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 87 | |
Michael Davis | 1947575 | 2017-02-23 18:30:23 -0600 | [diff] [blame^] | 88 | //Open sub panels that matches clicked button and add opened class |
| 89 | parent.find(subnavClass).css("display", "block").addClass('opened'); |
| 90 | |
| 91 | // var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/")+1); |
| 92 | // |
| 93 | // $('.nav__second-level a').each(function(){ |
| 94 | // console.log(pgurl); |
| 95 | // if($(this).attr("href") == pgurl || $(this).attr("href") == '' ) |
| 96 | // $('.nav__second-level li').addClass("active"); |
| 97 | // }) |
| 98 | }); |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 99 | |
| 100 | } |
Michael Davis | cb8bb19 | 2017-02-09 15:45:13 -0600 | [diff] [blame] | 101 | }); |
| 102 | } |
| 103 | |
| 104 | }()); |