/* eslint-disable camelcase */ (() => { const { Handlebars, EventManager } = ShopbySkin; Handlebars.registerHelper('stripNonDigits', function (value) { return value.replace(/[^0-9]/g, ''); }); const containerEl = document.querySelector('[shopby-footer-container]'); const shortcut = { async initialize(commonData) { await this.replaceManifestWith(this.generateManifestUrl(commonData)); this.executeServiceWorker(); }, replaceManifestWith(manifestURL) { const manifestPlaceholderEl = document.querySelector('[shopby-manifest-placeholder]'); manifestPlaceholderEl?.setAttribute('href', manifestURL); }, generateManifestUrl(commonData) { const mallName = commonData.mallConfig.mall?.mallName; const manifest = { name: mallName, short_name: mallName, description: mallName, start_url: location.origin, display: 'standalone', orientation: 'portrait', incognito: 'split', icons: [ { src: `https:${commonData.bannerConfig?.appIcon}`, sizes: '144x144', type: 'image/png', purpose: 'any', }, ], }; const blob = new Blob([JSON.stringify(manifest)], { type: 'application/json' }); return URL.createObjectURL(blob); }, executeServiceWorker() { this.deferPromptEvent(); }, deferPromptEvent() { window.addEventListener('beforeinstallprompt', (event) => { event.preventDefault(); window.deferredPrompt = event; }); window.addEventListener('appinstalled', (event) => { event.preventDefault(); window.deferredPrompt = null; }); }, }; const CLICK_EVENT_HANDLER_MAP = { HANDLE_ADD_TO_HOME_BTN_CLICK: () => { const promptEvent = window.deferredPrompt; if (!promptEvent) return; promptEvent.prompt(); window.deferredPrompt = null; }, }; const clickEventListener = (e) => { const action = e.target.getAttribute('shopby-action'); CLICK_EVENT_HANDLER_MAP[action]?.(e); }; containerEl?.addEventListener('click', clickEventListener); const footerInfo2Tit = document.querySelector('.footer__info2 .tit'); if (footerInfo2Tit) { footerInfo2Tit.addEventListener('click', function () { this.classList.toggle('active'); this.nextElementSibling.classList.toggle('hidden'); }); } // 주문서/장바구니 페이지에서는 플로팅 버튼 숨김 const hideFloatingPages = ['/pages/order/order-sheet-form.html', '/pages/order/cart.html']; const isHidePage = hideFloatingPages.some((page) => location.pathname.includes(page)); if (isHidePage) { const floatingButtons = document.getElementById('floating-buttons'); const floatMenuContainer = document.querySelector('.float-menu-container'); if (floatingButtons) floatingButtons.style.display = 'none'; if (floatMenuContainer) floatMenuContainer.style.display = 'none'; } // 상품 상세 페이지에서는 bottom: 60px const isProductDetailPage = location.pathname.includes('/pages/product/product-detail.html'); if (isProductDetailPage) { const floatingButtons = document.getElementById('floating-buttons'); const floatingButtons2 = document.querySelector('.float-menu-container'); if (floatingButtons) floatingButtons.style.bottom = '60px'; if (floatingButtons2) floatingButtons2.style.bottom = '60px'; } // 플로팅 메뉴 토글 const floatMenuToggle = document.querySelector('.float-menu-toggle'); const floatMenuContainer = document.querySelector('.float-menu-container'); if (floatMenuToggle && floatMenuContainer) { floatMenuToggle.addEventListener('click', () => { floatMenuToggle.classList.toggle('active'); floatMenuContainer.classList.toggle('open'); }); } ////////////////////////// /* PWA 설치 버튼 관련 */ ////////////////////////// // PWA 설치 프롬프트 저장 변수 let deferredPrompt = null; let isPageLoaded = false; // 플로팅 버튼 위치 재계산 함수 const repositionFloatButtons = () => { const container = document.querySelector('.float-menu-container.vertical'); if (!container) return; const buttons = Array.from(container.querySelectorAll('.float-menu-button')); const gap = 12; const buttonSize = 46; // 보이는 버튼만 필터링 (역순으로 - 아래부터 위로) const visibleButtons = buttons.filter((btn) => btn.style.display !== 'none').reverse(); // 보이는 버튼들에게 아래부터 위로 위치 지정 visibleButtons.forEach((btn, index) => { const offset = (buttonSize + gap) * (index + 1); btn.style.setProperty('--float-offset', `-${offset}px`); }); }; // 인앱브라우저 체크 const isInAppBrowser = /inapp|NAVER|KAKAOTALK|Instagram|FBAN|FBAV|Twitter|Line|Daum/i.test( navigator.userAgent ); // PWA 버튼 초기화 함수 const initPwaButton = () => { const installBtns = document.querySelectorAll('#pwa-install-btn'); if (installBtns.length === 0) return; // 이미 설치된 앱인지 확인 (standalone 모드 = PWA로 실행 중) const isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true; if (isStandalone) { installBtns.forEach((btn) => (btn.style.display = 'none')); repositionFloatButtons(); return; } // 미지원 브라우저 체크 (beforeinstallprompt 미지원) const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); const isUnsupportedBrowser = /Firefox|MSIE|Trident|Opera Mini|UCBrowser/i.test(navigator.userAgent) || (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)); // 조건: deferredPrompt 있거나 인앱브라우저거나 미지원 브라우저면 버튼 표시 if (deferredPrompt || isInAppBrowser || isUnsupportedBrowser) { installBtns.forEach((btn) => (btn.style.display = '')); } else { // 그 외 (이미 설치됨) → 숨김 installBtns.forEach((btn) => (btn.style.display = 'none')); } repositionFloatButtons(); }; // beforeinstallprompt 이벤트 캡처 window.addEventListener('beforeinstallprompt', (e) => { e.preventDefault(); deferredPrompt = e; // 페이지 로드가 이미 완료됐으면 버튼 다시 초기화 if (isPageLoaded) { const installBtns = document.querySelectorAll('#pwa-install-btn'); installBtns.forEach((btn) => (btn.style.display = '')); repositionFloatButtons(); } }); // 이미 설치된 경우 처리 window.addEventListener('appinstalled', () => { deferredPrompt = null; const installBtns = document.querySelectorAll('#pwa-install-btn'); installBtns.forEach((btn) => (btn.style.display = 'none')); }); // ShopBy 페이지 로드 완료 후 PWA 버튼 초기화 EventManager.on('PAGE_LOAD_COMPLETED', () => { isPageLoaded = true; const installBtns = document.querySelectorAll('#pwa-install-btn'); if (installBtns.length === 0) return; // 버튼 초기화 initPwaButton(); // 클릭 이벤트 바인딩 (모든 버튼에) installBtns.forEach((btn) => { btn.addEventListener('click', () => { // 인앱브라우저 안내 if (isInAppBrowser) { alert('이 브라우저는 앱 설치를 지원하지 않습니다.\nChrome, 삼성 인터넷 등을 이용해주세요.'); return; } // 설치 프롬프트가 있으면 실행 if (deferredPrompt) { deferredPrompt.prompt(); deferredPrompt = null; return; } // iOS Safari 안내 const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); const isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent); if (isIOS && isSafari) { alert( '홈 화면에 추가하려면:\n1. 하단의 공유 버튼(📤)을 탭하세요\n2. "홈 화면에 추가"를 선택하세요' ); return; } // 그 외 (미지원 브라우저) alert('이 브라우저는 앱 설치를 지원하지 않습니다.\nChrome, 삼성 인터넷 등을 이용해주세요.'); }); }); }); })();