/** * SmartMag Theme Frontend JS. * * @copyright 2024 ThemeSphere. */ "use strict"; if (!Bunyad) { var Bunyad = {}; } Bunyad.theme = (function($) { var hasTouch = false, responsiveMenu = false, isIframe = window.BunyadIsIframe || false, schemeKey = window.BunyadSchemeKey || 'bunyad-scheme'; if (!window.requestIdleCallback) { window.requestIdleCallback = requestAnimationFrame; } // module var self = { init: function() { // Detect touch capability dynamically. $(window).on('touchstart', function() { // First touch. if (!hasTouch) { $('body').addClass('touch'); self.touchNav(); } hasTouch = true; }); this.imageEffects(); // Setup all sliders this.sliders(); this.contentSlideshow(); // Setup mobile header and navigation this.header(); this.responsiveNav(); this.megaMenus(); // Start the news ticker this.newsTicker(); // Setup the lightbox requestIdleCallback(this.lightbox); // Search modal this.searchModal(); // Use sticky sidebar if enabled this.initStickySidebar(); // User ratings this.userRatings(); this.tabWidget(); /** * Woocommerce */ $('.woocommerce-ordering .drop li a').on('click', function(e) { var form = $(this).closest('form'); form.find('[name=orderby]').val($(this).parent().data('value')); form.trigger('submit'); e.preventDefault(); }); $(document).on('ts-ajax-pagination', e => { $(document).trigger('scroll.TSS'); Bunyad.sliders(); }); // Lower priority. requestIdleCallback(() => { self.contextualBinds(document); // Nav icons class if missing $('.navigation i.only-icon').each(function() { var el = $(this).closest('li'); if (!el.hasClass('only-icon')) { el.addClass('only-icon'); } }); // Single show comments. $(document).on('click', '.ts-comments-show', function() { $(this).hide() .closest('.comments') .find('.ts-comments-hidden').show(); return false; }); }, {timeout: 1000}); }, /** * @param {HTMLDocument|HTMLElement} document */ contextualBinds: function(document) { // Fit videos to container. requestAnimationFrame(() => { $('.featured-vid, .post-content', document).fitVids(); }); // Ratings $('.review-number', document).each(function() { var raw = $(this).find('span:not(.progress)').html(), progress = parseFloat(raw); $(this).find('.progress').css('width', (raw.search('%') === -1 ? (Math.round(progress / 10 * 100)) + '%' : progress)); }); $('.review-box ul li .bar', document).each(function() { $(this).data('width', $(this)[0].style.width).css('width', 0); }); new Bunyad_InView( document.querySelectorAll('.review-box ul li', document), entry => { const bar = $(entry.target).find('.bar'); bar.addClass('appear').css('width', bar.data('width')); }, {once: true} ); // Single top social sharing view all buttons $('.post-share-b .show-more', document).on('click', function() { $(this).parent().addClass('all'); return false; }); $('.service.s-link').on('click', e => { // Temporary input copy trick as clipboard API only available over https. const temp = document.createElement('input'); Object.assign(temp, { value: location.href, style: {position: 'absolute', left: '-200vw'} }); document.body.prepend(temp); temp.select(); document.execCommand('copy'); temp.remove(); let snackbar = document.querySelector('.ts-snackbar.copy-link'); if (!snackbar) { snackbar = document.createElement('div'); const message = e.currentTarget.querySelector('[data-message]').dataset.message; snackbar.className = 'ts-snackbar copy-link'; snackbar.innerHTML = `${message}`; document.body.append(snackbar); } if (snackbar.classList.contains('ts-snackbar-active')) { return false; } requestAnimationFrame(() => snackbar.classList.add('ts-snackbar-active')); const hideMessage = () => { snackbar.classList.remove('ts-snackbar-active'); snackbar.hide(); return false; } setTimeout(hideMessage, 5 * 1000); $(snackbar).on('click', '.tsi-close', hideMessage); return false; }); // Go back handler for 404. $('.go-back').on('click', function() { window.history.back(); return false; }); }, /** * Reinit common theme functionality, in a context, usually ALP feature. * * @param {HTMLDocument|HTMLElement} context */ reInit: function(context) { // Only for elements. if (context !== document) { this.imageEffects(context.querySelectorAll('img')); } this.contextualBinds(context); this.lightbox(); this.contentSlideshow(); this.userRatings(); setTimeout(this.stickySidebar, 50); Bunyad.sliders(); }, /** * Image scroll animations. */ imageEffects: function(elements) { // Image effects - remove temp CSS when loaded. if (!$('body').hasClass('img-effects')) { return; } if (!elements) { elements = document.querySelectorAll('.main-wrap .wp-post-image, .post-content img'); } elements.forEach(e => e.classList.add('no-display')); $('.bunyad-img-effects-css').remove(); new Bunyad_InView( elements, entry => entry.target.classList.add('appear'), {once: true}, () => $(elements).addClass('appear') ); }, newsTicker: function() { $('.trending-ticker').each(function() { if (!$(this).find('li.active').length) { $(this).find('li:first').addClass('active'); } let ticker = $(this); let interval = false; const delay = parseInt(ticker.data('delay') || 5) * 1000; const setTick = () => { var active = ticker.find('li.active'); active.fadeOut(function() { var next = active.next(); if (!next.length) { next = ticker.find('li:first'); } next.addClass('active').fadeIn(); active.removeClass('active'); }); } const startTicker = () => { if (interval) { return; } interval = window.setInterval(setTick, delay); } startTicker(); ticker.on('mouseenter', 'li', () => { clearInterval(interval); interval = false; }); ticker.on('mouseleave', 'li', startTicker); }); }, header: function() { const toggleDarkMode = (scheme) => { const prevScheme = (scheme == 'dark' ? 'light' : 'dark'); // Set initial scheme. const htmlClass = $('html').data('origClass'); let initialScheme; if (htmlClass) { initialScheme = htmlClass.includes('s-dark') ? 'dark' : 'light'; } if ($('html').data('autoDark')) { initialScheme = 'dark'; } if (scheme !== initialScheme) { localStorage.setItem(schemeKey, scheme); } else { localStorage.removeItem(schemeKey); } $('html') .removeClass(`s-${prevScheme} site-s-${prevScheme}`) .addClass(`s-${scheme} site-s-${scheme}`); }; $('.scheme-switcher .toggle').on('click', function() { const ele = $(this); let scheme = ele.hasClass('toggle-dark') ? 'dark' : 'light'; toggleDarkMode(scheme); // Notify auto-loaded posts. document.querySelectorAll('iframe.spc-auto-load-post').forEach(element => { element.contentWindow.postMessage({ action: 'toggle-dark-mode', scheme }, '*'); }); return false; }); // Listen and change in auto-loaded posts. if (isIframe) { window.addEventListener('message', e => { if (!e.data || e.data.action !== 'toggle-dark-mode') { return; } toggleDarkMode(e.data.scheme); }); } }, /** * Setup the responsive nav events and markup */ responsiveNav: function() { this.initResponsiveNav(); }, initResponsiveNav: function() { if (responsiveMenu || !$('.off-canvas').length) { responsiveMenu = true; return; } // Set responsive initialized responsiveMenu = true; // Test for overlay/non-obstrusive scrollbar (MacOS). const hasOverlayScroll = () => { const ele = document.createElement("div"); ele.setAttribute("style", "width:30px;height:30px;overflow:scroll;opacity:0"); document.body.appendChild(ele); const result = ele.offsetWidth === ele.clientWidth; document.body.removeChild(ele); return result; } const setupScroll = () => { if (!hasTouch && !hasOverlayScroll()) { document.body.classList.add("has-scrollbar"); // Reflow to fix scrollbars. const ele = $('.off-canvas-content'); ele.css('display', 'block'); requestAnimationFrame(function() { ele.css('display', 'flex'); }); } } const setupMenuHandlers = () => { // Setup mobile menu click handlers $('.mobile-menu li > a').each(function() { if ($(this).parent().children('ul').length) { $('').insertAfter($(this)); } }); $('.mobile-menu li .chevron').on('click', function() { $(this).closest('li').find('ul') .first() .parent() .toggleClass('active item-active'); return false; }); } const setupMobileMenu = () => { var container = $('.mobile-menu-container'); // No items for mobile menu? Grab from others if (!container.find('.mobile-menu').children().length) { // Merge existing menus var mobileMenu = container.find('.mobile-menu'), mainMenu = $('.navigation-main .menu'); if (!mainMenu.length) { return; } var menu = mainMenu.children().clone(); // add category mega menu links menu.find('.mega-menu .sub-cats').each(function() { var container = $(this).closest('.menu-item'); container.append($(this).find('.sub-nav')); container.find('.sub-nav').replaceWith(function() { return $('