bundle.js 2.16 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;

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

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

        var 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();
            }
        });
    },
    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;