/* * © NHN Commerce Corp. All rights reserved. * NHN Corp. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * @author Haekyu Cho * @since 2021-6-18 */ $(() => { const EXPIRE = 60 * 60 * 24 * 1000; // 1일 const GUEST_RECENT_PRODUCT_KEY = 'guestRecentProducts'; const localStorage = shopby.localStorage; const $recentProduct = $('#recentProduct'); const $cartProductTemplate = $('#cartProductTemplate').html(); const $scrollWrap = $('.scroll_wrap'); shopby.aside = { cartData: [], cartCurrentPage: 0, cartTotalPage: 0, async initiate() { if (shopby.logined()) { $('.btn_quick_order').removeClass('dn'); } await this.addRecentProductIfProductView(); this.bindEvents(); this.getRecentProducts(); this.getCartProducts(); // 장바구니 상품 this.renderScrollBanner(); }, bindEvents() { $(window).on('scroll', this.events.onScrollScrollBanner); $scrollWrap .on('click', '.btn_scroll_list_del', this.events.onClickDeleteRecentProduct.bind(this)) .on('click', '.bnt_scroll_prev', this.events.onClickPrevPage.bind(this)) .on('click', '.bnt_scroll_next', this.events.onClickNextPage.bind(this)) .on('click', '.btn_scr_top', this.events.onClickScrollTop.bind(this)) .on('click', '.btn_quick_my', this.events.onClickMyPage.bind(this)) .on('click', '#cartProduct', this.events.onClickCart.bind(this)); }, events: { onClickCart() { const isKtLogin = shopby.ktSso.isKtLogin('kt_sso_enc_token'); if (isKtLogin) { location.href = '/pages/order/cart.html'; } else { kt.popupLogin(); } }, onClickMyPage() { const isKtLogin = shopby.ktSso.isKtLogin('kt_sso_enc_token'); if (isKtLogin) { if (shopby.logined()) { location.href = '/pages/my/mypage.html'; } else { shopby.popup('kt-join-agreement', null, data => { if (data.state === 'ok') { window.location.href = `/pages/my/mypage.html`; } }); } } else { kt.popupLogin(); return false; } }, onClickPrevPage() { this.page.prevPage(); this.recentProductRender(this.page); }, onClickNextPage() { this.page.nextPage(); this.recentProductRender(this.page); }, onClickDeleteRecentProduct(e) { const name = 'productNo'; const productNo = e.target.dataset[name]; const refresh = productNo => { const items = this.page.getItemsByCache().filter(item => item[name] && item[name].toString() !== productNo); this.recentProductRender(this.page.create(items)); }; if (shopby.logined()) { shopby.api.product.deleteProfileRecentProducts({ productNo }).then(() => refresh.call(this, productNo)); } else { localStorage.popItemByOrder(GUEST_RECENT_PRODUCT_KEY, productNo); refresh.call(this, productNo); } }, onScrollScrollBanner() { const $scrollBanner = $scrollWrap.find('.scrollBanner'); const scrollWrapOffsetTop = $scrollWrap.offset().top; const scrollTop = $(window).scrollTop() + 200; const removeStyle = () => $scrollBanner.removeClass('ban_fixed').removeAttr('style'); const addStyle = () => $scrollBanner.addClass('ban_fixed'); scrollTop <= scrollWrapOffsetTop ? removeStyle() : addStyle(); }, onClickScrollTop() { $('body, html').animate( { scrollTop: 0, }, 400, ); }, }, recentProductRender(pageData) { $recentProduct.render(pageData).show(); }, // 장바구니 상품 노출 및 페이징 처리 cartProductRender({ target }) { const pageType = $(target).data('page-type'); if (pageType === 'prev') { if (this.cartCurrentPage === 1) return; this.cartCurrentPage = this.cartCurrentPage - 1; } if (pageType === 'next') { if (this.cartCurrentPage === this.cartTotalPage) return; this.cartCurrentPage = this.cartCurrentPage + 1; } const template = Handlebars.compile($cartProductTemplate); const cartData = typeof this.cartData === 'object' && Object.keys(this.cartData).length > 0 ? this.cartData[0].product : null; const html = template({ // cartData: this.cartData.slice(this.cartCurrentPage - 1, this.cartCurrentPage), cartData: cartData, // 가장 최근에 담은 상품만 노출 totalPage: this.cartTotalPage, currentPage: this.cartCurrentPage, }); $('.cartProductList').html(html); $('#cartProduct .js_current').text(this.cartCurrentPage); $('#cartProduct .js_total').text(this.cartTotalPage); }, renderScrollBanner() { const { scrollLeftBanner, scrollRightBanner } = this.getScrollBannerInfo(); const logined = shopby.logined(); $('#scroll_left').render(scrollLeftBanner); $('.right_banner').render(scrollRightBanner); $('#quickMenu').render({ logined: logined }); // [Jay] 템플릿 코드 노출돼서 랜더링 이후 노출처리 $('#asideTemp').removeClass('invisible'); }, getScrollBannerInfo() { const skin = shopby.cache.getSkin(); const scrollBannerInfo = { scrollLeftBanner: null, scrollRightBanner: null, }; if (!Array.isArray(skin)) { return scrollBannerInfo; } const found08 = skin.find(({ bannerGroupCode }) => bannerGroupCode === 'BANNER08'); const found09 = skin.find(({ bannerGroupCode }) => bannerGroupCode === 'BANNER09'); const latestLeftBanner = this.findLatestBanner(found08 ? found08.banners : []); const latestRightBanner = this.findLatestBanner(found09 ? found09.banners : []); scrollBannerInfo.scrollLeftBanner = latestLeftBanner && latestLeftBanner.bannerImages ? latestLeftBanner.bannerImages[0] : null; scrollBannerInfo.scrollRightBanner = latestRightBanner && latestRightBanner.bannerImages ? latestRightBanner.bannerImages[0] : null; return scrollBannerInfo; }, findLatestBanner(banners = []) { if (banners.length === 0) return; if (banners.length === 1) return banners[0]; return banners.reduce((prev, curr) => { return new Date(prev.updateDateTime).getTime() <= new Date(curr.updateDateTime).getTime() ? curr : prev; }); }, getRecentProducts() { const renderCallBack = result => { const expiredToken = !result || !result.error; if (expiredToken) { this.recentProductRender(this.page.create(result)); } else { this.page.evictItems(); this.recentProductRender(this.page.create()); } }; let items = this.page.getItemsByCache(); items = []; if (shopby.utils.isArrayNotEmpty(items)) { renderCallBack(items); return; } if (shopby.logined()) { // 샵바이 자체에서 최근 본 상품 데이터를 호출하는 시기가 5초 주기이므로 바로 리스트가 쌓이지 않아 5초후 노출되도록 수정 shopby.api.product .getProfileRecentProducts({ queryString: {} }) .then(({ data }) => renderCallBack.call(this, data)); } else { const guestRecentProductNos = (localStorage.getItemsByParced(GUEST_RECENT_PRODUCT_KEY) || []).join(','); if (guestRecentProductNos) { shopby.api.product .getGuestRecentProducts({ queryString: { mallProductNos: guestRecentProductNos } }) .then(({ data }) => renderCallBack.call(this, data)); } else { renderCallBack.call([]); } } }, async addRecentProductIfProductView() { const isProductView = window.location.href.includes('/product/view.html'); if (!isProductView) return; const name = 'productNo'; const searchParams = new URLSearchParams(window.location.search); if (searchParams.has(name) && Boolean(searchParams.get(name))) { const productNo = searchParams.get(name); if (shopby.logined()) { await shopby.api.product.postProfileRecentProducts({ requestBody: { productNo } }); } else { localStorage.unshiftItemByOrder(GUEST_RECENT_PRODUCT_KEY, productNo, EXPIRE); } this.page.evictItems(); } }, // 장바구니 상품 추출 getCartProducts() { return shopby.helper.cart.getCartData(true).then(({ list }) => { this.cartData = list; this.cartTotalPage = list.length; if (this.cartTotalPage === 0) $('#cartProduct .scr_paging').hide(); if (this.cartCurrentPage === 0 && this.cartTotalPage !== 0) this.cartCurrentPage = 1; this.cartProductRender(list); $('#cartProduct').show(); }); }, page: { currentPage: 1, totalPage: 0, totalCount: 0, PER_PAGE: 1, itemsByPaging: [], EXPIRE_TIME: 60 * 5 * 1000, _calculate() { this.totalCount = this.getItemsByCache().length; this.totalPage = Math.ceil(this.totalCount / this.PER_PAGE); const start = (this.currentPage - 1) * this.PER_PAGE; const end = start + this.PER_PAGE; this.itemsByPaging = this.getItemsByCache().slice(start, end) || []; // 최근상품 10개가 있고 현제 페이지가 2페이지에서 5개의 상품을 모두 삭제했을경우 이전페이지로 간다. if (this.currentPage > 1 && start === this.totalPage * this.PER_PAGE) { this.prevPage(); } }, prevPage() { const canPrev = this.currentPage > 1; this.currentPage = canPrev ? this.currentPage - 1 : this.totalPage; this._calculate(); }, nextPage() { const canNext = this.currentPage < this.totalPage; this.currentPage = canNext ? this.currentPage + 1 : 1; this._calculate(); }, create(items = []) { localStorage.setItemWithExpire(this.getExpireKey(), items, this.EXPIRE_TIME); this._calculate(); return this; }, getItemsByCache() { return localStorage.getItemWithExpire(this.getExpireKey()) || []; }, evictItems() { localStorage.removeItem(this.getExpireKey()); }, getExpireKey() { const key = shopby.cache.key.product.recent; return shopby.logined() ? key : `GUEST-${key}`; }, }, }; shopby.aside.initiate(); });