cart-together.js 8.4 KB
/**
 * Created by yoho on 2017-01-13.
 */

var lazyLoad = require('yoho-jquery-lazyload');
var dataLazyLoad = require('../plugins/lazy-load')(document);

var capi = require('./cart-api');
var yas = require('../common/data-yas');
var Dialog = require('../common/dialog').Dialog;
var togetherTpl = require('hbs/cart/cart-together-item.hbs');
var togetherPopTpl = require('hbs/cart/cart-together-pop-list.hbs');
var $fineSlide = $('[data-role=fine-slide]');
var $latestWalkSlide = $('[data-role=latestWalk-slide]');
var productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs');
var $goodsSelWin = $('#Y_goodsSelectWin');
var $body = $('body');

function randomString(len) {
    var $chars = 'ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-';

    var maxPos = $chars.length,
        pwd = '',
        i;

    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }

    return pwd;
}

function loadTogetherProduct(params) {
    var that = this;

    capi.getTogetherProduct(params).done(function(togetherInfo) {
        var _content;

        if (togetherInfo && togetherInfo.code === 200) {
            _content = togetherPopTpl(togetherInfo.data);

            if (that.togetherDialog && params) {
                that.togetherDialog.$el.find('.product-wrap').replaceWith($(_content).find('.product-wrap'));
                return;
            }

            that.togetherDialog = new Dialog({
                content: togetherPopTpl(togetherInfo.data),
                className: 'together-product-dialog'
            }).show();

            togetherDialogEventBind(that.togetherDialog); // eslint-disable-line
        }
    });
}

function togetherDialogEventBind(dg) {
    var $dialog = dg.$el;

    $body.addClass('mmodal-body');
    dg.hideDialog = function() {
        this.$el.hide();
        this.$mask.hide();
        $body.removeClass('mmodal-body');
    };
    dg.showDialog = function() {
        this.$el.show();
        this.$mask.show();
        $body.addClass('mmodal-body');
    };

    $dialog.on('click', '.price-tabs li', function() {
        var $this = $(this);
        var price = $this.data('price');

        $this.siblings('.active').removeClass('active');
        $this.addClass('active');

        dg.price = price;
        price && loadTogetherProduct({price: price});
    }).on('click', '.num-page', function() {
        var $this = $(this);

        if (!$this.hasClass('cur')) {
            loadTogetherProduct({
                price: dg.price,
                page: $.trim($this.text())
            });
        }
    }).on('click', '.pre-page, .next-page', function() {
        var $this = $(this);
        var page = $.trim($('.num-page.cur', $dialog).text());

        if ($this.hasClass('pre-page')) {
            page--;
        } else {
            page++;
        }

        loadTogetherProduct({
            price: dg.price,
            page: page
        });
    }).on('click', '.btn-add-cart', function() {
        var data = $(this).data();

        dg.hideDialog();
        showTogetherSelWin(data.id, data.skn, 'mmodalHideCallback');  // eslint-disable-line
        window.mmodalHideCallback = function() {
            dg.showDialog();
        };
    }).on('click', '.close-btn, .sure-btn', function() {
        dg.close();
        $body.removeClass('mmodal-body');
    });
}

function loadFineProduct(page) {
    capi.getFineProduct(page).done(function(fineInfo) {
        if (fineInfo && fineInfo.code === 200) {
            $fineSlide.html(togetherTpl($.extend({
                showAdd2Cart: true
            }, fineInfo.data)));
            $fineSlide.find('li').eq(4).css('margin-right', 0);

            // 若无数据,请隐藏容器
            if (!fineInfo.data || !fineInfo.data.item || fineInfo.data.item.length === 0) {
                $fineSlide.remove();
                $('[data-role=fine-tab]').remove();
                $('.bottom-tab-slide .change').remove();

                $('.bottom-tab-slide .bottom-title').eq(0).trigger('click');
                return;
            } else {
                $('.bottom-tab-slide').removeClass('none');
                $('.bottom-slide-content').removeClass('none');
            }

            lazyLoad($fineSlide.find('.lazy'));

            $fineSlide.find('.good').on('click', function() {
                var $this = $(this);
                var proId = $this.data('id');
                var loadYas = {
                    REC_POSE: 120003,
                    REC_ID: randomString(40),
                    PRD_ID: proId,
                    PRD_NUM: $this.data('index') + 1,
                    ACTION_ID: 1,
                    PAGE_NUM: 1
                };

                yas.givePoint('YB_CHOOSE_FOR_YOU_Y', loadYas);
            });

            yas.givePoint('YB_CHOOSE_FOR_YOU_Y', {
                REC_POSE: 120003,
                REC_ID: randomString(40),
                PRD_ID: $.map(fineInfo.data.item, function(value) {
                    return value.id;
                }).join(','),
                PRD_NUM: fineInfo.data.item.length,
                ACTION_ID: 0,
                PAGE_NUM: 1
            });
        }
    });/* .fail(function() {

     });*/
}

