"use strict"; /*Namespace ------------------------------------------------------- */ var aytias = aytias || {}; // Nodelist forEach polyfill if (window.NodeList && !NodeList.prototype.forEach) { NodeList.prototype.forEach = function (callback, thisArg) { thisArg = thisArg || window; for (var i = 0; i < this.length; i++) { callback.call(thisArg, this[i], i, this); } }; } /* Handle Accessiblity for Menu Items **-----------------------------------------------------*/ aytias.traverseMenu = { init: function () { let topNavigation = document.querySelector(".aytias-top-nav"); let primaryNavigation = document.getElementById("site-navigation"); // For top menu navigation if (topNavigation) { this.traverse(topNavigation); } // For primary menu navigation if (primaryNavigation) { this.traverse(primaryNavigation); } }, traverse: function (navigation) { let menu = navigation.getElementsByTagName("ul")[0]; if ("undefined" !== typeof menu) { if (!menu.classList.contains("nav-menu")) { menu.classList.add("nav-menu"); } // Get all the link elements within the menu. let links = menu.getElementsByTagName("a"); // Get all the link elements with children within the menu. let linksWithChildren = menu.querySelectorAll( ".menu-item-has-children > a, .page_item_has_children > a" ); // Toggle focus each time a menu link is focused or blurred. for (let link of links) { link.addEventListener("focus", this.toggleFocus, true); link.addEventListener("blur", this.toggleFocus, true); } // Toggle focus each time a menu link with children receive a touch event. for (let link of linksWithChildren) { link.addEventListener("touchstart", this.toggleFocus, false); } } }, toggleFocus: function (event) { if (event.type === "focus" || event.type === "blur") { let self = this; // Move up through the ancestors of the current link until we hit .nav-menu. while (!self.classList.contains("nav-menu")) { // On li elements toggle the class .focus. if ("li" === self.tagName.toLowerCase()) { self.classList.toggle("focus"); } self = self.parentNode; } } if (event.type === "touchstart") { let menuItem = this.parentNode; event.preventDefault(); for (let link of menuItem.parentNode.children) { if (menuItem !== link) { link.classList.remove("focus"); } } menuItem.classList.toggle("focus"); } }, }; /* Handle Focus for Dialog Accessiblity **-----------------------------------------------------*/ aytias.handleFocus = { init: function () { this.keepFocusInModal(); }, keepFocusInModal: function () { let searchModal = document.querySelector(".aytias-search-block"); let canvasModal = document.querySelector(".aytias-canvas-block"); document.addEventListener("keydown", function (event) { // Check for if tab key is pressed let KEYCODE_TAB = 9; let isTabPressed = event.key === "Tab" || event.keyCode === KEYCODE_TAB; if (!isTabPressed) { return; } let modal; if (document.body.classList.contains("aytias-search-canvas-open")) { modal = searchModal; } if (document.body.classList.contains("aytias-off-canvas-open")) { modal = canvasModal; } if (modal) { let focusableEls = modal.querySelectorAll( 'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="search"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])' ); let firstFocusableEl = focusableEls[0]; let lastFocusableEl = focusableEls[focusableEls.length - 1]; // if shift key pressed for shift + tab combination if (event.shiftKey) { if (document.activeElement === firstFocusableEl) { lastFocusableEl.focus(); // add focus for the last focusable element event.preventDefault(); } } else { // if tab key is pressed if (document.activeElement === lastFocusableEl) { // if focused has reached to last focusable element then focus first focusable element after pressing tab firstFocusableEl.focus(); // add focus for the first focusable element event.preventDefault(); } } } }); }, }; /* Preloader **-----------------------------------------------------*/ aytias.fadeOutPreloader = { init: function () { let preloader = document.querySelector(".aytias-loader-wrapper"); if (preloader) { let fadeOut = setInterval(function () { preloader.style.transition = "0.2s"; if (!preloader.style.opacity) { preloader.style.opacity = 1; } if (preloader.style.opacity > 0) { preloader.style.opacity -= 0.2; } else { preloader.style.display = "none"; clearInterval(fadeOut); } }, 100); } }, }; /* Scroll to top **-----------------------------------------------------*/ aytias.scrollToTop = { init: function () { let scrollToTopBtn = document.getElementById("aytias-scroll-to-top"); let rootElement = document.documentElement; if (scrollToTopBtn) { // Scroll to top on click this.goToTop(scrollToTopBtn, rootElement); // Render scroll to top button this.scrollToTopPosition(scrollToTopBtn, rootElement); } }, goToTop: function (scrollToTopBtn, rootElement) { scrollToTopBtn.addEventListener("click", function (elem) { elem.preventDefault(); rootElement.scrollTo({ top: 0, behavior: "smooth", }); }); }, scrollToTopPosition: function (scrollToTopBtn, rootElement) { window.addEventListener("scroll", function (event) { let scrollTotal = rootElement.scrollHeight - rootElement.clientHeight; // Show on certain window height if (rootElement.scrollTop / scrollTotal > 0.4) { scrollToTopBtn.classList.add("visible"); } else { scrollToTopBtn.classList.remove("visible"); } }); }, }; /* Sticky Menu **-----------------------------------------------------*/ aytias.stickyMenu = { init: function () { let navBar = document.querySelector(".aytias-header-plus-nav"); // Handle stikcy menu on scroll if (navBar && navBar.classList.contains("is-sticky")) { this.stickMenuOnScroll(navBar); } }, stickMenuOnScroll: function (navBar) { window.addEventListener("scroll", function (event) { let navOffset = navBar.offsetTop; let currentScrollPos = window.pageYOffset; if (navOffset > currentScrollPos || currentScrollPos === 0) { navBar.classList.remove("sticky-menu"); } else { navBar.classList.add("sticky-menu"); } }); }, }; /* Off Canvas **-----------------------------------------------------*/ aytias.offCanvasModal = { init: function () { let offCanvasBtn = document.getElementById("aytias-off-canvas-btn"); let closeOffCanvas = document.getElementById("aytias-off-canvas-close"); let overlayDiv = document.querySelector("#page.site"); if (offCanvasBtn) { let focusElement = document.querySelector( ".aytias-canvas-block #aytias-off-canvas-close" ); // Handle canvas modal when opened this.onOpen(offCanvasBtn, focusElement); // Handle canvas modal when closed this.onClose(offCanvasBtn, closeOffCanvas, focusElement); // When open, close if visitor clicks on the wrapping element of the modal. this.outsideModal(offCanvasBtn, overlayDiv, focusElement); // Close on escape key press. this.closeOnEscape(offCanvasBtn, focusElement); } }, onOpen: function (offCanvasBtn, focusElement) { offCanvasBtn.addEventListener("click", function (event) { event.preventDefault(); document.body.classList.add("aytias-off-canvas-open"); offCanvasBtn.setAttribute("aria-expanded", true); // Add focus after a timeout to take effect on hidden element to make the "all" transition work setTimeout(function () { focusElement.focus(); }, 500); }); }, onClose: function (offCanvasBtn, closeOffCanvas, focusElement) { closeOffCanvas.addEventListener("click", function (event) { event.preventDefault(); document.body.classList.remove("aytias-off-canvas-open"); offCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); offCanvasBtn.focus(); }); }, outsideModal: function (offCanvasBtn, overlayDiv, focusElement) { document.addEventListener("click", function (event) { if (document.body.classList.contains("aytias-off-canvas-open")) { if (event.target == overlayDiv) { document.body.classList.remove("aytias-off-canvas-open"); offCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); offCanvasBtn.focus(); console.log(offCanvasBtn); } } }); }, closeOnEscape: function (offCanvasBtn, focusElement) { document.addEventListener("keydown", function (event) { if (document.body.classList.contains("aytias-off-canvas-open")) { if (event.key === "Escape") { event.preventDefault(); document.body.classList.remove("aytias-off-canvas-open"); offCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); offCanvasBtn.focus(); } } }); }, }; /* Search Canvas **-----------------------------------------------------*/ aytias.searchCanvasModal = { init: function () { let searchCanvasBtn = document.getElementById( "aytias-search-canvas-btn" ); let closeSearchCanvas = document.getElementById( "aytias-search-canvas-close" ); let overlayDiv = document.querySelector("#page.site"); if (searchCanvasBtn) { let focusElement = document.querySelector( ".aytias-search-block .search-field" ); // Handle cover modals when they're opened this.onOpen(searchCanvasBtn, focusElement); // Handle cover modals when they're closed this.onClose(searchCanvasBtn, closeSearchCanvas, focusElement); // When open, close if visitor clicks on the outside the modal. this.outsideModal(searchCanvasBtn, overlayDiv, focusElement); // Close on escape key press. this.closeOnEscape(searchCanvasBtn, focusElement); } }, onOpen: function (searchCanvasBtn, focusElement) { searchCanvasBtn.addEventListener("click", function (event) { event.preventDefault(); document.body.classList.add("aytias-search-canvas-open"); searchCanvasBtn.setAttribute("aria-expanded", true); // Add focus after a timeout to take effect on hidden element to make the "all" transition work setTimeout(function () { focusElement.focus(); }, 500); }); }, onClose: function (searchCanvasBtn, closeSearchCanvas, focusElement) { closeSearchCanvas.addEventListener("click", function (event) { event.preventDefault(); document.body.classList.remove("aytias-search-canvas-open"); searchCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); searchCanvasBtn.focus(); }); }, outsideModal: function (searchCanvasBtn, overlayDiv, focusElement) { document.addEventListener("click", function (event) { if (document.body.classList.contains("aytias-search-canvas-open")) { if (event.target == overlayDiv) { document.body.classList.remove("aytias-search-canvas-open"); searchCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); searchCanvasBtn.focus(); } } }); }, closeOnEscape: function (searchCanvasBtn, focusElement) { document.addEventListener("keydown", function (event) { if (document.body.classList.contains("aytias-search-canvas-open")) { if (event.key === "Escape") { event.preventDefault(); document.body.classList.remove("aytias-search-canvas-open"); searchCanvasBtn.setAttribute("aria-expanded", false); focusElement.blur(); searchCanvasBtn.focus(); } } }); }, }; /* Background Image **-----------------------------------------------------*/ aytias.setBackgroundImage = { init: function () { let bgImageContainer = document.querySelectorAll(".aytias-bg-image"); if (bgImageContainer) { bgImageContainer.forEach(function (item) { let image = item.querySelector("img"); if (image) { let imageSrc = image.getAttribute("src"); if (imageSrc) { item.style.backgroundImage = "url(" + imageSrc + ")"; if (item.classList.contains("aytias-header-image")) { image.style.visibility = "hidden"; } else { image.style.display = "none"; } } } }); } }, }; /* Progress Bar **-----------------------------------------------------*/ aytias.progressBar = { init: function () { let progressBarDiv = document.getElementById("aytias-progress-bar"); if (progressBarDiv) { let body = document.body; let rootElement = document.documentElement; window.addEventListener("scroll", function (event) { let winScroll = body.scrollTop || rootElement.scrollTop; let height = rootElement.scrollHeight - rootElement.clientHeight; let scrolled = (winScroll / height) * 100; progressBarDiv.style.width = scrolled + "%"; }); } }, }; /* Load functions at proper events *--------------------------------------------------*/ /** * Is the DOM ready? * * This implementation is coming from https://gomakethings.com/a-native-javascript-equivalent-of-jquerys-ready-method/ * * @param {Function} fn Callback function to run. */ function aytiasDomReady(fn) { if (typeof fn !== "function") { return; } if ( document.readyState === "interactive" || document.readyState === "complete" ) { return fn(); } document.addEventListener("DOMContentLoaded", fn, false); } aytiasDomReady(function () { aytias.offCanvasModal.init(); aytias.searchCanvasModal.init(); aytias.stickyMenu.init(); aytias.scrollToTop.init(); aytias.handleFocus.init(); aytias.traverseMenu.init(); aytias.setBackgroundImage.init(); aytias.progressBar.init(); }); window.addEventListener("load", function (event) { aytias.fadeOutPreloader.init(); }); // For jQuery based functionalities !(function ($) { $(document).ready(function () { // Toggle sub menu items $(".sub-menu-toggle").click(function () { $(this).toggleClass("active"); $(this).attr("aria-expanded", function (i, attr) { return attr == "true" ? "false" : "true"; }); var currentClass = $(this).attr("data-toggle-target"); $(currentClass).toggleClass("active").toggle(350); }); }); })(jQuery);