detail.page.js 4.8 KB
/**
 *  商品详情
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/20
 */
require('../../scss/product/detail/_index.css');
var $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
    Hammer = require('yoho-hammer'),
    lazyLoad = require('yoho-jquery-lazyload');

require('./detail/page-render')(function() {
    var $discountFolder = $('.goods-discount .discount-folder'),
        $discountArrow = $('.goods-discount .first-item span');

    var goodsDiscountEl = document.getElementById('goodsDiscount'),
        goodsDiscountHammer = goodsDiscountEl && new Hammer(goodsDiscountEl),
        $discountFirstItem = $(goodsDiscountEl).find('.first-item');

    var $cart = $('.cart-bar');

    var appUrl = $('input[name="limitCodeUrl"]').val();

    var dialog = require('../plugin/dialog');

    require('../common');

    // add extra marign-bottom for footer to show the yoho copyright
    function showFooter() {
        var $cartBar = $('.cart-bar');

        var timer = setInterval(function() {
            if ($cartBar) {
                window.reMarginFooter('.cart-bar');
                clearInterval(timer);
            } else {
                $cartBar = $('.cart-bar');
            }
        }, 200);
    }

    showFooter();

    lazyLoad($('img.lazy'));

    if ($('#goodsDiscount h1').length < 1) {
        $('.dropdown').remove();
    }

    // 顶部swiper
    new Swiper('.banner-swiper', {
        lazyLoading: true,
        lazyLoadingInPrevNext: true,
        paginationClickable: true,
        pagination: '.banner-top .pagination-inner',
        nextButton: '.my-swiper-button-next',
        prevButton: '.my-swiper-button-prev',
        spaceBetween: 3
    });

    // 初始化goods-discount
    if ($discountFolder.children().length === 0) {
        $discountFolder.css('display', 'none');
        $discountArrow.html('');
    }

    // goods-discount下拉按钮点击事件
    if (goodsDiscountHammer && $discountFolder.children().length > 0) {
        goodsDiscountHammer.on('tap', function() {
            if ($discountFolder.is(':hidden')) {
                $discountFirstItem.removeClass('short-text');
                $discountArrow.removeClass('icon-down').addClass('icon-up').html('&#xe608;');
                $discountFolder.slideDown();
            } else {
                $discountFirstItem.addClass('short-text');
                $discountArrow.removeClass('icon-up').addClass('icon-down').html('&#xe609;');
                $discountFolder.slideUp();
            }
        });
    }

    $('#limit-sale').on('touchend', function(e) {
        var loginUrl = $('#loginUrl').val(),
            time,
            ifr;

        e.stopPropagation();

        if (loginUrl) {
            window.location = loginUrl;
        } else {
            dialog.showDialog({
                dialogText: '进入有货APP,获取限购码',
                hasFooter: {
                    rightBtnText: '打开Yoho!Buy有货APP'
                }
            }, function() {
                ifr = document.createElement('iframe');
                ifr.src = appUrl;
                ifr.style.display = 'none';
                document.body.appendChild(ifr);
                window.location.href = appUrl;

                time = Date.now();
                window.setTimeout(function() {
                    document.body.removeChild(ifr);
                    if (Date.now() - time < 3200) {
                        window.location.href = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho';
                    }
                }, 3000);

            }, null, true);

            $('.dialog-wrapper').off('touchstart').on('touchstart', function(para) {
                para.stopPropagation();
                if ($(para.target).hasClass('dialog-wrapper')) {
                    dialog.hideDialog();
                }
            });
        }
    });

    require('./detail/like');
    require('./detail/desc');
    require('./detail/comments-consults');
    require('./detail/consultform');
    require('./detail/recommend-for-you-product-desc');

    if ($('#remove-cart-count').length) {
        // 购物车商品数量
        $.ajax({
            type: 'GET',
            url: '/cart/index/count',
            success: function(data) {
                var count;

                if (data.code === 200) {
                    count = data.data.cart_goods_count || 0;
                    if (count === 0) {
                        return false;
                    }
                    if (count > 99) {
                        count = '99+';
                    }
                    $cart.find('.num-tag').html(count).removeClass('hide');
                }
            }
        });
    }

    if ($('.good-detail-page').length > 0) {
        $('#yoho-footer').css('border-top', '1px solid #e0e0e0');
    }
});
require('./detail/brand-coupon');