favorite.js 9.69 KB
/**
 * 我的收藏
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/2/23
 */

var $ = require('yoho.jquery'),
    Handlebars = require('yoho.handlebars');

var phoneReg = require('../passport/mail-phone-regx').phoneRegx['+86'];

var clockTxt = {
    on: '[ 降价通知 ]',
    off: '[ 取消通知 ]'
};

var favType;

var curSkn;

var $curClock;

require('yoho.dotdotdot');

//商品收藏
(function() {
    var $defaultSorts,
        $allSorts;

    var $bodyMask,
        $priceNotice,
        $noticeContent,
        $noticeSuccess,
        $noticeSubOrCancel;

    //关闭价格订阅弹窗和蒙层
    function closeNoticeBox() {
        $bodyMask.addClass('hide');
        $priceNotice.addClass('hide');

        //重置头部文字
        $noticeSubOrCancel.text('订阅成功');
    }

    //显示价格订阅弹窗和蒙层
    function showNoticeBox() {
        if (typeof $bodyMask === 'undefined') {
            $('body').append('<div class="body-mask hide"></div>');
            $bodyMask = $('.body-mask');
        }

        $bodyMask.css({
            height: $(document).height(),
            width: $(document).width()
        }).removeClass('hide');

        if ($curClock.hasClass('noticed')) {

            //取消降价通知
            $.ajax({
                type: 'GET',
                url: '/home/favorite/cancelnotice',
                data: {
                    id: curSkn
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $noticeContent.addClass('hide');
                    $noticeSuccess.removeClass('hide').find('.notice-num').text(data.data.num);
                    $noticeSubOrCancel.text('取消成功');

                    if ($curClock.closest('.reduction-products').length > 0) {
                        $curClock.closest('.fav-good').remove();
                    } else {
                        $curClock.text(clockTxt.on);
                    }
                }
            });
        }

        $priceNotice.removeClass('hide');
    }

    if ($('.fav-products').length === 0) {
        return;
    }

    favType = 'products';

    $defaultSorts = $('.default-sorts');
    $allSorts = $('.all-sorts');

    $priceNotice = $('.price-notice');

    $noticeContent = $('#price-notice .content');
    $noticeSuccess = $('#price-notice .success');
    $noticeSubOrCancel = $noticeSuccess.find('.subscribe-or-cancel');

    //展开分类
    $('#spread-sort').click(function() {
        $defaultSorts.slideUp(function() {
            $allSorts.slideDown();
        });
    });

    //收起分类
    $('#retract-sort').click(function() {
        $allSorts.slideUp(function() {
            $defaultSorts.slideDown();
        });
    });

    //降价通知
    $('.price-down-clock').click(function() {
        $curClock = $(this),

        showNoticeBox();

        curSkn = $curClock.closest('.fav-good').data('id');
    });

    //checkbox【手动触发checkbox点击后跳转链接的地址】
    $('.reduction-filter input[type="checkbox"]').change(function() {
        location.href = $(this).parent().attr('href');
    });

    //降价通知确定按钮
    $('#price-notice').on('click', '.close', function() {
        closeNoticeBox();
    }).on('click', '.price-notice-sure', function() {
        var $phone = $('#notice-phone-num'),
            phone,
            errTxt;

        if ($noticeSuccess.hasClass('hide')) {

            //订阅到手机号
            phone = $.trim($phone.val());

            if (phone === '' || !phoneReg.test(phone)) {
                errTxt = phone === '' ? '请输入手机号码' : '手机号码格式不正确';

                $phone.addClass('error');
                $('#price-notice .err-text').removeClass('hide').find('em').html(errTxt);
                return;
            } else {
                $phone.removeClass('error');
                $('#price-notice .err-text').addClass('hide');
            }

            $.ajax({
                type: 'GET',
                url: '/home/favorite/notice',
                data: {
                    mobile: phone,
                    id: curSkn
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $noticeSuccess.removeClass('hide');
                    $noticeContent.addClass('hide');
                    $noticeSuccess.find('.notice-num').html(data.data.num);

                    //切换商品通知文字
                    $curClock.toggleClass('noticed').text(clockTxt.off);
                }
            });
        } else {

            //关闭窗口
            closeNoticeBox();
        }
    });

    //商品可参加活动
    $('.has-activity').click(function() {
        var $this = $(this),
            $activites = $this.next('.activites'),
            $li = $this.closest('li');

        if ($activites.hasClass('hide')) {

            //显示
            $activites.removeClass('hide');
            $li.css('padding-bottom', $activites.outerHeight());
        } else {

            //隐藏
            $activites.addClass('hide');
            $li.css('padding-bottom', '');
        }
    });
}());


