bundle.js 2.83 KB
/**
 * Created by targaryen on 2016/11/25.
 */

let $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper');
let $bundle = $('#placeholder-bundle');
let bundleT = require('product/detail/bundle.hbs');
let bundleContentT = require('product/detail/bundle-content.hbs');

let bundleObj = {
    bundles: [],
    bundleIndex: 0,
    init(bundles) {
        let self = this;

        self.bundles = bundles;
        try {
            self.render();
            self.regTabsSwiper();
            self.regProductSwiper();
            self.regEvent();
        } catch (e) {
            console.error(e);
        }
    },
    render() {
        let self = this;

        let bundleHtml = bundleT({
            bundleDatas: self.bundles,
            bundleData: self.bundles[self.bundleIndex],
            bundleIndex: self.bundleIndex
        });

        return $bundle.replaceWith(bundleHtml);
    },
    renderProduct() {
        let self = this;

        let bundleHtml = bundleContentT({
            bundleData: self.bundles[self.bundleIndex],
        });

        $('.bundle-content').html(bundleHtml);
        self.regProductSwiper();
    },
    regEvent() {
        let self = this;

        $('.bundle-tabs').on('click', '.swiper-slide', (e) => {
            let index = $(e.currentTarget).data('index');

            if (self.bundleIndex !== index) {
                $(e.currentTarget).addClass('selected').siblings().removeClass('selected');
                self.bundleIndex = index;
                self.renderProduct();
                if (window._yas && window._yas.sendCustomInfo) {
                    window._yas.sendCustomInfo({
                        op: 'YB_GDS_PACKAGE_TAB_C',
                        param: JSON.stringify({
                            C_ID: window._ChannelVary[window.cookie('_Channel')],
                            PRD_ID: $('#productId').val(),
                            TAB_ID: index + 1,
                            PACKAGE_DETAIL: 1
                        })
                    }, true);
                }
            }
        });

        $('.bundle-nav').on('click', (e) => {
            let url = $(e.currentTarget).find('a').attr('href');

            window.location.href = url;
        });
    },
    regProductSwiper() {
        let self = this;

        if (self.bundles[self.bundleIndex].productList.length > 3) {
            new Swiper('.bundle-product', {
                freeMode: true,
                slidesPerView: 'auto',
                lazyLoading: true
            });
        }
    },
    regTabsSwiper() {
        let self = this;

        if (self.bundles.length > 3) {
            $('.bundle-tabs').removeClass('any');
            new Swiper('.bundle-tabs', {
                freeMode: true,
                slidesPerView: 'auto',
                lazyLoading: true
            });
        }
    }
};

module.exports = bundleObj;