Michael Davis | dfad5d5 | 2017-07-20 14:53:46 -0500 | [diff] [blame^] | 1 | (function() { |
| 2 | // var user = 'root'; |
| 3 | // var password = '0penBmc'; |
| 4 | // var ip = 'https://9.3.164.177'; |
| 5 | // |
| 6 | // var login = { |
| 7 | // "type": "POST", |
| 8 | // "url": ip + "/login", |
| 9 | // "dataType": "json", |
| 10 | // "async": false, |
| 11 | // "headers": { |
| 12 | // 'Accept': 'application/json', |
| 13 | // 'Content-Type': 'application/json' |
| 14 | // }, |
| 15 | // "xhrFields": { |
| 16 | // withCredentials: true |
| 17 | // }, |
| 18 | // "data": JSON.stringify({"data": [user, password]}), |
| 19 | // "success": function(response){ |
| 20 | // console.log(response); |
| 21 | // }, |
| 22 | // "error": function(xhr, textStatus, errorThrown){ |
| 23 | // console.log("not a successful request!") |
| 24 | // console.log(xhr, textStatus, errorThrown) |
| 25 | // } |
| 26 | // }; |
| 27 | // |
| 28 | // $.ajax(login); |
| 29 | |
| 30 | // Init functions |
| 31 | header(); |
| 32 | nav(); |
| 33 | |
| 34 | // Load logo |
| 35 | function loadLogo(){ |
| 36 | $('.logo__wrapper').load('logo.html', function(){ |
| 37 | |
| 38 | // Grab logo if has ID or not |
| 39 | var logoID = document.getElementById("header__logo"); |
| 40 | var logo = document.querySelectorAll("img, svg"); |
| 41 | |
| 42 | // Has ID - call header height |
| 43 | if(logoID && logoID !== null) { |
| 44 | getHeaderHeight(); |
| 45 | |
| 46 | // If logo exists but no ID - call header height |
| 47 | } else if (logo !== null && logo.length == 1){ |
| 48 | $('img, svg').on('load', function(){ |
| 49 | getHeaderHeight(); |
| 50 | }) |
| 51 | |
| 52 | // If no logo at all - call header height |
| 53 | } else { getHeaderHeight(); } |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | // Get header height |
| 58 | function getHeaderHeight() { |
| 59 | // Get header height after logo is loaded |
| 60 | var header = document.getElementById("header__wrapper"); |
| 61 | var headerHeight = header.offsetHeight; |
| 62 | |
| 63 | // Add body padding to compensate for fixed header |
| 64 | document.body.style.paddingTop = headerHeight + 'px'; |
| 65 | |
| 66 | nav(headerHeight); |
| 67 | } |
| 68 | // Include header |
| 69 | function header() { |
| 70 | $('#header__wrapper').load('header.html', function () { |
| 71 | |
| 72 | // include logo into header |
| 73 | loadLogo(); |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | // load navigation - pass in header height |
| 78 | function nav(height) { |
| 79 | $('#navigation').load('navigation.html', function (headerHeight) { |
| 80 | |
| 81 | var nav = document.getElementById("nav__top-level"); |
| 82 | var navWidth = nav.offsetWidth; // Get navigation width |
| 83 | var subnav = document.getElementsByClassName("nav__second-level"); |
| 84 | var navBtn = document.querySelectorAll('#nav__top-level button'); |
| 85 | |
| 86 | // Add body padding to compensate for fixed nav |
| 87 | document.body.style.paddingLeft = navWidth + 'px'; |
| 88 | |
| 89 | // Bump down nav to compensate for fixed header |
| 90 | nav.style.top = height + 'px'; |
| 91 | |
| 92 | // Bump second level nav down for fixed header |
| 93 | for (var i = 0; i < subnav.length; i++) { |
| 94 | subnav[i].style.top = height + 'px'; |
| 95 | } |
| 96 | |
| 97 | // Loop over nav buttons |
| 98 | for (var i = 0, len = navBtn.length; i < len; i++) { |
| 99 | |
| 100 | // Click event for each nav button |
| 101 | navBtn[i].addEventListener('click', function(){ |
| 102 | var parent = $(this).parents("#navigation"); |
| 103 | var btnId = $(this).attr("class").match(/btn[\w-]*\b/); |
| 104 | var subnavClass = $('.nav__second-level.' + btnId); |
| 105 | |
| 106 | // Remove opened class from buttons |
| 107 | parent.find('.opened').removeClass('opened'); |
| 108 | |
| 109 | // Add opened class to clicked button |
| 110 | this.classList.add("opened"); |
| 111 | |
| 112 | // Close all sub panels and remove opened class |
| 113 | parent.find('.nav__second-level').css("left", '-' + navWidth + "px").removeClass('opened'); |
| 114 | |
| 115 | // Open sub panels that matches clicked button and add opened class |
| 116 | parent.find(subnavClass).addClass('opened').css("left", navWidth); |
| 117 | }) |
| 118 | |
| 119 | } |
| 120 | |
| 121 | // Temp solution to close subnav - TODO: better way to close |
| 122 | for (var i = 0, len = subnav.length; i < len; i++) { |
| 123 | subnav[i].addEventListener('click', function(){ |
| 124 | this.classList.remove("opened"); |
| 125 | this.style.left = "-" + navWidth + "px"; |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | }()); |