function fetchLatestWalk(num) {

    $.ajax({
        url: '//www.yohobuy.com/common/recentReview',
        dataType: 'jsonp',
        data: {
            limit: num || 15
        },
        success: function(data) {
            var latestWalk = [],
                res, i, cur;

            if (data.code === 200) {
                res = data.data;

                // 若无最近浏览,请隐藏容器
                if (res.length === 0) {
                    $latestWalkSlide.hide();
                    $('[data-role=latestWalk-tab]').hide();
                    return;
                } else {
                    $('.bottom-tab-slide').removeClass('none');
                    $('.bottom-slide-content').removeClass('none');
                }

                for (i = 0; i < res.length; i++) {
                    cur = res[i];

                    latestWalk.push({
                        href: cur.url,
                        img: cur.pic_url,
                        title: cur.product_name,
                        price: cur.price,
                        marketPrice: cur.market_price
                    });
                }

                $latestWalkSlide.html(togetherTpl({
                    item: latestWalk,
                    hasNext: latestWalk && latestWalk.length > 5,
                    hasPrev: latestWalk && latestWalk.length > 5
                }));
                lazyLoad($latestWalkSlide.find('.lazy'));

                $latestWalkSlide.find('.individual-item').slider2({
                    shownum: 5,
                    isCircle: true
                });
            }
        }
    });
}

$('.bottom-tab-slide .change').on('click', function() {

    var pageNum = Number($fineSlide.data('pagenum') || 1);

    if (pageNum !== 3) {
        pageNum++;
    } else {
        pageNum = 1;
    }

    $fineSlide.data('pagenum', pageNum);
    loadFineProduct(pageNum);
});

// 推荐、优选、浏览切换
$('.bottom-tab-slide').on('click', '.bottom-title', function() {
    var $this = $(this),
        $triggers = $('.bottom-tab-slide .bottom-title'),
        index = $this.index(),
        $panels = $('.bottom-slide-content .slide-panel'),
        $curPanel = $panels.eq(index),
        $chgBtn = $('.bottom-tab-slide .change');

    $panels.hide();
    $panels.eq(index).show();

    if ($this.hasClass('cur')) {
        return;
    }

    $triggers.removeClass('cur');
    $this.addClass('cur');

    if ($this.data('type') === 'fine') {
        $chgBtn.show();
    } else {
        $chgBtn.hide();
    }

    $panels.hide();
    $curPanel.slideDown();
});

function showTogetherSelWin(id, skn, cbname) {

    capi.getProductInfo(id, skn).then(function(res) {
        $goodsSelWin.find('.detail-body').empty().append(productInfoTpl(res));
    });

    capi.showMDialog('#Y_goodsSelectWinDialog', cbname);
}

$('.bottom-slide-content').on('click', '[data-role=add2cart]', function() {
    var $this = $(this);
    var id = $this.data('id');
    var skn = $this.data('skn');

    showTogetherSelWin(id, skn);
});

// 凑单商品
$('.order-pay').on('click', '.piece-goods-btn', function() {
    loadTogetherProduct();
});

window.loadFineProduct = loadFineProduct;
window.fetchLatestWalk = fetchLatestWalk;

// 数据懒加载
dataLazyLoad.init({cls: '.lazy-load-object', threshold: 700});