/* eslint-disable camelcase */ (() => { const { EventManager, utils } = ShopbySkin; const { createPageScriptInstance } = utils; const pageScriptInstance = createPageScriptInstance('MOBILE'); const containerEl = document.querySelector('[shopby-footer-container]'); let shopbyCommonData = {}; EventManager.on('PAGE_LOAD_COMPLETED', (commonData) => { shopbyCommonData = commonData; }); 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: async () => { const promptEvent = window.deferredPrompt; if (!promptEvent) return; promptEvent.prompt(); const { outcome } = await promptEvent.userChoice; if (outcome === 'accepted') { // eslint-disable-next-line require-atomic-updates window.deferredPrompt = null; window.close(); } }, USE_TERMS: ({ usesAttachment }) => { EventManager.fire('OPEN_LAYER_MODAL', { name: 'use-terms', title: '이용약관', data: { isModify: false, usesAttachment, commonData: shopbyCommonData, }, }); }, PI_PROCESS_TERMS: ({ usesAttachment }) => { EventManager.fire('OPEN_LAYER_MODAL', { name: 'pi-process-terms', title: '개인정보처리방침', data: { isModify: false, usesAttachment, commonData: shopbyCommonData, }, }); }, }; const clickEventListener = (e) => { const action = e.target.getAttribute('shopby-action'); CLICK_EVENT_HANDLER_MAP[action]?.(e); }; containerEl?.addEventListener('click', clickEventListener); const useTermsModuleEl = document.querySelector('[shopby-footer-terms]'); useTermsModuleEl?.addEventListener('click', ({ target }) => { let action = target.getAttribute('shopby-action'); const termsKey = target.getAttribute('data-terms-key'); // [Jay] 약관키가 PI_PROCESS인 경우 개인정보처리방침 약관 모달 띄움 if (termsKey === 'PI_PROCESS') { action = 'PI_PROCESS_TERMS'; } const usesAttachment = target .closest('[shopby-uses-attachment]') ?.getAttribute('shopby-uses-attachment'); CLICK_EVENT_HANDLER_MAP[action]?.({ termsKey, usesAttachment }); }); EventManager.on('PAGE_LOAD_COMPLETED', (commonData) => { shortcut.initialize(commonData); pageScriptInstance.appendPageScript('COMMON_FOOTER'); }); })();