/** * Bongoto Theme – Customizer Live Preview * File: assets/js/customizer.js * * Live updates for settings using postMessage transport: * - bongoto_logo_max_height * - bongoto_show_site_title * - bongoto_header_sticky * - bongoto_header_show_search / _account / _cart / _upload * - bongoto_header_upload_label * - (bonus) bongoto_footer_copyright_extra * * Requires: wp.customize (loaded by WordPress in Customizer preview) */ (function( api, d ) { 'use strict'; if ( typeof api === 'undefined' ) return; /** * Helper: set max-height on logo images */ function setLogoMaxHeight(px) { const imgs = d.querySelectorAll('.bt-logo__img, .custom-logo'); imgs.forEach(img => { img.style.maxHeight = parseInt(px, 10) + 'px'; img.style.height = 'auto'; img.style.width = 'auto'; }); } /** * Helper: show/hide node list */ function setVisible(selector, visible) { d.querySelectorAll(selector).forEach(el => { el.style.display = visible ? '' : 'none'; el.hidden = !visible; if (visible) { el.classList.remove('bt-hidden'); } else { el.classList.add('bt-hidden'); } }); } /** * Logo Max Height */ api('bongoto_logo_max_height', function( value ) { setLogoMaxHeight(value.get()); value.bind(setLogoMaxHeight); }); /** * Site Title/Tagline toggle */ api('bongoto_show_site_title', function( value ) { function apply(v) { setVisible('.bt-site-title', !!v); } apply(value.get()); value.bind(apply); }); /** * Sticky header toggle (adds class to #masthead/.bt-header) */ api('bongoto_header_sticky', function( value ) { function apply(v) { const header = d.querySelector('#masthead') || d.querySelector('.bt-header'); if (!header) return; header.classList.toggle('bt-header--sticky', !!v); } apply(value.get()); value.bind(apply); }); /** * Header component visibility toggles */ const componentMap = { 'bongoto_header_show_search': '.bt-header-search', 'bongoto_header_show_account': '.bt-header-account', 'bongoto_header_show_cart': '.bt-header-cart', 'bongoto_header_show_upload': '.bt-header-upload, .bt-header-cta' }; Object.keys(componentMap).forEach(settingId => { api(settingId, function( value ) { const selector = componentMap[settingId]; function apply(v) { setVisible(selector, !!v); } apply(value.get()); value.bind(apply); }); }); /** * Upload button label */ api('bongoto_header_upload_label', function( value ) { function apply(v) { d.querySelectorAll('.bt-header-upload .bt-btn, .bt-header-cta .bt-btn').forEach(btn => { btn.textContent = v || btn.textContent; }); } apply(value.get()); value.bind(apply); }); /** * Footer copyright extra (live update in addition to selective refresh) */ api('bongoto_footer_copyright_extra', function( value ) { function apply(v) { const wrap = d.querySelector('.bt-footer-copyright'); if (!wrap) return; let extra = wrap.querySelector('.bt-footer-copyright-extra'); if (!extra) { extra = d.createElement('span'); extra.className = 'bt-footer-copyright-extra'; const p = wrap.querySelector('p') || wrap; p.appendChild(d.createElement('br')); p.appendChild(extra); } // Accept basic inline HTML; preview only (server sanitizes via sanitize callback) extra.innerHTML = v || ''; extra.style.display = v ? '' : 'none'; } apply(value.get()); value.bind(apply); }); })(window.wp && window.wp.customize, document);