//品牌收藏
(function() {
    var naTpl;
    var naCache = {};

    /**
     * 翻页控制
     * @param $li 当前品牌容器
     * @param flag 翻页方向/-1-pre 1-next
     */
    function pageControll($li, flag) {
        var id = $li.data('id'),
            $ul = $li.find('.na-goods-ul'),
            theCache = naCache[id],
            curPage = theCache.cur ? theCache.cur : 1,
            page = Math.ceil(theCache.total / 5);

        var $pre = $li.find('.na-pre');
        var $next = $li.find('.na-next');

        curPage += flag;

        //初始
        $pre.removeClass('hidden');
        $next.removeClass('hidden');

        //第一页
        if (curPage === 1) {
            $pre.addClass('hidden');
        }

        //最后一页
        if (curPage === page) {
            $next.addClass('hidden');
        }

        $ul.animate({
            marginLeft: -(curPage - 1) * $ul.parent('.na-goods').width()
        }, 200);

        theCache.cur = curPage;
    }

    function setListPadding($li, $naList) {
        $li.css('padding-bottom', $naList.outerHeight() + 10);
    }

    if ($('.fav-brands').length === 0) {
        return;
    }

    naTpl = Handlebars.compile($('#na-tpl').html());

    // 新品到着【重要信息:最多显示20个新品到着商品】
    $('.na-trigger').click(function() {
        var $this = $(this),
            $naList = $this.next('.na-list'),
            $li = $this.closest('li'),
            id;

        if (!$naList.hasClass('hide')) {
            $naList.addClass('hide');
            $li.css('padding-bottom', '');
            return;
        }

        id = $li.data('id');

        if (naCache[id] && naCache[id].cache) {
            $naList.removeClass('hide');
            setListPadding($li, $naList);
            return;
        }

        $.ajax({
            type: 'GET',
            url: '/home/favorite/newproduct',
            data: {
                id: id
            }
        }).then(function(data) {
            var html,
                res,
                tplData;

            if (data.code === 200) {
                res = data.data;
                tplData = res.length === 0 ? {} : {
                    naGoods: res
                };
                html = naTpl(tplData);

                $naList.removeClass('hide').find('.na-content').html(html);
                setListPadding($li, $naList);

                //初始,判断是否显示Next翻页
                if (res.length > 5) {
                    $li.find('.na-next').removeClass('hidden');
                }

                if (!naCache[id]) {
                    naCache[id] = {};
                }

                naCache[id].cache = true;//set cache
                naCache[id].total = res.length;
            }
        });
    });

    $('.na-content').on('click', '.na-pre, .na-next', function() {
        var $this = $(this);

        var flag = $this.hasClass('na-pre') ? -1 : 1;

        pageControll($this.closest('.fav-brand'), flag);
    });
}());

//文章收藏
(function() {
    if ($('.fav-articles').length < 0) {
        return;
    }

    $('.fav-articles .desc').dotdotdot();
}());

//全选【通用】
$('#me-checkall').click(function() {
    $('.checkbox input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});

//删除收藏
$('.del-favorite').click(function(e) {
    var id = $(this).closest('.fav-row').data('id');

    e.preventDefault();

    $.ajax({
        type: 'GET',
        url: '/home/favorite/cancel',
        data: {
            id: id,
            type: location.href.match(/type=(.*)[&]*/)[1]
        }
    }).then(function(data) {
        if (data.code === 200) {
            history.go(0);
        }
    });
});

//删除多个商品/品牌/文章【通用】
$('#me-del-checked').click(function() {
    var ids = [];

    $('.checkbox input[type="checkbox"]:checked').each(function() {
        ids.push($(this).closest('.fav-row').data('id'));
    });

    if (ids.length === 0) {
        return;
    }

    $.ajax({
        type: 'GET',
        url: '/home/favorite/cancel',
        data: {
            ids: ids.join(','),
            type: location.href.match(/type=(.*)[&]*/)[1]
        }
    }).then(function(data) {
        if (data.code === 200) {
            history.go(0);
        }
    });
});