(() => {
const { EventManager, utils, constants } = ShopbySkin;
const { isMobileDevice } = utils;
const headerEl = document.querySelector('shopby-header-v2');
// 외부스크립트 등록 기능. 삭제금지
ShopbyExternalScript?.initialize({
apiOption: {
platform: 'PC',
},
}).then(() => {
EventManager.fire('QUERY_PAGE_SCRIPTS', ShopbyExternalScript?.scripts);
});
// 클릭이벤트 맵
// 1. SIGN_OUT : 로그아웃
const headerActionMap = {
SIGN_OUT: () => {
EventManager.fire('SIGN_OUT');
},
GO_MY_MENU: (e) => {
if (!utils.isSignedIn()) {
e.preventDefault();
const from = e.target.closest('a')?.href || location.href;
const nextUrl = `${location.origin}/pages/sign-in/sign-in.html`;
location.href = `${nextUrl}?from=${encodeURIComponent(from)}`;
}
},
};
// 클릭이벤트 핸들러
const clickEventListener = (e) => {
const action = e?.target?.closest('[shopby-action]')?.getAttribute('shopby-action');
headerActionMap[action]?.(e);
};
// 상단 카테고리 slider 초기화
EventManager.on('PAGE_LOAD_COMPLETED', ({ profile }) => {
EventManager.fire('INIT_CATEGORY_SLIDER', {
direction: 'horizontal',
effect: 'slide',
slidesPerView: 'auto',
spaceBetween: 20,
});
// 외부스크립트 등록 기능. 삭제금지
ShopbyExternalScript?.setGlobalObjectSb({
getPlatform: () => (isMobileDevice ? 'MOBILE_WEB' : 'PC'),
profile,
});
});
headerEl?.addEventListener('click', clickEventListener);
// Shared interactions also run on native product, account and board pages.
if (headerEl && !headerEl.dataset.hgcInteractionsReady) {
headerEl.dataset.hgcInteractionsReady = 'true';
const syncHeader = () => {
const panel = headerEl.querySelector('#hgc-category-panel');
const toggle = headerEl.querySelector('.hgc-category-toggle');
if (panel && toggle) toggle.setAttribute('aria-expanded', String(panel.classList.contains('is-active')));
const params = new URLSearchParams(location.search);
const community = location.pathname.startsWith('/pages/board/') && params.get('boardId') === 'free';
headerEl.querySelectorAll('.hgc-services a').forEach((link) => {
const active = community ? link.getAttribute('href').includes('boardId=free') : link.getAttribute('href') === '/';
if (active) link.setAttribute('aria-current', 'page');
else link.removeAttribute('aria-current');
});
headerEl.querySelectorAll('.hgc-nav-main a').forEach((link) => {
const target = new URL(link.href);
const active = location.pathname === target.pathname && params.get('keywords') === target.searchParams.get('keywords');
if (active) link.setAttribute('aria-current', 'page');
else link.removeAttribute('aria-current');
});
};
headerEl.addEventListener('click', (event) => {
if (event.target.closest('[data-hgc-collection]')) {
EventManager.fire('MODAL_ALERT_OPEN', {
noticeType: '',
message: '처음 들이기 좋은 식물을 고르고 있어요.
상품이 준비되면 이곳에서 안내할게요.',
});
return;
}
if (!event.target.closest('[data-hgc-encyclopedia]')) return;
EventManager.fire('MODAL_ALERT_OPEN', {
noticeType: '',
message: '식물도감을 준비하고 있어요.
식물의 이름과 특징을 만날 수 있도록 곧 안내할게요.',
});
});
headerEl.addEventListener('keydown', (event) => {
if (event.key !== 'Escape') return;
const panel = headerEl.querySelector('#hgc-category-panel');
const toggle = headerEl.querySelector('.hgc-category-toggle');
if (panel?.classList.contains('is-active') && toggle) {
toggle.click();
toggle.focus();
}
});
new MutationObserver(syncHeader).observe(headerEl, {
childList: true, subtree: true, attributes: true, attributeFilter: ['class'],
});
syncHeader();
}
const searchParams = new URLSearchParams(location.search);
const trackingKey = searchParams.get('trackingKey');
const channelType = searchParams.get('channelType');
const { setTrackingKey, setChannelType } = ShopbySkin.utils;
setTrackingKey(trackingKey);
setChannelType(channelType);
const requireMarketingPrivacyAgreeNotification = sessionStorage.getItem(
constants.REQUIRE_MARKETING_PRIVACY_AGREE_NOTIFICATION
);
if (requireMarketingPrivacyAgreeNotification) {
sessionStorage.removeItem(constants.REQUIRE_MARKETING_PRIVACY_AGREE_NOTIFICATION);
if (!utils.isSignIn()) return;
const excludePages = new Set(['/pages/sign-in/expired-password-change-form.html', '/pages/my/modify-member.html']);
if (excludePages.has(location.pathname)) return;
EventManager.fire('MODAL_ALERT_OPEN', {
noticeType: '',
message: '이메일 및 SMS 수신을 위해
마케팅 목적의 개인정보 수집·이용에 동의해 주세요.',
onClose: () => {
const nextUrl = `${location.origin}/pages/my/modify-member.html`;
location.replace(nextUrl);
},
});
}
})();