(function ($) { // Use this variable to set up the common and page specific functions. If you // rename this variable, you will also need to rename the namespace below. var bstarter = { // All pages common: { init: function () { this.expandNavbarOnClickSearch(); this.add_pretty_photo_gallery();//add pretty photo gallery this.scrollToElement(); this.scrollDown(); this.scrollToTop(); this.mobileMenuToggle(); this.infiniteScroll(); }, expandNavbarOnClickSearch: function () { // TO DO: work on search animation var form_container = $('.js-search-box-to-expand'); $('.js-expand-search').on('click', function (e) { form_container.removeClass('u-hidden'); $('.rocksite-o-search-navbar .rocksite-c-search-field__input').focus(); e.preventDefault(); }); $('.js-close-search').on('click', function (e) { form_container.addClass('u-hidden'); $('.js-expand-search').focus(); e.preventDefault(); }); }, infiniteScroll: function () { // init Infinite Scroll $('.js-infinite-scroll').infiniteScroll({ path: '.next.page-numbers', append: '.rocksite-c-article-box', status: '.scroller-status', hideNav: '.nav-links', }); }, add_pretty_photo_gallery: function () { if ($("a[rel^='rocksite-resize-photo']").length > 0) { $("a[rel^='rocksite-resize-photo']").prettyPhoto(); } }, mobileMenuToggle: function () { // expand mobile menu var $mobileMenu = $('.js-mobile-menu'); var $htmlDom = $('html'); $('.js-toggle-mobile').on('click', function (e) { $buttonToglle = $(this); $mobileMenu.toggleClass('---active'); $mobileMenu.find('li:first-child a').focus(); $htmlDom.toggleClass('u-no-scrool-html'); $buttonToglle.toggleClass('-close-btn-wrapper'); $buttonToglle.find('.rocksite-m-bars').toggleClass('-close-btn'); e.preventDefault(); }); // expand submenu $mobileMenu.find('.-has-children > a, .-js-menu-toggle').on('click', function (e) { if ($(this).attr('href') == '#') { e.preventDefault(); } $(this).parent().toggleClass('--open'); }); }, scrollToElement: function () { //$('body').scrollspy({target: '#rocksite-spy-scroll-nav', offset: 50}); $(".js-scroll-to-menu a").on('click', function () { var $container = $(this); var $parent_container = $container.parents('.js-scroll-to-menu'); var containerTo = $container.attr('href'); var offset = 1 * $parent_container.data('scroll-offset'); $('html, body').animate({ scrollTop: $(containerTo).offset().top - offset }, 2000); }); }, scrollDown: function () { $('.js-scroll-down').on('click', function (e) { var $btn = $(this); var containerTo = $btn.attr('href'); e.preventDefault(); $('html, body').animate({ scrollTop: $(containerTo).offset().top }, 500, 'linear'); }); }, scrollToTop: function () { $btnTop = $('#scroll-top-top'); //Check to see if the window is top if not then display button $(window).scroll(function () { if ($(this).scrollTop() > 100) { $btnTop.addClass('slideInUp'); } else { $btnTop.removeClass('slideInUp'); } }); //Click event to scroll to top $btnTop.click(function () { $('html, body').animate({scrollTop: 0}, 800); return false; }); }, }, // Home home: { init: function () { } }, }; // The routing fires all common scripts, followed by the page specific scripts. // Add additional events for more control over timing e.g. a finalize event var UTIL = { fire: function (func, funcname, args) { var namespace = bstarter; funcname = (funcname === undefined) ? 'init' : funcname; if (func !== '' && namespace[func] && typeof namespace[func][funcname] === 'function') { namespace[func][funcname](args); } }, loadEvents: function () { UTIL.fire('common'); $.each(document.body.className.replace(/-/g, '_').split(/\s+/), function (i, classnm) { UTIL.fire(classnm); }); } }; $(document).ready(UTIL.loadEvents); $(window).load(function () { smartlib_preloader(); }); function smartlib_preloader() { imageSources = [] $('img').each(function () { var sources = $(this).attr('src'); imageSources.push(sources); }); if ($(imageSources).load()) { $('.rocksite-m-preloader').fadeOut('slow'); } } /*Fix double click Ipad*/ $('body').on('touchstart', '*', function () { //listen to touch var jQueryElement = $(this); var element = jQueryElement.get(0); // find tapped HTML element if (!element.click) { var eventObj = document.createEvent('MouseEvents'); eventObj.initEvent('click', true, true); element.dispatchEvent(eventObj); } }); })(jQuery); // Fully reference jQuery after this point. /** * * Trap focus functions * Code for trapping focus inside modal * https://gist.github.com/myogeshchavan97/d50d42aa9205573b811587d57c2e58a6#file-trap_focus-js * */ const focusableElements = 'button, [href]:not(.js-expand-search), input, select, textarea, [tabindex]:not([tabindex="-1"])'; const modal = document.querySelector('.js-modal-component'); // select the modal by it's id if(modal !== null) { const firstFocusableElement = modal.querySelectorAll(focusableElements)[0]; // get first element to be focused inside modal const focusableContent = modal.querySelectorAll(focusableElements); const lastFocusableElement = focusableContent[focusableContent.length - 1]; // get last element to be focused inside modal document.addEventListener('keydown', function (e) { let isTabPressed = e.key === 'Tab' || e.keyCode === 9; if (!isTabPressed) { return; } if (e.shiftKey) { // if shift key pressed for shift + tab combination if (document.activeElement === firstFocusableElement) { lastFocusableElement.focus(); // add focus for the last focusable element e.preventDefault(); } } else { // if tab key is pressed if (document.activeElement === lastFocusableElement) { // if focused has reached to last focusable element then focus first focusable element after pressing tab firstFocusableElement.focus(); // add focus for the first focusable element e.preventDefault(); } } }); /** * Code for trapping focus inside mobile menu * */ const hamburger = document.querySelector('.js-toggle-mobile'); hamburger.addEventListener('keydown', function (e) { const mobileMenu = document.querySelector('.js-mobile-menu'); // select the modal by it's id const firstFocusableElementMobile = mobileMenu.querySelectorAll(focusableElements)[0]; // get first element to be focused inside modal const focusableContentMobile = mobileMenu.querySelectorAll(focusableElements); const lastFocusableElementMobile = focusableContentMobile[focusableContentMobile.length - 1]; document.addEventListener('keydown', function (e) { let isTabPressed = e.key === 'Tab' || e.keyCode === 9; if (!isTabPressed) { return; } if (e.shiftKey) { // if shift key pressed for shift + tab combination console.log(document.activeElement); if (document.activeElement === firstFocusableElementMobile) { lastFocusableElementMobile.focus(); // add focus for the last focusable element e.preventDefault(); } } else { // if tab key is pressed if (document.activeElement === lastFocusableElementMobile) { // if focused has reached to last focusable element then focus first focusable element after pressing tab firstFocusableElementMobile.focus(); // add focus for the first focusable element e.preventDefault(); } } }); firstFocusableElementMobile.focus(); }); firstFocusableElement.focus(); }